1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin
24da2e3ebdSchin #include <ast.h>
25da2e3ebdSchin #include <ctype.h>
26da2e3ebdSchin
27da2e3ebdSchin /*
28da2e3ebdSchin * convert string to 4 byte local byte order ip address
29da2e3ebdSchin * with optional prefix bits
30da2e3ebdSchin * pointer to first unused char placed in *e, even on error
31da2e3ebdSchin * return 0:ok <0:error
32da2e3ebdSchin *
33da2e3ebdSchin * valid addresses match the egrep RE:
34da2e3ebdSchin *
35da2e3ebdSchin * [0-9]{1,3}(\.[0-9]{1,3})*|0[xX][0-9a-fA-Z]+
36da2e3ebdSchin *
37da2e3ebdSchin * valid bits/masks match the egrep RE:
38da2e3ebdSchin *
39da2e3ebdSchin * (/([0-9]+|[0-9]{1,3}(\.[0-9]{1,3})*))?
40da2e3ebdSchin *
41da2e3ebdSchin * if pbits!=0 and no bits/mask specified then trailing 0's in addr
42da2e3ebdSchin * are used to compute the mask
43da2e3ebdSchin */
44da2e3ebdSchin
45da2e3ebdSchin int
strtoip4(register const char * s,char ** e,uint32_t * paddr,unsigned char * pbits)46da2e3ebdSchin strtoip4(register const char* s, char** e, uint32_t* paddr, unsigned char* pbits)
47da2e3ebdSchin {
48da2e3ebdSchin register int c;
49da2e3ebdSchin register unsigned int n;
50da2e3ebdSchin register uint32_t addr;
51da2e3ebdSchin register int part;
52da2e3ebdSchin register unsigned char bits;
53da2e3ebdSchin uint32_t z;
54da2e3ebdSchin int old;
55da2e3ebdSchin int r;
56da2e3ebdSchin const char* b;
57da2e3ebdSchin
58da2e3ebdSchin r = -1;
59da2e3ebdSchin while (isspace(*s))
60da2e3ebdSchin s++;
61da2e3ebdSchin b = s;
62da2e3ebdSchin addr = 0;
63da2e3ebdSchin bits = 0;
64da2e3ebdSchin part = 0;
65da2e3ebdSchin do
66da2e3ebdSchin {
67da2e3ebdSchin n = 0;
68da2e3ebdSchin while ((c = *s++) >= '0' && c <= '9')
69da2e3ebdSchin n = n * 10 + (c - '0');
70da2e3ebdSchin if ((c == 'x' || c == 'X') && !part)
71da2e3ebdSchin {
72da2e3ebdSchin addr = n;
73da2e3ebdSchin for (;;)
74da2e3ebdSchin {
75da2e3ebdSchin if ((c = *s++) >= '0' && c <= '9')
76da2e3ebdSchin c -= '0';
77da2e3ebdSchin else if (c >= 'a' && c <= 'f')
78da2e3ebdSchin c -= 'a' - 10;
79da2e3ebdSchin else if (c >= 'A' && c <= 'F')
80da2e3ebdSchin c -= 'F' - 10;
81da2e3ebdSchin else
82da2e3ebdSchin break;
83da2e3ebdSchin addr = addr * 16 + c;
84da2e3ebdSchin }
85da2e3ebdSchin part = 4;
86da2e3ebdSchin break;
87da2e3ebdSchin }
88da2e3ebdSchin if (n > 0xff)
89da2e3ebdSchin goto done;
90da2e3ebdSchin addr = (addr << 8) | n;
91da2e3ebdSchin part++;
92da2e3ebdSchin } while (c == '.');
93da2e3ebdSchin if ((s - b) == 1 && c != '/' || part > 4)
94da2e3ebdSchin goto done;
95da2e3ebdSchin if (old = part < 4)
96da2e3ebdSchin while (part++ < 4)
97da2e3ebdSchin addr <<= 8;
98da2e3ebdSchin if (pbits)
99da2e3ebdSchin {
100da2e3ebdSchin if (c == '/')
101da2e3ebdSchin {
102da2e3ebdSchin part = 0;
103da2e3ebdSchin z = 0;
104da2e3ebdSchin for (;;)
105da2e3ebdSchin {
106da2e3ebdSchin n = 0;
107da2e3ebdSchin while ((c = *s++) >= '0' && c <= '9')
108da2e3ebdSchin n = n * 10 + (c - '0');
109da2e3ebdSchin z = (z << 8) | n;
110da2e3ebdSchin part++;
111da2e3ebdSchin if (c != '.')
112da2e3ebdSchin break;
113da2e3ebdSchin old = 1;
114da2e3ebdSchin }
115da2e3ebdSchin if (part > 4)
116da2e3ebdSchin goto done;
117da2e3ebdSchin if (z <= 32 && (!old || part < 2))
118da2e3ebdSchin bits = z;
119da2e3ebdSchin else if (z)
120da2e3ebdSchin {
121da2e3ebdSchin if (part == 4 && (z & 0x8000001) == 1)
122da2e3ebdSchin z = ~z;
123da2e3ebdSchin while (!(z & 1))
124da2e3ebdSchin z >>= 1;
125da2e3ebdSchin while (z & 1)
126da2e3ebdSchin {
127da2e3ebdSchin z >>= 1;
128da2e3ebdSchin bits++;
129da2e3ebdSchin }
130da2e3ebdSchin }
131da2e3ebdSchin }
132da2e3ebdSchin else if ((z = (addr >> 24)) < 128)
133da2e3ebdSchin bits = 8;
134da2e3ebdSchin else if (z < 192)
135da2e3ebdSchin bits = 16;
136da2e3ebdSchin else
137da2e3ebdSchin bits = 24;
138da2e3ebdSchin if (*pbits = bits)
139da2e3ebdSchin addr &= ~((((uint32_t)1)<<(32-bits))-1);
140da2e3ebdSchin else
141da2e3ebdSchin addr = 0;
142da2e3ebdSchin }
143da2e3ebdSchin if (paddr)
144da2e3ebdSchin *paddr = addr;
145da2e3ebdSchin r = 0;
146da2e3ebdSchin done:
147da2e3ebdSchin if (e)
148da2e3ebdSchin *e = (char*)(s - 1);
149da2e3ebdSchin return r;
150da2e3ebdSchin }
151