xref: /freebsd/contrib/ntp/libntp/decodenetnum.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
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 	u_int32 *netnum
15 	)
16 {
17 	register const char *cp;
18 	register char *bp;
19 	register int i;
20 	register int temp;
21 	register int eos;
22 	char buf[80];		/* will core dump on really stupid stuff */
23 
24 	cp = num;
25 	*netnum = 0;
26 
27 	if (*cp == '[') {
28 		eos = ']';
29 		cp++;
30 	} else {
31 		eos = '\0';
32 	}
33 
34 	for (i = 0; i < 4; i++) {
35 		bp = buf;
36 		while (isdigit((int)*cp))
37 		    *bp++ = *cp++;
38 		if (bp == buf)
39 		    break;
40 
41 		if (i < 3) {
42 			if (*cp++ != '.')
43 			    break;
44 		} else if (*cp != eos)
45 		    break;
46 
47 		*bp = '\0';
48 		temp = atoi(buf);
49 		if (temp > 255)
50 		    break;
51 		*netnum <<= 8;
52 		*netnum += temp;
53 	}
54 
55 	if (i < 4)
56 	    return 0;
57 	*netnum = htonl(*netnum);
58 	return 1;
59 }
60