Fix address sizes when calling gethostbyaddr
authorStefan Vogel <sv@exept.de>
Wed, 22 Sep 1999 11:44:57 +0200
changeset 820 b6860c3660c8
parent 819 4cc15700d123
child 821 40b0b69bf4b5
Fix address sizes when calling gethostbyaddr (accept returns size including port, gethostbyaddr wants size of ip address only)
Socket.st
--- a/Socket.st	Tue Sep 21 03:53:01 1999 +0200
+++ b/Socket.st	Wed Sep 22 11:44:57 1999 +0200
@@ -1379,30 +1379,31 @@
 
     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;
-	}
+        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 );
+        RETURN ( nil );
     }
 
     if (hp != NULL) {
-	RETURN (__MKSTRING(hp->h_name));
+        RETURN (__MKSTRING(hp->h_name));
     }
 
     /*
@@ -1425,6 +1426,67 @@
      "
 !
 
+hostWithIpV6Address:anAddress
+    "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:."
+
+%{
+#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
+%}
+
+    "
+     Socket ipV6AddressOfHost:'clam'
+     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam') 
+     Socket ipV6AddressOfHost:'porty'
+     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty') 
+     Socket hostWithIpV6Address:#[1 2 3 4 5 6 7 8 9 10 11 12 13 14]  
+     "
+!
+
 ipAddressOfHost:aHostName
     "return the IP (internet-) number for a hostname as a byteArray,
      where the network bytes come first (no matter what the cpus byteOrder is).
@@ -1489,67 +1551,6 @@
     "
 !
 
-hostWithIpV6Address:anAddress
-    "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:."
-
-%{
-#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
-%}
-
-    "
-     Socket ipV6AddressOfHost:'clam'
-     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'clam') 
-     Socket ipV6AddressOfHost:'porty'
-     Socket hostWithIpV6Address:(Socket ipAddressOfHost:'porty') 
-     Socket hostWithIpV6Address:#[1 2 3 4 5 6 7 8 9 10 11 12 13 14]  
-     "
-!
-
 ipV6AddressOfHost:aHostName
     "return the IPv6 (internet-) number for a hostname as a byteArray,
      where the network bytes come first (no matter what the cpus byteOrder is).
@@ -3091,22 +3092,22 @@
      Return the true if ok; false if not.
 
      NOTICE: this method will block, if no connection is already pending.
-	     use readWait or Socket>>accept."
+             use readWait or Socket>>accept."
 
     |serverSocketFd|
 
     filePointer notNil ifTrue:[
-	^ self errorAlreadyOpen
+        ^ self errorAlreadyOpen
     ].
 
     domain := aSocket domain.
     socketType := aSocket type.
     serverSocketFd := aSocket fileDescriptor.
     serverSocketFd isNil ifTrue:[
-	^ self error:'invalid server socket'
+        ^ self error:'invalid server socket'
     ].
     (serverSocketFd isMemberOf:SmallInteger) ifFalse:[
-	^ self error:'invalid server socket'
+        ^ self error:'invalid server socket'
     ].
 %{
 #ifndef NO_SOCKET
@@ -3127,22 +3128,22 @@
 
 #ifdef AF_INET
     if (__INST(domain) == @symbol(inet)) {
-	alen0 = sizeof(sa.in);
+        alen0 = sizeof(sa.in);
     }
 #endif
 #ifdef AF_INET6
     if (__INST(domain) == @symbol(inet6)) {
-	alen0 = sizeof(sa.in6);
+        alen0 = sizeof(sa.in6);
     }
 #endif
 #ifdef AF_UNIX
     if (__INST(domain) == @symbol(unix)) {
-	alen0 = sizeof(sa.un);
+        alen0 = sizeof(sa.un);
     }
 #endif
 # ifdef AF_APPLETALK
     if (__INST(domain) == @symbol(appletalk)) {
-	alen0 = sizeof(sa.at);
+        alen0 = sizeof(sa.at);
     }
 # endif
 
@@ -3151,12 +3152,12 @@
      */
 # ifdef AF_X25
     if (__INST(domain) == @symbol(x25)) {
-	alen0 = sizeof(sa.x25);
+        alen0 = sizeof(sa.x25);
     }
 # endif
 # ifdef AF_AX25
     if (__INST(domain) == @symbol(ax25)) {
-	alen0 = sizeof(sa.ax25);
+        alen0 = sizeof(sa.ax25);
     }
 # endif
 # ifdef AF_NS
@@ -3166,12 +3167,12 @@
 # endif
 # ifdef AF_DECnet
     if (__INST(domain) == @symbol(decnet)) {
-	alen0 = sizeof(sa.dn);
+        alen0 = sizeof(sa.dn);
     }
 # endif
 # ifdef AF_SNA
     if (__INST(domain) == @symbol(sna)) {
-	alen0 = sizeof(sa.sna);
+        alen0 = sizeof(sa.sna);
     }
 # endif
 # ifdef AF_RAW
@@ -3188,7 +3189,7 @@
 # endif
 # ifdef AF_IPX
     if (__INST(domain) == @symbol(ipx)) {
-	alen0 = sizeof(sa.ipx);
+        alen0 = sizeof(sa.ipx);
     }
 # endif
 # ifdef AF_BRIDGE
@@ -3210,8 +3211,8 @@
 
     __BEGIN_INTERRUPTABLE__
     do {
-	alen = alen0;
-	newSock = accept(sock, (struct sockaddr *) &sa, &alen);
+        alen = alen0;
+        newSock = accept(sock, (struct sockaddr *) &sa, &alen);
     } while ((newSock < 0) && (errno == EINTR));
     __END_INTERRUPTABLE__
 
@@ -3220,9 +3221,9 @@
 #endif
 
     if (newSock < 0) {
-	DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno));
-	__INST(lastErrorNumber) = __MKSMALLINT(errno);
-	RETURN (false);
+        DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno));
+        __INST(lastErrorNumber) = __MKSMALLINT(errno);
+        RETURN (false);
     }
 
     /*
@@ -3230,71 +3231,71 @@
      */
 #ifdef AF_INET
     if (__INST(domain) == @symbol(inet)) {
-	GETHOSTBYADDR(he, (char *) &sa.in.sin_addr.s_addr, alen, AF_INET);
-	if (! he) {
-	    unsigned long norder;
-
-	    norder = htonl(sa.in.sin_addr.s_addr) ;
-	    sprintf(dotted, "%d.%d.%d.%d",
-		    (norder >> 24) & 0xFF,
-		    (norder >> 16) & 0xFF,
-		    (norder >> 8) & 0xFF,
-		    norder & 0xFF);
-	}
-	DBGPRINTF(("SOCKET: accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
-	__INST(peerName) = __MKSTRING((he ? he->h_name : dotted));
-	__STORESELF(peerName);
+        GETHOSTBYADDR(he, (char *) &sa.in.sin_addr, sizeof(sa.in.sin_addr), AF_INET);
+        if (! he) {
+            unsigned long norder;
+
+            norder = htonl(sa.in.sin_addr.s_addr) ;
+            sprintf(dotted, "%d.%d.%d.%d",
+                    (norder >> 24) & 0xFF,
+                    (norder >> 16) & 0xFF,
+                    (norder >> 8) & 0xFF,
+                    norder & 0xFF);
+        }
+        DBGPRINTF(("SOCKET: accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
+        __INST(peerName) = __MKSTRING((he ? he->h_name : dotted));
+        __STORESELF(peerName);
     }
 #endif
 
 #ifdef AF_INET6
     if (__INST(domain) == @symbol(inet6)) {
-	GETHOSTBYADDR(he, (char *) &sa.in6.sin6_addr.s6_addr, alen, AF_INET6);
-	if (! he) {
-	    unsigned long norder;
-
-	    /*
-	     * XXX: what is inet6's naming convention ?
-	     */
-	    norder = htonl(sa.in.sin_addr.s_addr) ;
-	    sprintf(dotted, "%d.%d.%d.%d.%d.%d...",
-		    sa.in6.sin6_addr.s6_addr[0],
-		    sa.in6.sin6_addr.s6_addr[1],
-		    sa.in6.sin6_addr.s6_addr[2],
-		    sa.in6.sin6_addr.s6_addr[3],
-		    sa.in6.sin6_addr.s6_addr[4],
-		    sa.in6.sin6_addr.s6_addr[5]);
-	}
-	DBGPRINTF(("SOCKET: accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
-	__INST(peerName) = __MKSTRING((he ? he->h_name : dotted));
-	__STORESELF(peerName);
+        GETHOSTBYADDR(he, (char *) &sa.in6.sin6_addr, sizeof(sa.in.sin6_addr), AF_INET6);
+        if (! he) {
+            unsigned long norder;
+
+            /*
+             * XXX: what is inet6's naming convention ?
+             */
+            norder = htonl(sa.in.sin_addr.s_addr) ;
+            sprintf(dotted, "%d.%d.%d.%d.%d.%d...",
+                    sa.in6.sin6_addr.s6_addr[0],
+                    sa.in6.sin6_addr.s6_addr[1],
+                    sa.in6.sin6_addr.s6_addr[2],
+                    sa.in6.sin6_addr.s6_addr[3],
+                    sa.in6.sin6_addr.s6_addr[4],
+                    sa.in6.sin6_addr.s6_addr[5]);
+        }
+        DBGPRINTF(("SOCKET: accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
+        __INST(peerName) = __MKSTRING((he ? he->h_name : dotted));
+        __STORESELF(peerName);
     }
 #endif
 
 #ifdef AF_UNIX
     if (__INST(domain) == @symbol(unix)) {
-	DBGPRINTF(("SOCKET: accepted connection on unix socket\n")) ;
-	/* nothing to be done here */
+        DBGPRINTF(("SOCKET: accepted connection on unix socket\n")) ;
+        /* nothing to be done here */
     }
 #endif
 
 #ifdef AF_APPLETALK
     if (__INST(domain) == @symbol(appletalk)) {
-	GETHOSTBYADDR(he, (char *) &sa.at.sat_addr, alen, AF_APPLETALK);
-	if (! he) {
-	    unsigned net;
-
-	    /*
-	     * XXX: what is apples naming convention ?
-	     */
-	    net = htons(sa.at.sat_addr.s_net) ;
-	    sprintf(dotted, "%d.%d",
-		    net,
-		    sa.at.sat_addr.s_node);
-	}
-	DBGPRINTF(("SOCKET: accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
-	__INST(peerName) = __MKSTRING((he ? he->h_name : dotted));
-	__STORESELF(peerName);
+        GETHOSTBYADDR(he, (char *) &sa.at.sat_addr, sizeof(sa.in.sat_addr), AF_APPLETALK);
+        if (! he) {
+            unsigned net;
+
+            /*
+             * XXX: what is apples naming convention ?
+             */
+            net = htons(sa.at.sat_addr.s_net) ;
+            sprintf(dotted, "%d.%d",
+                    net,
+                    sa.at.sat_addr.s_node);
+        }
+        DBGPRINTF(("SOCKET: accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
+        __INST(peerName) = __MKSTRING((he ? he->h_name : dotted));
+        __STORESELF(peerName);
     }
 # endif
 
@@ -3360,17 +3361,17 @@
      */
     fp = fdopen(newSock, "r+");
     if (! fp) {
-	DBGPRINTF(("SOCKET: fdopen call failed\n"));
-	__INST(lastErrorNumber) = __MKSMALLINT(errno);
-	close(newSock);
-	RETURN (false);
+        DBGPRINTF(("SOCKET: fdopen call failed\n"));
+        __INST(lastErrorNumber) = __MKSMALLINT(errno);
+        close(newSock);
+        RETURN (false);
     } else {
 #ifdef BUGGY_STDIO_LIB
-	setbuf(fp, NULL);
-	__INST(buffered) = false;
+        setbuf(fp, NULL);
+        __INST(buffered) = false;
 #endif
-	__INST(filePointer) = __MKOBJ(fp);
-	__STORESELF(filePointer);
+        __INST(filePointer) = __MKOBJ(fp);
+        __STORESELF(filePointer);
     }
 #endif
 %}.
@@ -4527,5 +4528,5 @@
 !Socket class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.132 1999-09-21 01:53:01 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.133 1999-09-22 09:44:57 stefan Exp $'
 ! !