OSDN Git Service

Remove SFTP support to release 1.98f.
[ffftp/ffftp.git] / putty / WINDOWS / WINHANDL.C
diff --git a/putty/WINDOWS/WINHANDL.C b/putty/WINDOWS/WINHANDL.C
deleted file mode 100644 (file)
index dbcab2b..0000000
+++ /dev/null
@@ -1,596 +0,0 @@
-/*\r
- * winhandl.c: Module to give Windows front ends the general\r
- * ability to deal with consoles, pipes, serial ports, or any other\r
- * type of data stream accessed through a Windows API HANDLE rather\r
- * than a WinSock SOCKET.\r
- *\r
- * We do this by spawning a subthread to continuously try to read\r
- * from the handle. Every time a read successfully returns some\r
- * data, the subthread sets an event object which is picked up by\r
- * the main thread, and the main thread then sets an event in\r
- * return to instruct the subthread to resume reading.\r
- * \r
- * Output works precisely the other way round, in a second\r
- * subthread. The output subthread should not be attempting to\r
- * write all the time, because it hasn't always got data _to_\r
- * write; so the output thread waits for an event object notifying\r
- * it to _attempt_ a write, and then it sets an event in return\r
- * when one completes.\r
- * \r
- * (It's terribly annoying having to spawn a subthread for each\r
- * direction of each handle. Technically it isn't necessary for\r
- * serial ports, since we could use overlapped I/O within the main\r
- * thread and wait directly on the event objects in the OVERLAPPED\r
- * structures. However, we can't use this trick for some types of\r
- * file handle at all - for some reason Windows restricts use of\r
- * OVERLAPPED to files which were opened with the overlapped flag -\r
- * and so we must use threads for those. This being the case, it's\r
- * simplest just to use threads for everything rather than trying\r
- * to keep track of multiple completely separate mechanisms.)\r
- */\r
-\r
-#include <assert.h>\r
-\r
-#include "putty.h"\r
-\r
-/* ----------------------------------------------------------------------\r
- * Generic definitions.\r
- */\r
-\r
-/*\r
- * Maximum amount of backlog we will allow to build up on an input\r
- * handle before we stop reading from it.\r
- */\r
-#define MAX_BACKLOG 32768\r
-\r
-struct handle_generic {\r
-    /*\r
-     * Initial fields common to both handle_input and handle_output\r
-     * structures.\r
-     * \r
-     * The three HANDLEs are set up at initialisation time and are\r
-     * thereafter read-only to both main thread and subthread.\r
-     * `moribund' is only used by the main thread; `done' is\r
-     * written by the main thread before signalling to the\r
-     * subthread. `defunct' and `busy' are used only by the main\r
-     * thread.\r
-     */\r
-    HANDLE h;                         /* the handle itself */\r
-    HANDLE ev_to_main;                /* event used to signal main thread */\r
-    HANDLE ev_from_main;              /* event used to signal back to us */\r
-    int moribund;                     /* are we going to kill this soon? */\r
-    int done;                         /* request subthread to terminate */\r
-    int defunct;                      /* has the subthread already gone? */\r
-    int busy;                         /* operation currently in progress? */\r
-    void *privdata;                   /* for client to remember who they are */\r
-};\r
-\r
-/* ----------------------------------------------------------------------\r
- * Input threads.\r
- */\r
-\r
-/*\r
- * Data required by an input thread.\r
- */\r
-struct handle_input {\r
-    /*\r
-     * Copy of the handle_generic structure.\r
-     */\r
-    HANDLE h;                         /* the handle itself */\r
-    HANDLE ev_to_main;                /* event used to signal main thread */\r
-    HANDLE ev_from_main;              /* event used to signal back to us */\r
-    int moribund;                     /* are we going to kill this soon? */\r
-    int done;                         /* request subthread to terminate */\r
-    int defunct;                      /* has the subthread already gone? */\r
-    int busy;                         /* operation currently in progress? */\r
-    void *privdata;                   /* for client to remember who they are */\r
-\r
-    /*\r
-     * Data set at initialisation and then read-only.\r
-     */\r
-    int flags;\r
-\r
-    /*\r
-     * Data set by the input thread before signalling ev_to_main,\r
-     * and read by the main thread after receiving that signal.\r
-     */\r
-    char buffer[4096];                /* the data read from the handle */\r
-    DWORD len;                        /* how much data that was */\r
-    int readerr;                      /* lets us know about read errors */\r
-\r
-    /*\r
-     * Callback function called by this module when data arrives on\r
-     * an input handle.\r
-     */\r
-    handle_inputfn_t gotdata;\r
-};\r
-\r
-/*\r
- * The actual thread procedure for an input thread.\r
- */\r
-static DWORD WINAPI handle_input_threadfunc(void *param)\r
-{\r
-    struct handle_input *ctx = (struct handle_input *) param;\r
-    OVERLAPPED ovl, *povl;\r
-    HANDLE oev;\r
-    int readret, readlen;\r
-\r
-    if (ctx->flags & HANDLE_FLAG_OVERLAPPED) {\r
-       povl = &ovl;\r
-       oev = CreateEvent(NULL, TRUE, FALSE, NULL);\r
-    } else {\r
-       povl = NULL;\r
-    }\r
-\r
-    if (ctx->flags & HANDLE_FLAG_UNITBUFFER)\r
-       readlen = 1;\r
-    else\r
-       readlen = sizeof(ctx->buffer);\r
-\r
-    while (1) {\r
-       if (povl) {\r
-           memset(povl, 0, sizeof(OVERLAPPED));\r
-           povl->hEvent = oev;\r
-       }\r
-       readret = ReadFile(ctx->h, ctx->buffer,readlen, &ctx->len, povl);\r
-       if (!readret)\r
-           ctx->readerr = GetLastError();\r
-       else\r
-           ctx->readerr = 0;\r
-       if (povl && !readret && ctx->readerr == ERROR_IO_PENDING) {\r
-           WaitForSingleObject(povl->hEvent, INFINITE);\r
-           readret = GetOverlappedResult(ctx->h, povl, &ctx->len, FALSE);\r
-           if (!readret)\r
-               ctx->readerr = GetLastError();\r
-           else\r
-               ctx->readerr = 0;\r
-       }\r
-\r
-       if (!readret) {\r
-           /*\r
-            * Windows apparently sends ERROR_BROKEN_PIPE when a\r
-            * pipe we're reading from is closed normally from the\r
-            * writing end. This is ludicrous; if that situation\r
-            * isn't a natural EOF, _nothing_ is. So if we get that\r
-            * particular error, we pretend it's EOF.\r
-            */\r
-           if (ctx->readerr == ERROR_BROKEN_PIPE)\r
-               ctx->readerr = 0;\r
-           ctx->len = 0;\r
-       }\r
-\r
-       if (readret && ctx->len == 0 &&\r
-           (ctx->flags & HANDLE_FLAG_IGNOREEOF))\r
-           continue;\r
-\r
-       SetEvent(ctx->ev_to_main);\r
-\r
-       if (!ctx->len)\r
-           break;\r
-\r
-       WaitForSingleObject(ctx->ev_from_main, INFINITE);\r
-       if (ctx->done)\r
-           break;                     /* main thread told us to shut down */\r
-    }\r
-\r
-    if (povl)\r
-       CloseHandle(oev);\r
-\r
-    return 0;\r
-}\r
-\r
-/*\r
- * This is called after a succcessful read, or from the\r
- * `unthrottle' function. It decides whether or not to begin a new\r
- * read operation.\r
- */\r
-static void handle_throttle(struct handle_input *ctx, int backlog)\r
-{\r
-    if (ctx->defunct)\r
-       return;\r
-\r
-    /*\r
-     * If there's a read operation already in progress, do nothing:\r
-     * when that completes, we'll come back here and be in a\r
-     * position to make a better decision.\r
-     */\r
-    if (ctx->busy)\r
-       return;\r
-\r
-    /*\r
-     * Otherwise, we must decide whether to start a new read based\r
-     * on the size of the backlog.\r
-     */\r
-    if (backlog < MAX_BACKLOG) {\r
-       SetEvent(ctx->ev_from_main);\r
-       ctx->busy = TRUE;\r
-    }\r
-}\r
-\r
-/* ----------------------------------------------------------------------\r
- * Output threads.\r
- */\r
-\r
-/*\r
- * Data required by an output thread.\r
- */\r
-struct handle_output {\r
-    /*\r
-     * Copy of the handle_generic structure.\r
-     */\r
-    HANDLE h;                         /* the handle itself */\r
-    HANDLE ev_to_main;                /* event used to signal main thread */\r
-    HANDLE ev_from_main;              /* event used to signal back to us */\r
-    int moribund;                     /* are we going to kill this soon? */\r
-    int done;                         /* request subthread to terminate */\r
-    int defunct;                      /* has the subthread already gone? */\r
-    int busy;                         /* operation currently in progress? */\r
-    void *privdata;                   /* for client to remember who they are */\r
-\r
-    /*\r
-     * Data set at initialisation and then read-only.\r
-     */\r
-    int flags;\r
-\r
-    /*\r
-     * Data set by the main thread before signalling ev_from_main,\r
-     * and read by the input thread after receiving that signal.\r
-     */\r
-    char *buffer;                     /* the data to write */\r
-    DWORD len;                        /* how much data there is */\r
-\r
-    /*\r
-     * Data set by the input thread before signalling ev_to_main,\r
-     * and read by the main thread after receiving that signal.\r
-     */\r
-    DWORD lenwritten;                 /* how much data we actually wrote */\r
-    int writeerr;                     /* return value from WriteFile */\r
-\r
-    /*\r
-     * Data only ever read or written by the main thread.\r
-     */\r
-    bufchain queued_data;             /* data still waiting to be written */\r
-\r
-    /*\r
-     * Callback function called when the backlog in the bufchain\r
-     * drops.\r
-     */\r
-    handle_outputfn_t sentdata;\r
-};\r
-\r
-static DWORD WINAPI handle_output_threadfunc(void *param)\r
-{\r
-    struct handle_output *ctx = (struct handle_output *) param;\r
-    OVERLAPPED ovl, *povl;\r
-    HANDLE oev;\r
-    int writeret;\r
-\r
-    if (ctx->flags & HANDLE_FLAG_OVERLAPPED) {\r
-       povl = &ovl;\r
-       oev = CreateEvent(NULL, TRUE, FALSE, NULL);\r
-    } else {\r
-       povl = NULL;\r
-    }\r
-\r
-    while (1) {\r
-       WaitForSingleObject(ctx->ev_from_main, INFINITE);\r
-       if (ctx->done) {\r
-           SetEvent(ctx->ev_to_main);\r
-           break;\r
-       }\r
-       if (povl) {\r
-           memset(povl, 0, sizeof(OVERLAPPED));\r
-           povl->hEvent = oev;\r
-       }\r
-\r
-       writeret = WriteFile(ctx->h, ctx->buffer, ctx->len,\r
-                            &ctx->lenwritten, povl);\r
-       if (!writeret)\r
-           ctx->writeerr = GetLastError();\r
-       else\r
-           ctx->writeerr = 0;\r
-       if (povl && !writeret && GetLastError() == ERROR_IO_PENDING) {\r
-           writeret = GetOverlappedResult(ctx->h, povl,\r
-                                          &ctx->lenwritten, TRUE);\r
-           if (!writeret)\r
-               ctx->writeerr = GetLastError();\r
-           else\r
-               ctx->writeerr = 0;\r
-       }\r
-\r
-       SetEvent(ctx->ev_to_main);\r
-       if (!writeret)\r
-           break;\r
-    }\r
-\r
-    if (povl)\r
-       CloseHandle(oev);\r
-\r
-    return 0;\r
-}\r
-\r
-static void handle_try_output(struct handle_output *ctx)\r
-{\r
-    void *senddata;\r
-    int sendlen;\r
-\r
-    if (!ctx->busy && bufchain_size(&ctx->queued_data)) {\r
-       bufchain_prefix(&ctx->queued_data, &senddata, &sendlen);\r
-       ctx->buffer = senddata;\r
-       ctx->len = sendlen;\r
-       SetEvent(ctx->ev_from_main);\r
-       ctx->busy = TRUE;\r
-    }\r
-}\r
-\r
-/* ----------------------------------------------------------------------\r
- * Unified code handling both input and output threads.\r
- */\r
-\r
-struct handle {\r
-    int output;\r
-    union {\r
-       struct handle_generic g;\r
-       struct handle_input i;\r
-       struct handle_output o;\r
-    } u;\r
-};\r
-\r
-static tree234 *handles_by_evtomain;\r
-\r
-static int handle_cmp_evtomain(void *av, void *bv)\r
-{\r
-    struct handle *a = (struct handle *)av;\r
-    struct handle *b = (struct handle *)bv;\r
-\r
-    if ((unsigned)a->u.g.ev_to_main < (unsigned)b->u.g.ev_to_main)\r
-       return -1;\r
-    else if ((unsigned)a->u.g.ev_to_main > (unsigned)b->u.g.ev_to_main)\r
-       return +1;\r
-    else\r
-       return 0;\r
-}\r
-\r
-static int handle_find_evtomain(void *av, void *bv)\r
-{\r
-    HANDLE *a = (HANDLE *)av;\r
-    struct handle *b = (struct handle *)bv;\r
-\r
-    if ((unsigned)*a < (unsigned)b->u.g.ev_to_main)\r
-       return -1;\r
-    else if ((unsigned)*a > (unsigned)b->u.g.ev_to_main)\r
-       return +1;\r
-    else\r
-       return 0;\r
-}\r
-\r
-struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata,\r
-                               void *privdata, int flags)\r
-{\r
-    struct handle *h = snew(struct handle);\r
-    DWORD in_threadid; /* required for Win9x */\r
-\r
-    h->output = FALSE;\r
-    h->u.i.h = handle;\r
-    h->u.i.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL);\r
-    h->u.i.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL);\r
-    h->u.i.gotdata = gotdata;\r
-    h->u.i.defunct = FALSE;\r
-    h->u.i.moribund = FALSE;\r
-    h->u.i.done = FALSE;\r
-    h->u.i.privdata = privdata;\r
-    h->u.i.flags = flags;\r
-\r
-    if (!handles_by_evtomain)\r
-       handles_by_evtomain = newtree234(handle_cmp_evtomain);\r
-    add234(handles_by_evtomain, h);\r
-\r
-    CreateThread(NULL, 0, handle_input_threadfunc,\r
-                &h->u.i, 0, &in_threadid);\r
-    h->u.i.busy = TRUE;\r
-\r
-    return h;\r
-}\r
-\r
-struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata,\r
-                                void *privdata, int flags)\r
-{\r
-    struct handle *h = snew(struct handle);\r
-    DWORD out_threadid; /* required for Win9x */\r
-\r
-    h->output = TRUE;\r
-    h->u.o.h = handle;\r
-    h->u.o.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL);\r
-    h->u.o.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL);\r
-    h->u.o.busy = FALSE;\r
-    h->u.o.defunct = FALSE;\r
-    h->u.o.moribund = FALSE;\r
-    h->u.o.done = FALSE;\r
-    h->u.o.privdata = privdata;\r
-    bufchain_init(&h->u.o.queued_data);\r
-    h->u.o.sentdata = sentdata;\r
-    h->u.o.flags = flags;\r
-\r
-    if (!handles_by_evtomain)\r
-       handles_by_evtomain = newtree234(handle_cmp_evtomain);\r
-    add234(handles_by_evtomain, h);\r
-\r
-    CreateThread(NULL, 0, handle_output_threadfunc,\r
-                &h->u.o, 0, &out_threadid);\r
-\r
-    return h;\r
-}\r
-\r
-int handle_write(struct handle *h, const void *data, int len)\r
-{\r
-    assert(h->output);\r
-    bufchain_add(&h->u.o.queued_data, data, len);\r
-    handle_try_output(&h->u.o);\r
-    return bufchain_size(&h->u.o.queued_data);\r
-}\r
-\r
-HANDLE *handle_get_events(int *nevents)\r
-{\r
-    HANDLE *ret;\r
-    struct handle *h;\r
-    int i, n, size;\r
-\r
-    /*\r
-     * Go through our tree counting the handle objects currently\r
-     * engaged in useful activity.\r
-     */\r
-    ret = NULL;\r
-    n = size = 0;\r
-    if (handles_by_evtomain) {\r
-       for (i = 0; (h = index234(handles_by_evtomain, i)) != NULL; i++) {\r
-           if (h->u.g.busy) {\r
-               if (n >= size) {\r
-                   size += 32;\r
-                   ret = sresize(ret, size, HANDLE);\r
-               }\r
-               ret[n++] = h->u.g.ev_to_main;\r
-           }\r
-       }\r
-    }\r
-\r
-    *nevents = n;\r
-    return ret;\r
-}\r
-\r
-static void handle_destroy(struct handle *h)\r
-{\r
-    if (h->output)\r
-       bufchain_clear(&h->u.o.queued_data);\r
-    CloseHandle(h->u.g.ev_from_main);\r
-    CloseHandle(h->u.g.ev_to_main);\r
-    del234(handles_by_evtomain, h);\r
-    sfree(h);\r
-}\r
-\r
-void handle_free(struct handle *h)\r
-{\r
-    /*\r
-     * If the handle is currently busy, we cannot immediately free\r
-     * it. Instead we must wait until it's finished its current\r
-     * operation, because otherwise the subthread will write to\r
-     * invalid memory after we free its context from under it.\r
-     */\r
-    assert(h && !h->u.g.moribund);\r
-    if (h->u.g.busy) {\r
-       /*\r
-        * Just set the moribund flag, which will be noticed next\r
-        * time an operation completes.\r
-        */\r
-       h->u.g.moribund = TRUE;\r
-    } else if (h->u.g.defunct) {\r
-       /*\r
-        * There isn't even a subthread; we can go straight to\r
-        * handle_destroy.\r
-        */\r
-       handle_destroy(h);\r
-    } else {\r
-       /*\r
-        * The subthread is alive but not busy, so we now signal it\r
-        * to die. Set the moribund flag to indicate that it will\r
-        * want destroying after that.\r
-        */\r
-       h->u.g.moribund = TRUE;\r
-       h->u.g.done = TRUE;\r
-       h->u.g.busy = TRUE;\r
-       SetEvent(h->u.g.ev_from_main);\r
-    }\r
-}\r
-\r
-void handle_got_event(HANDLE event)\r
-{\r
-    struct handle *h;\r
-\r
-    assert(handles_by_evtomain);\r
-    h = find234(handles_by_evtomain, &event, handle_find_evtomain);\r
-    if (!h) {\r
-       /*\r
-        * This isn't an error condition. If two or more event\r
-        * objects were signalled during the same select operation,\r
-        * and processing of the first caused the second handle to\r
-        * be closed, then it will sometimes happen that we receive\r
-        * an event notification here for a handle which is already\r
-        * deceased. In that situation we simply do nothing.\r
-        */\r
-       return;\r
-    }\r
-\r
-    if (h->u.g.moribund) {\r
-       /*\r
-        * A moribund handle is already treated as dead from the\r
-        * external user's point of view, so do nothing with the\r
-        * actual event. Just signal the thread to die if\r
-        * necessary, or destroy the handle if not.\r
-        */\r
-       if (h->u.g.done) {\r
-           handle_destroy(h);\r
-       } else {\r
-           h->u.g.done = TRUE;\r
-           h->u.g.busy = TRUE;\r
-           SetEvent(h->u.g.ev_from_main);\r
-       }\r
-       return;\r
-    }\r
-\r
-    if (!h->output) {\r
-       int backlog;\r
-\r
-       h->u.i.busy = FALSE;\r
-\r
-       /*\r
-        * A signal on an input handle means data has arrived.\r
-        */\r
-       if (h->u.i.len == 0) {\r
-           /*\r
-            * EOF, or (nearly equivalently) read error.\r
-            */\r
-           h->u.i.gotdata(h, NULL, -h->u.i.readerr);\r
-           h->u.i.defunct = TRUE;\r
-       } else {\r
-           backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len);\r
-           handle_throttle(&h->u.i, backlog);\r
-       }\r
-    } else {\r
-       h->u.o.busy = FALSE;\r
-\r
-       /*\r
-        * A signal on an output handle means we have completed a\r
-        * write. Call the callback to indicate that the output\r
-        * buffer size has decreased, or to indicate an error.\r
-        */\r
-       if (h->u.o.writeerr) {\r
-           /*\r
-            * Write error. Send a negative value to the callback,\r
-            * and mark the thread as defunct (because the output\r
-            * thread is terminating by now).\r
-            */\r
-           h->u.o.sentdata(h, -h->u.o.writeerr);\r
-           h->u.o.defunct = TRUE;\r
-       } else {\r
-           bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten);\r
-           h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data));\r
-           handle_try_output(&h->u.o);\r
-       }\r
-    }\r
-}\r
-\r
-void handle_unthrottle(struct handle *h, int backlog)\r
-{\r
-    assert(!h->output);\r
-    handle_throttle(&h->u.i, backlog);\r
-}\r
-\r
-int handle_backlog(struct handle *h)\r
-{\r
-    assert(h->output);\r
-    return bufchain_size(&h->u.o.queued_data);\r
-}\r
-\r
-void *handle_get_privdata(struct handle *h)\r
-{\r
-    return h->u.g.privdata;\r
-}\r