libnet: avoid type-punning compiler warning

This commit is contained in:
Stefan Seyfried
2013-03-24 17:02:44 +01:00
parent 78d859eb35
commit a0c277eaac

View File

@@ -157,24 +157,27 @@ void netGetDefaultRoute( char *ip )
{ {
FILE *fp; FILE *fp;
char interface[9]; char interface[9];
unsigned char destination[4]; uint32_t destination;
unsigned char gateway[4]; uint32_t gw;
uint8_t gateway[4];
char zeile[256]; char zeile[256];
*ip = 0 ; *ip = 0 ;
fp = fopen("/proc/net/route","r"); fp = fopen("/proc/net/route","r");
if (fp == NULL) if (fp == NULL)
return; return;
fgets(zeile,sizeof(zeile),fp); fgets(zeile,sizeof(zeile),fp); /* skip header */
while(fgets(zeile,sizeof(zeile),fp)) while(fgets(zeile,sizeof(zeile),fp))
{ {
sscanf(zeile,"%8s %x %x",interface,(unsigned *) destination,(unsigned *) gateway); destination = 1; /* in case sscanf fails */
if (*(unsigned *)destination == 0) sscanf(zeile,"%8s %x %x", interface, &destination, &gw);
{ if (destination)
sprintf(ip,"%d.%d.%d.%d",gateway[0],gateway[1],gateway[2],gateway[3]); continue;
/* big/little endian kernels have reversed entries, so this is correct */
memcpy(gateway, &gw, 4);
sprintf(ip, "%d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]);
break; break;
} }
}
fclose(fp); fclose(fp);
} }