linux - How to get my external ip (primary interface) without internet connection, without eth0 and without any hardcoded constnts using C -


most examples uses e.g. internet connection e.g. google dns. vary bad way since not work without internet in local network.

also examples uses bad way analysing "eth0". very bad way have hardcoded strings. e.g. in pc not work since addapter name "ens160" instead "eth0":

ens160    link encap:ethernet  hwaddr 00:50:56:ba:75:ea             inet addr:192.168.0.237  bcast:192.168.1.255  mask:255.255.254.0           inet6 addr: ... scope:link           broadcast running multicast  mtu:1500  metric:1           rx packets:205280 errors:0 dropped:1 overruns:0 frame:0           tx packets:68958 errors:0 dropped:0 overruns:0 carrier:0           collisions:0 txqueuelen:1000            rx bytes:266539618 (266.5 mb)  tx bytes:4771936 (4.7 mb)  lo        link encap:local loopback             inet addr:127.0.0.1  mask:255.0.0.0           inet6 addr: ::1/128 scope:host           loopback running  mtu:65536  metric:1           rx packets:825 errors:0 dropped:0 overruns:0 frame:0           tx packets:825 errors:0 dropped:0 overruns:0 carrier:0           collisions:0 txqueuelen:1            rx bytes:74843 (74.8 kb)  tx bytes:74843 (74.8 kb) 

my address in localnetwork "192.168.0.237", ens160. default interface , ip uses in system when library creates sockets (e.g. microhttpd or other). how retrieve using standard linux libraries/functions?

thank in advance

if want discover default adapter's assigned ip address, e.g. 192.168.0.237, call getifaddrs , enumerate each address. same list of adapters ifconfig display associated information gateway , netmask. filter out ones flagged iff_loopback or don't have iff_up. sample code here.

if have more 1 such address, need check route table , find default route (the 1 target ip address of 0.0.0.0).

jselbie@ubuntu:~$ route -n kernel ip routing table destination     gateway         genmask         flags metric ref    use iface 0.0.0.0         192.168.254.2   0.0.0.0         ug    100    0        0 ens33 169.254.0.0     0.0.0.0         255.255.0.0     u     1000   0        0 ens33 192.168.254.0   0.0.0.0         255.255.255.0   u     100    0        0 ens33 

the first entry, 1 destination route of 0.0.0.0, default route , sends through ens33 adapter. if there more 1 default route, use 1 lower metric field. in case, interface of 0.0.0.0 route ens33. ip address of ens33 can discovered through call getifaddrs described above. (reference ifa_name field in enumerate list of ifaddrs.)

as programatically enumerating routing table in c, don't remember how this. quick internet search suggests can using netlink socket. consult source code route command well.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -