Helium
network.h
1 /* *
2  * Lightway Core
3  * Copyright (C) 2023 Express VPN International Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #ifndef HE_NETWORK_H
21 #define HE_NETWORK_H
22 
23 #include "he.h"
24 #include "he_internal.h"
25 
26 #pragma pack(1)
27 
28 #define HE_IP_DONT_FRAGMENT (1 << 14)
29 #define HE_IP_NO_FRAG_OFFSET 0
30 #define HE_IP_MORE_FRAGMENTS (1 << 13)
31 #define HE_IP_FRAGMENT_OFFSET_MULTIPLIER 8
32 #define HE_IP_LENGTH_BITMASK 0x0F
33 typedef struct {
34  uint8_t ver_ihl; // 4 bits version and 4 bits internet header length
35  uint8_t tos;
36  uint16_t total_length;
37  uint16_t id;
38  uint16_t flags_fo; // 3 bits flags and 13 bits fragment-offset
39  uint8_t ttl;
40  uint8_t protocol;
41  uint16_t checksum;
42  uint32_t src_addr;
43  uint32_t dst_addr;
45 
46 #define HE_IP_TCP 0x06
47 #define HE_IP_UDP 0x11
48 #define HE_TCP_SYN 0x02
49 typedef struct {
50  uint16_t src_port;
51  uint16_t dst_port;
52  uint32_t seq;
53  uint32_t ack;
54  uint8_t data_offset; // 4 bits
55  uint8_t flags;
56  uint16_t window_size;
57  uint16_t checksum;
58  uint16_t urgent_p;
59 } tcp_header_t;
60 
61 // https://www.rfc-editor.org/rfc/rfc9293.html#Option-Definitions
62 #define HE_TCP_OPT_END 0
63 #define HE_TCP_OPT_NOP 1
64 #define HE_TCP_OPT_MSS 2
65 #define HE_TCP_MSS_OPT_SIZE 4
66 
67 typedef struct {
68  uint8_t kind;
69  uint8_t size;
70 } tcp_option_t;
71 
72 typedef struct {
73  uint16_t src_port;
74  uint16_t dst_port;
75  uint16_t length;
76  uint16_t checksum;
77 } udp_header_t;
78 
79 #pragma pack()
80 
81 void he_internal_calculate_differential_checksum(uint16_t *cksum, void *newp, void *oldp, size_t n);
82 bool he_internal_is_ipv4_packet_valid(uint8_t *packet, size_t length);
83 
84 #endif
he_internal.h
Core internal header file for libhelium.
ipv4_header_t
Definition: network.h:33
udp_header_t
Definition: network.h:72
tcp_option_t
Definition: network.h:67
tcp_header_t
Definition: network.h:49
he.h
Core public header file for libhelium.