1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
$NetBSD: patch-ad,v 1.4 2003/07/05 14:25:51 wiz Exp $
--- src/core/network.c.orig Mon Aug 26 21:10:02 2002
+++ src/core/network.c
@@ -201,10 +201,13 @@ GIOChannel *net_connect_ip(IPADDR *ip, i
/* set our own address */
if (my_ip != NULL) {
sin_set_ip(&so, my_ip);
- if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
+ if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
/* failed, set it back to INADDR_ANY */
- sin_set_ip(&so, NULL);
- bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
+ int old_errno = errno;
+
+ close(handle);
+ errno = old_errno;
+ return NULL;
}
}
@@ -466,33 +469,31 @@ int net_gethostbyname(const char *addr,
int net_gethostbyaddr(IPADDR *ip, char **name)
{
#ifdef HAVE_IPV6
- struct addrinfo req, *ai;
+ union sockaddr_union so;
int host_error;
+ char hostname[NI_MAXHOST];
#else
struct hostent *hp;
#endif
- char ipname[MAX_IP_LEN];
g_return_val_if_fail(ip != NULL, -1);
g_return_val_if_fail(name != NULL, -1);
- net_ip2host(ip, ipname);
-
*name = NULL;
#ifdef HAVE_IPV6
- memset(&req, 0, sizeof(struct addrinfo));
- req.ai_socktype = SOCK_STREAM;
- req.ai_flags = AI_CANONNAME;
+ memset(&so, 0, sizeof(so));
+ sin_set_ip(&so, ip);
/* save error to host_error for later use */
- host_error = getaddrinfo(ipname, NULL, &req, &ai);
- if (host_error != 0)
- return host_error;
- *name = g_strdup(ai->ai_canonname);
+ host_error = getnameinfo((struct sockaddr *) &so, sizeof(so),
+ hostname, sizeof(hostname), NULL, 0, 0);
+ if (host_error != 0)
+ return host_error;
- freeaddrinfo(ai);
+ *name = g_strdup(hostname);
#else
- hp = gethostbyaddr(ipname, strlen(ipname), AF_INET);
+ if (ip->family != AF_INET) return -1;
+ hp = gethostbyaddr(&ip->ip, 4, AF_INET);
if (hp == NULL) return -1;
*name = g_strdup(hp->h_name);
|