When polling MacTCP connections, check the connection state and if the far
[u/mdw/putty] / mac / mtcpnet.c
index 4524818..bd63087 100644 (file)
@@ -142,6 +142,19 @@ enum { uppAddrToStrProcInfo = kCStackBased
 
 /* End of AddressXlation.h bits */
 
+/* TCP connection states, mysteriously missing from <MacTCP.h> */
+#define TCPS_CLOSED            0
+#define TCPS_LISTEN            2
+#define TCPS_SYN_RECEIVED      4
+#define TCPS_SYN_SENT          6
+#define TCPS_ESTABLISHED       8
+#define TCPS_FIN_WAIT_1                10
+#define TCPS_FIN_WAIT_2                12
+#define TCPS_CLOSE_WAIT                14
+#define TCPS_CLOSING           16
+#define TCPS_LAST_ACK          18
+#define TCPS_TIME_WAIT         20
+
 struct Socket_tag {
     struct socket_function_table *fn;
     /* the above variable absolutely *must* be the first in this structure */
@@ -197,8 +210,8 @@ static pascal void mactcp_asr(StreamPtr, unsigned short, Ptr, unsigned short,
 static Plug mactcp_plug(Socket, Plug);
 static void mactcp_flush(Socket);
 static void mactcp_close(Socket);
-static int mactcp_write(Socket, char *, int);
-static int mactcp_write_oob(Socket, char *, int);
+static int mactcp_write(Socket, char const *, int);
+static int mactcp_write_oob(Socket, char const*, int);
 static void mactcp_set_private_ptr(Socket, void *);
 static void *mactcp_get_private_ptr(Socket);
 static char *mactcp_socket_error(Socket);
@@ -545,14 +558,18 @@ static void mactcp_close(Socket sock)
     sfree(s);
 }
 
-static int mactcp_write(Socket sock, char *buf, int len)
+static int mactcp_write(Socket sock, char const *buf, int len)
 {
     Actual_Socket s = (Actual_Socket) sock;
     wdsEntry wds[2];
     TCPiopb pb;
 
+    /* 
+     * Casting away const from buf should be safe -- MacTCP won't
+     * write to it.
+     */
     wds[0].length = len;
-    wds[0].ptr = buf;
+    wds[0].ptr = (char *)buf;
     wds[1].length = 0;
 
     pb.ioCRefNum = mactcp.refnum;
@@ -567,7 +584,7 @@ static int mactcp_write(Socket sock, char *buf, int len)
     return 0;
 }
 
-static int mactcp_write_oob(Socket sock, char *buf, int len)
+static int mactcp_write_oob(Socket sock, char const *buf, int len)
 {
 
     fatalbox("mactcp_write_oob");
@@ -602,8 +619,12 @@ void mactcp_poll(void)
            if (pb.csParam.status.amtUnreadData == 0)
                break;
            mactcp_recv(s, pb.csParam.status.amtUnreadData);
-           /* Should check connectionState in case remote has closed */
        } while (TRUE);
+       switch (pb.csParam.status.connectionState) {
+         case TCPS_CLOSE_WAIT:
+           /* Remote end has sent us a FIN */
+           plug_closing(s->plug, NULL, 0, 0);
+       }
       next_socket:
        ;
     }