use common byteOrder when representing IP numbers as byteArrays (always network-first)
authorClaus Gittinger <cg@exept.de>
Tue, 31 Oct 1995 18:49:10 +0100
changeset 98 5e787ba2cb31
parent 97 5b9cbd04b129
child 99 7d016cc30052
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Socket.st
--- a/Socket.st	Tue Oct 31 18:30:21 1995 +0100
+++ b/Socket.st	Tue Oct 31 18:49:10 1995 +0100
@@ -22,7 +22,7 @@
 COPYRIGHT (c) 1992 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.31 1995-10-31 16:15:11 cg Exp $
+$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.32 1995-10-31 17:49:10 cg Exp $
 '!
 
 !Socket class methodsFor:'documentation'!
@@ -43,7 +43,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.31 1995-10-31 16:15:11 cg Exp $
+$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.32 1995-10-31 17:49:10 cg Exp $
 "
 !
 
@@ -265,7 +265,7 @@
 static int __debugging__ = 0;
 
 #ifdef DEBUG
-# define DBGPRINTF(x)    printf x
+# define DBGPRINTF(x)    { if (__debugging__) printf x; }
 #else
 # define DBGPRINTF(x)    /* as nothing */
 #endif
@@ -304,7 +304,10 @@
 !Socket class methodsFor:'queries'!
 
 ipAddressOfHost:aHostName
-    "return the IP (internet-) number for a hostname"
+    "return the IP (internet-) number for a hostname as a byteArray,
+     where the network bytes come first (no matter what the cpus byteOrder is).
+     If the host is unknown, return nil.
+     This is the reverse operation to #hostWithIpAddress:."
 
     |b1 b2 b3 b4|
 
@@ -322,7 +325,7 @@
 	} else {
 	    /* do we know the host's address? */
 	    if ((hp = gethostbyname((char *) _stringVal(aHostName))) == NULL) {
-		DBGPRINTF(("unknown host\n"));
+		DBGPRINTF(("SOCKET: unknown host\n"));
 		RETURN ( nil );
 	    }
 	    bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
@@ -333,6 +336,8 @@
     if (sa.sin_family != AF_INET) {
 	RETURN ( nil );
     }
+
+    sa.sin_addr.s_addr = ntohl(sa.sin_addr.s_addr);    
     b1 = _MKSMALLINT((sa.sin_addr.s_addr >> 24) & 0xFF);
     b2 = _MKSMALLINT((sa.sin_addr.s_addr >> 16) & 0xFF);
     b3 = _MKSMALLINT((sa.sin_addr.s_addr >> 8) & 0xFF);
@@ -342,15 +347,24 @@
     ^ ByteArray with:b1 with:b2 with:b3 with:b4
 
     "
-     Socket ipAddressOfHost:'clam'
-     Socket ipAddressOfHost:'porty'
-     Socket ipAddressOfHost:'josef'
-     Socket ipAddressOfHost:'styx.com'
+     Socket ipAddressOfHost:'clam' 
+     Socket ipAddressOfHost:'porty'    
+     Socket ipAddressOfHost:'axept'    
+     Socket ipAddressOfHost:'axept'    
+     Socket ipAddressOfHost:'1.2.3.4'    
+     Socket ipAddressOfHost:'193.15.16.17'    
+     Socket ipAddressOfHost:'josef'     
+     Socket ipAddressOfHost:'styx.com'  
+     Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost') 
+     Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1') 
     "
 !
 
 hostWithIpAddress:anAddress
-    "return the hostname for an IP (internet-) address"
+    "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).
+     This is the reverse operation to #ipAddressOfHost:."
 
     |b1 b2 b3 b4|
 
@@ -368,11 +382,12 @@
 	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? */
 	hp = gethostbyaddr((char *) &sa.sin_addr.s_addr, sizeof(sa.sin_addr.s_addr), AF_INET);
 	if (hp == NULL) {
-	    DBGPRINTF(("unknown address: %d.%d.%d.%d\n", 
+	    DBGPRINTF(("SOCKET: unknown address: %d.%d.%d.%d\n", 
 		       _intVal(b1), _intVal(b2), _intVal(b3), _intVal(b4)));
 	} else {
 	    sa.sin_family = hp->h_addrtype;
@@ -394,8 +409,9 @@
      Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam') 
      Socket ipAddressOfHost:'porty'
      Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty') 
-     Socket hostWithIpAddress:#[1 2 3 4]
-    "
+     Socket hostWithIpAddress:#[1 2 3 4]  
+     Socket hostWithIpAddress:(Socket ipAddressOfHost:'1.2.3.4')  
+     "
 !
 
 portOfService:aNameOrNumber
@@ -787,21 +803,21 @@
     int dom, typ, pf, proto = 0, sock;
 
     if (! __isSymbol(domainArg)) { 
-	DBGPRINTF(("bad domain\n"));
+	DBGPRINTF(("SOCKET: bad domain\n"));
 	RETURN ( nil );
     }
     if (! __isSymbol(typeArg)) { 
-	DBGPRINTF(("bad type\n"));
+	DBGPRINTF(("SOCKET: bad type\n"));
 	RETURN ( nil );
     }
     if (protocolNumber != nil) {
-        if (!__isSmallInteger(protocolNumber)) {
-	    DBGPRINTF(("bad protocol\n"));
+	if (!__isSmallInteger(protocolNumber)) {
+	    DBGPRINTF(("SOCKET: bad protocol\n"));
 	    RETURN ( nil );
 	}
 	proto = __intVal(protocolNumber);
     }
-	
+        
 
     /*
      * get address and protocol-family
@@ -847,7 +863,7 @@
     } else
 #endif
     {
-	DBGPRINTF(("unknown domain <%s>\n", _stringVal(domainArg)));
+	DBGPRINTF(("SOCKET: unknown domain <%s>\n", _stringVal(domainArg)));
 	RETURN ( nil );
     }
 
@@ -872,17 +888,17 @@
     else
 #endif
     {
-	DBGPRINTF(("bad type <%s>\n", _stringVal(typeArg)));
+	DBGPRINTF(("SOCKET: bad type <%s>\n", _stringVal(typeArg)));
 	RETURN ( nil );
     }
 
     __BEGIN_INTERRUPTABLE__
     do {
-	DBGPRINTF(("opening socket domain=%d type=%d proto=%d\n", dom, typ, proto));
+	DBGPRINTF(("SOCKET: opening socket domain=%d type=%d proto=%d\n", dom, typ, proto));
 	sock = socket(dom, typ, proto);
 #if defined(EPROTONOSUPPORT) /* for SGI */
 	if ((proto != 0) && (sock < 0) && (errno == EPROTONOSUPPORT)) {
-	    DBGPRINTF(("retry with UNSPEC protocol\n"));
+	    DBGPRINTF(("SOCKET: retry with UNSPEC protocol\n"));
 	    proto = 0;
 	    sock = socket(dom, typ, 0);
 	}
@@ -891,7 +907,7 @@
     __END_INTERRUPTABLE__
 
     if (sock < 0) {
-	DBGPRINTF(("socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno));
+	DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno));
 	_INST(lastErrorNumber) = _MKSMALLINT(errno);
     } else {
 	/* 
@@ -899,7 +915,7 @@
 	 */
 	fp = fdopen(sock, "r+");
 	if (! fp) {
-	    DBGPRINTF(("fdopen call failed\n"));
+	    DBGPRINTF(("SOCKET: fdopen call failed\n"));
 	    _INST(lastErrorNumber) = _MKSMALLINT(errno);
 	    __BEGIN_INTERRUPTABLE__
 	    close(sock);
@@ -963,7 +979,7 @@
 	     * do we know the host's address? 
 	     */
 	    if ((hp = gethostbyname((char *) _stringVal(hostName))) == NULL) {
-		DBGPRINTF(("unknown host\n"));
+		DBGPRINTF(("SOCKET: unknown host\n"));
 		RETURN ( nil );
 	    }
 	    bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
@@ -982,7 +998,7 @@
     __END_INTERRUPTABLE__
 
     if (sock < 0) {
-	DBGPRINTF(("socket(dom=%d typ=%d proto=0) call failed errno=%d\n", sa.sin_family, SOCK_DGRAM, errno));
+	DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=0) call failed errno=%d\n", sa.sin_family, SOCK_DGRAM, errno));
 	_INST(lastErrorNumber) = _MKSMALLINT(errno);
     } else {
 	/* 
@@ -1004,7 +1020,7 @@
 	__END_INTERRUPTABLE__
 
 	if (ret < 0) {
-	    DBGPRINTF(("bind/connect call failed\n"));
+	    DBGPRINTF(("SOCKET: bind/connect call failed\n"));
 	    _INST(lastErrorNumber) = _MKSMALLINT(errno);
 	    __BEGIN_INTERRUPTABLE__
 	    close(sock) ;
@@ -1015,7 +1031,7 @@
 	     */
 	    fp = fdopen(sock, "r+");
 	    if (! fp) {
-		DBGPRINTF(("fdopen call failed\n"));
+		DBGPRINTF(("SOCKET: fdopen call failed\n"));
 		_INST(lastErrorNumber) = _MKSMALLINT(errno);
 		__BEGIN_INTERRUPTABLE__
 		close(sock);
@@ -1088,7 +1104,7 @@
 	     * do we know the host's address? 
 	     */
 	    if ((hp = gethostbyname((char *) _stringVal(hostName))) == NULL) {
-		DBGPRINTF(("unknown host\n"));
+		DBGPRINTF(("SOCKET: unknown host\n"));
 		RETURN ( nil );
 	    }
 	    bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
@@ -1106,7 +1122,7 @@
     __END_INTERRUPTABLE__
 
     if (sock < 0) {
-	DBGPRINTF(("socket(dom=%d typ=%d proto=0) call failed errno=%d\n", sa.sin_family, SOCK_STREAM, errno));
+	DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=0) call failed errno=%d\n", sa.sin_family, SOCK_STREAM, errno));
 	_INST(lastErrorNumber) = _MKSMALLINT(errno);
     } else {
 	/* 
@@ -1125,7 +1141,7 @@
 	     * should I also do this for DGRAM sockets ?
 	     */
 	    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) {
-		DBGPRINTF(("setsockopt - SO_REUSEADDR failed\n"));
+		DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n"));
 	    }
 #endif /* SO_REUSEADDR */
 	    sa.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -1136,7 +1152,7 @@
 	__END_INTERRUPTABLE__
 
 	if (ret < 0) { 
-	    DBGPRINTF(("bind/connect call failed errno=%d\n", errno));
+	    DBGPRINTF(("SOCKET: bind/connect call failed errno=%d\n", errno));
 	    _INST(lastErrorNumber) = _MKSMALLINT(errno);
 	    __BEGIN_INTERRUPTABLE__
 	    close(sock) ;
@@ -1147,7 +1163,7 @@
 	     */
 	    fp = fdopen(sock, "r+");
 	    if (! fp) {
-		DBGPRINTF(("fdopen failed\n"));
+		DBGPRINTF(("SOCKET: fdopen failed\n"));
 		_INST(lastErrorNumber) = _MKSMALLINT(errno);
 		__BEGIN_INTERRUPTABLE__
 		close(sock);
@@ -1236,7 +1252,7 @@
     extern OBJ LargeInteger;
 
     if (!__isString(_INST(domain)) && !__isSymbol(_INST(domain))) {
-	DBGPRINTF(("invalid domain arg\n"));
+	DBGPRINTF(("SOCKET: invalid domain arg\n"));
 	RETURN (false);
     }
 
@@ -1249,7 +1265,7 @@
 	 */
 	insock.sin_family = AF_INET;
 	if (! __isSmallInteger(portNrOrName)) {
-	    DBGPRINTF(("invalid port arg\n"));
+	    DBGPRINTF(("SOCKET: invalid port arg\n"));
 	    RETURN (false);
 	}
 	insock.sin_port = htons((u_short) _intVal(portNrOrName));
@@ -1300,7 +1316,7 @@
 #endif
 
     if (! ok) {
-	DBGPRINTF(("unsupported domain\n"));
+	DBGPRINTF(("SOCKET: unsupported domain\n"));
 	RETURN (false);
     }
 
@@ -1308,7 +1324,7 @@
 
 #ifdef SO_REUSEADDR
     if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) {
-	DBGPRINTF(("setsockopt - SO_REUSEADDR failed\n"));
+	DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n"));
     }
 #endif /* SO_REUSEADDR */
 
@@ -1317,7 +1333,7 @@
     } while ((ret < 0) && (errno == EINTR));
 
     if (ret < 0) {
-	DBGPRINTF(("bind failed errno=%d\n", errno));
+	DBGPRINTF(("SOCKET: bind failed errno=%d\n", errno));
 	_INST(lastErrorNumber) = _MKSMALLINT(errno);
 	RETURN (false);
     }
@@ -1354,11 +1370,11 @@
     int ok;
 
     if (! __isString(hostName)) {
-	DBGPRINTF(("invalid hostname arg\n"));
+	DBGPRINTF(("SOCKET: invalid hostname arg\n"));
 	RETURN (false);
     }
     if (!__isString(_INST(domain)) && !__isSymbol(_INST(domain))) {
-	DBGPRINTF(("invalid domain arg\n"));
+	DBGPRINTF(("SOCKET: invalid domain arg\n"));
 	RETURN (false);
     }
 
@@ -1367,7 +1383,7 @@
 #ifdef AF_INET
     if (myDomain == @symbol(inet)) {
 	if (! __isSmallInteger(portNrOrName)) {
-	    DBGPRINTF(("invalid port arg\n"));
+	    DBGPRINTF(("SOCKET: invalid port arg\n"));
 	    RETURN (false);
 	}
 
@@ -1386,7 +1402,7 @@
 	     * do we know the host's address? 
 	     */
 	    if ((hp = gethostbyname((char *) _stringVal(hostName))) == NULL) {
-		DBGPRINTF(("unknown host:%s\n", _stringVal(hostName)));
+		DBGPRINTF(("SOCKET: unknown host:%s\n", _stringVal(hostName)));
 		RETURN (false);
 	    }
 	    bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
@@ -1428,7 +1444,7 @@
 #endif
 
     if (! ok) {
-	DBGPRINTF(("unsupported domain\n"));
+	DBGPRINTF(("SOCKET: unsupported domain\n"));
 	RETURN (false);
     }
 
@@ -1445,7 +1461,7 @@
     __END_INTERRUPTABLE__
 
     if (ret < 0) { 
-	DBGPRINTF(("connect failed errno=%d\n", errno));
+	DBGPRINTF(("SOCKET: connect failed errno=%d\n", errno));
 	_INST(lastErrorNumber) = _MKSMALLINT(errno);
 	RETURN (false);
     }
@@ -1466,7 +1482,7 @@
     int ret;
 
     if (! __isSmallInteger(aNumber)) {
-	DBGPRINTF(("invalid arg\n"));
+	DBGPRINTF(("SOCKET: invalid arg\n"));
 	RETURN (false);
     }
 
@@ -1479,7 +1495,7 @@
     __END_INTERRUPTABLE__
 
     if (ret < 0) {
-	DBGPRINTF(("listen call failed errno=%d\n", errno));
+	DBGPRINTF(("SOCKET: listen call failed errno=%d\n", errno));
 	_INST(lastErrorNumber) = _MKSMALLINT(errno);
 	RETURN (false);
     }
@@ -1537,7 +1553,7 @@
     __END_INTERRUPTABLE__
 
     if (newSock < 0) {
-	DBGPRINTF(("accept call failed errno=%d\n", errno));
+	DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno));
 	_INST(lastErrorNumber) = _MKSMALLINT(errno);
 	RETURN (false);
     }
@@ -1558,7 +1574,7 @@
 		    (norder >> 8) & 0xFF,
 		    norder & 0xFF);
 	}
-	DBGPRINTF(("accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
+	DBGPRINTF(("SOCKET: accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
 	_INST(peerName) = _MKSTRING((he ? he->h_name : dotted) COMMA_CON);
     }
 #endif
@@ -1568,7 +1584,7 @@
      */
     fp = fdopen(newSock, "r+");
     if (! fp) {
-	DBGPRINTF(("fdopen call failed\n"));
+	DBGPRINTF(("SOCKET: fdopen call failed\n"));
 	_INST(lastErrorNumber) = _MKSMALLINT(errno);
 	close(newSock);
 	RETURN (false);