use nameLookup from SocketAddress for all addr-families
authorClaus Gittinger <cg@exept.de>
Tue, 15 Jul 2003 10:10:16 +0200
changeset 1278 7251c65b8d38
parent 1277 1a3385f21b76
child 1279 13ccd72f5f5d
use nameLookup from SocketAddress for all addr-families
Socket.st
--- a/Socket.st	Mon Jul 14 16:28:35 2003 +0200
+++ b/Socket.st	Tue Jul 15 10:10:16 2003 +0200
@@ -1409,49 +1409,16 @@
      This is the reverse operation to #hostWithAppletalkAddress:.
      WARNING: untested code - I have no appletalk to test this."
 
-%{  /* STACK: 100000 */
-#if !defined(NO_SOCKET) && defined(AF_APPLETALK)
-    struct sockaddr_at sa;
-    struct hostent *hp ;
-    long addr;
-    OBJ rslt;
-
-    sa.sat_family = -1;
-
-    if (__isString(aHostName)) {
-	bzero(&sa, sizeof(sa)) ;
-
-	/* do we know the host's address? */
-	GETHOSTBYNAME(hp, __stringVal(aHostName))
-	if (hp == NULL) {
-	    DBGPRINTF(("SOCKET: unknown appletalk host: %s\n", __stringVal(aHostName)));
-	    RETURN ( nil );
-	}
-	bcopy(hp->h_addr, (char *) &sa.sat_addr, hp->h_length) ;
-	sa.sat_family = hp->h_addrtype;
-    }
-
-    /* if the addressing family is not AF_APPLETALK, Return nil */
-    if (sa.sat_family != AF_APPLETALK) {
-	DBGPRINTF(("SOCKET: not an appletalk host\n"));
-	RETURN ( nil );
-    }
-
-    sa.sat_addr.s_net = ntohs(sa.sat_addr.s_net);    
-    rslt = __BYTEARRAY_NEW_INT(3);
-    if (rslt != nil) {
-	__ByteArrayInstPtr(rslt)->ba_element[0] = (sa.sat_addr.s_net >> 8) & 0xFF;
-	__ByteArrayInstPtr(rslt)->ba_element[1] = (sa.sat_addr.s_net) & 0xFF;
-	__ByteArrayInstPtr(rslt)->ba_element[2] = (sa.sat_addr.s_node) & 0xFF;
-       RETURN (rslt);
-    }
-#endif
-%}.
-    ^ nil
-
+    NameLookupError 
+        handle:[:ex | 
+            ^ nil
+        ]
+        do:[
+            ^ (AppletalkSocketAddress hostName:aHostName) address
+        ]
 !
 
-hostWithAppletalkAddress:anAddress
+hostWithAppletalkAddress:addrByteArray
     "return the hostname for an APPLETALK address.
      The address is supposed to be a byteArray consisting of 3 bytes,
      the network bytes come first (no matter what the local byteorder is).
@@ -1460,182 +1427,69 @@
      This is is the reverse operation to #appletalkAddressOfHost:.
      WARNING: untested code - I have no appletalk to test this."
 
-    |b1 b2 b3 b4|
-
-    b1 := anAddress at:1. "/ net-hi
-    b2 := anAddress at:2. "/ net-low
-    b3 := anAddress at:3. "/ node
-
-%{  /* STACK: 100000 */
-#if !defined(NO_SOCKET) && defined(AF_APPLETALK)
-    struct sockaddr_at sa ;
-    struct hostent *hp ;
-
-    sa.sat_family = -1;
-
-    if (__bothSmallInteger(b1, b2) && __bothSmallInteger(b3, b4)) {
-	bzero(&sa, sizeof(sa)) ;
-
-	sa.sat_addr.s_net = __intVal(b1) & 0xFF;
-	sa.sat_addr.s_net = (sa.sat_addr.s_net << 8) | (__intVal(b2) & 0xFF);
-	sa.sat_addr.s_net = htons(sa.sat_addr.s_net);    
-	sa.sat_addr.s_node = __intVal(b3) & 0xFF;
-	sa.sat_family = AF_APPLETALK;
-
-	/* do we know the host's address? */
-	GETHOSTBYADDR(hp, (char *) &sa.sat_addr, sizeof(sa.sat_addr), AF_APPLETALK);
-	if (hp != NULL) {
-	    sa.sat_family = hp->h_addrtype;
-	} else {
-	    DBGPRINTF(("SOCKET: unknown appletalk address: %d.%d.%d.%d\n", 
-		       __intVal(b1), __intVal(b2), __intVal(b3)));
-	}
-    }
-
-    /* if the addressing family is not AF_APPLETALK, Return nil */
-    if (sa.sat_family != AF_APPLETALK) {
-	DBGPRINTF(("SOCKET: not an appletalk host\n"));
-	RETURN ( nil );
-    }
-
-    if (hp != NULL) {
-	RETURN (__MKSTRING(hp->h_name));
-    }
-#endif
-    RETURN (nil);
-%}.
+    NameLookupError 
+        handle:[:ex | 
+            ^ nil
+        ]
+        do:[
+            ^ (AppletalkSocketAddress hostAddress:addrByteArray) hostName
+        ]
+
     "
-     Socket appletalkAddressOfHost:'clam'
+     Socket appletalkAddressOfHost:'yourAppleHere'
      Socket hostWithAppletalkAddress:#[1 2 3]  
      "
 !
 
-hostWithIpAddress:anAddress
+hostWithIpAddress:addrByteArray
     "return the hostname for an IP (internet-) address.
      The address is supposed to be a byteArray consisting of 4 bytes,
      the network bytes come first (no matter what the local byteorder is).
      Nil is returned for an unknown host or if its not an internet host.
      This is the reverse operation to #ipAddressOfHost:."
 
-    |b1 b2 b3 b4|
-
-    b1 := anAddress at:1.
-    b2 := anAddress at:2.
-    b3 := anAddress at:3.
-    b4 := anAddress at:4.
-
-%{  /* STACK: 100000 */
-#if !defined(NO_SOCKET) && defined(AF_INET)
-    struct sockaddr_in sa ;
-    struct hostent *hp ;
-
-    sa.sin_family = -1;
-    if (__bothSmallInteger(b1, b2) && __bothSmallInteger(b3, b4)) {
-	bzero(&sa, sizeof(sa)) ;
-	sa.sin_addr.s_addr = __intVal(b1) & 0xFF;
-	sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (__intVal(b2) & 0xFF);
-	sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (__intVal(b3) & 0xFF);
-	sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (__intVal(b4) & 0xFF);
-	sa.sin_addr.s_addr = htonl(sa.sin_addr.s_addr);    
-	sa.sin_family = AF_INET;
-	/* do we know the host's address? */
-	GETHOSTBYADDR(hp, (char *) &sa.sin_addr, sizeof(sa.sin_addr), AF_INET);
-        
-	if (hp == NULL) {
-	    DBGPRINTF(("SOCKET: unknown ip address: %d.%d.%d.%d\n", 
-		       __intVal(b1), __intVal(b2), __intVal(b3), __intVal(b4)));
-	} else {
-	    sa.sin_family = hp->h_addrtype;
-	}
-    }
-
-    /* if the addressing family is not AF_INET, Return nil */
-    if (sa.sin_family != AF_INET) {
-	RETURN ( nil );
-    }
-
-    if (hp != NULL) {
-	RETURN (__MKSTRING(hp->h_name));
-    }
-
-    /*
-     * Return it in dot-notation
-     */
-    RETURN (__MKSTRING(inet_ntoa(sa.sin_addr)));
-#else
-    RETURN (nil);
-#endif
-%}
+    NameLookupError 
+        handle:[:ex | 
+            ^ nil
+        ]
+        do:[
+            ^ (IPSocketAddress hostAddress:addrByteArray) hostName
+        ]
 
     "
-     Socket ipAddressOfHost:'clam'
+     Socket ipAddressOfHost:'clam'          
      Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam') 
-     Socket ipAddressOfHost:'porty'
-     Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty') 
-     Socket hostWithIpAddress:#[1 2 3 4]  
-     Socket hostWithIpAddress:#[127 0 0 1]  
-     Socket hostWithIpAddress:(Socket ipAddressOfHost:'1.2.3.4')  
+     Socket ipAddressOfHost:'porty'                           
+     Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty')
+     Socket hostWithIpAddress:#[1 2 3 4]                     
+     Socket hostWithIpAddress:#[127 0 0 1]                    
+     Socket hostWithIpAddress:(Socket ipAddressOfHost:'1.2.3.4')
+     Socket hostWithIpAddress:(Socket ipAddressOfHost:'www.altavista.com') 
      "
 !
 
-hostWithIpV6Address:anAddress
+hostWithIpV6Address:addrByteArray
     "return the hostname for an IPv6 (internet-) address.
      The address is supposed to be a byteArray consisting ??? bytes,
      the network bytes come first (no matter what the local byteorder is).
      Nil is returned for an unknown host or if its not an internet host.
      This is the reverse operation to #ipV6AddressOfHost:."
 
-%{  /* STACK: 100000 */
-#if !defined(NO_SOCKET) && defined(AF_INET6)
-    struct sockaddr_in6 sa ;
-    struct hostent *hp ;
-
-    sa.sin6_family = -1;
-    if (__isByteArray(anAddress)) {
-	bzero(&sa, sizeof(sa)) ;
-	if (__byteArraySize(anAddress) <= sizeof(sa.sin6_addr.s6_addr))
-	    goto bad;
-	bcopy(sa.sin6_addr.s6_addr, __ByteArrayInstPtr(anAddress)->ba_element, sizeof(sa.sin6_addr.s6_addr));
-
-	sa.sin6_family = AF_INET6;
-	/* do we know the host's address? */
-	GETHOSTBYADDR(hp, (char *) &sa.sin6_addr, sizeof(sa.sin6_addr), AF_INET6);
-	if (hp == NULL) {
-	    DBGPRINTF(("SOCKET: unknown ipv6 address: %d.%d.%d.%d...\n", 
-		       sa.sin6_addr.s6_addr[0], 
-		       sa.sin6_addr.s6_addr[1], 
-		       sa.sin6_addr.s6_addr[2],
-		       sa.sin6_addr.s6_addr[3] ));
-	} else {
-	    sa.sin6_family = hp->h_addrtype;
-	}
-    }
-    bad: ;
-    /* if the addressing family is not AF_INET6, Return nil */
-    if (sa.sin6_family != AF_INET6) {
-	DBGPRINTF(("SOCKET: not an ipv6 host\n")); 
-	RETURN ( nil );
-    }
-
-    if (hp != NULL) {
-	RETURN (__MKSTRING(hp->h_name));
-    }
-
-    /*
-     * Return it in dot-notation
-     */
-    RETURN (__MKSTRING(inet_ntoa(sa.sin6_addr)));
-#else
-    RETURN (nil);
-#endif
-%}
+    NameLookupError 
+        handle:[:ex | 
+            ^ nil
+        ]
+        do:[
+            ^ (IPv6SocketAddress hostAddress:addrByteArray) hostName
+        ]
 
     "
-     Socket ipV6AddressOfHost:'clam'
+     Socket ipV6AddressOfHost:'clam' 
      Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam') 
-     Socket ipV6AddressOfHost:'porty'
+     Socket ipV6AddressOfHost:'porty'                           
      Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty') 
      Socket hostWithIpV6Address:#[1 2 3 4 5 6 7 8 9 10 11 12 13 14]  
+     Socket ipV6AddressOfHost:'www.exept.de'                           
      "
 !
 
@@ -1660,7 +1514,7 @@
      Socket ipAddressOfHost:'193.15.16.17'
      Socket ipAddressOfHost:'josef'      
      Socket ipAddressOfHost:'styx.com' 
-     Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost')'localhost'  
+     Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost') 
      Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1') 
     "
 !
@@ -4707,5 +4561,5 @@
 !Socket class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.181 2003-07-14 11:11:15 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.182 2003-07-15 08:10:16 cg Exp $'
 ! !