00001 /* 00002 * inet_aton.c -- provides inet_aton() if necessary. 00003 * 00004 * $Id: inet_aton.c,v 1.21 2011/02/13 14:19:33 simple Exp $ 00005 */ 00006 /* 00007 * Portions Copyright (C) 2000 - 2011 Eggheads Development Team 00008 * 00009 * This program is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU General Public License 00011 * as published by the Free Software Foundation; either version 2 00012 * of the License, or (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 */ 00023 00024 #include "main.h" 00025 #include "inet_aton.h" 00026 00027 #ifndef HAVE_ISASCII 00028 # define inet_isascii(x) 1 /* Let checks succeed if we don't have isascii(). */ 00029 #else 00030 # define inet_isascii(x) egg_isascii(x) 00031 #endif 00032 00033 #ifndef HAVE_INET_ATON 00034 /* 00035 * ++Copyright++ 1983, 1990, 1993 00036 * - 00037 * Copyright (c) 1983, 1990, 1993 00038 * The Regents of the University of California. All rights reserved. 00039 * 00040 * Redistribution and use in source and binary forms, with or without 00041 * modification, are permitted provided that the following conditions 00042 * are met: 00043 * 1. Redistributions of source code must retain the above copyright 00044 * notice, this list of conditions and the following disclaimer. 00045 * 2. Redistributions in binary form must reproduce the above copyright 00046 * notice, this list of conditions and the following disclaimer in the 00047 * documentation and/or other materials provided with the distribution. 00048 * 3. All advertising materials mentioning features or use of this software 00049 * must display the following acknowledgement: 00050 * This product includes software developed by the University of 00051 * California, Berkeley and its contributors. 00052 * 4. Neither the name of the University nor the names of its contributors 00053 * may be used to endorse or promote products derived from this software 00054 * without specific prior written permission. 00055 * 00056 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00057 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00058 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00059 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00060 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00061 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00062 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00063 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00064 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00065 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00066 * SUCH DAMAGE. 00067 * - 00068 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 00069 * 00070 * Permission to use, copy, modify, and distribute this software for any 00071 * purpose with or without fee is hereby granted, provided that the above 00072 * copyright notice and this permission notice appear in all copies, and that 00073 * the name of Digital Equipment Corporation not be used in advertising or 00074 * publicity pertaining to distribution of the document or software without 00075 * specific, written prior permission. 00076 * 00077 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 00078 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 00079 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 00080 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 00081 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 00082 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 00083 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 00084 * SOFTWARE. 00085 * --Copyright-- 00086 */ 00087 00088 #if defined(LIBC_SCCS) && !defined(lint) 00089 static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; 00090 static char rcsid[] = "$-Id: inet_addr.c,v 1.11 1999/04/29 18:19:53 drepper Exp $"; 00091 #endif /* LIBC_SCCS and not lint */ 00092 00093 #include <sys/types.h> 00094 #include <sys/param.h> 00095 #include <ctype.h> 00096 00097 /* 00098 * Check whether "cp" is a valid ascii representation 00099 * of an Internet address and convert to a binary address. 00100 * Returns 1 if the address is valid, 0 if not. 00101 * This replaces inet_addr, the return value from which 00102 * cannot distinguish between failure and a local broadcast address. 00103 */ 00104 int egg_inet_aton(cp, addr) 00105 const char *cp; 00106 struct in_addr *addr; 00107 { 00108 static const u_32bit_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff }; 00109 register u_32bit_t val; /* changed from u_long --david */ 00110 register int base; 00111 register int n; 00112 register char c; 00113 u_32bit_t parts[4]; 00114 register u_32bit_t *pp = parts; 00115 00116 egg_bzero(parts, sizeof(parts)); 00117 00118 c = *cp; 00119 for (;;) { 00120 /* 00121 * Collect number up to ``.''. 00122 * Values are specified as for C: 00123 * 0x=hex, 0=octal, isdigit=decimal. 00124 */ 00125 if (!egg_isdigit(c)) 00126 goto ret_0; 00127 base = 10; 00128 if (c == '0') { 00129 c = *++cp; 00130 if (c == 'x' || c == 'X') 00131 base = 16, c = *++cp; 00132 else 00133 base = 8; 00134 } 00135 val = 0; 00136 for (;;) { 00137 if (inet_isascii(c) && egg_isdigit(c)) { 00138 val = (val * base) + (c - '0'); 00139 c = *++cp; 00140 } else if (base == 16 && inet_isascii(c) && egg_isxdigit(c)) { 00141 val = (val << 4) | (c + 10 - (egg_islower(c) ? 'a' : 'A')); 00142 c = *++cp; 00143 } else 00144 break; 00145 } 00146 if (c == '.') { 00147 /* 00148 * Internet format: 00149 * a.b.c.d 00150 * a.b.c (with c treated as 16 bits) 00151 * a.b (with b treated as 24 bits) 00152 */ 00153 if (pp >= parts + 3) 00154 goto ret_0; 00155 *pp++ = val; 00156 c = *++cp; 00157 } else 00158 break; 00159 } 00160 /* 00161 * Check for trailing characters. 00162 */ 00163 if (c != '\0' && (!inet_isascii(c) || !egg_isspace(c))) 00164 goto ret_0; 00165 /* 00166 * Concoct the address according to 00167 * the number of parts specified. 00168 */ 00169 n = pp - parts + 1; 00170 00171 if (n == 0 || /* initial nondigit */ 00172 parts[0] > 0xff || parts[1] > 0xff || parts[2] > 0xff || 00173 val > max[n - 1]) 00174 goto ret_0; 00175 00176 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); 00177 00178 if (addr) 00179 addr->s_addr = htonl(val); 00180 return 1; 00181 00182 ret_0: 00183 return 0; 00184 } 00185 #endif /* !HAVE_INET_ATON */