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
28 
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 
93 
99 static inline void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
100 {
101  hdr->v_ih &= 0x0f;
102  hdr->v_ih |= 0x40;
103 }
104 
112 static inline uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
113 {
114  return ((hdr->v_ih) >> 4);
115 }
116 
123 static inline void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
124 {
125  hdr->v_ih &= 0xf0;
126  hdr->v_ih |= 0x0f & (ihl >> 5);
127 }
128 
136 static inline uint16_t ipv4_hdr_get_ihl(ipv4_hdr_t *hdr)
137 {
138  return (hdr->v_ih & 0x0f) << 5;
139 }
140 
147 static inline void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
148 {
149  hdr->fl_fo.u8[0] &= 0x1f;
150  hdr->fl_fo.u8[0] |= (0xe0 & (flags << 5));
151 }
152 
160 static inline uint8_t ipv4_hdr_get_flags(ipv4_hdr_t *hdr)
161 {
162  return (((hdr->fl_fo.u8[0]) >> 5) & 0x07);
163 }
164 
171 static inline void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
172 {
173  hdr->fl_fo.u8[0] &= 0xe0;
174  hdr->fl_fo.u8[0] |= (0x1f & (fo >> 8));
175  hdr->fl_fo.u8[1] = 0xff & fo;
176 }
177 
185 static inline uint16_t ipv4_hdr_get_fo(ipv4_hdr_t *hdr)
186 {
187  return (((hdr->fl_fo.u8[0] & 0x1f) << 8) + hdr->fl_fo.u8[1]);
188 }
189 
190 #ifdef __cplusplus
191 }
192 #endif
193 
194 #endif /* NET_IPV4_HDR_H */
195 
ipv4_hdr_t::protocol
uint8_t protocol
protocol of this packet
Definition: hdr.h:87
ipv4_hdr_t::src
ipv4_addr_t src
source address of this packet
Definition: hdr.h:89
ipv4_hdr_t::csum
network_uint16_t csum
checksum of this packet
Definition: hdr.h:88
ipv4_addr_t
Data type to represent an IPv4 address.
Definition: addr.h:41
ipv4_hdr_set_version
static void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition: hdr.h:99
ipv4_hdr_t::dst
ipv4_addr_t dst
destination address of this packet
Definition: hdr.h:90
byteorder.h
Functions to work with different byte orders.
addr.h
IPv6 address type and helper functions definitions.
be_uint16_t
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:77
ipv4_hdr_set_ihl
static void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
Sets the Internet Header Length field of hdr.
Definition: hdr.h:123
be_uint16_t::u8
uint8_t u8[2]
8 bit representation
Definition: byteorder.h:79
ipv4_hdr_t::v_ih
uint8_t v_ih
Version and Internet Header Length.
Definition: hdr.h:69
ipv4_hdr_t::id
network_uint16_t id
identification value of packet
Definition: hdr.h:72
ipv4_hdr_t::tl
network_uint16_t tl
total length of the datagram
Definition: hdr.h:71
ipv4_hdr_t::ts
uint8_t ts
type of service of packet
Definition: hdr.h:70
ipv4_hdr_t::ttl
uint8_t ttl
time to live for this packet
Definition: hdr.h:86
ipv4_hdr_t::fl_fo
network_uint16_t fl_fo
version control flags and Fragment Offset.
Definition: hdr.h:85
ipv4_hdr_get_version
static uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
Gets the value of the version field of hdr.
Definition: hdr.h:112
ipv4_hdr_get_fo
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:185
ipv4_hdr_set_fo
static void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
Sets the Fragment Offset field of hdr.
Definition: hdr.h:171
ipv4_hdr_t
Data type to represent an IPv4 packet header.
Definition: hdr.h:55
ipv4_hdr_get_ihl
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:136
ipv4_hdr_get_flags
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:160
ipv4_hdr_set_flags
static void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
Sets the Version Control Flags field of hdr.
Definition: hdr.h:147