hdr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 José Ignacio Alamos <jialamos@uc.cl>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
20 #ifndef NET_IPV4_HDR_H
21 #define NET_IPV4_HDR_H
22 
23 #include "byteorder.h"
24 #include "net/ipv4/addr.h"
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
55 typedef struct __attribute__((packed)) {
69  uint8_t v_ih;
70  uint8_t ts;
86  uint8_t ttl;
87  uint8_t protocol;
91 } ipv4_hdr_t;
92 
98 static inline void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
99 {
100  hdr->v_ih &= 0x0f;
101  hdr->v_ih |= 0x40;
102 }
103 
111 static inline uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
112 {
113  return ((hdr->v_ih) >> 4);
114 }
115 
122 static inline void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
123 {
124  hdr->v_ih &= 0xf0;
125  hdr->v_ih |= 0x0f & (ihl >> 5);
126 }
127 
135 static inline uint16_t ipv4_hdr_get_ihl(ipv4_hdr_t *hdr)
136 {
137  return (hdr->v_ih & 0x0f) << 5;
138 }
139 
146 static inline void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
147 {
148  hdr->fl_fo.u8[0] &= 0x1f;
149  hdr->fl_fo.u8[0] |= (0xe0 & (flags << 5));
150 }
151 
159 static inline uint8_t ipv4_hdr_get_flags(ipv4_hdr_t *hdr)
160 {
161  return (((hdr->fl_fo.u8[0]) >> 5) & 0x07);
162 }
163 
170 static inline void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
171 {
172  hdr->fl_fo.u8[0] &= 0xe0;
173  hdr->fl_fo.u8[0] |= (0x1f & (fo >> 8));
174  hdr->fl_fo.u8[1] = 0xff & fo;
175 }
176 
184 static inline uint16_t ipv4_hdr_get_fo(ipv4_hdr_t *hdr)
185 {
186  return (((hdr->fl_fo.u8[0] & 0x1f) << 8) + hdr->fl_fo.u8[1]);
187 }
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 
193 #endif /* NET_IPV4_HDR_H */
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:159
static void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
Sets the Version Control Flags field of hdr.
Definition: hdr.h:146
static void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
Sets the Internet Header Length field of hdr.
Definition: hdr.h:122
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:135
static uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
Gets the value of the version field of hdr.
Definition: hdr.h:111
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:184
static void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition: hdr.h:98
static void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
Sets the Fragment Offset field of hdr.
Definition: hdr.h:170
IPv4 address type and helper functions definitions.
Data type to represent an IPv4 packet header.
Definition: hdr.h:55
network_uint16_t tl
total length of the datagram
Definition: hdr.h:71
ipv4_addr_t src
source address of this packet
Definition: hdr.h:89
uint8_t protocol
protocol of this packet
Definition: hdr.h:87
uint8_t v_ih
Version and Internet Header Length.
Definition: hdr.h:69
network_uint16_t csum
checksum of this packet
Definition: hdr.h:88
ipv4_addr_t dst
destination address of this packet
Definition: hdr.h:90
network_uint16_t fl_fo
version control flags and Fragment Offset.
Definition: hdr.h:85
uint8_t ttl
time to live for this packet
Definition: hdr.h:86
network_uint16_t id
identification value of packet
Definition: hdr.h:72
uint8_t ts
type of service of packet
Definition: hdr.h:70
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:74
uint8_t u8[2]
8 bit representation
Definition: byteorder.h:76
Data type to represent an IPv4 address.
Definition: addr.h:53