--- a/UnixOperatingSystem.st Tue Jan 03 19:08:26 2017 +0100
+++ b/UnixOperatingSystem.st Thu Jan 05 22:53:34 2017 +0100
@@ -7634,27 +7634,26 @@
getNetworkAddressInfo
"return a Dictionary of network interface information.
- key -> name of interface
- value -> a Set of network address
- information for the interface - a dictionaries containing the
- information about the configuration of each interface in the system.
- The dictionary keys are:
- #address
- #netmask
- #flags
- #destAddress"
+ key -> name of interface
+ value -> a Set of network address
+ information for the interface - a dictionaries containing the
+ information about the configuration of each interface in the system.
+ The dictionary keys are:
+ #address
+ #netmask
+ #flags
+ #destAddress"
|returnArray addressArray nameArray noOfIf retDictionary error retIndex|
noOfIf := 0.
%{
-#if defined(LINUX)
-# include <ifaddrs.h>
-# include <netinet/in.h>
-# include <sys/socket.h>
-# include <netpacket/packet.h>
+#include <ifaddrs.h>
+#if 0 && defined(linux)
+# include <linux/if_packet.h>
# include <net/ethernet.h> /* the L2 protocols */
+#endif
struct ifaddrs *ifap, *ifaLoop;
int n_ifa = 0;
@@ -7662,8 +7661,8 @@
OBJ t;
if (getifaddrs(&ifap) < 0) {
- error = __MKSTRING("getifaddrs() failed");
- goto out;
+ error = __MKSTRING("getifaddrs() failed");
+ goto out;
}
for (ifaLoop = ifap; ifaLoop != 0; ifaLoop = ifaLoop->ifa_next) n_ifa++;
@@ -7672,98 +7671,95 @@
returnArray = __ARRAY_NEW_INT(n_ifa*6);
if (returnArray == 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;
}
for (ifaLoop = ifap; ifaLoop != 0; ifaLoop = ifaLoop->ifa_next) {
- int family, len;
-
- if (ifaLoop->ifa_addr == 0)
- continue;
- family = ifaLoop->ifa_addr->sa_family;
- switch (family) {
- case AF_INET:
- len = sizeof(struct sockaddr_in);
- break;
- case AF_INET6:
- len = sizeof(struct sockaddr_in6);
- break;
-#if 0
- case AF_PACKET:
- len = sizeof(sockaddr_ll);
- break;
-#endif
- default:
- /* skip */
- continue;
- };
- t = __MKSTRING(ifaLoop->ifa_name);
- __arrayVal(returnArray)[retI++] = t; __STORE(returnArray, t);
- t = __MKUINT(ifaLoop->ifa_flags);
- __arrayVal(returnArray)[retI++] = t; __STORE(returnArray, t);
- t = __MKBYTEARRAY((char *)ifaLoop->ifa_addr, len);
- __arrayVal(returnArray)[retI++] = t; __STORE(returnArray, t);
- if (ifaLoop->ifa_netmask != 0) {
- t = __MKBYTEARRAY((char *)ifaLoop->ifa_netmask, len);
- __arrayVal(returnArray)[retI] = t; __STORE(returnArray, t);
- }
- retI++;
- if ((ifaLoop->ifa_flags&IFF_POINTOPOINT) && ifaLoop->ifa_dstaddr != 0) {
- t = __MKBYTEARRAY((char *)ifaLoop->ifa_dstaddr, len);
- __arrayVal(returnArray)[retI++] = t; __STORE(returnArray, t);
- } else if (retI++, ifaLoop->ifa_broadaddr != 0) {
- t = __MKBYTEARRAY((char *)ifaLoop->ifa_broadaddr, len);
- __arrayVal(returnArray)[retI] = t; __STORE(returnArray, t);
- }
- retI++;
+ int family, len;
+
+ if (ifaLoop->ifa_addr == 0)
+ continue;
+ family = ifaLoop->ifa_addr->sa_family;
+ switch (family) {
+ case AF_INET:
+ len = sizeof(struct sockaddr_in);
+ break;
+ case AF_INET6:
+ len = sizeof(struct sockaddr_in6);
+ break;
+#if 0 && defined(linux)
+ case AF_PACKET:
+ len = sizeof(struct sockaddr_ll);
+ break;
+#endif
+ default:
+ /* skip */
+ continue;
+ };
+ t = __MKSTRING(ifaLoop->ifa_name);
+ __arrayVal(returnArray)[retI++] = t; __STORE(returnArray, t);
+ t = __MKUINT(ifaLoop->ifa_flags);
+ __arrayVal(returnArray)[retI++] = t; __STORE(returnArray, t);
+ t = __MKBYTEARRAY((char *)ifaLoop->ifa_addr, len);
+ __arrayVal(returnArray)[retI++] = t; __STORE(returnArray, t);
+ if (ifaLoop->ifa_netmask != 0) {
+ t = __MKBYTEARRAY((char *)ifaLoop->ifa_netmask, len);
+ __arrayVal(returnArray)[retI] = t; __STORE(returnArray, t);
+ }
+ retI++;
+ if ((ifaLoop->ifa_flags&IFF_POINTOPOINT) && ifaLoop->ifa_dstaddr != 0) {
+ t = __MKBYTEARRAY((char *)ifaLoop->ifa_dstaddr, len);
+ __arrayVal(returnArray)[retI++] = t; __STORE(returnArray, t);
+ } else if (retI++, ifaLoop->ifa_broadaddr != 0) {
+ t = __MKBYTEARRAY((char *)ifaLoop->ifa_broadaddr, len);
+ __arrayVal(returnArray)[retI] = t; __STORE(returnArray, t);
+ }
+ retI++;
}
bad:
freeifaddrs(ifap);
-#else
- error = @symbol(notSupported);
-#endif
out:;
%}.
error notNil ifTrue:[
- self primitiveFailed:error.
- "return empty dictionary if proceeding from error"
- ^ Dictionary new.
- ].
-
- retDictionary := Dictionary new:noOfIf.
+ self primitiveFailed:error.
+ "return empty dictionary if proceeding from error"
+ ^ Dictionary new.
+ ].
+
+ retDictionary := OrderedDictionary new:noOfIf.
retIndex := 1.
1 to:noOfIf do:[:cnt|
- |name addressBytes set dict|
-
- name := returnArray at:retIndex.
- addressBytes := returnArray at:retIndex+2.
-
- addressBytes notNil ifTrue:[
- set := retDictionary at:name ifAbsentPut:[Set new].
- dict := Dictionary new:5.
- dict at:#flags put:(returnArray at:retIndex+1).
- dict at:#address put:(SocketAddress fromBytes:addressBytes).
- addressBytes := returnArray at:retIndex+3.
- addressBytes notNil ifTrue:[
- dict at:#netMask put:(SocketAddress fromBytes:addressBytes).
- ].
- addressBytes := returnArray at:retIndex+4.
- addressBytes notNil ifTrue:[
- dict at:#destAddress put:(SocketAddress fromBytes:addressBytes).
- ].
- addressBytes := returnArray at:retIndex+5.
- addressBytes notNil ifTrue:[
- dict at:#broadcastAddress put:(SocketAddress fromBytes:addressBytes).
- ].
- set add:dict.
- ].
- retIndex := retIndex + 6.
+ |name addressBytes set dict|
+
+ name := returnArray at:retIndex.
+ addressBytes := returnArray at:retIndex+2.
+
+ addressBytes notNil ifTrue:[
+ set := retDictionary at:name ifAbsentPut:[OrderedCollection new].
+ dict := Dictionary new:4.
+ dict at:#flags put:(returnArray at:retIndex+1).
+ dict at:#address put:(SocketAddress fromBytes:addressBytes).
+ addressBytes := returnArray at:retIndex+3.
+ addressBytes notNil ifTrue:[
+ dict at:#netMask put:(SocketAddress fromBytes:addressBytes).
+ ].
+ addressBytes := returnArray at:retIndex+4.
+ addressBytes notNil ifTrue:[
+ dict at:#destAddress put:(SocketAddress fromBytes:addressBytes).
+ ].
+ addressBytes := returnArray at:retIndex+5.
+ addressBytes notNil ifTrue:[
+ dict at:#broadcastAddress put:(SocketAddress fromBytes:addressBytes).
+ ].
+ set add:dict.
+ ].
+ retIndex := retIndex + 6.
].
^ retDictionary
@@ -7775,146 +7771,23 @@
getNetworkAddresses
"return a dictionary filled with
- key -> name of interface
- value -> the socket address of the interface
+ key -> name of interface
+ value -> the first socket address of the interface
for each interface"
- |addressArray nameArray noOfIf retDictionary error|
-
- noOfIf := 0.
-
-%{
-#if defined(SIOCGIFADDR) && defined(SIOCGIFCONF)
- int afinet_socket = -1;
-
- struct ifconf ifc;
- struct ifreq *ifr;
- struct ifreq ifreq;
- unsigned char buf[1024];
- int n_ifs, i, countOfIf;
- OBJ t;
-
- /*
- ** Open an INET socket
- */
-
- afinet_socket = socket(AF_INET, SOCK_DGRAM, 0);
- if (afinet_socket < 0) {
- error = __MKSTRING("Cannot open socket");
- goto bad;
- }
-
- /*
- ** Get the list of network interfaces
- */
-
- ifc.ifc_len = sizeof (buf);
- 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;
- }
-
- // get the number of interfaces in the returned structure
-
- // cg: sigh
- // on linux, the records have constant size;
- // on osx, the max of the alen of each entry and the struct size needs to be taken to advance
-
- // I am not sure, which one is the correct way to do (maybe we should invert the test to say #ifdef linux ????
- // Please check on BSD, solaris, etc... (my feeling is, the the other would behave like __osx__ does, as they are all BSDish)
- // see also 2nd loop below
-#ifndef __osx__
- n_ifs = ifc.ifc_len / sizeof (struct ifreq);
-#else
- {
- unsigned char *cp = buf;
- unsigned char *limit = buf + ifc.ifc_len;
-
- n_ifs = 0;
- while (cp < limit) {
- int sz;
-
- ifr = (struct ifreq *)cp;
- sz = IFNAMSIZ + ifr->ifr_addr.sa_len;
-
- cp += sz;
- n_ifs++;
- }
- }
-#endif
-
- nameArray = __ARRAY_NEW_INT(n_ifs);
- addressArray = __ARRAY_NEW_INT(n_ifs);
-
- if (nameArray == nil || addressArray == nil) {
- /* Creating a string wouldn/t work here */
- error = @symbol(allocationFailure);
- goto bad;
- }
-
- /*
- ** Iterate of the list of the system's netif. Find all
- ** active interfaces and their ethernet addresses
- */
- countOfIf = 0;
-
- for (i=0, ifr = ifc.ifc_req; i < n_ifs; i++) {
- /*
- ** 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;
- }
- // see (sigh) comment above
-#ifndef __osx__
- ifr++;
-#else
- {
- int sz = IFNAMSIZ + ifr->ifr_addr.sa_len;
-
- ifr = (struct ifreq *)( ((char *)ifr)+sz );
- }
-#endif
- }
-
- noOfIf = __mkSmallInteger(countOfIf);
-bad:
- if (afinet_socket >= 0)
- close(afinet_socket);
-#else
- error = @symbol(notSupported);
-#endif /* defined(SIOCGIFADDR) */
-%}.
-
- retDictionary := Dictionary new:noOfIf.
- error notNil ifTrue:[
- self primitiveFailed:error.
- "return empty dictionary if proceed from error"
- ^ retDictionary.
- ].
-
- 1 to:noOfIf do:[:cnt|
- "take the first address, if there is more than one!!"
- retDictionary at:(nameArray at:cnt) ifAbsentPut:(SocketAddress fromBytes:(addressArray at:cnt)).
- ].
-
- ^ retDictionary
-
- "
- OperatingSystem getNetworkAddresses
+ |addressInfo newDict|
+
+ addressInfo := self getNetworkAddressInfo.
+ newDict := OrderedDictionary new:addressInfo size.
+
+ addressInfo keysAndValuesDo:[:ifName :infoColl|
+ newDict at:ifName put:(infoColl first at:#address)
+ ].
+
+ ^ newDict.
+
+ "
+ self getNetworkAddresses
"
!