|
| 1 | +/* |
| 2 | + * |
| 3 | + * This inet_aton() function was taken from the GNU C library and |
| 4 | + * incorporated into Postgres for those systems which do not have this |
| 5 | + * routine in their standard C libraries. |
| 6 | + * |
| 7 | + * The function was been extracted whole from the file inet_aton.c in |
| 8 | + * Release 5.3.12 of the Linux C library, which is derived from the |
| 9 | + * GNU C library, by Bryan Henderson in October 1996. The copyright |
| 10 | + * notice from that file is below. |
| 11 | + */ |
| 12 | + |
| 13 | +/* |
| 14 | + * Copyright (c) 1983, 1990, 1993 |
| 15 | + *The Regents of the University of California. All rights reserved. |
| 16 | + * |
| 17 | + * Redistribution and use in source and binary forms, with or without |
| 18 | + * modification, are permitted provided that the following conditions |
| 19 | + * are met: |
| 20 | + * 1. Redistributions of source code must retain the above copyright |
| 21 | + * notice, this list of conditions and the following disclaimer. |
| 22 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | + * notice, this list of conditions and the following disclaimer in the |
| 24 | + * documentation and/or other materials provided with the distribution. |
| 25 | + * 3. All advertising materials mentioning features or use of this software |
| 26 | + * must display the following acknowledgement: |
| 27 | + *This product includes software developed by the University of |
| 28 | + *California, Berkeley and its contributors. |
| 29 | + * 4. Neither the name of the University nor the names of its contributors |
| 30 | + * may be used to endorse or promote products derived from this software |
| 31 | + * without specific prior written permission. |
| 32 | + * |
| 33 | + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 34 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 35 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 36 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 37 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 38 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 39 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 40 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 41 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 42 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 43 | + * SUCH DAMAGE. */ |
| 44 | + |
| 45 | +#include<sys/types.h> |
| 46 | +#include<netinet/in.h> |
| 47 | +#include<ctype.h> |
| 48 | +#include"inet_aton.h" |
| 49 | + |
| 50 | +/* |
| 51 | + * Check whether "cp" is a valid ascii representation |
| 52 | + * of an Internet address and convert to a binary address. |
| 53 | + * Returns 1 if the address is valid, 0 if not. |
| 54 | + * This replaces inet_addr, the return value from which |
| 55 | + * cannot distinguish between failure and a local broadcast address. |
| 56 | + */ |
| 57 | +int |
| 58 | +inet_aton(constchar*cp,structin_addr*addr) |
| 59 | +{ |
| 60 | +registeru_longval; |
| 61 | +registerintbase,n; |
| 62 | +registercharc; |
| 63 | +u_intparts[4]; |
| 64 | +registeru_int*pp=parts; |
| 65 | + |
| 66 | +for (;;) { |
| 67 | +/* |
| 68 | + * Collect number up to ``.''. |
| 69 | + * Values are specified as for C: |
| 70 | + * 0x=hex, 0=octal, other=decimal. |
| 71 | + */ |
| 72 | +val=0;base=10; |
| 73 | +if (*cp=='0') { |
| 74 | +if (*++cp=='x'||*cp=='X') |
| 75 | +base=16,cp++; |
| 76 | +else |
| 77 | +base=8; |
| 78 | +} |
| 79 | +while ((c=*cp)!='\0') { |
| 80 | +if (isascii(c)&&isdigit(c)) { |
| 81 | +val= (val*base)+ (c-'0'); |
| 82 | +cp++; |
| 83 | +continue; |
| 84 | +} |
| 85 | +if (base==16&&isascii(c)&&isxdigit(c)) { |
| 86 | +val= (val <<4)+ |
| 87 | +(c+10- (islower(c) ?'a' :'A')); |
| 88 | +cp++; |
| 89 | +continue; |
| 90 | +} |
| 91 | +break; |
| 92 | +} |
| 93 | +if (*cp=='.') { |
| 94 | +/* |
| 95 | + * Internet format: |
| 96 | + *a.b.c.d |
| 97 | + *a.b.c(with c treated as 16-bits) |
| 98 | + *a.b(with b treated as 24 bits) |
| 99 | + */ |
| 100 | +if (pp >=parts+3||val>0xff) |
| 101 | +return (0); |
| 102 | +*pp++=val,cp++; |
| 103 | +}else |
| 104 | +break; |
| 105 | +} |
| 106 | +/* |
| 107 | + * Check for trailing characters. |
| 108 | + */ |
| 109 | +if (*cp&& (!isascii(*cp)|| !isspace(*cp))) |
| 110 | +return (0); |
| 111 | +/* |
| 112 | + * Concoct the address according to |
| 113 | + * the number of parts specified. |
| 114 | + */ |
| 115 | +n=pp-parts+1; |
| 116 | +switch (n) { |
| 117 | + |
| 118 | +case1:/* a -- 32 bits */ |
| 119 | +break; |
| 120 | + |
| 121 | +case2:/* a.b -- 8.24 bits */ |
| 122 | +if (val>0xffffff) |
| 123 | +return (0); |
| 124 | +val |=parts[0] <<24; |
| 125 | +break; |
| 126 | + |
| 127 | +case3:/* a.b.c -- 8.8.16 bits */ |
| 128 | +if (val>0xffff) |
| 129 | +return (0); |
| 130 | +val |= (parts[0] <<24) | (parts[1] <<16); |
| 131 | +break; |
| 132 | + |
| 133 | +case4:/* a.b.c.d -- 8.8.8.8 bits */ |
| 134 | +if (val>0xff) |
| 135 | +return (0); |
| 136 | +val |= (parts[0] <<24) | (parts[1] <<16) | (parts[2] <<8); |
| 137 | +break; |
| 138 | +} |
| 139 | +if (addr) |
| 140 | +addr->s_addr=htonl(val); |
| 141 | +return (1); |
| 142 | +} |