aboutsummaryrefslogtreecommitdiff
path: root/contrib/ntp/libntp/decodenetnum.c
blob: a24e4933af91c0928e434f3e76821bc66971d843 (plain) (blame)
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
54
55
56
57
58
59
/*
 * 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,
	u_int32 *netnum
	)
{
	register const char *cp;
	register char *bp;
	register int i;
	register int temp;
	register int eos;
	char buf[80];		/* will core dump on really stupid stuff */

	cp = num;
	*netnum = 0;

	if (*cp == '[') {
		eos = ']';
		cp++;
	} else {
		eos = '\0';
	}

	for (i = 0; i < 4; i++) {
		bp = buf;
		while (isdigit((int)*cp))
		    *bp++ = *cp++;
		if (bp == buf)
		    break;

		if (i < 3) {
			if (*cp++ != '.')
			    break;
		} else if (*cp != eos)
		    break;

		*bp = '\0';
		temp = atoi(buf);
		if (temp > 255)
		    break;
		*netnum <<= 8;
		*netnum += temp;
	}
	
	if (i < 4)
	    return 0;
	*netnum = htonl(*netnum);
	return 1;
}