aboutsummaryrefslogtreecommitdiff
path: root/src/inet.h
blob: 0f6e30b0d52c14a0389e6b037e9f2e69a9b6507e (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
/** \file
 * \brief The utility functions for the internet protocol.
 * \note The header includes functions which are required when using raw TCP/IP
 *	sockets.
 */
/*
* Copyright (c) 2019-2021 David Timber <mieabby@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#include "protocol.h"


/* Alias declarations */
typedef struct prne_iphdr4 prne_iphdr4_t;
typedef struct prne_iphdr6 prne_iphdr6_t;

/** \struct prne_iphdr4 \struct prne_iphdr6
 * \brief The workaround for the issues in uClibc headers.
 * \note At the time of writing the code, the IPv6 support of uClibc was not
 *	complete and there were some problems using the IP headers provided by
 *	uClibc. These structures are exactly the same as the counterparts in the
 *	standard IP headers.
 * \note The values must be in the host byte order. Unlike the standard
 * 	functions, the serialisation and deserialisation functions are responsible
 * 	for byte order conversion.
 */

struct prne_iphdr4 {
	uint8_t saddr[4];
	uint8_t daddr[4];
	uint16_t total_len;
	uint16_t id;
	uint8_t ttl;
	uint8_t protocol;
	uint8_t ihl;
};

struct prne_iphdr6 {
	uint8_t saddr[16];
	uint8_t daddr[16];
	uint32_t flow_label;
	uint16_t payload_len;
	uint8_t next_hdr;
	uint8_t hop_limit;
};

/**
 * \brief Set bits to represent the CIDR
 * \param out The byte array used to represent the netmask.
 * \param cidr The CIDR value.
 * \note The number of elements modified is calculated by dividing \p cidr by 8.
 *	For example, if the \p cidr is passed as 24, only the first 3 elements of
 *	\p out are modified to set the first 24 bits.
 * \warning The behaviour is undefined if \p cidr divided by 8 is larger than
 *	the size of \p out (buffer overflow).
 */
void prne_netmask_from_cidr (uint8_t *out, size_t cidr);
/**
 * \brief Calculate the checksum of the IPv4 TCP packet.
 * \param ih The pointer to the IPv4 header structure.
 * \param th The pointer to the TCP header data.
 * \param th_len The byte length of the TCP header data.
 * \param data The pointer to the payload data.
 * \param data_len The byte length of the payload data.
 * \return The calculated checksum value in the host byte order. The value must
 *	be converted to the network byte order.
 */
uint16_t prne_calc_tcp_chksum4 (
	const prne_iphdr4_t *ih,
	const uint8_t *th,
	size_t th_len,
	const uint8_t *data,
	size_t data_len);
/**
 * \brief Calculate the checksum of the IPv6 TCP packet.
 * \param ih The pointer to the IPv6 header structure.
 * \param th The pointer to the TCP header data.
 * \param th_len The byte length of the TCP header data.
 * \param data The pointer to the payload data.
 * \param data_len The byte length of the payload data.
 * \return The calculated checksum value in the host byte order. The value must
 *	be converted to the network byte order.
 * \note The same algorithm(the function) can be used to calculate the checksum
 *	values of ICMP packets.
 */
uint16_t prne_calc_tcp_chksum6 (
	const prne_iphdr6_t *ih,
	const uint8_t *th,
	size_t th_len,
	const uint8_t *data,
	size_t data_len);

/**
 * \brief Serialise the IPv4 header structure for transmission.
 * \param mem The destination buffer. The length of the buffer must be at least
 *	20 bytes.
 * \param in The pointer to the IPv4 header structure.
 */
void prne_ser_iphdr4 (uint8_t *mem, const prne_iphdr4_t *in);
/**
 * \brief Serialise the IPv6 header structure for transmission.
 * \param mem The destination buffer. The length of the buffer must be at least
 *	40 bytes.
 * \param in The pointer to the IPv6 header structure.
 */
void prne_ser_iphdr6 (uint8_t *mem, const prne_iphdr6_t *in);
/**
 * \brief Deserialise the IPv4 header from the binary data.
 * \param data The binary data. The length must be at least 20 bytes.
 * \param out The pointer to the IPv4 header structure.
 */
void prne_dser_iphdr4 (const uint8_t *data, prne_iphdr4_t *out);
/**
 * \brief Deserialise the IPv6 header from the binary data.
 * \param data The binary data. The length must be at least 40 bytes.
 * \param out The pointer to the IPv6 header structure.
 */
void prne_dser_iphdr6 (const uint8_t *data, prne_iphdr6_t *out);