Loading...
Searching...
No Matches
hdr.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2015 José Ignacio Alamos <jialamos@uc.cl>
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
27
28#include <assert.h>
29
30#include "byteorder.h"
31#include "net/ipv4/addr.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
42#define IPV4_HDR_FLAGS_RESERVED (0x04)
43#define IPV4_HDR_FLAGS_DF (0x02)
44#define IPV4_HDR_FLAGS_MF (0x01)
46
116
122static inline void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
123{
124 hdr->v_ihl &= 0x0f;
125 hdr->v_ihl |= 0x40;
126}
127
135static inline uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
136{
137 return ((hdr->v_ihl) >> 4);
138}
139
146static inline void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
147{
148 assert(ihl >= 20 && ihl <= 60);
149 assert(ihl % 4 == 0);
150
151 hdr->v_ihl &= 0xf0;
152 hdr->v_ihl |= 0x0f & (ihl >> 2);
153}
154
162static inline uint16_t ipv4_hdr_get_ihl(ipv4_hdr_t *hdr)
163{
164 return (hdr->v_ihl & 0x0f) << 2;
165}
166
173static inline void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
174{
175 hdr->fl_fo.u8[0] &= 0x1f;
176 hdr->fl_fo.u8[0] |= (0xe0 & (flags << 5));
177}
178
186static inline uint8_t ipv4_hdr_get_flags(ipv4_hdr_t *hdr)
187{
188 return (((hdr->fl_fo.u8[0]) >> 5) & 0x07);
189}
190
197static inline void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
198{
199 hdr->fl_fo.u8[0] &= 0xe0;
200 hdr->fl_fo.u8[0] |= (0x1f & (fo >> 8));
201 hdr->fl_fo.u8[1] = 0xff & fo;
202}
203
211static inline uint16_t ipv4_hdr_get_fo(ipv4_hdr_t *hdr)
212{
213 return (((hdr->fl_fo.u8[0] & 0x1f) << 8) + hdr->fl_fo.u8[1]);
214}
215
216#ifdef __cplusplus
217}
218#endif
219
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:143
Functions to work with different byte orders.
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition byteorder.h:92
static uint8_t ipv4_hdr_get_flags(ipv4_hdr_t *hdr)
Gets the value of the Version Control Flags field of hdr.
Definition hdr.h:186
static void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
Sets the Version Control Flags field of hdr.
Definition hdr.h:173
static void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
Sets the Internet Header Length field of hdr.
Definition hdr.h:146
static uint16_t ipv4_hdr_get_ihl(ipv4_hdr_t *hdr)
Gets the value of the Internet Header Length field of hdr.
Definition hdr.h:162
static uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
Gets the value of the version field of hdr.
Definition hdr.h:135
static uint16_t ipv4_hdr_get_fo(ipv4_hdr_t *hdr)
Gets the value of the Fragment Offset field of hdr.
Definition hdr.h:211
static void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
Sets the version field of hdr to 4.
Definition hdr.h:122
static void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
Sets the Fragment Offset field of hdr.
Definition hdr.h:197
IPv4 address type and helper functions definitions.
Data type to represent an IPv4 packet header.
Definition hdr.h:72
network_uint16_t tl
total length of the datagram
Definition hdr.h:93
ipv4_addr_t src
source address of this packet
Definition hdr.h:113
uint8_t protocol
protocol of this packet
Definition hdr.h:111
uint8_t v_ihl
Version and Internet Header Length.
Definition hdr.h:91
network_uint16_t csum
checksum of this packet
Definition hdr.h:112
ipv4_addr_t dst
destination address of this packet
Definition hdr.h:114
network_uint16_t fl_fo
Version control flags and Fragment Offset.
Definition hdr.h:109
uint8_t ttl
time to live for this packet
Definition hdr.h:110
network_uint16_t id
identification value of packet
Definition hdr.h:94
uint8_t ts
type of service of packet
Definition hdr.h:92
uint8_t u8[2]
8 bit representation
Definition byteorder.h:66
Data type to represent an IPv4 address.
Definition addr.h:55