]> arthur.barton.de Git - ngircd-alex.git/blob - src/ipaddr/ng_ipaddr.h
IPv6 support.
[ngircd-alex.git] / src / ipaddr / ng_ipaddr.h
1 /*
2  * Functions for AF_ agnostic ipv4/ipv6 handling.
3  *
4  * (c) 2008 Florian Westphal <fw@strlen.de>, public domain.
5  */
6
7 #ifndef NG_IPADDR_HDR
8 #define NG_IPADDR_HDR
9 #include "portab.h"
10
11 #ifdef HAVE_ARPA_INET_H
12 # include <arpa/inet.h>
13 #else
14 # define PF_INET AF_INET
15 #endif
16
17
18 #ifdef WANT_IPV6
19 #define NG_INET_ADDRSTRLEN      INET6_ADDRSTRLEN
20 #else
21 #define NG_INET_ADDRSTRLEN      16
22 #endif
23
24
25 #ifdef WANT_IPV6
26 typedef union {
27         struct sockaddr sa;
28         struct sockaddr_in sin4;
29         struct sockaddr_in6 sin6;
30 } ng_ipaddr_t;
31 #else
32 /* assume compiler can't deal with typedef struct {... */
33 struct NG_IP_ADDR_DONTUSE {
34         struct sockaddr_in sin4;
35 };
36 typedef struct NG_IP_ADDR_DONTUSE ng_ipaddr_t;
37 #endif
38
39
40 static inline int
41 ng_ipaddr_af(const ng_ipaddr_t *a)
42 {
43 #ifdef WANT_IPV6
44         return a->sa.sa_family;
45 #else
46         assert(a->sin4.sin_family == 0 || a->sin4.sin_family == AF_INET);
47         return a->sin4.sin_family;
48 #endif
49 }
50
51
52 static inline socklen_t
53 ng_ipaddr_salen(const ng_ipaddr_t *a)
54 {
55 #ifdef WANT_IPV6
56         assert(a->sa.sa_family == AF_INET || a->sa.sa_family == AF_INET6);
57         if (a->sa.sa_family == AF_INET6)
58                 return sizeof(a->sin6);
59 #endif
60         assert(a->sin4.sin_family == AF_INET);
61         return sizeof(a->sin4);
62 }
63
64
65 static inline UINT16
66 ng_ipaddr_getport(const ng_ipaddr_t *a)
67 {
68 #ifdef WANT_IPV6
69         int af = a->sa.sa_family;
70
71         assert(af == AF_INET || af == AF_INET6);
72
73         if (af == AF_INET6)
74                 return ntohs(a->sin6.sin6_port);
75 #endif /* WANT_IPV6 */
76         assert(a->sin4.sin_family == AF_INET);
77         return ntohs(a->sin4.sin_port);
78 }
79
80 /*
81  * init a ng_ipaddr_t object.
82  * @param addr: pointer to ng_ipaddr_t to initialize.
83  * @param ip_str: ip address in dotted-decimal (ipv4) or hexadecimal (ipv6) notation
84  *                if ip_str is NULL it is treated as 0.0.0.0/[::]
85  * @param port: transport layer port number to use.
86  */
87 GLOBAL bool ng_ipaddr_init PARAMS((ng_ipaddr_t *addr, const char *ip_str, UINT16 port));
88
89 /* set sin4/sin6_port, depending on a->sa_family */
90 GLOBAL void ng_ipaddr_setport PARAMS((ng_ipaddr_t *a, UINT16 port));
91
92 /* return true if a and b have the same IP address. If a and b have different AF, return false. */
93 GLOBAL bool ng_ipaddr_ipequal PARAMS((const ng_ipaddr_t *a, const ng_ipaddr_t *b));
94
95
96 #ifdef WANT_IPV6
97 /* convert struct sockaddr to string, returns pointer to static buffer */
98 GLOBAL const char *ng_ipaddr_tostr PARAMS((const ng_ipaddr_t *addr));
99
100 /* convert struct sockaddr to string. dest must be NG_INET_ADDRSTRLEN bytes long */
101 GLOBAL bool ng_ipaddr_tostr_r PARAMS((const ng_ipaddr_t *addr, char *dest));
102 #else
103 static inline const char *
104 ng_ipaddr_tostr(const ng_ipaddr_t *addr) { return inet_ntoa(addr->sin4.sin_addr); }
105
106 static inline bool
107 ng_ipaddr_tostr_r(const ng_ipaddr_t *addr, char *d)
108 {
109         strlcpy(d, inet_ntoa(addr->sin4.sin_addr), NG_INET_ADDRSTRLEN);
110         return true;
111 }
112 #endif
113 #endif
114
115 /* -eof- */