File: ip_bytes.h

package info (click to toggle)
libreswan 5.2-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 81,632 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (115 lines) | stat: -rw-r--r-- 3,438 bytes parent folder | download
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
/* ip address type, for libreswan
 *
 * Copyright (C) 2021 Andrew Cagney
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/lgpl-2.1.txt>.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 * License for more details.
 *
 */

#ifndef IP_BYTES_H
#define IP_BYTES_H

#include <stdint.h>		/* for uint8_t */

struct ip_info;
enum ip_version;

/*
 * We need something that makes static IPv4 initializers possible
 * (struct in_addr requires htonl() which is run-time only).
 */

struct ip_bytes {
	uint8_t byte[16];
};

extern const struct ip_bytes unset_ip_bytes;

#define PRI_IP_BYTES "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
#define pri_ip_bytes(B)							\
	(B).byte[0],							\
		(B).byte[1],						\
		(B).byte[2],						\
		(B).byte[3],						\
		(B).byte[4],						\
		(B).byte[5],						\
		(B).byte[6],						\
		(B).byte[7],						\
		(B).byte[8],						\
		(B).byte[9],						\
		(B).byte[10],						\
		(B).byte[11],						\
		(B).byte[12],						\
		(B).byte[13],						\
		(B).byte[14],						\
		(B).byte[15]

/*
 * Modify address bytes broken down according to AFI as
 * ROUTING-PREFIX:HOST-ID.
 */

extern const struct ip_routing_prefix_blit keep_routing_prefix;
extern const struct ip_routing_prefix_blit clear_routing_prefix;
extern const struct ip_routing_prefix_blit set_routing_prefix;
extern const struct ip_host_identifier_blit clear_host_identifier;
extern const struct ip_host_identifier_blit set_host_identifier;

struct ip_bytes ip_bytes_blit(const struct ip_info *afi,
			      const struct ip_bytes bytes,
			      const struct ip_routing_prefix_blit *routing_prefix,
			      const struct ip_host_identifier_blit *host_identifier,
			      int prefix_len);

/* Calculate l-r using unsigned arithmetic */
struct ip_bytes ip_bytes_sub(const struct ip_info *afi,
			     const struct ip_bytes l,
			     const struct ip_bytes r);

/* find first non-zero bit from left */
int ip_bytes_first_set_bit(const struct ip_info *afi,
			   const struct ip_bytes bytes);

/* match prefixes, or -1 */

int ip_bytes_prefix_len(const struct ip_info *afi,
			const struct ip_bytes lo,
			const struct ip_bytes hi);
int ip_bytes_host_len(const struct ip_info *afi,
		      const struct ip_bytes lo,
		      const struct ip_bytes hi);
int ip_bytes_mask_len(const struct ip_info *afi,
		      const struct ip_bytes bytes);

int ip_bytes_cmp(enum ip_version l_version, const struct ip_bytes l_bytes,
		 enum ip_version r_version, const struct ip_bytes r_bytes);

bool ip_bytes_is_zero(const struct ip_bytes *bytes);

#define IP_INFO_UNSET(IP)			\
	(IP == NULL ? "null" :			\
	 !IP->is_set ? "unset" :		\
	 IP->info == NULL ? "family-unset" :	\
	 NULL)

#define IP_INFO_PROTOCOL_UNSET(IP)			\
	(IP == NULL ? "null" :				\
	 !IP->is_set ? "unset" :			\
	 IP->info == NULL ? "family-unset" :		\
	 IP->protocol == NULL ? "protocol-unset" :	\
	 NULL)

size_t jam_ip_bytes_range(struct jambuf *buf,
			  const struct ip_info *afi,
			  const struct ip_bytes lo,
			  const struct ip_bytes hi);

#endif