xref: /openbsd/lib/libc/asr/getnetnamadr.c (revision cecf84d4)
1 /*	$OpenBSD: getnetnamadr.c,v 1.9 2015/01/16 16:48:51 deraadt Exp $	*/
2 /*
3  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>	/* ALIGN */
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 
24 #include <asr.h>
25 #include <errno.h>
26 #include <resolv.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 static void _fillnetent(const struct netent *, struct netent *, char *buf,
31     size_t);
32 
33 static struct netent	 _netent;
34 static char		 _entbuf[4096];
35 
36 static char *_empty[] = { NULL, };
37 
38 static void
39 _fillnetent(const struct netent *e, struct netent *r, char *buf, size_t len)
40 {
41 	char	**ptr, *end, *pos;
42 	size_t	n, i;
43 	int	naliases;
44 
45 	bzero(buf, len);
46 	bzero(r, sizeof(*r));
47 	r->n_aliases = _empty;
48 
49 	end = buf + len;
50 	ptr = (char **)ALIGN(buf);
51 
52 	if ((char *)ptr >= end)
53 		return;
54 
55 	for (naliases = 0; e->n_aliases[naliases]; naliases++)
56 		;
57 
58 	r->n_name = NULL;
59 	r->n_addrtype = e->n_addrtype;
60 	r->n_net = e->n_net;
61 	r->n_aliases = ptr;
62 
63 	pos = (char *)(ptr + (naliases + 1));
64 	if (pos > end)
65 		r->n_aliases = _empty;
66 
67 	n = strlcpy(pos, e->n_name, end - pos);
68 	if (n >= end - pos)
69 		return;
70 	r->n_name = pos;
71 	pos += n + 1;
72 
73 	for (i = 0; i < naliases; i++) {
74 		n = strlcpy(pos, e->n_aliases[i], end - pos);
75 		if (n >= end - pos)
76 			return;
77 		r->n_aliases[i] = pos;
78 		pos += n + 1;
79 	}
80 }
81 
82 struct netent *
83 getnetbyname(const char *name)
84 {
85 	struct asr_query *as;
86 	struct asr_result ar;
87 
88 	res_init();
89 
90 	as = getnetbyname_async(name, NULL);
91 	if (as == NULL) {
92 		h_errno = NETDB_INTERNAL;
93 		return (NULL);
94 	}
95 
96 	asr_run_sync(as, &ar);
97 
98 	errno = ar.ar_errno;
99 	h_errno = ar.ar_h_errno;
100 	if (ar.ar_netent == NULL)
101 		return (NULL);
102 
103 	_fillnetent(ar.ar_netent, &_netent, _entbuf, sizeof(_entbuf));
104 	free(ar.ar_netent);
105 
106 	return (&_netent);
107 }
108 
109 struct netent *
110 getnetbyaddr(in_addr_t net, int type)
111 {
112 	struct asr_query *as;
113 	struct asr_result ar;
114 
115 	res_init();
116 
117 	as = getnetbyaddr_async(net, type, NULL);
118 	if (as == NULL) {
119 		h_errno = NETDB_INTERNAL;
120 		return (NULL);
121 	}
122 
123 	asr_run_sync(as, &ar);
124 
125 	errno = ar.ar_errno;
126 	h_errno = ar.ar_h_errno;
127 	if (ar.ar_netent == NULL)
128 		return (NULL);
129 
130 	_fillnetent(ar.ar_netent, &_netent, _entbuf, sizeof(_entbuf));
131 	free(ar.ar_netent);
132 
133 	return (&_netent);
134 }
135