summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/getnameinfo.c
diff options
context:
space:
mode:
authorcmetz <>1999-06-23 22:38:52 +0000
committercmetz <>1999-06-23 22:38:52 +0000
commitdb859794a581f4638e6accbed6235a18bc8f74b3 (patch)
tree85b56cd6123a97a0a71c1f443b406754eca70df1 /src/lib/libc/net/getnameinfo.c
parentbb8e4a1e2b3fa93fc2def309cb510f1734df4d77 (diff)
downloadopenbsd-db859794a581f4638e6accbed6235a18bc8f74b3.tar.gz
openbsd-db859794a581f4638e6accbed6235a18bc8f74b3.tar.bz2
openbsd-db859794a581f4638e6accbed6235a18bc8f74b3.zip
Removed portability ifdefs.
Diffstat (limited to 'src/lib/libc/net/getnameinfo.c')
-rw-r--r--src/lib/libc/net/getnameinfo.c345
1 files changed, 7 insertions, 338 deletions
diff --git a/src/lib/libc/net/getnameinfo.c b/src/lib/libc/net/getnameinfo.c
index 15aa67d464..95b27a7006 100644
--- a/src/lib/libc/net/getnameinfo.c
+++ b/src/lib/libc/net/getnameinfo.c
@@ -30,98 +30,19 @@
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE. 31 * SUCH DAMAGE.
32 * 32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Craig Metz and
44 * by other contributors.
45 * 4. Neither the name of the author nor the names of contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by Craig Metz and
72 * by other contributors.
73 * 4. Neither the name of the author nor the names of contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 */ 33 */
89 34
90/* getnameinfo() v1.38 */ 35/* getnameinfo() v1.38 */
91 36
92/* To enable debugging support (REQUIRES NRL support library), define: */
93/* #define DEBUG 1 */
94
95#ifdef __OpenBSD__
96#define HAVE_POSIX1G_TYPES 1
97#define INET6 1
98#define LOCAL 1
99#define NETDB 1
100#define SALEN 1
101#undef RESOLVER
102#undef HOSTTABLE
103#undef DEBUG
104#undef HAVE_GETSERVBYNAME_R
105#undef HAVE_GETHOSTBYNAME2_R
106#endif /* __OpenBSD__ */
107
108#include <sys/types.h> 37#include <sys/types.h>
109#include <sys/socket.h> 38#include <sys/socket.h>
110 39
111#include <netinet/in.h> 40#include <netinet/in.h>
112#if LOCAL
113#include <sys/un.h> 41#include <sys/un.h>
114#include <sys/utsname.h> 42#include <sys/utsname.h>
115#endif /* LOCAL */
116#include <netdb.h> 43#include <netdb.h>
117#include <errno.h> 44#include <errno.h>
118#include <string.h> 45#include <string.h>
119#if RESOLVER
120#include <arpa/nameser.h>
121#include <resolv.h>
122#endif /* RESOLVER */
123
124#include "support.h"
125 46
126#ifndef AF_LOCAL 47#ifndef AF_LOCAL
127#define AF_LOCAL AF_UNIX 48#define AF_LOCAL AF_UNIX
@@ -131,214 +52,11 @@
131#define min(x,y) (((x) > (y)) ? (y) : (x)) 52#define min(x,y) (((x) > (y)) ? (y) : (x))
132#endif /* min */ 53#endif /* min */
133 54
134#if DEBUG
135#if RESOLVER
136#define DEBUG_MESSAGES (_res.options & RES_DEBUG)
137#else /* RESOLVER */
138int __getnameinfo_debug = 0;
139#define DEBUG_MESSAGES (__getnameinfo_debug)
140#endif /* RESOLVER */
141#endif /* DEBUG */
142
143#if DEBUG
144#define RETURN_ERROR(x) do { \ 55#define RETURN_ERROR(x) do { \
145 if (DEBUG_MESSAGES) \
146 fprintf(stderr, "%s:%d: returning %s\n", __FILE__, __LINE__, #x); \
147 rval = (x); \ 56 rval = (x); \
148 goto ret; \ 57 goto ret; \
149 } while(0) 58 } while(0)
150#else /* DEBUG */
151#define RETURN_ERROR(x) do { \
152 rval = (x); \
153 goto ret; \
154 } while(0)
155#endif /* DEBUG */
156
157#if HOSTTABLE
158static int hosttable_lookup_name(int family, void *addr, char *name, int namelen, int flags)
159{
160 int rval;
161 FILE *f;
162 char buffer[1024];
163 char addrbuf[16];
164 char *c, *c2;
165 int i;
166 char *prevcname = NULL;
167
168 if (!(f = fopen("/etc/hosts", "r")))
169 RETURN_ERROR(EAI_SYSTEM);
170
171 while(fgets(buffer, sizeof(buffer), f)) {
172 if (c = strchr(buffer, '#'))
173 *c = 0;
174
175 c = buffer;
176 while(*c && !isspace(*c)) c++;
177 if (!*c)
178 continue;
179
180 *(c++) = 0;
181
182 if (family == AF_INET)
183 if (inet_pton(AF_INET, buffer, addrbuf) > 0)
184 if (!memcmp(addrbuf, addr, sizeof(struct in_addr)))
185 goto build;
186
187#if INET6
188 if (family == AF_INET6)
189 if (inet_pton(AF_INET6, buffer, addrbuf) > 0)
190 if (!memcmp(addrbuf, addr, sizeof(struct in6_addr)))
191 goto build;
192#endif /* INET6 */
193
194 continue;
195
196build:
197 while(*c && isspace(*c)) c++;
198 if (!*c)
199 continue;
200 59
201 c2 = c;
202 while(*c2 && !isspace(*c2)) c2++;
203 if (!*c2)
204 continue;
205 *c2 = 0;
206
207 if ((flags & NI_NOFQDN) && (_res.options & RES_INIT) && _res.defdname[0] && (c2 = strstr(c + 1, _res.defdname)) && (*(--c2) == '.')) {
208 *c2 = 0;
209 i = min(c2 - c, namelen) - 1;
210 strncpy(name, c, i);
211 } else
212 strncpy(name, c, namelen - 1);
213
214 rval = 0;
215 goto ret;
216 };
217
218 RETURN_ERROR(1);
219
220ret:
221 fclose(f);
222 return rval;
223};
224#endif /* HOSTTABLE */
225
226#if RESOLVER
227#if INET6
228static char hextab[] = { '0', '1', '2', '3', '4', '5', '6', '7',
229 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
230#endif /* INET6 */
231
232struct rrheader {
233 int16_t type;
234 int16_t class;
235 u_int32_t ttl;
236 int16_t size;
237};
238#define RRHEADER_SZ 10
239
240int resolver_lookup_name(const char *ptrname, char *name, int namelen, int flags)
241{
242 int rval;
243 char answer[PACKETSZ];
244 int answerlen;
245 char dn[MAXDNAME];
246 char *prevcname = NULL;
247 void *p, *ep;
248 int answers, i;
249 uint16_t rtype, rclass;
250
251 if ((answerlen = res_search(ptrname, C_IN, T_PTR, answer, sizeof(answer))) < 0) {
252 switch(h_errno) {
253 case NETDB_INTERNAL:
254 RETURN_ERROR(EAI_SYSTEM);
255 case HOST_NOT_FOUND:
256 RETURN_ERROR(1);
257 case TRY_AGAIN:
258 RETURN_ERROR(EAI_AGAIN);
259 case NO_RECOVERY:
260 RETURN_ERROR(EAI_FAIL);
261 case NO_DATA:
262 RETURN_ERROR(1);
263 default:
264 RETURN_ERROR(EAI_FAIL);
265 };
266 };
267
268 p = answer;
269 ep = answer + answerlen;
270
271 if (answerlen < sizeof(HEADER))
272 RETURN_ERROR(EAI_FAIL);
273
274 {
275 HEADER *h = (HEADER *)p;
276 if (!h->qr || (h->opcode != QUERY) || (h->qdcount != htons(1)) || !h->ancount)
277 RETURN_ERROR(EAI_FAIL);
278
279 answers = ntohs(h->ancount);
280 };
281 p += sizeof(HEADER);
282
283 if ((i = dn_expand(answer, ep, p, dn, sizeof(dn))) < 0)
284 RETURN_ERROR(EAI_FAIL);
285
286 p += i;
287
288 if (p + 2*sizeof(u_int16_t) >= ep)
289 RETURN_ERROR(EAI_FAIL);
290
291 GETSHORT(rtype, p);
292 GETSHORT(rclass, p);
293
294 if ((rtype != T_PTR) || (rclass != C_IN))
295 RETURN_ERROR(EAI_FAIL);
296
297 while(answers--) {
298 if ((i = dn_expand(answer, ep, p, dn, sizeof(dn))) < 0)
299 RETURN_ERROR(EAI_FAIL);
300
301 p += i;
302
303 if (p + RRHEADER_SZ >= ep)
304 RETURN_ERROR(EAI_FAIL);
305
306 GETSHORT(rtype, p);
307 GETSHORT(rclass, p);
308 p += sizeof(uint32_t);
309 if (rclass != C_IN)
310 RETURN_ERROR(EAI_FAIL);
311 GETSHORT(rclass, p);
312 i = rclass;
313
314 if (p + i > ep)
315 RETURN_ERROR(EAI_FAIL);
316
317 if (rtype == T_PTR) {
318 if (dn_expand(answer, ep, p, dn, sizeof(dn)) != i)
319 RETURN_ERROR(EAI_FAIL);
320
321 {
322 char *c2;
323
324 if ((flags & NI_NOFQDN) && (_res.options & RES_INIT) && _res.defdname[0] && (c2 = strstr(dn + 1, _res.defdname)) && (*(--c2) == '.')) {
325 *c2 = 0;
326 strncpy(name, dn, min(c2 - dn, namelen) - 1);
327 } else
328 strncpy(name, dn, namelen - 1);
329 };
330 };
331 p += i;
332 };
333
334 rval = 0;
335
336ret:
337 return rval;
338};
339#endif /* RESOLVER */
340
341#if NETDB
342static int netdb_lookup_name(int family, void *addr, int addrlen, char *name, 60static int netdb_lookup_name(int family, void *addr, int addrlen, char *name,
343 int namelen) 61 int namelen)
344{ 62{
@@ -379,7 +97,6 @@ static int netdb_lookup_name(int family, void *addr, int addrlen, char *name,
379ret: 97ret:
380 return rval; 98 return rval;
381} 99}
382#endif /* NETDB */
383 100
384int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) 101int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
385{ 102{
@@ -391,7 +108,6 @@ int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t ho
391 108
392 if (host && (hostlen > 0)) 109 if (host && (hostlen > 0))
393 switch(sa->sa_family) { 110 switch(sa->sa_family) {
394#if INET6
395 case AF_INET6: 111 case AF_INET6:
396 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)sa)->sin6_addr)) { 112 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)sa)->sin6_addr)) {
397 if (flags & NI_NUMERICHOST) 113 if (flags & NI_NUMERICHOST)
@@ -404,9 +120,7 @@ int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t ho
404 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) { 120 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)sa)->sin6_addr)) {
405 struct sockaddr_in sin; 121 struct sockaddr_in sin;
406 memset(&sin, 0, sizeof(struct sockaddr_in)); 122 memset(&sin, 0, sizeof(struct sockaddr_in));
407#if SALEN
408 sin.sin_len = sizeof(struct sockaddr_in); 123 sin.sin_len = sizeof(struct sockaddr_in);
409#endif /* SALEN */
410 sin.sin_family = AF_INET; 124 sin.sin_family = AF_INET;
411 sin.sin_port = ((struct sockaddr_in6 *)sa)->sin6_port; 125 sin.sin_port = ((struct sockaddr_in6 *)sa)->sin6_port;
412 sin.sin_addr.s_addr = ((u_int32_t *)&((struct sockaddr_in6 *)sa)->sin6_addr)[3]; 126 sin.sin_addr.s_addr = ((u_int32_t *)&((struct sockaddr_in6 *)sa)->sin6_addr)[3];
@@ -420,37 +134,13 @@ int getnameinfo(const struct sockaddr *sa, size_t addrlen, char *host, size_t ho
420 if (flags & NI_NUMERICHOST) 134 if (flags & NI_NUMERICHOST)
421 goto inet6_noname; 135 goto inet6_noname;
422 136
423#if HOSTTABLE 137 if ((rval = netdb_lookup_name(AF_INET6,
424 if ((rval = hosttable_lookup_name(AF_INET6, &((struct sockaddr_in6 *)sa)->sin6_addr, host, hostlen, flags)) < 0) 138 &((struct sockaddr_in6 *)sa)->sin6_addr, sizeof(struct in6_addr),
139 host, hostlen, flags)) < 0)
425 goto ret; 140 goto ret;
426 141
427 if (!rval) 142 if (!rval)
428 break; 143 break;
429#endif /* HOSTTABLE */
430#if RESOLVER
431 {
432 char ptrname[sizeof("0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.ip6.int.")];
433 {
434 int i;
435 char *c = ptrname;
436 u_int8_t *p = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr + sizeof(struct in6_addr) - 1;
437
438 for (i = sizeof(struct in6_addr) / sizeof(u_int8_t); i > 0; i--, p--) {
439 *(c++) = hextab[*p & 0x0f];
440 *(c++) = '.';
441 *(c++) = hextab[(*p & 0xf0) >> 4];
442 *(c++) = '.';
443 };
444 strcpy(c, "ip6.int.");
445 };
446
447 if ((rval = resolver_lookup_name(ptrname, host, hostlen, flags)) < 0)
448 goto ret;
449
450 if (!rval)
451 break;
452 };
453#endif /* RESOLVER */
454 144
455inet6_noname: 145inet6_noname:
456 if (flags & NI_NAMEREQD) 146 if (flags & NI_NAMEREQD)
@@ -460,7 +150,6 @@ inet6_noname:
460 RETURN_ERROR(EAI_NONAME); 150 RETURN_ERROR(EAI_NONAME);
461 151
462 break; 152 break;
463#endif /* INET6 */
464 case AF_INET: 153 case AF_INET:
465 if (flags & NI_NUMERICHOST) 154 if (flags & NI_NUMERICHOST)
466 goto inet_noname; 155 goto inet_noname;
@@ -470,27 +159,13 @@ inet6_noname:
470 break; 159 break;
471 }; 160 };
472 161
473#if HOSTTABLE 162 if ((rval = netdb_lookup_name(AF_INET,
474 if ((rval = hosttable_lookup_name(AF_INET, &((struct sockaddr_in *)sa)->sin_addr, host, hostlen, flags)) < 0) 163 &((struct sockaddr_in *)sa)->sin_addr, sizeof(struct in_addr),
164 host, hostlen, flags)) < 0)
475 goto ret; 165 goto ret;
476 166
477 if (!rval) 167 if (!rval)
478 break; 168 break;
479#endif /* HOSTTABLE */
480#if RESOLVER
481 {
482 char ptrname[30];
483 u_int8_t *p = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
484 sprintf(ptrname, "%d.%d.%d.%d.in-addr.arpa.", p[3], p[2], p[1], p[0]);
485
486 if ((rval = resolver_lookup_name(ptrname, host, hostlen, flags)) < 0)
487 goto ret;
488
489 if (!rval)
490 break;
491 };
492#endif /* RESOLVER */
493
494inet_noname: 169inet_noname:
495 if (flags & NI_NAMEREQD) 170 if (flags & NI_NAMEREQD)
496 RETURN_ERROR(EAI_NONAME); 171 RETURN_ERROR(EAI_NONAME);
@@ -499,7 +174,6 @@ inet_noname:
499 RETURN_ERROR(EAI_NONAME); 174 RETURN_ERROR(EAI_NONAME);
500 175
501 break; 176 break;
502#if LOCAL
503 case AF_LOCAL: 177 case AF_LOCAL:
504 if (!(flags & NI_NUMERICHOST)) { 178 if (!(flags & NI_NUMERICHOST)) {
505 struct utsname utsname; 179 struct utsname utsname;
@@ -515,7 +189,6 @@ inet_noname:
515 189
516 strncpy(host, "localhost", hostlen - 1); 190 strncpy(host, "localhost", hostlen - 1);
517 break; 191 break;
518#endif /* LOCAL */
519 default: 192 default:
520 RETURN_ERROR(EAI_FAMILY); 193 RETURN_ERROR(EAI_FAMILY);
521 }; 194 };
@@ -523,9 +196,7 @@ inet_noname:
523 if (serv && (servlen > 0)) 196 if (serv && (servlen > 0))
524 switch(sa->sa_family) { 197 switch(sa->sa_family) {
525 case AF_INET: 198 case AF_INET:
526#if INET6
527 case AF_INET6: 199 case AF_INET6:
528#endif /* INET6 */
529 if (!(flags & NI_NUMERICSERV)) { 200 if (!(flags & NI_NUMERICSERV)) {
530 struct servent *s; 201 struct servent *s;
531 if (s = getservbyport(((struct sockaddr_in *)sa)->sin_port, (flags & NI_DGRAM) ? "udp" : "tcp")) { 202 if (s = getservbyport(((struct sockaddr_in *)sa)->sin_port, (flags & NI_DGRAM) ? "udp" : "tcp")) {
@@ -539,11 +210,9 @@ inet_noname:
539 }; 210 };
540 snprintf(serv, servlen - 1, "%d", ntohs(((struct sockaddr_in *)sa)->sin_port)); 211 snprintf(serv, servlen - 1, "%d", ntohs(((struct sockaddr_in *)sa)->sin_port));
541 break; 212 break;
542#if LOCAL
543 case AF_LOCAL: 213 case AF_LOCAL:
544 strncpy(serv, ((struct sockaddr_un *)sa)->sun_path, servlen - 1); 214 strncpy(serv, ((struct sockaddr_un *)sa)->sun_path, servlen - 1);
545 break; 215 break;
546#endif /* LOCAL */
547 }; 216 };
548 217
549 if (host && (hostlen > 0)) 218 if (host && (hostlen > 0))