Port forwarding bug fix: we were unable to handle receiving
[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
47/*
8df7a775 48 * Called to set up the raw connection.
49 *
5e1a8e27 50 * Returns an error message, or NULL on success.
51 *
6e1ebb76 52 * Also places the canonical host name into `realhost'. It must be
53 * freed by the caller.
5e1a8e27 54 */
32874aea 55static char *raw_init(char *host, int port, char **realhost)
56{
7e78000d 57 static struct plug_function_table fn_table = {
58 raw_closing,
59 raw_receive
60 }, *fn_table_ptr = &fn_table;
61
8df7a775 62 SockAddr addr;
63 char *err;
5e1a8e27 64
65 /*
66 * Try to find host.
67 */
8df7a775 68 addr = sk_namelookup(host, realhost);
32874aea 69 if ((err = sk_addr_error(addr)))
8df7a775 70 return err;
5e1a8e27 71
72 if (port < 0)
73 port = 23; /* default telnet port */
74
75 /*
76 * Open socket.
77 */
7e78000d 78 s = sk_new(addr, port, 0, 1, &fn_table_ptr);
32874aea 79 if ((err = sk_socket_error(s)))
8df7a775 80 return err;
5e1a8e27 81
8df7a775 82 sk_addr_free(addr);
5e1a8e27 83
84 return NULL;
85}
86
87/*
5e1a8e27 88 * Called to send data down the raw connection.
89 */
5471d09a 90static int raw_send(char *buf, int len)
32874aea 91{
5e1a8e27 92
8df7a775 93 if (s == NULL)
5e1a8e27 94 return;
95
5471d09a 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 */
104static int raw_sendbuffer(void)
105{
106 return raw_bufsize;
5e1a8e27 107}
108
109/*
110 * Called to set the size of the window
111 */
32874aea 112static void raw_size(void)
113{
5e1a8e27 114 /* Do nothing! */
115 return;
116}
117
118/*
119 * Send raw special codes.
120 */
32874aea 121static void raw_special(Telnet_Special code)
122{
5e1a8e27 123 /* Do nothing! */
124 return;
125}
126
32874aea 127static Socket raw_socket(void)
128{
129 return s;
130}
8ccc75b0 131
32874aea 132static int raw_sendok(void)
133{
134 return 1;
135}
4017be6d 136
5471d09a 137static void raw_unthrottle(int backlog)
138{
139 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
140}
141
32874aea 142static int raw_ldisc(int option)
143{
0965bee0 144 if (option == LD_EDIT || option == LD_ECHO)
32874aea 145 return 1;
0965bee0 146 return 0;
147}
148
5e1a8e27 149Backend raw_backend = {
150 raw_init,
5e1a8e27 151 raw_send,
5471d09a 152 raw_sendbuffer,
5e1a8e27 153 raw_size,
4017be6d 154 raw_special,
8ccc75b0 155 raw_socket,
97db3be4 156 raw_sendok,
0965bee0 157 raw_ldisc,
5471d09a 158 raw_unthrottle,
97db3be4 159 1
5e1a8e27 160};