changed: #getNetworkAddresses
authorStefan Vogel <sv@exept.de>
Mon, 08 Oct 2012 14:34:01 +0200
changeset 14377 055d0bcf41ec
parent 14376 608194431b5e
child 14378 3f49f1548631
changed: #getNetworkAddresses if there are multiple network addresses for an interface: the first one has priority
UnixOperatingSystem.st
--- a/UnixOperatingSystem.st	Wed Oct 03 16:51:45 2012 +0200
+++ b/UnixOperatingSystem.st	Mon Oct 08 14:34:01 2012 +0200
@@ -6978,8 +6978,8 @@
 
 getNetworkAddresses
     "return a dictionary filled with
-	key -> name of interface
-	value -> the socket adress of the interface
+        key -> name of interface
+        value -> the socket adress of the interface
      for each interface"
 
     |addressArray nameArray noOfIf retDictionary error|
@@ -7003,8 +7003,8 @@
 
     afinet_socket = socket(AF_INET, SOCK_DGRAM, 0);
     if (afinet_socket < 0) {
-	error = __MKSTRING("Cannot open socket");
-	goto bad;
+        error = __MKSTRING("Cannot open socket");
+        goto bad;
     }
 
     /*
@@ -7015,9 +7015,9 @@
     ifc.ifc_buf = (caddr_t) buf;
 
     if (ioctl (afinet_socket, SIOCGIFCONF, (caddr_t) &ifc) < 0) {
-	close(afinet_socket);
-	error = __MKSTRING("ioctl(SIOCGIFCONF) failed");
-	goto bad;
+        close(afinet_socket);
+        error = __MKSTRING("ioctl(SIOCGIFCONF) failed");
+        goto bad;
     }
 
     n_ifs = ifc.ifc_len / sizeof (struct ifreq);
@@ -7026,9 +7026,9 @@
     addressArray = __ARRAY_NEW_INT(n_ifs);
 
     if (nameArray == nil || addressArray == nil) {
-	/* Creating a string wouldn/t work here */
-	error = @symbol(allocationFailure);
-	goto bad;
+        /* Creating a string wouldn/t work here */
+        error = @symbol(allocationFailure);
+        goto bad;
     }
 
     /*
@@ -7038,28 +7038,28 @@
     countOfIf = 0;
 
     for (i=0, ifr = ifc.ifc_req; i < n_ifs; i++, ifr++) {
-	/*
-	** Get Flags for this interface
-	*/
-
-	memcpy(&ifreq, ifr, sizeof(ifreq));
-	/*
-	** Get address for this interface
-	*/
-	memcpy(&ifreq, ifr, sizeof(ifreq));
-	if (ioctl (afinet_socket, SIOCGIFADDR, &ifreq) >= 0) {
-	    t = __MKBYTEARRAY((char *)&ifreq.ifr_addr, sizeof(ifreq.ifr_addr));
-	    __arrayVal(addressArray)[countOfIf] = t; __STORE(addressArray, t);
-	    t = __MKSTRING(&ifreq.ifr_name);
-	    __arrayVal(nameArray)[countOfIf] = t; __STORE(nameArray, t);
-	    countOfIf += 1;
-	}
+        /*
+        ** Get Flags for this interface
+        */
+
+        memcpy(&ifreq, ifr, sizeof(ifreq));
+        /*
+        ** Get address for this interface
+        */
+        memcpy(&ifreq, ifr, sizeof(ifreq));
+        if (ioctl (afinet_socket, SIOCGIFADDR, &ifreq) >= 0) {
+            t = __MKBYTEARRAY((char *)&ifreq.ifr_addr, sizeof(ifreq.ifr_addr));
+            __arrayVal(addressArray)[countOfIf] = t; __STORE(addressArray, t);
+            t = __MKSTRING(&ifreq.ifr_name);
+            __arrayVal(nameArray)[countOfIf] = t; __STORE(nameArray, t);
+            countOfIf += 1;
+        }
     }
 
     noOfIf = __mkSmallInteger(countOfIf);
 bad:
     if (afinet_socket >= 0)
-	close(afinet_socket);
+        close(afinet_socket);
 #else
     error = @symbol(notSupported);
 #endif /* defined(SIOCGIFADDR) */
@@ -7067,13 +7067,14 @@
 
     retDictionary := Dictionary new:noOfIf.
     error notNil ifTrue:[
-	self primitiveFailed:error.
-	"return empty dictionary if proceed from error"
-	^  retDictionary.
+        self primitiveFailed:error.
+        "return empty dictionary if proceed from error"
+        ^  retDictionary.
     ].
 
     1 to:noOfIf do:[:cnt|
-	retDictionary at:(nameArray at:cnt) put:(SocketAddress fromBytes:(addressArray at:cnt)).
+        "take the first address, if there is more than one!!"
+        retDictionary at:(nameArray at:cnt) ifAbsentPut:(SocketAddress fromBytes:(addressArray at:cnt)).
     ].
 
     ^ retDictionary
@@ -13280,11 +13281,11 @@
 !UnixOperatingSystem class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.285 2012-10-03 14:51:45 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.286 2012-10-08 12:34:01 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.285 2012-10-03 14:51:45 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.286 2012-10-08 12:34:01 stefan Exp $'
 ! !
 
 UnixOperatingSystem initialize!