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 
20 #include "byteorder.h"
21 #include "net/ipv4/addr.h"
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
52 typedef struct __attribute__((packed)) {
66  uint8_t v_ih;
67  uint8_t ts;
83  uint8_t ttl;
84  uint8_t protocol;
88 } ipv4_hdr_t;
89 
95 static inline void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
96 {
97  hdr->v_ih &= 0x0f;
98  hdr->v_ih |= 0x40;
99 }
100 
108 static inline uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
109 {
110  return ((hdr->v_ih) >> 4);
111 }
112 
119 static inline void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
120 {
121  hdr->v_ih &= 0xf0;
122  hdr->v_ih |= 0x0f & (ihl >> 5);
123 }
124 
132 static inline uint16_t ipv4_hdr_get_ihl(ipv4_hdr_t *hdr)
133 {
134  return (hdr->v_ih & 0x0f) << 5;
135 }
136 
143 static inline void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
144 {
145  hdr->fl_fo.u8[0] &= 0x1f;
146  hdr->fl_fo.u8[0] |= (0xe0 & (flags << 5));
147 }
148 
156 static inline uint8_t ipv4_hdr_get_flags(ipv4_hdr_t *hdr)
157 {
158  return (((hdr->fl_fo.u8[0]) >> 5) & 0x07);
159 }
160 
167 static inline void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
168 {
169  hdr->fl_fo.u8[0] &= 0xe0;
170  hdr->fl_fo.u8[0] |= (0x1f & (fo >> 8));
171  hdr->fl_fo.u8[1] = 0xff & fo;
172 }
173 
181 static inline uint16_t ipv4_hdr_get_fo(ipv4_hdr_t *hdr)
182 {
183  return (((hdr->fl_fo.u8[0] & 0x1f) << 8) + hdr->fl_fo.u8[1]);
184 }
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
Functions to work with different byte orders.
static uint8_t ipv4_hdr_get_flags(ipv4_hdr_t *hdr)
brief Gets the value of the Version Control Flags field of hdr
Definition: hdr.h:156
static void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
Sets the Version Control Flags field of hdr.
Definition: hdr.h:143
static void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
Sets the Internet Header Length field of hdr.
Definition: hdr.h:119
static uint16_t ipv4_hdr_get_ihl(ipv4_hdr_t *hdr)
brief Gets the value of the Internet Header Length field of hdr
Definition: hdr.h:132
static uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
Gets the value of the version field of hdr.
Definition: hdr.h:108
static uint16_t ipv4_hdr_get_fo(ipv4_hdr_t *hdr)
brief Gets the value of the Fragment Offset field of hdr
Definition: hdr.h:181
static void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition: hdr.h:95
static void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
Sets the Fragment Offset field of hdr.
Definition: hdr.h:167
IPv4 address type and helper functions definitions.
Data type to represent an IPv4 packet header.
Definition: hdr.h:52
network_uint16_t tl
total length of the datagram
Definition: hdr.h:68
ipv4_addr_t src
source address of this packet
Definition: hdr.h:86
uint8_t protocol
protocol of this packet
Definition: hdr.h:84
uint8_t v_ih
Version and Internet Header Length.
Definition: hdr.h:66
network_uint16_t csum
checksum of this packet
Definition: hdr.h:85
ipv4_addr_t dst
destination address of this packet
Definition: hdr.h:87
network_uint16_t fl_fo
version control flags and Fragment Offset.
Definition: hdr.h:82
uint8_t ttl
time to live for this packet
Definition: hdr.h:83
network_uint16_t id
identification value of packet
Definition: hdr.h:69
uint8_t ts
type of service of packet
Definition: hdr.h:67
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:70
uint8_t u8[2]
8 bit representation
Definition: byteorder.h:72
Data type to represent an IPv4 address.
Definition: addr.h:55