]>
jfr.im git - irc/quakenet/newserv.git/blob - lib/irc_ipv6.c
1 /* IPv6 helper functions */
11 #warning This source file is probably GPLed, it needs relicensing.
13 /** Convert an IP address to printable ASCII form.
14 * This is generally deprecated in favor of ircd_ntoa_r().
15 * @param[in] in Address to convert.
16 * @return Pointer to a static buffer containing the readable form.
18 const char* ircd_ntoa(const struct irc_in_addr
* in
)
20 static char buf
[SOCKIPLEN
];
21 return ircd_ntoa_r(buf
, in
);
24 /** Convert an IP address to printable ASCII form.
25 * @param[out] buf Output buffer to write to.
26 * @param[in] in Address to format.
27 * @return Pointer to the output buffer \a buf.
29 const char* ircd_ntoa_r(char* buf
, const struct irc_in_addr
* in
)
34 if (irc_in_addr_is_ipv4(in
)) {
37 pch
= (unsigned char*)&in
->in6_16
[6];
38 sprintf(buf
,"%d.%d.%d.%d",pch
[0],pch
[1],pch
[2],pch
[3]);
41 unsigned int pos
, part
, max_start
, max_zeros
, curr_zeros
, ii
;
43 /* Find longest run of zeros. */
44 for (max_start
= ii
= 1, max_zeros
= curr_zeros
= 0; ii
< 8; ++ii
) {
47 else if (curr_zeros
> max_zeros
) {
48 max_start
= ii
- curr_zeros
;
49 max_zeros
= curr_zeros
;
53 if (curr_zeros
> max_zeros
) {
54 max_start
= ii
- curr_zeros
;
55 max_zeros
= curr_zeros
;
58 /* Print out address. */
59 /** Append \a CH to the output buffer. */
60 #define APPEND(CH) do { buf[pos++] = (CH); } while (0)
61 for (pos
= ii
= 0; (ii
< 8); ++ii
) {
62 if ((max_zeros
> 0) && (ii
== max_start
)) {
67 part
= ntohs(in
->in6_16
[ii
]);
68 pos
+=sprintf(buf
+pos
,"%x",part
);
74 /* Nul terminate and return number of characters used. */
80 /** Attempt to parse an IPv4 address into a network-endian form.
81 * @param[in] input Input string.
82 * @param[out] output Network-endian representation of the address.
83 * @param[out] pbits Number of bits found in pbits.
84 * @return Number of characters used from \a input, or 0 if the parse failed.
87 ircd_aton_ip4(const char *input
, unsigned int *output
, unsigned char *pbits
)
89 unsigned int dots
= 0, pos
= 0, part
= 0, ip
= 0, bits
;
91 /* Intentionally no support for bizarre IPv4 formats (plain
92 * integers, octal or hex components) -- only vanilla dotted
98 while (1) switch (input
[pos
]) {
103 ip
|= part
<< (24 - 8 * dots
);
109 if (input
[++pos
] == '.')
111 ip
|= part
<< (24 - 8 * dots
++);
113 if (input
[pos
] == '*') {
114 while (input
[++pos
] == '*') ;
115 if (input
[pos
] != '\0')
124 if (!pbits
|| !IsDigit(input
[pos
+ 1]))
126 for (bits
= 0; IsDigit(input
[++pos
]); )
127 bits
= bits
* 10 + input
[pos
] - '0';
131 case '0': case '1': case '2': case '3': case '4':
132 case '5': case '6': case '7': case '8': case '9':
133 part
= part
* 10 + input
[pos
++] - '0';
142 /** Parse a numeric IPv4 or IPv6 address into an irc_in_addr.
143 * @param[in] input Input buffer.
144 * @param[out] ip Receives parsed IP address.
145 * @param[out] pbits If non-NULL, receives number of bits specified in address mask.
146 * @return Number of characters used from \a input, or 0 if the
147 * address was unparseable or malformed.
150 ipmask_parse(const char *input
, struct irc_in_addr
*ip
, unsigned char *pbits
)
157 memset(ip
, 0, sizeof(*ip
));
158 colon
= strchr(input
, ':');
159 dot
= strchr(input
, '.');
161 if (colon
&& (!dot
|| (dot
> colon
))) {
162 unsigned int part
= 0, pos
= 0, ii
= 0, colon
= 8;
163 const char *part_start
= NULL
;
165 /* Parse IPv6, possibly like ::127.0.0.1.
166 * This is pretty straightforward; the only trick is borrowed
167 * from Paul Vixie (BIND): when it sees a "::" continue as if
168 * it were a single ":", but note where it happened, and fill
169 * with zeros afterward.
171 if (input
[pos
] == ':') {
172 if ((input
[pos
+1] != ':') || (input
[pos
+2] == ':'))
176 part_start
= input
+ pos
;
178 while (ii
< 8) switch (input
[pos
]) {
180 case '0': case '1': case '2': case '3': case '4':
181 case '5': case '6': case '7': case '8': case '9':
182 chval
= input
[pos
] - '0';
184 part
= (part
<< 4) | chval
;
189 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
190 chval
= input
[pos
] - 'A' + 10;
192 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
193 chval
= input
[pos
] - 'a' + 10;
196 part_start
= input
+ ++pos
;
197 if (input
[pos
] == '.')
199 ip
->in6_16
[ii
++] = htons(part
);
201 if (input
[pos
] == ':') {
211 len
= ircd_aton_ip4(part_start
, &ip4
, pbits
);
212 if (!len
|| (ii
> 6))
214 ip
->in6_16
[ii
++] = htons(ntohl(ip4
) >> 16);
215 ip
->in6_16
[ii
++] = htons(ntohl(ip4
) & 65535);
218 pos
= part_start
+ len
- input
;
222 if (!pbits
|| !IsDigit(input
[pos
+ 1]))
224 ip
->in6_16
[ii
++] = htons(part
);
225 for (part
= 0; IsDigit(input
[++pos
]); )
226 part
= part
* 10 + input
[pos
] - '0';
232 while (input
[++pos
] == '*') ;
233 if (input
[pos
] != '\0' || colon
< 8)
239 ip
->in6_16
[ii
++] = htons(part
);
240 if (colon
== 8 && ii
< 8)
251 /* Shift stuff after "::" up and fill middle with zeros. */
252 for (jj
= 0; jj
< ii
- colon
; jj
++)
253 ip
->in6_16
[7 - jj
] = ip
->in6_16
[ii
- jj
- 1];
254 for (jj
= 0; jj
< 8 - ii
; jj
++)
255 ip
->in6_16
[colon
+ jj
] = 0;
258 } else if (dot
|| strchr(input
, '/')) {
260 int len
= ircd_aton_ip4(input
, &addr
, pbits
);
262 ip
->in6_16
[5] = htons(65535);
263 ip
->in6_16
[6] = htons(ntohl(addr
) >> 16);
264 ip
->in6_16
[7] = htons(ntohl(addr
) & 65535);
269 } else if (input
[0] == '*') {
270 unsigned int pos
= 0;
271 while (input
[++pos
] == '*') ;
272 if (input
[pos
] != '\0')
277 } else return 0; /* parse failed */
280 /* from numnicks.c */
283 * Converts a numeric to the corresponding character.
284 * The following characters are currently known to be forbidden:
286 * '\\0' : Because we use '\\0' as end of line.
288 * ' ' : Because parse_*() uses this as parameter separator.
290 * ':' : Because parse_server() uses this to detect if a prefix is a
293 * '+' : Because m_nick() uses this to determine if parv[6] is a
296 * '&', '#', '$', '@' and '%' :
297 * Because m_message() matches these characters to detect special cases.
299 static const char convert2y
[] = {
300 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
301 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
302 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
303 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
306 /** Converts a character to its (base64) numnick value. */
307 static const unsigned int convert2n
[] = {
308 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
309 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
310 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
311 52,53,54,55,56,57,58,59,60,61, 0, 0, 0, 0, 0, 0,
312 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
313 15,16,17,18,19,20,21,22,23,24,25,62, 0,63, 0, 0,
314 0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
315 41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0,
317 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
318 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
319 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
320 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
321 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
322 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
323 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
324 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
327 /** Number of bits encoded in one numnick character. */
329 /** Bitmask to select value of next numnick character. */
330 #define NUMNICKMASK 63 /* (NUMNICKBASE-1) */
331 /** Number of servers representable in a numnick. */
335 /** Convert a string to its value as a numnick.
336 * @param[in] s Numnick string to decode.
337 * @return %Numeric nickname value.
339 unsigned int base64toint(const char* s
)
341 unsigned int i
= convert2n
[(unsigned char) *s
++];
344 i
+= convert2n
[(unsigned char) *s
++];
349 /** Encode a number as a numnick.
350 * @param[out] buf Output buffer.
351 * @param[in] v Value to encode.
352 * @param[in] count Number of numnick digits to write to \a buf.
354 const char* inttobase64(char* buf
, unsigned int v
, unsigned int count
)
358 buf
[--count
] = convert2y
[(v
& NUMNICKMASK
)];
364 /** Number of bits encoded in one numnick character. */
367 /** Encode an IP address in the base64 used by numnicks.
368 * For IPv4 addresses (including IPv4-mapped and IPv4-compatible IPv6
369 * addresses), the 32-bit host address is encoded directly as six
372 * For IPv6 addresses, each 16-bit address segment is encoded as three
373 * characters, but the longest run of zero segments is encoded using an
375 * @param[out] buf Output buffer to write to.
376 * @param[in] addr IP address to encode.
377 * @param[in] count Number of bytes writable to \a buf.
378 * @param[in] v6_ok If non-zero, peer understands base-64 encoded IPv6 addresses.
380 const char* iptobase64(char* buf
, const struct irc_in_addr
* addr
, unsigned int count
, int v6_ok
)
382 if (irc_in_addr_is_ipv4(addr
)) {
384 inttobase64(buf
, (ntohs(addr
->in6_16
[6]) << 16) | ntohs(addr
->in6_16
[7]), 6);
387 if (addr
->in6_16
[0] == htons(0x2002))
388 inttobase64(buf
, (ntohs(addr
->in6_16
[1]) << 16) | ntohs(addr
->in6_16
[2]), 6);
390 strcpy(buf
, "AAAAAA");
392 unsigned int max_start
, max_zeros
, curr_zeros
, zero
, ii
;
396 /* Can start by printing out the leading non-zero parts. */
397 for (ii
= 0; (addr
->in6_16
[ii
]) && (ii
< 8); ++ii
) {
398 inttobase64(output
, ntohs(addr
->in6_16
[ii
]), 3);
401 /* Find the longest run of zeros. */
402 for (max_start
= zero
= ii
, max_zeros
= curr_zeros
= 0; ii
< 8; ++ii
) {
403 if (!addr
->in6_16
[ii
])
405 else if (curr_zeros
> max_zeros
) {
406 max_start
= ii
- curr_zeros
;
407 max_zeros
= curr_zeros
;
411 if (curr_zeros
> max_zeros
) {
412 max_start
= ii
- curr_zeros
;
413 max_zeros
= curr_zeros
;
416 /* Print the rest of the address */
417 for (ii
= zero
; ii
< 8; ) {
418 if ((ii
== max_start
) && max_zeros
) {
422 inttobase64(output
, ntohs(addr
->in6_16
[ii
]), 3);
432 /** Decode an IP address from base64.
433 * @param[in] input Input buffer to decode.
434 * @param[out] addr IP address structure to populate.
436 void base64toip(const char* input
, struct irc_in_addr
* addr
)
438 memset(addr
, 0, sizeof(*addr
));
439 if (strlen(input
) == 6) {
440 unsigned int in
= base64toint(input
);
441 /* An all-zero address should stay that way. */
443 addr
->in6_16
[5] = htons(65535);
444 addr
->in6_16
[6] = htons(in
>> 16);
445 addr
->in6_16
[7] = htons(in
& 65535);
448 unsigned int pos
= 0;
452 for (left
= (25 - strlen(input
)) / 3 - pos
; left
; left
--)
453 addr
->in6_16
[pos
++] = 0;
456 unsigned short accum
= convert2n
[(unsigned char)*input
++];
457 accum
= (accum
<< NUMNICKLOG
) | convert2n
[(unsigned char)*input
++];
458 accum
= (accum
<< NUMNICKLOG
) | convert2n
[(unsigned char)*input
++];
459 addr
->in6_16
[pos
++] = ntohs(accum
);