C – Specify interface when adding a default gateway via SIOCADDRT

Specify interface when adding a default gateway via SIOCADDRT… here is a solution to the problem.

Specify interface when adding a default gateway via SIOCADDRT

I’M TRYING TO SET MY MACHINE’S DEFAULT GATEWAY VIA SIOCADDRT AND IOCTL(). I found the following code snippet online and it works great:

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 TODO... How do you actually specify the interface to use? When I execute this code, it seems to default to eth0 on my machine (which is my only interface). The rt_entry structure in the route.h header is defined as follows (also 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          */
};

Thanks for any help.

Solution

Obviously, you can use two fields 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 is a rt_dev field in the structure, which I think you can set to interface name:

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

Related Problems and Solutions