Enable better build-time flexibility over which WinSock to include
[u/mdw/putty] / telnet.c
index 6b09a4a..6618fdf 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,19 +171,6 @@ 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 log_option (char *sender, int cmd, int option) {
     char buf[50];
     sprintf(buf, "%s:\t%s %s", sender,
@@ -384,7 +377,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 +389,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 +422,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 +457,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 +558,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 +589,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 +604,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,8 +620,11 @@ 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:
@@ -637,13 +640,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);
        }
@@ -745,10 +750,16 @@ static void telnet_special (Telnet_Special code) {
     }
 }
 
+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
 };