networking - How to determine (using C API) the system's default NIC? -
in linux, after establishing dhcp lease, when run command route
, last line gives me system's default route , nic.
e.g.,
kernel ip routing table destination gateway genmask flags metric ref use iface 142.157.138.0 * 255.255.254.0 u 2 0 0 wlan0 link-local * 255.255.0.0 u 1000 0 0 wlan0 default 142.157.138.1 0.0.0.0 ug 0 0 0 wlan0
is there way nic marked "default" through c api, instead of running route
, parsing output?
thanks.
(interesting in linux , darwin, windows.)
i don't know off-hand, study source. on ubuntu box, route
provided net-tools
:
~$ route /sbin/route ~$ dpkg -s /sbin/route net-tools: /sbin/route
so make directory somewhere, run apt-get source net-tools
. poked source, , route.c
looked place start. if there's no arguments passed route
, calls route_info()
given address family , options, grep
that:
~/z$ grep -r route_info * lib/net-support.h:extern int route_info(const char *afname, int flags); lib/getroute.c: *960221 {1.02} bernd eckenfels: renamed route_info getroute.c lib/getroute.c:int route_info(const char *afname, int options) netstat.c: *960204 {1.10} bernd eckenfels: aftrans, usage, new route_info, netstat.c: = route_info(afname, options); route.c: * {1.79} bernd eckenfels: route_info route.c: = route_info(afname, options);
great. lib/getroute.c
looks promising. looks it's choosing function call based of table of function pointers, sorted address family. let's find ipv4 one:
inet_aftype.rprint = inet_rprint;
so need find definition of inet_rprint
. grep
delivers: it's in lib/inet_gr.c
. interesting thing here fopen(_path_procnet_route, "r")
, defined in lib/pathnames.h
/proc/net/route
. proc_gen_fmt
defined in lib/proc.c
used make parsing output easier.
so answer, @ least on gnu/linux box, read /proc/net/route
.
Comments
Post a Comment