Colin's and my fixes to connection_fatal().
[u/mdw/putty] / telnet.c
index f2d31ef..d358f82 100644 (file)
--- a/telnet.c
+++ b/telnet.c
 #define        TELOPT_AUTHENTICATION 37       /* Authenticate */
 #define        TELOPT_ENCRYPT  38             /* Encryption option */
 #define TELOPT_NEW_ENVIRON 39         /* New - Environment variables */
+#define TELOPT_TN3270E 40             /* TN3270 enhancements */
+#define TELOPT_XAUTH   41
+#define TELOPT_CHARSET 42             /* Character set */
+#define TELOPT_RSP     43             /* Remote serial port */
+#define TELOPT_COM_PORT_OPTION 44      /* Com port control */
+#define TELOPT_SLE     45             /* Suppress local echo */
+#define TELOPT_STARTTLS        46             /* Start TLS */
+#define TELOPT_KERMIT  47             /* Automatic Kermit file transfer */
+#define TELOPT_SEND_URL        48
+#define TELOPT_FORWARD_X 49
+#define TELOPT_PRAGMA_LOGON    138
+#define TELOPT_SSPI_LOGON      139
+#define TELOPT_PRAGMA_HEARTBEAT        140
 #define        TELOPT_EXOPL    255            /* extended-options-list */
 
 #define        TELQUAL_IS      0              /* option is... */
@@ -132,6 +145,19 @@ static char *telopt(int opt)
     i(AUTHENTICATION);
     i(ENCRYPT);
     i(NEW_ENVIRON);
+    i(TN3270E);
+    i(XAUTH);
+    i(CHARSET);
+    i(RSP);
+    i(COM_PORT_OPTION);
+    i(SLE);
+    i(STARTTLS);
+    i(KERMIT);
+    i(SEND_URL);
+    i(FORWARD_X);
+    i(PRAGMA_LOGON);
+    i(SSPI_LOGON);
+    i(PRAGMA_HEARTBEAT);
     i(EXOPL);
 #undef i
     return "<unknown>";
@@ -585,18 +611,11 @@ static void do_telnet_read(Telnet telnet, char *buf, int len)
            else {
              subneg_addchar:
                if (telnet->sb_len >= telnet->sb_size) {
-                   unsigned char *newbuf;
                    telnet->sb_size += SB_DELTA;
-                   newbuf = (telnet->sb_buf ?
-                             srealloc(telnet->sb_buf, telnet->sb_size) :
-                             smalloc(telnet->sb_size));
-                   if (newbuf)
-                       telnet->sb_buf = newbuf;
-                   else
-                       telnet->sb_size -= SB_DELTA;
+                   telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
+                                            unsigned char);
                }
-               if (telnet->sb_len < telnet->sb_size)
-                   telnet->sb_buf[telnet->sb_len++] = c;
+               telnet->sb_buf[telnet->sb_len++] = c;
                telnet->state = SUBNEGOT;       /* in case we came here by goto */
            }
            break;
@@ -624,7 +643,7 @@ static int telnet_closing(Plug plug, char *error_msg, int error_code,
     if (error_msg) {
        /* A socket error has occurred. */
        logevent(telnet->frontend, error_msg);
-       connection_fatal("%s", error_msg);
+       connection_fatal(telnet->frontend, "%s", error_msg);
     }                                 /* Otherwise, the remote side closed the connection normally. */
     return 0;
 }
@@ -665,7 +684,7 @@ static char *telnet_init(void *frontend_handle, void **backend_handle,
     char *err;
     Telnet telnet;
 
-    telnet = smalloc(sizeof(*telnet));
+    telnet = snew(struct telnet_tag);
     telnet->fn = &fn_table;
     telnet->cfg = *cfg;                       /* STRUCTURE COPY */
     telnet->s = NULL;
@@ -902,9 +921,10 @@ static void telnet_special(void *handle, Telnet_Special code)
        telnet->bufsize = sk_write(telnet->s, (char *)b, 2);
        break;
       case TS_EOL:
-       /* In BINARY mode, CR-LF becomes just CR. */
+       /* In BINARY mode, CR-LF becomes just CR -
+        * and without the NUL suffix too. */
        if (telnet->opt_states[o_we_bin.index] == ACTIVE)
-           telnet->bufsize = sk_write(telnet->s, "\r", 2);
+           telnet->bufsize = sk_write(telnet->s, "\r", 1);
        else
            telnet->bufsize = sk_write(telnet->s, "\r\n", 2);
        break;
@@ -935,6 +955,29 @@ static void telnet_special(void *handle, Telnet_Special code)
     }
 }
 
+static const struct telnet_special *telnet_get_specials(void *handle)
+{
+    static const struct telnet_special specials[] = {
+       {"Are You There", TS_AYT},
+       {"Break", TS_BRK},
+       {"Synch", TS_SYNCH},
+       {"Erase Character", TS_EC},
+       {"Erase Line", TS_EL},
+       {"Go Ahead", TS_GA},
+       {"No Operation", TS_NOP},
+       {"", 0},
+       {"Abort Process", TS_ABORT},
+       {"Abort Output", TS_AO},
+       {"Interrupt Process", TS_IP},
+       {"Suspend Process", TS_SUSP},
+       {"", 0},
+       {"End Of Record", TS_EOR},
+       {"End Of File", TS_EOF},
+       {NULL, 0}
+    };
+    return specials;
+}
+
 static Socket telnet_socket(void *handle)
 {
     Telnet telnet = (Telnet) handle;
@@ -976,9 +1019,12 @@ static void telnet_provide_logctx(void *handle, void *logctx)
 
 static int telnet_exitcode(void *handle)
 {
-    /* Telnet telnet = (Telnet) handle; */
-    /* Telnet doesn't transmit exit codes back to the client */
-    return 0;
+    Telnet telnet = (Telnet) handle;
+    if (telnet->s != NULL)
+        return -1;                     /* still connected */
+    else
+        /* Telnet doesn't transmit exit codes back to the client */
+        return 0;
 }
 
 Backend telnet_backend = {
@@ -989,6 +1035,7 @@ Backend telnet_backend = {
     telnet_sendbuffer,
     telnet_size,
     telnet_special,
+    telnet_get_specials,
     telnet_socket,
     telnet_exitcode,
     telnet_sendok,