Configurable TCP_NODELAY option on network connections
[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 #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 static void raw_sent(Plug plug, int bufsize)
48 {
49 raw_bufsize = bufsize;
50 }
51
52 /*
53 * Called to set up the raw connection.
54 *
55 * Returns an error message, or NULL on success.
56 *
57 * Also places the canonical host name into `realhost'. It must be
58 * freed by the caller.
59 */
60 static char *raw_init(char *host, int port, char **realhost, int nodelay)
61 {
62 static struct plug_function_table fn_table = {
63 raw_closing,
64 raw_receive,
65 raw_sent
66 }, *fn_table_ptr = &fn_table;
67
68 SockAddr addr;
69 char *err;
70
71 /*
72 * Try to find host.
73 */
74 {
75 char buf[200];
76 sprintf(buf, "Looking up host \"%.170s\"", host);
77 logevent(buf);
78 }
79 addr = sk_namelookup(host, realhost);
80 if ((err = sk_addr_error(addr)))
81 return err;
82
83 if (port < 0)
84 port = 23; /* default telnet port */
85
86 /*
87 * Open socket.
88 */
89 {
90 char buf[200], addrbuf[100];
91 sk_getaddr(addr, addrbuf, 100);
92 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
93 logevent(buf);
94 }
95 s = sk_new(addr, port, 0, 1, nodelay, &fn_table_ptr);
96 if ((err = sk_socket_error(s)))
97 return err;
98
99 sk_addr_free(addr);
100
101 return NULL;
102 }
103
104 /*
105 * Called to send data down the raw connection.
106 */
107 static int raw_send(char *buf, int len)
108 {
109 if (s == NULL)
110 return 0;
111
112 raw_bufsize = sk_write(s, buf, len);
113
114 return raw_bufsize;
115 }
116
117 /*
118 * Called to query the current socket sendability status.
119 */
120 static int raw_sendbuffer(void)
121 {
122 return raw_bufsize;
123 }
124
125 /*
126 * Called to set the size of the window
127 */
128 static void raw_size(void)
129 {
130 /* Do nothing! */
131 return;
132 }
133
134 /*
135 * Send raw special codes.
136 */
137 static void raw_special(Telnet_Special code)
138 {
139 /* Do nothing! */
140 return;
141 }
142
143 static Socket raw_socket(void)
144 {
145 return s;
146 }
147
148 static int raw_sendok(void)
149 {
150 return 1;
151 }
152
153 static void raw_unthrottle(int backlog)
154 {
155 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
156 }
157
158 static int raw_ldisc(int option)
159 {
160 if (option == LD_EDIT || option == LD_ECHO)
161 return 1;
162 return 0;
163 }
164
165 Backend raw_backend = {
166 raw_init,
167 raw_send,
168 raw_sendbuffer,
169 raw_size,
170 raw_special,
171 raw_socket,
172 raw_sendok,
173 raw_ldisc,
174 raw_unthrottle,
175 1
176 };