File: | core/network.c |
Location: | line 134, column 9 |
Description: | Value stored to 'family' is never read |
1 | /* |
2 | network.c : Network stuff |
3 | |
4 | Copyright (C) 1999-2000 Timo Sirainen |
5 | |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
10 | |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU General Public License along |
17 | with this program; if not, write to the Free Software Foundation, Inc., |
18 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 | */ |
20 | |
21 | #include "module.h" |
22 | #include "network.h" |
23 | |
24 | #include <sys/un.h> |
25 | |
26 | #ifndef INADDR_NONE0xffffffff |
27 | # define INADDR_NONE0xffffffff INADDR_BROADCAST(u_int32_t)0xffffffff |
28 | #endif |
29 | |
30 | union sockaddr_union { |
31 | struct sockaddr sa; |
32 | struct sockaddr_in sin; |
33 | #ifdef HAVE_IPV61 |
34 | struct sockaddr_in6 sin6; |
35 | #endif |
36 | }; |
37 | |
38 | #ifdef HAVE_IPV61 |
39 | # define SIZEOF_SOCKADDR(so)((so).sa.sa_family == 28 ? sizeof(so.sin6) : sizeof(so.sin)) ((so).sa.sa_family == AF_INET628 ? \ |
40 | sizeof(so.sin6) : sizeof(so.sin)) |
41 | #else |
42 | # define SIZEOF_SOCKADDR(so)((so).sa.sa_family == 28 ? sizeof(so.sin6) : sizeof(so.sin)) (sizeof(so.sin)) |
43 | #endif |
44 | |
45 | #ifdef WIN32 |
46 | # define g_io_channel_new(handle)g_io_channel_unix_new(handle) g_io_channel_win32_new_stream_socket(handle) |
47 | #else |
48 | # define g_io_channel_new(handle)g_io_channel_unix_new(handle) g_io_channel_unix_new(handle) |
49 | #endif |
50 | |
51 | /* Cygwin need this, don't know others.. */ |
52 | /*#define BLOCKING_SOCKETS 1*/ |
53 | |
54 | IPADDR ip4_any = { |
55 | AF_INET2, |
56 | { INADDR_ANY(u_int32_t)0x00000000 } |
57 | }; |
58 | |
59 | int net_ip_compare(IPADDR *ip1, IPADDR *ip2) |
60 | { |
61 | if (ip1->family != ip2->family) |
62 | return 0; |
63 | |
64 | #ifdef HAVE_IPV61 |
65 | if (ip1->family == AF_INET628) |
66 | return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0; |
67 | #endif |
68 | |
69 | return memcmp(&ip1->ip, &ip2->ip, 4) == 0; |
70 | } |
71 | |
72 | |
73 | static void sin_set_ip(union sockaddr_union *so, const IPADDR *ip) |
74 | { |
75 | if (ip == NULL((void *)0)) { |
76 | #ifdef HAVE_IPV61 |
77 | so->sin6.sin6_family = AF_INET628; |
78 | so->sin6.sin6_addr = in6addr_any; |
79 | #else |
80 | so->sin.sin_family = AF_INET2; |
81 | so->sin.sin_addr.s_addr = INADDR_ANY(u_int32_t)0x00000000; |
82 | #endif |
83 | return; |
84 | } |
85 | |
86 | so->sin.sin_family = ip->family; |
87 | #ifdef HAVE_IPV61 |
88 | if (ip->family == AF_INET628) |
89 | memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip)); |
90 | else |
91 | #endif |
92 | memcpy(&so->sin.sin_addr, &ip->ip, 4); |
93 | } |
94 | |
95 | void sin_get_ip(const union sockaddr_union *so, IPADDR *ip) |
96 | { |
97 | ip->family = so->sin.sin_family; |
98 | |
99 | #ifdef HAVE_IPV61 |
100 | if (ip->family == AF_INET628) |
101 | memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip)); |
102 | else |
103 | #endif |
104 | memcpy(&ip->ip, &so->sin.sin_addr, 4); |
105 | } |
106 | |
107 | static void sin_set_port(union sockaddr_union *so, int port) |
108 | { |
109 | #ifdef HAVE_IPV61 |
110 | if (so->sin.sin_family == AF_INET628) |
111 | so->sin6.sin6_port = htons((unsigned short)port)__bswap16((unsigned short)port); |
112 | else |
113 | #endif |
114 | so->sin.sin_port = htons((unsigned short)port)__bswap16((unsigned short)port); |
115 | } |
116 | |
117 | static int sin_get_port(union sockaddr_union *so) |
118 | { |
119 | #ifdef HAVE_IPV61 |
120 | if (so->sin.sin_family == AF_INET628) |
121 | return ntohs(so->sin6.sin6_port)__bswap16(so->sin6.sin6_port); |
122 | #endif |
123 | return ntohs(so->sin.sin_port)__bswap16(so->sin.sin_port); |
124 | } |
125 | |
126 | /* Connect to socket */ |
127 | GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip) |
128 | { |
129 | IPADDR ip4, ip6, *ip; |
130 | int family; |
131 | |
132 | g_return_val_if_fail(addr != NULL, NULL)do{ if (addr != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "addr != NULL"); return ( ((void *)0)); }; }while (0); |
133 | |
134 | family = my_ip == NULL((void *)0) ? 0 : my_ip->family; |
Value stored to 'family' is never read | |
135 | if (net_gethostbyname(addr, &ip4, &ip6) == -1) |
136 | return NULL((void *)0); |
137 | |
138 | if (my_ip == NULL((void *)0)) { |
139 | /* prefer IPv4 addresses */ |
140 | ip = ip4.family != 0 ? &ip4 : &ip6; |
141 | } else if (IPADDR_IS_V6(my_ip)((my_ip)->family != 2)) { |
142 | /* my_ip is IPv6 address, use it if possible */ |
143 | if (ip6.family != 0) |
144 | ip = &ip6; |
145 | else { |
146 | my_ip = NULL((void *)0); |
147 | ip = &ip4; |
148 | } |
149 | } else { |
150 | /* my_ip is IPv4 address, use it if possible */ |
151 | if (ip4.family != 0) |
152 | ip = &ip4; |
153 | else { |
154 | my_ip = NULL((void *)0); |
155 | ip = &ip6; |
156 | } |
157 | } |
158 | |
159 | return net_connect_ip(ip, port, my_ip); |
160 | } |
161 | |
162 | /* Connect to socket with ip address */ |
163 | GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip) |
164 | { |
165 | union sockaddr_union so; |
166 | int handle, ret, opt = 1; |
167 | |
168 | if (my_ip != NULL((void *)0) && ip->family != my_ip->family) { |
169 | g_warning("net_connect_ip(): ip->family != my_ip->family")g_log (((gchar*) 0), G_LOG_LEVEL_WARNING, "net_connect_ip(): ip->family != my_ip->family" ); |
170 | my_ip = NULL((void *)0); |
171 | } |
172 | |
173 | /* create the socket */ |
174 | memset(&so, 0, sizeof(so)); |
175 | so.sin.sin_family = ip->family; |
176 | handle = socket(ip->family, SOCK_STREAM1, 0); |
177 | |
178 | if (handle == -1) |
179 | return NULL((void *)0); |
180 | |
181 | /* set socket options */ |
182 | #ifndef WIN32 |
183 | fcntl(handle, F_SETFL4, O_NONBLOCK0x0004); |
184 | #endif |
185 | setsockopt(handle, SOL_SOCKET0xffff, SO_REUSEADDR0x0004, &opt, sizeof(opt)); |
186 | setsockopt(handle, SOL_SOCKET0xffff, SO_KEEPALIVE0x0008, &opt, sizeof(opt)); |
187 | |
188 | /* set our own address */ |
189 | if (my_ip != NULL((void *)0)) { |
190 | sin_set_ip(&so, my_ip); |
191 | if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)((so).sa.sa_family == 28 ? sizeof(so.sin6) : sizeof(so.sin))) < 0) { |
192 | int old_errno = errno(* __error()); |
193 | |
194 | close(handle); |
195 | errno(* __error()) = old_errno; |
196 | return NULL((void *)0); |
197 | } |
198 | } |
199 | |
200 | /* connect */ |
201 | sin_set_ip(&so, ip); |
202 | sin_set_port(&so, port); |
203 | ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so)((so).sa.sa_family == 28 ? sizeof(so.sin6) : sizeof(so.sin))); |
204 | |
205 | #ifndef WIN32 |
206 | if (ret < 0 && errno(* __error()) != EINPROGRESS36) |
207 | #else |
208 | if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) |
209 | #endif |
210 | { |
211 | int old_errno = errno(* __error()); |
212 | close(handle); |
213 | errno(* __error()) = old_errno; |
214 | return NULL((void *)0); |
215 | } |
216 | |
217 | return g_io_channel_new(handle)g_io_channel_unix_new(handle); |
218 | } |
219 | |
220 | /* Connect to named UNIX socket */ |
221 | GIOChannel *net_connect_unix(const char *path) |
222 | { |
223 | struct sockaddr_un sa; |
224 | int handle, ret; |
225 | |
226 | /* create the socket */ |
227 | handle = socket(PF_UNIX1, SOCK_STREAM1, 0); |
228 | if (handle == -1) |
229 | return NULL((void *)0); |
230 | |
231 | /* set socket options */ |
232 | #ifndef WIN32 |
233 | fcntl(handle, F_SETFL4, O_NONBLOCK0x0004); |
234 | #endif |
235 | |
236 | /* connect */ |
237 | memset(&sa, 0, sizeof(sa)); |
238 | sa.sun_family = AF_UNIX1; |
239 | strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1); |
240 | sa.sun_path[sizeof(sa.sun_path)-1] = '\0'; |
241 | |
242 | ret = connect(handle, (struct sockaddr *) &sa, sizeof(sa)); |
243 | if (ret < 0 && errno(* __error()) != EINPROGRESS36) { |
244 | int old_errno = errno(* __error()); |
245 | close(handle); |
246 | errno(* __error()) = old_errno; |
247 | return NULL((void *)0); |
248 | } |
249 | |
250 | return g_io_channel_new(handle)g_io_channel_unix_new(handle); |
251 | } |
252 | |
253 | /* Disconnect socket */ |
254 | void net_disconnect(GIOChannel *handle) |
255 | { |
256 | g_return_if_fail(handle != NULL)do{ if (handle != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "handle != NULL"); return ; }; }while (0); |
257 | |
258 | g_io_channel_close(handle); |
259 | g_io_channel_unref(handle); |
260 | } |
261 | |
262 | /* Listen for connections on a socket. if `my_ip' is NULL, listen in any |
263 | address. */ |
264 | GIOChannel *net_listen(IPADDR *my_ip, int *port) |
265 | { |
266 | union sockaddr_union so; |
267 | int ret, handle, opt = 1; |
268 | socklen_t len; |
269 | |
270 | g_return_val_if_fail(port != NULL, NULL)do{ if (port != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "port != NULL"); return ( ((void *)0)); }; }while (0); |
271 | |
272 | memset(&so, 0, sizeof(so)); |
273 | sin_set_ip(&so, my_ip); |
274 | sin_set_port(&so, *port); |
275 | |
276 | /* create the socket */ |
277 | handle = socket(so.sin.sin_family, SOCK_STREAM1, 0); |
278 | #ifdef HAVE_IPV61 |
279 | if (handle == -1 && (errno(* __error()) == EINVAL22 || errno(* __error()) == EAFNOSUPPORT47)) { |
280 | /* IPv6 is not supported by OS */ |
281 | so.sin.sin_family = AF_INET2; |
282 | so.sin.sin_addr.s_addr = INADDR_ANY(u_int32_t)0x00000000; |
283 | |
284 | handle = socket(AF_INET2, SOCK_STREAM1, 0); |
285 | } |
286 | #endif |
287 | if (handle == -1) |
288 | return NULL((void *)0); |
289 | |
290 | /* set socket options */ |
291 | #ifndef WIN32 |
292 | fcntl(handle, F_SETFL4, O_NONBLOCK0x0004); |
293 | #endif |
294 | setsockopt(handle, SOL_SOCKET0xffff, SO_REUSEADDR0x0004, &opt, sizeof(opt)); |
295 | setsockopt(handle, SOL_SOCKET0xffff, SO_KEEPALIVE0x0008, &opt, sizeof(opt)); |
296 | |
297 | /* specify the address/port we want to listen in */ |
298 | ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so)((so).sa.sa_family == 28 ? sizeof(so.sin6) : sizeof(so.sin))); |
299 | if (ret >= 0) { |
300 | /* get the actual port we started listen */ |
301 | len = SIZEOF_SOCKADDR(so)((so).sa.sa_family == 28 ? sizeof(so.sin6) : sizeof(so.sin)); |
302 | ret = getsockname(handle, &so.sa, &len); |
303 | if (ret >= 0) { |
304 | *port = sin_get_port(&so); |
305 | |
306 | /* start listening */ |
307 | if (listen(handle, 1) >= 0) |
308 | return g_io_channel_new(handle)g_io_channel_unix_new(handle); |
309 | } |
310 | |
311 | } |
312 | |
313 | /* error */ |
314 | close(handle); |
315 | return NULL((void *)0); |
316 | } |
317 | |
318 | /* Accept a connection on a socket */ |
319 | GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port) |
320 | { |
321 | union sockaddr_union so; |
322 | int ret; |
323 | socklen_t addrlen; |
324 | |
325 | g_return_val_if_fail(handle != NULL, NULL)do{ if (handle != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "handle != NULL"); return (((void *)0)); }; }while (0); |
326 | |
327 | addrlen = sizeof(so); |
328 | ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen); |
329 | |
330 | if (ret < 0) |
331 | return NULL((void *)0); |
332 | |
333 | if (addr != NULL((void *)0)) sin_get_ip(&so, addr); |
334 | if (port != NULL((void *)0)) *port = sin_get_port(&so); |
335 | |
336 | #ifndef WIN32 |
337 | fcntl(ret, F_SETFL4, O_NONBLOCK0x0004); |
338 | #endif |
339 | return g_io_channel_new(ret)g_io_channel_unix_new(ret); |
340 | } |
341 | |
342 | /* Read data from socket, return number of bytes read, -1 = error */ |
343 | int net_receive(GIOChannel *handle, char *buf, int len) |
344 | { |
345 | gsize ret; |
346 | int err; |
347 | |
348 | g_return_val_if_fail(handle != NULL, -1)do{ if (handle != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "handle != NULL"); return (-1); }; }while (0); |
349 | g_return_val_if_fail(buf != NULL, -1)do{ if (buf != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "buf != NULL"); return ( -1); }; }while (0); |
350 | |
351 | err = g_io_channel_read(handle, buf, len, &ret); |
352 | if (err == 0 && ret == 0) |
353 | return -1; /* disconnected */ |
354 | |
355 | if (err == G_IO_ERROR_AGAIN || (err != 0 && errno(* __error()) == EINTR4)) |
356 | return 0; /* no bytes received */ |
357 | |
358 | return err == 0 ? (int)ret : -1; |
359 | } |
360 | |
361 | /* Transmit data, return number of bytes sent, -1 = error */ |
362 | int net_transmit(GIOChannel *handle, const char *data, int len) |
363 | { |
364 | gsize ret; |
365 | int err; |
366 | |
367 | g_return_val_if_fail(handle != NULL, -1)do{ if (handle != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "handle != NULL"); return (-1); }; }while (0); |
368 | g_return_val_if_fail(data != NULL, -1)do{ if (data != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "data != NULL"); return ( -1); }; }while (0); |
369 | |
370 | err = g_io_channel_write(handle, (char *) data, len, &ret); |
371 | if (err == G_IO_ERROR_AGAIN || |
372 | (err != 0 && (errno(* __error()) == EINTR4 || errno(* __error()) == EPIPE32))) |
373 | return 0; |
374 | |
375 | return err == 0 ? (int)ret : -1; |
376 | } |
377 | |
378 | /* Get socket address/port */ |
379 | int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port) |
380 | { |
381 | union sockaddr_union so; |
382 | socklen_t addrlen; |
383 | |
384 | g_return_val_if_fail(handle != NULL, -1)do{ if (handle != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "handle != NULL"); return (-1); }; }while (0); |
385 | g_return_val_if_fail(addr != NULL, -1)do{ if (addr != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "addr != NULL"); return ( -1); }; }while (0); |
386 | |
387 | addrlen = sizeof(so); |
388 | if (getsockname(g_io_channel_unix_get_fd(handle), |
389 | (struct sockaddr *) &so, &addrlen) == -1) |
390 | return -1; |
391 | |
392 | sin_get_ip(&so, addr); |
393 | if (port) *port = sin_get_port(&so); |
394 | |
395 | return 0; |
396 | } |
397 | |
398 | /* Get IP addresses for host, both IPv4 and IPv6 if possible. |
399 | If ip->family is 0, the address wasn't found. |
400 | Returns 0 = ok, others = error code for net_gethosterror() */ |
401 | int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6) |
402 | { |
403 | #ifdef HAVE_IPV61 |
404 | union sockaddr_union *so; |
405 | struct addrinfo hints, *ai, *ailist; |
406 | int ret, count_v4, count_v6, use_v4, use_v6; |
407 | #else |
408 | struct hostent *hp; |
409 | int count; |
410 | #endif |
411 | |
412 | g_return_val_if_fail(addr != NULL, -1)do{ if (addr != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "addr != NULL"); return ( -1); }; }while (0); |
413 | |
414 | memset(ip4, 0, sizeof(IPADDR)); |
415 | memset(ip6, 0, sizeof(IPADDR)); |
416 | |
417 | #ifdef HAVE_IPV61 |
418 | memset(&hints, 0, sizeof(struct addrinfo)); |
419 | hints.ai_socktype = SOCK_STREAM1; |
420 | |
421 | /* save error to host_error for later use */ |
422 | ret = getaddrinfo(addr, NULL((void *)0), &hints, &ailist); |
423 | if (ret != 0) |
424 | return ret; |
425 | |
426 | /* count IPs */ |
427 | count_v4 = count_v6 = 0; |
428 | for (ai = ailist; ai != NULL((void *)0); ai = ai->ai_next) { |
429 | if (ai->ai_family == AF_INET2) |
430 | count_v4++; |
431 | else if (ai->ai_family == AF_INET628) |
432 | count_v6++; |
433 | } |
434 | |
435 | if (count_v4 == 0 && count_v6 == 0) |
436 | return HOST_NOT_FOUND1; /* shouldn't happen? */ |
437 | |
438 | /* if there are multiple addresses, return random one */ |
439 | use_v4 = count_v4 <= 1 ? 0 : rand() % count_v4; |
440 | use_v6 = count_v6 <= 1 ? 0 : rand() % count_v6; |
441 | |
442 | count_v4 = count_v6 = 0; |
443 | for (ai = ailist; ai != NULL((void *)0); ai = ai->ai_next) { |
444 | so = (union sockaddr_union *) ai->ai_addr; |
445 | |
446 | if (ai->ai_family == AF_INET2) { |
447 | if (use_v4 == count_v4) |
448 | sin_get_ip(so, ip4); |
449 | count_v4++; |
450 | } else if (ai->ai_family == AF_INET628) { |
451 | if (use_v6 == count_v6) |
452 | sin_get_ip(so, ip6); |
453 | count_v6++; |
454 | } |
455 | } |
456 | freeaddrinfo(ailist); |
457 | return 0; |
458 | #else |
459 | hp = gethostbyname(addr); |
460 | if (hp == NULL((void *)0)) |
461 | return h_errno(*__h_errno()); |
462 | |
463 | /* count IPs */ |
464 | count = 0; |
465 | while (hp->h_addr_list[count] != NULL((void *)0)) |
466 | count++; |
467 | |
468 | if (count == 0) |
469 | return HOST_NOT_FOUND1; /* shouldn't happen? */ |
470 | |
471 | /* if there are multiple addresses, return random one */ |
472 | ip4->family = AF_INET2; |
473 | memcpy(&ip4->ip, hp->h_addr_list[rand() % count], 4); |
474 | |
475 | return 0; |
476 | #endif |
477 | } |
478 | |
479 | /* Get name for host, *name should be g_free()'d unless it's NULL. |
480 | Return values are the same as with net_gethostbyname() */ |
481 | int net_gethostbyaddr(IPADDR *ip, char **name) |
482 | { |
483 | #ifdef HAVE_IPV61 |
484 | union sockaddr_union so; |
485 | int host_error; |
486 | char hostname[NI_MAXHOST1025]; |
487 | #else |
488 | struct hostent *hp; |
489 | #endif |
490 | |
491 | g_return_val_if_fail(ip != NULL, -1)do{ if (ip != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "ip != NULL"); return (- 1); }; }while (0); |
492 | g_return_val_if_fail(name != NULL, -1)do{ if (name != ((void *)0)) { } else { g_return_if_fail_warning (((gchar*) 0), __PRETTY_FUNCTION__, "name != NULL"); return ( -1); }; }while (0); |
493 | |
494 | *name = NULL((void *)0); |
495 | #ifdef HAVE_IPV61 |
496 | memset(&so, 0, sizeof(so)); |
497 | sin_set_ip(&so, ip); |
498 | |
499 | /* save error to host_error for later use */ |
500 | host_error = getnameinfo((struct sockaddr *) &so, sizeof(so), |
501 | hostname, sizeof(hostname), NULL((void *)0), 0, 0); |
502 | if (host_error != 0) |
503 | return host_error; |
504 | |
505 | *name = g_strdup(hostname); |
506 | #else |
507 | if (ip->family != AF_INET2) return -1; |
508 | hp = gethostbyaddr((const char *) &ip->ip, 4, AF_INET2); |
509 | if (hp == NULL((void *)0)) return -1; |
510 | |
511 | *name = g_strdup(hp->h_name); |
512 | #endif |
513 | |
514 | return 0; |
515 | } |
516 | |
517 | int net_ip2host(IPADDR *ip, char *host) |
518 | { |
519 | #ifdef HAVE_IPV61 |
520 | if (!inet_ntop__inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN46)) |
521 | return -1; |
522 | #else |
523 | unsigned long ip4; |
524 | |
525 | if (ip->family != AF_INET2) { |
526 | strcpy(host, "0.0.0.0"); |
527 | } else { |
528 | ip4 = ntohl(ip->ip.s_addr)__bswap32(ip->ip.s_addr); |
529 | g_snprintf(host, MAX_IP_LEN46, "%lu.%lu.%lu.%lu", |
530 | (ip4 & 0xff000000UL) >> 24, |
531 | (ip4 & 0x00ff0000) >> 16, |
532 | (ip4 & 0x0000ff00) >> 8, |
533 | (ip4 & 0x000000ff)); |
534 | } |
535 | #endif |
536 | return 0; |
537 | } |
538 | |
539 | int net_host2ip(const char *host, IPADDR *ip) |
540 | { |
541 | unsigned long addr; |
542 | |
543 | if (strchr(host, ':') != NULL((void *)0)) { |
544 | /* IPv6 */ |
545 | ip->family = AF_INET628; |
546 | #ifdef HAVE_IPV61 |
547 | if (inet_pton__inet_pton(AF_INET628, host, &ip->ip) == 0) |
548 | return -1; |
549 | #else |
550 | ip->ip.s_addr = 0; |
551 | #endif |
552 | } else { |
553 | /* IPv4 */ |
554 | ip->family = AF_INET2; |
555 | #ifdef HAVE_INET_ATON |
556 | if (inet_aton__inet_aton(host, &ip->ip.s_addr) == 0) |
557 | return -1; |
558 | #else |
559 | addr = inet_addr__inet_addr(host); |
560 | if (addr == INADDR_NONE0xffffffff) |
561 | return -1; |
562 | |
563 | memcpy(&ip->ip, &addr, 4); |
564 | #endif |
565 | } |
566 | |
567 | return 0; |
568 | } |
569 | |
570 | /* Get socket error */ |
571 | int net_geterror(GIOChannel *handle) |
572 | { |
573 | int data; |
574 | socklen_t len = sizeof(data); |
575 | |
576 | if (getsockopt(g_io_channel_unix_get_fd(handle), |
577 | SOL_SOCKET0xffff, SO_ERROR0x1007, (void *) &data, &len) == -1) |
578 | return -1; |
579 | |
580 | return data; |
581 | } |
582 | |
583 | /* get error of net_gethostname() */ |
584 | const char *net_gethosterror(int error) |
585 | { |
586 | #ifdef HAVE_IPV61 |
587 | g_return_val_if_fail(error != 0, NULL)do{ if (error != 0) { } else { g_return_if_fail_warning (((gchar *) 0), __PRETTY_FUNCTION__, "error != 0"); return (((void *)0 )); }; }while (0); |
588 | |
589 | return gai_strerror(error); |
590 | #else |
591 | switch (error) { |
592 | case HOST_NOT_FOUND1: |
593 | return "Host not found"; |
594 | case NO_ADDRESS4: |
595 | return "No IP address found for name"; |
596 | case NO_RECOVERY3: |
597 | return "A non-recovable name server error occurred"; |
598 | case TRY_AGAIN2: |
599 | return "A temporary error on an authoritative name server"; |
600 | } |
601 | |
602 | /* unknown error */ |
603 | return NULL((void *)0); |
604 | #endif |
605 | } |
606 | |
607 | /* return TRUE if host lookup failed because it didn't exist (ie. not |
608 | some error with name server) */ |
609 | int net_hosterror_notfound(int error) |
610 | { |
611 | #ifdef HAVE_IPV61 |
612 | #ifdef EAI_NODATA /* NODATA is depricated */ |
613 | return error != 1 && (error == EAI_NONAME8 || error == EAI_NODATA); |
614 | #else |
615 | return error != 1 && (error == EAI_NONAME8); |
616 | #endif |
617 | #else |
618 | return error == HOST_NOT_FOUND1 || error == NO_ADDRESS4; |
619 | #endif |
620 | } |
621 | |
622 | /* Get name of TCP service */ |
623 | char *net_getservbyport(int port) |
624 | { |
625 | struct servent *entry; |
626 | |
627 | entry = getservbyport(htons((unsigned short) port)__bswap16((unsigned short) port), "tcp"); |
628 | return entry == NULL((void *)0) ? NULL((void *)0) : entry->s_name; |
629 | } |
630 | |
631 | int is_ipv4_address(const char *host) |
632 | { |
633 | while (*host != '\0') { |
634 | if (*host != '.' && !i_isdigit(*host)__isctype(((int) (unsigned char) (*host)), 0x00000400L)) |
635 | return 0; |
636 | host++; |
637 | } |
638 | |
639 | return 1; |
640 | } |
641 | |
642 | int is_ipv6_address(const char *host) |
643 | { |
644 | while (*host != '\0') { |
645 | if (*host != ':' && !i_isxdigit(*host)__isctype(((int) (unsigned char) (*host)), 0x00010000L)) |
646 | return 0; |
647 | host++; |
648 | } |
649 | |
650 | return 1; |
651 | } |