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-2022 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.1.8
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  bool_t dontRoute; ///<Do not send the packet via a router
119  bool_t routerAlert; ///<Add an IP Router Alert option
120 #if (IP_DIFF_SERV_SUPPORT == ENABLED)
121  uint8_t dscp; ///<Differentiated services codepoint
122 #endif
123 #if (ETH_SUPPORT == ENABLED)
124  MacAddr srcMacAddr; ///<Source MAC address
125  MacAddr destMacAddr; ///<Destination MAC address
126 #endif
127 #if (ETH_VLAN_SUPPORT == ENABLED)
128  int8_t vlanPcp; ///<VLAN priority (802.1Q)
129  int8_t vlanDei; ///<Drop eligible indicator
130 #endif
131 #if (ETH_VMAN_SUPPORT == ENABLED)
132  int8_t vmanPcp; ///<VMAN priority (802.1ad)
133  int8_t vmanDei; ///<Drop eligible indicator
134 #endif
135 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
136  uint8_t port; ///<Egress port identifier
137  uint32_t ports; ///<Egress port map
138  bool_t override; ///<Override port state
139 #endif
140 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
141  int32_t timestampId; ///<Unique identifier for hardware time stamping
142 #endif
143 };
144 
145 
146 /**
147  * @brief Additional options passed to the stack (RX path)
148  **/
149 
151 {
152  uint8_t ttl; ///<Time-to-live 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
uint8_t length
Definition: coap_common.h:193
NetTimerCallback callback
Definition: net_misc.h:95
error_t netAttachTimerCallback(systime_t period, NetTimerCallback callback, void *param)
Register timer callback.
Definition: net_misc.c:344
int bool_t
Definition: compiler_port.h:48
bool_t running
Definition: net_misc.h:173
systime_t timerPeriod
Definition: net_misc.h:94
uint8_t data[]
Definition: ethernet.h:220
systime_t timerValue
Definition: net_misc.h:93
uint16_t counter
Definition: net_misc.h:185
uint8_t dscp
Differentiated services codepoint.
Definition: net_misc.h:121
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:102
bool_t netTimerRunning(NetTimer *timer)
Check whether the timer is running.
Definition: net_misc.c:776
uint16_t ethType
Ethernet type field.
Definition: net_misc.h:156
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:382
Pseudo-random number generator state.
Definition: net_misc.h:184
Ethernet.
bool_t dontRoute
Do not send the packet via a router.
Definition: net_misc.h:118
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:152
__start_packed struct @0 MacAddr
MAC address.
Timestamp.
Definition: net_misc.h:105
error_t
Error codes.
Definition: error.h:43
MacAddr srcMacAddr
Source MAC address.
Definition: net_misc.h:154
uint8_t ttl
Time-to-live value.
Definition: net_misc.h:117
Timer callback entry.
Definition: net_misc.h:92
uint32_t ports
Egress port map.
Definition: net_misc.h:137
MacAddr srcMacAddr
Source MAC address.
Definition: net_misc.h:124
void netStartTimer(NetTimer *timer, systime_t interval)
Start timer.
Definition: net_misc.c:749
Timer.
Definition: net_misc.h:172
Additional options passed to the stack (TX path)
Definition: net_misc.h:116
#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:927
#define NetInterface
Definition: net.h:36
int8_t vmanDei
Drop eligible indicator.
Definition: net_misc.h:133
uint32_t netGenerateRandBit(NetRandState *state)
Generate one random bit.
Definition: net_misc.c:955
void netTick(void)
Manage TCP/IP timers.
Definition: net_misc.c:413
NetInterface * interface
Definition: net_misc.h:74
#define NetTxAncillary
Definition: net_misc.h:36
systime_t startTime
Definition: net_misc.h:174
uint32_t netGenerateRand(void)
Generate a random 32-bit value.
Definition: net_misc.c:874
Additional options passed to the stack (RX path)
Definition: net_misc.h:151
bool_t routerAlert
Add an IP Router Alert option.
Definition: net_misc.h:119
int8_t vmanPcp
VMAN priority (802.1ad)
Definition: net_misc.h:132
MacAddr destMacAddr
Destination MAC address.
Definition: net_misc.h:125
error_t netDetachLinkChangeCallback(NetInterface *interface, NetLinkChangeCallback callback, void *param)
Unregister link change callback.
Definition: net_misc.c:165
uint8_t port
Egress port identifier.
Definition: net_misc.h:136
int8_t vlanPcp
VLAN priority (802.1Q)
Definition: net_misc.h:128
MacAddr destMacAddr
Destination MAC address.
Definition: net_misc.h:155
uint32_t systime_t
System time.
void netStopTimer(NetTimer *timer)
Stop timer.
Definition: net_misc.c:763
uint8_t port
Ingress port identifier.
Definition: net_misc.h:159
NetLinkChangeCallback callback
Definition: net_misc.h:75
IPv4 and IPv6 common routines.
Link change callback entry.
Definition: net_misc.h:73
uint8_t s
void netProcessLinkChange(NetInterface *interface)
Process link state change event.
Definition: net_misc.c:198
void netInitRand(void)
Initialize random number generator.
Definition: net_misc.c:818
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:789
TCP/IP stack core.
error_t netAttachLinkChangeCallback(NetInterface *interface, NetLinkChangeCallback callback, void *param)
Register link change callback.
Definition: net_misc.c:127
NetTimestamp timestamp
Captured time stamp.
Definition: net_misc.h:162
uint32_t s
Definition: net_misc.h:106
int8_t vlanDei
Drop eligible indicator.
Definition: net_misc.h:129
uint32_t netGenerateRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:900
void(* NetTimerCallback)(void *param)
Timer callback.
Definition: net_misc.h:84
systime_t interval
Definition: net_misc.h:175
int32_t timestampId
Unique identifier for hardware time stamping.
Definition: net_misc.h:141