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