Fix line endings (svn:eol-style properties and actual CRs).
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Mon, 28 Aug 2006 11:13:56 +0000 (11:13 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Mon, 28 Aug 2006 11:13:56 +0000 (11:13 +0000)
git-svn-id: svn://svn.tartarus.org/sgt/putty@6817 cda61777-01e9-0310-a592-d414129be87e

sercfg.c
windows/winproxy.c
windows/winser.c

index c75879c..10b582f 100644 (file)
--- a/sercfg.c
+++ b/sercfg.c
-/*\r
- * sercfg.c - the serial-port specific parts of the PuTTY\r
- * configuration box. Centralised as cross-platform code because\r
- * more than one platform will want to use it, but not part of the\r
- * main configuration. The expectation is that each platform's\r
- * local config function will call out to ser_setup_config_box() if\r
- * it needs to set up the standard serial stuff. (Of course, it can\r
- * then apply local tweaks after ser_setup_config_box() returns, if\r
- * it needs to.)\r
- */\r
-\r
-#include <assert.h>\r
-#include <stdlib.h>\r
-\r
-#include "putty.h"\r
-#include "dialog.h"\r
-#include "storage.h"\r
-\r
-static void serial_parity_handler(union control *ctrl, void *dlg,\r
-                                 void *data, int event)\r
-{\r
-    static const struct {\r
-       const char *name;\r
-       int val;\r
-    } parities[] = {\r
-       {"None", SER_PAR_NONE},\r
-       {"Odd", SER_PAR_ODD},\r
-       {"Even", SER_PAR_EVEN},\r
-       {"Mark", SER_PAR_MARK},\r
-       {"Space", SER_PAR_SPACE},\r
-    };\r
-    int i;\r
-    Config *cfg = (Config *)data;\r
-\r
-    if (event == EVENT_REFRESH) {\r
-       dlg_update_start(ctrl, dlg);\r
-       dlg_listbox_clear(ctrl, dlg);\r
-       for (i = 0; i < lenof(parities); i++)\r
-           dlg_listbox_addwithid(ctrl, dlg, parities[i].name,\r
-                                 parities[i].val);\r
-       for (i = 0; i < lenof(parities); i++)\r
-           if (cfg->serparity == parities[i].val)\r
-               dlg_listbox_select(ctrl, dlg, i);\r
-       dlg_update_done(ctrl, dlg);\r
-    } else if (event == EVENT_SELCHANGE) {\r
-       int i = dlg_listbox_index(ctrl, dlg);\r
-       if (i < 0)\r
-           i = SER_PAR_NONE;\r
-       else\r
-           i = dlg_listbox_getid(ctrl, dlg, i);\r
-       cfg->serparity = i;\r
-    }\r
-}\r
-\r
-static void serial_flow_handler(union control *ctrl, void *dlg,\r
-                               void *data, int event)\r
-{\r
-    static const struct {\r
-       const char *name;\r
-       int val;\r
-    } flows[] = {\r
-       {"None", SER_FLOW_NONE},\r
-       {"XON/XOFF", SER_FLOW_XONXOFF},\r
-       {"RTS/CTS", SER_FLOW_RTSCTS},\r
-       {"DSR/DTR", SER_FLOW_DSRDTR},\r
-    };\r
-    int i;\r
-    Config *cfg = (Config *)data;\r
-\r
-    if (event == EVENT_REFRESH) {\r
-       dlg_update_start(ctrl, dlg);\r
-       dlg_listbox_clear(ctrl, dlg);\r
-       for (i = 0; i < lenof(flows); i++)\r
-           dlg_listbox_addwithid(ctrl, dlg, flows[i].name,\r
-                                 flows[i].val);\r
-       for (i = 0; i < lenof(flows); i++)\r
-           if (cfg->serflow == flows[i].val)\r
-               dlg_listbox_select(ctrl, dlg, i);\r
-       dlg_update_done(ctrl, dlg);\r
-    } else if (event == EVENT_SELCHANGE) {\r
-       int i = dlg_listbox_index(ctrl, dlg);\r
-       if (i < 0)\r
-           i = SER_PAR_NONE;\r
-       else\r
-           i = dlg_listbox_getid(ctrl, dlg, i);\r
-       cfg->serflow = i;\r
-    }\r
-}\r
-\r
-void ser_setup_config_box(struct controlbox *b, int midsession)\r
-{\r
-    struct controlset *s;\r
-    union control *c;\r
-\r
-    /*\r
-     * Add the serial back end to the protocols list at the top of\r
-     * the config box.\r
-     */\r
-    s = ctrl_getset(b, "Session", "hostport",\r
-                   "Specify your connection by host name or IP address");\r
-    {\r
-       int i;\r
-       extern void config_protocolbuttons_handler(union control *, void *,\r
-                                                  void *, int);\r
-        for (i = 0; i < s->ncontrols; i++) {\r
-            c = s->ctrls[i];\r
-           if (c->generic.type == CTRL_RADIO &&\r
-               c->generic.handler == config_protocolbuttons_handler) {\r
-               c->radio.nbuttons++;\r
-               c->radio.ncolumns++;\r
-               c->radio.buttons =\r
-                   sresize(c->radio.buttons, c->radio.nbuttons, char *);\r
-               c->radio.buttons[c->radio.nbuttons-1] =\r
-                   dupstr("Serial");\r
-               c->radio.buttondata =\r
-                   sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);\r
-               c->radio.buttondata[c->radio.nbuttons-1] = I(PROT_SERIAL);\r
-               if (c->radio.shortcuts) {\r
-                   c->radio.shortcuts =\r
-                       sresize(c->radio.shortcuts, c->radio.nbuttons, char);\r
-                   c->radio.shortcuts[c->radio.nbuttons-1] = NO_SHORTCUT;\r
-               }\r
-           }\r
-       }\r
-    }\r
-\r
-    /*\r
-     * Entirely new Connection/Serial panel for serial port\r
-     * configuration.\r
-     */\r
-    ctrl_settitle(b, "Connection/Serial",\r
-                 "Options controlling local serial lines");\r
-\r
-    if (!midsession) {\r
-       /*\r
-        * We don't permit switching to a different serial port in\r
-        * midflight, although we do allow all other\r
-        * reconfiguration.\r
-        */\r
-       s = ctrl_getset(b, "Connection/Serial", "serline",\r
-                       "Select a serial line");\r
-       ctrl_editbox(s, "Serial line to connect to", 'l', 40,\r
-                    HELPCTX(serial_line),\r
-                    dlg_stdeditbox_handler, I(offsetof(Config,serline)),\r
-                    I(sizeof(((Config *)0)->serline)));\r
-    }\r
-\r
-    s = ctrl_getset(b, "Connection/Serial", "sercfg", "Configure the serial line");\r
-    ctrl_editbox(s, "Speed (baud)", 's', 40,\r
-                HELPCTX(serial_speed),\r
-                dlg_stdeditbox_handler, I(offsetof(Config,serspeed)), I(-1));\r
-    ctrl_editbox(s, "Data bits", 'b', 40,\r
-                HELPCTX(serial_databits),\r
-                dlg_stdeditbox_handler,I(offsetof(Config,serdatabits)),I(-1));\r
-    /*\r
-     * Stop bits come in units of one half.\r
-     */\r
-    ctrl_editbox(s, "Stop bits", 't', 40,\r
-                HELPCTX(serial_stopbits),\r
-                dlg_stdeditbox_handler,I(offsetof(Config,serstopbits)),I(-2));\r
-    ctrl_droplist(s, "Parity", 'p', 40,\r
-                 HELPCTX(serial_parity),\r
-                 serial_parity_handler, I(0));\r
-    ctrl_droplist(s, "Flow control", 'f', 40,\r
-                 HELPCTX(serial_flow),\r
-                 serial_flow_handler, I(0));\r
-}\r
+/*
+ * sercfg.c - the serial-port specific parts of the PuTTY
+ * configuration box. Centralised as cross-platform code because
+ * more than one platform will want to use it, but not part of the
+ * main configuration. The expectation is that each platform's
+ * local config function will call out to ser_setup_config_box() if
+ * it needs to set up the standard serial stuff. (Of course, it can
+ * then apply local tweaks after ser_setup_config_box() returns, if
+ * it needs to.)
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+
+#include "putty.h"
+#include "dialog.h"
+#include "storage.h"
+
+static void serial_parity_handler(union control *ctrl, void *dlg,
+                                 void *data, int event)
+{
+    static const struct {
+       const char *name;
+       int val;
+    } parities[] = {
+       {"None", SER_PAR_NONE},
+       {"Odd", SER_PAR_ODD},
+       {"Even", SER_PAR_EVEN},
+       {"Mark", SER_PAR_MARK},
+       {"Space", SER_PAR_SPACE},
+    };
+    int i;
+    Config *cfg = (Config *)data;
+
+    if (event == EVENT_REFRESH) {
+       dlg_update_start(ctrl, dlg);
+       dlg_listbox_clear(ctrl, dlg);
+       for (i = 0; i < lenof(parities); i++)
+           dlg_listbox_addwithid(ctrl, dlg, parities[i].name,
+                                 parities[i].val);
+       for (i = 0; i < lenof(parities); i++)
+           if (cfg->serparity == parities[i].val)
+               dlg_listbox_select(ctrl, dlg, i);
+       dlg_update_done(ctrl, dlg);
+    } else if (event == EVENT_SELCHANGE) {
+       int i = dlg_listbox_index(ctrl, dlg);
+       if (i < 0)
+           i = SER_PAR_NONE;
+       else
+           i = dlg_listbox_getid(ctrl, dlg, i);
+       cfg->serparity = i;
+    }
+}
+
+static void serial_flow_handler(union control *ctrl, void *dlg,
+                               void *data, int event)
+{
+    static const struct {
+       const char *name;
+       int val;
+    } flows[] = {
+       {"None", SER_FLOW_NONE},
+       {"XON/XOFF", SER_FLOW_XONXOFF},
+       {"RTS/CTS", SER_FLOW_RTSCTS},
+       {"DSR/DTR", SER_FLOW_DSRDTR},
+    };
+    int i;
+    Config *cfg = (Config *)data;
+
+    if (event == EVENT_REFRESH) {
+       dlg_update_start(ctrl, dlg);
+       dlg_listbox_clear(ctrl, dlg);
+       for (i = 0; i < lenof(flows); i++)
+           dlg_listbox_addwithid(ctrl, dlg, flows[i].name,
+                                 flows[i].val);
+       for (i = 0; i < lenof(flows); i++)
+           if (cfg->serflow == flows[i].val)
+               dlg_listbox_select(ctrl, dlg, i);
+       dlg_update_done(ctrl, dlg);
+    } else if (event == EVENT_SELCHANGE) {
+       int i = dlg_listbox_index(ctrl, dlg);
+       if (i < 0)
+           i = SER_PAR_NONE;
+       else
+           i = dlg_listbox_getid(ctrl, dlg, i);
+       cfg->serflow = i;
+    }
+}
+
+void ser_setup_config_box(struct controlbox *b, int midsession)
+{
+    struct controlset *s;
+    union control *c;
+
+    /*
+     * Add the serial back end to the protocols list at the top of
+     * the config box.
+     */
+    s = ctrl_getset(b, "Session", "hostport",
+                   "Specify your connection by host name or IP address");
+    {
+       int i;
+       extern void config_protocolbuttons_handler(union control *, void *,
+                                                  void *, int);
+        for (i = 0; i < s->ncontrols; i++) {
+            c = s->ctrls[i];
+           if (c->generic.type == CTRL_RADIO &&
+               c->generic.handler == config_protocolbuttons_handler) {
+               c->radio.nbuttons++;
+               c->radio.ncolumns++;
+               c->radio.buttons =
+                   sresize(c->radio.buttons, c->radio.nbuttons, char *);
+               c->radio.buttons[c->radio.nbuttons-1] =
+                   dupstr("Serial");
+               c->radio.buttondata =
+                   sresize(c->radio.buttondata, c->radio.nbuttons, intorptr);
+               c->radio.buttondata[c->radio.nbuttons-1] = I(PROT_SERIAL);
+               if (c->radio.shortcuts) {
+                   c->radio.shortcuts =
+                       sresize(c->radio.shortcuts, c->radio.nbuttons, char);
+                   c->radio.shortcuts[c->radio.nbuttons-1] = NO_SHORTCUT;
+               }
+           }
+       }
+    }
+
+    /*
+     * Entirely new Connection/Serial panel for serial port
+     * configuration.
+     */
+    ctrl_settitle(b, "Connection/Serial",
+                 "Options controlling local serial lines");
+
+    if (!midsession) {
+       /*
+        * We don't permit switching to a different serial port in
+        * midflight, although we do allow all other
+        * reconfiguration.
+        */
+       s = ctrl_getset(b, "Connection/Serial", "serline",
+                       "Select a serial line");
+       ctrl_editbox(s, "Serial line to connect to", 'l', 40,
+                    HELPCTX(serial_line),
+                    dlg_stdeditbox_handler, I(offsetof(Config,serline)),
+                    I(sizeof(((Config *)0)->serline)));
+    }
+
+    s = ctrl_getset(b, "Connection/Serial", "sercfg", "Configure the serial line");
+    ctrl_editbox(s, "Speed (baud)", 's', 40,
+                HELPCTX(serial_speed),
+                dlg_stdeditbox_handler, I(offsetof(Config,serspeed)), I(-1));
+    ctrl_editbox(s, "Data bits", 'b', 40,
+                HELPCTX(serial_databits),
+                dlg_stdeditbox_handler,I(offsetof(Config,serdatabits)),I(-1));
+    /*
+     * Stop bits come in units of one half.
+     */
+    ctrl_editbox(s, "Stop bits", 't', 40,
+                HELPCTX(serial_stopbits),
+                dlg_stdeditbox_handler,I(offsetof(Config,serstopbits)),I(-2));
+    ctrl_droplist(s, "Parity", 'p', 40,
+                 HELPCTX(serial_parity),
+                 serial_parity_handler, I(0));
+    ctrl_droplist(s, "Flow control", 'f', 40,
+                 HELPCTX(serial_flow),
+                 serial_flow_handler, I(0));
+}
index 5fecfc2..c8221ed 100644 (file)
-/*\r
- * winproxy.c: Windows implementation of platform_new_connection(),\r
- * supporting an OpenSSH-like proxy command via the winhandl.c\r
- * mechanism.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <assert.h>\r
-\r
-#define DEFINE_PLUG_METHOD_MACROS\r
-#include "tree234.h"\r
-#include "putty.h"\r
-#include "network.h"\r
-#include "proxy.h"\r
-\r
-typedef struct Socket_localproxy_tag *Local_Proxy_Socket;\r
-\r
-struct Socket_localproxy_tag {\r
-    const struct socket_function_table *fn;\r
-    /* the above variable absolutely *must* be the first in this structure */\r
-\r
-    HANDLE to_cmd_H, from_cmd_H;\r
-    struct handle *to_cmd_h, *from_cmd_h;\r
-\r
-    char *error;\r
-\r
-    Plug plug;\r
-\r
-    void *privptr;\r
-};\r
-\r
-int localproxy_gotdata(struct handle *h, void *data, int len)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);\r
-\r
-    if (len < 0) {\r
-       return plug_closing(ps->plug, "Read error from local proxy command",\r
-                           0, 0);\r
-    } else if (len == 0) {\r
-       return plug_closing(ps->plug, NULL, 0, 0);\r
-    } else {\r
-       return plug_receive(ps->plug, 1, data, len);\r
-    }\r
-}\r
-\r
-void localproxy_sentdata(struct handle *h, int new_backlog)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);\r
-    \r
-    plug_sent(ps->plug, new_backlog);\r
-}\r
-\r
-static Plug sk_localproxy_plug (Socket s, Plug p)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;\r
-    Plug ret = ps->plug;\r
-    if (p)\r
-       ps->plug = p;\r
-    return ret;\r
-}\r
-\r
-static void sk_localproxy_close (Socket s)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;\r
-\r
-    handle_free(ps->to_cmd_h);\r
-    handle_free(ps->from_cmd_h);\r
-    CloseHandle(ps->to_cmd_H);\r
-    CloseHandle(ps->from_cmd_H);\r
-\r
-    sfree(ps);\r
-}\r
-\r
-static int sk_localproxy_write (Socket s, const char *data, int len)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;\r
-\r
-    return handle_write(ps->to_cmd_h, data, len);\r
-}\r
-\r
-static int sk_localproxy_write_oob(Socket s, const char *data, int len)\r
-{\r
-    /*\r
-     * oob data is treated as inband; nasty, but nothing really\r
-     * better we can do\r
-     */\r
-    return sk_localproxy_write(s, data, len);\r
-}\r
-\r
-static void sk_localproxy_flush(Socket s)\r
-{\r
-    /* Local_Proxy_Socket ps = (Local_Proxy_Socket) s; */\r
-    /* do nothing */\r
-}\r
-\r
-static void sk_localproxy_set_private_ptr(Socket s, void *ptr)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;\r
-    ps->privptr = ptr;\r
-}\r
-\r
-static void *sk_localproxy_get_private_ptr(Socket s)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;\r
-    return ps->privptr;\r
-}\r
-\r
-static void sk_localproxy_set_frozen(Socket s, int is_frozen)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;\r
-\r
-    /*\r
-     * FIXME\r
-     */\r
-}\r
-\r
-static const char *sk_localproxy_socket_error(Socket s)\r
-{\r
-    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;\r
-    return ps->error;\r
-}\r
-\r
-Socket platform_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
-    char *cmd;\r
-\r
-    static const struct socket_function_table socket_fn_table = {\r
-       sk_localproxy_plug,\r
-       sk_localproxy_close,\r
-       sk_localproxy_write,\r
-       sk_localproxy_write_oob,\r
-       sk_localproxy_flush,\r
-       sk_localproxy_set_private_ptr,\r
-       sk_localproxy_get_private_ptr,\r
-       sk_localproxy_set_frozen,\r
-       sk_localproxy_socket_error\r
-    };\r
-\r
-    Local_Proxy_Socket ret;\r
-    HANDLE us_to_cmd, us_from_cmd, cmd_to_us, cmd_from_us;\r
-    SECURITY_ATTRIBUTES sa;\r
-    STARTUPINFO si;\r
-    PROCESS_INFORMATION pi;\r
-\r
-    if (cfg->proxy_type != PROXY_CMD)\r
-       return NULL;\r
-\r
-    cmd = format_telnet_command(addr, port, cfg);\r
-\r
-    {\r
-       char *msg = dupprintf("Starting local proxy command: %s", cmd);\r
-       /* We're allowed to pass NULL here, because we're part of the Windows\r
-        * front end so we know logevent doesn't expect any data. */\r
-       logevent(NULL, msg);\r
-       sfree(msg);\r
-    }\r
-\r
-    ret = snew(struct Socket_localproxy_tag);\r
-    ret->fn = &socket_fn_table;\r
-    ret->plug = plug;\r
-    ret->error = NULL;\r
-\r
-    /*\r
-     * Create the pipes to the proxy command, and spawn the proxy\r
-     * command process.\r
-     */\r
-    sa.nLength = sizeof(sa);\r
-    sa.lpSecurityDescriptor = NULL;    /* default */\r
-    sa.bInheritHandle = TRUE;\r
-    if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {\r
-       ret->error = dupprintf("Unable to create pipes for proxy command");\r
-       return (Socket)ret;\r
-    }\r
-\r
-    if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {\r
-       CloseHandle(us_from_cmd);\r
-       CloseHandle(cmd_to_us);\r
-       ret->error = dupprintf("Unable to create pipes for proxy command");\r
-       return (Socket)ret;\r
-    }\r
-\r
-    SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);\r
-    SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);\r
-\r
-    si.cb = sizeof(si);\r
-    si.lpReserved = NULL;\r
-    si.lpDesktop = NULL;\r
-    si.lpTitle = NULL;\r
-    si.dwFlags = STARTF_USESTDHANDLES;\r
-    si.cbReserved2 = 0;\r
-    si.lpReserved2 = NULL;\r
-    si.hStdInput = cmd_from_us;\r
-    si.hStdOutput = cmd_to_us;\r
-    si.hStdError = NULL;\r
-    CreateProcess(NULL, cmd, NULL, NULL, TRUE,\r
-                 CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,\r
-                 NULL, NULL, &si, &pi);\r
-\r
-    CloseHandle(cmd_from_us);\r
-    CloseHandle(cmd_to_us);\r
-\r
-    ret->to_cmd_H = us_to_cmd;\r
-    ret->from_cmd_H = us_from_cmd;\r
-\r
-    ret->from_cmd_h = handle_input_new(ret->from_cmd_H, localproxy_gotdata,\r
-                                      ret, 0);\r
-    ret->to_cmd_h = handle_output_new(ret->to_cmd_H, localproxy_sentdata,\r
-                                     ret, 0);\r
-\r
-    /* We are responsible for this and don't need it any more */\r
-    sk_addr_free(addr);\r
-\r
-    return (Socket) ret;\r
-}\r
+/*
+ * winproxy.c: Windows implementation of platform_new_connection(),
+ * supporting an OpenSSH-like proxy command via the winhandl.c
+ * mechanism.
+ */
+
+#include <stdio.h>
+#include <assert.h>
+
+#define DEFINE_PLUG_METHOD_MACROS
+#include "tree234.h"
+#include "putty.h"
+#include "network.h"
+#include "proxy.h"
+
+typedef struct Socket_localproxy_tag *Local_Proxy_Socket;
+
+struct Socket_localproxy_tag {
+    const struct socket_function_table *fn;
+    /* the above variable absolutely *must* be the first in this structure */
+
+    HANDLE to_cmd_H, from_cmd_H;
+    struct handle *to_cmd_h, *from_cmd_h;
+
+    char *error;
+
+    Plug plug;
+
+    void *privptr;
+};
+
+int localproxy_gotdata(struct handle *h, void *data, int len)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);
+
+    if (len < 0) {
+       return plug_closing(ps->plug, "Read error from local proxy command",
+                           0, 0);
+    } else if (len == 0) {
+       return plug_closing(ps->plug, NULL, 0, 0);
+    } else {
+       return plug_receive(ps->plug, 1, data, len);
+    }
+}
+
+void localproxy_sentdata(struct handle *h, int new_backlog)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);
+    
+    plug_sent(ps->plug, new_backlog);
+}
+
+static Plug sk_localproxy_plug (Socket s, Plug p)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
+    Plug ret = ps->plug;
+    if (p)
+       ps->plug = p;
+    return ret;
+}
+
+static void sk_localproxy_close (Socket s)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
+
+    handle_free(ps->to_cmd_h);
+    handle_free(ps->from_cmd_h);
+    CloseHandle(ps->to_cmd_H);
+    CloseHandle(ps->from_cmd_H);
+
+    sfree(ps);
+}
+
+static int sk_localproxy_write (Socket s, const char *data, int len)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
+
+    return handle_write(ps->to_cmd_h, data, len);
+}
+
+static int sk_localproxy_write_oob(Socket s, const char *data, int len)
+{
+    /*
+     * oob data is treated as inband; nasty, but nothing really
+     * better we can do
+     */
+    return sk_localproxy_write(s, data, len);
+}
+
+static void sk_localproxy_flush(Socket s)
+{
+    /* Local_Proxy_Socket ps = (Local_Proxy_Socket) s; */
+    /* do nothing */
+}
+
+static void sk_localproxy_set_private_ptr(Socket s, void *ptr)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
+    ps->privptr = ptr;
+}
+
+static void *sk_localproxy_get_private_ptr(Socket s)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
+    return ps->privptr;
+}
+
+static void sk_localproxy_set_frozen(Socket s, int is_frozen)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
+
+    /*
+     * FIXME
+     */
+}
+
+static const char *sk_localproxy_socket_error(Socket s)
+{
+    Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
+    return ps->error;
+}
+
+Socket platform_new_connection(SockAddr addr, char *hostname,
+                              int port, int privport,
+                              int oobinline, int nodelay, int keepalive,
+                              Plug plug, const Config *cfg)
+{
+    char *cmd;
+
+    static const struct socket_function_table socket_fn_table = {
+       sk_localproxy_plug,
+       sk_localproxy_close,
+       sk_localproxy_write,
+       sk_localproxy_write_oob,
+       sk_localproxy_flush,
+       sk_localproxy_set_private_ptr,
+       sk_localproxy_get_private_ptr,
+       sk_localproxy_set_frozen,
+       sk_localproxy_socket_error
+    };
+
+    Local_Proxy_Socket ret;
+    HANDLE us_to_cmd, us_from_cmd, cmd_to_us, cmd_from_us;
+    SECURITY_ATTRIBUTES sa;
+    STARTUPINFO si;
+    PROCESS_INFORMATION pi;
+
+    if (cfg->proxy_type != PROXY_CMD)
+       return NULL;
+
+    cmd = format_telnet_command(addr, port, cfg);
+
+    {
+       char *msg = dupprintf("Starting local proxy command: %s", cmd);
+       /* We're allowed to pass NULL here, because we're part of the Windows
+        * front end so we know logevent doesn't expect any data. */
+       logevent(NULL, msg);
+       sfree(msg);
+    }
+
+    ret = snew(struct Socket_localproxy_tag);
+    ret->fn = &socket_fn_table;
+    ret->plug = plug;
+    ret->error = NULL;
+
+    /*
+     * Create the pipes to the proxy command, and spawn the proxy
+     * command process.
+     */
+    sa.nLength = sizeof(sa);
+    sa.lpSecurityDescriptor = NULL;    /* default */
+    sa.bInheritHandle = TRUE;
+    if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
+       ret->error = dupprintf("Unable to create pipes for proxy command");
+       return (Socket)ret;
+    }
+
+    if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
+       CloseHandle(us_from_cmd);
+       CloseHandle(cmd_to_us);
+       ret->error = dupprintf("Unable to create pipes for proxy command");
+       return (Socket)ret;
+    }
+
+    SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);
+    SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);
+
+    si.cb = sizeof(si);
+    si.lpReserved = NULL;
+    si.lpDesktop = NULL;
+    si.lpTitle = NULL;
+    si.dwFlags = STARTF_USESTDHANDLES;
+    si.cbReserved2 = 0;
+    si.lpReserved2 = NULL;
+    si.hStdInput = cmd_from_us;
+    si.hStdOutput = cmd_to_us;
+    si.hStdError = NULL;
+    CreateProcess(NULL, cmd, NULL, NULL, TRUE,
+                 CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
+                 NULL, NULL, &si, &pi);
+
+    CloseHandle(cmd_from_us);
+    CloseHandle(cmd_to_us);
+
+    ret->to_cmd_H = us_to_cmd;
+    ret->from_cmd_H = us_from_cmd;
+
+    ret->from_cmd_h = handle_input_new(ret->from_cmd_H, localproxy_gotdata,
+                                      ret, 0);
+    ret->to_cmd_h = handle_output_new(ret->to_cmd_H, localproxy_sentdata,
+                                     ret, 0);
+
+    /* We are responsible for this and don't need it any more */
+    sk_addr_free(addr);
+
+    return (Socket) ret;
+}
index f6def7a..35a4ed4 100644 (file)
-/*\r
- * Serial back end (Windows-specific).\r
- */\r
-\r
-/*\r
- * TODO:\r
- * \r
- *  - sending breaks?\r
- *     + looks as if you do this by calling SetCommBreak(handle),\r
- *      then waiting a bit, then doing ClearCommBreak(handle). A\r
- *      small job for timing.c, methinks.\r
- *\r
- *  - why are we dropping data when talking to judicator?\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <limits.h>\r
-\r
-#include "putty.h"\r
-\r
-#define SERIAL_MAX_BACKLOG 4096\r
-\r
-typedef struct serial_backend_data {\r
-    HANDLE port;\r
-    struct handle *out, *in;\r
-    void *frontend;\r
-    int bufsize;\r
-} *Serial;\r
-\r
-static void serial_terminate(Serial serial)\r
-{\r
-    if (serial->out) {\r
-       handle_free(serial->out);\r
-       serial->out = NULL;\r
-    }\r
-    if (serial->in) {\r
-       handle_free(serial->in);\r
-       serial->in = NULL;\r
-    }\r
-    if (serial->port) {\r
-       CloseHandle(serial->port);\r
-       serial->port = NULL;\r
-    }\r
-}\r
-\r
-static int serial_gotdata(struct handle *h, void *data, int len)\r
-{\r
-    Serial serial = (Serial)handle_get_privdata(h);\r
-    if (len <= 0) {\r
-       const char *error_msg;\r
-\r
-       /*\r
-        * Currently, len==0 should never happen because we're\r
-        * ignoring EOFs. However, it seems not totally impossible\r
-        * that this same back end might be usable to talk to named\r
-        * pipes or some other non-serial device, in which case EOF\r
-        * may become meaningful here.\r
-        */\r
-       if (len == 0)\r
-           error_msg = "End of file reading from serial device";\r
-       else\r
-           error_msg = "Error reading from serial device";\r
-\r
-       serial_terminate(serial);\r
-\r
-       notify_remote_exit(serial->frontend);\r
-\r
-       logevent(serial->frontend, error_msg);\r
-\r
-       connection_fatal(serial->frontend, "%s", error_msg);\r
-\r
-       return 0;                      /* placate optimiser */\r
-    } else {\r
-       return from_backend(serial->frontend, 0, data, len);\r
-    }\r
-}\r
-\r
-static void serial_sentdata(struct handle *h, int new_backlog)\r
-{\r
-    Serial serial = (Serial)handle_get_privdata(h);\r
-    if (new_backlog < 0) {\r
-       const char *error_msg = "Error writing to serial device";\r
-\r
-       serial_terminate(serial);\r
-\r
-       notify_remote_exit(serial->frontend);\r
-\r
-       logevent(serial->frontend, error_msg);\r
-\r
-       connection_fatal(serial->frontend, "%s", error_msg);\r
-    } else {\r
-       serial->bufsize = new_backlog;\r
-    }\r
-}\r
-\r
-static const char *serial_configure(Serial serial, HANDLE serport, Config *cfg)\r
-{\r
-    DCB dcb;\r
-    COMMTIMEOUTS timeouts;\r
-\r
-    /*\r
-     * Set up the serial port parameters. If we can't even\r
-     * GetCommState, we ignore the problem on the grounds that the\r
-     * user might have pointed us at some other type of two-way\r
-     * device instead of a serial port.\r
-     */\r
-    if (GetCommState(serport, &dcb)) {\r
-       char *msg;\r
-       const char *str;\r
-\r
-       /*\r
-        * Boilerplate.\r
-        */\r
-       dcb.fBinary = TRUE;\r
-       dcb.fDtrControl = DTR_CONTROL_ENABLE;\r
-       dcb.fDsrSensitivity = FALSE;\r
-       dcb.fTXContinueOnXoff = FALSE;\r
-       dcb.fOutX = FALSE;\r
-       dcb.fInX = FALSE;\r
-       dcb.fErrorChar = FALSE;\r
-       dcb.fNull = FALSE;\r
-       dcb.fRtsControl = RTS_CONTROL_ENABLE;\r
-       dcb.fAbortOnError = FALSE;\r
-       dcb.fOutxCtsFlow = FALSE;\r
-       dcb.fOutxDsrFlow = FALSE;\r
-\r
-       /*\r
-        * Configurable parameters.\r
-        */\r
-       dcb.BaudRate = cfg->serspeed;\r
-       msg = dupprintf("Configuring baud rate %d", cfg->serspeed);\r
-       logevent(serial->frontend, msg);\r
-       sfree(msg);\r
-\r
-       dcb.ByteSize = cfg->serdatabits;\r
-       msg = dupprintf("Configuring %d data bits", cfg->serdatabits);\r
-       logevent(serial->frontend, msg);\r
-       sfree(msg);\r
-\r
-       switch (cfg->serstopbits) {\r
-         case 2: dcb.StopBits = ONESTOPBIT; str = "1"; break;\r
-         case 3: dcb.StopBits = ONE5STOPBITS; str = "1.5"; break;\r
-         case 4: dcb.StopBits = TWOSTOPBITS; str = "2"; break;\r
-         default: return "Invalid number of stop bits (need 1, 1.5 or 2)";\r
-       }\r
-       msg = dupprintf("Configuring %s data bits", str);\r
-       logevent(serial->frontend, msg);\r
-       sfree(msg);\r
-\r
-       switch (cfg->serparity) {\r
-         case SER_PAR_NONE: dcb.Parity = NOPARITY; str = "no"; break;\r
-         case SER_PAR_ODD: dcb.Parity = ODDPARITY; str = "odd"; break;\r
-         case SER_PAR_EVEN: dcb.Parity = EVENPARITY; str = "even"; break;\r
-         case SER_PAR_MARK: dcb.Parity = MARKPARITY; str = "mark"; break;\r
-         case SER_PAR_SPACE: dcb.Parity = SPACEPARITY; str = "space"; break;\r
-       }\r
-       msg = dupprintf("Configuring %s parity", str);\r
-       logevent(serial->frontend, msg);\r
-       sfree(msg);\r
-\r
-       switch (cfg->serflow) {\r
-         case SER_FLOW_NONE:\r
-           str = "no";\r
-           break;\r
-         case SER_FLOW_XONXOFF:\r
-           dcb.fOutX = dcb.fInX = TRUE;\r
-           str = "XON/XOFF";\r
-           break;\r
-         case SER_FLOW_RTSCTS:\r
-           dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;\r
-           dcb.fOutxCtsFlow = TRUE;\r
-           str = "RTS/CTS";\r
-           break;\r
-         case SER_FLOW_DSRDTR:\r
-           dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;\r
-           dcb.fOutxDsrFlow = TRUE;\r
-           str = "DSR/DTR";\r
-           break;\r
-       }\r
-       msg = dupprintf("Configuring %s flow control", str);\r
-       logevent(serial->frontend, msg);\r
-       sfree(msg);\r
-\r
-       if (!SetCommState(serport, &dcb))\r
-           return "Unable to configure serial port";\r
-\r
-       timeouts.ReadIntervalTimeout = 1;\r
-       timeouts.ReadTotalTimeoutMultiplier = 0;\r
-       timeouts.ReadTotalTimeoutConstant = 0;\r
-       timeouts.WriteTotalTimeoutMultiplier = 0;\r
-       timeouts.WriteTotalTimeoutConstant = 0;\r
-       if (!SetCommTimeouts(serport, &timeouts))\r
-           return "Unable to configure serial timeouts";\r
-    }\r
-\r
-    return NULL;\r
-}\r
-\r
-/*\r
- * Called to set up the serial connection.\r
- * \r
- * Returns an error message, or NULL on success.\r
- *\r
- * Also places the canonical host name into `realhost'. It must be\r
- * freed by the caller.\r
- */\r
-static const char *serial_init(void *frontend_handle, void **backend_handle,\r
-                              Config *cfg,\r
-                              char *host, int port, char **realhost, int nodelay,\r
-                              int keepalive)\r
-{\r
-    Serial serial;\r
-    HANDLE serport;\r
-    const char *err;\r
-\r
-    serial = snew(struct serial_backend_data);\r
-    serial->port = NULL;\r
-    serial->out = serial->in = NULL;\r
-    *backend_handle = serial;\r
-\r
-    serial->frontend = frontend_handle;\r
-\r
-    {\r
-       char *msg = dupprintf("Opening serial device %s", host);\r
-       logevent(serial->frontend, msg);\r
-    }\r
-\r
-    serport = CreateFile(cfg->serline, GENERIC_READ | GENERIC_WRITE, 0, NULL,\r
-                        OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);\r
-    if (serport == INVALID_HANDLE_VALUE)\r
-       return "Unable to open serial port";\r
-\r
-    err = serial_configure(serial, serport, cfg);\r
-    if (err)\r
-       return err;\r
-\r
-    serial->port = serport;\r
-    serial->out = handle_output_new(serport, serial_sentdata, serial,\r
-                                   HANDLE_FLAG_OVERLAPPED);\r
-    serial->in = handle_input_new(serport, serial_gotdata, serial,\r
-                                 HANDLE_FLAG_OVERLAPPED |\r
-                                 HANDLE_FLAG_IGNOREEOF);\r
-\r
-    *realhost = dupstr(cfg->serline);\r
-\r
-    return NULL;\r
-}\r
-\r
-static void serial_free(void *handle)\r
-{\r
-    Serial serial = (Serial) handle;\r
-\r
-    serial_terminate(serial);\r
-    sfree(serial);\r
-}\r
-\r
-static void serial_reconfig(void *handle, Config *cfg)\r
-{\r
-    Serial serial = (Serial) handle;\r
-    const char *err;\r
-\r
-    err = serial_configure(serial, serial->port, cfg);\r
-\r
-    /*\r
-     * FIXME: what should we do if err returns something?\r
-     */\r
-}\r
-\r
-/*\r
- * Called to send data down the serial connection.\r
- */\r
-static int serial_send(void *handle, char *buf, int len)\r
-{\r
-    Serial serial = (Serial) handle;\r
-\r
-    if (serial->out == NULL)\r
-       return 0;\r
-\r
-    serial->bufsize = handle_write(serial->out, buf, len);\r
-    return serial->bufsize;\r
-}\r
-\r
-/*\r
- * Called to query the current sendability status.\r
- */\r
-static int serial_sendbuffer(void *handle)\r
-{\r
-    Serial serial = (Serial) handle;\r
-    return serial->bufsize;\r
-}\r
-\r
-/*\r
- * Called to set the size of the window\r
- */\r
-static void serial_size(void *handle, int width, int height)\r
-{\r
-    /* Do nothing! */\r
-    return;\r
-}\r
-\r
-/*\r
- * Send serial special codes.\r
- */\r
-static void serial_special(void *handle, Telnet_Special code)\r
-{\r
-    /*\r
-     * FIXME: serial break? XON? XOFF?\r
-     */\r
-    return;\r
-}\r
-\r
-/*\r
- * Return a list of the special codes that make sense in this\r
- * protocol.\r
- */\r
-static const struct telnet_special *serial_get_specials(void *handle)\r
-{\r
-    /*\r
-     * FIXME: serial break? XON? XOFF?\r
-     */\r
-    return NULL;\r
-}\r
-\r
-static int serial_connected(void *handle)\r
-{\r
-    return 1;                         /* always connected */\r
-}\r
-\r
-static int serial_sendok(void *handle)\r
-{\r
-    return 1;\r
-}\r
-\r
-static void serial_unthrottle(void *handle, int backlog)\r
-{\r
-    Serial serial = (Serial) handle;\r
-    if (serial->in)\r
-       handle_unthrottle(serial->in, backlog);\r
-}\r
-\r
-static int serial_ldisc(void *handle, int option)\r
-{\r
-    /*\r
-     * Local editing and local echo are off by default.\r
-     */\r
-    return 0;\r
-}\r
-\r
-static void serial_provide_ldisc(void *handle, void *ldisc)\r
-{\r
-    /* This is a stub. */\r
-}\r
-\r
-static void serial_provide_logctx(void *handle, void *logctx)\r
-{\r
-    /* This is a stub. */\r
-}\r
-\r
-static int serial_exitcode(void *handle)\r
-{\r
-    Serial serial = (Serial) handle;\r
-    if (serial->port != NULL)\r
-        return -1;                     /* still connected */\r
-    else\r
-        /* Exit codes are a meaningless concept with serial ports */\r
-        return INT_MAX;\r
-}\r
-\r
-/*\r
- * cfg_info for Serial does nothing at all.\r
- */\r
-static int serial_cfg_info(void *handle)\r
-{\r
-    return 0;\r
-}\r
-\r
-Backend serial_backend = {\r
-    serial_init,\r
-    serial_free,\r
-    serial_reconfig,\r
-    serial_send,\r
-    serial_sendbuffer,\r
-    serial_size,\r
-    serial_special,\r
-    serial_get_specials,\r
-    serial_connected,\r
-    serial_exitcode,\r
-    serial_sendok,\r
-    serial_ldisc,\r
-    serial_provide_ldisc,\r
-    serial_provide_logctx,\r
-    serial_unthrottle,\r
-    serial_cfg_info,\r
-    1\r
-};\r
+/*
+ * Serial back end (Windows-specific).
+ */
+
+/*
+ * TODO:
+ * 
+ *  - sending breaks?
+ *     + looks as if you do this by calling SetCommBreak(handle),
+ *      then waiting a bit, then doing ClearCommBreak(handle). A
+ *      small job for timing.c, methinks.
+ *
+ *  - why are we dropping data when talking to judicator?
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#include "putty.h"
+
+#define SERIAL_MAX_BACKLOG 4096
+
+typedef struct serial_backend_data {
+    HANDLE port;
+    struct handle *out, *in;
+    void *frontend;
+    int bufsize;
+} *Serial;
+
+static void serial_terminate(Serial serial)
+{
+    if (serial->out) {
+       handle_free(serial->out);
+       serial->out = NULL;
+    }
+    if (serial->in) {
+       handle_free(serial->in);
+       serial->in = NULL;
+    }
+    if (serial->port) {
+       CloseHandle(serial->port);
+       serial->port = NULL;
+    }
+}
+
+static int serial_gotdata(struct handle *h, void *data, int len)
+{
+    Serial serial = (Serial)handle_get_privdata(h);
+    if (len <= 0) {
+       const char *error_msg;
+
+       /*
+        * Currently, len==0 should never happen because we're
+        * ignoring EOFs. However, it seems not totally impossible
+        * that this same back end might be usable to talk to named
+        * pipes or some other non-serial device, in which case EOF
+        * may become meaningful here.
+        */
+       if (len == 0)
+           error_msg = "End of file reading from serial device";
+       else
+           error_msg = "Error reading from serial device";
+
+       serial_terminate(serial);
+
+       notify_remote_exit(serial->frontend);
+
+       logevent(serial->frontend, error_msg);
+
+       connection_fatal(serial->frontend, "%s", error_msg);
+
+       return 0;                      /* placate optimiser */
+    } else {
+       return from_backend(serial->frontend, 0, data, len);
+    }
+}
+
+static void serial_sentdata(struct handle *h, int new_backlog)
+{
+    Serial serial = (Serial)handle_get_privdata(h);
+    if (new_backlog < 0) {
+       const char *error_msg = "Error writing to serial device";
+
+       serial_terminate(serial);
+
+       notify_remote_exit(serial->frontend);
+
+       logevent(serial->frontend, error_msg);
+
+       connection_fatal(serial->frontend, "%s", error_msg);
+    } else {
+       serial->bufsize = new_backlog;
+    }
+}
+
+static const char *serial_configure(Serial serial, HANDLE serport, Config *cfg)
+{
+    DCB dcb;
+    COMMTIMEOUTS timeouts;
+
+    /*
+     * Set up the serial port parameters. If we can't even
+     * GetCommState, we ignore the problem on the grounds that the
+     * user might have pointed us at some other type of two-way
+     * device instead of a serial port.
+     */
+    if (GetCommState(serport, &dcb)) {
+       char *msg;
+       const char *str;
+
+       /*
+        * Boilerplate.
+        */
+       dcb.fBinary = TRUE;
+       dcb.fDtrControl = DTR_CONTROL_ENABLE;
+       dcb.fDsrSensitivity = FALSE;
+       dcb.fTXContinueOnXoff = FALSE;
+       dcb.fOutX = FALSE;
+       dcb.fInX = FALSE;
+       dcb.fErrorChar = FALSE;
+       dcb.fNull = FALSE;
+       dcb.fRtsControl = RTS_CONTROL_ENABLE;
+       dcb.fAbortOnError = FALSE;
+       dcb.fOutxCtsFlow = FALSE;
+       dcb.fOutxDsrFlow = FALSE;
+
+       /*
+        * Configurable parameters.
+        */
+       dcb.BaudRate = cfg->serspeed;
+       msg = dupprintf("Configuring baud rate %d", cfg->serspeed);
+       logevent(serial->frontend, msg);
+       sfree(msg);
+
+       dcb.ByteSize = cfg->serdatabits;
+       msg = dupprintf("Configuring %d data bits", cfg->serdatabits);
+       logevent(serial->frontend, msg);
+       sfree(msg);
+
+       switch (cfg->serstopbits) {
+         case 2: dcb.StopBits = ONESTOPBIT; str = "1"; break;
+         case 3: dcb.StopBits = ONE5STOPBITS; str = "1.5"; break;
+         case 4: dcb.StopBits = TWOSTOPBITS; str = "2"; break;
+         default: return "Invalid number of stop bits (need 1, 1.5 or 2)";
+       }
+       msg = dupprintf("Configuring %s data bits", str);
+       logevent(serial->frontend, msg);
+       sfree(msg);
+
+       switch (cfg->serparity) {
+         case SER_PAR_NONE: dcb.Parity = NOPARITY; str = "no"; break;
+         case SER_PAR_ODD: dcb.Parity = ODDPARITY; str = "odd"; break;
+         case SER_PAR_EVEN: dcb.Parity = EVENPARITY; str = "even"; break;
+         case SER_PAR_MARK: dcb.Parity = MARKPARITY; str = "mark"; break;
+         case SER_PAR_SPACE: dcb.Parity = SPACEPARITY; str = "space"; break;
+       }
+       msg = dupprintf("Configuring %s parity", str);
+       logevent(serial->frontend, msg);
+       sfree(msg);
+
+       switch (cfg->serflow) {
+         case SER_FLOW_NONE:
+           str = "no";
+           break;
+         case SER_FLOW_XONXOFF:
+           dcb.fOutX = dcb.fInX = TRUE;
+           str = "XON/XOFF";
+           break;
+         case SER_FLOW_RTSCTS:
+           dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
+           dcb.fOutxCtsFlow = TRUE;
+           str = "RTS/CTS";
+           break;
+         case SER_FLOW_DSRDTR:
+           dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
+           dcb.fOutxDsrFlow = TRUE;
+           str = "DSR/DTR";
+           break;
+       }
+       msg = dupprintf("Configuring %s flow control", str);
+       logevent(serial->frontend, msg);
+       sfree(msg);
+
+       if (!SetCommState(serport, &dcb))
+           return "Unable to configure serial port";
+
+       timeouts.ReadIntervalTimeout = 1;
+       timeouts.ReadTotalTimeoutMultiplier = 0;
+       timeouts.ReadTotalTimeoutConstant = 0;
+       timeouts.WriteTotalTimeoutMultiplier = 0;
+       timeouts.WriteTotalTimeoutConstant = 0;
+       if (!SetCommTimeouts(serport, &timeouts))
+           return "Unable to configure serial timeouts";
+    }
+
+    return NULL;
+}
+
+/*
+ * Called to set up the serial connection.
+ * 
+ * Returns an error message, or NULL on success.
+ *
+ * Also places the canonical host name into `realhost'. It must be
+ * freed by the caller.
+ */
+static const char *serial_init(void *frontend_handle, void **backend_handle,
+                              Config *cfg,
+                              char *host, int port, char **realhost, int nodelay,
+                              int keepalive)
+{
+    Serial serial;
+    HANDLE serport;
+    const char *err;
+
+    serial = snew(struct serial_backend_data);
+    serial->port = NULL;
+    serial->out = serial->in = NULL;
+    *backend_handle = serial;
+
+    serial->frontend = frontend_handle;
+
+    {
+       char *msg = dupprintf("Opening serial device %s", host);
+       logevent(serial->frontend, msg);
+    }
+
+    serport = CreateFile(cfg->serline, GENERIC_READ | GENERIC_WRITE, 0, NULL,
+                        OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
+    if (serport == INVALID_HANDLE_VALUE)
+       return "Unable to open serial port";
+
+    err = serial_configure(serial, serport, cfg);
+    if (err)
+       return err;
+
+    serial->port = serport;
+    serial->out = handle_output_new(serport, serial_sentdata, serial,
+                                   HANDLE_FLAG_OVERLAPPED);
+    serial->in = handle_input_new(serport, serial_gotdata, serial,
+                                 HANDLE_FLAG_OVERLAPPED |
+                                 HANDLE_FLAG_IGNOREEOF);
+
+    *realhost = dupstr(cfg->serline);
+
+    return NULL;
+}
+
+static void serial_free(void *handle)
+{
+    Serial serial = (Serial) handle;
+
+    serial_terminate(serial);
+    sfree(serial);
+}
+
+static void serial_reconfig(void *handle, Config *cfg)
+{
+    Serial serial = (Serial) handle;
+    const char *err;
+
+    err = serial_configure(serial, serial->port, cfg);
+
+    /*
+     * FIXME: what should we do if err returns something?
+     */
+}
+
+/*
+ * Called to send data down the serial connection.
+ */
+static int serial_send(void *handle, char *buf, int len)
+{
+    Serial serial = (Serial) handle;
+
+    if (serial->out == NULL)
+       return 0;
+
+    serial->bufsize = handle_write(serial->out, buf, len);
+    return serial->bufsize;
+}
+
+/*
+ * Called to query the current sendability status.
+ */
+static int serial_sendbuffer(void *handle)
+{
+    Serial serial = (Serial) handle;
+    return serial->bufsize;
+}
+
+/*
+ * Called to set the size of the window
+ */
+static void serial_size(void *handle, int width, int height)
+{
+    /* Do nothing! */
+    return;
+}
+
+/*
+ * Send serial special codes.
+ */
+static void serial_special(void *handle, Telnet_Special code)
+{
+    /*
+     * FIXME: serial break? XON? XOFF?
+     */
+    return;
+}
+
+/*
+ * Return a list of the special codes that make sense in this
+ * protocol.
+ */
+static const struct telnet_special *serial_get_specials(void *handle)
+{
+    /*
+     * FIXME: serial break? XON? XOFF?
+     */
+    return NULL;
+}
+
+static int serial_connected(void *handle)
+{
+    return 1;                         /* always connected */
+}
+
+static int serial_sendok(void *handle)
+{
+    return 1;
+}
+
+static void serial_unthrottle(void *handle, int backlog)
+{
+    Serial serial = (Serial) handle;
+    if (serial->in)
+       handle_unthrottle(serial->in, backlog);
+}
+
+static int serial_ldisc(void *handle, int option)
+{
+    /*
+     * Local editing and local echo are off by default.
+     */
+    return 0;
+}
+
+static void serial_provide_ldisc(void *handle, void *ldisc)
+{
+    /* This is a stub. */
+}
+
+static void serial_provide_logctx(void *handle, void *logctx)
+{
+    /* This is a stub. */
+}
+
+static int serial_exitcode(void *handle)
+{
+    Serial serial = (Serial) handle;
+    if (serial->port != NULL)
+        return -1;                     /* still connected */
+    else
+        /* Exit codes are a meaningless concept with serial ports */
+        return INT_MAX;
+}
+
+/*
+ * cfg_info for Serial does nothing at all.
+ */
+static int serial_cfg_info(void *handle)
+{
+    return 0;
+}
+
+Backend serial_backend = {
+    serial_init,
+    serial_free,
+    serial_reconfig,
+    serial_send,
+    serial_sendbuffer,
+    serial_size,
+    serial_special,
+    serial_get_specials,
+    serial_connected,
+    serial_exitcode,
+    serial_sendok,
+    serial_ldisc,
+    serial_provide_ldisc,
+    serial_provide_logctx,
+    serial_unthrottle,
+    serial_cfg_info,
+    1
+};