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