xref: /openbsd-src/lib/libc/asr/getnetnamadr.c (revision aea60bee5e9bad0aab62f480f19c2fb34c068de4)
1*aea60beeSderaadt /*	$OpenBSD: getnetnamadr.c,v 1.9 2015/01/16 16:48:51 deraadt Exp $	*/
28082e013Seric /*
38082e013Seric  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
48082e013Seric  *
58082e013Seric  * Permission to use, copy, modify, and distribute this software for any
68082e013Seric  * purpose with or without fee is hereby granted, provided that the above
78082e013Seric  * copyright notice and this permission notice appear in all copies.
88082e013Seric  *
98082e013Seric  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
108082e013Seric  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
118082e013Seric  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
128082e013Seric  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
138082e013Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
148082e013Seric  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
158082e013Seric  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
168082e013Seric  */
178082e013Seric 
18*aea60beeSderaadt #include <sys/param.h>	/* ALIGN */
198082e013Seric #include <sys/types.h>
20d216d6b1Seric #include <sys/socket.h>
218082e013Seric #include <netinet/in.h>
22d216d6b1Seric #include <netdb.h>
238082e013Seric 
24d216d6b1Seric #include <asr.h>
258082e013Seric #include <errno.h>
268082e013Seric #include <resolv.h>
278082e013Seric #include <stdlib.h>
288082e013Seric #include <string.h>
298082e013Seric 
308082e013Seric static void _fillnetent(const struct netent *, struct netent *, char *buf,
318082e013Seric     size_t);
328082e013Seric 
338082e013Seric static struct netent	 _netent;
348082e013Seric static char		 _entbuf[4096];
358082e013Seric 
368082e013Seric static char *_empty[] = { NULL, };
378082e013Seric 
388082e013Seric static void
_fillnetent(const struct netent * e,struct netent * r,char * buf,size_t len)398082e013Seric _fillnetent(const struct netent *e, struct netent *r, char *buf, size_t len)
408082e013Seric {
418082e013Seric 	char	**ptr, *end, *pos;
428082e013Seric 	size_t	n, i;
438082e013Seric 	int	naliases;
448082e013Seric 
45b55974edSeric 	bzero(buf, len);
46b55974edSeric 	bzero(r, sizeof(*r));
47b55974edSeric 	r->n_aliases = _empty;
48b55974edSeric 
498082e013Seric 	end = buf + len;
50b55974edSeric 	ptr = (char **)ALIGN(buf);
51b55974edSeric 
52b55974edSeric 	if ((char *)ptr >= end)
53b55974edSeric 		return;
548082e013Seric 
558082e013Seric 	for (naliases = 0; e->n_aliases[naliases]; naliases++)
568082e013Seric 		;
578082e013Seric 
588082e013Seric 	r->n_name = NULL;
598082e013Seric 	r->n_addrtype = e->n_addrtype;
608082e013Seric 	r->n_net = e->n_net;
618082e013Seric 	r->n_aliases = ptr;
628082e013Seric 
638082e013Seric 	pos = (char *)(ptr + (naliases + 1));
64b55974edSeric 	if (pos > end)
658082e013Seric 		r->n_aliases = _empty;
668082e013Seric 
678082e013Seric 	n = strlcpy(pos, e->n_name, end - pos);
688082e013Seric 	if (n >= end - pos)
698082e013Seric 		return;
708082e013Seric 	r->n_name = pos;
718082e013Seric 	pos += n + 1;
728082e013Seric 
738082e013Seric 	for (i = 0; i < naliases; i++) {
748082e013Seric 		n = strlcpy(pos, e->n_aliases[i], end - pos);
758082e013Seric 		if (n >= end - pos)
768082e013Seric 			return;
778082e013Seric 		r->n_aliases[i] = pos;
788082e013Seric 		pos += n + 1;
798082e013Seric 	}
808082e013Seric }
818082e013Seric 
828082e013Seric struct netent *
getnetbyname(const char * name)838082e013Seric getnetbyname(const char *name)
848082e013Seric {
855be03f8fSeric 	struct asr_query *as;
865be03f8fSeric 	struct asr_result ar;
878082e013Seric 
881ed934d0Seric 	res_init();
891ed934d0Seric 
908082e013Seric 	as = getnetbyname_async(name, NULL);
918082e013Seric 	if (as == NULL) {
928082e013Seric 		h_errno = NETDB_INTERNAL;
938082e013Seric 		return (NULL);
948082e013Seric 	}
958082e013Seric 
965be03f8fSeric 	asr_run_sync(as, &ar);
978082e013Seric 
988082e013Seric 	errno = ar.ar_errno;
998082e013Seric 	h_errno = ar.ar_h_errno;
1008082e013Seric 	if (ar.ar_netent == NULL)
1018082e013Seric 		return (NULL);
1028082e013Seric 
1038082e013Seric 	_fillnetent(ar.ar_netent, &_netent, _entbuf, sizeof(_entbuf));
1048082e013Seric 	free(ar.ar_netent);
1058082e013Seric 
1068082e013Seric 	return (&_netent);
1078082e013Seric }
1088082e013Seric 
1098082e013Seric struct netent *
getnetbyaddr(in_addr_t net,int type)1108082e013Seric getnetbyaddr(in_addr_t net, int type)
1118082e013Seric {
1125be03f8fSeric 	struct asr_query *as;
1135be03f8fSeric 	struct asr_result ar;
1148082e013Seric 
1151ed934d0Seric 	res_init();
1161ed934d0Seric 
1178082e013Seric 	as = getnetbyaddr_async(net, type, NULL);
1188082e013Seric 	if (as == NULL) {
1198082e013Seric 		h_errno = NETDB_INTERNAL;
1208082e013Seric 		return (NULL);
1218082e013Seric 	}
1228082e013Seric 
1235be03f8fSeric 	asr_run_sync(as, &ar);
1248082e013Seric 
1258082e013Seric 	errno = ar.ar_errno;
1268082e013Seric 	h_errno = ar.ar_h_errno;
1278082e013Seric 	if (ar.ar_netent == NULL)
1288082e013Seric 		return (NULL);
1298082e013Seric 
1308082e013Seric 	_fillnetent(ar.ar_netent, &_netent, _entbuf, sizeof(_entbuf));
1318082e013Seric 	free(ar.ar_netent);
1328082e013Seric 
1338082e013Seric 	return (&_netent);
1348082e013Seric }
135