@@ -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 ;
4544struct 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) {
212177struct 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*/
229194struct 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 ) {
265230struct 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 }
0 commit comments