Borland makefile needs to define _WINDOWS; apparently this makefile
[u/mdw/putty] / raw.c
CommitLineData
5e1a8e27 1#include <stdio.h>
2#include <stdlib.h>
5e1a8e27 3
4#include "putty.h"
5
6#ifndef FALSE
7#define FALSE 0
8#endif
9#ifndef TRUE
10#define TRUE 1
11#endif
12
5471d09a 13#define RAW_MAX_BACKLOG 4096
14
51470298 15typedef struct raw_backend_data {
16 const struct plug_function_table *fn;
17 /* the above field _must_ be first in the structure */
5e1a8e27 18
51470298 19 Socket s;
20 int bufsize;
21 void *frontend;
22} *Raw;
5e1a8e27 23
51470298 24static void raw_size(void *handle, int width, int height);
25
26static void c_write(Raw raw, char *buf, int len)
32874aea 27{
51470298 28 int backlog = from_backend(raw->frontend, 0, buf, len);
29 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
5e1a8e27 30}
31
32874aea 32static int raw_closing(Plug plug, char *error_msg, int error_code,
33 int calling_back)
34{
51470298 35 Raw raw = (Raw) plug;
36
37 if (raw->s) {
38 sk_close(raw->s);
39 raw->s = NULL;
f3ab576e 40 }
7e78000d 41 if (error_msg) {
32874aea 42 /* A socket error has occurred. */
a8327734 43 logevent(raw->frontend, error_msg);
247308b5 44 connection_fatal("%s", error_msg);
32874aea 45 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 46 return 0;
47}
48
32874aea 49static int raw_receive(Plug plug, int urgent, char *data, int len)
50{
51470298 51 Raw raw = (Raw) plug;
52 c_write(raw, data, len);
8df7a775 53 return 1;
5e1a8e27 54}
55
3ad9d396 56static void raw_sent(Plug plug, int bufsize)
57{
51470298 58 Raw raw = (Raw) plug;
59 raw->bufsize = bufsize;
3ad9d396 60}
61
5e1a8e27 62/*
8df7a775 63 * Called to set up the raw connection.
64 *
5e1a8e27 65 * Returns an error message, or NULL on success.
66 *
6e1ebb76 67 * Also places the canonical host name into `realhost'. It must be
68 * freed by the caller.
5e1a8e27 69 */
51470298 70static char *raw_init(void *frontend_handle, void **backend_handle,
71 char *host, int port, char **realhost, int nodelay)
32874aea 72{
51470298 73 static const struct plug_function_table fn_table = {
7e78000d 74 raw_closing,
3ad9d396 75 raw_receive,
76 raw_sent
51470298 77 };
8df7a775 78 SockAddr addr;
79 char *err;
51470298 80 Raw raw;
5e1a8e27 81
51470298 82 raw = smalloc(sizeof(*raw));
83 raw->fn = &fn_table;
84 raw->s = NULL;
85 *backend_handle = raw;
86
87 raw->frontend = frontend_handle;
887035a5 88
5e1a8e27 89 /*
90 * Try to find host.
91 */
3ad9d396 92 {
93 char buf[200];
94 sprintf(buf, "Looking up host \"%.170s\"", host);
a8327734 95 logevent(raw->frontend, buf);
3ad9d396 96 }
8df7a775 97 addr = sk_namelookup(host, realhost);
32874aea 98 if ((err = sk_addr_error(addr)))
8df7a775 99 return err;
5e1a8e27 100
101 if (port < 0)
102 port = 23; /* default telnet port */
103
104 /*
105 * Open socket.
106 */
3ad9d396 107 {
108 char buf[200], addrbuf[100];
109 sk_getaddr(addr, addrbuf, 100);
110 sprintf(buf, "Connecting to %.100s port %d", addrbuf, port);
a8327734 111 logevent(raw->frontend, buf);
3ad9d396 112 }
51470298 113 raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, (Plug) raw);
114 if ((err = sk_socket_error(raw->s)))
8df7a775 115 return err;
5e1a8e27 116
8df7a775 117 sk_addr_free(addr);
5e1a8e27 118
119 return NULL;
120}
121
122/*
5e1a8e27 123 * Called to send data down the raw connection.
124 */
51470298 125static int raw_send(void *handle, char *buf, int len)
32874aea 126{
51470298 127 Raw raw = (Raw) handle;
128
129 if (raw->s == NULL)
b5a460cd 130 return 0;
5e1a8e27 131
51470298 132 raw->bufsize = sk_write(raw->s, buf, len);
5471d09a 133
51470298 134 return raw->bufsize;
5471d09a 135}
136
137/*
138 * Called to query the current socket sendability status.
139 */
51470298 140static int raw_sendbuffer(void *handle)
5471d09a 141{
51470298 142 Raw raw = (Raw) handle;
143 return raw->bufsize;
5e1a8e27 144}
145
146/*
147 * Called to set the size of the window
148 */
51470298 149static void raw_size(void *handle, int width, int height)
32874aea 150{
5e1a8e27 151 /* Do nothing! */
152 return;
153}
154
155/*
156 * Send raw special codes.
157 */
51470298 158static void raw_special(void *handle, Telnet_Special code)
32874aea 159{
5e1a8e27 160 /* Do nothing! */
161 return;
162}
163
51470298 164static Socket raw_socket(void *handle)
32874aea 165{
51470298 166 Raw raw = (Raw) handle;
167 return raw->s;
32874aea 168}
8ccc75b0 169
51470298 170static int raw_sendok(void *handle)
32874aea 171{
172 return 1;
173}
4017be6d 174
51470298 175static void raw_unthrottle(void *handle, int backlog)
5471d09a 176{
51470298 177 Raw raw = (Raw) handle;
178 sk_set_frozen(raw->s, backlog > RAW_MAX_BACKLOG);
5471d09a 179}
180
51470298 181static int raw_ldisc(void *handle, int option)
32874aea 182{
0965bee0 183 if (option == LD_EDIT || option == LD_ECHO)
32874aea 184 return 1;
0965bee0 185 return 0;
186}
187
b9d7bcad 188static void raw_provide_ldisc(void *handle, void *ldisc)
189{
190 /* This is a stub. */
191}
192
a8327734 193static void raw_provide_logctx(void *handle, void *logctx)
194{
195 /* This is a stub. */
196}
197
51470298 198static int raw_exitcode(void *handle)
d8d6c7e5 199{
200 /* Exit codes are a meaningless concept in the Raw protocol */
201 return 0;
202}
203
5e1a8e27 204Backend raw_backend = {
205 raw_init,
5e1a8e27 206 raw_send,
5471d09a 207 raw_sendbuffer,
5e1a8e27 208 raw_size,
4017be6d 209 raw_special,
8ccc75b0 210 raw_socket,
d8d6c7e5 211 raw_exitcode,
97db3be4 212 raw_sendok,
0965bee0 213 raw_ldisc,
b9d7bcad 214 raw_provide_ldisc,
a8327734 215 raw_provide_logctx,
5471d09a 216 raw_unthrottle,
97db3be4 217 1
5e1a8e27 218};