xref: /freebsd/contrib/ntp/libntp/decodenetnum.c (revision a9e8641da961bcf3d24afc85fd657f2083a872a2)
1 /*
2  * decodenetnum - return a net number (this is crude, but careful)
3  */
4 #include <sys/types.h>
5 #include <ctype.h>
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8 
9 #include "ntp_stdlib.h"
10 
11 int
12 decodenetnum(
13 	const char *num,
14 	struct sockaddr_storage *netnum
15 	)
16 {
17 	struct addrinfo hints, *ai = NULL;
18 	register int err, i;
19 	register const char *cp;
20 	char name[80];
21 
22 	cp = num;
23 
24 	if (*cp == '[') {
25 		cp++;
26 		for (i = 0; *cp != ']'; cp++, i++)
27 			name[i] = *cp;
28 	name[i] = '\0';
29 	num = name;
30 	}
31 	memset(&hints, 0, sizeof(struct addrinfo));
32 	hints.ai_flags = AI_NUMERICHOST;
33 	err = getaddrinfo(num, NULL, &hints, &ai);
34 	if (err != 0)
35 		return 0;
36 	memcpy(netnum, (struct sockaddr_storage *)ai->ai_addr, ai->ai_addrlen);
37 	freeaddrinfo(ai);
38 	return 1;
39 }
40