Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
ip_address.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
20 #ifndef UTIL_IP_ADDRESS_H
21 #define UTIL_IP_ADDRESS_H
22 
23 
24 #ifdef __APPLE__
25 #include <libkern/OSByteOrder.h>
26 #define __bswap_constant_32(x) OSSwapInt32(x)
27 #include <machine/endian.h>
28 #else
29 #ifdef __ANDROID__
30 #include <byteswap.h>
31 #endif
32 
33 #endif
34 #include <errno.h>
35 
36 #ifndef _WIN32
37 #include <netinet/in.h> // struct sockadd
38 #include <arpa/inet.h> // inet_ntop
39 #include <netdb.h> // struct addrinfo
40 #endif
41 #include <stdbool.h>
42 #include <stdlib.h>
43 #include <stdio.h> // snprintf
44 #include <string.h> // memset
45 
46 #include "types.h"
47 
48 #define bytes_to_bits(x) (x * 8)
49 #define IPV6_ADDR_LEN 16 /* bytes */
50 #define IPV4_ADDR_LEN 4 /* bytes */
51 #define IPV6_ADDR_LEN_BITS bytes_to_bits(IPV6_ADDR_LEN)
52 #define IPV4_ADDR_LEN_BITS bytes_to_bits(IPV4_ADDR_LEN)
53 
54 /* Presentation format */
55 #ifndef INET_ADDRSTRLEN
56 #define INET_ADDRSTRLEN 16
57 #endif
58 
59 #ifndef INET6_ADDRSTRLEN
60 #define INET6_ADDRSTRLEN 46
61 #endif
62 //#define INET_MAX_ADDRSTRLEN INET6_ADDRSTRLEN
63 
64 #define IP_MAX_ADDR_LEN IPV6_ADDR_LEN
65 
66 #define DUMMY_PORT 1234
67 
68 typedef union {
69  struct {
70  u32 pad[3];
71  union {
72  struct in_addr as_inaddr;
73  u8 buffer[4];
74  u8 as_u8[4];
75  u16 as_u16[2];
76  u32 as_u32;
77  } v4;
78  };
79  union {
80  struct in6_addr as_in6addr;
81  u8 buffer[16];
82  u8 as_u8[16];
83  u16 as_u16[8];
84  u32 as_u32[4];
85  u64 as_u64[2];
86  } v6;
87 #if 0 /* removed as prone to error due to IPv4 padding */
88  u8 buffer[IP_MAX_ADDR_LEN];
89  u8 as_u8[IP_MAX_ADDR_LEN];
90  u16 as_u16[IP_MAX_ADDR_LEN >> 1];
91  u32 as_u32[IP_MAX_ADDR_LEN >> 2];
92  u64 as_u64[IP_MAX_ADDR_LEN >> 3];
93 #endif
94 } ip_address_t;
95 
96 #define MAXSZ_IP4_ADDRESS_ INET_ADDRSTRLEN - 1
97 #define MAXSZ_IP6_ADDRESS_ INET6_ADDRSTRLEN - 1
98 #define MAXSZ_IP_ADDRESS_ MAXSZ_IP6_ADDRESS_
99 #define MAXSZ_IP4_ADDRESS MAXSZ_IP4_ADDRESS_ + 1
100 #define MAXSZ_IP6_ADDRESS MAXSZ_IP6_ADDRESS_ + 1
101 #define MAXSZ_IP_ADDRESS MAXSZ_IP_ADDRESS_ + 1
102 
103 typedef struct {
104  int family;
105  ip_address_t address;
106  u8 len;
107 } ip_prefix_t;
108 
109 #define MAXSZ_PREFIX_ MAXSZ_IP_ADDRESS_ + 1 + 3
110 #define MAXSZ_PREFIX MAXSZ_PREFIX_ + 1
111 
112 extern const ip_address_t IPV4_LOOPBACK;
113 extern const ip_address_t IPV6_LOOPBACK;
114 extern const ip_address_t IPV4_ANY;
115 extern const ip_address_t IPV6_ANY;
116 extern const ip_address_t IP_ADDRESS_EMPTY;
117 
118 #define IP_ANY(family) (family == AF_INET) ? IPV4_ANY : IPV6_ANY
119 
120 
121 #define MAX_PORT 1 << (8 * sizeof(u16))
122 #define IS_VALID_PORT(x) ((x > 0) && ((int)x < MAX_PORT))
123 
124 #define MAXSZ_PORT_ 5
125 #define MAXSZ_PORT MAXSZ_PORT_ + 1
126 
127 #define IS_VALID_FAMILY(x) ((x == AF_INET) || (x == AF_INET6))
128 
129 /* IP address */
130 
131 int ip_address_get_family (const char * ip_address);
132 int ip_address_len (int family);
133 const u8 * ip_address_get_buffer(const ip_address_t * ip_address, int family);
134 int ip_address_ntop (const ip_address_t * ip_address, char *dst,
135  const size_t len, int family);
136 int ip_address_pton (const char *ip_address_str, ip_address_t * ip_address);
137 int ip_address_snprintf(char * s, size_t size, const ip_address_t * ip_address,
138  int family);
139 int ip_address_to_sockaddr(const ip_address_t * ip_address, struct sockaddr *sa,
140  int family);
141 int ip_address_cmp(const ip_address_t * ip1, const ip_address_t * ip2, int family);
142 int ip_address_empty(const ip_address_t * ip);
143 
144 /* Prefix */
145 
146 int ip_prefix_pton (const char *ip_address_str, ip_prefix_t * ip_prefix);
147 int ip_prefix_ntop_short (const ip_prefix_t * ip_prefix, char *dst, size_t size);
148 int ip_prefix_ntop (const ip_prefix_t * ip_prefix, char *dst, size_t size);
149 int ip_prefix_len (const ip_prefix_t * prefix);
150 bool ip_prefix_empty (const ip_prefix_t * prefix);
151 int ip_prefix_to_sockaddr(const ip_prefix_t * prefix, struct sockaddr *sa);
152 int ip_prefix_cmp(const ip_prefix_t * prefix1, const ip_prefix_t * prefix2);
153 
154 /* URL */
155 
156 #define MAXSZ_PROTO_ 8 /* inetX:// */
157 #define MAXSZ_PROTO MAXSZ_PROTO_ + NULLTERM
158 
159 #define MAXSZ_URL4_ MAXSZ_PROTO_ + MAXSZ_IP4_ADDRESS_ + MAXSZ_PORT_
160 #define MAXSZ_URL6_ MAXSZ_PROTO_ + MAXSZ_IP6_ADDRESS_ + MAXSZ_PORT_
161 #define MAXSZ_URL_ MAXSZ_URL6_
162 #define MAXSZ_URL4 MAXSZ_URL4_ + NULLTERM
163 #define MAXSZ_URL6 MAXSZ_URL6_ + NULLTERM
164 #define MAXSZ_URL MAXSZ_URL_ + NULLTERM
165 
166 int url_snprintf(char * s, size_t size, int family,
167  const ip_address_t * ip_address, u16 port);
168 
169 #endif /* UTIL_IP_ADDRESS_H */
ip_address_t
Definition: ip_address.h:68
ip_prefix_t
Definition: ip_address.h:103