/[cvs]/eggdrop1.8/src/compat/inet_ntop.c
ViewVC logotype

Annotation of /eggdrop1.8/src/compat/inet_ntop.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations) (download) (as text)
Thu Aug 5 18:12:05 2010 UTC (9 years, 1 month ago) by pseudo
Branch: MAIN
File MIME type: text/x-chdr
Added new, full IPv6 support to eggdrop.

1 pseudo 1.1 /*
2     * inet_ntop.c -- provides inet_ntop() if necessary
3     *
4     * $Id$
5     */
6     /*
7     * Portions Copyright (C) 2010 Eggheads Development Team
8     *
9     * This program is free software; you can redistribute it and/or
10     * modify it under the terms of the GNU General Public License
11     * as published by the Free Software Foundation; either version 2
12     * of the License, or (at your option) any later version.
13     *
14     * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17     * GNU General Public License for more details.
18     *
19     * You should have received a copy of the GNU General Public License
20     * along with this program; if not, write to the Free Software
21     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22     */
23    
24     #include "inet_ntop.h"
25    
26     #ifndef HAVE_INET_NTOP
27     /*
28     * Copyright (c) 1996-1999 by Internet Software Consortium.
29     *
30     * Permission to use, copy, modify, and distribute this software for any
31     * purpose with or without fee is hereby granted, provided that the above
32     * copyright notice and this permission notice appear in all copies.
33     *
34     * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
35     * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
36     * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
37     * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
38     * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
39     * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
40     * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41     * SOFTWARE.
42     */
43    
44     #include <sys/param.h>
45     #include <sys/types.h>
46     #include <arpa/inet.h>
47    
48     #include <stdio.h>
49     #include <string.h>
50    
51     #ifdef SPRINTF_CHAR
52     # define SPRINTF(x) strlen(sprintf/**/x)
53     #else
54     # define SPRINTF(x) ((size_t)sprintf x)
55     #endif
56    
57     #define NS_INADDRSZ 4 /* IPv4 T_A */
58     #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
59     #define NS_INT16SZ 2 /* #/bytes of data in a u_int16_t */
60    
61     /*
62     * WARNING: Don't even consider trying to compile this on a system where
63     * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
64     */
65    
66     static const char *inet_ntop4 (const u_char *src, char *dst, socklen_t size);
67     static const char *inet_ntop6 (const u_char *src, char *dst, socklen_t size);
68    
69     /* char *
70     * inet_ntop(af, src, dst, size)
71     * convert a network format address to presentation format.
72     * return:
73     * pointer to presentation format address (`dst'), or NULL (see errno).
74     * author:
75     * Paul Vixie, 1996.
76     */
77     const char *
78     inet_ntop(af, src, dst, size)
79     int af;
80     const void *src;
81     char *dst;
82     socklen_t size;
83     {
84     switch (af) {
85     case AF_INET:
86     return (inet_ntop4(src, dst, size));
87     #ifdef IPV6
88     case AF_INET6:
89     return (inet_ntop6(src, dst, size));
90     #endif
91     default:
92     return NULL;
93     }
94     /* NOTREACHED */
95     }
96    
97     /* const char *
98     * inet_ntop4(src, dst, size)
99     * format an IPv4 address
100     * return:
101     * `dst' (as a const)
102     * notes:
103     * (1) uses no statics
104     * (2) takes a u_char* not an in_addr as input
105     * author:
106     * Paul Vixie, 1996.
107     */
108     static const char *inet_ntop4(src, dst, size)
109     const u_char *src;
110     char *dst;
111     socklen_t size;
112     {
113     static const char fmt[] = "%u.%u.%u.%u";
114     char tmp[sizeof "255.255.255.255"];
115    
116     if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
117     /* __set_errno (ENOSPC); */
118     return (NULL);
119     }
120     return strcpy(dst, tmp);
121     }
122    
123     /* const char *
124     * inet_ntop6(src, dst, size)
125     * convert IPv6 binary address into presentation (printable) format
126     * author:
127     * Paul Vixie, 1996.
128     */
129     #ifdef IPV6
130     static const char *
131     inet_ntop6(src, dst, size)
132     const u_char *src;
133     char *dst;
134     socklen_t size;
135     {
136     /*
137     * Note that int32_t and int16_t need only be "at least" large enough
138     * to contain a value of the specified size. On some systems, like
139     * Crays, there is no such thing as an integer variable with 16 bits.
140     * Keep this in mind if you think this function should have been coded
141     * to use pointer overlays. All the world's not a VAX.
142     */
143     char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
144     struct { int base, len; } best, cur;
145     u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
146     int i;
147    
148     /*
149     * Preprocess:
150     * Copy the input (bytewise) array into a wordwise array.
151     * Find the longest run of 0x00's in src[] for :: shorthanding.
152     */
153     memset(words, '\0', sizeof words);
154     best.len = cur.len = 0;
155     for (i = 0; i < NS_IN6ADDRSZ; i += 2)
156     words[i / 2] = (src[i] << 8) | src[i + 1];
157     best.base = -1;
158     cur.base = -1;
159     for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
160     if (words[i] == 0) {
161     if (cur.base == -1)
162     cur.base = i, cur.len = 1;
163     else
164     cur.len++;
165     } else {
166     if (cur.base != -1) {
167     if (best.base == -1 || cur.len > best.len)
168     best = cur;
169     cur.base = -1;
170     }
171     }
172     }
173     if (cur.base != -1) {
174     if (best.base == -1 || cur.len > best.len)
175     best = cur;
176     }
177     if (best.base != -1 && best.len < 2)
178     best.base = -1;
179    
180     /*
181     * Format the result.
182     */
183     tp = tmp;
184     for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
185     /* Are we inside the best run of 0x00's? */
186     if (best.base != -1 && i >= best.base &&
187     i < (best.base + best.len)) {
188     if (i == best.base)
189     *tp++ = ':';
190     continue;
191     }
192     /* Are we following an initial run of 0x00s or any real hex? */
193     if (i != 0)
194     *tp++ = ':';
195     /* Is this address an encapsulated IPv4? */
196     if (i == 6 && best.base == 0 &&
197     (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
198     if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
199     return (NULL);
200     tp += strlen(tp);
201     break;
202     }
203     tp += SPRINTF((tp, "%x", words[i]));
204     }
205     /* Was it a trailing run of 0x00's? */
206     if (best.base != -1 && (best.base + best.len) ==
207     (NS_IN6ADDRSZ / NS_INT16SZ))
208     *tp++ = ':';
209     *tp++ = '\0';
210    
211     /*
212     * Check for overflow, copy, and we're done.
213     */
214     if ((socklen_t)(tp - tmp) > size) {
215     /* __set_errno (ENOSPC); */
216     return (NULL);
217     }
218     return strcpy(dst, tmp);
219     }
220     #endif /* IPV6 */
221    
222     #endif /* HAVE_INET_NTOP */

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23