Revision 2c2fad64 net_helpers.c

View differences:

net_helpers.c
92 92
	static char addr[128] = "";
93 93
	char iface[IFNAMSIZ] = "";
94 94
	char line[128] = "x";
95
	struct ifaddrs *ifAddrStruct = NULL;
95
	struct ifaddrs *ifaddr, *ifa;
96
	char *ret = NULL;
96 97

  
97 98
	FILE *r = fopen("/proc/net/route", "r");
98 99
	if (!r) return NULL;
......
110 111
	}
111 112
	if (iface[0] == 0) return NULL;
112 113

  
113
	if (getifaddrs(&ifAddrStruct) < 0) return NULL;
114
	if (getifaddrs(&ifaddr) < 0) {
115
		perror("getifaddrs");
116
		return NULL;
117
	}
114 118

  
115
	while (ifAddrStruct) {
116
		if (ifAddrStruct->ifa_addr && ifAddrStruct->ifa_addr->sa_family == AF_INET && 
117
			ifAddrStruct->ifa_name && !strcmp(ifAddrStruct->ifa_name, iface))  {
118
			void *tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
119
			return inet_ntop(AF_INET, tmpAddrPtr, addr, 127);
119
	ifa = ifaddr;
120
	while (ifa) {
121
		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET && 
122
			ifa->ifa_name && !strcmp(ifa->ifa_name, iface))  {
123
			void *tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
124
			if (inet_ntop(AF_INET, tmpAddrPtr, addr, 127)) {
125
				ret = addr;
126
			} else {
127
				perror("inet_ntop error");
128
				ret = NULL;
129
			}
130
			break;
120 131
		}
121
	ifAddrStruct=ifAddrStruct->ifa_next;
132
	ifa=ifa->ifa_next;
122 133
	}
123 134

  
124
	return NULL;
135
	freeifaddrs(ifaddr);
136
	return ret;
125 137
#else
126 138
        return simple_ip_addr();
127 139
#endif

Also available in: Unified diff