]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/lib/lwip/lib/contrib/src/core/ipv4/ip_addr.c
update
[l4.git] / l4 / pkg / ankh / lib / lwip / lib / contrib / src / core / ipv4 / ip_addr.c
1 /**
2  * @file
3  * This is the IPv4 address tools implementation.
4  *
5  */
6
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved. 
10  * 
11  * Redistribution and use in source and binary forms, with or without modification, 
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission. 
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  * 
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38
39 #include "lwip/opt.h"
40 #include "lwip/ip_addr.h"
41 #include "lwip/netif.h"
42
43 /* used by IP_ADDR_ANY and IP_ADDR_BROADCAST in ip_addr.h */
44 const ip_addr_t ip_addr_any = { IPADDR_ANY };
45 const ip_addr_t ip_addr_broadcast = { IPADDR_BROADCAST };
46
47 /**
48  * Determine if an address is a broadcast address on a network interface 
49  * 
50  * @param addr address to be checked
51  * @param netif the network interface against which the address is checked
52  * @return returns non-zero if the address is a broadcast address
53  */
54 u8_t ip_addr_isbroadcast(const ip_addr_t *addr, const struct netif *netif)
55 {
56   u32_t addr2test;
57
58   addr2test = ip4_addr_get_u32(addr);
59   /* all ones (broadcast) or all zeroes (old skool broadcast) */
60   if ((~addr2test == IPADDR_ANY) ||
61       (addr2test == IPADDR_ANY))
62     return 1;
63   /* no broadcast support on this network interface? */
64   else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0)
65     /* the given address cannot be a broadcast address
66      * nor can we check against any broadcast addresses */
67     return 0;
68   /* address matches network interface address exactly? => no broadcast */
69   else if (addr2test == ip4_addr_get_u32(&netif->ip_addr))
70     return 0;
71   /*  on the same (sub) network... */
72   else if (ip_addr_netcmp(addr, &(netif->ip_addr), &(netif->netmask))
73          /* ...and host identifier bits are all ones? =>... */
74           && ((addr2test & ~ip4_addr_get_u32(&netif->netmask)) ==
75            (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask))))
76     /* => network broadcast address */
77     return 1;
78   else
79     return 0;
80 }
81
82 /* Here for now until needed in other places in lwIP */
83 #ifndef isprint
84 #define in_range(c, lo, up)  ((u8_t)c >= lo && (u8_t)c <= up)
85 #define isprint(c)           in_range(c, 0x20, 0x7f)
86 #define isdigit(c)           in_range(c, '0', '9')
87 #define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
88 #define islower(c)           in_range(c, 'a', 'z')
89 #define isspace(c)           (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
90 #endif    
91     
92 /**
93  * Ascii internet address interpretation routine.
94  * The value returned is in network order.
95  *
96  * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
97  * @return ip address in network order
98  */
99 u32_t
100 ipaddr_addr(const char *cp)
101 {
102   ip_addr_t val;
103
104   if (ipaddr_aton(cp, &val)) {
105     return ip4_addr_get_u32(&val);
106   }
107   return (IPADDR_NONE);
108 }
109
110 /**
111  * Check whether "cp" is a valid ascii representation
112  * of an Internet address and convert to a binary address.
113  * Returns 1 if the address is valid, 0 if not.
114  * This replaces inet_addr, the return value from which
115  * cannot distinguish between failure and a local broadcast address.
116  *
117  * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
118  * @param addr pointer to which to save the ip address in network order
119  * @return 1 if cp could be converted to addr, 0 on failure
120  */
121 int
122 ipaddr_aton(const char *cp, ip_addr_t *addr)
123 {
124   u32_t val;
125   u8_t base;
126   char c;
127   u32_t parts[4];
128   u32_t *pp = parts;
129
130   c = *cp;
131   for (;;) {
132     /*
133      * Collect number up to ``.''.
134      * Values are specified as for C:
135      * 0x=hex, 0=octal, 1-9=decimal.
136      */
137     if (!isdigit(c))
138       return (0);
139     val = 0;
140     base = 10;
141     if (c == '0') {
142       c = *++cp;
143       if (c == 'x' || c == 'X') {
144         base = 16;
145         c = *++cp;
146       } else
147         base = 8;
148     }
149     for (;;) {
150       if (isdigit(c)) {
151         val = (val * base) + (int)(c - '0');
152         c = *++cp;
153       } else if (base == 16 && isxdigit(c)) {
154         val = (val << 4) | (int)(c + 10 - (islower(c) ? 'a' : 'A'));
155         c = *++cp;
156       } else
157         break;
158     }
159     if (c == '.') {
160       /*
161        * Internet format:
162        *  a.b.c.d
163        *  a.b.c   (with c treated as 16 bits)
164        *  a.b (with b treated as 24 bits)
165        */
166       if (pp >= parts + 3) {
167         return (0);
168       }
169       *pp++ = val;
170       c = *++cp;
171     } else
172       break;
173   }
174   /*
175    * Check for trailing characters.
176    */
177   if (c != '\0' && !isspace(c)) {
178     return (0);
179   }
180   /*
181    * Concoct the address according to
182    * the number of parts specified.
183    */
184   switch (pp - parts + 1) {
185
186   case 0:
187     return (0);       /* initial nondigit */
188
189   case 1:             /* a -- 32 bits */
190     break;
191
192   case 2:             /* a.b -- 8.24 bits */
193     if (val > 0xffffffUL) {
194       return (0);
195     }
196     val |= parts[0] << 24;
197     break;
198
199   case 3:             /* a.b.c -- 8.8.16 bits */
200     if (val > 0xffff) {
201       return (0);
202     }
203     val |= (parts[0] << 24) | (parts[1] << 16);
204     break;
205
206   case 4:             /* a.b.c.d -- 8.8.8.8 bits */
207     if (val > 0xff) {
208       return (0);
209     }
210     val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
211     break;
212   default:
213     LWIP_ASSERT("unhandled", 0);
214     break;
215   }
216   if (addr) {
217     ip4_addr_set_u32(addr, htonl(val));
218   }
219   return (1);
220 }
221
222 /**
223  * Convert numeric IP address into decimal dotted ASCII representation.
224  * returns ptr to static buffer; not reentrant!
225  *
226  * @param addr ip address in network order to convert
227  * @return pointer to a global static (!) buffer that holds the ASCII
228  *         represenation of addr
229  */
230 char *
231 ipaddr_ntoa(const ip_addr_t *addr)
232 {
233   static char str[16];
234   return ipaddr_ntoa_r(addr, str, 16);
235 }
236
237 /**
238  * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
239  *
240  * @param addr ip address in network order to convert
241  * @param buf target buffer where the string is stored
242  * @param buflen length of buf
243  * @return either pointer to buf which now holds the ASCII
244  *         representation of addr or NULL if buf was too small
245  */
246 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
247 {
248   u32_t s_addr;
249   char inv[3];
250   char *rp;
251   u8_t *ap;
252   u8_t rem;
253   u8_t n;
254   u8_t i;
255   int len = 0;
256
257   s_addr = ip4_addr_get_u32(addr);
258
259   rp = buf;
260   ap = (u8_t *)&s_addr;
261   for(n = 0; n < 4; n++) {
262     i = 0;
263     do {
264       rem = *ap % (u8_t)10;
265       *ap /= (u8_t)10;
266       inv[i++] = '0' + rem;
267     } while(*ap);
268     while(i--) {
269       if (len++ >= buflen) {
270         return NULL;
271       }
272       *rp++ = inv[i];
273     }
274     if (len++ >= buflen) {
275       return NULL;
276     }
277     *rp++ = '.';
278     ap++;
279   }
280   *--rp = 0;
281   return buf;
282 }