Yet another possible segfault path in the backends fixed. I don't
[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 void c_write(char *buf, int len)
19 {
20 from_backend(0, buf, len);
21 }
22
23 static int raw_closing(Plug plug, char *error_msg, int error_code,
24 int calling_back)
25 {
26 if (s) {
27 sk_close(s);
28 s = NULL;
29 }
30 if (error_msg) {
31 /* A socket error has occurred. */
32 connection_fatal(error_msg);
33 } /* Otherwise, the remote side closed the connection normally. */
34 return 0;
35 }
36
37 static int raw_receive(Plug plug, int urgent, char *data, int len)
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'. It must be
49 * freed by the caller.
50 */
51 static char *raw_init(char *host, int port, char **realhost)
52 {
53 static struct plug_function_table fn_table = {
54 raw_closing,
55 raw_receive
56 }, *fn_table_ptr = &fn_table;
57
58 SockAddr addr;
59 char *err;
60
61 /*
62 * Try to find host.
63 */
64 addr = sk_namelookup(host, realhost);
65 if ((err = sk_addr_error(addr)))
66 return err;
67
68 if (port < 0)
69 port = 23; /* default telnet port */
70
71 /*
72 * Open socket.
73 */
74 s = sk_new(addr, port, 0, 1, &fn_table_ptr);
75 if ((err = sk_socket_error(s)))
76 return err;
77
78 sk_addr_free(addr);
79
80 return NULL;
81 }
82
83 /*
84 * Called to send data down the raw connection.
85 */
86 static void raw_send(char *buf, int len)
87 {
88
89 if (s == NULL)
90 return;
91
92 sk_write(s, buf, len);
93 }
94
95 /*
96 * Called to set the size of the window
97 */
98 static void raw_size(void)
99 {
100 /* Do nothing! */
101 return;
102 }
103
104 /*
105 * Send raw special codes.
106 */
107 static void raw_special(Telnet_Special code)
108 {
109 /* Do nothing! */
110 return;
111 }
112
113 static Socket raw_socket(void)
114 {
115 return s;
116 }
117
118 static int raw_sendok(void)
119 {
120 return 1;
121 }
122
123 static int raw_ldisc(int option)
124 {
125 if (option == LD_EDIT || option == LD_ECHO)
126 return 1;
127 return 0;
128 }
129
130 Backend raw_backend = {
131 raw_init,
132 raw_send,
133 raw_size,
134 raw_special,
135 raw_socket,
136 raw_sendok,
137 raw_ldisc,
138 1
139 };