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/inet_aton.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/10.2/sys/libkern/inet_aton.c 199208 2009-11-12 00:46:28Z attilio $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/ctype.h>
   32 #include <sys/limits.h>
   33 #include <sys/systm.h>
   34 
   35 #include <netinet/in.h>
   36 
   37 int
   38 inet_aton(const char *cp, struct in_addr *addr)
   39 {
   40         u_long parts[4];
   41         in_addr_t val;
   42         const char *c;
   43         char *endptr;
   44         int gotend, n;
   45 
   46         c = (const char *)cp;
   47         n = 0;
   48 
   49         /*
   50          * Run through the string, grabbing numbers until
   51          * the end of the string, or some error
   52          */
   53         gotend = 0;
   54         while (!gotend) {
   55                 unsigned long l;
   56 
   57                 l = strtoul(c, &endptr, 0);
   58 
   59                 if (l == ULONG_MAX || (l == 0 && endptr == c))
   60                         return (0);
   61 
   62                 val = (in_addr_t)l;
   63 
   64                 /*
   65                  * If the whole string is invalid, endptr will equal
   66                  * c.. this way we can make sure someone hasn't
   67                  * gone '.12' or something which would get past
   68                  * the next check.
   69                  */
   70                 if (endptr == c)
   71                         return (0);
   72                 parts[n] = val;
   73                 c = endptr;
   74 
   75                 /* Check the next character past the previous number's end */
   76                 switch (*c) {
   77                 case '.' :
   78 
   79                         /* Make sure we only do 3 dots .. */
   80                         if (n == 3)     /* Whoops. Quit. */
   81                                 return (0);
   82                         n++;
   83                         c++;
   84                         break;
   85 
   86                 case '\0':
   87                         gotend = 1;
   88                         break;
   89 
   90                 default:
   91                         if (isspace((unsigned char)*c)) {
   92                                 gotend = 1;
   93                                 break;
   94                         } else {
   95 
   96                                 /* Invalid character, then fail. */
   97                                 return (0);
   98                         }
   99                 }
  100 
  101         }
  102 
  103         /* Concoct the address according to the number of parts specified. */
  104         switch (n) {
  105         case 0:                         /* a -- 32 bits */
  106 
  107                 /*
  108                  * Nothing is necessary here.  Overflow checking was
  109                  * already done in strtoul().
  110                  */
  111                 break;
  112         case 1:                         /* a.b -- 8.24 bits */
  113                 if (val > 0xffffff || parts[0] > 0xff)
  114                         return (0);
  115                 val |= parts[0] << 24;
  116                 break;
  117 
  118         case 2:                         /* a.b.c -- 8.8.16 bits */
  119                 if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff)
  120                         return (0);
  121                 val |= (parts[0] << 24) | (parts[1] << 16);
  122                 break;
  123 
  124         case 3:                         /* a.b.c.d -- 8.8.8.8 bits */
  125                 if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff ||
  126                     parts[2] > 0xff)
  127                         return (0);
  128                 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  129                 break;
  130         }
  131 
  132         if (addr != NULL)
  133                 addr->s_addr = htonl(val);
  134         return (1);
  135 }
  136 

Cache object: 2a7d2f0a6ba53462172d521181101e62


[ 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.