6

I am writing a daemon running on an embedded platform that needs to change the default route of the device according to which interface it is connecting on at a given time. How can I do this programatically? I know I can use system("route del default && route add default gateway blah"); etc but is there a more direct way?

UPDATE: I solved my particular problem by discovering a patch to pppd that allows a replacedefaultroute option. The patch also includes code for programmatically modifying the routing table. It is included in this gentoo bug report http://bugs.gentoo.org/227321

sigjuice
  • 28,661
  • 12
  • 68
  • 93
Matthew Smith
  • 6,165
  • 6
  • 34
  • 35

3 Answers3

7

Check out SIOCADDRT/SIOCDELRT ioctls (Stevens' "Unix Network Programming" would be a good reference.)

Lance Richardson
  • 4,610
  • 23
  • 30
  • 1
    Awesome, I get either shell level access or I have to parse /proc/net/route and use ioctls! I suppose I can rip some code from other projects. Currently looking at how pppd does it. – Matthew Smith Apr 15 '09 at 02:37
1

I typed

man route

cause I know sometimes I can get a hint in the "see also" section for how to do the same thing programatically. I saw there was a man page on page 4 for "route" (route(4) was in the see also section). So tried

man 4 route

and it looked promising for doing what the route command can do programmitically.

Note I just noticed this is only for BSD systems. Here is a URL with the manpage http://www.manpagez.com/man/4/route/. This may or may not be helpful. Maybe you can google some of the stuff in here and add the word "linux".

Doug T.
  • 64,223
  • 27
  • 138
  • 202
0

I would recommend just using that route command. Thats what it's there for, and anything else will just be a hack.

Nick Whaley
  • 2,729
  • 2
  • 21
  • 28