10 #include "wvinterface.h"
12 #include "wvstringlist.h"
14 #include <net/route.h>
20 : ifc(_ifc), ip(_net), gateway(_gate), table(_table), src()
29 return WvString(
"%s via %s %s %s metric %s%s",
38 bool WvIPRoute::operator== (
const WvIPRoute &r2)
const
40 return (ip.network() == r2.ip.network() && ip.netmask() == r2.ip.netmask()
41 && gateway == r2.gateway
42 && ifc == r2.ifc && metric == r2.metric
43 && table == r2.table);
51 WvIPRouteList::WvIPRouteList() : log(
"Route Table",
WvLog::Debug)
63 WvString ifc, table, gate, addr, mask, src;
68 WvStringList::Iter word(words);
73 WvFile kinfo(
"/proc/net/route", O_RDONLY);
75 while ((line = kinfo.
getline()) != NULL)
82 if (words.count() < 10)
86 word.next(); ifc = *word;
87 word.next(); addr = *word;
88 word.next(); gate = *word;
89 word.next(); flags = strtoul(*word, NULL, 16);
92 word.next(); metric = atoi(*word);
93 word.next(); mask = *word;
96 if (!(flags & RTF_UP))
101 uint32_t a = strtoul(addr, NULL, 16), m = strtoul(mask, NULL, 16);
102 uint32_t g = strtoul(gate, NULL, 16);
107 r =
new WvIPRoute(ifc, net, gw, metric,
"default");
113 const char *argv[] = {
"ip",
"route",
"list",
"table",
"all", NULL };
114 WvPipe defaults(argv[0], argv,
false,
true,
false);
120 ifc = gate = table =
"";
126 if (words.count() < 3)
131 if (*word ==
"broadcast" || *word ==
"local")
139 if (!word.next())
break;
142 if (word1 ==
"table")
144 if (word2 ==
"local")
152 else if (word1 ==
"dev")
154 else if (word1 ==
"via")
156 else if (word1 ==
"metric")
157 metric = word2.
num();
158 else if (word1 ==
"scope")
160 else if (word1 ==
"proto" && word2 ==
"kernel")
162 else if (word1 ==
"src")
165 log(WvLog::Debug,
"Unknown keyvalue: '%s' '%s' in (%s)\n",
178 log(WvLog::Debug2,
"No interface given for this route; skipped.\n");
194 if (!r.ip.
is_default() && r.table ==
"default")
207 Iter oi(old_kern), ni(*
this);
221 for (oi.rewind(); oi.next(); )
223 if (oi->metric == 99)
continue;
225 for (ni.rewind(); ni.next(); )
226 if (*ni == *oi)
break;
231 log(
"Del %s\n", *oi);
232 i.
delroute(oi->ip, oi->gateway, oi->metric, realtable(*oi));
237 for (ni.rewind(); ni.next(); )
239 for (oi.rewind(); oi.next(); )
240 if (*oi == *ni)
break;
245 log(
"Add %s\n", *ni);
246 i.
addroute(ni->ip, ni->gateway, ni->src, ni->metric,
257 for (i.rewind(); i.next(); )
259 if (i->ip.includes(addr))
An IP network comprises two WvIPAddr structures: an address and a netmask.
List of IP Routes currently in effect.
virtual bool isok() const
return true if the stream is actually usable right now
char * blocking_getline(time_t wait_msec, int separator='\n', int readahead=1024)
This is a version of getline() that allows you to block for more data to arrive.
void get_kernel()
automatically fill the list with appropriate data from the kernel
WvFile implements a stream connected to a file or Unix device.
Implementation of a WvPipe stream.
bool is_default() const
is this net the default gateway? (0.0.0.0/0)
Manipulate the kernel routing table in strange and interesting ways ;)
WvString is an implementation of a simple and efficient printable-string class.
void set_kernel()
automatically set the kernel to the values in the RouteList
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
char * getline(time_t wait_msec=0, char separator='\n', int readahead=1024)
Read up to one line of data from the stream and return a pointer to the internal buffer containing th...
int addroute(const WvIPNet &dest, int metric=0, WvStringParm table="default")
add a route to the given network through this interface.
A WvInterface manages a particular network interface.
An IP address is made up of a "dotted quad" – four decimal numbers in the form www....
int delroute(const WvIPNet &dest, int metric=0, WvStringParm table="default")
delete a route to the given network through this interface.
This is a WvList of WvStrings, and is a really handy way to parse strings.
void split(WvStringParm s, const char *splitchars=" \t\r\n", int limit=0)
split s and form a list ignoring splitchars (except at beginning and end) ie.
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
int num() const
Return a stdc++ string with the contents of this string.
WvIPRoute * find(const WvIPAddr &addr)
find the routing entry that matches 'addr'