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