Jordan Russell's patch (3rd of several). We now don't call TermOut()
[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. */
36 connection_fatal(error_msg);
37 } /* Otherwise, the remote side closed the connection normally. */
7e78000d 38 return 0;
39}
40
32874aea 41static int raw_receive(Plug plug, int urgent, char *data, int len)
42{
8df7a775 43 c_write(data, len);
44 return 1;
5e1a8e27 45}
46
3ad9d396 47static void raw_sent(Plug plug, int bufsize)
48{
49 raw_bufsize = bufsize;
50}
51
5e1a8e27 52/*
8df7a775 53 * Called to set up the raw connection.
54 *
5e1a8e27 55 * Returns an error message, or NULL on success.
56 *
6e1ebb76 57 * Also places the canonical host name into `realhost'. It must be
58 * freed by the caller.
5e1a8e27 59 */
2184a5d9 60static char *raw_init(char *host, int port, char **realhost, int nodelay)
32874aea 61{
7e78000d 62 static struct plug_function_table fn_table = {
63 raw_closing,
3ad9d396 64 raw_receive,
65 raw_sent
7e78000d 66 }, *fn_table_ptr = &fn_table;
67
8df7a775 68 SockAddr addr;
69 char *err;
5e1a8e27 70
71 /*
72 * Try to find host.
73 */
3ad9d396 74 {
75 char buf[200];
76 sprintf(buf, "Looking up host \"%.170s\"", host);
77 logevent(buf);
78 }
8df7a775 79 addr = sk_namelookup(host, realhost);
32874aea 80 if ((err = sk_addr_error(addr)))
8df7a775 81 return err;
5e1a8e27 82
83 if (port < 0)
84 port = 23; /* default telnet port */
85
86 /*
87 * Open socket.
88 */
3ad9d396 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 }
2184a5d9 95 s = sk_new(addr, port, 0, 1, nodelay, &fn_table_ptr);
32874aea 96 if ((err = sk_socket_error(s)))
8df7a775 97 return err;
5e1a8e27 98
8df7a775 99 sk_addr_free(addr);
5e1a8e27 100
101 return NULL;
102}
103
104/*
5e1a8e27 105 * Called to send data down the raw connection.
106 */
5471d09a 107static int raw_send(char *buf, int len)
32874aea 108{
8df7a775 109 if (s == NULL)
b5a460cd 110 return 0;
5e1a8e27 111
5471d09a 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 */
120static int raw_sendbuffer(void)
121{
122 return raw_bufsize;
5e1a8e27 123}
124
125/*
126 * Called to set the size of the window
127 */
32874aea 128static void raw_size(void)
129{
5e1a8e27 130 /* Do nothing! */
131 return;
132}
133
134/*
135 * Send raw special codes.
136 */
32874aea 137static void raw_special(Telnet_Special code)
138{
5e1a8e27 139 /* Do nothing! */
140 return;
141}
142
32874aea 143static Socket raw_socket(void)
144{
145 return s;
146}
8ccc75b0 147
32874aea 148static int raw_sendok(void)
149{
150 return 1;
151}
4017be6d 152
5471d09a 153static void raw_unthrottle(int backlog)
154{
155 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
156}
157
32874aea 158static int raw_ldisc(int option)
159{
0965bee0 160 if (option == LD_EDIT || option == LD_ECHO)
32874aea 161 return 1;
0965bee0 162 return 0;
163}
164
5e1a8e27 165Backend raw_backend = {
166 raw_init,
5e1a8e27 167 raw_send,
5471d09a 168 raw_sendbuffer,
5e1a8e27 169 raw_size,
4017be6d 170 raw_special,
8ccc75b0 171 raw_socket,
97db3be4 172 raw_sendok,
0965bee0 173 raw_ldisc,
5471d09a 174 raw_unthrottle,
97db3be4 175 1
5e1a8e27 176};