~ubuntu-branches/ubuntu/precise/krb5/precise-updates

« back to all changes in this revision

Viewing changes to src/util/wshelper/inetaton.c

  • Committer: Package Import Robot
  • Author(s): Sam Hartman
  • Date: 2011-12-01 19:34:41 UTC
  • mfrom: (28.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20111201193441-9tipg3aru1jsidyv
Tags: 1.10+dfsg~alpha1-6
* Fix segfault with unknown hostnames in krb5_sname_to_principal,
  Closes: #650671
* Indicate that this library breaks libsmbclient versions that depend on
  krb5_locate_kdc, Closes: #650603, #650611

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *      @doc RESOLVE
 
4
 *
 
5
 * @module inetaton.c  |
 
6
 *
 
7
 *      from the BIND 4.9.x inetaddr.c
 
8
 *
 
9
 *        Contains implementation of inet_aton
 
10
 
 
11
 *        WSHelper DNS/Hesiod Library for WINSOCK
 
12
 *
 
13
 */
 
14
 
 
15
/*
 
16
 * Copyright (c) 1983, 1990 Regents of the University of California.
 
17
 * All rights reserved.
 
18
 *
 
19
 * Redistribution and use in source and binary forms, with or without
 
20
 * modification, are permitted provided that the following conditions
 
21
 * are met:
 
22
 * 1. Redistributions of source code must retain the above copyright
 
23
 *    notice, this list of conditions and the following disclaimer.
 
24
 * 2. Redistributions in binary form must reproduce the above copyright
 
25
 *    notice, this list of conditions and the following disclaimer in the
 
26
 *    documentation and/or other materials provided with the distribution.
 
27
 * 3. All advertising materials mentioning features or use of this software
 
28
 *    must display the following acknowledgement:
 
29
 *      This product includes software developed by the University of
 
30
 *      California, Berkeley and its contributors.
 
31
 * 4. Neither the name of the University nor the names of its contributors
 
32
 *    may be used to endorse or promote products derived from this software
 
33
 *    without specific prior written permission.
 
34
 *
 
35
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
36
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
37
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
38
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
39
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
40
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
41
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
42
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
43
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
44
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
45
 * SUCH DAMAGE.
 
46
 */
 
47
 
 
48
#if defined(LIBC_SCCS) && !defined(lint)
 
49
static char sccsid[] = "@(#)inet_addr.c 5.11 (Berkeley) 12/9/91";
 
50
#endif /* LIBC_SCCS and not lint */
 
51
 
 
52
#include <windows.h>
 
53
#include <winsock.h>
 
54
#include <ctype.h>
 
55
 
 
56
 
 
57
/*
 
58
        converts a string containing an (Ipv4) Internet Protocol dotted address into a proper address for the in_addr structure
 
59
 
 
60
        \param[in]      cp Null-terminated character string representing a number expressed in the
 
61
        Internet standard ".'' (dotted) notation.
 
62
        \param[in, out] addr pointer to the in_addr structure. The s_addr memeber will be populated
 
63
 
 
64
 
 
65
        \retval Returns 1 if the address is valid, 0 if not.
 
66
 
 
67
 */
 
68
unsigned long
 
69
WINAPI
 
70
inet_aton(register const char *cp, struct in_addr *addr)
 
71
{
 
72
    register u_long val, base;
 
73
        ULONG_PTR n;
 
74
    register char c;
 
75
    u_long parts[4], *pp = parts;
 
76
 
 
77
    for (;;) {
 
78
        /*
 
79
         * Collect number up to ``.''.
 
80
         * Values are specified as for C:
 
81
         * 0x=hex, 0=octal, other=decimal.
 
82
         */
 
83
        val = 0; base = 10;
 
84
        if (*cp == '0') {
 
85
            if (*++cp == 'x' || *cp == 'X')
 
86
                base = 16, cp++;
 
87
            else
 
88
                base = 8;
 
89
        }
 
90
        while ((c = *cp) != '\0') {
 
91
            if (isascii(c) && isdigit(c)) {
 
92
                val = (val * base) + (c - '0');
 
93
                cp++;
 
94
                continue;
 
95
            }
 
96
            if (base == 16 && isascii(c) && isxdigit(c)) {
 
97
                val = (val << 4) +
 
98
                    (c + 10 - (islower(c) ? 'a' : 'A'));
 
99
                cp++;
 
100
                continue;
 
101
            }
 
102
            break;
 
103
        }
 
104
        if (*cp == '.') {
 
105
            /*
 
106
             * Internet format:
 
107
             *  a.b.c.d
 
108
             *  a.b.c   (with c treated as 16-bits)
 
109
             *  a.b     (with b treated as 24 bits)
 
110
             */
 
111
            if (pp >= parts + 3 || val > 0xff)
 
112
                return (0);
 
113
            *pp++ = val, cp++;
 
114
        } else
 
115
            break;
 
116
    }
 
117
    /*
 
118
     * Check for trailing characters.
 
119
     */
 
120
    if (*cp && (!isascii(*cp) || !isspace(*cp)))
 
121
        return (0);
 
122
    /*
 
123
     * Concoct the address according to
 
124
     * the number of parts specified.
 
125
     */
 
126
    n = pp - parts + 1;
 
127
    switch (n) {
 
128
 
 
129
    case 1:                             /* a -- 32 bits */
 
130
        break;
 
131
 
 
132
    case 2:                             /* a.b -- 8.24 bits */
 
133
        if (val > 0xffffff)
 
134
            return (0);
 
135
        val |= parts[0] << 24;
 
136
        break;
 
137
 
 
138
        case 3:                         /* a.b.c -- 8.8.16 bits */
 
139
            if (val > 0xffff)
 
140
                return (0);
 
141
            val |= (parts[0] << 24) | (parts[1] << 16);
 
142
            break;
 
143
 
 
144
        case 4:                         /* a.b.c.d -- 8.8.8.8 bits */
 
145
            if (val > 0xff)
 
146
                return (0);
 
147
            val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
 
148
            break;
 
149
    }
 
150
    if (addr)
 
151
        addr->s_addr = htonl(val);
 
152
    return (1);
 
153
}