~ubuntu-branches/ubuntu/oneiric/eggdrop/oneiric

« back to all changes in this revision

Viewing changes to src/compat/inet_aton.c

  • Committer: Bazaar Package Importer
  • Author(s): Guilherme de S. Pastore
  • Date: 2004-06-17 09:15:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040617091528-64rrw1sa33lkfhmh
Tags: 1.6.16-2
* Fixed typo on README.Debian
* Fixed hyphens in manual page
* Converted debian/rules to CDBS
* Set path to binary on example config file
* Changed LANGDIR on src/eggdrop.h (Closes: #254824)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * inet_aton.c -- provides inet_aton() if necessary.
3
3
 *
4
 
 * $Id: inet_aton.c,v 1.7 2002/01/02 03:46:36 guppy Exp $
 
4
 * $Id: inet_aton.c,v 1.15 2004/01/09 05:56:37 wcc Exp $
5
5
 */
6
6
/*
7
 
 * Portions Copyright (C) 2000, 2001, 2002 Eggheads Development Team
 
7
 * Portions Copyright (C) 2000, 2001, 2002, 2003, 2004 Eggheads Development Team
8
8
 *
9
9
 * This program is free software; you can redistribute it and/or
10
10
 * modify it under the terms of the GNU General Public License
25
25
#include "inet_aton.h"
26
26
 
27
27
#ifndef HAVE_ISASCII
28
 
/* Let all checks succeed if we don't have isascii(). */
29
 
#  define isascii(x)    1
 
28
#  define inet_isascii(x) 1 /* Let checks succeed if we don't have isascii(). */
 
29
#else
 
30
#  define inet_isascii(x) egg_isascii(x)
30
31
#endif
31
32
 
32
33
#ifndef HAVE_INET_ATON
46
47
 *    documentation and/or other materials provided with the distribution.
47
48
 * 3. All advertising materials mentioning features or use of this software
48
49
 *    must display the following acknowledgement:
49
 
 *      This product includes software developed by the University of
50
 
 *      California, Berkeley and its contributors.
 
50
 *      This product includes software developed by the University of
 
51
 *      California, Berkeley and its contributors.
51
52
 * 4. Neither the name of the University nor the names of its contributors
52
53
 *    may be used to endorse or promote products derived from this software
53
54
 *    without specific prior written permission.
100
101
 * This replaces inet_addr, the return value from which
101
102
 * cannot distinguish between failure and a local broadcast address.
102
103
 */
103
 
int
104
 
egg_inet_aton(cp, addr)
105
 
        const char *cp;
106
 
        struct in_addr *addr;
 
104
int egg_inet_aton(cp, addr)
 
105
const char *cp;
 
106
struct in_addr *addr;
107
107
{
108
 
        static const u_32bit_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
109
 
        register u_32bit_t val; /* changed from u_long --david */
110
 
        register int base;
111
 
        register int n;
112
 
        register char c;
113
 
        u_32bit_t parts[4];
114
 
        register u_32bit_t *pp = parts;
115
 
 
116
 
        egg_bzero(parts, sizeof (parts));
117
 
 
118
 
        c = *cp;
119
 
        for (;;) {
120
 
                /*
121
 
                 * Collect number up to ``.''.
122
 
                 * Values are specified as for C:
123
 
                 * 0x=hex, 0=octal, isdigit=decimal.
124
 
                 */
125
 
                if (!isdigit(c))
126
 
                        goto ret_0;
127
 
                base = 10;
128
 
                if (c == '0') {
129
 
                        c = *++cp;
130
 
                        if (c == 'x' || c == 'X')
131
 
                                base = 16, c = *++cp;
132
 
                        else
133
 
                                base = 8;
134
 
                }
135
 
                val = 0;
136
 
                for (;;) {
137
 
                        if (isascii(c) && isdigit(c)) {
138
 
                                val = (val * base) + (c - '0');
139
 
                                c = *++cp;
140
 
                        } else if (base == 16 && isascii(c) && isxdigit(c)) {
141
 
                                val = (val << 4) |
142
 
                                        (c + 10 - (islower(c) ? 'a' : 'A'));
143
 
                                c = *++cp;
144
 
                        } else
145
 
                                break;
146
 
                }
147
 
                if (c == '.') {
148
 
                        /*
149
 
                         * Internet format:
150
 
                         *      a.b.c.d
151
 
                         *      a.b.c   (with c treated as 16 bits)
152
 
                         *      a.b     (with b treated as 24 bits)
153
 
                         */
154
 
                        if (pp >= parts + 3)
155
 
                                goto ret_0;
156
 
                        *pp++ = val;
157
 
                        c = *++cp;
158
 
                } else
159
 
                        break;
160
 
        }
161
 
        /*
162
 
         * Check for trailing characters.
163
 
         */
164
 
        if (c != '\0' && (!isascii(c) || !isspace(c)))
165
 
                goto ret_0;
166
 
        /*
167
 
         * Concoct the address according to
168
 
         * the number of parts specified.
169
 
         */
170
 
        n = pp - parts + 1;
171
 
 
172
 
        if (n == 0      /* initial nondigit */
173
 
            || parts[0] > 0xff || parts[1] > 0xff || parts[2] > 0xff
174
 
            || val > max[n - 1])
175
 
          goto ret_0;
176
 
 
177
 
        val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
178
 
 
179
 
        if (addr)
180
 
                addr->s_addr = htonl(val);
181
 
        return (1);
 
108
  static const u_32bit_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
 
109
  register u_32bit_t val;       /* changed from u_long --david */
 
110
  register int base;
 
111
  register int n;
 
112
  register char c;
 
113
  u_32bit_t parts[4];
 
114
  register u_32bit_t *pp = parts;
 
115
 
 
116
  egg_bzero(parts, sizeof(parts));
 
117
 
 
118
  c = *cp;
 
119
  for (;;) {
 
120
    /*
 
121
     * Collect number up to ``.''.
 
122
     * Values are specified as for C:
 
123
     * 0x=hex, 0=octal, isdigit=decimal.
 
124
     */
 
125
    if (!egg_isdigit(c))
 
126
      goto ret_0;
 
127
    base = 10;
 
128
    if (c == '0') {
 
129
      c = *++cp;
 
130
      if (c == 'x' || c == 'X')
 
131
        base = 16, c = *++cp;
 
132
      else
 
133
        base = 8;
 
134
    }
 
135
    val = 0;
 
136
    for (;;) {
 
137
      if (inet_isascii(c) && egg_isdigit(c)) {
 
138
        val = (val * base) + (c - '0');
 
139
        c = *++cp;
 
140
      } else if (base == 16 && inet_isascii(c) && egg_isxdigit(c)) {
 
141
        val = (val << 4) | (c + 10 - (egg_islower(c) ? 'a' : 'A'));
 
142
        c = *++cp;
 
143
      } else
 
144
        break;
 
145
    }
 
146
    if (c == '.') {
 
147
      /*
 
148
       * Internet format:
 
149
       *      a.b.c.d
 
150
       *      a.b.c   (with c treated as 16 bits)
 
151
       *      a.b     (with b treated as 24 bits)
 
152
       */
 
153
      if (pp >= parts + 3)
 
154
        goto ret_0;
 
155
      *pp++ = val;
 
156
      c = *++cp;
 
157
    } else
 
158
      break;
 
159
  }
 
160
  /*
 
161
   * Check for trailing characters.
 
162
   */
 
163
  if (c != '\0' && (!inet_isascii(c) || !egg_isspace(c)))
 
164
    goto ret_0;
 
165
  /*
 
166
   * Concoct the address according to
 
167
   * the number of parts specified.
 
168
   */
 
169
  n = pp - parts + 1;
 
170
 
 
171
  if (n == 0 ||                 /* initial nondigit */
 
172
      parts[0] > 0xff || parts[1] > 0xff || parts[2] > 0xff ||
 
173
      val > max[n - 1])
 
174
    goto ret_0;
 
175
 
 
176
  val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
 
177
 
 
178
  if (addr)
 
179
    addr->s_addr = htonl(val);
 
180
  return 1;
182
181
 
183
182
ret_0:
184
 
        return (0);
 
183
  return 0;
185
184
}
186
 
#endif /* HAVE_INET_ATON */
 
185
#endif /* !HAVE_INET_ATON */