/[lwip]/lwip/src/core/inet.c
ViewVC logotype

Diff of /lwip/src/core/inet.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9.2.1 by likewise, Wed May 14 14:38:28 2003 UTC revision 1.9.2.2 by likewise, Wed Jun 4 10:30:00 2003 UTC
# Line 168  inet_chksum_pbuf(struct pbuf *p) Line 168  inet_chksum_pbuf(struct pbuf *p)
168    return ~(acc & 0xffffUL);    return ~(acc & 0xffffUL);
169  }  }
170    
171    /*-----------------------------------------------------------------------------------*/
172     /*
173      * Ascii internet address interpretation routine.
174      * The value returned is in network order.
175      */
176    
177     /*  */
178     /* inet_addr */
179     u32_t inet_addr(const char *cp)
180     {
181         struct in_addr val;
182    
183         if (inet_aton(cp, &val)) {
184             return (val.s_addr);
185         }
186         return (INADDR_NONE);
187     }
188    
189     /*
190      * Check whether "cp" is a valid ascii representation
191      * of an Internet address and convert to a binary address.
192      * Returns 1 if the address is valid, 0 if not.
193      * This replaces inet_addr, the return value from which
194      * cannot distinguish between failure and a local broadcast address.
195      */
196     /*  */
197     /* inet_aton */
198     int inet_aton(const char *cp, struct in_addr *addr)
199     {
200         u32_t val;
201         int base, n;
202         char c;
203         u32_t parts[4];
204         u32_t* pp = parts;
205    
206         c = *cp;
207         for (;;) {
208             /*
209              * Collect number up to ``.''.
210              * Values are specified as for C:
211              * 0x=hex, 0=octal, isdigit=decimal.
212              */
213             if (!isdigit(c))
214                 return (0);
215             val = 0; base = 10;
216             if (c == '0') {
217                 c = *++cp;
218                 if (c == 'x' || c == 'X')
219                     base = 16, c = *++cp;
220                 else
221                     base = 8;
222             }
223             for (;;) {
224                 if (isascii(c) && isdigit(c)) {
225                     val = (val * base) + (c - '0');
226                     c = *++cp;
227                 } else if (base == 16 && isascii(c) && isxdigit(c)) {
228                     val = (val << 4) |
229                         (c + 10 - (islower(c) ? 'a' : 'A'));
230                     c = *++cp;
231                 } else
232                 break;
233             }
234             if (c == '.') {
235                 /*
236                  * Internet format:
237                  *  a.b.c.d
238                  *  a.b.c   (with c treated as 16 bits)
239                  *  a.b (with b treated as 24 bits)
240                  */
241                 if (pp >= parts + 3)
242                     return (0);
243                 *pp++ = val;
244                 c = *++cp;
245             } else
246                 break;
247         }
248         /*
249          * Check for trailing characters.
250          */
251         if (c != '\0' && (!isascii(c) || !isspace(c)))
252             return (0);
253         /*
254          * Concoct the address according to
255          * the number of parts specified.
256          */
257         n = pp - parts + 1;
258         switch (n) {
259    
260         case 0:
261             return (0);     /* initial nondigit */
262    
263         case 1:             /* a -- 32 bits */
264             break;
265    
266         case 2:             /* a.b -- 8.24 bits */
267             if (val > 0xffffff)
268                 return (0);
269             val |= parts[0] << 24;
270             break;
271    
272         case 3:             /* a.b.c -- 8.8.16 bits */
273             if (val > 0xffff)
274                 return (0);
275             val |= (parts[0] << 24) | (parts[1] << 16);
276             break;
277    
278         case 4:             /* a.b.c.d -- 8.8.8.8 bits */
279             if (val > 0xff)
280                 return (0);
281             val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
282             break;
283         }
284         if (addr)
285             addr->s_addr = htonl(val);
286         return (1);
287     }
288    
289    /* Convert numeric IP address into decimal dotted ASCII representation.
290     * returns ptr to static buffer; not reentrant!
291     */
292    u8_t *inet_ntoa(u32_t addr)
293    {
294            static u8_t str[16];
295            u8_t inv[3];
296            u8_t *rp;
297            u8_t *ap;
298            u8_t rem;
299            u8_t n;
300            u8_t i;
301    
302            rp = str;
303            ap = (u8_t *)&addr;
304            for(n = 0; n < 4; n++) {
305                    i = 0;
306                    do {
307                            rem = *ap % (u8_t)10;
308                            *ap /= (u8_t)10;
309                            inv[i++] = '0' + rem;
310                    } while(*ap);
311                    while(i--)
312                            *rp++ = inv[i];
313                    *rp++ = '.';
314                    ap++;
315            }
316            *--rp = 0;
317            return str;
318    }
319    
320    /*-----------------------------------------------------------------------------------*/
321  #ifndef BYTE_ORDER  #ifndef BYTE_ORDER
322  #error BYTE_ORDER is not defined  #error BYTE_ORDER is not defined
323  #endif  #endif

Legend:
Removed from v.1.9.2.1  
changed lines
  Added in v.1.9.2.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26