summaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/api/ip_address.h
blob: f44fb1bc3906c63509a71af335e2a10657158f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file ip_address.h
 * \brief IP address type support
 */
#ifndef IP_ADDRESS_H
#define IP_ADDRESS_H

#include <arpa/inet.h>  // inet_ntop
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define __bswap_constant_32(x) OSSwapInt32(x)
#include <machine/endian.h>
#else
#include <endian.h>
#ifdef __ANDROID__
#include <byteswap.h>
#endif
#endif
#include <errno.h>
#include <netdb.h>       // struct addrinfo
#include <netinet/in.h>  // INET*_ADDRSTRLEN, IN*ADDR_LOOPBACK
#include <stdlib.h>
#include <string.h>  // memset

#include <hicn/api/types.h>

#define bytes_to_bits(x) (x * 8)
#define IPV6_ADDR_LEN 16 /* bytes */
#define IPV4_ADDR_LEN 4  /* bytes */
#define IPV6_ADDR_LEN_BITS bytes_to_bits(IPV6_ADDR_LEN)
#define IPV4_ADDR_LEN_BITS bytes_to_bits(IPV4_ADDR_LEN)

#define IP_MAX_ADDR_LEN IPV6_ADDR_LEN

#define DUMMY_PORT 1234

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef union {
  union {
    struct in_addr as_inaddr;
    u8 as_u8[4];
    u16 as_u16[2];
    u32 as_u32;
  } v4;
  union {
    struct in6_addr as_in6addr;
    u8 as_u8[16];
    u16 as_u16[8];
    u32 as_u32[4];
    u64 as_u64[2];
  } v6;
  u8 buffer[IP_MAX_ADDR_LEN];
  u8 as_u8[IP_MAX_ADDR_LEN];
  u16 as_u16[IP_MAX_ADDR_LEN >> 1];
  u32 as_u32[IP_MAX_ADDR_LEN >> 2];
  u64 as_u64[IP_MAX_ADDR_LEN >> 3];
} ip_address_t;

#define MAXSZ_IP4_ADDRESS_ INET_ADDRSTRLEN - 1
#define MAXSZ_IP6_ADDRESS_ INET6_ADDRSTRLEN - 1
#define MAXSZ_IP_ADDRESS_ MAXSZ_IP6_ADDRESS_
#define MAXSZ_IP4_ADDRESS MAXSZ_IP4_ADDRESS_ + 1
#define MAXSZ_IP6_ADDRESS MAXSZ_IP6_ADDRESS_ + 1
#define MAXSZ_IP_ADDRESS MAXSZ_IP_ADDRESS_ + 1

/* No htonl() with const */
static const ip_address_t IPV4_LOOPBACK = {
#if __BYTE_ORDER == __LITTLE_ENDIAN
#ifdef __ANDROID__
    .v4.as_inaddr.s_addr = bswap_32(INADDR_LOOPBACK),
#else
    .v4.as_inaddr.s_addr = __bswap_constant_32(INADDR_LOOPBACK),
#endif
#else
    .v4.as_inaddr.s_addr = INADDR_LOOPBACK,
#endif
};

static const ip_address_t IPV6_LOOPBACK = {
    .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT,
};

#define MAX_PORT 1 << (8 * sizeof(u16))
#define IS_VALID_PORT(x) ((x > 0) && (x < MAX_PORT))

#define MAXSZ_PORT_ 5
#define MAXSZ_PORT MAXSZ_PORT_ + 1

#define IS_VALID_FAMILY(x) ((x == AF_INET) || (x == AF_INET6))

static inline int ip_address_get_family(const char *ip_address) {
  struct addrinfo hint, *res = NULL;
  int rc;

  memset(&hint, '\0', sizeof hint);

  hint.ai_family = PF_UNSPEC;
  hint.ai_flags = AI_NUMERICHOST;

  rc = getaddrinfo(ip_address, NULL, &hint, &res);
  if (rc) {
    return -1;
  }
  rc = res->ai_family;
  freeaddrinfo(res);
  return rc;
}

static inline int ip_address_len(const ip_address_t *ip_address, int family) {
  return (family == AF_INET6) ? IPV6_ADDR_LEN
                              : (family == AF_INET) ? IPV4_ADDR_LEN : 0;
}

static inline int ip_address_ntop(const ip_address_t *ip_address, char *dst,
                                  const size_t len, int family) {
  const char *s = inet_ntop(family, ip_address->buffer, dst, len);
  return (s ? 1 : -1);
}

/*
 * Parse ip addresses in presentation format, or prefixes (in bits, separated by
 * a slash)
 */
static inline int ip_address_pton(const char *ip_address_str,
                                  ip_address_t *ip_address) {
  int pton_fd;
#ifdef IP_ADDRESS_PREFIX
  char *p;
  char *eptr;
  u32 dst_len;
#endif /* IP_ADDRESS_PREFIX */
  char *addr = strdup(ip_address_str);
  int family;

#ifdef IP_ADDRESS_PREFIX
  p = strchr(addr, '/');
  if (!p) {
    dst_len = 0;  // until we get the ip address family
  } else {
    dst_len = strtoul(p + 1, &eptr, 10);
    *p = 0;
  }
#endif /* IP_ADDRESS_PREFIX */

  family = ip_address_get_family(addr);

  switch (family) {
    case AF_INET6:
#ifdef IP_ADDRESS_PREFIX
      if (dst_len > IPV6_ADDR_LEN_BITS) goto ERR;
#endif /* IP_ADDRESS_PREFIX */
      pton_fd = inet_pton(AF_INET6, addr, &ip_address->buffer);
      break;
    case AF_INET:
#ifdef IP_ADDRESS_PREFIX
      if (dst_len > IPV4_ADDR_LEN_BITS) goto ERR;
#endif /* IP_ADDRESS_PREFIX */
      pton_fd = inet_pton(AF_INET, addr, &ip_address->buffer);
      break;
    default:
      goto ERR;
  }

  //   0 = not in presentation format
  // < 0 = other error (use perror)
  if (pton_fd <= 0) {
    goto ERR;
  }

  return 1;
ERR:
  free(addr);
  return -1;
}

static inline int ip_address_snprintf(char *s, size_t size,
                                      const ip_address_t *ip_address,
                                      int family) {
  size_t len = family == AF_INET ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN;
  const char *rc = inet_ntop(family, ip_address->buffer, s, len);
  return rc ? strlen(rc) : -1;
}

static inline int ip_address_to_sockaddr(const ip_address_t *ip_address,
                                         struct sockaddr *sockaddr_address,
                                         int family) {
  struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *)sockaddr_address;
  struct sockaddr_in *tmp4 = (struct sockaddr_in *)sockaddr_address;

  switch (family) {
    case AF_INET6:
      tmp6->sin6_family = AF_INET6;
      tmp6->sin6_port = DUMMY_PORT;
      tmp6->sin6_scope_id = 0;
      memcpy(&tmp6->sin6_addr, ip_address->buffer, IPV6_ADDR_LEN);
      break;
    case AF_INET:
      tmp4->sin_family = AF_INET;
      tmp4->sin_port = DUMMY_PORT;
      memcpy(&tmp4->sin_addr, ip_address->buffer, IPV4_ADDR_LEN);
      break;
    default:
      return -1;
  }

  return 1;
}

#endif /* IP_ADDRESS_H */