The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/net/inet_aton.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2001 Charles Mott <cm@linktel.net>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 #include <sys/param.h>
   27 #include <sys/systm.h>
   28 
   29 #include <netinet/in.h>
   30 
   31 /* XXX ctype.h is missing, see libkern/stdio/scanf.c */
   32 #if 1
   33 static inline int
   34 isspace(char c)
   35 {
   36         return c == ' ' || c == '\t' || c == '\n' || c == '\12';
   37 }
   38 #endif
   39 
   40 int
   41 inet_aton(const char *cp, struct in_addr *addr)
   42 {
   43         u_long parts[4];
   44         in_addr_t val = 0;
   45         const char *c;
   46         char *endptr;
   47         int gotend, n;
   48 
   49         c = (const char *)cp;
   50         n = 0;
   51 
   52         /*
   53          * Run through the string, grabbing numbers until
   54          * the end of the string, or some error
   55          */
   56         gotend = 0;
   57         while (!gotend) {
   58                 unsigned long l;
   59 
   60                 l = strtoul(c, &endptr, 0);
   61 
   62                 if (l == ULONG_MAX || (l == 0 && endptr == c)) {
   63                         return 0;
   64                 }
   65 
   66                 val = (in_addr_t)l;
   67 
   68                 /*
   69                  * If the whole string is invalid, endptr will equal
   70                  * c.. this way we can make sure someone hasn't
   71                  * gone '.12' or something which would get past
   72                  * the next check.
   73                  */
   74                 if (endptr == c) {
   75                         return 0;
   76                 }
   77                 parts[n] = val;
   78                 c = endptr;
   79 
   80                 /* Check the next character past the previous number's end */
   81                 switch (*c) {
   82                 case '.':
   83 
   84                         /* Make sure we only do 3 dots .. */
   85                         if (n == 3) {   /* Whoops. Quit. */
   86                                 return 0;
   87                         }
   88                         n++;
   89                         c++;
   90                         break;
   91 
   92                 case '\0':
   93                         gotend = 1;
   94                         break;
   95 
   96                 default:
   97                         if (isspace((unsigned char)*c)) {
   98                                 gotend = 1;
   99                                 break;
  100                         } else {
  101                                 /* Invalid character, then fail. */
  102                                 return 0;
  103                         }
  104                 }
  105         }
  106 
  107         /* Concoct the address according to the number of parts specified. */
  108         switch (n) {
  109         case 0:                         /* a -- 32 bits */
  110 
  111                 /*
  112                  * Nothing is necessary here.  Overflow checking was
  113                  * already done in strtoul().
  114                  */
  115                 break;
  116         case 1:                         /* a.b -- 8.24 bits */
  117                 if (val > 0xffffff || parts[0] > 0xff) {
  118                         return 0;
  119                 }
  120                 val |= parts[0] << 24;
  121                 break;
  122 
  123         case 2:                         /* a.b.c -- 8.8.16 bits */
  124                 if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff) {
  125                         return 0;
  126                 }
  127                 val |= (parts[0] << 24) | (parts[1] << 16);
  128                 break;
  129 
  130         case 3:                         /* a.b.c.d -- 8.8.8.8 bits */
  131                 if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff ||
  132                     parts[2] > 0xff) {
  133                         return 0;
  134                 }
  135                 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  136                 break;
  137         }
  138 
  139         if (addr != NULL) {
  140                 addr->s_addr = htonl(val);
  141         }
  142         return 1;
  143 }

Cache object: 4517443c88ad375a786a1fd5182e7a96


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.