Attempt to ensure that everything passed to connection_fatal() is
[u/mdw/putty] / raw.c
... / ...
CommitLineData
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
16static Socket s = NULL;
17static int raw_bufsize;
18
19static void raw_size(void);
20
21static 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
27static 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 logevent(error_msg);
37 connection_fatal("%s", error_msg);
38 } /* Otherwise, the remote side closed the connection normally. */
39 return 0;
40}
41
42static int raw_receive(Plug plug, int urgent, char *data, int len)
43{
44 c_write(data, len);
45 return 1;
46}
47
48static void raw_sent(Plug plug, int bufsize)
49{
50 raw_bufsize = bufsize;
51}
52
53/*
54 * Called to set up the raw connection.
55 *
56 * Returns an error message, or NULL on success.
57 *
58 * Also places the canonical host name into `realhost'. It must be
59 * freed by the caller.
60 */
61static char *raw_init(char *host, int port, char **realhost, int nodelay)
62{
63 static struct plug_function_table fn_table = {
64 raw_closing,
65 raw_receive,
66 raw_sent
67 }, *fn_table_ptr = &fn_table;
68
69 SockAddr addr;
70 char *err;
71
72 /*
73 * Try to find host.
74 */
75 {
76 char buf[200];
77 sprintf(buf, "Looking up host \"%.170s\"", host);
78 logevent(buf);
79 }
80 addr = sk_namelookup(host, realhost);
81 if ((err = sk_addr_error(addr)))
82 return err;
83
84 if (port < 0)
85 port = 23; /* default telnet port */
86
87 /*
88 * Open socket.
89 */
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 }
96 s = new_connection(addr, *realhost, port, 0, 1, nodelay, &fn_table_ptr);
97 if ((err = sk_socket_error(s)))
98 return err;
99
100 sk_addr_free(addr);
101
102 return NULL;
103}
104
105/*
106 * Called to send data down the raw connection.
107 */
108static int raw_send(char *buf, int len)
109{
110 if (s == NULL)
111 return 0;
112
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;
124}
125
126/*
127 * Called to set the size of the window
128 */
129static void raw_size(void)
130{
131 /* Do nothing! */
132 return;
133}
134
135/*
136 * Send raw special codes.
137 */
138static void raw_special(Telnet_Special code)
139{
140 /* Do nothing! */
141 return;
142}
143
144static Socket raw_socket(void)
145{
146 return s;
147}
148
149static int raw_sendok(void)
150{
151 return 1;
152}
153
154static void raw_unthrottle(int backlog)
155{
156 sk_set_frozen(s, backlog > RAW_MAX_BACKLOG);
157}
158
159static int raw_ldisc(int option)
160{
161 if (option == LD_EDIT || option == LD_ECHO)
162 return 1;
163 return 0;
164}
165
166static int raw_exitcode(void)
167{
168 /* Exit codes are a meaningless concept in the Raw protocol */
169 return 0;
170}
171
172Backend raw_backend = {
173 raw_init,
174 raw_send,
175 raw_sendbuffer,
176 raw_size,
177 raw_special,
178 raw_socket,
179 raw_exitcode,
180 raw_sendok,
181 raw_ldisc,
182 raw_unthrottle,
183 1
184};