net.h
Go to the documentation of this file.
1 /**
2  * @file net.h
3  * @brief TCP/IP stack core
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 _NET_H
30 #define _NET_H
31 
32 //Forward declaration of NetInterface structure
33 struct _NetInterface;
34 #define NetInterface struct _NetInterface
35 
36 //Dependencies
37 #include "os_port.h"
38 #include "net_config.h"
39 #include "core/net_legacy.h"
40 #include "core/net_mem.h"
41 #include "core/nic.h"
42 #include "core/ethernet.h"
43 #include "ipv4/ipv4.h"
44 #include "ipv4/ipv4_frag.h"
45 #include "ipv4/auto_ip.h"
46 #include "ipv6/ipv6.h"
47 #include "ipv4/arp.h"
48 #include "ipv6/ndp.h"
49 #include "ipv6/ndp_router_adv.h"
50 #include "ipv6/slaac.h"
51 #include "ppp/ppp.h"
52 #include "dhcp/dhcp_client.h"
53 #include "dhcp/dhcp_server.h"
54 #include "dhcpv6/dhcpv6_client.h"
55 #include "dns/dns_client.h"
56 #include "mdns/mdns_responder.h"
57 #include "mdns/mdns_common.h"
58 #include "dns_sd/dns_sd.h"
59 #include "cpu_endian.h"
60 #include "error.h"
61 
62 
63 /*
64  * CycloneTCP Open is licensed under GPL version 2. In particular:
65  *
66  * - If you link your program to CycloneTCP Open, the result is a derivative
67  * work that can only be distributed under the same GPL license terms.
68  *
69  * - If additions or changes to CycloneTCP Open are made, the result is a
70  * derivative work that can only be distributed under the same license terms.
71  *
72  * - The GPL license requires that you make the source code available to
73  * whoever you make the binary available to.
74  *
75  * - If you sell or distribute a hardware product that runs CycloneTCP Open,
76  * the GPL license requires you to provide public and full access to all
77  * source code on a nondiscriminatory basis.
78  *
79  * If you fully understand and accept the terms of the GPL license, then edit
80  * the os_port_config.h header and add the following directive:
81  *
82  * #define GPL_LICENSE_TERMS_ACCEPTED
83  */
84 
85 #ifndef GPL_LICENSE_TERMS_ACCEPTED
86  #error Before compiling CycloneTCP Open, you must accept the terms of the GPL license
87 #endif
88 
89 
90 //Version string
91 #define NET_VERSION_STRING "1.9.0"
92 //Major version
93 #define NET_MAJOR_VERSION 1
94 //Minor version
95 #define NET_MINOR_VERSION 9
96 //Revision number
97 #define NET_REV_NUMBER 0
98 
99 //RTOS support
100 #ifndef NET_RTOS_SUPPORT
101  #define NET_RTOS_SUPPORT ENABLED
102 #elif (NET_RTOS_SUPPORT != ENABLED && NET_RTOS_SUPPORT != DISABLED)
103  #error NET_RTOS_SUPPORT parameter is not valid
104 #endif
105 
106 //Number of network adapters
107 #ifndef NET_INTERFACE_COUNT
108  #define NET_INTERFACE_COUNT 1
109 #elif (NET_INTERFACE_COUNT < 1)
110  #error NET_INTERFACE_COUNT parameter is not valid
111 #endif
112 
113 //Maximum number of callback functions that can be registered
114 //to monitor link changes
115 #ifndef NET_CALLBACK_TABLE_SIZE
116  #define NET_CALLBACK_TABLE_SIZE 6
117 #elif (NET_CALLBACK_TABLE_SIZE < 1)
118  #error NET_CALLBACK_TABLE_SIZE parameter is not valid
119 #endif
120 
121 //Maximum length of interface name
122 #ifndef NET_MAX_IF_NAME_LEN
123  #define NET_MAX_IF_NAME_LEN 8
124 #elif (NET_MAX_IF_NAME_LEN < 1)
125  #error NET_MAX_IF_NAME_LEN parameter is not valid
126 #endif
127 
128 //Maximum length of host name
129 #ifndef NET_MAX_HOSTNAME_LEN
130  #define NET_MAX_HOSTNAME_LEN 16
131 #elif (NET_MAX_HOSTNAME_LEN < 1)
132  #error NET_MAX_HOSTNAME_LEN parameter is not valid
133 #endif
134 
135 //Maximum length of proxy server name
136 #ifndef NET_MAX_PROXY_NAME_LEN
137  #define NET_MAX_PROXY_NAME_LEN 16
138 #elif (NET_MAX_PROXY_NAME_LEN < 1)
139  #error NET_MAX_PROXY_NAME_LEN parameter is not valid
140 #endif
141 
142 //OS resources are statically allocated at compile time
143 #ifndef NET_STATIC_OS_RESOURCES
144  #define NET_STATIC_OS_RESOURCES DISABLED
145 #elif (NET_STATIC_OS_RESOURCES != ENABLED && NET_STATIC_OS_RESOURCES != DISABLED)
146  #error NET_STATIC_OS_RESOURCES parameter is not valid
147 #endif
148 
149 //Stack size required to run the TCP/IP task
150 #ifndef NET_TASK_STACK_SIZE
151  #define NET_TASK_STACK_SIZE 650
152 #elif (NET_TASK_STACK_SIZE < 1)
153  #error NET_TASK_STACK_SIZE parameter is not valid
154 #endif
155 
156 //Priority at which the TCP/IP task should run
157 #ifndef NET_TASK_PRIORITY
158  #define NET_TASK_PRIORITY OS_TASK_PRIORITY_HIGH
159 #endif
160 
161 //TCP/IP stack tick interval
162 #ifndef NET_TICK_INTERVAL
163  #define NET_TICK_INTERVAL 100
164 #elif (NET_TICK_INTERVAL < 10)
165  #error NET_TICK_INTERVAL parameter is not valid
166 #endif
167 
168 //C++ guard
169 #ifdef __cplusplus
170  extern "C" {
171 #endif
172 
173 
174 /**
175  * @brief Structure describing a network interface
176  **/
177 
179 {
180  uint_t index; ///<Zero-based index
181  uint32_t id; ///<A unique number identifying the interface
182  Eui64 eui64; ///<EUI-64 interface identifier
183  char_t name[NET_MAX_IF_NAME_LEN + 1]; ///<A unique name identifying the interface
185  char_t proxyName[NET_MAX_PROXY_NAME_LEN + 1]; ///<Proxy server name
186  uint16_t proxyPort; ///<Proxy server port
187  const NicDriver *nicDriver; ///<NIC driver
188  const PhyDriver *phyDriver; ///<PHY driver
189  uint8_t phyAddr; ///<PHY address
190  const SpiDriver *spiDriver; ///<Underlying SPI driver
191  const UartDriver *uartDriver; ///<Underlying UART driver
192  const ExtIntDriver *extIntDriver; ///<External interrupt line driver
193  uint8_t nicContext[NIC_CONTEXT_SIZE]; ///<Driver specific context
194  OsEvent nicTxEvent; ///<Network controller TX event
195  bool_t nicEvent; ///<A NIC event is pending
196  bool_t phyEvent; ///<A PHY event is pending
197  bool_t linkState; ///<Link state
198  uint32_t linkSpeed; ///<Link speed
199  NicDuplexMode duplexMode; ///<Duplex mode
200  bool_t configured; ///<Configuration done
201 
202 #if (ETH_SUPPORT == ENABLED)
203  MacAddr macAddr; ///<Link-layer address
205 #endif
206 #if (ETH_VLAN_SUPPORT == ENABLED)
207  uint16_t vlanId; ///<VLAN identifier (802.1q)
208 #endif
209 #if (ETH_VMAN_SUPPORT == ENABLED)
210  uint16_t vmanId; ///<VMAN identifier (802.1ad)
211 #endif
212 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
213  uint8_t port; ///<Switch port identifier
214 #endif
215 #if (ETH_VLAN_SUPPORT == ENABLED || ETH_PORT_TAGGING_SUPPORT == ENABLED)
216  NetInterface *parent; ///<Interface on top of which the virtual interface runs
217 #endif
218 
219 #if (IPV4_SUPPORT == ENABLED)
220  Ipv4Context ipv4Context; ///<IPv4 context
222 #if (IGMP_SUPPORT == ENABLED)
223  systime_t igmpv1RouterPresentTimer; ///<IGMPv1 router present timer
224  bool_t igmpv1RouterPresent; ///<An IGMPv1 query has been recently heard
225 #endif
226 #if (AUTO_IP_SUPPORT == ENABLED)
227  AutoIpContext *autoIpContext; ///<Auto-IP context
228 #endif
229 #if (DHCP_CLIENT_SUPPORT == ENABLED)
230  DhcpClientContext *dhcpClientContext; ///<DHCP client context
231 #endif
232 #if (DHCP_SERVER_SUPPORT == ENABLED)
233  DhcpServerContext *dhcpServerContext; ///<DHCP server context
234 #endif
235 #endif
236 
237 #if (IPV6_SUPPORT == ENABLED)
238  Ipv6Context ipv6Context; ///<IPv6 context
239 #if (NDP_SUPPORT == ENABLED)
240  NdpContext ndpContext; ///<NDP context
241 #endif
242 #if (NDP_ROUTER_ADV_SUPPORT == ENABLED)
243  NdpRouterAdvContext *ndpRouterAdvContext; ///<RA service context
244 #endif
245 #if (SLAAC_SUPPORT == ENABLED)
246  SlaacContext *slaacContext; ///<SLAAC context
247 #endif
248 #if (DHCPV6_CLIENT_SUPPORT == ENABLED)
249  Dhcpv6ClientContext *dhcpv6ClientContext; ///<DHCPv6 client context
250 #endif
251 #endif
252 
253 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
254  MdnsResponderContext *mdnsResponderContext; ///<mDNS responder context
255 #endif
256 
257 #if (DNS_SD_SUPPORT == ENABLED)
258  DnsSdContext *dnsSdContext; ///DNS-SD context
259 #endif
260 
261 #if (PPP_SUPPORT == ENABLED)
262  PppContext *pppContext; ///<PPP context
263 #endif
264 };
265 
266 
267 /**
268  * @brief Link change callback
269  **/
270 
271 typedef void (*LinkChangeCallback)(NetInterface *interface, bool_t linkState, void *param);
272 
273 
274 /**
275  * @brief Entry describing a user callback
276  **/
277 
278 typedef struct
279 {
282  void *param;
284 
285 
286 //Global variables
287 extern OsTask *netTaskHandle;
288 extern OsMutex netMutex;
289 extern OsEvent netEvent;
291 
292 //TCP/IP stack related functions
293 error_t netInit(void);
294 
295 error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr);
296 error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr);
297 
298 error_t netSetEui64(NetInterface *interface, const Eui64 *eui64);
299 error_t netGetEui64(NetInterface *interface, Eui64 *eui64);
300 
301 error_t netSetInterfaceId(NetInterface *interface, uint32_t id);
303 error_t netSetHostname(NetInterface *interface, const char_t *name);
304 error_t netSetProxy(NetInterface *interface, const char_t *name, uint16_t port);
305 
306 error_t netSetVlanId(NetInterface *interface, uint16_t vlanId);
307 error_t netSetVmanId(NetInterface *interface, uint16_t vmanId);
308 error_t netSetSwitchPort(NetInterface *interface, uint8_t port);
309 
311  NetInterface *physicalInterface);
312 
313 error_t netSetDriver(NetInterface *interface, const NicDriver *driver);
314 
315 error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver);
316 error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr);
317 
318 error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver);
319 error_t netSetUartDriver(NetInterface *interface, const UartDriver *driver);
320 error_t netSetExtIntDriver(NetInterface *interface, const ExtIntDriver *driver);
321 
322 error_t netSetLinkState(NetInterface *interface, NicLinkState linkState);
324 
326 
327 void netTask(void);
328 void netTick(void);
329 
331 
332 error_t netInitRand(uint32_t seed);
333 uint32_t netGetRand(void);
334 int32_t netGetRandRange(int32_t min, int32_t max);
335 
337  LinkChangeCallback callback, void *param, uint_t *cookie);
338 
340 
341 void netInvokeLinkChangeCallback(NetInterface *interface, bool_t linkState);
342 
343 //C++ guard
344 #ifdef __cplusplus
345  }
346 #endif
347 
348 #endif
PPP (Point-to-Point Protocol)
ARP (Address Resolution Protocol)
Ipv4Context ipv4Context
IPv4 context.
Definition: net.h:220
bool_t nicEvent
A NIC event is pending.
Definition: net.h:195
uint32_t systime_t
Definition: compiler_port.h:44
bool_t configured
Configuration done.
Definition: net.h:200
#define NIC_CONTEXT_SIZE
Definition: nic.h:51
error_t netSetParentInterface(NetInterface *interface, NetInterface *physicalInterface)
Attach a virtual interface to a given physical interface.
Definition: net.c:637
#define DhcpClientContext
Definition: dhcp_client.h:152
void netInvokeLinkChangeCallback(NetInterface *interface, bool_t linkState)
Invoke link change callback.
Definition: net.c:1657
error_t netSetInterfaceId(NetInterface *interface, uint32_t id)
Set interface identifier.
Definition: net.c:429
mDNS responder (Multicast DNS)
bool_t igmpv1RouterPresent
An IGMPv1 query has been recently heard.
Definition: net.h:224
char char_t
Definition: compiler_port.h:41
MacAddr macAddr
Link-layer address.
Definition: net.h:203
DnsSdContext * dnsSdContext
Definition: net.h:258
DhcpClientContext * dhcpClientContext
DHCP client context.
Definition: net.h:230
const UartDriver * uartDriver
Underlying UART driver.
Definition: net.h:191
DHCP server (Dynamic Host Configuration Protocol)
DhcpServerContext * dhcpServerContext
DHCP server context.
Definition: net.h:233
Structure describing a network interface.
Definition: net.h:178
error_t netSetVlanId(NetInterface *interface, uint16_t vlanId)
Specify VLAN identifier (802.1q)
Definition: net.c:538
NicDuplexMode
Duplex mode.
Definition: nic.h:104
uint8_t nicContext[NIC_CONTEXT_SIZE]
Driver specific context.
Definition: net.h:193
NetInterface netInterface[NET_INTERFACE_COUNT]
Definition: net.c:74
int32_t netGetRandRange(int32_t min, int32_t max)
Get a random value in the specified range.
Definition: net.c:1554
NdpContext ndpContext
NDP context.
Definition: net.h:240
IPv6 Stateless Address Autoconfiguration.
NicLinkState
Link state.
Definition: nic.h:79
void(* LinkChangeCallback)(NetInterface *interface, bool_t linkState, void *param)
Link change callback.
Definition: net.h:271
Legacy definitions.
UART driver.
Definition: nic.h:227
uint16_t vlanId
VLAN identifier (802.1q)
Definition: net.h:207
char_t name[NET_MAX_IF_NAME_LEN+1]
A unique name identifying the interface.
Definition: net.h:183
error_t netSetProxy(NetInterface *interface, const char_t *name, uint16_t port)
Set proxy server.
Definition: net.c:509
Eui64 eui64
EUI-64 interface identifier.
Definition: net.h:182
#define NET_MAX_PROXY_NAME_LEN
Definition: net.h:137
SlaacContext * slaacContext
SLAAC context.
Definition: net.h:246
#define Dhcpv6ClientContext
error_t netSetExtIntDriver(NetInterface *interface, const ExtIntDriver *driver)
Set external interrupt line driver.
Definition: net.c:797
#define MdnsResponderContext
Event object.
error_t netSetVmanId(NetInterface *interface, uint16_t vmanId)
Specify VMAN identifier (802.1ad)
Definition: net.c:572
OsEvent netEvent
Definition: net.c:72
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:65
__start_packed struct @113 Eui64
EUI-64 identifier.
Error codes description.
bool_t phyEvent
A PHY event is pending.
Definition: net.h:196
error_t netInit(void)
TCP/IP stack initialization.
Definition: net.c:103
DHCP client (Dynamic Host Configuration Protocol)
DHCPv6 client (Dynamic Host Configuration Protocol for IPv6)
PHY driver.
Definition: nic.h:196
error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr)
Set Ethernet PHY address.
Definition: net.c:718
error_t netSetSwitchPort(NetInterface *interface, uint8_t port)
Specify switch port.
Definition: net.c:606
error_t netSetHostname(NetInterface *interface, const char_t *name)
Set host name.
Definition: net.c:481
Ethernet.
uint8_t port
Switch port identifier.
Definition: net.h:213
error_t netAttachLinkChangeCallback(NetInterface *interface, LinkChangeCallback callback, void *param, uint_t *cookie)
Register link change callback.
Definition: net.c:1584
DNS client (Domain Name System)
Task object.
OsMutex netMutex
Definition: net.c:70
NDP context.
Definition: ndp.h:572
char_t name[]
char_t proxyName[NET_MAX_PROXY_NAME_LEN+1]
Proxy server name.
Definition: net.h:185
error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr)
Set MAC address.
Definition: net.c:300
uint16_t vmanId
VMAN identifier (802.1ad)
Definition: net.h:210
char_t hostname[NET_MAX_HOSTNAME_LEN+1]
Host name.
Definition: net.h:184
#define SlaacContext
Definition: slaac.h:48
error_t netSetEui64(NetInterface *interface, const Eui64 *eui64)
Set EUI-64 interface identifier.
Definition: net.c:372
#define NET_INTERFACE_COUNT
Definition: net.h:108
SPI driver.
Definition: nic.h:212
#define ARP_CACHE_SIZE
Definition: arp.h:44
const ExtIntDriver * extIntDriver
External interrupt line driver.
Definition: net.h:192
IPv4 (Internet Protocol Version 4)
ArpCacheEntry arpCache[ARP_CACHE_SIZE]
ARP cache.
Definition: net.h:221
error_t netSetLinkState(NetInterface *interface, NicLinkState linkState)
Set link state (for virtual drivers only)
Definition: net.c:822
error_t netSetUartDriver(NetInterface *interface, const UartDriver *driver)
Set UART driver.
Definition: net.c:772
void netTask(void)
TCP/IP events handling.
Definition: net.c:1056
RTOS abstraction layer.
AutoIpContext * autoIpContext
Auto-IP context.
Definition: net.h:227
uint16_t proxyPort
Proxy server port.
Definition: net.h:186
PppContext * pppContext
DNS-SD context.
Definition: net.h:262
NIC driver.
Definition: nic.h:161
Auto-IP (Dynamic Configuration of IPv4 Link-Local Addresses)
RA service context.
const SpiDriver * spiDriver
Underlying SPI driver.
Definition: net.h:190
Ipv6Context ipv6Context
IPv6 context.
Definition: net.h:238
error_t netDetachLinkChangeCallback(uint_t cookie)
Unregister link change callback.
Definition: net.c:1633
bool_t linkState
Link state.
Definition: net.h:197
uint_t index
Zero-based index.
Definition: net.h:180
#define PppContext
Definition: ppp.h:36
IPv4 fragmentation and reassembly.
Byte order conversion.
uint8_t cookie[]
Definition: dtls_misc.h:194
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:1495
Dhcpv6ClientContext * dhcpv6ClientContext
DHCPv6 client context.
Definition: net.h:249
IPv6 (Internet Protocol Version 6)
void netTick(void)
Manage TCP/IP timers.
Definition: net.c:1185
error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr)
Retrieve MAC address.
Definition: net.c:335
error_t netSetDriver(NetInterface *interface, const NicDriver *driver)
Set Ethernet MAC driver.
Definition: net.c:668
DHCP server context.
Definition: dhcp_server.h:114
systime_t igmpv1RouterPresentTimer
IGMPv1 router present timer.
Definition: net.h:223
error_t
Error codes.
Definition: error.h:40
NdpRouterAdvContext * ndpRouterAdvContext
RA service context.
Definition: net.h:243
unsigned int uint_t
Definition: compiler_port.h:43
__start_packed struct @112 MacAddr
MAC address.
#define NetInterface
Definition: net.h:34
ARP cache entry.
Definition: arp.h:181
Memory management.
uint32_t id
A unique number identifying the interface.
Definition: net.h:181
const PhyDriver * phyDriver
PHY driver.
Definition: net.h:188
Mutex object.
error_t netGetEui64(NetInterface *interface, Eui64 *eui64)
Retrieve EUI-64 interface identifier.
Definition: net.c:397
DNS-SD (DNS-Based Service Discovery)
uint16_t port
Definition: dns_common.h:221
error_t netSetInterfaceName(NetInterface *interface, const char_t *name)
Set interface name.
Definition: net.c:454
NDP (Neighbor Discovery Protocol)
OsTask * netTaskHandle
Definition: net.c:68
Definitions common to mDNS client and mDNS responder.
const NicDriver * nicDriver
NIC driver.
Definition: net.h:187
error_t netInitRand(uint32_t seed)
Seed pseudo-random number generator.
Definition: net.c:1508
IPv4 context.
Definition: ipv4.h:317
IPv6 context.
Definition: ipv6.h:460
uint8_t phyAddr
PHY address.
Definition: net.h:189
OsEvent nicTxEvent
Network controller TX event.
Definition: net.h:194
error_t netConfigInterface(NetInterface *interface)
Configure network interface.
Definition: net.c:880
uint32_t linkSpeed
Link speed.
Definition: net.h:198
External interrupt line driver.
Definition: nic.h:240
NicDuplexMode duplexMode
Duplex mode.
Definition: net.h:199
NetInterface * parent
Interface on top of which the virtual interface runs.
Definition: net.h:216
error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver)
Set SPI driver.
Definition: net.c:747
bool_t netGetLinkState(NetInterface *interface)
Get link state.
Definition: net.c:854
error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver)
Set Ethernet PHY driver.
Definition: net.c:693
MdnsResponderContext * mdnsResponderContext
mDNS responder context
Definition: net.h:254
#define NET_MAX_HOSTNAME_LEN
Definition: net.h:130
Router advertisement service.
#define NET_MAX_IF_NAME_LEN
Definition: net.h:123
int bool_t
Definition: compiler_port.h:47
Network interface controller abstraction layer.
#define AutoIpContext
Definition: auto_ip.h:137
uint32_t netGetRand(void)
Get a random value.
Definition: net.c:1523
MacFilterEntry macAddrFilter[MAC_ADDR_FILTER_SIZE]
MAC filter table.
Definition: net.h:204
#define DnsSdContext
Definition: dns_sd.h:88
MAC filter table entry.
Definition: ethernet.h:208