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