Add a configuration option for TCP keepalives (SO_KEEPALIVE), default off.
[u/mdw/putty] / mac / otnet.c
index baa7a8a..35d3b16 100644 (file)
@@ -2,6 +2,11 @@
  * Macintosh OpenTransport networking abstraction
  */
 
+#if TARGET_API_MAC_CARBON
+#define OTCARBONAPPLICATION 1
+#endif
+
+#include <Files.h> /* Needed by OpenTransportInternet.h */
 #include <OpenTransport.h>
 #include <OpenTptInternet.h>
 
@@ -74,26 +79,28 @@ void ot_cleanup(void)
     CloseOpenTransport();
 }
 
-SockAddr ot_namelookup(char *host, char **canonicalname)
+SockAddr ot_namelookup(char const *host, char **canonicalname)
 {
-    SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
+    SockAddr ret = snew(struct SockAddr_tag);
     char *realhost;
-    
-    ret->error = OTInetStringToAddress(ot.inetsvc, host, &ret->hostinfo);
+
+    /* Casting away const -- hope OTInetStringToAddress is sensible */
+    ret->error = OTInetStringToAddress(ot.inetsvc, (char *)host,
+                                      &ret->hostinfo);
     ret->resolved = TRUE;
 
     if (ret->error == kOTNoError)
        realhost = ret->hostinfo.name;
     else
        realhost = "";
-    *canonicalname = smalloc(1+strlen(realhost));
+    *canonicalname = snewn(1+strlen(realhost), char);
     strcpy(*canonicalname, realhost);
     return ret;
 }
 
-SockAddr ot_nonamelookup(char *host)
+SockAddr ot_nonamelookup(char const *host)
 {
-    SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
+    SockAddr ret = snew(struct SockAddr_tag);
     
     ret->resolved = FALSE;
     ret->error = kOTNoError;
@@ -178,7 +185,7 @@ static int ot_tcp_write_oob(Socket s, char const *data, int len);
 static void ot_tcp_set_private_ptr(Socket s, void *ptr);
 static void *ot_tcp_get_private_ptr(Socket s);
 static void ot_tcp_set_frozen(Socket s, int is_frozen);
-static char *ot_tcp_socket_error(Socket s);
+static const char *ot_tcp_socket_error(Socket s);
 static void ot_recv(Actual_Socket s);
 void ot_poll(void);
 
@@ -198,7 +205,7 @@ Socket ot_register(void *sock, Plug plug)
     
     Actual_Socket ret;
 
-    ret = smalloc(sizeof(struct Socket_tag));
+    ret = snew(struct Socket_tag);
     ret->fn = &fn_table;
     ret->error = kOTNoError;
     ret->plug = plug;
@@ -227,7 +234,7 @@ Socket ot_register(void *sock, Plug plug)
 }
 
 Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
-             int nodelay, Plug plug)
+             int nodelay, int keepalive, Plug plug)
 {
     static struct socket_function_table fn_table = {
        ot_tcp_plug,
@@ -247,7 +254,7 @@ Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
     InetAddress dest;
     TCall connectCall;
 
-    ret = smalloc(sizeof(struct Socket_tag));
+    ret = snew(struct Socket_tag);
     ret->fn = &fn_table;
     ret->error = kOTNoError;
     ret->plug = plug;
@@ -274,7 +281,7 @@ Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
        return (Socket) ret;
     }
 
-    /* TODO: oobinline, nodelay */
+    /* TODO: oobinline, nodelay, keepalive */
 
     /*
      * Bind to local address.
@@ -317,6 +324,8 @@ Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
        ret->next->prev = &ret->next;
     ot.socklist = ret;
 
+    /* XXX: don't know whether we can sk_addr_free(addr); */
+
     return (Socket) ret;
 }
     
@@ -413,7 +422,7 @@ char *ot_addr_error(SockAddr addr)
     sprintf(buf, "error %d", addr->error);
     return buf;
 }
-static char *ot_tcp_socket_error(Socket sock)
+static const char *ot_tcp_socket_error(Socket sock)
 {
     Actual_Socket s = (Actual_Socket) sock;
     static char buf[128];
@@ -467,7 +476,7 @@ void ot_recv(Actual_Socket s)
     do {
        o = OTRcv(s->ep, buf, sizeof(buf), &flags);
        if (o > 0)
-           plug_receive(s->plug, 0, buf, sizeof(buf));
+           plug_receive(s->plug, 0, buf, o);
        if (o < 0 && o != kOTNoDataErr)
            plug_closing(s->plug, NULL, 0, 0); /* XXX Error msg */
     } while (o > 0);