Socket.st
changeset 508 55d1e49c7157
parent 507 91fb3dfd91a5
child 509 eb5a2d5bab0b
--- a/Socket.st	Mon Mar 24 21:43:35 1997 +0100
+++ b/Socket.st	Mon Mar 24 23:24:35 1997 +0100
@@ -1555,24 +1555,44 @@
 # ifdef AF_INET
     if (myDomain == @symbol(inet)) {
 	/*
-	 * INET addresses - port must be a smallinteger
+	 * INET addresses - port must be a smallinteger or nil
 	 */
 	sa.in.sin_family = AF_INET;
-	if (! __isSmallInteger(portNrOrName)) {
-	    DBGPRINTF(("SOCKET: invalid port arg\n"));
-	    RETURN (false);
+
+	if (portNrOrName == nil) {
+	    sa.in.sin_port = 0;
+	} else {
+	    if (! __isSmallInteger(portNrOrName)) {
+	        DBGPRINTF(("SOCKET: invalid port arg\n"));
+	        RETURN (false);
+	    }
+	    sa.in.sin_port = htons((u_short) _intVal(portNrOrName));
 	}
-	sa.in.sin_port = htons((u_short) _intVal(portNrOrName));
+
+	/*
+	 * INET addresses - addr must be nil, integer or byteArray
+	 */
 	if (address == nil) {
 	    sa.in.sin_addr.s_addr = htonl(INADDR_ANY);
 	} else {
 	    if (__isInteger(address)) {
 		sa.in.sin_addr.s_addr = htonl(__longIntVal(address));
 	    } else {
-		printf("SOCKET: address bind not yet supported\n");
-		RETURN (false);
+	        if (__isByteArray(address)) {
+		    unsigned char *cp;
+		    int n;
+
+	            cp = __ByteArrayInstPtr(address)->ba_element;
+	            n = __byteArraySize(address);
+	            if (n > 4) n = 4;
+	    	    bcopy(cp, &sa.in.sin_addr.s_addr, n);
+		} else {
+		    printf("SOCKET: address bind not yet supported\n");
+		    RETURN (false);
+		}
 	    }
 	}
+	DBGPRINTF(("SOCKET: bind addr: %x port: %x\n", sa.in.sin_addr.s_addr, sa.in.sin_port));
 	sockaddr_size = sizeof(struct sockaddr_in);
 	ok = 1;
     }
@@ -1669,8 +1689,8 @@
             if (getsockname(sock, (struct sockaddr *)&sa, &sockaddr_size) < 0) {
                 DBGPRINTF(("SOCKET: cannot get peername\n"));
             } else {
-                p = sa.in.sin_port;
-		DBGPRINTF(("SOCKET: anon port=%d\n", p));
+		DBGPRINTF(("SOCKET: anon port=%x\n", sa.in.sin_port));
+                p = ntohs(sa.in.sin_port);
 	        __INST(port) = __MKSMALLINT(p);
             }
         }
@@ -1876,16 +1896,11 @@
     ok = 0;
     myDomain = __INST(domain);
     bzero((char *) &sa, sizeof(sa)) ;
+
 #ifdef AF_INET
     if (myDomain == @symbol(inet)) {
 	char *hostName;
 
-	if (! __isString(hostOrPathName)) {
-	    DBGPRINTF(("SOCKET: invalid hostname arg\n"));
-	    RETURN (false);
-	}
-	hostName = (char *) _stringVal(hostOrPathName);
-
 	if (! __isSmallInteger(portNrOrName)) {
 	    DBGPRINTF(("SOCKET: invalid port arg\n"));
 	    RETURN (false);
@@ -1894,22 +1909,42 @@
 	sa.in.sin_family = AF_INET;
 	sa.in.sin_port = htons((u_short) _intVal(portNrOrName)) ;
 
-	if ((addr = inet_addr(hostName)) != -1) {
-	    /* 
-	     * is Internet addr in octet notation 
-	     */
-	    bcopy(&addr, (char *) &sa.in.sin_addr, sizeof(addr)) ; /* set address */
+	if (__isByteArray(hostOrPathName)) {
+	    unsigned char *cp;
+	    int n;
+
+	    cp = __ByteArrayInstPtr(hostOrPathName)->ba_element;
+	    n = __byteArraySize(hostOrPathName);
+	    if (n > 4) n = 4;
+	    bcopy(cp, &sa.in.sin_addr.s_addr, n);
 	} else {
-	    /* 
-	     * do we know the host's address? 
-	     */
-	    if ((hp = gethostbyname(hostName)) == NULL) {
-		DBGPRINTF(("SOCKET: unknown host:%s\n", hostName));
-		RETURN (false);
+	    if (! __isString(hostOrPathName)) {
+	        DBGPRINTF(("SOCKET: invalid hostname arg\n"));
+	        RETURN (false);
 	    }
-	    bcopy(hp->h_addr, (char *) &sa.in.sin_addr, hp->h_length) ;
-	    sa.in.sin_family = hp->h_addrtype;
+
+	    hostName = (char *) _stringVal(hostOrPathName);
+
+	    if ((addr = inet_addr(hostName)) != -1) {
+	        /* 
+	         * is Internet addr in octet notation 
+	         */
+	        bcopy(&addr, (char *) &sa.in.sin_addr, sizeof(addr)) ; /* set address */
+	    } else {
+	        /* 
+	         * do we know the host's address? 
+	         */
+	        if ((hp = gethostbyname(hostName)) == NULL) {
+		    DBGPRINTF(("SOCKET: unknown host:%s\n", hostName));
+		    RETURN (false);
+	        }
+	        bcopy(hp->h_addr, (char *) &sa.in.sin_addr, hp->h_length) ;
+	        sa.in.sin_family = hp->h_addrtype;
+	    }
 	}
+
+	DBGPRINTF(("SOCKET: connect addr: %x port: %d\n", sa.in.sin_addr, sa.in.sin_port));
+
 	sockaddr_size = sizeof(struct sockaddr_in);
 	ok = 1;
     }
@@ -2680,5 +2715,5 @@
 !Socket class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.78 1997-03-24 20:43:35 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.79 1997-03-24 22:24:35 cg Exp $'
 ! !