13
13
#include <sys/types.h>
14
14
#include <sys/wait.h>
15
15
#include <sys/timex.h>
16
+ #include <time.h>
16
17
#include <linux/if_tun.h>
17
18
#include <arpa/inet.h>
18
19
#include <net/if.h>
@@ -222,11 +223,12 @@ struct in6_ifreq {
222
223
223
224
static const char * pseudo_random_global_id (const char * device )
224
225
{
225
- static char id [40 ];
226
- char tmp [40 ];
226
+ static char id [INET6_ADDRSTRLEN ];
227
+ char tmp [INET6_ADDRSTRLEN - 10 ];
227
228
unsigned char eui64 [16 ];
228
229
unsigned char hash [SHA_DIGEST_LENGTH ];
229
230
struct ntptimeval ntv ;
231
+ time_t tv ;
230
232
struct ifreq ifr ;
231
233
unsigned char mac [18 ];
232
234
int sockfd ;
@@ -246,16 +248,19 @@ static const char *pseudo_random_global_id(const char *device)
246
248
strncpy (ifr .ifr_name , device , sizeof (ifr .ifr_name ) - 1 );
247
249
248
250
if (ioctl (sockfd , SIOCGIFHWADDR , & ifr ) < 0 ) {
249
- int rand = open ("/dev/urandom" , O_RDONLY );
250
- if (rand == -1 ) {
251
- rand = open ("/dev/random" , O_RDONLY );
252
- }
253
- if (rand == -1 ) {
251
+ int rand = open ("/dev/urandom" , O_RDONLY );
252
+ if (rand == -1 ) {
253
+ rand = open ("/dev/random" , O_RDONLY );
254
+ }
255
+ if (rand == -1 ) {
254
256
perror ("cannot get dev hwaddr and cannot open random" );
255
257
return NULL ;
256
- }
257
- read (rand , & mac , sizeof (mac ));
258
- close (rand );
258
+ }
259
+ if (read (rand , & mac , sizeof (mac )) != sizeof (mac )) {
260
+ perror ("cannot get dev hwaddr and cannot read random" );
261
+ return NULL ;
262
+ }
263
+ close (rand );
259
264
}
260
265
else {
261
266
strncpy (mac , ifr .ifr_ifru .ifru_addr .sa_data , sizeof (mac ));
@@ -269,10 +274,10 @@ static const char *pseudo_random_global_id(const char *device)
269
274
/*
270
275
* 1) Obtain the current time of day in 64-bit NTP format [NTP].
271
276
*/
272
- if ( ntp_gettime ( & ntv ) == -1 ) {
273
- perror ( "cannot get ntp time" ) ;
274
- return NULL ;
275
- }
277
+ time ( & tv );
278
+ ntv . time . tv_sec = tv ;
279
+ // TODO: check NTP format
280
+ ntv . time . tv_usec = 0 ;
276
281
277
282
/*
278
283
* 2) Obtain an EUI-64 identifier from the system running this
@@ -320,7 +325,7 @@ static const char *pseudo_random_global_id(const char *device)
320
325
* ID to create a Local IPv6 address prefix.
321
326
*/
322
327
323
- sprintf (id , "fd00:%s::/64" , tmp );
328
+ snprintf (id , sizeof ( id ) , "fd00:%s::/64" , tmp );
324
329
325
330
return id ;
326
331
}
@@ -1032,11 +1037,11 @@ static int parse_cidr6(struct in6_addr *network, struct in6_addr *netmask,
1032
1037
1033
1038
for (int i = 0 ; i < 4 ; i ++ , prefix -= 32 ) {
1034
1039
if (prefix >= 32 ) {
1035
- netmask -> __in6_u . __u6_addr32 [i ] = 0xffffffff ;
1040
+ netmask -> s6_addr32 [i ] = 0xffffffff ;
1036
1041
} else if (prefix > 0 ) {
1037
- netmask -> __in6_u . __u6_addr32 [i ] = htonl (~((1 << (32 - prefix )) - 1 ));
1042
+ netmask -> s6_addr32 [i ] = htonl (~((1 << (32 - prefix )) - 1 ));
1038
1043
} else {
1039
- netmask -> __in6_u . __u6_addr32 [i ] = 0 ;
1044
+ netmask -> s6_addr32 [i ] = 0 ;
1040
1045
}
1041
1046
}
1042
1047
0 commit comments