aboutsummaryrefslogtreecommitdiffstats
path: root/lib/src/util/ip_address.c
blob: 3624a6fca51c22ab3f4a0729ae78c1bd95215014 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * Copyright (c) 2021 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.c
 * \brief Implementation of IP address type
 */

#include <hicn/util/ip_address.h>
#include <hicn/util/log.h>
#include <hicn/util/sstrncpy.h>
#include <hicn/common.h>

#if __BYTE_ORDER == __LITTLE_ENDIAN
#ifdef __ANDROID__
#define SWAP(x) bswap_32 (x)
#else
#define SWAP(x) __bswap_constant_32 (x)
#endif
#else
#define SWAP(x) x
#endif

/* No htonl() with const */
const hicn_ip_address_t IPV4_LOOPBACK = {
  .v4.as_inaddr.s_addr = SWAP (INADDR_LOOPBACK),
};

const hicn_ip_address_t IPV6_LOOPBACK = {
  .v6.as_in6addr = IN6ADDR_LOOPBACK_INIT,
};

const hicn_ip_address_t IPV4_ANY = {
  .v4.as_inaddr.s_addr = INADDR_ANY,
};

const hicn_ip_address_t IPV6_ANY = {
  .v6.as_in6addr = IN6ADDR_ANY_INIT,
};

const ipv4_address_t IP4_ADDRESS_EMPTY = {
  .as_u32 = 0,
};

const ipv6_address_t IP6_ADDRESS_EMPTY = {
  .as_u64 = { 0, 0 },
};

const hicn_ip_address_t IP_ADDRESS_EMPTY = {
  .v6.as_u64 = { 0, 0 },
};

/* IP address */

int
hicn_ip_address_get_family (const hicn_ip_address_t *address)
{
  return hicn_ip_address_is_v4 (address) ? AF_INET : AF_INET6;
}

int
hicn_ip_address_str_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;
}

int
hicn_ip_address_len (int family)
{
  return (family == AF_INET6) ? IPV6_ADDR_LEN :
	 (family == AF_INET)  ? IPV4_ADDR_LEN :
				      0;
}

int
hicn_ip_address_get_len (const hicn_ip_address_t *ip_address)
{
  return hicn_ip_address_len (hicn_ip_address_get_family (ip_address));
}

int
hicn_ip_address_get_len_bits (const hicn_ip_address_t *ip_address)
{
  return bytes_to_bits (hicn_ip_address_get_len (ip_address));
}

int
hicn_ip_address_ntop (const hicn_ip_address_t *ip_address, char *dst,
		      const size_t len, int family)
{
  const char *s;
  switch (family)
    {
    case AF_INET:
      s = inet_ntop (AF_INET, ip_address->v4.buffer, dst, (socklen_t) len);
      break;
    case AF_INET6:
      s = inet_ntop (AF_INET6, ip_address->v6.buffer, dst, (socklen_t) len);
      break;
    default:
      return -1;
    }
  return (s ? 1 : -1);
}

/*
 * Parse ip addresses in presentation format
 */
int
hicn_ip_address_pton (const char *hicn_ip_address_str,
		      hicn_ip_address_t *ip_address)
{
  int pton_fd;
  int family;

  family = hicn_ip_address_str_get_family (hicn_ip_address_str);

  switch (family)
    {
    case AF_INET:
      ip_address->pad[0] = 0;
      ip_address->pad[1] = 0;
      ip_address->pad[2] = 0;
      pton_fd =
	inet_pton (AF_INET, hicn_ip_address_str, &ip_address->v4.buffer);
      break;
    case AF_INET6:
      pton_fd =
	inet_pton (AF_INET6, hicn_ip_address_str, &ip_address->v6.buffer);
      break;
    default:
      return -1;
    }

  //   0 = not in presentation format
  // < 0 = other error (use perror)
  if (pton_fd <= 0)
    return -1;

  return 0;
}

int
hicn_ip_address_snprintf (char *s, size_t size,
			  const hicn_ip_address_t *ip_address)
{
  const char *rc;
  int family = hicn_ip_address_get_family (ip_address);
  switch (family)
    {
    case AF_INET:
      if (size < INET_ADDRSTRLEN)
	return -1;
      rc = inet_ntop (AF_INET, ip_address->v4.buffer, s, INET_ADDRSTRLEN);
      break;
    case AF_INET6:
      if (size < INET6_ADDRSTRLEN)
	return -1;
      rc = inet_ntop (AF_INET6, ip_address->v6.buffer, s, INET6_ADDRSTRLEN);
      break;
    default:
      return -1;
    }
  if (!rc)
    return -1;
  return (int) strnlen_s (s, MAXSZ_IP_ADDRESS);
}

int
hicn_ip_address_to_sockaddr (const hicn_ip_address_t *ip_address,
			     struct sockaddr *sa, int family)
{
  struct sockaddr_in6 *tmp6 = (struct sockaddr_in6 *) sa;
  struct sockaddr_in *tmp4 = (struct sockaddr_in *) sa;

  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->v6.buffer, IPV6_ADDR_LEN);
      break;
    case AF_INET:
      tmp4->sin_family = AF_INET;
      tmp4->sin_port = DUMMY_PORT;
      memcpy (&tmp4->sin_addr, ip_address->v4.buffer, IPV4_ADDR_LEN);
      break;
    default:
      return -1;
    }

  return 1;
}

int
hicn_ip_address_cmp (const hicn_ip_address_t *ip1,
		     const hicn_ip_address_t *ip2)
{
  /* This works as soon as all members are initialized */
  return memcmp (ip1, ip2, sizeof (hicn_ip_address_t));
}

bool
hicn_ip_address_equals (const hicn_ip_address_t *ip1,
			const hicn_ip_address_t *ip2)
{
  return hicn_ip_address_cmp (ip1, ip2) == 0;
}

int
hicn_ip_address_empty (const hicn_ip_address_t *ip)
{
  return (memcmp (ip, &IP_ADDRESS_EMPTY, sizeof (hicn_ip_address_t)) == 0);
}

/* Prefix */

/* Parse IP Prefixes in presentation format (in bits, separated by a slash) */
int
hicn_ip_prefix_pton (const char *hicn_ip_address_str,
		     hicn_ip_prefix_t *hicn_ip_prefix)
{
  int pton_fd;
  char *p;
  char *eptr;
  char *addr = strdup (hicn_ip_address_str);

  p = strchr (addr, '/');
  if (!p)
    {
      hicn_ip_prefix->len = ~0; // until we get the ip address family
    }
  else
    {
      hicn_ip_prefix->len = (u8) strtoul (p + 1, &eptr, 10);
      *p = 0;
    }

  hicn_ip_prefix->family = hicn_ip_address_str_get_family (addr);

  switch (hicn_ip_prefix->family)
    {
    case AF_INET6:
      if (hicn_ip_prefix->len == (u8) ~0)
	hicn_ip_prefix->len = IPV6_ADDR_LEN_BITS;
      if (hicn_ip_prefix->len > IPV6_ADDR_LEN_BITS)
	goto ERR;
      pton_fd = inet_pton (AF_INET6, addr, &hicn_ip_prefix->address.v6.buffer);
      break;
    case AF_INET:
      if (hicn_ip_prefix->len == (u8) ~0)
	hicn_ip_prefix->len = IPV4_ADDR_LEN_BITS;
      if (hicn_ip_prefix->len > IPV4_ADDR_LEN_BITS)
	goto ERR;
      pton_fd = inet_pton (AF_INET, addr, &hicn_ip_prefix->address.v4.buffer);
      break;
    default:
      goto ERR;
    }

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

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

int
hicn_ip_prefix_ntop_short (const hicn_ip_prefix_t *hicn_ip_prefix, char *dst,
			   size_t size)
{
  char ip_s[MAXSZ_IP_ADDRESS];
  const char *s;
  switch (hicn_ip_prefix->family)
    {
    case AF_INET:
      s = inet_ntop (AF_INET, hicn_ip_prefix->address.v4.buffer, ip_s,
		     MAXSZ_IP_ADDRESS);
      break;
    case AF_INET6:
      s = inet_ntop (AF_INET6, hicn_ip_prefix->address.v6.buffer, ip_s,
		     MAXSZ_IP_ADDRESS);
      break;
    default:
      return -1;
    }
  if (!s)
    return -1;
  int rc = snprintf (dst, size, "%s", ip_s);
  if (rc >= size)
    return (int) size;
  return rc;
}

int
hicn_ip_prefix_ntop (const hicn_ip_prefix_t *hicn_ip_prefix, char *dst,
		     size_t size)
{
  char ip_s[MAXSZ_IP_ADDRESS];
  const char *s;
  switch (hicn_ip_prefix->family)
    {
    case AF_INET:
      s = inet_ntop (AF_INET, hicn_ip_prefix->address.v4.buffer, ip_s,
		     MAXSZ_IP_ADDRESS);
      break;
    case AF_INET6:
      s = inet_ntop (AF_INET6, hicn_ip_prefix->address.v6.buffer, ip_s,
		     MAXSZ_IP_ADDRESS);
      break;
    default:
      return -1;
    }
  if (!s)
    return -1;
  int rc = snprintf (dst, size, "%s/%d", ip_s, hicn_ip_prefix->len);
  if (rc >= size)
    return (int) size;
  return rc;
}

int
hicn_ip_prefix_snprintf (char *s, size_t size, const hicn_ip_prefix_t *prefix)
{
  return hicn_ip_prefix_ntop (prefix, s, size);
}

int
hicn_ip_prefix_len (const hicn_ip_prefix_t *prefix)
{
  return prefix->len; // hicn_ip_address_len(&prefix->address, prefix->family);
}

const u8 *
hicn_ip_address_get_buffer (const hicn_ip_address_t *ip_address, int family)
{
  switch (family)
    {
    case AF_INET:
      return ip_address->v4.buffer;
    case AF_INET6:
      return ip_address->v6.buffer;
    default:
      return NULL;
    }
}

bool
hicn_ip_prefix_empty (const hicn_ip_prefix_t *prefix)
{
  return prefix->len == 0;
}

int
hicn_ip_prefix_to_sockaddr (const hicn_ip_prefix_t *prefix,
			    struct sockaddr *sa)
{
  // XXX assert len == hicn_ip_address_len
  return hicn_ip_address_to_sockaddr (&prefix->address, sa, prefix->family);
}

int
hicn_ip_prefix_cmp (const hicn_ip_prefix_t *prefix1,
		    const hicn_ip_prefix_t *prefix2)
{
  if (prefix1->family < prefix2->family)
    return -1;
  else if (prefix1->family > prefix2->family)
    return 1;

  if (prefix1->len < prefix2->len)
    return -1;
  else if (prefix1->len > prefix2->len)
    return 1;

  return hicn_ip_address_cmp (&prefix1->address, &prefix2->address);
}

/* Network byte order + host bit order */

uint8_t
hicn_ip_address_get_bit (const hicn_ip_address_t *address, uint8_t pos)
{
  return (address->v6.as_u8[pos / 8] >> (7 - pos % 8)) & 0x1;
}

bool
hicn_ip_address_match_family (const hicn_ip_address_t *address, int family)
{
  return hicn_ip_address_get_family (address) == family;
}

uint32_t
hicn_ip_address_get_hash (const hicn_ip_address_t *address)
{
  return hash32 (address, sizeof (address));
}

/* URL */

#define MAXSZ_PROTO_ 8 /* inetX:// */
#define MAXSZ_PROTO  MAXSZ_PROTO_ + NULLTERM

#define MAXSZ_URL4_ MAXSZ_PROTO_ + MAXSZ_IP4_ADDRESS_ + MAXSZ_PORT_
#define MAXSZ_URL6_ MAXSZ_PROTO_ + MAXSZ_IP6_ADDRESS_ + MAXSZ_PORT_
#define MAXSZ_URL_  MAXSZ_URL6_
#define MAXSZ_URL4  MAXSZ_URL4_ + NULLTERM
#define MAXSZ_URL6  MAXSZ_URL6_ + NULLTERM
#define MAXSZ_URL   MAXSZ_URL_ + NULLTERM

int
url_snprintf (char *s, size_t size, const hicn_ip_address_t *ip_address,
	      u16 port)
{
  char hicn_ip_address_s[MAXSZ_IP_ADDRESS];
  int rc;

  int family = hicn_ip_address_get_family (ip_address);

  /* Other address are currently not supported */
  if (!IS_VALID_FAMILY (family))
    return -1;

  if (!hicn_ip_address_match_family (ip_address, family))
    return -1;
  rc =
    hicn_ip_address_snprintf (hicn_ip_address_s, MAXSZ_IP_ADDRESS, ip_address);
  if (rc >= MAXSZ_IP_ADDRESS)
    WARN ("[url_snprintf] Unexpected ip_address truncation");
  if (rc < 0)
    return rc;

  return snprintf (s, size, "inet%c://%s:%d", (family == AF_INET) ? '4' : '6',
		   hicn_ip_address_s, port);
}