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