H
hilmilho
Hi you,
I'm facing a strange problem, and I don't know what to do. Maybe
someone could help me understand this?
I'm storing IP and Netmask on the struct below.
1st I store IP an print it, its OK, like "127.0.0.1".
Then I store Netmask and it somehow messes the IP's last octet, like
"127.0.0." ...
Check out line comments below, please...
Thanks
---------------begin code--------------------------------------------
#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE ((int)(size * sizeof (struct ifreq)))
//This is the struct used to store IP and Netmask
struct ifinfo {
char * name;
char * ip;
char * netmask;
u32 ip32;
u32 netmask32;
};
....
....
....
//Stores IP on the struct
char *ip = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->ip = (char*) malloc(sizeof(*ip)+2);
strcpy( if_info->ip, ip );
if_info->ip32 = inaddrr(ifr_addr.sa_data).s_addr;
// *** Prints IP OK, like "127.0.0.1"
printf("\nIP=%s", if_info->ip);
//Stores Netmask on the struct
if (0 == ioctl(sockfd, SIOCGIFNETMASK, ifr) &&
strcmp("255.255.255.255", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {
char * netmask = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->netmask = (char*) malloc(sizeof(*netmask));
strcpy( if_info->netmask, netmask );
if_info->netmask32 = inaddrr(ifr_addr.sa_data).s_addr;
}
// *** Prints IP, without last octet, like "127.0.0."
printf("\nteste: %s=%s", ip, if_info->ip);
---------------end code--------------------------------------------
I'm facing a strange problem, and I don't know what to do. Maybe
someone could help me understand this?
I'm storing IP and Netmask on the struct below.
1st I store IP an print it, its OK, like "127.0.0.1".
Then I store Netmask and it somehow messes the IP's last octet, like
"127.0.0." ...
Check out line comments below, please...
Thanks
---------------begin code--------------------------------------------
#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE ((int)(size * sizeof (struct ifreq)))
//This is the struct used to store IP and Netmask
struct ifinfo {
char * name;
char * ip;
char * netmask;
u32 ip32;
u32 netmask32;
};
....
....
....
//Stores IP on the struct
char *ip = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->ip = (char*) malloc(sizeof(*ip)+2);
strcpy( if_info->ip, ip );
if_info->ip32 = inaddrr(ifr_addr.sa_data).s_addr;
// *** Prints IP OK, like "127.0.0.1"
printf("\nIP=%s", if_info->ip);
//Stores Netmask on the struct
if (0 == ioctl(sockfd, SIOCGIFNETMASK, ifr) &&
strcmp("255.255.255.255", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {
char * netmask = inet_ntoa(inaddrr(ifr_addr.sa_data));
if_info->netmask = (char*) malloc(sizeof(*netmask));
strcpy( if_info->netmask, netmask );
if_info->netmask32 = inaddrr(ifr_addr.sa_data).s_addr;
}
// *** Prints IP, without last octet, like "127.0.0."
printf("\nteste: %s=%s", ip, if_info->ip);
---------------end code--------------------------------------------