File: decodenetnum.c

package info (click to toggle)
ntp 1%3A4.2.2.p4%2Bdfsg-2etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 10,236 kB
  • ctags: 10,865
  • sloc: ansic: 90,242; sh: 4,314; perl: 1,331; makefile: 631; awk: 417; asm: 37; sed: 7
file content (39 lines) | stat: -rw-r--r-- 749 bytes parent folder | download | duplicates (3)
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
/*
 * decodenetnum - return a net number (this is crude, but careful)
 */
#include <sys/types.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "ntp_stdlib.h"

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

	cp = num;

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