]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - telnet.c
Inaugural merge from branch 'pre-0.65'.
[PuTTY.git] / telnet.c
index dc98a7272aac27c5eb72d59d3747b0233214e1d1..ca1665258c61429384135c619aa7c6989f270d91 100644 (file)
--- a/telnet.c
+++ b/telnet.c
@@ -4,6 +4,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <limits.h>
 
 #include "putty.h"
 
@@ -112,7 +113,7 @@ enum { TELOPTS(telnet_enum) dummy=0 };
        ( (x) != IAC && \
              (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR))
 
-static char *telopt(int opt)
+static const char *telopt(int opt)
 {
 #define telnet_str(x,y) case TELOPT_##x: return #x;
     switch (opt) {
@@ -181,6 +182,7 @@ typedef struct telnet_tag {
     /* the above field _must_ be first in the structure */
 
     Socket s;
+    int closed_on_socket_error;
 
     void *frontend;
     void *ldisc;
@@ -210,14 +212,14 @@ typedef struct telnet_tag {
 
 #define SB_DELTA 1024
 
-static void c_write(Telnet telnet, char *buf, int len)
+static void c_write(Telnet telnet, const char *buf, int len)
 {
     int backlog;
     backlog = from_backend(telnet->frontend, 0, buf, len);
     sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG);
 }
 
-static void log_option(Telnet telnet, char *sender, int cmd, int option)
+static void log_option(Telnet telnet, const char *sender, int cmd, int option)
 {
     char *buf;
     /*
@@ -262,7 +264,7 @@ static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
     else if (o->option == TELOPT_SGA && o->send == DO)
        telnet->editing = !enabled;
     if (telnet->ldisc)                /* cause ldisc to notice the change */
-       ldisc_send(telnet->ldisc, NULL, 0, 0);
+       ldisc_echoedit_update(telnet->ldisc);
 
     /* Ensure we get the minimum options */
     if (!telnet->activated) {
@@ -660,6 +662,7 @@ static void telnet_log(Plug plug, int type, SockAddr addr, int port,
        msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg);
 
     logevent(telnet->frontend, msg);
+    sfree(msg);
 }
 
 static int telnet_closing(Plug plug, const char *error_msg, int error_code,
@@ -676,6 +679,8 @@ static int telnet_closing(Plug plug, const char *error_msg, int error_code,
     if (telnet->s) {
         sk_close(telnet->s);
         telnet->s = NULL;
+        if (error_msg)
+            telnet->closed_on_socket_error = TRUE;
        notify_remote_exit(telnet->frontend);
     }
     if (error_msg) {
@@ -710,7 +715,7 @@ static void telnet_sent(Plug plug, int bufsize)
  * freed by the caller.
  */
 static const char *telnet_init(void *frontend_handle, void **backend_handle,
-                              Conf *conf, char *host, int port,
+                              Conf *conf, const char *host, int port,
                               char **realhost, int nodelay, int keepalive)
 {
     static const struct plug_function_table fn_table = {
@@ -729,6 +734,7 @@ static const char *telnet_init(void *frontend_handle, void **backend_handle,
     telnet->fn = &fn_table;
     telnet->conf = conf_copy(conf);
     telnet->s = NULL;
+    telnet->closed_on_socket_error = FALSE;
     telnet->echoing = TRUE;
     telnet->editing = TRUE;
     telnet->activated = FALSE;
@@ -812,15 +818,10 @@ static const char *telnet_init(void *frontend_handle, void **backend_handle,
 
        sfree(*realhost);
        *realhost = dupstr(loghost);
-       colon = strrchr(*realhost, ':');
-       if (colon) {
-           /*
-            * FIXME: if we ever update this aspect of ssh.c for
-            * IPv6 literal management, this should change in line
-            * with it.
-            */
+
+       colon = host_strrchr(*realhost, ':');
+       if (colon)
            *colon++ = '\0';
-       }
     }
 
     return NULL;
@@ -854,7 +855,7 @@ static void telnet_reconfig(void *handle, Conf *conf)
 /*
  * Called to send data down the Telnet connection.
  */
-static int telnet_send(void *handle, char *buf, int len)
+static int telnet_send(void *handle, const char *buf, int len)
 {
     Telnet telnet = (Telnet) handle;
     unsigned char *p, *end;
@@ -1096,6 +1097,8 @@ static int telnet_exitcode(void *handle)
     Telnet telnet = (Telnet) handle;
     if (telnet->s != NULL)
         return -1;                     /* still connected */
+    else if (telnet->closed_on_socket_error)
+        return INT_MAX;     /* a socket error counts as an unclean exit */
     else
         /* Telnet doesn't transmit exit codes back to the client */
         return 0;