]>
Commit | Line | Data |
---|---|---|
526e7c1d P |
1 | /* IPv6 helper prototypes */ |
2 | ||
3 | #ifndef __irc_ipv6_H | |
4 | #define __irc_ipv6_H | |
5 | ||
e03b3e2b CP |
6 | #include <limits.h> |
7 | ||
526e7c1d P |
8 | /* from res.h */ |
9 | ||
10 | /** Structure to store an IP address. */ | |
11 | struct irc_in_addr | |
12 | { | |
13 | unsigned short in6_16[8]; /**< IPv6 encoded parts, little-endian. */ | |
14 | }; | |
15 | ||
16 | /** Structure to store an IP address and port number. */ | |
17 | struct irc_sockaddr | |
18 | { | |
19 | struct irc_in_addr addr; /**< IP address. */ | |
20 | unsigned short port; /**< Port number, host-endian. */ | |
21 | }; | |
22 | ||
dfa52f1d P |
23 | #define irc_bitlen(ADDR, BITS) (irc_in_addr_is_ipv4(ADDR) ? (BITS) - 96 : (BITS)) |
24 | ||
526e7c1d P |
25 | /** Evaluate to non-zero if \a ADDR is a valid address (not all 0s and not all 1s). */ |
26 | #define irc_in_addr_valid(ADDR) (((ADDR)->in6_16[0] && ~(ADDR)->in6_16[0]) \ | |
27 | || (ADDR)->in6_16[1] != (ADDR)->in6_16[0] \ | |
28 | || (ADDR)->in6_16[2] != (ADDR)->in6_16[0] \ | |
29 | || (ADDR)->in6_16[3] != (ADDR)->in6_16[0] \ | |
30 | || (ADDR)->in6_16[4] != (ADDR)->in6_16[0] \ | |
31 | || (ADDR)->in6_16[5] != (ADDR)->in6_16[0] \ | |
32 | || (ADDR)->in6_16[6] != (ADDR)->in6_16[0] \ | |
33 | || (ADDR)->in6_16[7] != (ADDR)->in6_16[0]) | |
34 | /** Evaluate to non-zero if \a ADDR (of type struct irc_in_addr) is an IPv4 address. */ | |
35 | #define irc_in_addr_is_ipv4(ADDR) (!(ADDR)->in6_16[0] && !(ADDR)->in6_16[1] && !(ADDR)->in6_16[2] \ | |
7e7f5936 P |
36 | && !(ADDR)->in6_16[3] && !(ADDR)->in6_16[4] && ((!(ADDR)->in6_16[5] \ |
37 | && (ADDR)->in6_16[6]) || (ADDR)->in6_16[5] == 65535)) | |
526e7c1d P |
38 | /** Evaluate to non-zero if \a A is a different IP than \a B. */ |
39 | #define irc_in_addr_cmp(A,B) (irc_in_addr_is_ipv4(A) ? ((A)->in6_16[6] != (B)->in6_16[6] \ | |
40 | || (A)->in6_16[7] != (B)->in6_16[7] || !irc_in_addr_is_ipv4(B)) \ | |
41 | : memcmp((A), (B), sizeof(struct irc_in_addr))) | |
42 | /** Evaluate to non-zero if \a ADDR is a loopback address. */ | |
43 | #define irc_in_addr_is_loopback(ADDR) (!(ADDR)->in6_16[0] && !(ADDR)->in6_16[1] && !(ADDR)->in6_16[2] \ | |
44 | && !(ADDR)->in6_16[3] && !(ADDR)->in6_16[4] \ | |
45 | && ((!(ADDR)->in6_16[5] \ | |
46 | && ((!(ADDR)->in6_16[6] && (ADDR)->in6_16[7] == htons(1)) \ | |
47 | || (ntohs((ADDR)->in6_16[6]) & 0xff00) == 0x7f00)) \ | |
48 | || (((ADDR)->in6_16[5] == 65535) \ | |
49 | && (ntohs((ADDR)->in6_16[6]) & 0xff00) == 0x7f00))) | |
50 | ||
51 | ||
52 | /* from ircd_defs.h */ | |
53 | ||
54 | /** Maximum length of a numeric IP (v4 or v6) address. | |
55 | * "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255" | |
56 | */ | |
57 | #define SOCKIPLEN 45 | |
58 | ||
59 | /* from ircd_string.h */ | |
60 | ||
61 | extern const char* ircd_ntoa(const struct irc_in_addr* addr); | |
62 | extern const char* ircd_ntoa_r(char* buf, const struct irc_in_addr* addr); | |
63 | #define ircd_aton(ADDR, STR) ipmask_parse((STR), (ADDR), NULL) | |
64 | extern int ipmask_parse(const char *in, struct irc_in_addr *mask, unsigned char *bits_ptr); | |
873f4217 | 65 | extern int ipmask_check(const struct irc_in_addr *, const struct irc_in_addr *, unsigned char); |
4338f313 | 66 | void ip_canonicalize_tunnel(struct irc_in_addr *out, const struct irc_in_addr *in); |
526e7c1d P |
67 | |
68 | #define IPtostr(ipaddr) ircd_ntoa(&(ipaddr)) | |
7b1dadec | 69 | #define irc_in_addr_v4_to_int(ADDR) ((ntohs((ADDR)->in6_16[6]) << 16) | ntohs((ADDR)->in6_16[7])) |
526e7c1d P |
70 | |
71 | /* from numnicks.h */ | |
72 | ||
73 | extern const char* iptobase64(char* buf, const struct irc_in_addr* addr, unsigned int count, int v6_ok); | |
74 | extern void base64toip(const char* s, struct irc_in_addr* addr); | |
75 | ||
76 | /* from ircd_chattr.h */ | |
77 | ||
78 | /** Array mapping characters to attribute bitmasks. */ | |
79 | extern const unsigned int IRCD_CharAttrTab[]; | |
80 | ||
81 | #define NTL_DIGIT 0x0008 /**< 0123456789 */ | |
82 | ||
83 | /** Test whether a character is a digit. */ | |
84 | #define IsDigit(c) (IRCD_CharAttrTab[(c) - CHAR_MIN] & NTL_DIGIT) | |
85 | ||
86 | #endif /* __irc_ipv6_H */ |