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; |
5e1a8e27 |
18 | |
5e1a8e27 |
19 | static void raw_size(void); |
20 | |
32874aea |
21 | static 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 |
27 | static 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 |
41 | static 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 |
55 | static 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 |
90 | static int raw_send(char *buf, int len) |
32874aea |
91 | { |
8df7a775 |
92 | if (s == NULL) |
b5a460cd |
93 | return 0; |
5e1a8e27 |
94 | |
5471d09a |
95 | raw_bufsize = sk_write(s, buf, len); |
96 | |
97 | return raw_bufsize; |
98 | } |
99 | |
100 | /* |
101 | * Called to query the current socket sendability status. |
102 | */ |
103 | static int raw_sendbuffer(void) |
104 | { |
105 | return raw_bufsize; |
5e1a8e27 |
106 | } |
107 | |
108 | /* |
109 | * Called to set the size of the window |
110 | */ |
32874aea |
111 | static void raw_size(void) |
112 | { |
5e1a8e27 |
113 | /* Do nothing! */ |
114 | return; |
115 | } |
116 | |
117 | /* |
118 | * Send raw special codes. |
119 | */ |
32874aea |
120 | static void raw_special(Telnet_Special code) |
121 | { |
5e1a8e27 |
122 | /* Do nothing! */ |
123 | return; |
124 | } |
125 | |
32874aea |
126 | static Socket raw_socket(void) |
127 | { |
128 | return s; |
129 | } |
8ccc75b0 |
130 | |
32874aea |
131 | static int raw_sendok(void) |
132 | { |
133 | return 1; |
134 | } |
4017be6d |
135 | |
5471d09a |
136 | static void raw_unthrottle(int backlog) |
137 | { |
138 | sk_set_frozen(s, backlog > RAW_MAX_BACKLOG); |
139 | } |
140 | |
32874aea |
141 | static int raw_ldisc(int option) |
142 | { |
0965bee0 |
143 | if (option == LD_EDIT || option == LD_ECHO) |
32874aea |
144 | return 1; |
0965bee0 |
145 | return 0; |
146 | } |
147 | |
5e1a8e27 |
148 | Backend raw_backend = { |
149 | raw_init, |
5e1a8e27 |
150 | raw_send, |
5471d09a |
151 | raw_sendbuffer, |
5e1a8e27 |
152 | raw_size, |
4017be6d |
153 | raw_special, |
8ccc75b0 |
154 | raw_socket, |
97db3be4 |
155 | raw_sendok, |
0965bee0 |
156 | raw_ldisc, |
5471d09a |
157 | raw_unthrottle, |
97db3be4 |
158 | 1 |
5e1a8e27 |
159 | }; |