Ensure all backends _remember_ the connection has closed after
[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 skt, int urgent, char *data, int len) {
28 if (urgent==3) {
29 /* A socket error has occurred. */
30 s = NULL;
31 connection_fatal(data);
32 len = 0;
33 }
34 if (!len) {
35 /* Connection has closed. */
36 sk_close(s);
37 s = NULL;
38 return 0;
39 }
40 c_write(data, len);
41 return 1;
42 }
43
44 /*
45 * Called to set up the raw connection.
46 *
47 * Returns an error message, or NULL on success.
48 *
49 * Also places the canonical host name into `realhost'.
50 */
51 static char *raw_init (char *host, int port, char **realhost) {
52 SockAddr addr;
53 char *err;
54
55 /*
56 * Try to find host.
57 */
58 addr = sk_namelookup(host, realhost);
59 if ( (err = sk_addr_error(addr)) )
60 return err;
61
62 if (port < 0)
63 port = 23; /* default telnet port */
64
65 /*
66 * Open socket.
67 */
68 s = sk_new(addr, port, 0, raw_receive);
69 if ( (err = sk_socket_error(s)) )
70 return err;
71
72 sk_addr_free(addr);
73
74 return NULL;
75 }
76
77 /*
78 * Called to send data down the raw connection.
79 */
80 static void raw_send (char *buf, int len) {
81
82 if (s == NULL)
83 return;
84
85 sk_write(s, buf, len);
86 }
87
88 /*
89 * Called to set the size of the window
90 */
91 static void raw_size(void) {
92 /* Do nothing! */
93 return;
94 }
95
96 /*
97 * Send raw special codes.
98 */
99 static void raw_special (Telnet_Special code) {
100 /* Do nothing! */
101 return;
102 }
103
104 static Socket raw_socket(void) { return s; }
105
106 static int raw_sendok(void) { return 1; }
107
108 static int raw_ldisc(int option) {
109 if (option == LD_EDIT || option == LD_ECHO)
110 return 1;
111 return 0;
112 }
113
114 Backend raw_backend = {
115 raw_init,
116 raw_send,
117 raw_size,
118 raw_special,
119 raw_socket,
120 raw_sendok,
121 raw_ldisc,
122 1
123 };