"Fossies" - the Fresh Open Source Software Archive

Member "mod_ftp-0.9.6/modules/ftp/ftp_inet_pton.c" (8 Jun 2009, 7347 Bytes) of package /linux/www/apache_httpd_modules/old/mod_ftp-0.9.6-beta.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ftp_inet_pton.c" see the Fossies "Dox" file reference documentation.

    1 /* Licensed to the Apache Software Foundation (ASF) under one or more
    2  * contributor license agreements.  See the NOTICE file distributed with
    3  * this work for additional information regarding copyright ownership.
    4  * The ASF licenses this file to You under the Apache License, Version 2.0
    5  * (the "License"); you may not use this file except in compliance with
    6  * the License.  You may obtain a copy of the License at
    7  *
    8  *     http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 /*
   18  * Original Copyright (c) 2005 Covalent Technologies
   19  *
   20  * FTP Protocol module for Apache 2.0
   21  */
   22 
   23 /* Copyright (c) 1996 by Internet Software Consortium.
   24  *
   25  * Permission to use, copy, modify, and distribute this software for any
   26  * purpose with or without fee is hereby granted, provided that the above
   27  * copyright notice and this permission notice appear in all copies.
   28  *
   29  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
   30  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
   31  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
   32  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
   33  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
   34  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
   35  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
   36  * SOFTWARE.
   37  */
   38 
   39 #include "mod_ftp.h"
   40 #include "ftp_internal.h"
   41 #include "apr_network_io.h"
   42 
   43 #if APR_HAVE_SYS_SOCKET_H
   44 #include <sys/socket.h>
   45 #endif
   46 #if APR_HAVE_NETINET_IN_H
   47 #include <netinet/in.h>
   48 #endif
   49 #if APR_HAVE_ARPA_INET_H
   50 #include <arpa/inet.h>
   51 #endif
   52 #if APR_HAVE_STRING_H
   53 #include <string.h>
   54 #endif
   55 #if APR_HAVE_ERRNO_H
   56 #include <errno.h>
   57 #endif
   58 
   59 #ifndef IN6ADDRSZ
   60 #define IN6ADDRSZ   16
   61 #endif
   62 
   63 #ifndef INT16SZ
   64 #define INT16SZ sizeof(apr_int16_t)
   65 #endif
   66 
   67 #ifndef INADDRSZ
   68 #define INADDRSZ    4
   69 #endif
   70 
   71 #ifndef __P
   72 #define __P(x) x
   73 #endif
   74 
   75 #if !defined(EAFNOSUPPORT) && defined(WSAEAFNOSUPPORT)
   76 #define EAFNOSUPPORT WSAEAFNOSUPPORT
   77 #endif
   78 
   79 /*
   80  * WARNING: Don't even consider trying to compile this on a system where
   81  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
   82  */
   83 
   84 static int inet_pton4 __P((const char *src, unsigned char *dst));
   85 #if APR_HAVE_IPV6
   86 static int inet_pton6 __P((const char *src, unsigned char *dst));
   87 #endif
   88 
   89 /* int
   90  * inet_pton(af, src, dst)
   91  *      convert from presentation format (which usually means ASCII printable)
   92  *      to network format (which is usually some kind of binary format).
   93  * return:
   94  *      1 if the address was valid for the specified address family
   95  *      0 if the address wasn't valid (`dst' is untouched in this case)
   96  *      -1 if some other error occurred (`dst' is untouched in this case, too)
   97  * author:
   98  *      Paul Vixie, 1996.
   99  */
  100 int ftp_inet_pton(int af, const char *src, void *dst)
  101 {
  102     switch (af) {
  103     case AF_INET:
  104         return (inet_pton4(src, dst));
  105 #if APR_HAVE_IPV6
  106     case AF_INET6:
  107         return (inet_pton6(src, dst));
  108 #endif
  109     default:
  110         errno = EAFNOSUPPORT;
  111         return (-1);
  112     }
  113     /* NOTREACHED */
  114 }
  115 
  116 /* int
  117  * inet_pton4(src, dst)
  118  *      like inet_aton() but without all the hexadecimal and shorthand.
  119  * return:
  120  *      1 if `src' is a valid dotted quad, else 0.
  121  * notice:
  122  *      does not touch `dst' unless it's returning 1.
  123  * author:
  124  *      Paul Vixie, 1996.
  125  */
  126 static int inet_pton4(const char *src, unsigned char *dst)
  127 {
  128     static const char digits[] = "0123456789";
  129     int saw_digit, octets, ch;
  130     unsigned char tmp[INADDRSZ], *tp;
  131 
  132     saw_digit = 0;
  133     octets = 0;
  134     *(tp = tmp) = 0;
  135     while ((ch = *src++) != '\0') {
  136         const char *pch;
  137 
  138         if ((pch = ap_strchr_c(digits, ch)) != NULL) {
  139             unsigned int new = *tp * 10 + (pch - digits);
  140 
  141             if (new > 255)
  142                 return (0);
  143             *tp = new;
  144             if (!saw_digit) {
  145                 if (++octets > 4)
  146                     return (0);
  147                 saw_digit = 1;
  148             }
  149         }
  150         else if (ch == '.' && saw_digit) {
  151             if (octets == 4)
  152                 return (0);
  153             *++tp = 0;
  154             saw_digit = 0;
  155         }
  156         else
  157             return (0);
  158     }
  159     if (octets < 4)
  160         return (0);
  161 
  162     memcpy(dst, tmp, INADDRSZ);
  163     return (1);
  164 }
  165 
  166 #if APR_HAVE_IPV6
  167 /* int
  168  * inet_pton6(src, dst)
  169  *      convert presentation level address to network order binary form.
  170  * return:
  171  *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
  172  * notice:
  173  *      (1) does not touch `dst' unless it's returning 1.
  174  *      (2) :: in a full address is silently ignored.
  175  * credit:
  176  *      inspired by Mark Andrews.
  177  * author:
  178  *      Paul Vixie, 1996.
  179  */
  180 static int inet_pton6(const char *src, unsigned char *dst)
  181 {
  182     static const char xdigits_l[] = "0123456789abcdef", xdigits_u[] = "0123456789ABCDEF";
  183     unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
  184     const char *xdigits, *curtok;
  185     int ch, saw_xdigit;
  186     unsigned int val;
  187 
  188     memset((tp = tmp), '\0', IN6ADDRSZ);
  189     endp = tp + IN6ADDRSZ;
  190     colonp = NULL;
  191     /* Leading :: requires some special handling. */
  192     if (*src == ':')
  193         if (*++src != ':')
  194             return (0);
  195     curtok = src;
  196     saw_xdigit = 0;
  197     val = 0;
  198     while ((ch = *src++) != '\0') {
  199         const char *pch;
  200 
  201         if ((pch = ap_strchr_c((xdigits = xdigits_l), ch)) == NULL)
  202             pch = ap_strchr_c((xdigits = xdigits_u), ch);
  203         if (pch != NULL) {
  204             val <<= 4;
  205             val |= (pch - xdigits);
  206             if (val > 0xffff)
  207                 return (0);
  208             saw_xdigit = 1;
  209             continue;
  210         }
  211         if (ch == ':') {
  212             curtok = src;
  213             if (!saw_xdigit) {
  214                 if (colonp)
  215                     return (0);
  216                 colonp = tp;
  217                 continue;
  218             }
  219             if (tp + INT16SZ > endp)
  220                 return (0);
  221             *tp++ = (unsigned char) (val >> 8) & 0xff;
  222             *tp++ = (unsigned char) val & 0xff;
  223             saw_xdigit = 0;
  224             val = 0;
  225             continue;
  226         }
  227         if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
  228             inet_pton4(curtok, tp) > 0) {
  229             tp += INADDRSZ;
  230             saw_xdigit = 0;
  231             break;              /* '\0' was seen by inet_pton4(). */
  232         }
  233         return (0);
  234     }
  235     if (saw_xdigit) {
  236         if (tp + INT16SZ > endp)
  237             return (0);
  238         *tp++ = (unsigned char) (val >> 8) & 0xff;
  239         *tp++ = (unsigned char) val & 0xff;
  240     }
  241     if (colonp != NULL) {
  242         /*
  243          * Since some memmove()'s erroneously fail to handle
  244          * overlapping regions, we'll do the shift by hand.
  245          */
  246         const int n = tp - colonp;
  247         int i;
  248 
  249         for (i = 1; i <= n; i++) {
  250             endp[-i] = colonp[n - i];
  251             colonp[n - i] = 0;
  252         }
  253         tp = endp;
  254     }
  255     if (tp != endp)
  256         return (0);
  257     memcpy(dst, tmp, IN6ADDRSZ);
  258     return (1);
  259 }
  260 #endif