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-2023 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.3.2
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 dontRoute; ///<Do not send the packet via a router
120  bool_t routerAlert; ///<Add an IP Router Alert option
121 #if (ETH_SUPPORT == ENABLED)
122  MacAddr srcMacAddr; ///<Source MAC address
123  MacAddr destMacAddr; ///<Destination MAC address
124 #endif
125 #if (ETH_VLAN_SUPPORT == ENABLED)
126  int8_t vlanPcp; ///<VLAN priority (802.1Q)
127  int8_t vlanDei; ///<Drop eligible indicator
128 #endif
129 #if (ETH_VMAN_SUPPORT == ENABLED)
130  int8_t vmanPcp; ///<VMAN priority (802.1ad)
131  int8_t vmanDei; ///<Drop eligible indicator
132 #endif
133 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
134  uint8_t port; ///<Egress port identifier
135  uint32_t ports; ///<Egress port map
136  bool_t override; ///<Override port state
137 #endif
138 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
139  int32_t timestampId; ///<Unique identifier for hardware time stamping
140 #endif
141 };
142 
143 
144 /**
145  * @brief Additional options passed to the stack (RX path)
146  **/
147 
149 {
150  uint8_t ttl; ///<Time-to-live value
151  uint8_t tos; ///<Type-of-service value
152 #if (ETH_SUPPORT == ENABLED)
153  MacAddr srcMacAddr; ///<Source MAC address
154  MacAddr destMacAddr; ///<Destination MAC address
155  uint16_t ethType; ///<Ethernet type field
156 #endif
157 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
158  uint8_t port; ///<Ingress port identifier
159 #endif
160 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
161  NetTimestamp timestamp; ///<Captured time stamp
162 #endif
163 };
164 
165 
166 /**
167  * @brief Timer
168  **/
169 
170 typedef struct
171 {
175 } NetTimer;
176 
177 
178 /**
179  * @brief Pseudo-random number generator state
180  **/
181 
182 typedef struct
183 {
184  uint16_t counter;
185  uint8_t s[36];
186 } NetRandState;
187 
188 
189 //Global constants
192 
193 //TCP/IP stack related functions
195  NetLinkChangeCallback callback, void *param);
196 
198  NetLinkChangeCallback callback, void *param);
199 
200 void netProcessLinkChange(NetInterface *interface);
201 
203  void *param);
204 
205 error_t netDetachTimerCallback(NetTimerCallback callback, void *param);
206 
207 void netTick(void);
208 
209 void netStartTimer(NetTimer *timer, systime_t interval);
210 void netStopTimer(NetTimer *timer);
213 
214 void netInitRand(void);
215 uint32_t netGenerateRand(void);
216 uint32_t netGenerateRandRange(uint32_t min, uint32_t max);
217 void netGenerateRandData(uint8_t *data, size_t length);
218 uint32_t netGenerateRandBit(NetRandState *state);
219 
220 //C++ guard
221 #ifdef __cplusplus
222 }
223 #endif
224 
225 #endif
int bool_t
Definition: compiler_port.h:53
error_t
Error codes.
Definition: error.h:43
Ethernet.
uint8_t data[]
Definition: ethernet.h:220
MacAddr
Definition: ethernet.h:193
IPv4 and IPv6 common routines.
uint8_t s
Definition: ndp.h:343
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
void netInitRand(void)
Initialize random number generator.
Definition: net_misc.c:831
void netStartTimer(NetTimer *timer, systime_t interval)
Start timer.
Definition: net_misc.c:762
uint32_t netGenerateRandBit(NetRandState *state)
Generate one random bit.
Definition: net_misc.c:968
error_t netDetachTimerCallback(NetTimerCallback callback, void *param)
Unregister timer callback.
Definition: net_misc.c:381
#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:197
void netGenerateRandData(uint8_t *data, size_t length)
Get a string of random data.
Definition: net_misc.c:940
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:887
uint32_t netGenerateRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:913
error_t netDetachLinkChangeCallback(NetInterface *interface, NetLinkChangeCallback callback, void *param)
Unregister link change callback.
Definition: net_misc.c:164
bool_t netTimerExpired(NetTimer *timer)
Check whether the timer has expired.
Definition: net_misc.c:802
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:789
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:100
error_t netAttachTimerCallback(systime_t period, NetTimerCallback callback, void *param)
Register timer callback.
Definition: net_misc.c:343
error_t netAttachLinkChangeCallback(NetInterface *interface, NetLinkChangeCallback callback, void *param)
Register link change callback.
Definition: net_misc.c:126
void netStopTimer(NetTimer *timer)
Stop timer.
Definition: net_misc.c:776
void netTick(void)
Manage TCP/IP timers.
Definition: net_misc.c:412
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:149
uint8_t port
Ingress port identifier.
Definition: net_misc.h:158
uint8_t tos
Type-of-service value.
Definition: net_misc.h:151
NetTimestamp timestamp
Captured time stamp.
Definition: net_misc.h:161
uint8_t ttl
Time-to-live value.
Definition: net_misc.h:150
MacAddr srcMacAddr
Source MAC address.
Definition: net_misc.h:153
MacAddr destMacAddr
Destination MAC address.
Definition: net_misc.h:154
uint16_t ethType
Ethernet type field.
Definition: net_misc.h:155
Additional options passed to the stack (TX path)
Definition: net_misc.h:116
uint8_t port
Egress port identifier.
Definition: net_misc.h:134
int8_t vmanPcp
VMAN priority (802.1ad)
Definition: net_misc.h:130
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:139
uint32_t ports
Egress port map.
Definition: net_misc.h:135
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:119
int8_t vlanPcp
VLAN priority (802.1Q)
Definition: net_misc.h:126
MacAddr srcMacAddr
Source MAC address.
Definition: net_misc.h:122
MacAddr destMacAddr
Destination MAC address.
Definition: net_misc.h:123
bool_t routerAlert
Add an IP Router Alert option.
Definition: net_misc.h:120
int8_t vmanDei
Drop eligible indicator.
Definition: net_misc.h:131
int8_t vlanDei
Drop eligible indicator.
Definition: net_misc.h:127
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:183
uint16_t counter
Definition: net_misc.h:184
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:171
bool_t running
Definition: net_misc.h:172
systime_t interval
Definition: net_misc.h:174
systime_t startTime
Definition: net_misc.h:173
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:366