File: decodenetnum.c

package info (click to toggle)
ntp 1%3A4.2.6.p2%2Bdfsg-1%2Bdeb6u4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 22,612 kB
  • ctags: 21,640
  • sloc: ansic: 139,032; sh: 11,227; perl: 1,338; makefile: 1,110; yacc: 1,001; awk: 417; asm: 37; sed: 7
file content (53 lines) | stat: -rw-r--r-- 911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * decodenetnum - return a net number (this is crude, but careful)
 */
#include <config.h>
#include <sys/types.h>
#include <ctype.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#include "ntp_stdlib.h"
#include "ntp_assert.h"

int
decodenetnum(
	const char *num,
	sockaddr_u *netnum
	)
{
	struct addrinfo hints, *ai = NULL;
	register int err;
	register const char *cp;
	char name[80];
	char *np;

	NTP_REQUIRE(num != NULL);

	if (strlen(num) >= sizeof(name)) {
		return 0;
	}

	if ('[' != num[0]) 
		cp = num;
	else {
		cp = num + 1;
		np = name; 
		while (*cp && ']' != *cp)
			*np++ = *cp++;
		*np = 0;
		cp = name; 
	}
	memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_NUMERICHOST;
	err = getaddrinfo(cp, NULL, &hints, &ai);
	if (err != 0)
		return 0;
	memcpy(netnum, ai->ai_addr, ai->ai_addrlen); 
	freeaddrinfo(ai);
	return 1;
}