Cute though DNSAddresses are, they're a poor match for the model the
authorben <ben@cda61777-01e9-0310-a592-d414129be87e>
Sun, 12 Jan 2003 13:06:59 +0000 (13:06 +0000)
committerben <ben@cda61777-01e9-0310-a592-d414129be87e>
Sun, 12 Jan 2003 13:06:59 +0000 (13:06 +0000)
rest of PuTTY has of DNS resolution, so I've replaced them with something
more conventional.  Sorry.

The new code has the advantage of working, of course.

git-svn-id: svn://svn.tartarus.org/sgt/putty@2551 cda61777-01e9-0310-a592-d414129be87e

mac/otnet.c

index 7a2cbd8..4c9ec4b 100644 (file)
@@ -39,19 +39,27 @@ struct Socket_tag {
 typedef struct Socket_tag *Actual_Socket;
 
 struct SockAddr_tag {
 typedef struct Socket_tag *Actual_Socket;
 
 struct SockAddr_tag {
+    int resolved;
     OSStatus error;
     OSStatus error;
-    DNSAddress address;
+    InetHostInfo hostinfo;
+    char hostname[512];
 };
 
 /* Globals */
 
 static struct {
     Actual_Socket socklist;
 };
 
 /* Globals */
 
 static struct {
     Actual_Socket socklist;
+    InetSvcRef inetsvc;
 } ot;
 
 OSErr ot_init(void)
 {
 } 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)
 }
 
 void ot_cleanup(void)
@@ -69,14 +77,17 @@ void ot_cleanup(void)
 SockAddr ot_namelookup(char *host, char **canonicalname)
 {
     SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
 SockAddr ot_namelookup(char *host, char **canonicalname)
 {
     SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
+    char *realhost;
     
     
-    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);
+    ret->error = OTInetStringToAddress(ot.inetsvc, host, &ret->hostinfo);
+    ret->resolved = TRUE;
+
+    if (ret->error == kOTNoError)
+       realhost = ret->hostinfo.name;
+    else
+       realhost = "";
+    *canonicalname = smalloc(1+strlen(realhost));
+    strcpy(*canonicalname, realhost);
     return ret;
 }
 
     return ret;
 }
 
@@ -84,14 +95,24 @@ SockAddr ot_nonamelookup(char *host)
 {
     SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
     
 {
     SockAddr ret = smalloc(sizeof(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)
 {
     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" */
 }
 
 /* I think "local" here really means "loopback" */
@@ -104,19 +125,28 @@ int ot_hostname_is_local(char *name)
 
 int ot_address_is_local(SockAddr addr)
 {
 
 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 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)
 {
 }
 
 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)
 }
 
 void ot_addr_free(SockAddr addr)
@@ -214,6 +244,7 @@ Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
     Actual_Socket ret;
     EndpointRef ep;
     OSStatus err;
     Actual_Socket ret;
     EndpointRef ep;
     OSStatus err;
+    InetAddress dest;
     TCall connectCall;
 
     ret = smalloc(sizeof(struct Socket_tag));
     TCall connectCall;
 
     ret = smalloc(sizeof(struct Socket_tag));
@@ -262,11 +293,12 @@ Socket ot_new(SockAddr addr, int port, int privport, int oobinline,
      * Connect to remote address.
      */
 
      * 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));
 
     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);
   
 
     err = OTConnect(ep, &connectCall, nil);