Make -ut work the right way round! :-)
[u/mdw/putty] / raw.c
CommitLineData
5e1a8e27 1#include <windows.h>
2#include <stdio.h>
3#include <stdlib.h>
5e1a8e27 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
5471d09a 14#define RAW_MAX_BACKLOG 4096
15
8df7a775 16static Socket s = NULL;
5471d09a 17static int raw_bufsize;
5e1a8e27 18
5e1a8e27 19static void raw_size(void);
20
32874aea 21static void c_write(char *buf, int len)
22{
5471d09a 23 int backlog = from_backend(0, buf, len);
24 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
5e1a8e27 25}
26
32874aea 27static int raw_closing(Plug plug, char *error_msg, int error_code,
28 int calling_back)
29{
f3ab576e 30 if (s) {
31 sk_close(s);
32 s = NULL;
33 }
7e78000d 34 if (error_msg) {
32874aea 35 /* A socket error has occurred. */
247308b5 36 logevent(error_msg);
37 connection_fatal("%s", error_msg);
32874aea 38 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 39 return 0;
40}
41
32874aea 42static int raw_receive(Plug plug, int urgent, char *data, int len)
43{
8df7a775 44 c_write(data, len);
45 return 1;
5e1a8e27 46}
47
3ad9d396 48static void raw_sent(Plug plug, int bufsize)
49{
50 raw_bufsize = bufsize;
51}
52
5e1a8e27 53/*
8df7a775 54 * Called to set up the raw connection.
55 *
5e1a8e27 56 * Returns an error message, or NULL on success.
57 *
6e1ebb76 58 * Also places the canonical host name into `realhost'. It must be
59 * freed by the caller.
5e1a8e27 60 */
2184a5d9 61static char *raw_init(char *host, int port, char **realhost, int nodelay)
32874aea 62{
7e78000d 63 static struct plug_function_table fn_table = {
64 raw_closing,
3ad9d396 65 raw_receive,
66 raw_sent
7e78000d 67 }, *fn_table_ptr = &fn_table;
68
8df7a775 69 SockAddr addr;
70 char *err;
5e1a8e27 71
72 /*
73 * Try to find host.
74 */
3ad9d396 75 {
76 char buf[200];
77 sprintf(buf, "Looking up host \"%.170s\"", host);
78 logevent(buf);
79 }
8df7a775 80 addr = sk_namelookup(host, realhost);
32874aea 81 if ((err = sk_addr_error(addr)))
8df7a775 82 return err;
5e1a8e27 83
84 if (port < 0)
85 port = 23; /* default telnet port */
86
87 /*
88 * Open socket.
89 */
3ad9d396 90 {
91 char buf[200], addrbuf[100];
92 sk_getaddr(addr, addrbuf, 100);
93 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
94 logevent(buf);
95 }
8eebd221 96 s = new_connection(addr, *realhost, port, 0, 1, nodelay, &fn_table_ptr);
32874aea 97 if ((err = sk_socket_error(s)))
8df7a775 98 return err;
5e1a8e27 99
8df7a775 100 sk_addr_free(addr);
5e1a8e27 101
102 return NULL;
103}
104
105/*
5e1a8e27 106 * Called to send data down the raw connection.
107 */
5471d09a 108static int raw_send(char *buf, int len)
32874aea 109{
8df7a775 110 if (s == NULL)
b5a460cd 111 return 0;
5e1a8e27 112
5471d09a 113 raw_bufsize = sk_write(s, buf, len);
114
115 return raw_bufsize;
116}
117
118/*
119 * Called to query the current socket sendability status.
120 */
121static int raw_sendbuffer(void)
122{
123 return raw_bufsize;
5e1a8e27 124}
125
126/*
127 * Called to set the size of the window
128 */
32874aea 129static void raw_size(void)
130{
5e1a8e27 131 /* Do nothing! */
132 return;
133}
134
135/*
136 * Send raw special codes.
137 */
32874aea 138static void raw_special(Telnet_Special code)
139{
5e1a8e27 140 /* Do nothing! */
141 return;
142}
143
32874aea 144static Socket raw_socket(void)
145{
146 return s;
147}
8ccc75b0 148
32874aea 149static int raw_sendok(void)
150{
151 return 1;
152}
4017be6d 153
5471d09a 154static void raw_unthrottle(int backlog)
155{
156 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
157}
158
32874aea 159static int raw_ldisc(int option)
160{
0965bee0 161 if (option == LD_EDIT || option == LD_ECHO)
32874aea 162 return 1;
0965bee0 163 return 0;
164}
165
d8d6c7e5 166static int raw_exitcode(void)
167{
168 /* Exit codes are a meaningless concept in the Raw protocol */
169 return 0;
170}
171
5e1a8e27 172Backend raw_backend = {
173 raw_init,
5e1a8e27 174 raw_send,
5471d09a 175 raw_sendbuffer,
5e1a8e27 176 raw_size,
4017be6d 177 raw_special,
8ccc75b0 178 raw_socket,
d8d6c7e5 179 raw_exitcode,
97db3be4 180 raw_sendok,
0965bee0 181 raw_ldisc,
5471d09a 182 raw_unthrottle,
97db3be4 183 1
5e1a8e27 184};