Colin's const-fixing Patch Of Death. Seems to build fine on Windows
[u/mdw/putty] / mac / otnet.c
index 7a2cbd8..970a78e 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>
 
@@ -39,19 +44,27 @@ struct Socket_tag {
 typedef struct Socket_tag *Actual_Socket;
 
 struct SockAddr_tag {
+    int resolved;
     OSStatus error;
-    DNSAddress address;
+    InetHostInfo hostinfo;
+    char hostname[512];
 };
 
 /* Globals */
 
 static struct {
     Actual_Socket socklist;
+    InetSvcRef inetsvc;
 } ot;
 
 OSErr ot_init(void)
 {
-    return InitOpenTransport();
+    OSStatus err;
+
+    err = InitOpenTransport();
+    if (err != kOTNoError) return err;
+    ot.inetsvc = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err);
+    return err;
 }
 
 void ot_cleanup(void)
@@ -66,32 +79,47 @@ 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));
-    
-    ret->error = kOTNoError;
-    OTInitDNSAddress(&(ret->address), host);
-  
-    /* for now we'll pretend canonicalname is always just host */
-
-    *canonicalname = smalloc(1+strlen(host));
-    strcpy(*canonicalname, host);
+    SockAddr ret = snew(struct SockAddr_tag);
+    char *realhost;
+
+    /* 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 = 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);
     
-    OTInitDNSAddress(&(ret->address), host);
-  
+    ret->resolved = FALSE;
+    ret->error = kOTNoError;
+    ret->hostname[0] = '\0';
+    strncat(ret->hostname, host, lenof(ret->hostname) - 1);
     return ret;
 }
 
 void ot_getaddr(SockAddr addr, char *buf, int buflen)
 {
-    strncpy(buf, (addr->address).fName, buflen);
+    char mybuf[16];
+
+    buf[0] = '\0';
+    if (addr->resolved) {
+       /* XXX only return first address */
+       OTInetHostToString(addr->hostinfo.addrs[0], mybuf);
+       strncat(buf, mybuf, buflen - 1);
+    } else
+       strncat(buf, addr->hostname, buflen - 1);
 }
 
 /* I think "local" here really means "loopback" */
@@ -104,19 +132,28 @@ int ot_hostname_is_local(char *name)
 
 int ot_address_is_local(SockAddr addr)
 {
+    int i;
 
-    /* FIXME */
+    if (addr->resolved)
+       for (i = 0; i < kMaxHostAddrs; i++)
+           if (addr->hostinfo.addrs[i] & 0xff000000 == 0x7f000000)
+               return TRUE;
     return FALSE;
 }
 
 int ot_addrtype(SockAddr addr)
 {
-    return ADDRTYPE_IPV4;
+
+    if (addr->resolved)
+       return ADDRTYPE_IPV4;
+    return ADDRTYPE_NAME;
 }
 
 void ot_addrcopy(SockAddr addr, char *buf)
 {
-  
+
+    /* XXX only return first address */
+    memcpy(buf, &addr->hostinfo.addrs[0], 4);
 }
 
 void ot_addr_free(SockAddr addr)
@@ -148,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);
 
@@ -168,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;
@@ -214,9 +251,10 @@ Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
     Actual_Socket ret;
     EndpointRef ep;
     OSStatus err;
+    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;
@@ -262,11 +300,12 @@ Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
      * Connect to remote address.
      */
 
-    /* FIXME: bolt the port onto the end */
+    /* XXX Try non-primary addresses */
+    OTInitInetAddress(&dest, port, addr->hostinfo.addrs[0]);
 
     memset(&connectCall, 0, sizeof(TCall));
-    connectCall.addr.buf = (UInt8 *) &(addr->address);
-    connectCall.addr.len = sizeof(DNSAddress);
+    connectCall.addr.buf = (UInt8 *) &dest;
+    connectCall.addr.len = sizeof(dest);
 
     err = OTConnect(ep, &connectCall, nil);
   
@@ -281,6 +320,8 @@ Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
     /* Add this to the list of all sockets */
     ret->next = ot.socklist;
     ret->prev = &ot.socklist;
+    if (ret->next != NULL)
+       ret->next->prev = &ret->next;
     ot.socklist = ret;
 
     return (Socket) ret;
@@ -379,7 +420,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];
@@ -430,9 +471,13 @@ void ot_recv(Actual_Socket s)
 
     if (s->frozen) return;
 
-    while ((o = OTRcv(s->ep, buf, sizeof(buf), &flags)) != kOTNoDataErr) {
-       plug_receive(s->plug, 0, buf, sizeof(buf));
-    }
+    do {
+       o = OTRcv(s->ep, buf, sizeof(buf), &flags);
+       if (o > 0)
+           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);
 }