Skip to content

Commit 9defa2f

Browse files
UglymothaSietse van Zanen
andauthored
Dynamic if (#3)
* test travis-ci * Delete test * Dynamic Interfaces * revert freebsd stuff * revert freebsd stuff * clearroutes * clearroutes * style * style * unused var * Update mroute-api.c * remove unused getvifidx() * remove unused getVifIx * remove unused macros * upstrVif unsigned * upstrvif unsigned int * typo * upstrvif unsigned Co-authored-by: Sietse van Zanen <uglymotha@sanbuild.wizdom.nu>
1 parent bed8ecd commit 9defa2f

File tree

7 files changed

+266
-245
lines changed

7 files changed

+266
-245
lines changed

src/config.c

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ static void initCommonConfig(void) {
9090

9191
// aimwang: default value
9292
commonConfig.defaultInterfaceState = IF_STATE_DISABLED;
93-
commonConfig.rescanVif = 0;
93+
commonConfig.rescanVif = 0;
94+
commonConfig.rescanConf = 0;
9495
}
9596

9697
/**
@@ -222,9 +223,43 @@ int loadConfig(char *configFile) {
222223
continue;
223224
}
224225
else if(strcmp("rescanvif", token)==0) {
225-
// Got a defaultdown token...
226-
my_log(LOG_DEBUG, 0, "Config: Need detect new interface.");
227-
commonConfig.rescanVif = 1;
226+
// Got a rescanvif token...
227+
token = nextConfigToken();
228+
int intToken = atoi(token);
229+
if (intToken != 0 ) {
230+
if (intToken<10) intToken=10;
231+
my_log(LOG_DEBUG, 0, "Config: Need detect new interface every %ds.", intToken);
232+
}
233+
commonConfig.rescanVif = intToken;
234+
235+
// Read next token...
236+
token = nextConfigToken();
237+
continue;
238+
}
239+
else if(strcmp("rescanconf", token)==0) {
240+
// Got a rescanconf token...
241+
token = nextConfigToken();
242+
int intToken = atoi(token);
243+
if (intToken != 0 ) {
244+
if (intToken<10) intToken=10;
245+
my_log(LOG_DEBUG, 0, "Config: Need detect config change every %ds.", intToken);
246+
}
247+
commonConfig.rescanConf = intToken;
248+
249+
// Read next token...
250+
token = nextConfigToken();
251+
continue;
252+
}
253+
else if(strcmp("loglevel", token)==0) {
254+
// Got a loglevel token...
255+
token = nextConfigToken();
256+
int intToken = atoi(token);
257+
if (intToken < 0 || intToken > 7) {
258+
my_log(LOG_ERR, 0, "Config: Loglevel must be 0 - 7");
259+
} else {
260+
LogLevel = intToken;
261+
my_log(LOG_DEBUG, 0, "Config: LogLevel %d", LogLevel);
262+
}
228263

229264
// Read next token...
230265
token = nextConfigToken();
@@ -349,23 +384,23 @@ void createVifs(struct IfDescP *RebuildP) {
349384
switch (oDp->state) {
350385
case IF_STATE_DISABLED:
351386
switch (Dp->state) {
352-
case IF_STATE_DISABLED: { continue; }
353-
case IF_STATE_DOWNSTREAM: { oDp=NULL; break; }
354-
case IF_STATE_UPSTREAM: { oDp=NULL; break; }
387+
case IF_STATE_DISABLED: { continue; }
388+
case IF_STATE_DOWNSTREAM: { oDp=NULL; break; }
389+
case IF_STATE_UPSTREAM: { oDp=NULL; break; }
355390
}
356391
break;
357392
case IF_STATE_DOWNSTREAM:
358393
switch (Dp->state) {
359-
case IF_STATE_DISABLED: { delVIF(oDp); continue; }
360-
case IF_STATE_DOWNSTREAM: { break; }
361-
case IF_STATE_UPSTREAM: { delVIF(oDp); oDp=NULL; break; }
394+
case IF_STATE_DISABLED: { delVIF(oDp); continue; }
395+
case IF_STATE_DOWNSTREAM: { break; }
396+
case IF_STATE_UPSTREAM: { delVIF(oDp); oDp=NULL; break; }
362397
}
363398
break;
364399
case IF_STATE_UPSTREAM:
365400
switch (Dp->state) {
366-
case IF_STATE_DISABLED: { clearRoutes(); delVIF(oDp); continue; }
367-
case IF_STATE_DOWNSTREAM: { clearRoutes(); delVIF(oDp); oDp=NULL; break; }
368-
case IF_STATE_UPSTREAM: { break; }
401+
case IF_STATE_DISABLED: { clearRoutes(oDp); delVIF(oDp); continue; }
402+
case IF_STATE_DOWNSTREAM: { clearRoutes(oDp); delVIF(oDp); oDp=NULL; break; }
403+
case IF_STATE_UPSTREAM: { break; }
369404
}
370405
break;
371406
}
@@ -389,7 +424,7 @@ void createVifs(struct IfDescP *RebuildP) {
389424
upStreamIfIdx[upsvifcount++] = Ix;
390425
}
391426
}
392-
addVIF(Dp);
427+
addVIF(Dp, oDp);
393428
vifcount++;
394429
}
395430

src/ifvc.c

Lines changed: 83 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ static inline uint32_t s_addr_from_sockaddr(const struct sockaddr *addr) {
4141
return addr_in.sin_addr.s_addr;
4242
}
4343

44-
struct IfDesc IfDescVc[ MAX_IF ], *IfDescEp = IfDescVc;
4544
struct IfDescP IfDescP = { NULL, NULL, 0 };
4645

4746
/* aimwang: add for detect interface and rebuild IfVc record */
@@ -50,7 +49,7 @@ struct IfDescP IfDescP = { NULL, NULL, 0 };
5049
* For example: /etc/ppp/ip-up & ip-down can touch a file /tmp/ppp_changed
5150
* So I can check if the file exist then run me and delete the file.
5251
***************************************************/
53-
void rebuildIfVc () {
52+
void rebuildIfVc() {
5453
// Build new IfDesc Table. Keep Copy of Old.
5554
struct IfDescP OldIfDescP=IfDescP, TmpIfDescP=IfDescP;
5655
buildIfVc();
@@ -64,9 +63,7 @@ void rebuildIfVc () {
6463

6564
// Free the old IfDesc Table.
6665
if (OldIfDescP.S != NULL) {
67-
for (struct IfDesc *Dp = TmpIfDescP.S; Dp < TmpIfDescP.E; Dp++) {
68-
free(Dp->allowednets);
69-
}
66+
for (struct IfDesc *Dp = TmpIfDescP.S; Dp < TmpIfDescP.E; Dp++) free(Dp->allowednets);
7067
free(OldIfDescP.S);
7168
}
7269
}
@@ -76,130 +73,98 @@ void rebuildIfVc () {
7673
** the module will fail if they are called before the vector is build.
7774
**
7875
*/
79-
void buildIfVc(void) {
80-
struct ifreq IfVc[ sizeof( IfDescVc ) / sizeof( IfDescVc[ 0 ] ) ];
81-
struct ifreq *IfEp;
76+
void buildIfVc() {
77+
// Get the config.
8278
struct Config *config = getCommonConfig();
8379

84-
int Sock;
85-
86-
if ( (Sock = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
87-
my_log( LOG_ERR, errno, "RAW socket open" );
88-
89-
/* get If vector
90-
*/
91-
{
92-
struct ifconf IoCtlReq;
93-
94-
IoCtlReq.ifc_buf = (void *)IfVc;
95-
IoCtlReq.ifc_len = sizeof( IfVc );
80+
unsigned int NrInt=0;
81+
struct ifaddrs *IfAddrsP, *TmpIfAddrsP;
9682

97-
if ( ioctl( Sock, SIOCGIFCONF, &IoCtlReq ) < 0 )
98-
my_log( LOG_ERR, errno, "ioctl SIOCGIFCONF" );
99-
100-
IfEp = (void *)((char *)IfVc + IoCtlReq.ifc_len);
83+
if ( (getifaddrs (&IfAddrsP)) == -1 ) {
84+
my_log ( LOG_ERR, errno, "buildIfVc: getifaddr() failed, cannot enumerate interfaces" );
85+
exit (1);
10186
}
10287

103-
/* loop over interfaces and copy interface info to IfDescVc
104-
*/
105-
{
106-
struct ifreq *IfPt, *IfNext;
88+
// Check nr of interfaces in system.
89+
for (TmpIfAddrsP=IfAddrsP; TmpIfAddrsP; NrInt++) TmpIfAddrsP = TmpIfAddrsP->ifa_next;
90+
IfDescP.nrint=NrInt;
91+
my_log (LOG_DEBUG, 0 , "buildIfVc: Found %u interface(s) on system", NrInt);
92+
93+
// Allocate memory for IfDesc Table.
94+
struct IfDesc *IfDescA =(struct IfDesc*)calloc(IfDescP.nrint,sizeof(struct IfDesc));
95+
if(IfDescA == NULL) my_log(LOG_ERR, 0, "Out of memory !");
96+
IfDescP.S=IfDescA;
97+
IfDescP.E=IfDescA;
10798

99+
// loop over interfaces and copy interface info to IfDescP
100+
for (TmpIfAddrsP=IfAddrsP; TmpIfAddrsP; TmpIfAddrsP=TmpIfAddrsP->ifa_next) {
108101
// Temp keepers of interface params...
109102
uint32_t addr, subnet, mask;
103+
char FmtBu[32];
110104

111-
for ( IfPt = IfVc; IfPt < IfEp; IfPt = IfNext ) {
112-
struct ifreq IfReq;
113-
char FmtBu[ 32 ];
114-
115-
IfNext = (struct ifreq *)((char *)&IfPt->ifr_addr +
116-
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
117-
IfPt->ifr_addr.sa_len
118-
#else
119-
sizeof(struct sockaddr_in)
120-
#endif
121-
);
122-
if (IfNext < IfPt + 1)
123-
IfNext = IfPt + 1;
124-
125-
strncpy( IfDescEp->Name, IfPt->ifr_name, sizeof( IfDescEp->Name ) );
126-
127-
// Currently don't set any allowed nets...
128-
//IfDescEp->allowednets = NULL;
129-
130-
// Set the index to -1 by default.
131-
IfDescEp->index = (unsigned int)-1;
132-
133-
/* don't retrieve more info for non-IP interfaces
134-
*/
135-
if ( IfPt->ifr_addr.sa_family != AF_INET ) {
136-
IfDescEp->InAdr.s_addr = 0; /* mark as non-IP interface */
137-
IfDescEp++;
138-
continue;
139-
}
105+
// don't create IfDesc for non-IP interfaces.
106+
if (TmpIfAddrsP->ifa_addr->sa_family != AF_INET) continue;
140107

141-
// Get the interface adress...
142-
IfDescEp->InAdr.s_addr = s_addr_from_sockaddr(&IfPt->ifr_addr);
143-
addr = IfDescEp->InAdr.s_addr;
108+
// Copy the interface name.
109+
int sz = strlen(TmpIfAddrsP->ifa_name) < sizeof(IfDescP.E->Name) ? strlen(TmpIfAddrsP->ifa_name) : sizeof(IfDescP.E->Name);
110+
memcpy(IfDescP.E->Name, TmpIfAddrsP->ifa_name, sz); IfDescP.E->Name[sz]='\0';
144111

145-
memcpy( IfReq.ifr_name, IfDescEp->Name, sizeof( IfReq.ifr_name ) );
112+
// Set the index to -1 by default.
113+
IfDescP.E->index = (unsigned int)-1;
146114

147-
// Get the subnet mask...
148-
if (ioctl(Sock, SIOCGIFNETMASK, &IfReq ) < 0)
149-
my_log(LOG_ERR, errno, "ioctl SIOCGIFNETMASK for %s", IfReq.ifr_name);
150-
mask = s_addr_from_sockaddr(&IfReq.ifr_addr); // Do not use ifr_netmask as it is not available on freebsd
151-
subnet = addr & mask;
115+
// Get the interface adress...
116+
IfDescP.E->InAdr.s_addr = s_addr_from_sockaddr(TmpIfAddrsP->ifa_addr);
117+
addr = IfDescP.E->InAdr.s_addr;
152118

153-
/* get if flags
154-
**
155-
** typical flags:
156-
** lo 0x0049 -> Running, Loopback, Up
157-
** ethx 0x1043 -> Multicast, Running, Broadcast, Up
158-
** ipppx 0x0091 -> NoArp, PointToPoint, Up
159-
** grex 0x00C1 -> NoArp, Running, Up
160-
** ipipx 0x00C1 -> NoArp, Running, Up
161-
*/
162-
if ( ioctl( Sock, SIOCGIFFLAGS, &IfReq ) < 0 )
163-
my_log( LOG_ERR, errno, "ioctl SIOCGIFFLAGS" );
164-
165-
IfDescEp->Flags = IfReq.ifr_flags;
166-
167-
// aimwang: when pppx get dstaddr for use
168-
if (0x10d1 == IfDescEp->Flags)
169-
{
170-
if ( ioctl( Sock, SIOCGIFDSTADDR, &IfReq ) < 0 )
171-
my_log(LOG_ERR, errno, "ioctl SIOCGIFDSTADDR for %s", IfReq.ifr_name);
172-
addr = s_addr_from_sockaddr(&IfReq.ifr_dstaddr);
173-
subnet = addr & mask;
174-
}
175119

176-
// Insert the verified subnet as an allowed net...
177-
IfDescEp->allowednets = (struct SubnetList *)malloc(sizeof(struct SubnetList));
178-
if(IfDescEp->allowednets == NULL) my_log(LOG_ERR, 0, "Out of memory !");
179-
180-
// Create the network address for the IF..
181-
IfDescEp->allowednets->next = NULL;
182-
IfDescEp->allowednets->subnet_mask = mask;
183-
IfDescEp->allowednets->subnet_addr = subnet;
184-
185-
// Set the default params for the IF...
186-
IfDescEp->state = config->defaultInterfaceState;
187-
IfDescEp->robustness = DEFAULT_ROBUSTNESS;
188-
IfDescEp->threshold = DEFAULT_THRESHOLD; /* ttl limit */
189-
IfDescEp->ratelimit = DEFAULT_RATELIMIT;
190-
191-
// Debug log the result...
192-
my_log( LOG_DEBUG, 0, "buildIfVc: Interface %s Addr: %s, Flags: 0x%04x, Network: %s",
193-
IfDescEp->Name,
194-
fmtInAdr( FmtBu, IfDescEp->InAdr ),
195-
IfDescEp->Flags,
196-
inetFmts(subnet,mask, s1));
197-
198-
IfDescEp++;
120+
// Get the subnet mask...
121+
mask = s_addr_from_sockaddr(TmpIfAddrsP->ifa_netmask);
122+
subnet = addr & mask;
123+
124+
/* get if flags
125+
**
126+
** typical flags:
127+
** lo 0x0049 -> Running, Loopback, Up
128+
** ethx 0x1043 -> Multicast, Running, Broadcast, Up
129+
** ipppx 0x0091 -> NoArp, PointToPoint, Up
130+
** grex 0x00C1 -> NoArp, Running, Up
131+
** ipipx 0x00C1 -> NoArp, Running, Up
132+
*/
133+
IfDescP.E->Flags = TmpIfAddrsP->ifa_flags;
134+
135+
// aimwang: when pppx get dstaddr for use
136+
if (0x10d1 == IfDescP.E->Flags) {
137+
addr = s_addr_from_sockaddr(TmpIfAddrsP->ifa_dstaddr);
138+
subnet = addr & mask;
199139
}
200-
}
201140

202-
close( Sock );
141+
// Insert the verified subnet as an allowed net...
142+
IfDescP.E->allowednets = (struct SubnetList *)malloc(sizeof(struct SubnetList));
143+
if(IfDescP.E->allowednets == NULL) my_log(LOG_ERR, 0, "Out of memory !");
144+
145+
// Create the network address for the IF..
146+
IfDescP.E->allowednets->next = NULL;
147+
IfDescP.E->allowednets->subnet_mask = mask;
148+
IfDescP.E->allowednets->subnet_addr = subnet;
149+
150+
// Set the default params for the IF...
151+
IfDescP.E->state = config->defaultInterfaceState;
152+
IfDescP.E->robustness = DEFAULT_ROBUSTNESS;
153+
IfDescP.E->threshold = DEFAULT_THRESHOLD; /* ttl limit */
154+
IfDescP.E->ratelimit = DEFAULT_RATELIMIT;
155+
156+
// Debug log the result...
157+
my_log( LOG_DEBUG, 0, "buildIfVc: Interface %s Addr: %s, Flags: 0x%04x, Network: %s",
158+
IfDescP.E->Name,
159+
fmtInAdr(FmtBu, IfDescP.E->InAdr),
160+
IfDescP.E->Flags,
161+
inetFmts(subnet,mask, s1));
162+
163+
IfDescP.E++;
164+
}
165+
166+
// Free the getifadds struct.
167+
free (IfAddrsP);
203168
}
204169

205170
/*
@@ -212,7 +177,7 @@ void buildIfVc(void) {
212177
struct IfDesc *getIfByName( const char *IfName ) {
213178
struct IfDesc *Dp;
214179

215-
for ( Dp = IfDescVc; Dp < IfDescEp; Dp++ )
180+
for ( Dp = IfDescP.S; Dp < IfDescP.E; Dp++ )
216181
if ( ! strcmp( IfName, Dp->Name ) )
217182
return Dp;
218183

@@ -227,8 +192,8 @@ struct IfDesc *getIfByName( const char *IfName ) {
227192
**
228193
*/
229194
struct IfDesc *getIfByIx( unsigned Ix ) {
230-
struct IfDesc *Dp = &IfDescVc[ Ix ];
231-
return Dp < IfDescEp ? Dp : NULL;
195+
struct IfDesc *Dp = IfDescP.S+Ix;
196+
return Dp < IfDescP.E ? Dp : NULL;
232197
}
233198

234199
/**
@@ -243,7 +208,7 @@ struct IfDesc *getIfByAddress( uint32_t ipaddr ) {
243208
struct IfDesc *res = NULL;
244209
uint32_t last_subnet_mask = 0;
245210

246-
for ( Dp = IfDescVc; Dp < IfDescEp; Dp++ ) {
211+
for ( Dp = IfDescP.S; Dp < IfDescP.E; Dp++ ) {
247212
// Loop through all registered allowed nets of the VIF...
248213
for(currsubnet = Dp->allowednets; currsubnet != NULL; currsubnet = currsubnet->next) {
249214
// Check if the ip falls in under the subnet....
@@ -265,7 +230,7 @@ struct IfDesc *getIfByAddress( uint32_t ipaddr ) {
265230
struct IfDesc *getIfByVifIndex( unsigned vifindex ) {
266231
struct IfDesc *Dp;
267232
if(vifindex>0) {
268-
for ( Dp = IfDescVc; Dp < IfDescEp; Dp++ ) {
233+
for ( Dp = IfDescP.S; Dp < IfDescP.E; Dp++ ) {
269234
if(Dp->index == vifindex) {
270235
return Dp;
271236
}

src/igmp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ void sendIgmp(uint32_t src, uint32_t dst, int type, int code, uint32_t group, in
329329
IP_HEADER_RAOPT_LEN + IGMP_MINLEN + datalen, 0,
330330
(struct sockaddr *)&sdst, sizeof(sdst)) < 0) {
331331
if (errno == ENETDOWN)
332-
my_log(LOG_ERR, errno, "Sender VIF was down.");
332+
my_log(LOG_NOTICE, errno, "Sender VIF was down.");
333333
else
334334
my_log(LOG_INFO, errno,
335335
"sendto to %s on %s",

0 commit comments

Comments
 (0)