ipv4_frag.h
Go to the documentation of this file.
1 /**
2  * @file ipv4_frag.h
3  * @brief IPv4 fragmentation and reassembly
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _IPV4_FRAG_H
30 #define _IPV4_FRAG_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "ipv4/ipv4.h"
35 
36 //IPv4 fragmentation support
37 #ifndef IPV4_FRAG_SUPPORT
38  #define IPV4_FRAG_SUPPORT ENABLED
39 #elif (IPV4_FRAG_SUPPORT != ENABLED && IPV4_FRAG_SUPPORT != DISABLED)
40  #error IPV4_FRAG_SUPPORT parameter is not valid
41 #endif
42 
43 //Reassembly algorithm tick interval
44 #ifndef IPV4_FRAG_TICK_INTERVAL
45  #define IPV4_FRAG_TICK_INTERVAL 1000
46 #elif (IPV4_FRAG_TICK_INTERVAL < 10)
47  #error IPV4_FRAG_TICK_INTERVAL parameter is not valid
48 #endif
49 
50 //Maximum number of fragmented packets the host will accept
51 //and hold in the reassembly queue simultaneously
52 #ifndef IPV4_MAX_FRAG_DATAGRAMS
53  #define IPV4_MAX_FRAG_DATAGRAMS 4
54 #elif (IPV4_MAX_FRAG_DATAGRAMS < 1)
55  #error IPV4_MAX_FRAG_DATAGRAMS parameter is not valid
56 #endif
57 
58 //Maximum datagram size the host will accept when reassembling fragments
59 #ifndef IPV4_MAX_FRAG_DATAGRAM_SIZE
60  #define IPV4_MAX_FRAG_DATAGRAM_SIZE 8192
61 #elif (IPV4_MAX_FRAG_DATAGRAM_SIZE < 576)
62  #error IPV4_MAX_FRAG_DATAGRAM_SIZE parameter is not valid
63 #endif
64 
65 //Maximum time an IPv4 fragment can spend waiting to be reassembled
66 #ifndef IPV4_FRAG_TIME_TO_LIVE
67  #define IPV4_FRAG_TIME_TO_LIVE 15000
68 #elif (IPV4_FRAG_TIME_TO_LIVE < 1000)
69  #error IPV4_FRAG_TIME_TO_LIVE parameter is not valid
70 #endif
71 
72 //Infinity is implemented by a very large integer
73 #define IPV4_INFINITY 0xFFFF
74 
75 //C++ guard
76 #ifdef __cplusplus
77  extern "C" {
78 #endif
79 
80 
81 //CodeWarrior or Win32 compiler?
82 #if defined(__CWCC__) || defined(_WIN32)
83  #pragma pack(push, 1)
84 #endif
85 
86 
87 /**
88  * @brief Hole descriptor
89  **/
90 
91 typedef __start_packed struct
92 {
93  uint16_t first;
94  uint16_t last;
95  uint16_t next;
97 
98 
99 //CodeWarrior or Win32 compiler?
100 #if defined(__CWCC__) || defined(_WIN32)
101  #pragma pack(pop)
102 #endif
103 
104 
105 /**
106  * @brief Reassembly buffer
107  **/
108 
109 typedef struct
110 {
115 
116 
117 /**
118  * @brief Fragmented packet descriptor
119  **/
120 
121 typedef struct
122 {
123  systime_t timestamp; ///<Time at which the first fragment was received
124  size_t headerLength; ///<Length of the header
125  size_t dataLen; ///<Length of the payload
126  uint16_t firstHole; ///<Index of the first hole
127  Ipv4ReassemblyBuffer buffer; ///<Buffer containing the reassembled datagram
128 } Ipv4FragDesc;
129 
130 
131 //Tick counter to handle periodic operations
133 
134 //IPv4 datagram fragmentation and reassembly
136  uint16_t id, const NetBuffer *payload, size_t payloadOffset, uint8_t timeToLive);
137 
138 void ipv4ReassembleDatagram(NetInterface *interface,
139  const Ipv4Header *packet, size_t length);
140 
141 void ipv4FragTick(NetInterface *interface);
142 
143 Ipv4FragDesc *ipv4SearchFragQueue(NetInterface *interface, const Ipv4Header *packet);
144 void ipv4FlushFragQueue(NetInterface *interface);
145 
146 Ipv4HoleDesc *ipv4FindHole(Ipv4FragDesc *frag, uint16_t offset);
147 void ipv4DumpHoleList(Ipv4FragDesc *frag);
148 
149 //C++ guard
150 #ifdef __cplusplus
151  }
152 #endif
153 
154 #endif
uint32_t systime_t
Definition: compiler_port.h:44
size_t headerLength
Length of the header.
Definition: ipv4_frag.h:124
TCP/IP stack core.
void ipv4DumpHoleList(Ipv4FragDesc *frag)
Dump hole descriptor list.
Definition: ipv4_frag.c:649
systime_t ipv4FragTickCounter
Definition: ipv4_frag.c:55
Ipv4HoleDesc * ipv4FindHole(Ipv4FragDesc *frag, uint16_t offset)
Retrieve hole descriptor.
Definition: ipv4_frag.c:637
#define Ipv4PseudoHeader
Definition: ipv4.h:37
#define IPV4_MAX_FRAG_DATAGRAM_SIZE
Definition: ipv4_frag.h:60
void ipv4FragTick(NetInterface *interface)
Fragment reassembly timeout handler.
Definition: ipv4_frag.c:446
uint16_t first
Definition: ipv4_frag.h:93
Ipv4FragDesc * ipv4SearchFragQueue(NetInterface *interface, const Ipv4Header *packet)
Search for a matching datagram in the reassembly queue.
Definition: ipv4_frag.c:514
Fragmented packet descriptor.
Definition: ipv4_frag.h:121
systime_t timestamp
Time at which the first fragment was received.
Definition: ipv4_frag.h:123
uint16_t next
Definition: ipv4_frag.h:95
IPv4 (Internet Protocol Version 4)
Ipv4ReassemblyBuffer buffer
Buffer containing the reassembled datagram.
Definition: ipv4_frag.h:127
uint16_t last
Definition: ipv4_frag.h:94
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
error_t ipv4FragmentDatagram(NetInterface *interface, Ipv4PseudoHeader *pseudoHeader, uint16_t id, const NetBuffer *payload, size_t payloadOffset, uint8_t timeToLive)
Fragment an IPv4 datagram into smaller packets.
Definition: ipv4_frag.c:69
__start_packed struct _Ipv4Header __end_packed
#define N(size)
Definition: net_mem.h:62
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
void ipv4ReassembleDatagram(NetInterface *interface, const Ipv4Header *packet, size_t length)
IPv4 datagram reassembly algorithm.
Definition: ipv4_frag.c:176
#define NetInterface
Definition: net.h:34
Reassembly buffer.
Definition: ipv4_frag.h:109
#define Ipv4Header
Definition: ipv4.h:34
__start_packed struct @175 Ipv4HoleDesc
Hole descriptor.
void ipv4FlushFragQueue(NetInterface *interface)
Flush IPv4 reassembly queue.
Definition: ipv4_frag.c:616
uint8_t length
Definition: dtls_misc.h:140
uint16_t firstHole
Index of the first hole.
Definition: ipv4_frag.h:126
size_t dataLen
Length of the payload.
Definition: ipv4_frag.h:125
Structure describing a chunk of data.
Definition: net_mem.h:74