This pierce of code cause Segmentation fault at Pi
Posted: Sun Jan 19, 2014 6:42 pm
I have a pierce of code which runs fine on desktop and I adopt it for the pi but it causes segmentation fault on inet_ntop function. I changed to wlan0 for pi. You can try it on the Pi. I don' t know why is it Ok on desktop but segmentation fault at pi
Code: Select all
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <errno.h>
struct ifaddrs *ifaddr, *ifa;
int family;
char *src;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
//for pi
if (0 == strcmp(ifa->ifa_name, "wlan0" ) && (family == AF_INET)) { // v4
inet_ntop(
AF_INET,
&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr,
src,
sizeof(struct sockaddr_in)
);
break;
}
}
freeifaddrs(ifaddr);