Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

inet_pton.c

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

Generated on Tue Apr 16 12:37:36 2002 for rsync by doxygen1.2.15