Improve socket error handling so that a socket error isn't an
[u/mdw/putty] / raw.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 raw_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 raw_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 c_write(data, len);
40 return 1;
41 }
42
43 /*
44 * Called to set up the raw connection.
45 *
46 * Returns an error message, or NULL on success.
47 *
48 * Also places the canonical host name into `realhost'.
49 */
50 static char *raw_init (char *host, int port, char **realhost) {
51 SockAddr addr;
52 char *err;
53
54 /*
55 * Try to find host.
56 */
57 addr = sk_namelookup(host, realhost);
58 if ( (err = sk_addr_error(addr)) )
59 return err;
60
61 if (port < 0)
62 port = 23; /* default telnet port */
63
64 /*
65 * Open socket.
66 */
67 s = sk_new(addr, port, 0, raw_receive);
68 if ( (err = sk_socket_error(s)) )
69 return err;
70
71 sk_addr_free(addr);
72
73 /*
74 * We have no pre-session phase.
75 */
76 begin_session();
77
78 return NULL;
79 }
80
81 /*
82 * Called to send data down the raw connection.
83 */
84 static void raw_send (char *buf, int len) {
85
86 if (s == NULL)
87 return;
88
89 sk_write(s, buf, len);
90 }
91
92 /*
93 * Called to set the size of the window
94 */
95 static void raw_size(void) {
96 /* Do nothing! */
97 return;
98 }
99
100 /*
101 * Send raw special codes.
102 */
103 static void raw_special (Telnet_Special code) {
104 /* Do nothing! */
105 return;
106 }
107
108 static Socket raw_socket(void) { return s; }
109
110 static int raw_sendok(void) { return 1; }
111
112 Backend raw_backend = {
113 raw_init,
114 raw_send,
115 raw_size,
116 raw_special,
117 raw_socket,
118 raw_sendok,
119 1
120 };