Specify interface when adding default gateway via SIOCADDRT

c, ioctl, linux, routes

Solution

Apparently there are two fields you can use to specify the interface:

struct ifnet *rt_ifp;

struct ifaddr *rt_ifa;

These two fields represent the “answer”, as it were, to the question posed by a route lookup; that is, they name the interface and interface address to be used in sending a packet to the destination or set of destinations which this route represents.

Update: There's an `rt_dev` field in the sturct, I think you can set it to the interface name:

char ifname[] = "eth1";
rtentry.rt_dev = ifname;

Problem

I'm trying to set my machine's default gateway via `SIOCADDRT` and `ioctl()`. I found the following snippet online that works just fine: ``` int fd = socket( PF_INET, SOCK_DGRAM, IPPROTO_IP ); struct rtentry route; memset( &route, 0, sizeof( route ) ); struct sockaddr_in *addr = (struct sockaddr_in *)&route.rt_gateway; addr->sin_family = AF_INET; addr->sin_addr.s_addr = inet_addr( "10.0.2.2" ); addr = (struct sockaddr_in*) &route.rt_dst; addr->sin_family = AF_INET; addr->sin_addr.s_addr = INADDR_ANY; addr = (struct sockaddr_in*) &route.rt_genmask; addr->sin_family = AF_INET; addr->sin_addr.s_addr = INADDR_ANY; // TODO Add the interface name to the request route.rt_flags = RTF_UP | RTF_GATEWAY; route.rt_metric = 0; int rc = ioctl( fd, SIOCADDRT, &route ); // [...] ``` Now for the `TODO` in this snippet... How do you actually specify which interface to use? When I execute this code, it seems to default to `eth0` on my machine (which is my only interface). The definition of the `rt_entry` struct in the `route.h` header is the following (Also available here): ``` /* This structure gets passed by the SIOCADDRT and SIOCDELRT calls. */ struct rtentry { unsigned long rt_pad1; struct sockaddr rt_dst; /* target address */ struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */ struct sockaddr rt_genmask; /* target network mask (IP) */ unsigned short rt_flags; short rt_pad2; unsigned long rt_pad3; void *rt_pad4; short rt_metric; /* +1 for binary compatibility! */ char *rt_dev; /* forcing the device at add */ unsigned long rt_mtu; /* per route MTU/Window */ #define rt_mss rt_mtu /* Compatibility :-( */ unsigned long rt_window; /* Window clamping */ unsigned short rt_irtt; /* Initial RTT */ }; ``` thx for any help.

Original source