src/compat/inet_aton.c File Reference

#include "main.h"
#include "inet_aton.h"
#include <sys/types.h>
#include <sys/param.h>
#include <ctype.h>
Include dependency graph for inet_aton.c:

Go to the source code of this file.

Defines

#define inet_isascii(x)   1

Functions

int egg_inet_aton (char *cp, struct in_addr *addr) const

Define Documentation

#define inet_isascii (  )     1

Definition at line 28 of file inet_aton.c.

Referenced by egg_inet_aton().


Function Documentation

int egg_inet_aton ( char *  cp,
struct in_addr *  addr 
) const

Definition at line 104 of file inet_aton.c.

References egg_bzero, egg_isdigit, egg_islower, egg_isspace, egg_isxdigit, and inet_isascii.

00107 {
00108   static const u_32bit_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
00109   register u_32bit_t val;       /* changed from u_long --david */
00110   register int base;
00111   register int n;
00112   register char c;
00113   u_32bit_t parts[4];
00114   register u_32bit_t *pp = parts;
00115 
00116   egg_bzero(parts, sizeof(parts));
00117 
00118   c = *cp;
00119   for (;;) {
00120     /*
00121      * Collect number up to ``.''.
00122      * Values are specified as for C:
00123      * 0x=hex, 0=octal, isdigit=decimal.
00124      */
00125     if (!egg_isdigit(c))
00126       goto ret_0;
00127     base = 10;
00128     if (c == '0') {
00129       c = *++cp;
00130       if (c == 'x' || c == 'X')
00131         base = 16, c = *++cp;
00132       else
00133         base = 8;
00134     }
00135     val = 0;
00136     for (;;) {
00137       if (inet_isascii(c) && egg_isdigit(c)) {
00138         val = (val * base) + (c - '0');
00139         c = *++cp;
00140       } else if (base == 16 && inet_isascii(c) && egg_isxdigit(c)) {
00141         val = (val << 4) | (c + 10 - (egg_islower(c) ? 'a' : 'A'));
00142         c = *++cp;
00143       } else
00144         break;
00145     }
00146     if (c == '.') {
00147       /*
00148        * Internet format:
00149        *      a.b.c.d
00150        *      a.b.c   (with c treated as 16 bits)
00151        *      a.b     (with b treated as 24 bits)
00152        */
00153       if (pp >= parts + 3)
00154         goto ret_0;
00155       *pp++ = val;
00156       c = *++cp;
00157     } else
00158       break;
00159   }
00160   /*
00161    * Check for trailing characters.
00162    */
00163   if (c != '\0' && (!inet_isascii(c) || !egg_isspace(c)))
00164     goto ret_0;
00165   /*
00166    * Concoct the address according to
00167    * the number of parts specified.
00168    */
00169   n = pp - parts + 1;
00170 
00171   if (n == 0 ||                 /* initial nondigit */
00172       parts[0] > 0xff || parts[1] > 0xff || parts[2] > 0xff ||
00173       val > max[n - 1])
00174     goto ret_0;
00175 
00176   val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
00177 
00178   if (addr)
00179     addr->s_addr = htonl(val);
00180   return 1;
00181 
00182 ret_0:
00183   return 0;
00184 }


Generated on 7 Sep 2016 for Eggdrop by  doxygen 1.6.1