2 * Network proxy abstraction in PuTTY
4 * A proxy layer, if necessary, wedges itself between the network
5 * code and the higher level backend.
10 #define DEFINE_PLUG_METHOD_MACROS
16 * Call this when proxy negotiation is complete, so that this
17 * socket can begin working normally.
19 void proxy_activate (Proxy_Socket p
)
24 p
->state
= PROXY_STATE_ACTIVE
;
26 /* let's try to keep extra receive events from coming through */
27 sk_set_frozen(p
->sub_socket
, 1);
29 /* send buffered OOB writes */
30 while (bufchain_size(&p
->pending_oob_output_data
) > 0) {
31 bufchain_prefix(&p
->pending_oob_output_data
, &data
, &len
);
32 sk_write_oob(p
->sub_socket
, data
, len
);
33 bufchain_consume(&p
->pending_oob_output_data
, len
);
35 bufchain_clear(&p
->pending_oob_output_data
);
37 /* send buffered normal writes */
38 while (bufchain_size(&p
->pending_output_data
) > 0) {
39 bufchain_prefix(&p
->pending_output_data
, &data
, &len
);
40 sk_write(p
->sub_socket
, data
, len
);
41 bufchain_consume(&p
->pending_output_data
, len
);
43 bufchain_clear(&p
->pending_output_data
);
45 /* if we were asked to flush the output during
46 * the proxy negotiation process, do so now.
48 if (p
->pending_flush
) sk_flush(p
->sub_socket
);
50 /* forward buffered recv data to the backend */
51 while (bufchain_size(&p
->pending_input_data
) > 0) {
52 bufchain_prefix(&p
->pending_input_data
, &data
, &len
);
53 plug_receive(p
->plug
, 0, data
, len
);
54 bufchain_consume(&p
->pending_input_data
, len
);
56 bufchain_clear(&p
->pending_input_data
);
58 /* now set the underlying socket to whatever freeze state they wanted */
59 sk_set_frozen(p
->sub_socket
, p
->freeze
);
62 /* basic proxy socket functions */
64 static Plug
sk_proxy_plug (Socket s
, Plug p
)
66 Proxy_Socket ps
= (Proxy_Socket
) s
;
73 static void sk_proxy_close (Socket s
)
75 Proxy_Socket ps
= (Proxy_Socket
) s
;
77 sk_close(ps
->sub_socket
);
81 static int sk_proxy_write (Socket s
, char *data
, int len
)
83 Proxy_Socket ps
= (Proxy_Socket
) s
;
85 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
86 bufchain_add(&ps
->pending_output_data
, data
, len
);
87 return bufchain_size(&ps
->pending_output_data
);
89 return sk_write(ps
->sub_socket
, data
, len
);
92 static int sk_proxy_write_oob (Socket s
, char *data
, int len
)
94 Proxy_Socket ps
= (Proxy_Socket
) s
;
96 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
97 bufchain_clear(&ps
->pending_output_data
);
98 bufchain_clear(&ps
->pending_oob_output_data
);
99 bufchain_add(&ps
->pending_oob_output_data
, data
, len
);
102 return sk_write_oob(ps
->sub_socket
, data
, len
);
105 static void sk_proxy_flush (Socket s
)
107 Proxy_Socket ps
= (Proxy_Socket
) s
;
109 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
110 ps
->pending_flush
= 1;
113 sk_flush(ps
->sub_socket
);
116 static void sk_proxy_set_private_ptr (Socket s
, void *ptr
)
118 Proxy_Socket ps
= (Proxy_Socket
) s
;
119 sk_set_private_ptr(ps
->sub_socket
, ptr
);
122 static void * sk_proxy_get_private_ptr (Socket s
)
124 Proxy_Socket ps
= (Proxy_Socket
) s
;
125 return sk_get_private_ptr(ps
->sub_socket
);
128 static void sk_proxy_set_frozen (Socket s
, int is_frozen
)
130 Proxy_Socket ps
= (Proxy_Socket
) s
;
132 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
133 ps
->freeze
= is_frozen
;
136 sk_set_frozen(ps
->sub_socket
, is_frozen
);
139 static char * sk_proxy_socket_error (Socket s
)
141 Proxy_Socket ps
= (Proxy_Socket
) s
;
142 if (ps
->error
!= NULL
|| ps
->sub_socket
== NULL
) {
145 return sk_socket_error(ps
->sub_socket
);
148 /* basic proxy plug functions */
150 static int plug_proxy_closing (Plug p
, char *error_msg
,
151 int error_code
, int calling_back
)
153 Proxy_Plug pp
= (Proxy_Plug
) p
;
154 Proxy_Socket ps
= pp
->proxy_socket
;
156 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
157 ps
->closing_error_msg
= error_msg
;
158 ps
->closing_error_code
= error_code
;
159 ps
->closing_calling_back
= calling_back
;
160 return ps
->negotiate(ps
, PROXY_CHANGE_CLOSING
);
162 return plug_closing(ps
->plug
, error_msg
,
163 error_code
, calling_back
);
166 static int plug_proxy_receive (Plug p
, int urgent
, char *data
, int len
)
168 Proxy_Plug pp
= (Proxy_Plug
) p
;
169 Proxy_Socket ps
= pp
->proxy_socket
;
171 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
172 /* we will lose the urgentness of this data, but since most,
173 * if not all, of this data will be consumed by the negotiation
174 * process, hopefully it won't affect the protocol above us
176 bufchain_add(&ps
->pending_input_data
, data
, len
);
177 ps
->receive_urgent
= urgent
;
178 ps
->receive_data
= data
;
179 ps
->receive_len
= len
;
180 return ps
->negotiate(ps
, PROXY_CHANGE_RECEIVE
);
182 return plug_receive(ps
->plug
, urgent
, data
, len
);
185 static void plug_proxy_sent (Plug p
, int bufsize
)
187 Proxy_Plug pp
= (Proxy_Plug
) p
;
188 Proxy_Socket ps
= pp
->proxy_socket
;
190 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
191 ps
->sent_bufsize
= bufsize
;
192 ps
->negotiate(ps
, PROXY_CHANGE_SENT
);
195 plug_sent(ps
->plug
, bufsize
);
198 static int plug_proxy_accepting (Plug p
, void *sock
)
200 Proxy_Plug pp
= (Proxy_Plug
) p
;
201 Proxy_Socket ps
= pp
->proxy_socket
;
203 if (ps
->state
!= PROXY_STATE_ACTIVE
) {
204 ps
->accepting_sock
= sock
;
205 return ps
->negotiate(ps
, PROXY_CHANGE_ACCEPTING
);
207 return plug_accepting(ps
->plug
, sock
);
210 static int proxy_for_destination (SockAddr addr
, char * hostname
, int port
)
214 int hostip_len
, hostname_len
;
217 /* we want a string representation of the IP address for comparisons */
218 sk_getaddr(addr
, hostip
, 64);
220 hostip_len
= strlen(hostip
);
221 hostname_len
= strlen(hostname
);
223 exclude_list
= cfg
.proxy_exclude_list
;
225 /* now parse the exclude list, and see if either our IP
226 * or hostname matches anything in it.
229 while (exclude_list
[s
]) {
230 while (exclude_list
[s
] &&
231 (isspace(exclude_list
[s
]) ||
232 exclude_list
[s
] == ',')) s
++;
234 if (!exclude_list
[s
]) break;
238 while (exclude_list
[e
] &&
239 (isalnum(exclude_list
[e
]) ||
240 exclude_list
[e
] == '-' ||
241 exclude_list
[e
] == '.' ||
242 exclude_list
[e
] == '*')) e
++;
244 if (exclude_list
[s
] == '*') {
245 /* wildcard at beginning of entry */
247 if (strnicmp(hostip
+ hostip_len
- (e
- s
- 1),
248 exclude_list
+ s
+ 1, e
- s
- 1) == 0 ||
249 strnicmp(hostname
+ hostname_len
- (e
- s
- 1),
250 exclude_list
+ s
+ 1, e
- s
- 1) == 0)
251 return 0; /* IP/hostname range excluded. do not use proxy. */
253 } else if (exclude_list
[e
-1] == '*') {
254 /* wildcard at end of entry */
256 if (strnicmp(hostip
, exclude_list
+ s
, e
- s
- 1) == 0 ||
257 strnicmp(hostname
, exclude_list
+ s
, e
- s
- 1) == 0)
258 return 0; /* IP/hostname range excluded. do not use proxy. */
261 /* no wildcard at either end, so let's try an absolute
262 * match (ie. a specific IP)
265 if (stricmp(hostip
, exclude_list
+ s
) == 0)
266 return 0; /* IP/hostname excluded. do not use proxy. */
267 if (stricmp(hostname
, exclude_list
+ s
) == 0)
268 return 0; /* IP/hostname excluded. do not use proxy. */
274 /* no matches in the exclude list, so use the proxy */
278 Socket
new_connection(SockAddr addr
, char *hostname
,
279 int port
, int privport
,
280 int oobinline
, int nodelay
, Plug plug
)
282 static struct socket_function_table socket_fn_table
= {
288 sk_proxy_set_private_ptr
,
289 sk_proxy_get_private_ptr
,
291 sk_proxy_socket_error
294 static struct plug_function_table plug_fn_table
= {
301 if (cfg
.proxy_type
!= PROXY_NONE
&&
302 proxy_for_destination(addr
, hostname
, port
))
307 char * proxy_canonical_name
;
309 ret
= smalloc(sizeof(struct Socket_proxy_tag
));
310 ret
->fn
= &socket_fn_table
;
312 ret
->remote_addr
= addr
;
313 ret
->remote_port
= port
;
315 bufchain_init(&ret
->pending_input_data
);
316 bufchain_init(&ret
->pending_output_data
);
317 bufchain_init(&ret
->pending_oob_output_data
);
319 ret
->sub_socket
= NULL
;
320 ret
->state
= PROXY_STATE_NEW
;
322 if (cfg
.proxy_type
== PROXY_HTTP
) {
323 ret
->negotiate
= proxy_http_negotiate
;
324 } else if (cfg
.proxy_type
== PROXY_SOCKS
) {
325 if (cfg
.proxy_socks_version
== 4)
326 ret
->negotiate
= proxy_socks4_negotiate
;
328 ret
->negotiate
= proxy_socks5_negotiate
;
329 } else if (cfg
.proxy_type
== PROXY_TELNET
) {
330 ret
->negotiate
= proxy_telnet_negotiate
;
332 ret
->error
= "Network error: Unknown proxy method";
336 /* create the proxy plug to map calls from the actual
337 * socket into our proxy socket layer */
338 pplug
= smalloc(sizeof(struct Plug_proxy_tag
));
339 pplug
->fn
= &plug_fn_table
;
340 pplug
->proxy_socket
= ret
;
343 proxy_addr
= sk_namelookup(cfg
.proxy_host
,
344 &proxy_canonical_name
);
345 sfree(proxy_canonical_name
);
347 /* create the actual socket we will be using,
348 * connected to our proxy server and port.
350 ret
->sub_socket
= sk_new(proxy_addr
, cfg
.proxy_port
,
352 nodelay
, (Plug
) pplug
);
353 if (sk_socket_error(ret
->sub_socket
) != NULL
)
356 sk_addr_free(proxy_addr
);
358 /* start the proxy negotiation process... */
359 sk_set_frozen(ret
->sub_socket
, 0);
360 ret
->negotiate(ret
, PROXY_CHANGE_NEW
);
365 /* no proxy, so just return the direct socket */
366 return sk_new(addr
, port
, privport
, oobinline
, nodelay
, plug
);
369 Socket
new_listener(int port
, Plug plug
, int local_host_only
)
371 /* TODO: SOCKS (and potentially others) support inbound
372 * TODO: connections via the proxy. support them.
375 return sk_newlistener(port
, plug
, local_host_only
);
378 /* ----------------------------------------------------------------------
379 * HTTP CONNECT proxy type.
382 static int get_line_end (char * data
, int len
)
388 if (data
[off
] == '\n') {
389 /* we have a newline */
392 /* is that the only thing on this line? */
393 if (off
<= 2) return off
;
395 /* if not, then there is the possibility that this header
396 * continues onto the next line, if it starts with a space
401 data
[off
+1] != ' ' &&
402 data
[off
+1] != '\t') return off
;
404 /* the line does continue, so we have to keep going
405 * until we see an the header's "real" end of line.
416 int proxy_http_negotiate (Proxy_Socket p
, int change
)
418 if (p
->state
== PROXY_STATE_NEW
) {
419 /* we are just beginning the proxy negotiate process,
420 * so we'll send off the initial bits of the request.
421 * for this proxy method, it's just a simple HTTP
424 char buf
[256], dest
[64];
426 sk_getaddr(p
->remote_addr
, dest
, 64);
428 sprintf(buf
, "CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",
429 dest
, p
->remote_port
, dest
, p
->remote_port
);
430 sk_write(p
->sub_socket
, buf
, strlen(buf
));
432 if (cfg
.proxy_username
[0] || cfg
.proxy_password
[0]) {
433 char buf
[sizeof(cfg
.proxy_username
)+sizeof(cfg
.proxy_password
)];
434 char buf2
[sizeof(buf
)*4/3 + 100];
436 sprintf(buf
, "%s:%s", cfg
.proxy_username
, cfg
.proxy_password
);
438 sprintf(buf2
, "Proxy-Authorization: basic ");
439 for (i
= 0, j
= strlen(buf2
); i
< len
; i
+= 3, j
+= 4)
440 base64_encode_atom(buf
+i
, (len
-i
> 3 ?
3 : len
-i
), buf2
+j
);
441 strcpy(buf2
+j
, "\r\n");
442 sk_write(p
->sub_socket
, buf2
, strlen(buf2
));
445 sprintf(buf
, "\r\n");
446 sk_write(p
->sub_socket
, buf
, strlen(buf
));
452 if (change
== PROXY_CHANGE_CLOSING
) {
453 /* if our proxy negotiation process involves closing and opening
454 * new sockets, then we would want to intercept this closing
455 * callback when we were expecting it. if we aren't anticipating
456 * a socket close, then some error must have occurred. we'll
457 * just pass those errors up to the backend.
459 return plug_closing(p
->plug
, p
->closing_error_msg
,
460 p
->closing_error_code
,
461 p
->closing_calling_back
);
464 if (change
== PROXY_CHANGE_SENT
) {
465 /* some (or all) of what we wrote to the proxy was sent.
466 * we don't do anything new, however, until we receive the
467 * proxy's response. we might want to set a timer so we can
468 * timeout the proxy negotiation after a while...
473 if (change
== PROXY_CHANGE_ACCEPTING
) {
474 /* we should _never_ see this, as we are using our socket to
475 * connect to a proxy, not accepting inbound connections.
476 * what should we do? close the socket with an appropriate
479 return plug_accepting(p
->plug
, p
->accepting_sock
);
482 if (change
== PROXY_CHANGE_RECEIVE
) {
483 /* we have received data from the underlying socket, which
484 * we'll need to parse, process, and respond to appropriately.
493 int min_ver
, maj_ver
, status
;
495 /* get the status line */
496 bufchain_prefix(&p
->pending_input_data
, &data
, &len
);
497 eol
= get_line_end(data
, len
);
498 if (eol
< 0) return 1;
500 sscanf((char *)data
, "HTTP/%i.%i %i", &maj_ver
, &min_ver
, &status
);
502 /* remove the status line from the input buffer. */
503 bufchain_consume(&p
->pending_input_data
, eol
);
505 /* TODO: we need to support Proxy-Auth headers */
507 if (status
< 200 || status
> 299) {
509 /* TODO: return a more specific error message,
510 * TODO: based on the status code.
512 plug_closing(p
->plug
, "Network error: Error while communicating with proxy",
513 PROXY_ERROR_GENERAL
, 0);
522 /* get headers. we're done when we get a
523 * header of length 2, (ie. just "\r\n")
526 bufchain_prefix(&p
->pending_input_data
, &data
, &len
);
527 eol
= get_line_end(data
, len
);
530 /* TODO: Proxy-Auth stuff. in some cases, we will
531 * TODO: need to extract information from headers.
533 bufchain_consume(&p
->pending_input_data
, eol
);
534 bufchain_prefix(&p
->pending_input_data
, &data
, &len
);
535 eol
= get_line_end(data
, len
);
540 bufchain_consume(&p
->pending_input_data
, 2);
542 /* proxy activate will have dealt with
543 * whatever is left of the buffer */
551 plug_closing(p
->plug
, "Network error: Unexpected proxy error",
552 PROXY_ERROR_UNEXPECTED
, 0);
556 /* ----------------------------------------------------------------------
560 /* SOCKS version 4 */
561 int proxy_socks4_negotiate (Proxy_Socket p
, int change
)
563 if (p
->state
== PROXY_CHANGE_NEW
) {
566 * version number (1 byte) = 4
567 * command code (1 byte)
570 * dest. port (2 bytes) [network order]
571 * dest. address (4 bytes)
572 * user ID (variable length, null terminated string)
578 if (sk_addrtype(p
->remote_addr
) != AF_INET
) {
579 plug_closing(p
->plug
, "Network error: SOCKS version 4 does not support IPv6",
580 PROXY_ERROR_GENERAL
, 0);
584 length
= strlen(cfg
.proxy_username
) + 9;
585 command
= (char*) malloc(length
);
586 strcpy(command
+ 8, cfg
.proxy_username
);
588 command
[0] = 4; /* version 4 */
589 command
[1] = 1; /* CONNECT command */
592 command
[2] = (char) (p
->remote_port
>> 8) & 0xff;
593 command
[3] = (char) p
->remote_port
& 0xff;
596 sk_addrcopy(p
->remote_addr
, command
+ 4);
598 sk_write(p
->sub_socket
, command
, length
);
605 if (change
== PROXY_CHANGE_CLOSING
) {
606 /* if our proxy negotiation process involves closing and opening
607 * new sockets, then we would want to intercept this closing
608 * callback when we were expecting it. if we aren't anticipating
609 * a socket close, then some error must have occurred. we'll
610 * just pass those errors up to the backend.
612 return plug_closing(p
->plug
, p
->closing_error_msg
,
613 p
->closing_error_code
,
614 p
->closing_calling_back
);
617 if (change
== PROXY_CHANGE_SENT
) {
618 /* some (or all) of what we wrote to the proxy was sent.
619 * we don't do anything new, however, until we receive the
620 * proxy's response. we might want to set a timer so we can
621 * timeout the proxy negotiation after a while...
626 if (change
== PROXY_CHANGE_ACCEPTING
) {
627 /* we should _never_ see this, as we are using our socket to
628 * connect to a proxy, not accepting inbound connections.
629 * what should we do? close the socket with an appropriate
632 return plug_accepting(p
->plug
, p
->accepting_sock
);
635 if (change
== PROXY_CHANGE_RECEIVE
) {
636 /* we have received data from the underlying socket, which
637 * we'll need to parse, process, and respond to appropriately.
642 * version number (1 byte) = 4
643 * reply code (1 byte)
644 * 90 = request granted
645 * 91 = request rejected or failed
646 * 92 = request rejected due to lack of IDENTD on client
647 * 93 = request rejected due to difference in user ID
648 * (what we sent vs. what IDENTD said)
649 * dest. port (2 bytes)
650 * dest. address (4 bytes)
656 /* get the response */
657 bufchain_prefix(&p
->pending_input_data
, &data
, &len
);
660 plug_closing(p
->plug
, "Network error: SOCKS proxy responded with "
661 "unexpected reply code version",
662 PROXY_ERROR_GENERAL
, 0);
670 plug_closing(p
->plug
, "Network error: SOCKS server wanted IDENTD on client",
671 PROXY_ERROR_GENERAL
, 0);
674 plug_closing(p
->plug
, "Network error: Username and IDENTD on client don't agree",
675 PROXY_ERROR_GENERAL
, 0);
679 plug_closing(p
->plug
, "Network error: Error while communicating with proxy",
680 PROXY_ERROR_GENERAL
, 0);
689 /* proxy activate will have dealt with
690 * whatever is left of the buffer */
695 plug_closing(p
->plug
, "Network error: Unexpected proxy error",
696 PROXY_ERROR_UNEXPECTED
, 0);
700 /* SOCKS version 5 */
701 int proxy_socks5_negotiate (Proxy_Socket p
, int change
)
703 if (p
->state
== PROXY_CHANGE_NEW
) {
706 * version number (1 byte) = 5
707 * number of available authentication methods (1 byte)
708 * available authentication methods (1 byte * previous value)
709 * authentication methods:
710 * 0x00 = no authentication
712 * 0x02 = username/password
718 command
[0] = 5; /* version 5 */
719 command
[1] = 1; /* TODO: we don't currently support any auth methods */
720 command
[2] = 0x00; /* no authentication */
722 sk_write(p
->sub_socket
, command
, 3);
728 if (change
== PROXY_CHANGE_CLOSING
) {
729 /* if our proxy negotiation process involves closing and opening
730 * new sockets, then we would want to intercept this closing
731 * callback when we were expecting it. if we aren't anticipating
732 * a socket close, then some error must have occurred. we'll
733 * just pass those errors up to the backend.
735 return plug_closing(p
->plug
, p
->closing_error_msg
,
736 p
->closing_error_code
,
737 p
->closing_calling_back
);
740 if (change
== PROXY_CHANGE_SENT
) {
741 /* some (or all) of what we wrote to the proxy was sent.
742 * we don't do anything new, however, until we receive the
743 * proxy's response. we might want to set a timer so we can
744 * timeout the proxy negotiation after a while...
749 if (change
== PROXY_CHANGE_ACCEPTING
) {
750 /* we should _never_ see this, as we are using our socket to
751 * connect to a proxy, not accepting inbound connections.
752 * what should we do? close the socket with an appropriate
755 return plug_accepting(p
->plug
, p
->accepting_sock
);
758 if (change
== PROXY_CHANGE_RECEIVE
) {
759 /* we have received data from the underlying socket, which
760 * we'll need to parse, process, and respond to appropriately.
769 * version number (1 byte) = 5
770 * authentication method (1 byte)
771 * authentication methods:
772 * 0x00 = no authentication
774 * 0x02 = username/password
776 * 0xff = no acceptable methods
779 /* get the response */
780 bufchain_prefix(&p
->pending_input_data
, &data
, &len
);
783 plug_closing(p
->plug
, "Network error: Error while communicating with proxy",
784 PROXY_ERROR_GENERAL
, 0);
788 if (data
[1] == 0x00) p
->state
= 2; /* no authentication needed */
789 else if (data
[1] == 0x01) p
->state
= 4; /* GSSAPI authentication */
790 else if (data
[1] == 0x02) p
->state
= 5; /* username/password authentication */
791 else if (data
[1] == 0x03) p
->state
= 6; /* CHAP authentication */
793 plug_closing(p
->plug
, "Network error: We don't support any of the SOCKS "
794 "server's authentication methods",
795 PROXY_ERROR_GENERAL
, 0);
803 * version number (1 byte) = 5
804 * command code (1 byte)
808 * reserved (1 byte) = 0x00
809 * address type (1 byte)
811 * 3 = domainname (first byte has length, no terminating null)
813 * dest. address (variable)
814 * dest. port (2 bytes) [network order]
820 if (sk_addrtype(p
->remote_addr
) == AF_INET
) {
822 command
[3] = 1; /* IPv4 */
825 command
[3] = 4; /* IPv6 */
828 command
[0] = 5; /* version 5 */
829 command
[1] = 1; /* CONNECT command */
833 sk_addrcopy(p
->remote_addr
, command
+4);
836 command
[len
-2] = (char) (p
->remote_port
>> 8) & 0xff;
837 command
[len
-1] = (char) p
->remote_port
& 0xff;
839 sk_write(p
->sub_socket
, command
, len
);
848 * version number (1 bytes) = 5
849 * reply code (1 byte)
851 * 1 = general SOCKS server failure
852 * 2 = connection not allowed by ruleset
853 * 3 = network unreachable
854 * 4 = host unreachable
855 * 5 = connection refused
857 * 7 = command not supported
858 * 8 = address type not supported
859 * reserved (1 byte) = x00
860 * address type (1 byte)
862 * 3 = domainname (first byte has length, no terminating null)
864 * server bound address (variable)
865 * server bound port (2 bytes) [network order]
868 /* get the response */
869 bufchain_prefix(&p
->pending_input_data
, &data
, &len
);
872 plug_closing(p
->plug
, "Network error: Error while communicating with proxy",
873 PROXY_ERROR_GENERAL
, 0);
889 plug_closing(p
->plug
, "Network error: Error while communicating with proxy",
890 PROXY_ERROR_GENERAL
, 0);
899 /* proxy activate will have dealt with
900 * whatever is left of the buffer */
905 /* TODO: Handle GSSAPI authentication */
906 plug_closing(p
->plug
, "Network error: We don't support GSSAPI authentication",
907 PROXY_ERROR_GENERAL
, 0);
912 /* TODO: Handle username/password authentication */
913 plug_closing(p
->plug
, "Network error: We don't support username/password "
915 PROXY_ERROR_GENERAL
, 0);
920 /* TODO: Handle CHAP authentication */
921 plug_closing(p
->plug
, "Network error: We don't support CHAP authentication",
922 PROXY_ERROR_GENERAL
, 0);
927 plug_closing(p
->plug
, "Network error: Unexpected proxy error",
928 PROXY_ERROR_UNEXPECTED
, 0);
932 /* ----------------------------------------------------------------------
933 * `Telnet' proxy type.
935 * (This is for ad-hoc proxies where you connect to the proxy's
936 * telnet port and send a command such as `connect host port'. The
937 * command is configurable, since this proxy type is typically not
938 * standardised or at all well-defined.)
941 int proxy_telnet_negotiate (Proxy_Socket p
, int change
)
943 if (p
->state
== PROXY_CHANGE_NEW
) {
947 /* we need to escape \\, \%, \r, \n, \t, \x??, \0???,
948 * %%, %host, and %port
951 while (cfg
.proxy_telnet_command
[eo
] != 0) {
953 /* scan forward until we hit end-of-line,
954 * or an escape character (\ or %) */
955 while (cfg
.proxy_telnet_command
[eo
] != 0 &&
956 cfg
.proxy_telnet_command
[eo
] != '%' &&
957 cfg
.proxy_telnet_command
[eo
] != '\\') eo
++;
959 /* if we hit eol, break out of our escaping loop */
960 if (cfg
.proxy_telnet_command
[eo
] == 0) break;
962 /* if there was any unescaped text before the escape
963 * character, send that now */
965 sk_write(p
->sub_socket
,
966 cfg
.proxy_telnet_command
+ so
, eo
- so
);
971 /* if the escape character was the last character of
972 * the line, we'll just stop and send it. */
973 if (cfg
.proxy_telnet_command
[eo
] == 0) break;
975 if (cfg
.proxy_telnet_command
[so
] == '\\') {
977 /* we recognize \\, \%, \r, \n, \t, \x??.
978 * anything else, we just send unescaped (including the \).
981 switch (cfg
.proxy_telnet_command
[eo
]) {
984 sk_write(p
->sub_socket
, "\\", 1);
989 sk_write(p
->sub_socket
, "%%", 1);
994 sk_write(p
->sub_socket
, "\r", 1);
999 sk_write(p
->sub_socket
, "\n", 1);
1004 sk_write(p
->sub_socket
, "\t", 1);
1011 /* escaped hexadecimal value (ie. \xff) */
1012 unsigned char v
= 0;
1017 if (cfg
.proxy_telnet_command
[eo
] >= '0' &&
1018 cfg
.proxy_telnet_command
[eo
] <= '9')
1019 v
+= cfg
.proxy_telnet_command
[eo
] - '0';
1020 else if (cfg
.proxy_telnet_command
[eo
] >= 'a' &&
1021 cfg
.proxy_telnet_command
[eo
] <= 'f')
1022 v
+= cfg
.proxy_telnet_command
[eo
] - 'a' + 10;
1023 else if (cfg
.proxy_telnet_command
[eo
] >= 'A' &&
1024 cfg
.proxy_telnet_command
[eo
] <= 'F')
1025 v
+= cfg
.proxy_telnet_command
[eo
] - 'A' + 10;
1027 /* non hex character, so we abort and just
1028 * send the whole thing unescaped (including \x)
1030 sk_write(p
->sub_socket
, "\\", 1);
1035 /* we only extract two hex characters */
1037 sk_write(p
->sub_socket
, &v
, 1);
1049 sk_write(p
->sub_socket
,
1050 cfg
.proxy_telnet_command
+ so
, 2);
1056 /* % escape. we recognize %%, %host, %port. anything else,
1057 * we just send unescaped (including the %). */
1059 if (cfg
.proxy_telnet_command
[eo
] == '%') {
1060 sk_write(p
->sub_socket
, "%", 1);
1063 else if (strnicmp(cfg
.proxy_telnet_command
+ eo
,
1066 sk_getaddr(p
->remote_addr
, dest
, 64);
1067 sk_write(p
->sub_socket
, dest
, strlen(dest
));
1070 else if (strnicmp(cfg
.proxy_telnet_command
+ eo
,
1073 sprintf(port
, "%i", p
->remote_port
);
1074 sk_write(p
->sub_socket
, port
, strlen(port
));
1078 /* we don't escape this, so send the % now, and
1079 * don't advance eo, so that we'll consider the
1080 * text immediately following the % as unescaped.
1082 sk_write(p
->sub_socket
, "%", 1);
1086 /* resume scanning for additional escapes after this one. */
1090 /* if there is any unescaped text at the end of the line, send it */
1092 sk_write(p
->sub_socket
, cfg
.proxy_telnet_command
+ so
, eo
- so
);
1099 if (change
== PROXY_CHANGE_CLOSING
) {
1100 /* if our proxy negotiation process involves closing and opening
1101 * new sockets, then we would want to intercept this closing
1102 * callback when we were expecting it. if we aren't anticipating
1103 * a socket close, then some error must have occurred. we'll
1104 * just pass those errors up to the backend.
1106 return plug_closing(p
->plug
, p
->closing_error_msg
,
1107 p
->closing_error_code
,
1108 p
->closing_calling_back
);
1111 if (change
== PROXY_CHANGE_SENT
) {
1112 /* some (or all) of what we wrote to the proxy was sent.
1113 * we don't do anything new, however, until we receive the
1114 * proxy's response. we might want to set a timer so we can
1115 * timeout the proxy negotiation after a while...
1120 if (change
== PROXY_CHANGE_ACCEPTING
) {
1121 /* we should _never_ see this, as we are using our socket to
1122 * connect to a proxy, not accepting inbound connections.
1123 * what should we do? close the socket with an appropriate
1126 return plug_accepting(p
->plug
, p
->accepting_sock
);
1129 if (change
== PROXY_CHANGE_RECEIVE
) {
1130 /* we have received data from the underlying socket, which
1131 * we'll need to parse, process, and respond to appropriately.
1136 /* proxy activate will have dealt with
1137 * whatever is left of the buffer */
1141 plug_closing(p
->plug
, "Network error: Unexpected proxy error",
1142 PROXY_ERROR_UNEXPECTED
, 0);