Extensive changes that _should_ fix the socket buffering problems,
[sgt/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 #define RAW_MAX_BACKLOG 4096
15
16 static Socket s = NULL;
17 static int raw_bufsize;
18
19 static void raw_size(void);
20
21 static void c_write(char *buf, int len)
22 {
23 int backlog = from_backend(0, buf, len);
24 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
25 }
26
27 static int raw_closing(Plug plug, char *error_msg, int error_code,
28 int calling_back)
29 {
30 if (s) {
31 sk_close(s);
32 s = NULL;
33 }
34 if (error_msg) {
35 /* A socket error has occurred. */
36 connection_fatal(error_msg);
37 } /* Otherwise, the remote side closed the connection normally. */
38 return 0;
39 }
40
41 static int raw_receive(Plug plug, int urgent, char *data, int len)
42 {
43 c_write(data, len);
44 return 1;
45 }
46
47 /*
48 * Called to set up the raw connection.
49 *
50 * Returns an error message, or NULL on success.
51 *
52 * Also places the canonical host name into `realhost'. It must be
53 * freed by the caller.
54 */
55 static char *raw_init(char *host, int port, char **realhost)
56 {
57 static struct plug_function_table fn_table = {
58 raw_closing,
59 raw_receive
60 }, *fn_table_ptr = &fn_table;
61
62 SockAddr addr;
63 char *err;
64
65 /*
66 * Try to find host.
67 */
68 addr = sk_namelookup(host, realhost);
69 if ((err = sk_addr_error(addr)))
70 return err;
71
72 if (port < 0)
73 port = 23; /* default telnet port */
74
75 /*
76 * Open socket.
77 */
78 s = sk_new(addr, port, 0, 1, &fn_table_ptr);
79 if ((err = sk_socket_error(s)))
80 return err;
81
82 sk_addr_free(addr);
83
84 return NULL;
85 }
86
87 /*
88 * Called to send data down the raw connection.
89 */
90 static int raw_send(char *buf, int len)
91 {
92
93 if (s == NULL)
94 return;
95
96 raw_bufsize = sk_write(s, buf, len);
97
98 return raw_bufsize;
99 }
100
101 /*
102 * Called to query the current socket sendability status.
103 */
104 static int raw_sendbuffer(void)
105 {
106 return raw_bufsize;
107 }
108
109 /*
110 * Called to set the size of the window
111 */
112 static void raw_size(void)
113 {
114 /* Do nothing! */
115 return;
116 }
117
118 /*
119 * Send raw special codes.
120 */
121 static void raw_special(Telnet_Special code)
122 {
123 /* Do nothing! */
124 return;
125 }
126
127 static Socket raw_socket(void)
128 {
129 return s;
130 }
131
132 static int raw_sendok(void)
133 {
134 return 1;
135 }
136
137 static void raw_unthrottle(int backlog)
138 {
139 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
140 }
141
142 static int raw_ldisc(int option)
143 {
144 if (option == LD_EDIT || option == LD_ECHO)
145 return 1;
146 return 0;
147 }
148
149 Backend raw_backend = {
150 raw_init,
151 raw_send,
152 raw_sendbuffer,
153 raw_size,
154 raw_special,
155 raw_socket,
156 raw_sendok,
157 raw_ldisc,
158 raw_unthrottle,
159 1
160 };