inet_pton.c
Go to the documentation of this file.
00001 /* This is from the BIND 4.9.4 release, modified to compile by itself */
00002 
00003 /* Copyright (c) 1996 by Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00010  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00011  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00012  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00013  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00014  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00015  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00016  * SOFTWARE.
00017  */
00018 
00019 #include "curl_setup.h"
00020 
00021 #ifndef HAVE_INET_PTON
00022 
00023 #ifdef HAVE_SYS_PARAM_H
00024 #include <sys/param.h>
00025 #endif
00026 #ifdef HAVE_NETINET_IN_H
00027 #include <netinet/in.h>
00028 #endif
00029 #ifdef HAVE_ARPA_INET_H
00030 #include <arpa/inet.h>
00031 #endif
00032 
00033 #include "inet_pton.h"
00034 
00035 #define IN6ADDRSZ       16
00036 #define INADDRSZ         4
00037 #define INT16SZ          2
00038 
00039 /*
00040  * WARNING: Don't even consider trying to compile this on a system where
00041  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
00042  */
00043 
00044 static int      inet_pton4(const char *src, unsigned char *dst);
00045 #ifdef ENABLE_IPV6
00046 static int      inet_pton6(const char *src, unsigned char *dst);
00047 #endif
00048 
00049 /* int
00050  * inet_pton(af, src, dst)
00051  *      convert from presentation format (which usually means ASCII printable)
00052  *      to network format (which is usually some kind of binary format).
00053  * return:
00054  *      1 if the address was valid for the specified address family
00055  *      0 if the address wasn't valid (`dst' is untouched in this case)
00056  *      -1 if some other error occurred (`dst' is untouched in this case, too)
00057  * notice:
00058  *      On Windows we store the error in the thread errno, not
00059  *      in the winsock error code. This is to avoid losing the
00060  *      actual last winsock error. So use macro ERRNO to fetch the
00061  *      errno this function sets when returning (-1), not SOCKERRNO.
00062  * author:
00063  *      Paul Vixie, 1996.
00064  */
00065 int
00066 Curl_inet_pton(int af, const char *src, void *dst)
00067 {
00068   switch(af) {
00069   case AF_INET:
00070     return (inet_pton4(src, (unsigned char *)dst));
00071 #ifdef ENABLE_IPV6
00072   case AF_INET6:
00073     return (inet_pton6(src, (unsigned char *)dst));
00074 #endif
00075   default:
00076     SET_ERRNO(EAFNOSUPPORT);
00077     return (-1);
00078   }
00079   /* NOTREACHED */
00080 }
00081 
00082 /* int
00083  * inet_pton4(src, dst)
00084  *      like inet_aton() but without all the hexadecimal and shorthand.
00085  * return:
00086  *      1 if `src' is a valid dotted quad, else 0.
00087  * notice:
00088  *      does not touch `dst' unless it's returning 1.
00089  * author:
00090  *      Paul Vixie, 1996.
00091  */
00092 static int
00093 inet_pton4(const char *src, unsigned char *dst)
00094 {
00095   static const char digits[] = "0123456789";
00096   int saw_digit, octets, ch;
00097   unsigned char tmp[INADDRSZ], *tp;
00098 
00099   saw_digit = 0;
00100   octets = 0;
00101   tp = tmp;
00102   *tp = 0;
00103   while((ch = *src++) != '\0') {
00104     const char *pch;
00105 
00106     pch = strchr(digits, ch);
00107     if(pch) {
00108       unsigned int val = *tp * 10 + (unsigned int)(pch - digits);
00109 
00110       if(saw_digit && *tp == 0)
00111         return (0);
00112       if(val > 255)
00113         return (0);
00114       *tp = (unsigned char)val;
00115       if(! saw_digit) {
00116         if(++octets > 4)
00117           return (0);
00118         saw_digit = 1;
00119       }
00120     }
00121     else if(ch == '.' && saw_digit) {
00122       if(octets == 4)
00123         return (0);
00124       *++tp = 0;
00125       saw_digit = 0;
00126     }
00127     else
00128       return (0);
00129   }
00130   if(octets < 4)
00131     return (0);
00132   memcpy(dst, tmp, INADDRSZ);
00133   return (1);
00134 }
00135 
00136 #ifdef ENABLE_IPV6
00137 /* int
00138  * inet_pton6(src, dst)
00139  *      convert presentation level address to network order binary form.
00140  * return:
00141  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
00142  * notice:
00143  *      (1) does not touch `dst' unless it's returning 1.
00144  *      (2) :: in a full address is silently ignored.
00145  * credit:
00146  *      inspired by Mark Andrews.
00147  * author:
00148  *      Paul Vixie, 1996.
00149  */
00150 static int
00151 inet_pton6(const char *src, unsigned char *dst)
00152 {
00153   static const char xdigits_l[] = "0123456789abcdef",
00154     xdigits_u[] = "0123456789ABCDEF";
00155   unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
00156   const char *xdigits, *curtok;
00157   int ch, saw_xdigit;
00158   size_t val;
00159 
00160   memset((tp = tmp), 0, IN6ADDRSZ);
00161   endp = tp + IN6ADDRSZ;
00162   colonp = NULL;
00163   /* Leading :: requires some special handling. */
00164   if(*src == ':')
00165     if(*++src != ':')
00166       return (0);
00167   curtok = src;
00168   saw_xdigit = 0;
00169   val = 0;
00170   while((ch = *src++) != '\0') {
00171     const char *pch;
00172 
00173     pch = strchr((xdigits = xdigits_l), ch);
00174     if(!pch)
00175       pch = strchr((xdigits = xdigits_u), ch);
00176     if(pch != NULL) {
00177       val <<= 4;
00178       val |= (pch - xdigits);
00179       if(++saw_xdigit > 4)
00180         return (0);
00181       continue;
00182     }
00183     if(ch == ':') {
00184       curtok = src;
00185       if(!saw_xdigit) {
00186         if(colonp)
00187           return (0);
00188         colonp = tp;
00189         continue;
00190       }
00191       if(tp + INT16SZ > endp)
00192         return (0);
00193       *tp++ = (unsigned char) ((val >> 8) & 0xff);
00194       *tp++ = (unsigned char) (val & 0xff);
00195       saw_xdigit = 0;
00196       val = 0;
00197       continue;
00198     }
00199     if(ch == '.' && ((tp + INADDRSZ) <= endp) &&
00200         inet_pton4(curtok, tp) > 0) {
00201       tp += INADDRSZ;
00202       saw_xdigit = 0;
00203       break;    /* '\0' was seen by inet_pton4(). */
00204     }
00205     return (0);
00206   }
00207   if(saw_xdigit) {
00208     if(tp + INT16SZ > endp)
00209       return (0);
00210     *tp++ = (unsigned char) ((val >> 8) & 0xff);
00211     *tp++ = (unsigned char) (val & 0xff);
00212   }
00213   if(colonp != NULL) {
00214     /*
00215      * Since some memmove()'s erroneously fail to handle
00216      * overlapping regions, we'll do the shift by hand.
00217      */
00218     const ssize_t n = tp - colonp;
00219     ssize_t i;
00220 
00221     if(tp == endp)
00222       return (0);
00223     for(i = 1; i <= n; i++) {
00224       *(endp - i) = *(colonp + n - i);
00225       *(colonp + n - i) = 0;
00226     }
00227     tp = endp;
00228   }
00229   if(tp != endp)
00230     return (0);
00231   memcpy(dst, tmp, IN6ADDRSZ);
00232   return (1);
00233 }
00234 #endif /* ENABLE_IPV6 */
00235 
00236 #endif /* HAVE_INET_PTON */


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05