Header And Logo

PostgreSQL
| The world's most advanced open source database.

inet_aton.c

Go to the documentation of this file.
00001 /* src/port/inet_aton.c
00002  *
00003  *  This inet_aton() function was taken from the GNU C library and
00004  *  incorporated into Postgres for those systems which do not have this
00005  *  routine in their standard C libraries.
00006  *
00007  *  The function was been extracted whole from the file inet_aton.c in
00008  *  Release 5.3.12 of the Linux C library, which is derived from the
00009  *  GNU C library, by Bryan Henderson in October 1996.  The copyright
00010  *  notice from that file is below.
00011  */
00012 
00013 /*
00014  * Copyright (c) 1983, 1990, 1993
00015  *      The Regents of the University of California.  All rights reserved.
00016  *
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions
00019  * are met:
00020  * 1. Redistributions of source code must retain the above copyright
00021  *    notice, this list of conditions and the following disclaimer.
00022  * 2. Redistributions in binary form must reproduce the above copyright
00023  *    notice, this list of conditions and the following disclaimer in the
00024  *    documentation and/or other materials provided with the distribution.
00025  * 3. Neither the name of the University nor the names of its contributors
00026  *    may be used to endorse or promote products derived from this software
00027  *    without specific prior written permission.
00028  *
00029  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00030  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00031  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00032  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00033  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00034  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00035  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00036  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00038  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00039  * SUCH DAMAGE.  */
00040 
00041 #include "c.h"
00042 
00043 #include <netinet/in.h>
00044 #include <ctype.h>
00045 
00046 /*
00047  * Check whether "cp" is a valid ascii representation
00048  * of an Internet address and convert to a binary address.
00049  * Returns 1 if the address is valid, 0 if not.
00050  * This replaces inet_addr, the return value from which
00051  * cannot distinguish between failure and a local broadcast address.
00052  */
00053 int
00054 inet_aton(const char *cp, struct in_addr * addr)
00055 {
00056     unsigned int val;
00057     int         base,
00058                 n;
00059     char        c;
00060     u_int       parts[4];
00061     u_int      *pp = parts;
00062 
00063     for (;;)
00064     {
00065         /*
00066          * Collect number up to ``.''. Values are specified as for C: 0x=hex,
00067          * 0=octal, other=decimal.
00068          */
00069         val = 0;
00070         base = 10;
00071         if (*cp == '0')
00072         {
00073             if (*++cp == 'x' || *cp == 'X')
00074                 base = 16, cp++;
00075             else
00076                 base = 8;
00077         }
00078         while ((c = *cp) != '\0')
00079         {
00080             if (isdigit((unsigned char) c))
00081             {
00082                 val = (val * base) + (c - '0');
00083                 cp++;
00084                 continue;
00085             }
00086             if (base == 16 && isxdigit((unsigned char) c))
00087             {
00088                 val = (val << 4) +
00089                     (c + 10 - (islower((unsigned char) c) ? 'a' : 'A'));
00090                 cp++;
00091                 continue;
00092             }
00093             break;
00094         }
00095         if (*cp == '.')
00096         {
00097             /*
00098              * Internet format: a.b.c.d a.b.c   (with c treated as 16-bits)
00099              * a.b     (with b treated as 24 bits)
00100              */
00101             if (pp >= parts + 3 || val > 0xff)
00102                 return 0;
00103             *pp++ = val, cp++;
00104         }
00105         else
00106             break;
00107     }
00108 
00109     /*
00110      * Check for trailing junk.
00111      */
00112     while (*cp)
00113         if (!isspace((unsigned char) *cp++))
00114             return 0;
00115 
00116     /*
00117      * Concoct the address according to the number of parts specified.
00118      */
00119     n = pp - parts + 1;
00120     switch (n)
00121     {
00122 
00123         case 1:         /* a -- 32 bits */
00124             break;
00125 
00126         case 2:         /* a.b -- 8.24 bits */
00127             if (val > 0xffffff)
00128                 return 0;
00129             val |= parts[0] << 24;
00130             break;
00131 
00132         case 3:         /* a.b.c -- 8.8.16 bits */
00133             if (val > 0xffff)
00134                 return 0;
00135             val |= (parts[0] << 24) | (parts[1] << 16);
00136             break;
00137 
00138         case 4:         /* a.b.c.d -- 8.8.8.8 bits */
00139             if (val > 0xff)
00140                 return 0;
00141             val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
00142             break;
00143     }
00144     if (addr)
00145         addr->s_addr = htonl(val);
00146     return 1;
00147 }