1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
28 * convert string to 4 byte local byte order ip address
29 * with optional prefix bits
30 * pointer to first unused char placed in *e, even on error
31 * return 0:ok <0:error
33 * valid addresses match the egrep RE:
35 * [0-9]{1,3}(\.[0-9]{1,3})*|0[xX][0-9a-fA-Z]+
37 * valid bits/masks match the egrep RE:
39 * (/([0-9]+|[0-9]{1,3}(\.[0-9]{1,3})*))?
41 * if pbits!=0 and no bits/mask specified then trailing 0's in addr
42 * are used to compute the mask
46 strtoip4(register const char* s
, char** e
, uint32_t* paddr
, unsigned char* pbits
)
49 register unsigned int n
;
50 register uint32_t addr
;
52 register unsigned char bits
;
68 while ((c
= *s
++) >= '0' && c
<= '9')
69 n
= n
* 10 + (c
- '0');
70 if ((c
== 'x' || c
== 'X') && !part
)
75 if ((c
= *s
++) >= '0' && c
<= '9')
77 else if (c
>= 'a' && c
<= 'f')
79 else if (c
>= 'A' && c
<= 'F')
90 addr
= (addr
<< 8) | n
;
93 if ((s
- b
) == 1 && c
!= '/' || part
> 4)
107 while ((c
= *s
++) >= '0' && c
<= '9')
108 n
= n
* 10 + (c
- '0');
117 if (z
<= 32 && (!old
|| part
< 2))
121 if (part
== 4 && (z
& 0x8000001) == 1)
132 else if ((z
= (addr
>> 24)) < 128)
139 addr
&= ~((((uint32_t)1)<<(32-bits
))-1);