Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / decodenetnum.c
blob65a261987a1423dfc8fdf967f34533581c7f87fd
1 /* $NetBSD$ */
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
16 #include "ntp_stdlib.h"
17 #include "ntp_assert.h"
19 int
20 decodenetnum(
21 const char *num,
22 sockaddr_u *netnum
25 struct addrinfo hints, *ai = NULL;
26 register int err;
27 register const char *cp;
28 char name[80];
29 char *np;
31 NTP_REQUIRE(num != NULL);
32 NTP_REQUIRE(strlen(num) < sizeof(name));
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;
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;