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.4
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 #if (UDP_SUPPORT == ENABLED)
118  bool_t noChecksum; ///<Disable UDP checksum generation
119 #endif
120  uint8_t ttl; ///<Time-to-live value
121  uint8_t tos; ///<Type-of-service value
122  bool_t dontFrag; ///<Do not fragment the IP packet
123  bool_t dontRoute; ///<Do not send the packet via a router
124  bool_t routerAlert; ///<Add an IP Router Alert option
125 #if (ETH_SUPPORT == ENABLED)
126  MacAddr srcMacAddr; ///<Source MAC address
127  MacAddr destMacAddr; ///<Destination MAC address
128 #endif
129 #if (ETH_VLAN_SUPPORT == ENABLED)
130  int8_t vlanPcp; ///<VLAN priority (802.1Q)
131  int8_t vlanDei; ///<Drop eligible indicator
132 #endif
133 #if (ETH_VMAN_SUPPORT == ENABLED)
134  int8_t vmanPcp; ///<VMAN priority (802.1ad)
135  int8_t vmanDei; ///<Drop eligible indicator
136 #endif
137 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
138  uint8_t port; ///<Egress port identifier
139  uint32_t ports; ///<Egress port map
140  bool_t override; ///<Override port state
141 #endif
142 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
143  int32_t timestampId; ///<Unique identifier for hardware time stamping
144 #endif
145 };
146 
147 
148 /**
149  * @brief Additional options passed to the stack (RX path)
150  **/
151 
153 {
154  uint8_t ttl; ///<Time-to-live value
155  uint8_t tos; ///<Type-of-service value
156 #if (ETH_SUPPORT == ENABLED)
157  MacAddr srcMacAddr; ///<Source MAC address
158  MacAddr destMacAddr; ///<Destination MAC address
159  uint16_t ethType; ///<Ethernet type field
160 #endif
161 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
162  uint8_t port; ///<Ingress port identifier
163 #endif
164 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
165  NetTimestamp timestamp; ///<Captured time stamp
166 #endif
167 };
168 
169 
170 /**
171  * @brief Timer
172  **/
173 
174 typedef struct
175 {
179 } NetTimer;
180 
181 
182 /**
183  * @brief Pseudo-random number generator state
184  **/
185 
186 typedef struct
187 {
188  uint16_t counter;
189  uint8_t s[36];
190 } NetRandState;
191 
192 
193 //Global constants
196 
197 //TCP/IP stack related functions
199  NetLinkChangeCallback callback, void *param);
200 
202  NetLinkChangeCallback callback, void *param);
203 
204 void netProcessLinkChange(NetInterface *interface);
205 
207  void *param);
208 
209 error_t netDetachTimerCallback(NetTimerCallback callback, void *param);
210 
211 void netTick(void);
212 
213 void netStartTimer(NetTimer *timer, systime_t interval);
214 void netStopTimer(NetTimer *timer);
218 
219 void netInitRand(void);
220 uint32_t netGenerateRand(void);
221 uint32_t netGenerateRandRange(uint32_t min, uint32_t max);
222 void netGenerateRandData(uint8_t *data, size_t length);
223 uint32_t netGenerateRandBit(NetRandState *state);
224 
225 //C++ guard
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #endif
NetTimerCallback callback
Definition: net_misc.h:95
error_t netAttachTimerCallback(systime_t period, NetTimerCallback callback, void *param)
Register timer callback.
Definition: net_misc.c:347
int bool_t
Definition: compiler_port.h:53
bool_t running
Definition: net_misc.h:176
systime_t timerPeriod
Definition: net_misc.h:94
systime_t timerValue
Definition: net_misc.h:93
uint16_t counter
Definition: net_misc.h:188
uint8_t data[]
Definition: ethernet.h:222
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
bool_t netTimerRunning(NetTimer *timer)
Check whether the timer is running.
Definition: net_misc.c:793
uint16_t ethType
Ethernet type field.
Definition: net_misc.h:159
systime_t netGetRemainingTime(NetTimer *timer)
Get the remaining value of the running timer.
Definition: net_misc.c:837
void * param
Definition: net_misc.h:96
void * param
Definition: net_misc.h:76
error_t netDetachTimerCallback(NetTimerCallback callback, void *param)
Unregister timer callback.
Definition: net_misc.c:385
Pseudo-random number generator state.
Definition: net_misc.h:187
Ethernet.
bool_t dontRoute
Do not send the packet via a router.
Definition: net_misc.h:123
void(* NetLinkChangeCallback)(NetInterface *interface, bool_t linkState, void *param)
Link change callback.
Definition: net_misc.h:64
uint8_t ttl
Time-to-live value.
Definition: net_misc.h:154
Timestamp.
Definition: net_misc.h:105
error_t
Error codes.
Definition: error.h:43
MacAddr srcMacAddr
Source MAC address.
Definition: net_misc.h:157
uint8_t ttl
Time-to-live value.
Definition: net_misc.h:120
Timer callback entry.
Definition: net_misc.h:92
uint32_t ports
Egress port map.
Definition: net_misc.h:139
MacAddr srcMacAddr
Source MAC address.
Definition: net_misc.h:126
void netStartTimer(NetTimer *timer, systime_t interval)
Start timer.
Definition: net_misc.c:766
Timer.
Definition: net_misc.h:175
Additional options passed to the stack (TX path)
Definition: net_misc.h:116
uint8_t tos
Type-of-service value.
Definition: net_misc.h:121
#define NetRxAncillary
Definition: net_misc.h:40
void netGenerateRandData(uint8_t *data, size_t length)
Get a string of random data.
Definition: net_misc.c:975
#define NetInterface
Definition: net.h:36
int8_t vmanDei
Drop eligible indicator.
Definition: net_misc.h:135
uint32_t netGenerateRandBit(NetRandState *state)
Generate one random bit.
Definition: net_misc.c:1003
bool_t noChecksum
Disable UDP checksum generation.
Definition: net_misc.h:118
void netTick(void)
Manage TCP/IP timers.
Definition: net_misc.c:416
NetInterface * interface
Definition: net_misc.h:74
uint8_t tos
Type-of-service value.
Definition: net_misc.h:155
#define NetTxAncillary
Definition: net_misc.h:36
systime_t startTime
Definition: net_misc.h:177
uint32_t netGenerateRand(void)
Generate a random 32-bit value.
Definition: net_misc.c:922
Additional options passed to the stack (RX path)
Definition: net_misc.h:153
bool_t routerAlert
Add an IP Router Alert option.
Definition: net_misc.h:124
uint8_t length
Definition: tcp.h:368
int8_t vmanPcp
VMAN priority (802.1ad)
Definition: net_misc.h:134
MacAddr destMacAddr
Destination MAC address.
Definition: net_misc.h:127
error_t netDetachLinkChangeCallback(NetInterface *interface, NetLinkChangeCallback callback, void *param)
Unregister link change callback.
Definition: net_misc.c:168
MacAddr
Definition: ethernet.h:195
uint8_t port
Egress port identifier.
Definition: net_misc.h:138
int8_t vlanPcp
VLAN priority (802.1Q)
Definition: net_misc.h:130
MacAddr destMacAddr
Destination MAC address.
Definition: net_misc.h:158
uint32_t systime_t
System time.
void netStopTimer(NetTimer *timer)
Stop timer.
Definition: net_misc.c:780
uint8_t port
Ingress port identifier.
Definition: net_misc.h:162
NetLinkChangeCallback callback
Definition: net_misc.h:75
IPv4 and IPv6 common routines.
Link change callback entry.
Definition: net_misc.h:73
void netProcessLinkChange(NetInterface *interface)
Process link state change event.
Definition: net_misc.c:201
bool_t dontFrag
Do not fragment the IP packet.
Definition: net_misc.h:122
uint8_t s
Definition: igmp_common.h:234
void netInitRand(void)
Initialize random number generator.
Definition: net_misc.c:866
const NetTxAncillary NET_DEFAULT_TX_ANCILLARY
Definition: net_misc.c:71
uint32_t ns
Definition: net_misc.h:107
bool_t netTimerExpired(NetTimer *timer)
Check whether the timer has expired.
Definition: net_misc.c:806
TCP/IP stack core.
error_t netAttachLinkChangeCallback(NetInterface *interface, NetLinkChangeCallback callback, void *param)
Register link change callback.
Definition: net_misc.c:130
NetTimestamp timestamp
Captured time stamp.
Definition: net_misc.h:165
uint32_t s
Definition: net_misc.h:106
int8_t vlanDei
Drop eligible indicator.
Definition: net_misc.h:131
uint32_t netGenerateRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:948
void(* NetTimerCallback)(void *param)
Timer callback.
Definition: net_misc.h:84
systime_t interval
Definition: net_misc.h:178
int32_t timestampId
Unique identifier for hardware time stamping.
Definition: net_misc.h:143