streamers / net_helpers.c @ 1931ff6c
History | View | Annotate | Download (3.79 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2010 Luca Abeni
|
3 |
* Copyright (c) 2010 Csaba Kiraly
|
4 |
*
|
5 |
* This is free software; see gpl-3.0.txt
|
6 |
*/
|
7 |
#include <sys/types.h> |
8 |
#include <ifaddrs.h> |
9 |
#include <sys/ioctl.h> |
10 |
#include <sys/socket.h> |
11 |
#include <netinet/in.h> |
12 |
#include <arpa/inet.h> |
13 |
#include <net/if.h> /* For struct ifreq */ |
14 |
#include <unistd.h> |
15 |
#include <stdlib.h> |
16 |
#include <stdio.h> |
17 |
#include <string.h> |
18 |
#include <netdb.h> |
19 |
|
20 |
#include "net_helpers.h" |
21 |
|
22 |
char *iface_addr(const char *iface) |
23 |
{ |
24 |
int s, res;
|
25 |
struct ifreq iface_request;
|
26 |
struct sockaddr_in *sin;
|
27 |
char buff[512]; |
28 |
|
29 |
s = socket(AF_INET, SOCK_DGRAM, 0);
|
30 |
if (s < 0) { |
31 |
return NULL; |
32 |
} |
33 |
|
34 |
memset(&iface_request, 0, sizeof(struct ifreq)); |
35 |
sin = (struct sockaddr_in *)&iface_request.ifr_addr;
|
36 |
strcpy(iface_request.ifr_name, iface); |
37 |
/* sin->sin_family = AF_INET); */
|
38 |
res = ioctl(s, SIOCGIFADDR, &iface_request); |
39 |
if (res < 0) { |
40 |
perror("ioctl(SIOCGIFADDR)");
|
41 |
close(s); |
42 |
|
43 |
return NULL; |
44 |
} |
45 |
close(s); |
46 |
|
47 |
inet_ntop(AF_INET, &sin->sin_addr, buff, sizeof(buff));
|
48 |
|
49 |
return strdup(buff);
|
50 |
} |
51 |
|
52 |
|
53 |
|
54 |
/*
|
55 |
char *default_ip_addr()
|
56 |
{
|
57 |
char hostname[256];
|
58 |
struct hostent *host_entry;
|
59 |
char *ip;
|
60 |
|
61 |
fprintf(stderr, "Trying to guess IP ...");
|
62 |
if (gethostname(hostname, sizeof hostname) < 0) {
|
63 |
fprintf(stderr, "can't get hostname\n");
|
64 |
return NULL;
|
65 |
}
|
66 |
fprintf(stderr, "hostname is: %s ...", hostname);
|
67 |
|
68 |
host_entry = gethostbyname(hostname);
|
69 |
if (! host_entry) {
|
70 |
fprintf(stderr, "can't resolve IP\n");
|
71 |
return NULL;
|
72 |
}
|
73 |
inet_ntop(AF_INET, &sin->sin_addr, buff, sizeof(buff));
|
74 |
ip = inet_ntoa(host_entry->h_addr);
|
75 |
fprintf(stderr, "IP is: %s ...", ip);
|
76 |
|
77 |
return ip;
|
78 |
}
|
79 |
*/
|
80 |
|
81 |
const char *autodetect_ip_address() { |
82 |
#ifndef __linux__
|
83 |
return NULL; |
84 |
#endif
|
85 |
|
86 |
static char addr[128] = ""; |
87 |
char iface[IFNAMSIZ] = ""; |
88 |
char line[128] = "x"; |
89 |
struct ifaddrs *ifAddrStruct = NULL; |
90 |
|
91 |
FILE *r = fopen("/proc/net/route", "r"); |
92 |
if (!r) return NULL; |
93 |
|
94 |
while (1) { |
95 |
char dst[32]; |
96 |
char ifc[IFNAMSIZ];
|
97 |
|
98 |
fgets(line, 127, r);
|
99 |
if (feof(r)) break; |
100 |
if ((sscanf(line, "%s\t%s", iface, dst) == 2) && !strcpy(dst, "00000000")) { |
101 |
strcpy(iface, ifc); |
102 |
break;
|
103 |
} |
104 |
} |
105 |
if (iface[0] == 0) return NULL; |
106 |
|
107 |
if (getifaddrs(&ifAddrStruct) < 0) return NULL; |
108 |
|
109 |
while (ifAddrStruct) {
|
110 |
if (ifAddrStruct->ifa_addr && ifAddrStruct->ifa_addr->sa_family == AF_INET &&
|
111 |
ifAddrStruct->ifa_name && !strcmp(ifAddrStruct->ifa_name, iface)) { |
112 |
void *tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; |
113 |
return inet_ntop(AF_INET, tmpAddrPtr, addr, 127); |
114 |
} |
115 |
ifAddrStruct=ifAddrStruct->ifa_next; |
116 |
} |
117 |
|
118 |
return NULL; |
119 |
} |
120 |
|
121 |
|
122 |
const char *hostname_ip_addr() |
123 |
{ |
124 |
const char *ip; |
125 |
char hostname[256]; |
126 |
struct addrinfo * result;
|
127 |
struct addrinfo * res;
|
128 |
int error;
|
129 |
|
130 |
if (gethostname(hostname, sizeof hostname) < 0) { |
131 |
fprintf(stderr, "can't get hostname\n");
|
132 |
return NULL; |
133 |
} |
134 |
fprintf(stderr, "hostname is: %s ...", hostname);
|
135 |
|
136 |
|
137 |
/* resolve the domain name into a list of addresses */
|
138 |
error = getaddrinfo(hostname, NULL, NULL, &result); |
139 |
if (error != 0) { |
140 |
fprintf(stderr, "can't resolve IP: %s\n", gai_strerror(error));
|
141 |
return NULL; |
142 |
} |
143 |
|
144 |
/* loop over all returned results and do inverse lookup */
|
145 |
for (res = result; res != NULL; res = res->ai_next) { |
146 |
ip = inet_ntoa(((struct sockaddr_in*)res->ai_addr)->sin_addr);
|
147 |
fprintf(stderr, "IP is: %s ...", ip);
|
148 |
if ( strncmp("127.", ip, 4) == 0) { |
149 |
fprintf(stderr, ":( ...");
|
150 |
ip = NULL;
|
151 |
} else {
|
152 |
break;
|
153 |
} |
154 |
} |
155 |
freeaddrinfo(result); |
156 |
|
157 |
return ip;
|
158 |
} |
159 |
|
160 |
char *default_ip_addr()
|
161 |
{ |
162 |
const char *ip = NULL; |
163 |
|
164 |
fprintf(stderr, "Trying to guess IP ...");
|
165 |
|
166 |
//ip = hostname_ip_addr();
|
167 |
|
168 |
if (!ip) {
|
169 |
ip = autodetect_ip_address(); |
170 |
} |
171 |
if (!ip) {
|
172 |
fprintf(stderr, "cannot detect IP!\n");
|
173 |
return NULL; |
174 |
} |
175 |
fprintf(stderr, "IP is: %s ...\n", ip);
|
176 |
|
177 |
return strdup(ip);
|
178 |
} |