c91409da |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | |
5 | #include "putty.h" |
6 | |
7 | #ifndef FALSE |
8 | #define FALSE 0 |
9 | #endif |
10 | #ifndef TRUE |
11 | #define TRUE 1 |
12 | #endif |
13 | |
14 | static Socket s = NULL; |
15 | |
16 | static void rlogin_size(void); |
17 | |
18 | static int sb_opt, sb_len; |
19 | static char *sb_buf = NULL; |
20 | static int sb_size = 0; |
21 | #define SB_DELTA 1024 |
22 | |
23 | static void c_write (char *buf, int len) { |
24 | from_backend(0, buf, len); |
25 | } |
26 | |
7dbb9f56 |
27 | static int rlogin_receive (Socket skt, int urgent, char *data, int len) { |
2c94fd1c |
28 | if (urgent==3) { |
29 | /* A socket error has occurred. */ |
7dbb9f56 |
30 | s = NULL; |
2c94fd1c |
31 | connection_fatal(data); |
32 | len = 0; |
33 | } |
c91409da |
34 | if (!len) { |
35 | /* Connection has closed. */ |
36 | sk_close(s); |
37 | s = NULL; |
38 | return 0; |
39 | } |
40 | if (urgent == 2) { |
41 | char c; |
c91409da |
42 | |
43 | c = *data++; len--; |
44 | if (c == 0x80) |
45 | rlogin_size(); |
46 | /* |
47 | * We should flush everything (aka Telnet SYNCH) if we see |
48 | * 0x02, and we should turn off and on _local_ flow control |
49 | * on 0x10 and 0x20 respectively. I'm not convinced it's |
50 | * worth it... |
51 | */ |
52 | } |
53 | c_write(data, len); |
54 | return 1; |
55 | } |
56 | |
57 | /* |
58 | * Called to set up the rlogin connection. |
59 | * |
60 | * Returns an error message, or NULL on success. |
61 | * |
62 | * Also places the canonical host name into `realhost'. |
63 | */ |
64 | static char *rlogin_init (char *host, int port, char **realhost) { |
65 | SockAddr addr; |
66 | char *err; |
67 | |
68 | /* |
69 | * Try to find host. |
70 | */ |
71 | addr = sk_namelookup(host, realhost); |
72 | if ( (err = sk_addr_error(addr)) ) |
73 | return err; |
74 | |
75 | if (port < 0) |
76 | port = 513; /* default rlogin port */ |
77 | |
78 | /* |
79 | * Open socket. |
80 | */ |
81 | s = sk_new(addr, port, 1, rlogin_receive); |
82 | if ( (err = sk_socket_error(s)) ) |
83 | return err; |
84 | |
85 | sk_addr_free(addr); |
86 | |
87 | /* |
88 | * Send local username, remote username, terminal/speed |
89 | */ |
90 | |
91 | { |
92 | char z = 0; |
93 | char *p; |
94 | sk_write(s, &z, 1); |
95 | sk_write(s, cfg.localusername, strlen(cfg.localusername)); |
96 | sk_write(s, &z, 1); |
97 | sk_write(s, cfg.username, strlen(cfg.username)); |
98 | sk_write(s, &z, 1); |
99 | sk_write(s, cfg.termtype, strlen(cfg.termtype)); |
100 | sk_write(s, "/", 1); |
101 | for(p = cfg.termspeed; isdigit(*p); p++); |
102 | sk_write(s, cfg.termspeed, p - cfg.termspeed); |
103 | sk_write(s, &z, 1); |
104 | } |
105 | |
c91409da |
106 | return NULL; |
107 | } |
108 | |
109 | /* |
110 | * Called to send data down the rlogin connection. |
111 | */ |
112 | static void rlogin_send (char *buf, int len) { |
113 | |
114 | if (s == NULL) |
115 | return; |
116 | |
117 | sk_write(s, buf, len); |
118 | } |
119 | |
120 | /* |
121 | * Called to set the size of the window |
122 | */ |
123 | static void rlogin_size(void) { |
e418595a |
124 | char b[12] = { '\xFF', '\xFF', 0x73, 0x73, 0, 0, 0, 0, 0, 0, 0, 0 }; |
c91409da |
125 | |
126 | b[6] = cols >> 8; b[7] = cols & 0xFF; |
127 | b[4] = rows >> 8; b[5] = rows & 0xFF; |
128 | sk_write(s, b, 12); |
129 | return; |
130 | } |
131 | |
132 | /* |
133 | * Send rlogin special codes. |
134 | */ |
135 | static void rlogin_special (Telnet_Special code) { |
136 | /* Do nothing! */ |
137 | return; |
138 | } |
139 | |
140 | static Socket rlogin_socket(void) { return s; } |
141 | |
142 | static int rlogin_sendok(void) { return 1; } |
143 | |
0965bee0 |
144 | static int rlogin_ldisc(int option) { |
145 | return 0; |
146 | } |
147 | |
c91409da |
148 | Backend rlogin_backend = { |
149 | rlogin_init, |
150 | rlogin_send, |
151 | rlogin_size, |
152 | rlogin_special, |
153 | rlogin_socket, |
154 | rlogin_sendok, |
0965bee0 |
155 | rlogin_ldisc, |
c91409da |
156 | 1 |
157 | }; |