Introduce a sane interface function, from_backend(), for backends to
[u/mdw/putty] / telnet.c
index 6b09a4a..7b6c0bf 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -1,7 +1,13 @@
 #include <windows.h>
 #include <stdio.h>
 #include <stdlib.h>
+#ifndef AUTO_WINSOCK
+#ifdef WINSOCK_TWO
+#include <winsock2.h>
+#else
 #include <winsock.h>
+#endif
+#endif
 
 #include "putty.h"
 
@@ -165,17 +171,9 @@ static void s_write (void *buf, int len) {
     try_write();
 }
 
-static void c_write (char *buf, int len) {
-    while (len--) {
-       int new_head = (inbuf_head + 1) & INBUF_MASK;
-       if (new_head != inbuf_reap) {
-           inbuf[inbuf_head] = *buf++;
-           inbuf_head = new_head;
-       } else {
-            term_out();
-            if( inbuf_head == inbuf_reap ) len++; else break;
-       }
-    }
+static void c_write1(int c) {
+    char cc = (char)c;
+    from_backend(0, &cc, 1);
 }
 
 static void log_option (char *sender, int cmd, int option) {
@@ -384,7 +382,6 @@ static enum {
 } telnet_state = TOPLEVEL;
 
 static void do_telnet_read (char *buf, int len) {
-    unsigned char b[10];
 
     while (len--) {
        int c = (unsigned char) *buf++;
@@ -397,9 +394,8 @@ static void do_telnet_read (char *buf, int len) {
            else if (c == IAC)
                telnet_state = SEENIAC;
            else {
-               b[0] = c;
                if (!in_synch)
-                   c_write (b, 1);
+                   c_write1(c);
 
 #if 1
                /* I can't get the F***ing winsock to insert the urgent IAC
@@ -431,8 +427,7 @@ static void do_telnet_read (char *buf, int len) {
            else {
                /* ignore everything else; print it if it's IAC */
                if (c == IAC) {
-                   b[0] = c;
-                   c_write(b,1);
+                   c_write1(c);
                }
                telnet_state = TOPLEVEL;
            }
@@ -467,8 +462,8 @@ static void do_telnet_read (char *buf, int len) {
                    char *newbuf;
                    sb_size += SB_DELTA;
                    newbuf = (sb_buf ?
-                             realloc(sb_buf, sb_size) :
-                             malloc(sb_size));
+                             srealloc(sb_buf, sb_size) :
+                             smalloc(sb_size));
                    if (newbuf)
                        sb_buf = newbuf;
                    else
@@ -568,7 +563,7 @@ static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) {
          default: return "connect(): unknown error";
        }
 
-    if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ |
+    if (hwnd && WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ |
                        FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR)
        switch (WSAGetLastError()) {
          case WSAENETDOWN: return "Network is down";
@@ -599,6 +594,12 @@ static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) {
      * Set up SYNCH state.
      */
     in_synch = FALSE;
+
+    /*
+     * We have no pre-session phase.
+     */
+    begin_session();
+
     return NULL;
 }
 
@@ -608,7 +609,11 @@ static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) {
  */
 static int telnet_msg (WPARAM wParam, LPARAM lParam) {
     int ret;
-    char buf[256];
+    /* This needs to be larger than the packet size now that inbuf
+     * cannot overflow, in fact the fewer calls we make to windows
+     * the faster we will run!
+     */
+    char buf[16384];   
 
     /*
      * Because reading less than the whole of the available pending
@@ -620,16 +625,22 @@ static int telnet_msg (WPARAM wParam, LPARAM lParam) {
     if (s == INVALID_SOCKET)
        return 1;
 
-    if (WSAGETSELECTERROR(lParam) != 0)
+    if (WSAGETSELECTERROR(lParam) != 0) {
+        closesocket(s);
+        s = INVALID_SOCKET;
        return -WSAGETSELECTERROR(lParam);
+    }
 
     switch (WSAGETSELECTEVENT(lParam)) {
       case FD_READ:
       case FD_CLOSE:
        {
            int clear_of_oob = 1;
-           if (ioctlsocket (s, SIOCATMARK, &clear_of_oob) < 0 )
-               return -20000-WSAGetLastError();
+
+           /* Don't check for error return; some shims don't support
+            * this ioctl.
+            */
+           ioctlsocket (s, SIOCATMARK, &clear_of_oob);
 
            in_synch = !clear_of_oob;
 
@@ -637,13 +648,15 @@ static int telnet_msg (WPARAM wParam, LPARAM lParam) {
                ret = recv(s, buf, sizeof(buf), 0);
                if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
                    return 1;
-               if (ret < 0)                   /* any _other_ error */
+               if (ret < 0) {                 /* any _other_ error */
+                    closesocket(s);
+                    s = INVALID_SOCKET;
                    return -10000-WSAGetLastError();
+                }
                if (ret == 0) {
                    s = INVALID_SOCKET;
                    return 0;
                }
-
                do_telnet_read (buf, ret);
            } while (in_synch);
        }
@@ -742,13 +755,26 @@ static void telnet_special (Telnet_Special code) {
            send_opt (o_echo.nsend, o_echo.option);
        }
        break;
+      case TS_PING: 
+       if (o_they_sga.state == ACTIVE) {
+           b[1] = NOP; 
+           s_write (b, 2); 
+       }
+        break;
     }
 }
 
+static SOCKET telnet_socket(void) { return s; }
+
+static int telnet_sendok(void) { return 1; }
+
 Backend telnet_backend = {
     telnet_init,
     telnet_msg,
     telnet_send,
     telnet_size,
-    telnet_special
+    telnet_special,
+    telnet_socket,
+    telnet_sendok,
+    23
 };