1 #include <stdio.h>
2 #include <netdev_ipaddr.h>
3 #include <lwip/sockets.h>
4 
5 #define ASSERT (void)
6 
7 /* Here for now until needed in other places in lwIP */
8 #ifndef isprint
9 #define in_range(c, lo, up)  ((uint8_t)c >= lo && (uint8_t)c <= up)
10 #define isprint(c)           in_range(c, 0x20, 0x7f)
11 #define isdigit(c)           in_range(c, '0', '9')
12 #define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
13 #define islower(c)           in_range(c, 'a', 'z')
14 #define isspace(c)           (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
15 #endif
16 
17 
18 /**
19  * Check whether "cp" is a valid ascii representation
20  * of an Internet address and convert to a binary address.
21  * Returns 1 if the address is valid, 0 if not.
22  * This replaces inet_addr, the return value from which
23  * cannot distinguish between failure and a local broadcast address.
24  *
25  * @param cp IP address in ascii representation (e.g. "127.0.0.1")
26  * @param addr pointer to which to save the ip address in network order
27  * @return 1 if cp could be converted to addr, 0 on failure
28  */
netdev_ip4addr_aton(const char * cp,ip4_addr_t * addr)29 int netdev_ip4addr_aton(const char *cp, ip4_addr_t *addr)
30 {
31     uint32_t val;
32     uint8_t base;
33     char c;
34     uint32_t parts[4];
35     uint32_t *pp = parts;
36 
37     c = *cp;
38     for (;;)
39     {
40         /*
41          * Collect number up to ``.''.
42          * Values are specified as for C:
43          * 0x=hex, 0=octal, 1-9=decimal.
44          */
45         if (!isdigit(c))
46         {
47             return 0;
48         }
49         val = 0;
50         base = 10;
51         if (c == '0')
52         {
53             c = *++cp;
54             if (c == 'x' || c == 'X')
55             {
56                 base = 16;
57                 c = *++cp;
58             }
59             else
60             {
61                 base = 8;
62             }
63         }
64         for (;;)
65         {
66             if (isdigit(c))
67             {
68                 val = (val * base) + (uint32_t) (c - '0');
69                 c = *++cp;
70             }
71             else if (base == 16 && isxdigit(c))
72             {
73                 val = (val << 4) | (uint32_t) (c + 10 - (islower(c) ? 'a' : 'A'));
74                 c = *++cp;
75             }
76             else
77             {
78                 break;
79             }
80         }
81         if (c == '.')
82         {
83             /*
84              * Internet format:
85              *  a.b.c.d
86              *  a.b.c   (with c treated as 16 bits)
87              *  a.b (with b treated as 24 bits)
88              */
89             if (pp >= parts + 3)
90             {
91                 return 0;
92             }
93             *pp++ = val;
94             c = *++cp;
95         }
96         else
97         {
98             break;
99         }
100     }
101     /*
102      * Check for trailing characters.
103      */
104     if (c != '\0' && !isspace(c))
105     {
106         return 0;
107     }
108     /*
109      * Concoct the address according to
110      * the number of parts specified.
111      */
112     switch (pp - parts + 1)
113     {
114 
115     case 0:
116         return 0; /* initial nondigit */
117 
118     case 1: /* a -- 32 bits */
119         break;
120 
121     case 2: /* a.b -- 8.24 bits */
122         if (val > 0xffffffUL)
123         {
124             return 0;
125         }
126         if (parts[0] > 0xff)
127         {
128             return 0;
129         }
130         val |= parts[0] << 24;
131         break;
132 
133     case 3: /* a.b.c -- 8.8.16 bits */
134         if (val > 0xffff)
135         {
136             return 0;
137         }
138         if ((parts[0] > 0xff) || (parts[1] > 0xff))
139         {
140             return 0;
141         }
142         val |= (parts[0] << 24) | (parts[1] << 16);
143         break;
144 
145     case 4: /* a.b.c.d -- 8.8.8.8 bits */
146         if (val > 0xff)
147         {
148             return 0;
149         }
150         if ((parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
151         {
152             return 0;
153         }
154         val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
155         break;
156     default:
157         ASSERT(0);
158         break;
159     }
160     if (addr)
161     {
162         ip4_addr_set_u32(addr, htonl(val));
163     }
164     return 1;
165 }
166 
167 /**
168  * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
169  *
170  * @param addr ip address in network order to convert
171  * @param buf target buffer where the string is stored
172  * @param buflen length of buf
173  * @return either pointer to buf which now holds the ASCII
174  *         representation of addr or NULL if buf was too small
175  */
netdev_ip4addr_ntoa_r(const ip4_addr_t * addr,char * buf,int buflen)176 char *netdev_ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
177 {
178     uint32_t s_addr;
179     char inv[3];
180     char *rp;
181     uint8_t *ap;
182     uint8_t rem;
183     uint8_t n;
184     uint8_t i;
185     int len = 0;
186 
187     s_addr = ip4_addr_get_u32(addr);
188 
189     rp = buf;
190     ap = (uint8_t *) &s_addr;
191     for (n = 0; n < 4; n++)
192     {
193         i = 0;
194         do
195         {
196             rem = *ap % (uint8_t) 10;
197             *ap /= (uint8_t) 10;
198             inv[i++] = (char) ('0' + rem);
199         } while (*ap);
200         while (i--)
201         {
202             if (len++ >= buflen)
203             {
204                 return NULL;
205             }
206             *rp++ = inv[i];
207         }
208         if (len++ >= buflen)
209         {
210             return NULL;
211         }
212         *rp++ = '.';
213         ap++;
214     }
215     *--rp = 0;
216     return buf;
217 }
218 
219 
220 /**
221  * Convert numeric IP address into decimal dotted ASCII representation.
222  * returns ptr to static buffer; not reentrant!
223  *
224  * @param addr ip address in network order to convert
225  * @return pointer to a global static (!) buffer that holds the ASCII
226  *         representation of addr
227  */
netdev_ip4addr_ntoa(const ip4_addr_t * addr)228 char *netdev_ip4addr_ntoa(const ip4_addr_t *addr)
229 {
230     static char str[IP4ADDR_STRLEN_MAX];
231     return netdev_ip4addr_ntoa_r(addr, str, IP4ADDR_STRLEN_MAX);
232 }
233 
234 
235 /**
236  * Ascii internet address interpretation routine.
237  * The value returned is in network order.
238  *
239  * @param cp IP address in ascii representation (e.g. "127.0.0.1")
240  * @return ip address in network order
241  */
netdev_ipaddr_addr(const char * cp)242 in_addr_t netdev_ipaddr_addr(const char *cp)
243 {
244     ip4_addr_t val;
245 
246     if (netdev_ip4addr_aton(cp, &val)) {
247         return ip4_addr_get_u32(&val);
248     }
249     return (IPADDR_NONE);
250 }
251