Default to use /proc/net/route table

This commit is contained in:
Jeremy Lakeman 2011-06-03 23:59:26 -04:00
parent f532b32efa
commit a362fc074f
3 changed files with 72 additions and 3 deletions

View File

@ -25,6 +25,70 @@ struct reachable_peer {
unsigned char tq_avg; unsigned char tq_avg;
}; };
#ifndef RTF_UP
/* Keep this in sync with /usr/src/linux/include/linux/route.h */
#define RTF_UP 0x0001 /* route usable */
#define RTF_GATEWAY 0x0002 /* destination is a gateway */
#define RTF_HOST 0x0004 /* host entry (net otherwise) */
#define RTF_REINSTATE 0x0008 /* reinstate route after tmout */
#define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */
#define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */
#define RTF_MTU 0x0040 /* specific MTU for this route */
#ifndef RTF_MSS
#define RTF_MSS RTF_MTU /* Compatibility :-( */
#endif
#define RTF_WINDOW 0x0080 /* per route window clamping */
#define RTF_IRTT 0x0100 /* Initial round trip time */
#define RTF_REJECT 0x0200 /* Reject route */
#endif
int readRoutingTable(struct in_addr peers[],int *peer_count,int peer_max){
char devname[64];
unsigned long d, g, m;
int flgs, ref, use, metric, mtu, win, ir;
fprintf(stderr,"Reading routing table\n");
FILE *fp = fopen("/proc/net/route","r");
if (!fp) return -1;
fprintf(stderr,"Skipping line\n");
if (fscanf(fp, "%*[^\n]\n") < 0)
goto ERROR;
while(1){
int r;
fprintf(stderr,"Reading next route\n");
r = fscanf(fp, "%63s%lx%lx%X%d%d%d%lx%d%d%d\n",
devname, &d, &g, &flgs, &ref, &use, &metric, &m,
&mtu, &win, &ir);
if (r != 11) {
if ((r < 0) && feof(fp)) { /* EOF with no (nonspace) chars read. */
fprintf(stderr,"eof\n");
break;
}
ERROR:
fclose(fp);
return setReason("Unable to parse routing table");
}
if (!(flgs & RTF_UP)) { /* Skip interfaces that are down. */
if (debug>1) fprintf(stderr,"Skipping down interface %s\n",devname);
continue;
}
if (m!=0xFFFFFFFF){
if (debug>1) fprintf(stderr,"Skipping non host route to %d\n",(int)d);
continue; // only include host routes, TODO pickup any networks and send them broadcasts...
}
if (*peer_count<peer_max) peers[(*peer_count)++].s_addr=d;
}
fclose(fp);
return 0;
}
int readBatmanPeerFile(char *file_path,struct in_addr peers[],int *peer_count,int peer_max) int readBatmanPeerFile(char *file_path,struct in_addr peers[],int *peer_count,int peer_max)
{ {
/* Shiny new code to read the flat file containing peer list */ /* Shiny new code to read the flat file containing peer list */

View File

@ -335,6 +335,7 @@ int clearResponses(struct response_set *responses);
int responseFromPeerP(struct response_set *responses,int peerId); int responseFromPeerP(struct response_set *responses,int peerId);
int responseFromPeer(struct response_set *responses,int peerId); int responseFromPeer(struct response_set *responses,int peerId);
int additionalPeer(char *peer); int additionalPeer(char *peer);
int readRoutingTable(struct in_addr peers[],int *peer_count,int peer_max);
int readBatmanPeerFile(char *file_path,struct in_addr peers[],int *peer_count,int peer_max); int readBatmanPeerFile(char *file_path,struct in_addr peers[],int *peer_count,int peer_max);
int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,int peer_max); int getBatmanPeerList(char *socket_path,struct in_addr peers[],int *peer_count,int peer_max);
int hlrDump(unsigned char *hlr,int hofs); int hlrDump(unsigned char *hlr,int hofs);

10
peers.c
View File

@ -20,7 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "mphlr.h" #include "mphlr.h"
char *batman_socket=NULL; char *batman_socket=NULL;
char *batman_peerfile="/data/data/org.servalproject/var/batmand.peers"; char *batman_peerfile=NULL;
int peer_count=0; int peer_count=0;
struct in_addr peers[MAX_PEERS]; struct in_addr peers[MAX_PEERS];
@ -73,8 +73,12 @@ int getPeerList()
/* XXX Add broadcast address of every running interface */ /* XXX Add broadcast address of every running interface */
/* XXX Query BATMANd for other peers */ /* XXX Query BATMANd for other peers */
if (batman_peerfile) readBatmanPeerFile(batman_peerfile,peers,&peer_count,MAX_PEERS); if (batman_peerfile)
if (batman_socket) getBatmanPeerList(batman_socket,peers,&peer_count,MAX_PEERS); readBatmanPeerFile(batman_peerfile,peers,&peer_count,MAX_PEERS);
else if (batman_socket)
getBatmanPeerList(batman_socket,peers,&peer_count,MAX_PEERS);
else
readRoutingTable(peers,&peer_count,MAX_PEERS);
return 0; return 0;
} }