summaryrefslogtreecommitdiffstats
path: root/lib/src/compat.h
blob: 1a1743de2ea23fdac7bc1a6acf0893820bc57c3b (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
/*
 * 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 compat.h
 * @brief Implementation of the compatibility layer.
 *
 * The structure of the core API has evolved to support operations of a variety
 * of packet formats in addition to IPv4/TCP and IPv6/TCP, namely with the use
 * of ICMP for signalization and AH headers for integrity. The new API format
 * has been designed to scale better with the multiplicity of packet formats,
 * and provide a unified interface on top. We maintain an interface for the
 * former API in this file, which mainly acts as a wrapper on top of new calls.
 */
#ifndef HICN_COMPAT_H
#define HICN_COMPAT_H

#include "common.h"
#include "header.h"
#include "name.h"

/* HICN format options */
#define HFO_INET  1 << 0
#define HFO_INET6 1 << 1
#define HFO_TCP   1 << 2
#define HFO_ICMP  1 << 3
#define HFO_AH    1 << 4

#define _is_ipv4(format) ((format & HFO_INET))
#define _is_ipv6(format) ((format & HFO_INET6) >> 1)
#define _is_tcp(format)  ((format & HFO_TCP)   >> 2)
#define _is_icmp(format) ((format & HFO_ICMP)  >> 3)
#define _is_ah(format)   ((format & HFO_AH)    >> 4)

typedef enum
{
  HF_UNSPEC = 0,
  HF_INET_TCP = HFO_INET | HFO_TCP,
  HF_INET6_TCP = HFO_INET6 | HFO_TCP,
  HF_INET_ICMP = HFO_INET | HFO_ICMP,
  HF_INET6_ICMP = HFO_INET6 | HFO_ICMP,
  HF_INET_TCP_AH = HFO_INET | HFO_TCP | HFO_AH,
  HF_INET6_TCP_AH = HFO_INET6 | HFO_TCP | HFO_AH,
  HF_INET_ICMP_AH = HFO_INET | HFO_ICMP | HFO_AH,
  HF_INET6_ICMP_AH = HFO_INET6 | HFO_ICMP | HFO_AH
} hicn_format_t;

/**
 * Maximum Size of the hICN header (the effective size will depend on the
 * underlying IP version)
 */
#define HICN_HDR_LEN sizeof(hicn_header_t)

/**
 * Minimum required header length to determine the type and length of a supposed
 * hICN packet.
 * This should be equal to the maximum value over all possible hICN packet
 * formats, and less than the minimum possible IP packet size.
 */
#define HICN_V6_MIN_HDR_LEN 6	/* bytes */
#define HICN_V4_MIN_HDR_LEN 4	/* bytes */

// #define HICN_MIN_HDR_LEN ((HICN_V6_MIN_HDR_LEN > HICN_V4_MIN_HDR_LEN) ? HICN_V6_MIN_HDR_LEN : HICN_V4_MIN_HDR_LEN)
#define HICN_MIN_HDR_LEN HICN_V6_MIN_HDR_LEN

/**
 * @brief Parse packet headers and return hICN format
 * @param [in] format - hICN Format
 * @param [in, out] packet - Buffer containing the hICN header to be initialized
 * @return hICN error code
 */
int hicn_packet_init_header (hicn_format_t format, hicn_header_t * packet);

/**
 * @brief Parse packet headers and return hICN format
 * @param [in] h - hICN header
 * @param [out] format - hICN format
 * @return hICN error code
 */
int hicn_packet_get_format (const hicn_header_t * packet,
			    hicn_format_t * format);

/**
 * @brief Update checksums in packet headers
 * @param [in] format - hICN format
 * @param [in,out] packet - packet header
 * @return hICN error code
 */
int hicn_packet_compute_checksum (hicn_format_t format,
				  hicn_header_t * packet);

/**
 * @brief compute the checksum of the packet header, adding init_sum to the final value
 * @param [in] format - hICN format
 * @param [in,out] packet - packet header
 * @param [in] init_sum - value to add to the final checksum
 * @return hICN error code
 */
int hicn_packet_compute_header_checksum (hicn_format_t format,
					 hicn_header_t * packet,
					 u16 init_sum);

/**
 * @brief Verify checksums in packet headers
 * @param [in] format - hICN format
 * @param [in,out] packet - packet header
 * @return hICN error code
 */
int hicn_packet_check_integrity (hicn_format_t format,
				 hicn_header_t * packet);

// this is not accounted here
/**
 * @brief Return total length of hicn headers (but signature payload)
 * @param [in] format - hICN format
 * @param [out] header_length - Total length of headers
 * @return hICN error code
 */
int hicn_packet_get_header_length_from_format (hicn_format_t format,
					       size_t * header_length);

/**
 * @brief Return total length of hicn headers (before payload)
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] header_length - Total length of headers
 * @return hICN error code
 */
int hicn_packet_get_header_length (hicn_format_t format,
				   const hicn_header_t * packet,
				   size_t * header_length);

/**
 * @brief Return payload length
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] payload_length - payload length
 * @return hICN error code
 */
int hicn_packet_get_payload_length (hicn_format_t format,
				    const hicn_header_t * packet,
				    size_t * payload_length);

/**
 * @brief Sets payload length
 * @param [in] format - hICN format
 * @param [in,out] packet - packet header
 * @param [in] payload_length - payload length
 * @return hICN error code
 */
int hicn_packet_set_payload_length (hicn_format_t format,
				    hicn_header_t * packet,
				    const size_t payload_length);

/**
 * @brief Compare two hICN packets
 * @param [in] packet_1 - First packet
 * @param [in] packet_2 - Second packet
 * @return 0 if both packets are considered equal, any other value otherwise.
 */
int hicn_packet_compare (const hicn_header_t * packet1,
			 const hicn_header_t * packet2);

/**
 * @brief Retrieve the name of an interest/data packet
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] name - name holding the result
 * @param [in] is_interest - Flag to determine whether it is an interest (1) or
 * data packet (0)
 * @return hICN error code
 */
int hicn_packet_get_name (hicn_format_t format, const hicn_header_t * packet,
			  hicn_name_t * name, u8 is_interest);

/**
 * @brief Sets the name of an interest/data packet
 * @param [in] format - hICN format
 * @param [in,out] packet - packet header
 * @param [in] name - name to set into packet
 * @param [in] is_interest - Flag to determine whether it is an interest (1) or
 * data packet (0)
 * @return hICN error code
 */
int hicn_packet_set_name (hicn_format_t format, hicn_header_t * packet,
			  const hicn_name_t * name, u8 is_interest);

/**
 * @brief Sets the payload of a packet
 * @param [in] format - hICN format
 * @param [in,out] packet - packet header
 * @param [in] payload - payload to set
 * @param [in] payload_length - size of the payload to set
 * @return hICN error code
 *
 * NOTE:
 *  - The buffer holding payload is assumed sufficiently large
 *  - This function updates header fields with the new length, but no checksum.
 */
int hicn_packet_set_payload (hicn_format_t format, hicn_header_t * packet,
			     const u8 * payload, u16 payload_length);

/**
 * @brief Retrieves the payload of a packet
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] payload - pointer to buffer for storing the result
 * @param [out] payload_length - size of the retreived payload
 * @param [in] hard_copy - Flag : if true (eg. 1), a copy of the payload is made
 * into the payload buffer, otherwise (0) the pointer is changed to point to the payload offset in the packet.
 * @return hICN error code
 *
 * NOTE:
 *  - The buffer holding payload is assumed sufficiently large
 */
int hicn_packet_get_payload (hicn_format_t format,
			     const hicn_header_t * packet, u8 ** payload,
			     size_t * payload_size, bool hard_copy);

/**
 * @brief Retrieve the locator of an interest / data packet
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] ip_address - retrieved locator
 * @param [in] is_interest - Flag to determine whether it is an interest (1) or
 * data packet (0)
 * @return hICN error code
 */
int hicn_packet_get_locator (hicn_format_t format,
			     const hicn_header_t * packet,
			     ip_address_t * ip_address, bool is_interest);

/**
 * @brief Sets the locator of an interest / data packet
 * @param [in] format - hICN format
 * @param [in,out] packet - packet header
 * @param [out] ip_address - retrieved locator
 * @param [in] is_interest - Flag to determine whether it is an interest (1) or
 * data packet (0)
 * @return hICN error code
 */
int hicn_packet_set_locator (hicn_format_t format, hicn_header_t * packet,
			     const ip_address_t * ip_address,
			     bool is_interest);

/**
 * @brief Retrieves the signature size
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] bytes - Retrieved signature size
 * @return hICN error code
 */
int hicn_packet_get_signature_size (hicn_format_t format,
				    const hicn_header_t * packet,
				    size_t * bytes);

/**
 * @brief Sets the signature size
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [in] bytes - Retrieved signature size
 * @return hICN error code
 */
int hicn_packet_set_signature_size (hicn_format_t format,
				    hicn_header_t * packet, size_t bytes);

/**
 * @brief Sets the signature size
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [in] signature_timestamp - Signature timestamp to set
 * @return hICN error code
 */
int hicn_packet_set_signature_timestamp (hicn_format_t format, hicn_header_t * h,
				uint64_t signature_timestamp);

/**
 * @brief Sets the signature size
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] signature_timestamp - Retrieved signature timestamp
 * @return hICN error code
 */
int hicn_packet_get_signature_timestamp (hicn_format_t format, const hicn_header_t * h,
				uint64_t *signature_timestamp);

/**
 * @brief Sets the signature size
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [in] validation_algorithm - Validation algorithm to set
 * @return hICN error code
 */
int hicn_packet_set_validation_algorithm (hicn_format_t format, hicn_header_t * h,
				uint8_t validation_algorithm);

/**
 * @brief Sets the signature size
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] validation_algorithm - Retrieved validation algorithm
 * @return hICN error code
 */
int hicn_packet_get_validation_algorithm (hicn_format_t format, const hicn_header_t * h,
				uint8_t * validation_algorithm);

/**
 * @brief Sets the signature size
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [in] key_id - Key id to set
 * @return hICN error code
 */
int hicn_packet_set_key_id (hicn_format_t format, hicn_header_t * h,
				uint8_t *key_id);

/**
 * @brief Sets the signature size
 * @param [in] format - hICN format
 * @param [in] packet - packet header
 * @param [out] key_id - Retrieved key id
 * @return hICN error code
 */
int hicn_packet_get_key_id (hicn_format_t format, hicn_header_t * h,
				uint8_t ** key_id, uint8_t *key_id_length);

/**
 * @brief Retrieves the packet hop limit
 * @param [in] packet - packet header
 * @param [out] hops - Retrieved hop limit
 * @return hICN error code
 */
int hicn_packet_get_hoplimit (const hicn_header_t * packet, u8 * hops);

/**
 * @brief Sets the packet hop limit
 * @param [in] packet - packet header
 * @param [in] hops - Hop limit to set
 * @return hICN error code
 */
int hicn_packet_set_hoplimit (hicn_header_t * packet, u8 hops);

int hicn_packet_copy_header (hicn_format_t format,
			     const hicn_header_t * packet,
			     hicn_header_t * destination, bool copy_ah);

int hicn_packet_get_lifetime (const hicn_header_t * packet, u32 * lifetime);
int hicn_packet_set_lifetime (hicn_header_t * packet, u32 lifetime);
int hicn_packet_get_reserved_bits (const hicn_header_t * packet,
				   u8 * reserved_bits);
int hicn_packet_set_reserved_bits (hicn_header_t * packet,
				   const u8 reserved_bits);
int hicn_packet_get_payload_type (const hicn_header_t * packet,
				  hicn_payload_type_t * payload_type);
int hicn_packet_set_payload_type (hicn_header_t * packet,
				  const hicn_payload_type_t payload_type);

int hicn_packet_set_syn (hicn_header_t * packet);
int hicn_packet_reset_syn (hicn_header_t * packet);
int hicn_packet_test_syn (const hicn_header_t * packet, bool * flag);
int hicn_packet_set_ack (hicn_header_t * packet);
int hicn_packet_reset_ack (hicn_header_t * packet);
int hicn_packet_test_ack (const hicn_header_t * packet, bool * flag);
int hicn_packet_set_rst (hicn_header_t * packet);
int hicn_packet_reset_rst (hicn_header_t * packet);
int hicn_packet_test_rst (const hicn_header_t * packet, bool * flag);
int hicn_packet_set_fin (hicn_header_t * packet);
int hicn_packet_reset_fin (hicn_header_t * packet);
int hicn_packet_test_fin (const hicn_header_t * packet, bool * flag);
int hicn_packet_set_ece (hicn_header_t * packet);
int hicn_packet_reset_ece (hicn_header_t * packet);
int hicn_packet_test_ece (const hicn_header_t * packet, bool * flag);

int hicn_packet_set_src_port (hicn_header_t * packet, u16 src_port);
int hicn_packet_get_src_port (const hicn_header_t * packet, u16 * src_port);
int hicn_packet_set_dst_port (hicn_header_t * packet, u16 dst_port);
int hicn_packet_get_dst_port (const hicn_header_t * packet, u16 * dst_port);

/* Interest */

int hicn_interest_get_name (hicn_format_t format,
			    const hicn_header_t * interest,
			    hicn_name_t * name);
int hicn_interest_set_name (hicn_format_t format, hicn_header_t * interest,
			    const hicn_name_t * name);
int hicn_interest_get_locator (hicn_format_t format,
			       const hicn_header_t * interest,
			       ip_address_t * ip_address);
int hicn_interest_set_locator (hicn_format_t format, hicn_header_t * interest,
			       const ip_address_t * ip_address);
int hicn_interest_compare (const hicn_header_t * interest_1,
			   const hicn_header_t * interest_2);
int hicn_interest_set_lifetime (hicn_header_t * interest, u32 lifetime);
int hicn_interest_get_lifetime (const hicn_header_t * interest,
				u32 * lifetime);
int hicn_interest_get_header_length (hicn_format_t format,
				     const hicn_header_t * interest,
				     size_t * header_length);
int hicn_interest_get_payload_length (hicn_format_t format,
				      const hicn_header_t * interest,
				      size_t * payload_length);
int hicn_interest_set_payload (hicn_format_t format, hicn_header_t * interest,
			       const u8 * payload, size_t payload_length);
int hicn_interest_get_payload (hicn_format_t format,
			       const hicn_header_t * interest, u8 ** payload,
			       size_t * payload_size, bool hard_copy);
int hicn_interest_reset_for_hash (hicn_format_t format,
				  hicn_header_t * packet);

/* Data */

int hicn_data_get_name (hicn_format_t format, const hicn_header_t * data,
			hicn_name_t * name);
int hicn_data_set_name (hicn_format_t format, hicn_header_t * data,
			hicn_name_t * name);
int hicn_data_get_locator (hicn_format_t format, const hicn_header_t * data,
			   ip_address_t * ip_address);
int hicn_data_set_locator (hicn_format_t format, hicn_header_t * data,
			   const ip_address_t * ip_address);
int hicn_data_compare (const hicn_header_t * data_1,
		       const hicn_header_t * data_2);
int hicn_data_get_expiry_time (const hicn_header_t * data, u32 * expiry_time);
int hicn_data_set_expiry_time (hicn_header_t * data, u32 expiry_time);
int hicn_data_get_header_length (hicn_format_t format, hicn_header_t * data,
				 size_t * header_length);
int hicn_data_get_payload_length (hicn_format_t format,
				  const hicn_header_t * data,
				  size_t * payload_length);
int hicn_data_get_path_label (const hicn_header_t * data, u32 * path_label);
int hicn_data_set_path_label (hicn_header_t * data, u32 path_label);
int hicn_data_get_payload (hicn_format_t format, const hicn_header_t * data,
			   u8 ** payload, size_t * payload_size,
			   bool hard_copy);
int hicn_data_set_payload (hicn_format_t format, hicn_header_t * data,
			   const u8 * payload, size_t payload_length);
int hicn_data_get_payload_type (const hicn_header_t * data,
				hicn_payload_type_t * payload_type);
int hicn_data_set_payload_type (hicn_header_t * data,
				hicn_payload_type_t payload_type);
int hicn_data_reset_for_hash (hicn_format_t format, hicn_header_t * packet);

#endif /* HICN_COMPAT_H */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */