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_pton.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) 2004 by Internet Systems Consortium, Inc. ("ISC")
    3  * Copyright (c) 1996,1999 by Internet Software Consortium.
    4  *
    5  * Permission to use, copy, modify, and distribute this software for any
    6  * purpose with or without fee is hereby granted, provided that the above
    7  * copyright notice and this permission notice appear in all copies.
    8  *
    9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
   10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
   12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
   15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   16  */
   17 
   18 #if defined(LIBC_SCCS) && !defined(lint)
   19 static const char rcsid[] = "$Id: inet_pton.c,v 1.3.18.2 2005/07/28 07:38:07 marka Exp $";
   20 #endif /* LIBC_SCCS and not lint */
   21 #include <sys/cdefs.h>
   22 __FBSDID("$FreeBSD$");
   23 
   24 #include <sys/param.h>
   25 #include <sys/socket.h>
   26 #include <sys/systm.h>
   27 
   28 #include <netinet/in.h>
   29 
   30 /*%
   31  * WARNING: Don't even consider trying to compile this on a system where
   32  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
   33  */
   34 
   35 static int      inet_pton4(const char *src, u_char *dst);
   36 static int      inet_pton6(const char *src, u_char *dst);
   37 
   38 /* int
   39  * inet_pton(af, src, dst)
   40  *      convert from presentation format (which usually means ASCII printable)
   41  *      to network format (which is usually some kind of binary format).
   42  * return:
   43  *      1 if the address was valid for the specified address family
   44  *      0 if the address wasn't valid (`dst' is untouched in this case)
   45  *      -1 if some other error occurred (`dst' is untouched in this case, too)
   46  * author:
   47  *      Paul Vixie, 1996.
   48  */
   49 int
   50 inet_pton(int af, const char *src, void *dst)
   51 {
   52         switch (af) {
   53         case AF_INET:
   54                 return (inet_pton4(src, dst));
   55         case AF_INET6:
   56                 return (inet_pton6(src, dst));
   57         default:
   58                 return (-1);
   59         }
   60         /* NOTREACHED */
   61 }
   62 
   63 /* int
   64  * inet_pton4(src, dst)
   65  *      like inet_aton() but without all the hexadecimal and shorthand.
   66  * return:
   67  *      1 if `src' is a valid dotted quad, else 0.
   68  * notice:
   69  *      does not touch `dst' unless it's returning 1.
   70  * author:
   71  *      Paul Vixie, 1996.
   72  */
   73 static int
   74 inet_pton4(const char *src, u_char *dst)
   75 {
   76         static const char digits[] = "0123456789";
   77         int saw_digit, octets, ch;
   78 #define NS_INADDRSZ     4
   79         u_char tmp[NS_INADDRSZ], *tp;
   80 
   81         saw_digit = 0;
   82         octets = 0;
   83         *(tp = tmp) = 0;
   84         while ((ch = *src++) != '\0') {
   85                 const char *pch;
   86 
   87                 if ((pch = strchr(digits, ch)) != NULL) {
   88                         u_int new = *tp * 10 + (pch - digits);
   89 
   90                         if (saw_digit && *tp == 0)
   91                                 return (0);
   92                         if (new > 255)
   93                                 return (0);
   94                         *tp = new;
   95                         if (!saw_digit) {
   96                                 if (++octets > 4)
   97                                         return (0);
   98                                 saw_digit = 1;
   99                         }
  100                 } else if (ch == '.' && saw_digit) {
  101                         if (octets == 4)
  102                                 return (0);
  103                         *++tp = 0;
  104                         saw_digit = 0;
  105                 } else
  106                         return (0);
  107         }
  108         if (octets < 4)
  109                 return (0);
  110         memcpy(dst, tmp, NS_INADDRSZ);
  111         return (1);
  112 }
  113 
  114 /* int
  115  * inet_pton6(src, dst)
  116  *      convert presentation level address to network order binary form.
  117  * return:
  118  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
  119  * notice:
  120  *      (1) does not touch `dst' unless it's returning 1.
  121  *      (2) :: in a full address is silently ignored.
  122  * credit:
  123  *      inspired by Mark Andrews.
  124  * author:
  125  *      Paul Vixie, 1996.
  126  */
  127 static int
  128 inet_pton6(const char *src, u_char *dst)
  129 {
  130         static const char xdigits_l[] = "0123456789abcdef",
  131                           xdigits_u[] = "0123456789ABCDEF";
  132 #define NS_IN6ADDRSZ    16
  133 #define NS_INT16SZ      2
  134         u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  135         const char *xdigits, *curtok;
  136         int ch, seen_xdigits;
  137         u_int val;
  138 
  139         memset((tp = tmp), '\0', NS_IN6ADDRSZ);
  140         endp = tp + NS_IN6ADDRSZ;
  141         colonp = NULL;
  142         /* Leading :: requires some special handling. */
  143         if (*src == ':')
  144                 if (*++src != ':')
  145                         return (0);
  146         curtok = src;
  147         seen_xdigits = 0;
  148         val = 0;
  149         while ((ch = *src++) != '\0') {
  150                 const char *pch;
  151 
  152                 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  153                         pch = strchr((xdigits = xdigits_u), ch);
  154                 if (pch != NULL) {
  155                         val <<= 4;
  156                         val |= (pch - xdigits);
  157                         if (++seen_xdigits > 4)
  158                                 return (0);
  159                         continue;
  160                 }
  161                 if (ch == ':') {
  162                         curtok = src;
  163                         if (!seen_xdigits) {
  164                                 if (colonp)
  165                                         return (0);
  166                                 colonp = tp;
  167                                 continue;
  168                         } else if (*src == '\0') {
  169                                 return (0);
  170                         }
  171                         if (tp + NS_INT16SZ > endp)
  172                                 return (0);
  173                         *tp++ = (u_char) (val >> 8) & 0xff;
  174                         *tp++ = (u_char) val & 0xff;
  175                         seen_xdigits = 0;
  176                         val = 0;
  177                         continue;
  178                 }
  179                 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  180                     inet_pton4(curtok, tp) > 0) {
  181                         tp += NS_INADDRSZ;
  182                         seen_xdigits = 0;
  183                         break;  /*%< '\\' was seen by inet_pton4(). */
  184                 }
  185                 return (0);
  186         }
  187         if (seen_xdigits) {
  188                 if (tp + NS_INT16SZ > endp)
  189                         return (0);
  190                 *tp++ = (u_char) (val >> 8) & 0xff;
  191                 *tp++ = (u_char) val & 0xff;
  192         }
  193         if (colonp != NULL) {
  194                 /*
  195                  * Since some memmove()'s erroneously fail to handle
  196                  * overlapping regions, we'll do the shift by hand.
  197                  */
  198                 const int n = tp - colonp;
  199                 int i;
  200 
  201                 if (tp == endp)
  202                         return (0);
  203                 for (i = 1; i <= n; i++) {
  204                         endp[- i] = colonp[n - i];
  205                         colonp[n - i] = 0;
  206                 }
  207                 tp = endp;
  208         }
  209         if (tp != endp)
  210                 return (0);
  211         memcpy(dst, tmp, NS_IN6ADDRSZ);
  212         return (1);
  213 }
  214 
  215 /*! \file */

Cache object: 2ef8192e87641aeb2f650fd7cc171c31


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