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