~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to src/port/inet_aton.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* src/port/inet_aton.c
 
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. Neither the name of the University nor the names of its contributors
 
26
 *        may be used to endorse or promote products derived from this software
 
27
 *        without specific prior written permission.
 
28
 *
 
29
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
30
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
31
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
32
 * ARE DISCLAIMED.      IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
33
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
34
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
35
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
36
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
37
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
38
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
39
 * SUCH DAMAGE.  */
 
40
 
 
41
#include "c.h"
 
42
 
 
43
#include <netinet/in.h>
 
44
#include <ctype.h>
 
45
 
 
46
/*
 
47
 * Check whether "cp" is a valid ascii representation
 
48
 * of an Internet address and convert to a binary address.
 
49
 * Returns 1 if the address is valid, 0 if not.
 
50
 * This replaces inet_addr, the return value from which
 
51
 * cannot distinguish between failure and a local broadcast address.
 
52
 */
 
53
int
 
54
inet_aton(const char *cp, struct in_addr * addr)
 
55
{
 
56
        unsigned int val;
 
57
        int                     base,
 
58
                                n;
 
59
        char            c;
 
60
        u_int           parts[4];
 
61
        u_int      *pp = parts;
 
62
 
 
63
        for (;;)
 
64
        {
 
65
                /*
 
66
                 * Collect number up to ``.''. Values are specified as for C: 0x=hex,
 
67
                 * 0=octal, other=decimal.
 
68
                 */
 
69
                val = 0;
 
70
                base = 10;
 
71
                if (*cp == '0')
 
72
                {
 
73
                        if (*++cp == 'x' || *cp == 'X')
 
74
                                base = 16, cp++;
 
75
                        else
 
76
                                base = 8;
 
77
                }
 
78
                while ((c = *cp) != '\0')
 
79
                {
 
80
                        if (isdigit((unsigned char) c))
 
81
                        {
 
82
                                val = (val * base) + (c - '0');
 
83
                                cp++;
 
84
                                continue;
 
85
                        }
 
86
                        if (base == 16 && isxdigit((unsigned char) c))
 
87
                        {
 
88
                                val = (val << 4) +
 
89
                                        (c + 10 - (islower((unsigned char) c) ? 'a' : 'A'));
 
90
                                cp++;
 
91
                                continue;
 
92
                        }
 
93
                        break;
 
94
                }
 
95
                if (*cp == '.')
 
96
                {
 
97
                        /*
 
98
                         * Internet format: a.b.c.d a.b.c       (with c treated as 16-bits)
 
99
                         * a.b     (with b treated as 24 bits)
 
100
                         */
 
101
                        if (pp >= parts + 3 || val > 0xff)
 
102
                                return 0;
 
103
                        *pp++ = val, cp++;
 
104
                }
 
105
                else
 
106
                        break;
 
107
        }
 
108
 
 
109
        /*
 
110
         * Check for trailing junk.
 
111
         */
 
112
        while (*cp)
 
113
                if (!isspace((unsigned char) *cp++))
 
114
                        return 0;
 
115
 
 
116
        /*
 
117
         * Concoct the address according to the number of parts specified.
 
118
         */
 
119
        n = pp - parts + 1;
 
120
        switch (n)
 
121
        {
 
122
 
 
123
                case 1:                 /* a -- 32 bits */
 
124
                        break;
 
125
 
 
126
                case 2:                 /* a.b -- 8.24 bits */
 
127
                        if (val > 0xffffff)
 
128
                                return 0;
 
129
                        val |= parts[0] << 24;
 
130
                        break;
 
131
 
 
132
                case 3:                 /* a.b.c -- 8.8.16 bits */
 
133
                        if (val > 0xffff)
 
134
                                return 0;
 
135
                        val |= (parts[0] << 24) | (parts[1] << 16);
 
136
                        break;
 
137
 
 
138
                case 4:                 /* a.b.c.d -- 8.8.8.8 bits */
 
139
                        if (val > 0xff)
 
140
                                return 0;
 
141
                        val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
 
142
                        break;
 
143
        }
 
144
        if (addr)
 
145
                addr->s_addr = htonl(val);
 
146
        return 1;
 
147
}