UniRec  2.9.3
ipaddr.h
Go to the documentation of this file.
1 
38 /*
39  * Copyright (C) 2013,2014 CESNET
40  *
41  * LICENSE TERMS
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  * notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  * notice, this list of conditions and the following disclaimer in
50  * the documentation and/or other materials provided with the
51  * distribution.
52  * 3. Neither the name of the Company nor the names of its contributors
53  * may be used to endorse or promote products derived from this
54  * software without specific prior written permission.
55  *
56  * ALTERNATIVELY, provided that this notice is retained in full, this
57  * product may be distributed under the terms of the GNU General Public
58  * License (GPL) version 2 or later, in which case the provisions
59  * of the GPL apply INSTEAD OF those given above.
60  *
61  * This software is provided ``as is'', and any express or implied
62  * warranties, including, but not limited to, the implied warranties of
63  * merchantability and fitness for a particular purpose are disclaimed.
64  * In no event shall the company or contributors be liable for any
65  * direct, indirect, incidental, special, exemplary, or consequential
66  * damages (including, but not limited to, procurement of substitute
67  * goods or services; loss of use, data, or profits; or business
68  * interruption) however caused and on any theory of liability, whether
69  * in contract, strict liability, or tort (including negligence or
70  * otherwise) arising in any way out of the use of this software, even
71  * if advised of the possibility of such damage.
72  *
73  */
74 
75 #ifndef _IPADDR_H_
76 #define _IPADDR_H_
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
82 #include "inline.h"
83 
84 #include <stdint.h>
85 #include <string.h>
86 #ifndef __WIN32
87 #include <arpa/inet.h>
88 #include <sys/socket.h>
89 #include <sys/types.h>
90 #else
91 #define ntohl(x) ((x & 0x0000000ff) << 24) | ((x & 0x0000ff00) << 8) | \
92  ((x & 0x00ff0000) >> 8) | ((x & 0xff000000) >> 24)
93 #define htonl(x) ntohl(x)
94 #endif
95 
101 typedef union ip_addr_u {
105  uint8_t bytes[16];
109  uint8_t ui8[16];
113  uint16_t ui16[8];
117  uint32_t ui32[4];
121  uint64_t ui64[2];
122 } ip_addr_t;
123 
124 
131 INLINE int ip_is4(const ip_addr_t *addr)
132 {
133  return (addr->ui64[0] == 0 && addr->ui32[3] == 0xffffffff);
134 }
135 
143 INLINE int ip_is6(const ip_addr_t *addr)
144 {
145  return !ip_is4(addr);
146 }
147 
158 {
159  return ntohl(addr->ui32[2]);
160 }
161 
172 {
173  return (char *) &addr->bytes[8];
174 }
175 
176 
184 {
185  ip_addr_t a;
186  a.ui64[0] = 0;
187  a.ui32[2] = htonl(i);
188  a.ui32[3] = 0xffffffff;
189  return a;
190 }
191 
200 {
201  ip_addr_t a;
202  a.ui64[0] = 0;
203  a.bytes[8] = b[0];
204  a.bytes[9] = b[1];
205  a.bytes[10] = b[2];
206  a.bytes[11] = b[3];
207  a.ui32[3] = 0xffffffff;
208  return a;
209 }
210 
219 {
220  ip_addr_t a;
221  a.ui64[0] = 0;
222  a.bytes[8] = b[3];
223  a.bytes[9] = b[2];
224  a.bytes[10] = b[1];
225  a.bytes[11] = b[0];
226  a.ui32[3] = 0xffffffff;
227  return a;
228 }
229 
237 {
238  ip_addr_t a;
239  memcpy(&a, b, 16);
240  return a;
241 }
242 
250 {
251  ip_addr_t a;
252  int i;
253  for (i = 0; i < 16; i++) {
254  a.bytes[i] = b[15-i];
255  }
256  return a;
257 }
258 
266 INLINE int ip_cmp(const ip_addr_t *addr1, const ip_addr_t *addr2)
267 {
268  return memcmp((const char *)addr1, (const char *)addr2, 16);
269 }
270 
283 INLINE int ip_is_null(const ip_addr_t *addr)
284 {
285  if (addr->ui64[0] == 0) {
286  if (addr->ui64[1] == 0 || (addr->ui32[2] == 0 && addr->ui32[3] == 0xffffffff)) {
287  return 1;
288  }
289  }
290  return 0;
291 }
292 
293 #ifndef __WIN32
294 
301 INLINE int ip_from_str(const char *str, ip_addr_t *addr)
302 {
303  char tmp[16];
304  if (strchr(str, ':') == NULL) { // IPv4
305  if (inet_pton(AF_INET, str, (void *) tmp) != 1) {
306  return 0; // err
307  }
308  *addr = ip_from_4_bytes_be(tmp);
309  return 1;
310  } else { // IPv6
311  if (inet_pton(AF_INET6, str, (void *) tmp) != 1) {
312  return 0; // err
313  }
314  *addr = ip_from_16_bytes_be(tmp);
315  return 1;
316  }
317 }
318 
325 INLINE void ip_to_str(const ip_addr_t *addr, char *str)
326 {
327  if (ip_is4(addr)) { // IPv4
328  inet_ntop(AF_INET, ip_get_v4_as_bytes(addr), str, INET6_ADDRSTRLEN);
329  } else { // IPv6
330  inet_ntop(AF_INET6, addr, str, INET6_ADDRSTRLEN);
331  }
332 }
333 #endif
334 
335 #ifdef __cplusplus
336 }
337 #endif
338 
339 #endif
340 
INLINE ip_addr_t ip_from_16_bytes_le(char b[16])
Definition: ipaddr.h:249
INLINE uint32_t ip_get_v4_as_int(ip_addr_t *addr)
Definition: ipaddr.h:157
INLINE ip_addr_t ip_from_16_bytes_be(char b[16])
Definition: ipaddr.h:236
INLINE int ip_is_null(const ip_addr_t *addr)
Definition: ipaddr.h:283
uint8_t bytes[16]
Definition: ipaddr.h:105
INLINE int ip_from_str(const char *str, ip_addr_t *addr)
Definition: ipaddr.h:301
INLINE int ip_cmp(const ip_addr_t *addr1, const ip_addr_t *addr2)
Definition: ipaddr.h:266
INLINE ip_addr_t ip_from_4_bytes_be(char b[4])
Definition: ipaddr.h:199
uint32_t ui32[4]
Definition: ipaddr.h:117
INLINE ip_addr_t ip_from_int(uint32_t i)
Definition: ipaddr.h:183
#define INLINE
Definition: inline.h:12
INLINE int ip_is6(const ip_addr_t *addr)
Definition: ipaddr.h:143
memcpy(buffer, rec, ur_rec_size(tmplt, rec))
INLINE void ip_to_str(const ip_addr_t *addr, char *str)
Definition: ipaddr.h:325
INLINE int ip_is4(const ip_addr_t *addr)
Definition: ipaddr.h:131
INLINE ip_addr_t ip_from_4_bytes_le(char b[4])
Definition: ipaddr.h:218
uint64_t ui64[2]
Definition: ipaddr.h:121
INLINE char * ip_get_v4_as_bytes(const ip_addr_t *addr)
Definition: ipaddr.h:171