UnixOperatingSystem.st
changeset 14377 055d0bcf41ec
parent 14376 608194431b5e
child 14435 fc486e748569
equal deleted inserted replaced
14376:608194431b5e 14377:055d0bcf41ec
  6976     "
  6976     "
  6977 !
  6977 !
  6978 
  6978 
  6979 getNetworkAddresses
  6979 getNetworkAddresses
  6980     "return a dictionary filled with
  6980     "return a dictionary filled with
  6981 	key -> name of interface
  6981         key -> name of interface
  6982 	value -> the socket adress of the interface
  6982         value -> the socket adress of the interface
  6983      for each interface"
  6983      for each interface"
  6984 
  6984 
  6985     |addressArray nameArray noOfIf retDictionary error|
  6985     |addressArray nameArray noOfIf retDictionary error|
  6986 
  6986 
  6987     noOfIf := 0.
  6987     noOfIf := 0.
  7001     ** Open an INET socket
  7001     ** Open an INET socket
  7002     */
  7002     */
  7003 
  7003 
  7004     afinet_socket = socket(AF_INET, SOCK_DGRAM, 0);
  7004     afinet_socket = socket(AF_INET, SOCK_DGRAM, 0);
  7005     if (afinet_socket < 0) {
  7005     if (afinet_socket < 0) {
  7006 	error = __MKSTRING("Cannot open socket");
  7006         error = __MKSTRING("Cannot open socket");
  7007 	goto bad;
  7007         goto bad;
  7008     }
  7008     }
  7009 
  7009 
  7010     /*
  7010     /*
  7011     ** Get the list of network interfaces
  7011     ** Get the list of network interfaces
  7012     */
  7012     */
  7013 
  7013 
  7014     ifc.ifc_len = sizeof (buf);
  7014     ifc.ifc_len = sizeof (buf);
  7015     ifc.ifc_buf = (caddr_t) buf;
  7015     ifc.ifc_buf = (caddr_t) buf;
  7016 
  7016 
  7017     if (ioctl (afinet_socket, SIOCGIFCONF, (caddr_t) &ifc) < 0) {
  7017     if (ioctl (afinet_socket, SIOCGIFCONF, (caddr_t) &ifc) < 0) {
  7018 	close(afinet_socket);
  7018         close(afinet_socket);
  7019 	error = __MKSTRING("ioctl(SIOCGIFCONF) failed");
  7019         error = __MKSTRING("ioctl(SIOCGIFCONF) failed");
  7020 	goto bad;
  7020         goto bad;
  7021     }
  7021     }
  7022 
  7022 
  7023     n_ifs = ifc.ifc_len / sizeof (struct ifreq);
  7023     n_ifs = ifc.ifc_len / sizeof (struct ifreq);
  7024 
  7024 
  7025     nameArray    = __ARRAY_NEW_INT(n_ifs);
  7025     nameArray    = __ARRAY_NEW_INT(n_ifs);
  7026     addressArray = __ARRAY_NEW_INT(n_ifs);
  7026     addressArray = __ARRAY_NEW_INT(n_ifs);
  7027 
  7027 
  7028     if (nameArray == nil || addressArray == nil) {
  7028     if (nameArray == nil || addressArray == nil) {
  7029 	/* Creating a string wouldn/t work here */
  7029         /* Creating a string wouldn/t work here */
  7030 	error = @symbol(allocationFailure);
  7030         error = @symbol(allocationFailure);
  7031 	goto bad;
  7031         goto bad;
  7032     }
  7032     }
  7033 
  7033 
  7034     /*
  7034     /*
  7035     ** Iterate of the list of the system's netif. Find all
  7035     ** Iterate of the list of the system's netif. Find all
  7036     ** active interfaces and their ethernet addresses
  7036     ** active interfaces and their ethernet addresses
  7037     */
  7037     */
  7038     countOfIf = 0;
  7038     countOfIf = 0;
  7039 
  7039 
  7040     for (i=0, ifr = ifc.ifc_req; i < n_ifs; i++, ifr++) {
  7040     for (i=0, ifr = ifc.ifc_req; i < n_ifs; i++, ifr++) {
  7041 	/*
  7041         /*
  7042 	** Get Flags for this interface
  7042         ** Get Flags for this interface
  7043 	*/
  7043         */
  7044 
  7044 
  7045 	memcpy(&ifreq, ifr, sizeof(ifreq));
  7045         memcpy(&ifreq, ifr, sizeof(ifreq));
  7046 	/*
  7046         /*
  7047 	** Get address for this interface
  7047         ** Get address for this interface
  7048 	*/
  7048         */
  7049 	memcpy(&ifreq, ifr, sizeof(ifreq));
  7049         memcpy(&ifreq, ifr, sizeof(ifreq));
  7050 	if (ioctl (afinet_socket, SIOCGIFADDR, &ifreq) >= 0) {
  7050         if (ioctl (afinet_socket, SIOCGIFADDR, &ifreq) >= 0) {
  7051 	    t = __MKBYTEARRAY((char *)&ifreq.ifr_addr, sizeof(ifreq.ifr_addr));
  7051             t = __MKBYTEARRAY((char *)&ifreq.ifr_addr, sizeof(ifreq.ifr_addr));
  7052 	    __arrayVal(addressArray)[countOfIf] = t; __STORE(addressArray, t);
  7052             __arrayVal(addressArray)[countOfIf] = t; __STORE(addressArray, t);
  7053 	    t = __MKSTRING(&ifreq.ifr_name);
  7053             t = __MKSTRING(&ifreq.ifr_name);
  7054 	    __arrayVal(nameArray)[countOfIf] = t; __STORE(nameArray, t);
  7054             __arrayVal(nameArray)[countOfIf] = t; __STORE(nameArray, t);
  7055 	    countOfIf += 1;
  7055             countOfIf += 1;
  7056 	}
  7056         }
  7057     }
  7057     }
  7058 
  7058 
  7059     noOfIf = __mkSmallInteger(countOfIf);
  7059     noOfIf = __mkSmallInteger(countOfIf);
  7060 bad:
  7060 bad:
  7061     if (afinet_socket >= 0)
  7061     if (afinet_socket >= 0)
  7062 	close(afinet_socket);
  7062         close(afinet_socket);
  7063 #else
  7063 #else
  7064     error = @symbol(notSupported);
  7064     error = @symbol(notSupported);
  7065 #endif /* defined(SIOCGIFADDR) */
  7065 #endif /* defined(SIOCGIFADDR) */
  7066 %}.
  7066 %}.
  7067 
  7067 
  7068     retDictionary := Dictionary new:noOfIf.
  7068     retDictionary := Dictionary new:noOfIf.
  7069     error notNil ifTrue:[
  7069     error notNil ifTrue:[
  7070 	self primitiveFailed:error.
  7070         self primitiveFailed:error.
  7071 	"return empty dictionary if proceed from error"
  7071         "return empty dictionary if proceed from error"
  7072 	^  retDictionary.
  7072         ^  retDictionary.
  7073     ].
  7073     ].
  7074 
  7074 
  7075     1 to:noOfIf do:[:cnt|
  7075     1 to:noOfIf do:[:cnt|
  7076 	retDictionary at:(nameArray at:cnt) put:(SocketAddress fromBytes:(addressArray at:cnt)).
  7076         "take the first address, if there is more than one!!"
       
  7077         retDictionary at:(nameArray at:cnt) ifAbsentPut:(SocketAddress fromBytes:(addressArray at:cnt)).
  7077     ].
  7078     ].
  7078 
  7079 
  7079     ^ retDictionary
  7080     ^ retDictionary
  7080 
  7081 
  7081     "
  7082     "
 13278 ! !
 13279 ! !
 13279 
 13280 
 13280 !UnixOperatingSystem class methodsFor:'documentation'!
 13281 !UnixOperatingSystem class methodsFor:'documentation'!
 13281 
 13282 
 13282 version
 13283 version
 13283     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.285 2012-10-03 14:51:45 stefan Exp $'
 13284     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.286 2012-10-08 12:34:01 stefan Exp $'
 13284 !
 13285 !
 13285 
 13286 
 13286 version_CVS
 13287 version_CVS
 13287     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.285 2012-10-03 14:51:45 stefan Exp $'
 13288     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.286 2012-10-08 12:34:01 stefan Exp $'
 13288 ! !
 13289 ! !
 13289 
 13290 
 13290 UnixOperatingSystem initialize!
 13291 UnixOperatingSystem initialize!
 13291 UnixOperatingSystem::FileDescriptorHandle initialize!
 13292 UnixOperatingSystem::FileDescriptorHandle initialize!