aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2015-02-03 12:07:40 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2015-02-03 12:07:40 +0100
commit8e74adab0107658e3dc04ed342206272a284f43e (patch)
tree1c13c87b55e06b7f60fb982d044b0ff22d560be1
parentf7f70bf1b3025550ea4ad8d13d977b846a868a06 (diff)
downloadbusybox-w32-8e74adab0107658e3dc04ed342206272a284f43e.tar.gz
busybox-w32-8e74adab0107658e3dc04ed342206272a284f43e.tar.bz2
busybox-w32-8e74adab0107658e3dc04ed342206272a284f43e.zip
libbb: make INET[6]_rresolve use sockaddr2{host,dotted}_noport
function old new delta INET_rresolve 274 214 -60 INET6_rresolve 168 85 -83 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--libbb/inet_common.c84
1 files changed, 33 insertions, 51 deletions
diff --git a/libbb/inet_common.c b/libbb/inet_common.c
index b3e0802ee..5b4a4a10b 100644
--- a/libbb/inet_common.c
+++ b/libbb/inet_common.c
@@ -32,14 +32,12 @@ int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostf
32 return 0; 32 return 0;
33 } 33 }
34 /* If we expect this to be a hostname, try hostname database first */ 34 /* If we expect this to be a hostname, try hostname database first */
35#ifdef DEBUG
36 if (hostfirst) { 35 if (hostfirst) {
36#ifdef DEBUG
37 bb_error_msg("gethostbyname(%s)", name); 37 bb_error_msg("gethostbyname(%s)", name);
38 }
39#endif 38#endif
40 if (hostfirst) {
41 hp = gethostbyname(name); 39 hp = gethostbyname(name);
42 if (hp != NULL) { 40 if (hp) {
43 memcpy(&s_in->sin_addr, hp->h_addr_list[0], 41 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
44 sizeof(struct in_addr)); 42 sizeof(struct in_addr));
45 return 0; 43 return 0;
@@ -51,7 +49,7 @@ int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostf
51 bb_error_msg("getnetbyname(%s)", name); 49 bb_error_msg("getnetbyname(%s)", name);
52#endif 50#endif
53 np = getnetbyname(name); 51 np = getnetbyname(name);
54 if (np != NULL) { 52 if (np) {
55 s_in->sin_addr.s_addr = htonl(np->n_net); 53 s_in->sin_addr.s_addr = htonl(np->n_net);
56 return 1; 54 return 1;
57 } 55 }
@@ -66,7 +64,7 @@ int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostf
66 bb_error_msg("gethostbyname(%s)", name); 64 bb_error_msg("gethostbyname(%s)", name);
67#endif 65#endif
68 hp = gethostbyname(name); 66 hp = gethostbyname(name);
69 if (hp == NULL) { 67 if (!hp) {
70 return -1; 68 return -1;
71 } 69 }
72 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); 70 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
@@ -74,7 +72,7 @@ int FAST_FUNC INET_resolve(const char *name, struct sockaddr_in *s_in, int hostf
74} 72}
75 73
76 74
77/* numeric: & 0x8000: default instead of *, 75/* numeric: & 0x8000: "default" instead of "*",
78 * & 0x4000: host instead of net, 76 * & 0x4000: host instead of net,
79 * & 0x0fff: don't resolve 77 * & 0x0fff: don't resolve
80 */ 78 */
@@ -83,16 +81,16 @@ char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t ne
83 /* addr-to-name cache */ 81 /* addr-to-name cache */
84 struct addr { 82 struct addr {
85 struct addr *next; 83 struct addr *next;
86 struct sockaddr_in addr; 84 uint32_t nip;
87 int host; 85 smallint is_host;
88 char name[1]; 86 char name[1];
89 }; 87 };
90 static struct addr *cache = NULL; 88 static struct addr *cache = NULL;
91 89
92 struct addr *pn; 90 struct addr *pn;
93 char *name; 91 char *name;
94 uint32_t ad, host_ad; 92 uint32_t nip;
95 int host = 0; 93 smallint is_host;
96 94
97 if (s_in->sin_family != AF_INET) { 95 if (s_in->sin_family != AF_INET) {
98#ifdef DEBUG 96#ifdef DEBUG
@@ -102,61 +100,57 @@ char* FAST_FUNC INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t ne
102 errno = EAFNOSUPPORT; 100 errno = EAFNOSUPPORT;
103 return NULL; 101 return NULL;
104 } 102 }
105 ad = s_in->sin_addr.s_addr; 103 nip = s_in->sin_addr.s_addr;
106#ifdef DEBUG 104#ifdef DEBUG
107 bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric); 105 bb_error_msg("rresolve: %08x mask:%08x num:%08x", (unsigned)nip, netmask, numeric);
108#endif 106#endif
109 if (ad == INADDR_ANY) {
110 if ((numeric & 0x0FFF) == 0) {
111 if (numeric & 0x8000)
112 return xstrdup("default");
113 return xstrdup("*");
114 }
115 }
116 if (numeric & 0x0FFF) 107 if (numeric & 0x0FFF)
117 return xstrdup(inet_ntoa(s_in->sin_addr)); 108 return xmalloc_sockaddr2dotted_noport((void*)s_in);
109 if (nip == INADDR_ANY) {
110 if (numeric & 0x8000)
111 return xstrdup("default");
112 return xstrdup("*");
113 }
114
115 is_host = ((nip & (~netmask)) != 0 || (numeric & 0x4000));
118 116
119 if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
120 host = 1;
121 pn = cache; 117 pn = cache;
122 while (pn) { 118 while (pn) {
123 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { 119 if (pn->nip == nip && pn->is_host == is_host) {
124#ifdef DEBUG 120#ifdef DEBUG
125 bb_error_msg("rresolve: found %s %08x in cache", 121 bb_error_msg("rresolve: found %s %08x in cache",
126 (host ? "host" : "net"), (unsigned)ad); 122 (is_host ? "host" : "net"), (unsigned)nip);
127#endif 123#endif
128 return xstrdup(pn->name); 124 return xstrdup(pn->name);
129 } 125 }
130 pn = pn->next; 126 pn = pn->next;
131 } 127 }
132 128
133 host_ad = ntohl(ad);
134 name = NULL; 129 name = NULL;
135 if (host) { 130 if (is_host) {
136 struct hostent *ent;
137#ifdef DEBUG 131#ifdef DEBUG
138 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad); 132 bb_error_msg("sockaddr2host_noport(%08x)", (unsigned)nip);
139#endif 133#endif
140 ent = gethostbyaddr((char *) &ad, 4, AF_INET); 134 name = xmalloc_sockaddr2host_noport((void*)s_in);
141 if (ent)
142 name = xstrdup(ent->h_name);
143 } else if (ENABLE_FEATURE_ETC_NETWORKS) { 135 } else if (ENABLE_FEATURE_ETC_NETWORKS) {
144 struct netent *np; 136 struct netent *np;
145#ifdef DEBUG 137#ifdef DEBUG
146 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad); 138 bb_error_msg("getnetbyaddr(%08x)", (unsigned)ntohl(nip));
147#endif 139#endif
148 np = getnetbyaddr(host_ad, AF_INET); 140 np = getnetbyaddr(ntohl(nip), AF_INET);
149 if (np) 141 if (np)
150 name = xstrdup(np->n_name); 142 name = xstrdup(np->n_name);
151 } 143 }
152 if (!name) 144 if (!name)
153 name = xstrdup(inet_ntoa(s_in->sin_addr)); 145 name = xmalloc_sockaddr2dotted_noport((void*)s_in);
146
154 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */ 147 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */
155 pn->next = cache; 148 pn->next = cache;
156 pn->addr = *s_in; 149 pn->nip = nip;
157 pn->host = host; 150 pn->is_host = is_host;
158 strcpy(pn->name, name); 151 strcpy(pn->name, name);
159 cache = pn; 152 cache = pn;
153
160 return name; 154 return name;
161} 155}
162 156
@@ -188,9 +182,6 @@ int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
188 182
189char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric) 183char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
190{ 184{
191 char name[128];
192 int s;
193
194 if (sin6->sin6_family != AF_INET6) { 185 if (sin6->sin6_family != AF_INET6) {
195#ifdef DEBUG 186#ifdef DEBUG
196 bb_error_msg("rresolve: unsupported address family %d!", 187 bb_error_msg("rresolve: unsupported address family %d!",
@@ -200,8 +191,7 @@ char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
200 return NULL; 191 return NULL;
201 } 192 }
202 if (numeric & 0x7FFF) { 193 if (numeric & 0x7FFF) {
203 inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name)); 194 return xmalloc_sockaddr2dotted_noport((void*)sin6);
204 return xstrdup(name);
205 } 195 }
206 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 196 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
207 if (numeric & 0x8000) 197 if (numeric & 0x8000)
@@ -209,15 +199,7 @@ char* FAST_FUNC INET6_rresolve(struct sockaddr_in6 *sin6, int numeric)
209 return xstrdup("*"); 199 return xstrdup("*");
210 } 200 }
211 201
212 s = getnameinfo((struct sockaddr *) sin6, sizeof(*sin6), 202 return xmalloc_sockaddr2host_noport((void*)sin6);
213 name, sizeof(name),
214 /*serv,servlen:*/ NULL, 0,
215 0);
216 if (s != 0) {
217 bb_error_msg("getnameinfo failed");
218 return NULL;
219 }
220 return xstrdup(name);
221} 203}
222 204
223#endif /* CONFIG_FEATURE_IPV6 */ 205#endif /* CONFIG_FEATURE_IPV6 */