net_misc.h
Go to the documentation of this file.
1 /**
2  * @file net_misc.h
3  * @brief Helper functions for TCP/IP stack
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _NET_MISC_H
32 #define _NET_MISC_H
33 
34 //Forward declaration of NetTxAncillary structure
35 struct _NetTxAncillary;
36 #define NetTxAncillary struct _NetTxAncillary
37 
38 //Forward declaration of NetRxAncillary structure
39 struct _NetRxAncillary;
40 #define NetRxAncillary struct _NetRxAncillary
41 
42 //Dependencies
43 #include "core/net.h"
44 #include "core/ethernet.h"
45 #include "core/ip.h"
46 
47 //Get a given bit of the PRNG internal state
48 #define NET_RAND_GET_BIT(s, n) ((s[(n - 1) / 8] >> ((n - 1) % 8)) & 1)
49 
50 //Set a given bit of the PRNG internal state
51 #define NET_RAND_STATE_SET_BIT(s, n, v) s[(n - 1) / 8] = \
52  (s[(n - 1) / 8] & ~(1 << ((n - 1) % 8))) | (v) << ((n - 1) % 8)
53 
54 //C++ guard
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 
60 /**
61  * @brief Link change callback
62  **/
63 
64 typedef void (*NetLinkChangeCallback)(NetInterface *interface,
65  bool_t linkState, void *param);
66 
67 
68 /**
69  * @brief Link change callback entry
70  **/
71 
72 typedef struct
73 {
76  void *param;
78 
79 
80 /**
81  * @brief Timer callback
82  **/
83 
84 typedef void (*NetTimerCallback)(void *param);
85 
86 
87 /**
88  * @brief Timer callback entry
89  **/
90 
91 typedef struct
92 {
96  void *param;
98 
99 
100 /**
101  * @brief Timestamp
102  **/
103 
104 typedef struct
105 {
106  uint32_t s;
107  uint32_t ns;
108 } NetTimestamp;
109 
110 
111 /**
112  * @brief Additional options passed to the stack (TX path)
113  **/
114 
116 {
117  uint8_t ttl; ///<Time-to-live value
118  uint8_t tos; ///<Type-of-service value
119  bool_t dontFrag; ///<Do not fragment the IP packet
120  bool_t dontRoute; ///<Do not send the packet via a router
121  bool_t routerAlert; ///<Add an IP Router Alert option
122 #if (ETH_SUPPORT == ENABLED)
123  MacAddr srcMacAddr; ///<Source MAC address
124  MacAddr destMacAddr; ///<Destination MAC address
125 #endif
126 #if (ETH_VLAN_SUPPORT == ENABLED)
127  int8_t vlanPcp; ///<VLAN priority (802.1Q)
128  int8_t vlanDei; ///<Drop eligible indicator
129 #endif
130 #if (ETH_VMAN_SUPPORT == ENABLED)
131  int8_t vmanPcp; ///<VMAN priority (802.1ad)
132  int8_t vmanDei; ///<Drop eligible indicator
133 #endif
134 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
135  uint8_t port; ///<Egress port identifier
136  uint32_t ports; ///<Egress port map
137  bool_t override; ///<Override port state
138 #endif
139 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
140  int32_t timestampId; ///<Unique identifier for hardware time stamping
141 #endif
142 };
143 
144 
145 /**
146  * @brief Additional options passed to the stack (RX path)
147  **/
148 
150 {
151  uint8_t ttl; ///<Time-to-live value
152  uint8_t tos; ///<Type-of-service value
153 #if (ETH_SUPPORT == ENABLED)
154  MacAddr srcMacAddr; ///<Source MAC address
155  MacAddr destMacAddr; ///<Destination MAC address
156  uint16_t ethType; ///<Ethernet type field
157 #endif
158 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
159  uint8_t port; ///<Ingress port identifier
160 #endif
161 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
162  NetTimestamp timestamp; ///<Captured time stamp
163 #endif
164 };
165 
166 
167 /**
168  * @brief Timer
169  **/
170 
171 typedef struct
172 {
176 } NetTimer;
177 
178 
179 /**
180  * @brief Pseudo-random number generator state
181  **/
182 
183 typedef struct
184 {
185  uint16_t counter;
186  uint8_t s[36];
187 } NetRandState;
188 
189 
190 //Global constants
193 
194 //TCP/IP stack related functions
196  NetLinkChangeCallback callback, void *param);
197 
199  NetLinkChangeCallback callback, void *param);
200 
201 void netProcessLinkChange(NetInterface *interface);
202 
204  void *param);
205 
206 error_t netDetachTimerCallback(NetTimerCallback callback, void *param);
207 
208 void netTick(void);
209 
210 void netStartTimer(NetTimer *timer, systime_t interval);
211 void netStopTimer(NetTimer *timer);
214 
215 void netInitRand(void);
216 uint32_t netGenerateRand(void);
217 uint32_t netGenerateRandRange(uint32_t min, uint32_t max);
218 void netGenerateRandData(uint8_t *data, size_t length);
219 uint32_t netGenerateRandBit(NetRandState *state);
220 
221 //C++ guard
222 #ifdef __cplusplus
223 }
224 #endif
225 
226 #endif
int bool_t
Definition: compiler_port.h:53
error_t
Error codes.
Definition: error.h:43
Ethernet.
uint8_t data[]
Definition: ethernet.h:222
MacAddr
Definition: ethernet.h:195
IPv4 and IPv6 common routines.
uint8_t s
Definition: ndp.h:345
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
void netInitRand(void)
Initialize random number generator.
Definition: net_misc.c:832
void netStartTimer(NetTimer *timer, systime_t interval)
Start timer.
Definition: net_misc.c:763
uint32_t netGenerateRandBit(NetRandState *state)
Generate one random bit.
Definition: net_misc.c:969
error_t netDetachTimerCallback(NetTimerCallback callback, void *param)
Unregister timer callback.
Definition: net_misc.c:382
#define NetRxAncillary
Definition: net_misc.h:40
#define NetTxAncillary
Definition: net_misc.h:36
void netProcessLinkChange(NetInterface *interface)
Process link state change event.
Definition: net_misc.c:198
void netGenerateRandData(uint8_t *data, size_t length)
Get a string of random data.
Definition: net_misc.c:941
const NetTxAncillary NET_DEFAULT_TX_ANCILLARY
Definition: net_misc.c:71
uint32_t netGenerateRand(void)
Generate a random 32-bit value.
Definition: net_misc.c:888
uint32_t netGenerateRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:914
error_t netDetachLinkChangeCallback(NetInterface *interface, NetLinkChangeCallback callback, void *param)
Unregister link change callback.
Definition: net_misc.c:165
bool_t netTimerExpired(NetTimer *timer)
Check whether the timer has expired.
Definition: net_misc.c:803
void(* NetLinkChangeCallback)(NetInterface *interface, bool_t linkState, void *param)
Link change callback.
Definition: net_misc.h:64
bool_t netTimerRunning(NetTimer *timer)
Check whether the timer is running.
Definition: net_misc.c:790
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:101
error_t netAttachTimerCallback(systime_t period, NetTimerCallback callback, void *param)
Register timer callback.
Definition: net_misc.c:344
error_t netAttachLinkChangeCallback(NetInterface *interface, NetLinkChangeCallback callback, void *param)
Register link change callback.
Definition: net_misc.c:127
void netStopTimer(NetTimer *timer)
Stop timer.
Definition: net_misc.c:777
void netTick(void)
Manage TCP/IP timers.
Definition: net_misc.c:413
void(* NetTimerCallback)(void *param)
Timer callback.
Definition: net_misc.h:84
uint32_t systime_t
System time.
Additional options passed to the stack (RX path)
Definition: net_misc.h:150
uint8_t port
Ingress port identifier.
Definition: net_misc.h:159
uint8_t tos
Type-of-service value.
Definition: net_misc.h:152
NetTimestamp timestamp
Captured time stamp.
Definition: net_misc.h:162
uint8_t ttl
Time-to-live value.
Definition: net_misc.h:151
MacAddr srcMacAddr
Source MAC address.
Definition: net_misc.h:154
MacAddr destMacAddr
Destination MAC address.
Definition: net_misc.h:155
uint16_t ethType
Ethernet type field.
Definition: net_misc.h:156
Additional options passed to the stack (TX path)
Definition: net_misc.h:116
uint8_t port
Egress port identifier.
Definition: net_misc.h:135
int8_t vmanPcp
VMAN priority (802.1ad)
Definition: net_misc.h:131
uint8_t tos
Type-of-service value.
Definition: net_misc.h:118
int32_t timestampId
Unique identifier for hardware time stamping.
Definition: net_misc.h:140
uint32_t ports
Egress port map.
Definition: net_misc.h:136
uint8_t ttl
Time-to-live value.
Definition: net_misc.h:117
bool_t dontRoute
Do not send the packet via a router.
Definition: net_misc.h:120
int8_t vlanPcp
VLAN priority (802.1Q)
Definition: net_misc.h:127
MacAddr srcMacAddr
Source MAC address.
Definition: net_misc.h:123
bool_t dontFrag
Do not fragment the IP packet.
Definition: net_misc.h:119
MacAddr destMacAddr
Destination MAC address.
Definition: net_misc.h:124
bool_t routerAlert
Add an IP Router Alert option.
Definition: net_misc.h:121
int8_t vmanDei
Drop eligible indicator.
Definition: net_misc.h:132
int8_t vlanDei
Drop eligible indicator.
Definition: net_misc.h:128
Link change callback entry.
Definition: net_misc.h:73
void * param
Definition: net_misc.h:76
NetLinkChangeCallback callback
Definition: net_misc.h:75
NetInterface * interface
Definition: net_misc.h:74
Pseudo-random number generator state.
Definition: net_misc.h:184
uint16_t counter
Definition: net_misc.h:185
Timer callback entry.
Definition: net_misc.h:92
void * param
Definition: net_misc.h:96
systime_t timerPeriod
Definition: net_misc.h:94
NetTimerCallback callback
Definition: net_misc.h:95
systime_t timerValue
Definition: net_misc.h:93
Timer.
Definition: net_misc.h:172
bool_t running
Definition: net_misc.h:173
systime_t interval
Definition: net_misc.h:175
systime_t startTime
Definition: net_misc.h:174
Timestamp.
Definition: net_misc.h:105
uint32_t ns
Definition: net_misc.h:107
uint32_t s
Definition: net_misc.h:106
uint8_t length
Definition: tcp.h:368