1 /*	$NetBSD: decodenetnum.c,v 1.1.1.1 2009/12/13 16:55:02 kardel Exp $	*/
2 
3 /*
4  * decodenetnum - return a net number (this is crude, but careful)
5  */
6 #include <config.h>
7 #include <sys/types.h>
8 #include <ctype.h>
9 #ifdef HAVE_SYS_SOCKET_H
10 #include <sys/socket.h>
11 #endif
12 #ifdef HAVE_NETINET_IN_H
13 #include <netinet/in.h>
14 #endif
15 
16 #include "ntp_stdlib.h"
17 #include "ntp_assert.h"
18 
19 int
20 decodenetnum(
21 	const char *num,
22 	sockaddr_u *netnum
23 	)
24 {
25 	struct addrinfo hints, *ai = NULL;
26 	register int err;
27 	register const char *cp;
28 	char name[80];
29 	char *np;
30 
31 	NTP_REQUIRE(num != NULL);
32 	NTP_REQUIRE(strlen(num) < sizeof(name));
33 
34 	if ('[' != num[0])
35 		cp = num;
36 	else {
37 		cp = num + 1;
38 		np = name;
39 		while (*cp && ']' != *cp)
40 			*np++ = *cp++;
41 		*np = 0;
42 		cp = name;
43 	}
44 	memset(&hints, 0, sizeof(hints));
45 	hints.ai_flags = AI_NUMERICHOST;
46 	err = getaddrinfo(cp, NULL, &hints, &ai);
47 	if (err != 0)
48 		return 0;
49 	memcpy(netnum, ai->ai_addr, ai->ai_addrlen);
50 	freeaddrinfo(ai);
51 	return 1;
52 }
53