|
Post by barryk on Feb 25, 2018 0:44:06 GMT 1
I have been looking at fast efficient way of finding the IP address assigned to an interface, for example eth0, without using utilities such as 'ifconfig', 'ip' or 'hostname -I' I found likely candidates in /proc/net, however, I found that there are a couple of ways of doing it in C, see these refs: stackoverflow.com/questions/2283494/get-ip-address-of-an-interface-on-linuxstackoverflow.com/questions/20800319/how-do-i-get-my-ip-address-in-c-on-linuxThis looks like the simplest: #include <arpa/inet.h> #include <ifaddrs.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { struct ifaddrs *addrs, *tmp;
getifaddrs(&addrs); tmp = addrs;
while (tmp) { if (tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_INET) { struct sockaddr_in *pAddr = (struct sockaddr_in *)tmp->ifa_addr; printf("%s: %s\n", tmp->ifa_name, inet_ntoa(pAddr->sin_addr)); }
tmp = tmp->ifa_next; }
freeifaddrs(addrs);
exit(EXIT_SUCCESS); }
Works great: # ./test3b lo: 127.0.0.1 eth0: 192.168.0.2 I have had a go at converting this to BaCon, but hit some road-blocks. I was wondering if someone more skilled with BaCon could translate this? Of course, I could just use USEC, but it would be nice to do it as BaCon code. getifaddrs function: man7.org/linux/man-pages/man3/getifaddrs.3.html
|
|
airr
New Member
Posts: 47
|
Post by airr on Feb 25, 2018 1:53:35 GMT 1
Hi, BarryK!
I sort of cheated by using PRAGMA INCLUDE, but this seems to work (on my MAC, at least!)
PRAGMA INCLUDE <arpa/inet.h> PRAGMA INCLUDE <ifaddrs.h> PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <stdlib.h> PRAGMA INCLUDE <unistd.h> PRAGMA INCLUDE <string.h>
DECLARE *addrs TYPE struct ifaddrs DECLARE *tmp TYPE struct ifaddrs PROTO getifaddrs PROTO freeifaddrs
getifaddrs( &addrs ) tmp = addrs
WHILE tmp IF tmp->ifa_addr AND tmp->ifa_addr->sa_family == AF_INET THEN DECLARE *pAddr TYPE struct sockaddr_in pAddr = (struct sockaddr_in *)tmp->ifa_addr name$ = tmp->ifa_name ipaddress$ = inet_ntoa(pAddr->sin_addr) PRINT name$ & ": " & ipaddress$ ENDIF tmp = tmp->ifa_next WEND
freeifaddrs(addrs)
Output:
[riveraa@Alexis ~/Projects/Bacon] $ ./netiface lo0: 127.0.0.1 en0: 10.0.1.6
|
|
airr
New Member
Posts: 47
|
Post by airr on Feb 25, 2018 4:37:31 GMT 1
I was able to successfully compile and run this with just a single PRAGMA INCLUDE:
PRAGMA INCLUDE <ifaddrs.h>
The rest already are present by default, it appears.
|
|
|
Post by vovchik on Feb 25, 2018 10:20:29 GMT 1
Dear airr (and Barry), Nice porting. I was able to shave off a couple of PROTO lines by using OPTION PARSE FALSE: OPTION PARSE FALSE PRAGMA INCLUDE <ifaddrs.h> DECLARE *addrs TYPE struct ifaddrs DECLARE *tmp TYPE struct ifaddrs DECLARE *pAddr TYPE struct sockaddr_in DECLARE name$, ipaddress$ TYPE STRING getifaddrs(&addrs) tmp = addrs
WHILE tmp IF tmp->ifa_addr AND tmp->ifa_addr->sa_family = AF_INET THEN pAddr = (struct sockaddr_in *)tmp->ifa_addr name$ = tmp->ifa_name ipaddress$ = inet_ntoa(pAddr->sin_addr) PRINT name$ & ": " & ipaddress$ END IF tmp = tmp->ifa_next WEND With kind regards, vovchik
|
|
|
Post by Pjot on Feb 25, 2018 11:54:32 GMT 1
Well done folks! Best regards Peter
|
|
|
Post by barryk on Feb 25, 2018 16:10:41 GMT 1
Yes, fantastic!
|
|