ipv4/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 
network_uint16_t fl_fo
version control flags and Fragment Offset.
Definition: ipv4/hdr.h:85
IPv6 address type and helper functions definitions.
static uint16_t ipv4_hdr_get_ihl(ipv4_hdr_t *hdr)
brief Gets the value of the Internet Header Length field of hdr
Definition: ipv4/hdr.h:136
network_uint16_t tl
total length of the datagram
Definition: ipv4/hdr.h:71
uint8_t protocol
protocol of this packet
Definition: ipv4/hdr.h:87
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:76
uint8_t ts
type of service of packet
Definition: ipv4/hdr.h:70
Data type to represent an IPv4 packet header.
Definition: ipv4/hdr.h:55
uint8_t ttl
time to live for this packet
Definition: ipv4/hdr.h:86
network_uint16_t id
identification value of packet
Definition: ipv4/hdr.h:72
static uint8_t ipv4_hdr_get_version(ipv4_hdr_t *hdr)
Gets the value of the version field of hdr.
Definition: ipv4/hdr.h:112
static void ipv4_hdr_set_version(ipv4_hdr_t *hdr)
Sets the version field of hdr to 6.
Definition: ipv4/hdr.h:99
static uint16_t ipv4_hdr_get_fo(ipv4_hdr_t *hdr)
brief Gets the value of the Fragment Offset field of hdr
Definition: ipv4/hdr.h:185
ipv4_addr_t src
source address of this packet
Definition: ipv4/hdr.h:89
static void ipv4_hdr_set_fo(ipv4_hdr_t *hdr, uint16_t fo)
Sets the Fragment Offset field of hdr.
Definition: ipv4/hdr.h:171
Functions to work with different byte orders.
network_uint16_t csum
checksum of this packet
Definition: ipv4/hdr.h:88
static uint8_t ipv4_hdr_get_flags(ipv4_hdr_t *hdr)
brief Gets the value of the Version Control Flags field of hdr
Definition: ipv4/hdr.h:160
Data type to represent an IPv4 address.
Definition: ipv4/addr.h:40
uint8_t v_ih
Version and Internet Header Length.
Definition: ipv4/hdr.h:69
uint8_t u8[2]
8 bit representation
Definition: byteorder.h:78
ipv4_addr_t dst
destination address of this packet
Definition: ipv4/hdr.h:90
static void ipv4_hdr_set_flags(ipv4_hdr_t *hdr, uint8_t flags)
Sets the Version Control Flags field of hdr.
Definition: ipv4/hdr.h:147
static void ipv4_hdr_set_ihl(ipv4_hdr_t *hdr, uint16_t ihl)
Sets the Internet Header Length field of hdr.
Definition: ipv4/hdr.h:123