OSDN Git Service

Prepare for release of 1.98e.
[ffftp/ffftp.git] / putty / PROXY.C
diff --git a/putty/PROXY.C b/putty/PROXY.C
deleted file mode 100644 (file)
index 1f42999..0000000
+++ /dev/null
@@ -1,1478 +0,0 @@
-/*\r
- * Network proxy abstraction in PuTTY\r
- *\r
- * A proxy layer, if necessary, wedges itself between the network\r
- * code and the higher level backend.\r
- */\r
-\r
-#include <assert.h>\r
-#include <ctype.h>\r
-#include <string.h>\r
-\r
-#define DEFINE_PLUG_METHOD_MACROS\r
-#include "putty.h"\r
-#include "network.h"\r
-#include "proxy.h"\r
-\r
-#define do_proxy_dns(cfg) \\r
-    (cfg->proxy_dns == FORCE_ON || \\r
-        (cfg->proxy_dns == AUTO && cfg->proxy_type != PROXY_SOCKS4))\r
-\r
-/*\r
- * Call this when proxy negotiation is complete, so that this\r
- * socket can begin working normally.\r
- */\r
-void proxy_activate (Proxy_Socket p)\r
-{\r
-    void *data;\r
-    int len;\r
-    long output_before, output_after;\r
-    \r
-    p->state = PROXY_STATE_ACTIVE;\r
-\r
-    /* we want to ignore new receive events until we have sent\r
-     * all of our buffered receive data.\r
-     */\r
-    sk_set_frozen(p->sub_socket, 1);\r
-\r
-    /* how many bytes of output have we buffered? */\r
-    output_before = bufchain_size(&p->pending_oob_output_data) +\r
-       bufchain_size(&p->pending_output_data);\r
-    /* and keep track of how many bytes do not get sent. */\r
-    output_after = 0;\r
-    \r
-    /* send buffered OOB writes */\r
-    while (bufchain_size(&p->pending_oob_output_data) > 0) {\r
-       bufchain_prefix(&p->pending_oob_output_data, &data, &len);\r
-       output_after += sk_write_oob(p->sub_socket, data, len);\r
-       bufchain_consume(&p->pending_oob_output_data, len);\r
-    }\r
-\r
-    /* send buffered normal writes */\r
-    while (bufchain_size(&p->pending_output_data) > 0) {\r
-       bufchain_prefix(&p->pending_output_data, &data, &len);\r
-       output_after += sk_write(p->sub_socket, data, len);\r
-       bufchain_consume(&p->pending_output_data, len);\r
-    }\r
-\r
-    /* if we managed to send any data, let the higher levels know. */\r
-    if (output_after < output_before)\r
-       plug_sent(p->plug, output_after);\r
-\r
-    /* if we were asked to flush the output during\r
-     * the proxy negotiation process, do so now.\r
-     */\r
-    if (p->pending_flush) sk_flush(p->sub_socket);\r
-\r
-    /* if the backend wanted the socket unfrozen, try to unfreeze.\r
-     * our set_frozen handler will flush buffered receive data before\r
-     * unfreezing the actual underlying socket.\r
-     */\r
-    if (!p->freeze)\r
-       sk_set_frozen((Socket)p, 0);\r
-}\r
-\r
-/* basic proxy socket functions */\r
-\r
-static Plug sk_proxy_plug (Socket s, Plug p)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-    Plug ret = ps->plug;\r
-    if (p)\r
-       ps->plug = p;\r
-    return ret;\r
-}\r
-\r
-static void sk_proxy_close (Socket s)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-\r
-    sk_close(ps->sub_socket);\r
-    sk_addr_free(ps->remote_addr);\r
-    sfree(ps);\r
-}\r
-\r
-static int sk_proxy_write (Socket s, const char *data, int len)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-\r
-    if (ps->state != PROXY_STATE_ACTIVE) {\r
-       bufchain_add(&ps->pending_output_data, data, len);\r
-       return bufchain_size(&ps->pending_output_data);\r
-    }\r
-    return sk_write(ps->sub_socket, data, len);\r
-}\r
-\r
-static int sk_proxy_write_oob (Socket s, const char *data, int len)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-\r
-    if (ps->state != PROXY_STATE_ACTIVE) {\r
-       bufchain_clear(&ps->pending_output_data);\r
-       bufchain_clear(&ps->pending_oob_output_data);\r
-       bufchain_add(&ps->pending_oob_output_data, data, len);\r
-       return len;\r
-    }\r
-    return sk_write_oob(ps->sub_socket, data, len);\r
-}\r
-\r
-static void sk_proxy_flush (Socket s)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-\r
-    if (ps->state != PROXY_STATE_ACTIVE) {\r
-       ps->pending_flush = 1;\r
-       return;\r
-    }\r
-    sk_flush(ps->sub_socket);\r
-}\r
-\r
-static void sk_proxy_set_private_ptr (Socket s, void *ptr)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-    sk_set_private_ptr(ps->sub_socket, ptr);\r
-}\r
-\r
-static void * sk_proxy_get_private_ptr (Socket s)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-    return sk_get_private_ptr(ps->sub_socket);\r
-}\r
-\r
-static void sk_proxy_set_frozen (Socket s, int is_frozen)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-\r
-    if (ps->state != PROXY_STATE_ACTIVE) {\r
-       ps->freeze = is_frozen;\r
-       return;\r
-    }\r
-    \r
-    /* handle any remaining buffered recv data first */\r
-    if (bufchain_size(&ps->pending_input_data) > 0) {\r
-       ps->freeze = is_frozen;\r
-\r
-       /* loop while we still have buffered data, and while we are\r
-        * unfrozen. the plug_receive call in the loop could result \r
-        * in a call back into this function refreezing the socket, \r
-        * so we have to check each time.\r
-        */\r
-        while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {\r
-           void *data;\r
-           char databuf[512];\r
-           int len;\r
-           bufchain_prefix(&ps->pending_input_data, &data, &len);\r
-           if (len > lenof(databuf))\r
-               len = lenof(databuf);\r
-           memcpy(databuf, data, len);\r
-           bufchain_consume(&ps->pending_input_data, len);\r
-           plug_receive(ps->plug, 0, databuf, len);\r
-       }\r
-\r
-       /* if we're still frozen, we'll have to wait for another\r
-        * call from the backend to finish unbuffering the data.\r
-        */\r
-       if (ps->freeze) return;\r
-    }\r
-    \r
-    sk_set_frozen(ps->sub_socket, is_frozen);\r
-}\r
-\r
-static const char * sk_proxy_socket_error (Socket s)\r
-{\r
-    Proxy_Socket ps = (Proxy_Socket) s;\r
-    if (ps->error != NULL || ps->sub_socket == NULL) {\r
-       return ps->error;\r
-    }\r
-    return sk_socket_error(ps->sub_socket);\r
-}\r
-\r
-/* basic proxy plug functions */\r
-\r
-static void plug_proxy_log(Plug plug, int type, SockAddr addr, int port,\r
-                          const char *error_msg, int error_code)\r
-{\r
-    Proxy_Plug pp = (Proxy_Plug) plug;\r
-    Proxy_Socket ps = pp->proxy_socket;\r
-\r
-    plug_log(ps->plug, type, addr, port, error_msg, error_code);\r
-}\r
-\r
-static int plug_proxy_closing (Plug p, const char *error_msg,\r
-                              int error_code, int calling_back)\r
-{\r
-    Proxy_Plug pp = (Proxy_Plug) p;\r
-    Proxy_Socket ps = pp->proxy_socket;\r
-\r
-    if (ps->state != PROXY_STATE_ACTIVE) {\r
-       ps->closing_error_msg = error_msg;\r
-       ps->closing_error_code = error_code;\r
-       ps->closing_calling_back = calling_back;\r
-       return ps->negotiate(ps, PROXY_CHANGE_CLOSING);\r
-    }\r
-    return plug_closing(ps->plug, error_msg,\r
-                       error_code, calling_back);\r
-}\r
-\r
-static int plug_proxy_receive (Plug p, int urgent, char *data, int len)\r
-{\r
-    Proxy_Plug pp = (Proxy_Plug) p;\r
-    Proxy_Socket ps = pp->proxy_socket;\r
-\r
-    if (ps->state != PROXY_STATE_ACTIVE) {\r
-       /* we will lose the urgentness of this data, but since most,\r
-        * if not all, of this data will be consumed by the negotiation\r
-        * process, hopefully it won't affect the protocol above us\r
-        */\r
-       bufchain_add(&ps->pending_input_data, data, len);\r
-       ps->receive_urgent = urgent;\r
-       ps->receive_data = data;\r
-       ps->receive_len = len;\r
-       return ps->negotiate(ps, PROXY_CHANGE_RECEIVE);\r
-    }\r
-    return plug_receive(ps->plug, urgent, data, len);\r
-}\r
-\r
-static void plug_proxy_sent (Plug p, int bufsize)\r
-{\r
-    Proxy_Plug pp = (Proxy_Plug) p;\r
-    Proxy_Socket ps = pp->proxy_socket;\r
-\r
-    if (ps->state != PROXY_STATE_ACTIVE) {\r
-       ps->sent_bufsize = bufsize;\r
-       ps->negotiate(ps, PROXY_CHANGE_SENT);\r
-       return;\r
-    }\r
-    plug_sent(ps->plug, bufsize);\r
-}\r
-\r
-static int plug_proxy_accepting (Plug p, OSSocket sock)\r
-{\r
-    Proxy_Plug pp = (Proxy_Plug) p;\r
-    Proxy_Socket ps = pp->proxy_socket;\r
-\r
-    if (ps->state != PROXY_STATE_ACTIVE) {\r
-       ps->accepting_sock = sock;\r
-       return ps->negotiate(ps, PROXY_CHANGE_ACCEPTING);\r
-    }\r
-    return plug_accepting(ps->plug, sock);\r
-}\r
-\r
-/*\r
- * This function can accept a NULL pointer as `addr', in which case\r
- * it will only check the host name.\r
- */\r
-static int proxy_for_destination (SockAddr addr, char *hostname, int port,\r
-                                 const Config *cfg)\r
-{\r
-    int s = 0, e = 0;\r
-    char hostip[64];\r
-    int hostip_len, hostname_len;\r
-    const char *exclude_list;\r
-\r
-    /*\r
-     * Check the host name and IP against the hard-coded\r
-     * representations of `localhost'.\r
-     */\r
-    if (!cfg->even_proxy_localhost &&\r
-       (sk_hostname_is_local(hostname) ||\r
-        (addr && sk_address_is_local(addr))))\r
-       return 0;                      /* do not proxy */\r
-\r
-    /* we want a string representation of the IP address for comparisons */\r
-    if (addr) {\r
-       sk_getaddr(addr, hostip, 64);\r
-       hostip_len = strlen(hostip);\r
-    } else\r
-       hostip_len = 0;                /* placate gcc; shouldn't be required */\r
-\r
-    hostname_len = strlen(hostname);\r
-\r
-    exclude_list = cfg->proxy_exclude_list;\r
-\r
-    /* now parse the exclude list, and see if either our IP\r
-     * or hostname matches anything in it.\r
-     */\r
-\r
-    while (exclude_list[s]) {\r
-       while (exclude_list[s] &&\r
-              (isspace((unsigned char)exclude_list[s]) ||\r
-               exclude_list[s] == ',')) s++;\r
-\r
-       if (!exclude_list[s]) break;\r
-\r
-       e = s;\r
-\r
-       while (exclude_list[e] &&\r
-              (isalnum((unsigned char)exclude_list[e]) ||\r
-               exclude_list[e] == '-' ||\r
-               exclude_list[e] == '.' ||\r
-               exclude_list[e] == '*')) e++;\r
-\r
-       if (exclude_list[s] == '*') {\r
-           /* wildcard at beginning of entry */\r
-\r
-           if ((addr && strnicmp(hostip + hostip_len - (e - s - 1),\r
-                                 exclude_list + s + 1, e - s - 1) == 0) ||\r
-               strnicmp(hostname + hostname_len - (e - s - 1),\r
-                        exclude_list + s + 1, e - s - 1) == 0)\r
-               return 0; /* IP/hostname range excluded. do not use proxy. */\r
-\r
-       } else if (exclude_list[e-1] == '*') {\r
-           /* wildcard at end of entry */\r
-\r
-           if ((addr && strnicmp(hostip, exclude_list + s, e - s - 1) == 0) ||\r
-               strnicmp(hostname, exclude_list + s, e - s - 1) == 0)\r
-               return 0; /* IP/hostname range excluded. do not use proxy. */\r
-\r
-       } else {\r
-           /* no wildcard at either end, so let's try an absolute\r
-            * match (ie. a specific IP)\r
-            */\r
-\r
-           if (addr && strnicmp(hostip, exclude_list + s, e - s) == 0)\r
-               return 0; /* IP/hostname excluded. do not use proxy. */\r
-           if (strnicmp(hostname, exclude_list + s, e - s) == 0)\r
-               return 0; /* IP/hostname excluded. do not use proxy. */\r
-       }\r
-\r
-       s = e;\r
-\r
-       /* Make sure we really have reached the next comma or end-of-string */\r
-       while (exclude_list[s] &&\r
-              !isspace((unsigned char)exclude_list[s]) &&\r
-              exclude_list[s] != ',') s++;\r
-    }\r
-\r
-    /* no matches in the exclude list, so use the proxy */\r
-    return 1;\r
-}\r
-\r
-SockAddr name_lookup(char *host, int port, char **canonicalname,\r
-                    const Config *cfg, int addressfamily)\r
-{\r
-    if (cfg->proxy_type != PROXY_NONE &&\r
-       do_proxy_dns(cfg) &&\r
-       proxy_for_destination(NULL, host, port, cfg)) {\r
-       *canonicalname = dupstr(host);\r
-       return sk_nonamelookup(host);\r
-    }\r
-\r
-    return sk_namelookup(host, canonicalname, addressfamily);\r
-}\r
-\r
-Socket new_connection(SockAddr addr, char *hostname,\r
-                     int port, int privport,\r
-                     int oobinline, int nodelay, int keepalive,\r
-                     Plug plug, const Config *cfg)\r
-{\r
-    static const struct socket_function_table socket_fn_table = {\r
-       sk_proxy_plug,\r
-       sk_proxy_close,\r
-       sk_proxy_write,\r
-       sk_proxy_write_oob,\r
-       sk_proxy_flush,\r
-       sk_proxy_set_private_ptr,\r
-       sk_proxy_get_private_ptr,\r
-       sk_proxy_set_frozen,\r
-       sk_proxy_socket_error\r
-    };\r
-\r
-    static const struct plug_function_table plug_fn_table = {\r
-       plug_proxy_log,\r
-       plug_proxy_closing,\r
-       plug_proxy_receive,\r
-       plug_proxy_sent,\r
-       plug_proxy_accepting\r
-    };\r
-\r
-    if (cfg->proxy_type != PROXY_NONE &&\r
-       proxy_for_destination(addr, hostname, port, cfg))\r
-    {\r
-       Proxy_Socket ret;\r
-       Proxy_Plug pplug;\r
-       SockAddr proxy_addr;\r
-       char *proxy_canonical_name;\r
-       Socket sret;\r
-\r
-       if ((sret = platform_new_connection(addr, hostname, port, privport,\r
-                                           oobinline, nodelay, keepalive,\r
-                                           plug, cfg)) !=\r
-           NULL)\r
-           return sret;\r
-\r
-       ret = snew(struct Socket_proxy_tag);\r
-       ret->fn = &socket_fn_table;\r
-       ret->cfg = *cfg;               /* STRUCTURE COPY */\r
-       ret->plug = plug;\r
-       ret->remote_addr = addr;       /* will need to be freed on close */\r
-       ret->remote_port = port;\r
-\r
-       ret->error = NULL;\r
-       ret->pending_flush = 0;\r
-       ret->freeze = 0;\r
-\r
-       bufchain_init(&ret->pending_input_data);\r
-       bufchain_init(&ret->pending_output_data);\r
-       bufchain_init(&ret->pending_oob_output_data);\r
-\r
-       ret->sub_socket = NULL;\r
-       ret->state = PROXY_STATE_NEW;\r
-       ret->negotiate = NULL;\r
-       \r
-       if (cfg->proxy_type == PROXY_HTTP) {\r
-           ret->negotiate = proxy_http_negotiate;\r
-       } else if (cfg->proxy_type == PROXY_SOCKS4) {\r
-            ret->negotiate = proxy_socks4_negotiate;\r
-       } else if (cfg->proxy_type == PROXY_SOCKS5) {\r
-            ret->negotiate = proxy_socks5_negotiate;\r
-       } else if (cfg->proxy_type == PROXY_TELNET) {\r
-           ret->negotiate = proxy_telnet_negotiate;\r
-       } else {\r
-           ret->error = "Proxy error: Unknown proxy method";\r
-           return (Socket) ret;\r
-       }\r
-\r
-       /* create the proxy plug to map calls from the actual\r
-        * socket into our proxy socket layer */\r
-       pplug = snew(struct Plug_proxy_tag);\r
-       pplug->fn = &plug_fn_table;\r
-       pplug->proxy_socket = ret;\r
-\r
-       /* look-up proxy */\r
-       proxy_addr = sk_namelookup(cfg->proxy_host,\r
-                                  &proxy_canonical_name, cfg->addressfamily);\r
-       if (sk_addr_error(proxy_addr) != NULL) {\r
-           ret->error = "Proxy error: Unable to resolve proxy host name";\r
-           return (Socket)ret;\r
-       }\r
-       sfree(proxy_canonical_name);\r
-\r
-       /* create the actual socket we will be using,\r
-        * connected to our proxy server and port.\r
-        */\r
-       ret->sub_socket = sk_new(proxy_addr, cfg->proxy_port,\r
-                                privport, oobinline,\r
-                                nodelay, keepalive, (Plug) pplug);\r
-       if (sk_socket_error(ret->sub_socket) != NULL)\r
-           return (Socket) ret;\r
-\r
-       /* start the proxy negotiation process... */\r
-       sk_set_frozen(ret->sub_socket, 0);\r
-       ret->negotiate(ret, PROXY_CHANGE_NEW);\r
-\r
-       return (Socket) ret;\r
-    }\r
-\r
-    /* no proxy, so just return the direct socket */\r
-    return sk_new(addr, port, privport, oobinline, nodelay, keepalive, plug);\r
-}\r
-\r
-Socket new_listener(char *srcaddr, int port, Plug plug, int local_host_only,\r
-                   const Config *cfg, int addressfamily)\r
-{\r
-    /* TODO: SOCKS (and potentially others) support inbound\r
-     * TODO: connections via the proxy. support them.\r
-     */\r
-\r
-    return sk_newlistener(srcaddr, port, plug, local_host_only, addressfamily);\r
-}\r
-\r
-/* ----------------------------------------------------------------------\r
- * HTTP CONNECT proxy type.\r
- */\r
-\r
-static int get_line_end (char * data, int len)\r
-{\r
-    int off = 0;\r
-\r
-    while (off < len)\r
-    {\r
-       if (data[off] == '\n') {\r
-           /* we have a newline */\r
-           off++;\r
-\r
-           /* is that the only thing on this line? */\r
-           if (off <= 2) return off;\r
-\r
-           /* if not, then there is the possibility that this header\r
-            * continues onto the next line, if it starts with a space\r
-            * or a tab.\r
-            */\r
-\r
-           if (off + 1 < len &&\r
-               data[off+1] != ' ' &&\r
-               data[off+1] != '\t') return off;\r
-\r
-           /* the line does continue, so we have to keep going\r
-            * until we see an the header's "real" end of line.\r
-            */\r
-           off++;\r
-       }\r
-\r
-       off++;\r
-    }\r
-\r
-    return -1;\r
-}\r
-\r
-int proxy_http_negotiate (Proxy_Socket p, int change)\r
-{\r
-    if (p->state == PROXY_STATE_NEW) {\r
-       /* we are just beginning the proxy negotiate process,\r
-        * so we'll send off the initial bits of the request.\r
-        * for this proxy method, it's just a simple HTTP\r
-        * request\r
-        */\r
-       char *buf, dest[512];\r
-\r
-       sk_getaddr(p->remote_addr, dest, lenof(dest));\r
-\r
-       buf = dupprintf("CONNECT %s:%i HTTP/1.1\r\nHost: %s:%i\r\n",\r
-                       dest, p->remote_port, dest, p->remote_port);\r
-       sk_write(p->sub_socket, buf, strlen(buf));\r
-       sfree(buf);\r
-\r
-       if (p->cfg.proxy_username[0] || p->cfg.proxy_password[0]) {\r
-           char buf[sizeof(p->cfg.proxy_username)+sizeof(p->cfg.proxy_password)];\r
-           char buf2[sizeof(buf)*4/3 + 100];\r
-           int i, j, len;\r
-           sprintf(buf, "%s:%s", p->cfg.proxy_username, p->cfg.proxy_password);\r
-           len = strlen(buf);\r
-           sprintf(buf2, "Proxy-Authorization: Basic ");\r
-           for (i = 0, j = strlen(buf2); i < len; i += 3, j += 4)\r
-               base64_encode_atom((unsigned char *)(buf+i),\r
-                                  (len-i > 3 ? 3 : len-i), buf2+j);\r
-           strcpy(buf2+j, "\r\n");\r
-           sk_write(p->sub_socket, buf2, strlen(buf2));\r
-       }\r
-\r
-       sk_write(p->sub_socket, "\r\n", 2);\r
-\r
-       p->state = 1;\r
-       return 0;\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_CLOSING) {\r
-       /* if our proxy negotiation process involves closing and opening\r
-        * new sockets, then we would want to intercept this closing\r
-        * callback when we were expecting it. if we aren't anticipating\r
-        * a socket close, then some error must have occurred. we'll\r
-        * just pass those errors up to the backend.\r
-        */\r
-       return plug_closing(p->plug, p->closing_error_msg,\r
-                           p->closing_error_code,\r
-                           p->closing_calling_back);\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_SENT) {\r
-       /* some (or all) of what we wrote to the proxy was sent.\r
-        * we don't do anything new, however, until we receive the\r
-        * proxy's response. we might want to set a timer so we can\r
-        * timeout the proxy negotiation after a while...\r
-        */\r
-       return 0;\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_ACCEPTING) {\r
-       /* we should _never_ see this, as we are using our socket to\r
-        * connect to a proxy, not accepting inbound connections.\r
-        * what should we do? close the socket with an appropriate\r
-        * error message?\r
-        */\r
-       return plug_accepting(p->plug, p->accepting_sock);\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_RECEIVE) {\r
-       /* we have received data from the underlying socket, which\r
-        * we'll need to parse, process, and respond to appropriately.\r
-        */\r
-\r
-       char *data, *datap;\r
-       int len;\r
-       int eol;\r
-\r
-       if (p->state == 1) {\r
-\r
-           int min_ver, maj_ver, status;\r
-\r
-           /* get the status line */\r
-           len = bufchain_size(&p->pending_input_data);\r
-           assert(len > 0);           /* or we wouldn't be here */\r
-           data = snewn(len+1, char);\r
-           bufchain_fetch(&p->pending_input_data, data, len);\r
-           /*\r
-            * We must NUL-terminate this data, because Windows\r
-            * sscanf appears to require a NUL at the end of the\r
-            * string because it strlens it _first_. Sigh.\r
-            */\r
-           data[len] = '\0';\r
-\r
-           eol = get_line_end(data, len);\r
-           if (eol < 0) {\r
-               sfree(data);\r
-               return 1;\r
-           }\r
-\r
-           status = -1;\r
-           /* We can't rely on whether the %n incremented the sscanf return */\r
-           if (sscanf((char *)data, "HTTP/%i.%i %n",\r
-                      &maj_ver, &min_ver, &status) < 2 || status == -1) {\r
-               plug_closing(p->plug, "Proxy error: HTTP response was absent",\r
-                            PROXY_ERROR_GENERAL, 0);\r
-               sfree(data);\r
-               return 1;\r
-           }\r
-\r
-           /* remove the status line from the input buffer. */\r
-           bufchain_consume(&p->pending_input_data, eol);\r
-           if (data[status] != '2') {\r
-               /* error */\r
-               char *buf;\r
-               data[eol] = '\0';\r
-               while (eol > status &&\r
-                      (data[eol-1] == '\r' || data[eol-1] == '\n'))\r
-                   data[--eol] = '\0';\r
-               buf = dupprintf("Proxy error: %s", data+status);\r
-               plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);\r
-               sfree(buf);\r
-               sfree(data);\r
-               return 1;\r
-           }\r
-\r
-           sfree(data);\r
-\r
-           p->state = 2;\r
-       }\r
-\r
-       if (p->state == 2) {\r
-\r
-           /* get headers. we're done when we get a\r
-            * header of length 2, (ie. just "\r\n")\r
-            */\r
-\r
-           len = bufchain_size(&p->pending_input_data);\r
-           assert(len > 0);           /* or we wouldn't be here */\r
-           data = snewn(len, char);\r
-           datap = data;\r
-           bufchain_fetch(&p->pending_input_data, data, len);\r
-\r
-           eol = get_line_end(datap, len);\r
-           if (eol < 0) {\r
-               sfree(data);\r
-               return 1;\r
-           }\r
-           while (eol > 2)\r
-           {\r
-               bufchain_consume(&p->pending_input_data, eol);\r
-               datap += eol;\r
-               len   -= eol;\r
-               eol = get_line_end(datap, len);\r
-           }\r
-\r
-           if (eol == 2) {\r
-               /* we're done */\r
-               bufchain_consume(&p->pending_input_data, 2);\r
-               proxy_activate(p);\r
-               /* proxy activate will have dealt with\r
-                * whatever is left of the buffer */\r
-               sfree(data);\r
-               return 1;\r
-           }\r
-\r
-           sfree(data);\r
-           return 1;\r
-       }\r
-    }\r
-\r
-    plug_closing(p->plug, "Proxy error: unexpected proxy error",\r
-                PROXY_ERROR_UNEXPECTED, 0);\r
-    return 1;\r
-}\r
-\r
-/* ----------------------------------------------------------------------\r
- * SOCKS proxy type.\r
- */\r
-\r
-/* SOCKS version 4 */\r
-int proxy_socks4_negotiate (Proxy_Socket p, int change)\r
-{\r
-    if (p->state == PROXY_CHANGE_NEW) {\r
-\r
-       /* request format:\r
-        *  version number (1 byte) = 4\r
-        *  command code (1 byte)\r
-        *    1 = CONNECT\r
-        *    2 = BIND\r
-        *  dest. port (2 bytes) [network order]\r
-        *  dest. address (4 bytes)\r
-        *  user ID (variable length, null terminated string)\r
-        */\r
-\r
-       int length, type, namelen;\r
-       char *command, addr[4], hostname[512];\r
-\r
-       type = sk_addrtype(p->remote_addr);\r
-       if (type == ADDRTYPE_IPV6) {\r
-           plug_closing(p->plug, "Proxy error: SOCKS version 4 does"\r
-                        " not support IPv6", PROXY_ERROR_GENERAL, 0);\r
-           return 1;\r
-       } else if (type == ADDRTYPE_IPV4) {\r
-           namelen = 0;\r
-           sk_addrcopy(p->remote_addr, addr);\r
-       } else {                       /* type == ADDRTYPE_NAME */\r
-           assert(type == ADDRTYPE_NAME);\r
-           sk_getaddr(p->remote_addr, hostname, lenof(hostname));\r
-           namelen = strlen(hostname) + 1;   /* include the NUL */\r
-           addr[0] = addr[1] = addr[2] = 0;\r
-           addr[3] = 1;\r
-       }\r
-\r
-       length = strlen(p->cfg.proxy_username) + namelen + 9;\r
-       command = snewn(length, char);\r
-       strcpy(command + 8, p->cfg.proxy_username);\r
-\r
-       command[0] = 4; /* version 4 */\r
-       command[1] = 1; /* CONNECT command */\r
-\r
-       /* port */\r
-       command[2] = (char) (p->remote_port >> 8) & 0xff;\r
-       command[3] = (char) p->remote_port & 0xff;\r
-\r
-       /* address */\r
-       memcpy(command + 4, addr, 4);\r
-\r
-       /* hostname */\r
-       memcpy(command + 8 + strlen(p->cfg.proxy_username) + 1,\r
-              hostname, namelen);\r
-\r
-       sk_write(p->sub_socket, command, length);\r
-       sfree(command);\r
-\r
-       p->state = 1;\r
-       return 0;\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_CLOSING) {\r
-       /* if our proxy negotiation process involves closing and opening\r
-        * new sockets, then we would want to intercept this closing\r
-        * callback when we were expecting it. if we aren't anticipating\r
-        * a socket close, then some error must have occurred. we'll\r
-        * just pass those errors up to the backend.\r
-        */\r
-       return plug_closing(p->plug, p->closing_error_msg,\r
-                           p->closing_error_code,\r
-                           p->closing_calling_back);\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_SENT) {\r
-       /* some (or all) of what we wrote to the proxy was sent.\r
-        * we don't do anything new, however, until we receive the\r
-        * proxy's response. we might want to set a timer so we can\r
-        * timeout the proxy negotiation after a while...\r
-        */\r
-       return 0;\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_ACCEPTING) {\r
-       /* we should _never_ see this, as we are using our socket to\r
-        * connect to a proxy, not accepting inbound connections.\r
-        * what should we do? close the socket with an appropriate\r
-        * error message?\r
-        */\r
-       return plug_accepting(p->plug, p->accepting_sock);\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_RECEIVE) {\r
-       /* we have received data from the underlying socket, which\r
-        * we'll need to parse, process, and respond to appropriately.\r
-        */\r
-\r
-       if (p->state == 1) {\r
-           /* response format:\r
-            *  version number (1 byte) = 4\r
-            *  reply code (1 byte)\r
-            *    90 = request granted\r
-            *    91 = request rejected or failed\r
-            *    92 = request rejected due to lack of IDENTD on client\r
-            *    93 = request rejected due to difference in user ID \r
-            *         (what we sent vs. what IDENTD said)\r
-            *  dest. port (2 bytes)\r
-            *  dest. address (4 bytes)\r
-            */\r
-\r
-           char data[8];\r
-\r
-           if (bufchain_size(&p->pending_input_data) < 8)\r
-               return 1;              /* not got anything yet */\r
-           \r
-           /* get the response */\r
-           bufchain_fetch(&p->pending_input_data, data, 8);\r
-\r
-           if (data[0] != 0) {\r
-               plug_closing(p->plug, "Proxy error: SOCKS proxy responded with "\r
-                                     "unexpected reply code version",\r
-                            PROXY_ERROR_GENERAL, 0);\r
-               return 1;\r
-           }\r
-\r
-           if (data[1] != 90) {\r
-\r
-               switch (data[1]) {\r
-                 case 92:\r
-                   plug_closing(p->plug, "Proxy error: SOCKS server wanted IDENTD on client",\r
-                                PROXY_ERROR_GENERAL, 0);\r
-                   break;\r
-                 case 93:\r
-                   plug_closing(p->plug, "Proxy error: Username and IDENTD on client don't agree",\r
-                                PROXY_ERROR_GENERAL, 0);\r
-                   break;\r
-                 case 91:\r
-                 default:\r
-                   plug_closing(p->plug, "Proxy error: Error while communicating with proxy",\r
-                                PROXY_ERROR_GENERAL, 0);\r
-                   break;\r
-               }\r
-\r
-               return 1;\r
-           }\r
-           bufchain_consume(&p->pending_input_data, 8);\r
-\r
-           /* we're done */\r
-           proxy_activate(p);\r
-           /* proxy activate will have dealt with\r
-            * whatever is left of the buffer */\r
-           return 1;\r
-       }\r
-    }\r
-\r
-    plug_closing(p->plug, "Proxy error: unexpected proxy error",\r
-                PROXY_ERROR_UNEXPECTED, 0);\r
-    return 1;\r
-}\r
-\r
-/* SOCKS version 5 */\r
-int proxy_socks5_negotiate (Proxy_Socket p, int change)\r
-{\r
-    if (p->state == PROXY_CHANGE_NEW) {\r
-\r
-       /* initial command:\r
-        *  version number (1 byte) = 5\r
-        *  number of available authentication methods (1 byte)\r
-        *  available authentication methods (1 byte * previous value)\r
-        *    authentication methods:\r
-        *     0x00 = no authentication\r
-        *     0x01 = GSSAPI\r
-        *     0x02 = username/password\r
-        *     0x03 = CHAP\r
-        */\r
-\r
-       char command[5];\r
-       int len;\r
-\r
-       command[0] = 5; /* version 5 */\r
-       if (p->cfg.proxy_username[0] || p->cfg.proxy_password[0]) {\r
-           command[2] = 0x00;         /* no authentication */\r
-           len = 3;\r
-           proxy_socks5_offerencryptedauth (command, &len);\r
-           command[len++] = 0x02;             /* username/password */\r
-           command[1] = len - 2;       /* Number of methods supported */\r
-       } else {\r
-           command[1] = 1;            /* one methods supported: */\r
-           command[2] = 0x00;         /* no authentication */\r
-           len = 3;\r
-       }\r
-\r
-       sk_write(p->sub_socket, command, len);\r
-\r
-       p->state = 1;\r
-       return 0;\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_CLOSING) {\r
-       /* if our proxy negotiation process involves closing and opening\r
-        * new sockets, then we would want to intercept this closing\r
-        * callback when we were expecting it. if we aren't anticipating\r
-        * a socket close, then some error must have occurred. we'll\r
-        * just pass those errors up to the backend.\r
-        */\r
-       return plug_closing(p->plug, p->closing_error_msg,\r
-                           p->closing_error_code,\r
-                           p->closing_calling_back);\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_SENT) {\r
-       /* some (or all) of what we wrote to the proxy was sent.\r
-        * we don't do anything new, however, until we receive the\r
-        * proxy's response. we might want to set a timer so we can\r
-        * timeout the proxy negotiation after a while...\r
-        */\r
-       return 0;\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_ACCEPTING) {\r
-       /* we should _never_ see this, as we are using our socket to\r
-        * connect to a proxy, not accepting inbound connections.\r
-        * what should we do? close the socket with an appropriate\r
-        * error message?\r
-        */\r
-       return plug_accepting(p->plug, p->accepting_sock);\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_RECEIVE) {\r
-       /* we have received data from the underlying socket, which\r
-        * we'll need to parse, process, and respond to appropriately.\r
-        */\r
-\r
-       if (p->state == 1) {\r
-\r
-           /* initial response:\r
-            *  version number (1 byte) = 5\r
-            *  authentication method (1 byte)\r
-            *    authentication methods:\r
-            *     0x00 = no authentication\r
-            *     0x01 = GSSAPI\r
-            *     0x02 = username/password\r
-            *     0x03 = CHAP\r
-            *     0xff = no acceptable methods\r
-            */\r
-           char data[2];\r
-\r
-           if (bufchain_size(&p->pending_input_data) < 2)\r
-               return 1;              /* not got anything yet */\r
-\r
-           /* get the response */\r
-           bufchain_fetch(&p->pending_input_data, data, 2);\r
-\r
-           if (data[0] != 5) {\r
-               plug_closing(p->plug, "Proxy error: SOCKS proxy returned unexpected version",\r
-                            PROXY_ERROR_GENERAL, 0);\r
-               return 1;\r
-           }\r
-\r
-           if (data[1] == 0x00) p->state = 2; /* no authentication needed */\r
-           else if (data[1] == 0x01) p->state = 4; /* GSSAPI authentication */\r
-           else if (data[1] == 0x02) p->state = 5; /* username/password authentication */\r
-           else if (data[1] == 0x03) p->state = 6; /* CHAP authentication */\r
-           else {\r
-               plug_closing(p->plug, "Proxy error: SOCKS proxy did not accept our authentication",\r
-                            PROXY_ERROR_GENERAL, 0);\r
-               return 1;\r
-           }\r
-           bufchain_consume(&p->pending_input_data, 2);\r
-       }\r
-\r
-       if (p->state == 7) {\r
-\r
-           /* password authentication reply format:\r
-            *  version number (1 bytes) = 1\r
-            *  reply code (1 byte)\r
-            *    0 = succeeded\r
-            *    >0 = failed\r
-            */\r
-           char data[2];\r
-\r
-           if (bufchain_size(&p->pending_input_data) < 2)\r
-               return 1;              /* not got anything yet */\r
-\r
-           /* get the response */\r
-           bufchain_fetch(&p->pending_input_data, data, 2);\r
-\r
-           if (data[0] != 1) {\r
-               plug_closing(p->plug, "Proxy error: SOCKS password "\r
-                            "subnegotiation contained wrong version number",\r
-                            PROXY_ERROR_GENERAL, 0);\r
-               return 1;\r
-           }\r
-\r
-           if (data[1] != 0) {\r
-\r
-               plug_closing(p->plug, "Proxy error: SOCKS proxy refused"\r
-                            " password authentication",\r
-                            PROXY_ERROR_GENERAL, 0);\r
-               return 1;\r
-           }\r
-\r
-           bufchain_consume(&p->pending_input_data, 2);\r
-           p->state = 2;              /* now proceed as authenticated */\r
-       }\r
-\r
-       if (p->state == 8) {\r
-           int ret;\r
-           ret = proxy_socks5_handlechap(p);\r
-           if (ret) return ret;\r
-       }\r
-\r
-       if (p->state == 2) {\r
-\r
-           /* request format:\r
-            *  version number (1 byte) = 5\r
-            *  command code (1 byte)\r
-            *    1 = CONNECT\r
-            *    2 = BIND\r
-            *    3 = UDP ASSOCIATE\r
-            *  reserved (1 byte) = 0x00\r
-            *  address type (1 byte)\r
-            *    1 = IPv4\r
-            *    3 = domainname (first byte has length, no terminating null)\r
-            *    4 = IPv6\r
-            *  dest. address (variable)\r
-            *  dest. port (2 bytes) [network order]\r
-            */\r
-\r
-           char command[512];\r
-           int len;\r
-           int type;\r
-\r
-           type = sk_addrtype(p->remote_addr);\r
-           if (type == ADDRTYPE_IPV4) {\r
-               len = 10;              /* 4 hdr + 4 addr + 2 trailer */\r
-               command[3] = 1; /* IPv4 */\r
-               sk_addrcopy(p->remote_addr, command+4);\r
-           } else if (type == ADDRTYPE_IPV6) {\r
-               len = 22;              /* 4 hdr + 16 addr + 2 trailer */\r
-               command[3] = 4; /* IPv6 */\r
-               sk_addrcopy(p->remote_addr, command+4);\r
-           } else {\r
-               assert(type == ADDRTYPE_NAME);\r
-               command[3] = 3;\r
-               sk_getaddr(p->remote_addr, command+5, 256);\r
-               command[4] = strlen(command+5);\r
-               len = 7 + command[4];  /* 4 hdr, 1 len, N addr, 2 trailer */\r
-           }\r
-\r
-           command[0] = 5; /* version 5 */\r
-           command[1] = 1; /* CONNECT command */\r
-           command[2] = 0x00;\r
-\r
-           /* port */\r
-           command[len-2] = (char) (p->remote_port >> 8) & 0xff;\r
-           command[len-1] = (char) p->remote_port & 0xff;\r
-\r
-           sk_write(p->sub_socket, command, len);\r
-\r
-           p->state = 3;\r
-           return 1;\r
-       }\r
-\r
-       if (p->state == 3) {\r
-\r
-           /* reply format:\r
-            *  version number (1 bytes) = 5\r
-            *  reply code (1 byte)\r
-            *    0 = succeeded\r
-            *    1 = general SOCKS server failure\r
-            *    2 = connection not allowed by ruleset\r
-            *    3 = network unreachable\r
-            *    4 = host unreachable\r
-            *    5 = connection refused\r
-            *    6 = TTL expired\r
-            *    7 = command not supported\r
-            *    8 = address type not supported\r
-            * reserved (1 byte) = x00\r
-            * address type (1 byte)\r
-            *    1 = IPv4\r
-            *    3 = domainname (first byte has length, no terminating null)\r
-            *    4 = IPv6\r
-            * server bound address (variable)\r
-            * server bound port (2 bytes) [network order]\r
-            */\r
-           char data[5];\r
-           int len;\r
-\r
-           /* First 5 bytes of packet are enough to tell its length. */ \r
-           if (bufchain_size(&p->pending_input_data) < 5)\r
-               return 1;              /* not got anything yet */\r
-\r
-           /* get the response */\r
-           bufchain_fetch(&p->pending_input_data, data, 5);\r
-\r
-           if (data[0] != 5) {\r
-               plug_closing(p->plug, "Proxy error: SOCKS proxy returned wrong version number",\r
-                            PROXY_ERROR_GENERAL, 0);\r
-               return 1;\r
-           }\r
-\r
-           if (data[1] != 0) {\r
-               char buf[256];\r
-\r
-               strcpy(buf, "Proxy error: ");\r
-\r
-               switch (data[1]) {\r
-                 case 1: strcat(buf, "General SOCKS server failure"); break;\r
-                 case 2: strcat(buf, "Connection not allowed by ruleset"); break;\r
-                 case 3: strcat(buf, "Network unreachable"); break;\r
-                 case 4: strcat(buf, "Host unreachable"); break;\r
-                 case 5: strcat(buf, "Connection refused"); break;\r
-                 case 6: strcat(buf, "TTL expired"); break;\r
-                 case 7: strcat(buf, "Command not supported"); break;\r
-                 case 8: strcat(buf, "Address type not supported"); break;\r
-                 default: sprintf(buf+strlen(buf),\r
-                                  "Unrecognised SOCKS error code %d",\r
-                                  data[1]);\r
-                   break;\r
-               }\r
-               plug_closing(p->plug, buf, PROXY_ERROR_GENERAL, 0);\r
-\r
-               return 1;\r
-           }\r
-\r
-           /*\r
-            * Eat the rest of the reply packet.\r
-            */\r
-           len = 6;                   /* first 4 bytes, last 2 */\r
-           switch (data[3]) {\r
-             case 1: len += 4; break; /* IPv4 address */\r
-             case 4: len += 16; break;/* IPv6 address */\r
-             case 3: len += (unsigned char)data[4]; break; /* domain name */\r
-             default:\r
-               plug_closing(p->plug, "Proxy error: SOCKS proxy returned "\r
-                            "unrecognised address format",\r
-                            PROXY_ERROR_GENERAL, 0);\r
-               return 1;\r
-           }\r
-           if (bufchain_size(&p->pending_input_data) < len)\r
-               return 1;              /* not got whole reply yet */\r
-           bufchain_consume(&p->pending_input_data, len);\r
-\r
-           /* we're done */\r
-           proxy_activate(p);\r
-           return 1;\r
-       }\r
-\r
-       if (p->state == 4) {\r
-           /* TODO: Handle GSSAPI authentication */\r
-           plug_closing(p->plug, "Proxy error: We don't support GSSAPI authentication",\r
-                        PROXY_ERROR_GENERAL, 0);\r
-           return 1;\r
-       }\r
-\r
-       if (p->state == 5) {\r
-           if (p->cfg.proxy_username[0] || p->cfg.proxy_password[0]) {\r
-               char userpwbuf[514];\r
-               int ulen, plen;\r
-               ulen = strlen(p->cfg.proxy_username);\r
-               if (ulen > 255) ulen = 255; if (ulen < 1) ulen = 1;\r
-               plen = strlen(p->cfg.proxy_password);\r
-               if (plen > 255) plen = 255; if (plen < 1) plen = 1;\r
-               userpwbuf[0] = 1;      /* version number of subnegotiation */\r
-               userpwbuf[1] = ulen;\r
-               memcpy(userpwbuf+2, p->cfg.proxy_username, ulen);\r
-               userpwbuf[ulen+2] = plen;\r
-               memcpy(userpwbuf+ulen+3, p->cfg.proxy_password, plen);\r
-               sk_write(p->sub_socket, userpwbuf, ulen + plen + 3);\r
-               p->state = 7;\r
-           } else \r
-               plug_closing(p->plug, "Proxy error: Server chose "\r
-                            "username/password authentication but we "\r
-                            "didn't offer it!",\r
-                        PROXY_ERROR_GENERAL, 0);\r
-           return 1;\r
-       }\r
-\r
-       if (p->state == 6) {\r
-           int ret;\r
-           ret = proxy_socks5_selectchap(p);\r
-           if (ret) return ret;\r
-       }\r
-\r
-    }\r
-\r
-    plug_closing(p->plug, "Proxy error: Unexpected proxy error",\r
-                PROXY_ERROR_UNEXPECTED, 0);\r
-    return 1;\r
-}\r
-\r
-/* ----------------------------------------------------------------------\r
- * `Telnet' proxy type.\r
- *\r
- * (This is for ad-hoc proxies where you connect to the proxy's\r
- * telnet port and send a command such as `connect host port'. The\r
- * command is configurable, since this proxy type is typically not\r
- * standardised or at all well-defined.)\r
- */\r
-\r
-char *format_telnet_command(SockAddr addr, int port, const Config *cfg)\r
-{\r
-    char *ret = NULL;\r
-    int retlen = 0, retsize = 0;\r
-    int so = 0, eo = 0;\r
-#define ENSURE(n) do { \\r
-    if (retsize < retlen + n) { \\r
-       retsize = retlen + n + 512; \\r
-       ret = sresize(ret, retsize, char); \\r
-    } \\r
-} while (0)\r
-\r
-    /* we need to escape \\, \%, \r, \n, \t, \x??, \0???, \r
-     * %%, %host, %port, %user, and %pass\r
-     */\r
-\r
-    while (cfg->proxy_telnet_command[eo] != 0) {\r
-\r
-       /* scan forward until we hit end-of-line,\r
-        * or an escape character (\ or %) */\r
-       while (cfg->proxy_telnet_command[eo] != 0 &&\r
-              cfg->proxy_telnet_command[eo] != '%' &&\r
-              cfg->proxy_telnet_command[eo] != '\\') eo++;\r
-\r
-       /* if we hit eol, break out of our escaping loop */\r
-       if (cfg->proxy_telnet_command[eo] == 0) break;\r
-\r
-       /* if there was any unescaped text before the escape\r
-        * character, send that now */\r
-       if (eo != so) {\r
-           ENSURE(eo - so);\r
-           memcpy(ret + retlen, cfg->proxy_telnet_command + so, eo - so);\r
-           retlen += eo - so;\r
-       }\r
-\r
-       so = eo++;\r
-\r
-       /* if the escape character was the last character of\r
-        * the line, we'll just stop and send it. */\r
-       if (cfg->proxy_telnet_command[eo] == 0) break;\r
-\r
-       if (cfg->proxy_telnet_command[so] == '\\') {\r
-\r
-           /* we recognize \\, \%, \r, \n, \t, \x??.\r
-            * anything else, we just send unescaped (including the \).\r
-            */\r
-\r
-           switch (cfg->proxy_telnet_command[eo]) {\r
-\r
-             case '\\':\r
-               ENSURE(1);\r
-               ret[retlen++] = '\\';\r
-               eo++;\r
-               break;\r
-\r
-             case '%':\r
-               ENSURE(1);\r
-               ret[retlen++] = '%';\r
-               eo++;\r
-               break;\r
-\r
-             case 'r':\r
-               ENSURE(1);\r
-               ret[retlen++] = '\r';\r
-               eo++;\r
-               break;\r
-\r
-             case 'n':\r
-               ENSURE(1);\r
-               ret[retlen++] = '\n';\r
-               eo++;\r
-               break;\r
-\r
-             case 't':\r
-               ENSURE(1);\r
-               ret[retlen++] = '\t';\r
-               eo++;\r
-               break;\r
-\r
-             case 'x':\r
-             case 'X':\r
-               {\r
-                   /* escaped hexadecimal value (ie. \xff) */\r
-                   unsigned char v = 0;\r
-                   int i = 0;\r
-\r
-                   for (;;) {\r
-                       eo++;\r
-                       if (cfg->proxy_telnet_command[eo] >= '0' &&\r
-                           cfg->proxy_telnet_command[eo] <= '9')\r
-                           v += cfg->proxy_telnet_command[eo] - '0';\r
-                       else if (cfg->proxy_telnet_command[eo] >= 'a' &&\r
-                                cfg->proxy_telnet_command[eo] <= 'f')\r
-                           v += cfg->proxy_telnet_command[eo] - 'a' + 10;\r
-                       else if (cfg->proxy_telnet_command[eo] >= 'A' &&\r
-                                cfg->proxy_telnet_command[eo] <= 'F')\r
-                           v += cfg->proxy_telnet_command[eo] - 'A' + 10;\r
-                       else {\r
-                           /* non hex character, so we abort and just\r
-                            * send the whole thing unescaped (including \x)\r
-                            */\r
-                           ENSURE(1);\r
-                           ret[retlen++] = '\\';\r
-                           eo = so + 1;\r
-                           break;\r
-                       }\r
-\r
-                       /* we only extract two hex characters */\r
-                       if (i == 1) {\r
-                           ENSURE(1);\r
-                           ret[retlen++] = v;\r
-                           eo++;\r
-                           break;\r
-                       }\r
-\r
-                       i++;\r
-                       v <<= 4;\r
-                   }\r
-               }\r
-               break;\r
-\r
-             default:\r
-               ENSURE(2);\r
-               memcpy(ret+retlen, cfg->proxy_telnet_command + so, 2);\r
-               retlen += 2;\r
-               eo++;\r
-               break;\r
-           }\r
-       } else {\r
-\r
-           /* % escape. we recognize %%, %host, %port, %user, %pass.\r
-            * %proxyhost, %proxyport. Anything else we just send\r
-            * unescaped (including the %).\r
-            */\r
-\r
-           if (cfg->proxy_telnet_command[eo] == '%') {\r
-               ENSURE(1);\r
-               ret[retlen++] = '%';\r
-               eo++;\r
-           }\r
-           else if (strnicmp(cfg->proxy_telnet_command + eo,\r
-                             "host", 4) == 0) {\r
-               char dest[512];\r
-               int destlen;\r
-               sk_getaddr(addr, dest, lenof(dest));\r
-               destlen = strlen(dest);\r
-               ENSURE(destlen);\r
-               memcpy(ret+retlen, dest, destlen);\r
-               retlen += destlen;\r
-               eo += 4;\r
-           }\r
-           else if (strnicmp(cfg->proxy_telnet_command + eo,\r
-                             "port", 4) == 0) {\r
-               char portstr[8], portlen;\r
-               portlen = sprintf(portstr, "%i", port);\r
-               ENSURE(portlen);\r
-               memcpy(ret + retlen, portstr, portlen);\r
-               retlen += portlen;\r
-               eo += 4;\r
-           }\r
-           else if (strnicmp(cfg->proxy_telnet_command + eo,\r
-                             "user", 4) == 0) {\r
-               int userlen = strlen(cfg->proxy_username);\r
-               ENSURE(userlen);\r
-               memcpy(ret+retlen, cfg->proxy_username, userlen);\r
-               retlen += userlen;\r
-               eo += 4;\r
-           }\r
-           else if (strnicmp(cfg->proxy_telnet_command + eo,\r
-                             "pass", 4) == 0) {\r
-               int passlen = strlen(cfg->proxy_password);\r
-               ENSURE(passlen);\r
-               memcpy(ret+retlen, cfg->proxy_password, passlen);\r
-               retlen += passlen;\r
-               eo += 4;\r
-           }\r
-           else if (strnicmp(cfg->proxy_telnet_command + eo,\r
-                             "proxyhost", 9) == 0) {\r
-               int phlen = strlen(cfg->proxy_host);\r
-               ENSURE(phlen);\r
-               memcpy(ret+retlen, cfg->proxy_host, phlen);\r
-               retlen += phlen;\r
-               eo += 9;\r
-           }\r
-           else if (strnicmp(cfg->proxy_telnet_command + eo,\r
-                             "proxyport", 9) == 0) {\r
-                char pport[50];\r
-               int pplen;\r
-                sprintf(pport, "%d", cfg->proxy_port);\r
-                pplen = strlen(pport);\r
-               ENSURE(pplen);\r
-               memcpy(ret+retlen, pport, pplen);\r
-               retlen += pplen;\r
-               eo += 9;\r
-           }\r
-           else {\r
-               /* we don't escape this, so send the % now, and\r
-                * don't advance eo, so that we'll consider the\r
-                * text immediately following the % as unescaped.\r
-                */\r
-               ENSURE(1);\r
-               ret[retlen++] = '%';\r
-           }\r
-       }\r
-\r
-       /* resume scanning for additional escapes after this one. */\r
-       so = eo;\r
-    }\r
-\r
-    /* if there is any unescaped text at the end of the line, send it */\r
-    if (eo != so) {\r
-       ENSURE(eo - so);\r
-       memcpy(ret + retlen, cfg->proxy_telnet_command + so, eo - so);\r
-       retlen += eo - so;\r
-    }\r
-\r
-    ENSURE(1);\r
-    ret[retlen] = '\0';\r
-    return ret;\r
-\r
-#undef ENSURE\r
-}\r
-\r
-int proxy_telnet_negotiate (Proxy_Socket p, int change)\r
-{\r
-    if (p->state == PROXY_CHANGE_NEW) {\r
-       char *formatted_cmd;\r
-\r
-       formatted_cmd = format_telnet_command(p->remote_addr, p->remote_port,\r
-                                             &p->cfg);\r
-\r
-       sk_write(p->sub_socket, formatted_cmd, strlen(formatted_cmd));\r
-       sfree(formatted_cmd);\r
-\r
-       p->state = 1;\r
-       return 0;\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_CLOSING) {\r
-       /* if our proxy negotiation process involves closing and opening\r
-        * new sockets, then we would want to intercept this closing\r
-        * callback when we were expecting it. if we aren't anticipating\r
-        * a socket close, then some error must have occurred. we'll\r
-        * just pass those errors up to the backend.\r
-        */\r
-       return plug_closing(p->plug, p->closing_error_msg,\r
-                           p->closing_error_code,\r
-                           p->closing_calling_back);\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_SENT) {\r
-       /* some (or all) of what we wrote to the proxy was sent.\r
-        * we don't do anything new, however, until we receive the\r
-        * proxy's response. we might want to set a timer so we can\r
-        * timeout the proxy negotiation after a while...\r
-        */\r
-       return 0;\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_ACCEPTING) {\r
-       /* we should _never_ see this, as we are using our socket to\r
-        * connect to a proxy, not accepting inbound connections.\r
-        * what should we do? close the socket with an appropriate\r
-        * error message?\r
-        */\r
-       return plug_accepting(p->plug, p->accepting_sock);\r
-    }\r
-\r
-    if (change == PROXY_CHANGE_RECEIVE) {\r
-       /* we have received data from the underlying socket, which\r
-        * we'll need to parse, process, and respond to appropriately.\r
-        */\r
-\r
-       /* we're done */\r
-       proxy_activate(p);\r
-       /* proxy activate will have dealt with\r
-        * whatever is left of the buffer */\r
-       return 1;\r
-    }\r
-\r
-    plug_closing(p->plug, "Proxy error: Unexpected proxy error",\r
-                PROXY_ERROR_UNEXPECTED, 0);\r
-    return 1;\r
-}\r