How to programmatically edit a routing table

I am writing a daemon running on the embedded platform, which should change the default route of the device according to which it is currently connected. How can I do this programmatically? I know that I can use the system ("route del default && route add default gateway blah"); etc., but is there a more direct way?

UPDATE: I solved my problem by opening a patch for pppd that allows the use of the replaceefaultroute parameter. The patch also contains code for programmatically modifying the routing table. It is included in this gentoo bug report http://bugs.gentoo.org/227321

+6
c linux networking
source share
3 answers

Please note that SIOCADDRT / SIOCDELRT ioctls (Unix network programming for Stevens) would be a good reference.)

+7
source share

I typed

man route 

Because I know, sometimes I can get a hint in the "see also" section on how to do the same programmatically. I saw that for the β€œroute” there was a manual page on page 4 (the route (4) was in the β€œSee also” section). So I tried

 man 4 route 

and it looked promising for executing commands that a route command can execute programmatically.

Note I just noticed that this is only for BSD systems. Here is the url with the man page http://www.manpagez.com/man/4/route/ . This may or may not be useful. Maybe you can use some materials here and add the word "linux".

+1
source share

I would recommend just using this route command. Thats that it is there, and anything else will just be a hack.

0
source share

All Articles