net.c
Go to the documentation of this file.
1 /**
2  * @file net.c
3  * @brief TCP/IP stack core
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 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/socket.h"
37 #include "core/raw_socket.h"
38 #include "core/tcp_timer.h"
39 #include "core/tcp_misc.h"
40 #include "core/ethernet.h"
41 #include "ipv4/arp.h"
42 #include "ipv4/ipv4.h"
43 #include "ipv4/ipv4_routing.h"
44 #include "ipv4/auto_ip_misc.h"
45 #include "igmp/igmp_host.h"
46 #include "igmp/igmp_router.h"
47 #include "igmp/igmp_snooping.h"
48 #include "ipv6/ipv6.h"
49 #include "ipv6/ipv6_routing.h"
50 #include "ipv6/ndp.h"
51 #include "ipv6/ndp_router_adv.h"
52 #include "mld/mld_node.h"
53 #include "dhcp/dhcp_client_misc.h"
54 #include "dhcp/dhcp_server_misc.h"
56 #include "dns/dns_cache.h"
57 #include "dns/dns_client.h"
58 #include "mdns/mdns_client.h"
59 #include "mdns/mdns_responder.h"
60 #include "mdns/mdns_common.h"
62 #include "netbios/nbns_client.h"
63 #include "netbios/nbns_responder.h"
64 #include "netbios/nbns_common.h"
65 #include "llmnr/llmnr_responder.h"
66 #include "str.h"
67 #include "debug.h"
68 
69 #if (defined(WEB_SOCKET_SUPPORT) && WEB_SOCKET_SUPPORT == ENABLED)
70  #include "web_socket/web_socket.h"
71 #endif
72 
73 //TCP/IP stack context
75 
76 
77 /**
78  * @brief Initialize settings with default values
79  * @param[out] settings Structure that contains TCP/IP stack settings
80  **/
81 
83 {
84  //Default task parameters
85  settings->task = OS_TASK_DEFAULT_PARAMS;
87  settings->task.priority = NET_TASK_PRIORITY;
88 }
89 
90 
91 /**
92  * @brief Initialize TCP/IP stack (deprecated)
93  * @return Error code
94  **/
95 
97 {
98  error_t error;
99  NetSettings netSettings;
100 
101  //Get default settings
102  netGetDefaultSettings(&netSettings);
103 
104  //Initialize TCP/IP stack
105  error = netInitEx(&netContext, &netSettings);
106 
107  //Check status code
108  if(!error)
109  {
110  //Start TCP/IP stack
111  error = netStart(&netContext);
112  }
113 
114  //Return status code
115  return error;
116 }
117 
118 
119 /**
120  * @brief Initialize TCP/IP stack
121  * @param[in] context Pointer to the TCP/IP stack context
122  * @param[in] settings TCP/IP stack specific settings
123  * @return Error code
124  **/
125 
126 error_t netInitEx(NetContext *context, const NetSettings *settings)
127 {
128  error_t error;
129  uint_t i;
130  NetInterface *interface;
131 
132  //Clear TCP/IP stack context
133  osMemset(context, 0, sizeof(NetContext));
134 
135  //Initialize task parameters
136  context->taskParams = settings->task;
137  context->taskId = OS_INVALID_TASK_ID;
138 
139  //The TCP/IP process is currently suspended
141  //Get current time
143 
144  //Create a mutex to prevent simultaneous access to the TCP/IP stack
145  if(!osCreateMutex(&netMutex))
146  {
147  //Failed to create mutex
148  return ERROR_OUT_OF_RESOURCES;
149  }
150 
151  //Create a event object to receive notifications from device drivers
152  if(!osCreateEvent(&netEvent))
153  {
154  //Failed to create mutex
155  return ERROR_OUT_OF_RESOURCES;
156  }
157 
158  //Memory pool initialization
159  error = memPoolInit();
160  //Any error to report?
161  if(error)
162  return error;
163 
164  //Clear configuration data for each interface
165  osMemset(netInterface, 0, sizeof(netInterface));
166 
167  //Loop through network interfaces
168  for(i = 0; i < NET_INTERFACE_COUNT; i++)
169  {
170  //Point to the current interface
171  interface = &netInterface[i];
172 
173  //Default interface name
174  osSprintf(interface->name, "eth%u", i);
175 
176  //Zero-based index
177  interface->index = i;
178  //Unique number identifying the interface
179  interface->id = i;
180 
181 #if (ETH_SUPPORT == ENABLED)
182  //Default PHY address
183  interface->phyAddr = UINT8_MAX;
184 #endif
185 #if (TCP_SUPPORT == ENABLED)
186  //Default TCP initial retransmission timeout
187  interface->initialRto = TCP_INITIAL_RTO;
188 #endif
189  }
190 
191  //Socket related initialization
192  error = socketInit();
193  //Any error to report?
194  if(error)
195  return error;
196 
197 #if (defined(WEB_SOCKET_SUPPORT) && WEB_SOCKET_SUPPORT == ENABLED)
198  //WebSocket related initialization
199  webSocketInit();
200 #endif
201 
202 #if (IPV4_SUPPORT == ENABLED && IPV4_ROUTING_SUPPORT == ENABLED)
203  //Initialize IPv4 routing table
204  error = ipv4InitRouting();
205  //Any error to report?
206  if(error)
207  return error;
208 #endif
209 
210 #if (IPV6_SUPPORT == ENABLED && IPV6_ROUTING_SUPPORT == ENABLED)
211  //Initialize IPv6 routing table
212  error = ipv6InitRouting();
213  //Any error to report?
214  if(error)
215  return error;
216 #endif
217 
218 #if (UDP_SUPPORT == ENABLED)
219  //UDP related initialization
220  error = udpInit();
221  //Any error to report?
222  if(error)
223  return error;
224 #endif
225 
226 #if (TCP_SUPPORT == ENABLED)
227  //TCP related initialization
228  error = tcpInit();
229  //Any error to report?
230  if(error)
231  return error;
232 #endif
233 
234 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
235  NBNS_CLIENT_SUPPORT == ENABLED)
236  //DNS cache initialization
237  error = dnsInit();
238  //Any error to report?
239  if(error)
240  return error;
241 #endif
242 
243  //Initialize tick counters
244  nicTickCounter = 0;
245 
246 #if (PPP_SUPPORT == ENABLED)
247  pppTickCounter = 0;
248 #endif
249 #if (IPV4_SUPPORT == ENABLED && ETH_SUPPORT == ENABLED)
250  arpTickCounter = 0;
251 #endif
252 #if (IPV4_SUPPORT == ENABLED && IPV4_FRAG_SUPPORT == ENABLED)
254 #endif
255 #if (IPV4_SUPPORT == ENABLED && (IGMP_HOST_SUPPORT == ENABLED || \
256  IGMP_ROUTER_SUPPORT == ENABLED || IGMP_SNOOPING_SUPPORT == ENABLED))
257  igmpTickCounter = 0;
258 #endif
259 #if (IPV4_SUPPORT == ENABLED && AUTO_IP_SUPPORT == ENABLED)
260  autoIpTickCounter = 0;
261 #endif
262 #if (IPV4_SUPPORT == ENABLED && DHCP_CLIENT_SUPPORT == ENABLED)
264 #endif
265 #if (IPV4_SUPPORT == ENABLED && DHCP_SERVER_SUPPORT == ENABLED)
267 #endif
268 #if (IPV6_SUPPORT == ENABLED && IPV6_FRAG_SUPPORT == ENABLED)
270 #endif
271 #if (IPV6_SUPPORT == ENABLED && MLD_NODE_SUPPORT == ENABLED)
272  mldTickCounter = 0;
273 #endif
274 #if (IPV6_SUPPORT == ENABLED && NDP_SUPPORT == ENABLED)
275  ndpTickCounter = 0;
276 #endif
277 #if (IPV6_SUPPORT == ENABLED && NDP_ROUTER_ADV_SUPPORT == ENABLED)
279 #endif
280 #if (IPV6_SUPPORT == ENABLED && DHCPV6_CLIENT_SUPPORT == ENABLED)
282 #endif
283 #if (TCP_SUPPORT == ENABLED)
284  tcpTickCounter = 0;
285 #endif
286 #if (DNS_CLIENT_SUPPORT == ENABLED || MDNS_CLIENT_SUPPORT == ENABLED || \
287  NBNS_CLIENT_SUPPORT == ENABLED)
288  dnsTickCounter = 0;
289 #endif
290 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
292 #endif
293 #if (DNS_SD_RESPONDER_SUPPORT == ENABLED)
295 #endif
296 
297  //Successful initialization
298  return NO_ERROR;
299 }
300 
301 
302 /**
303  * @brief Start TCP/IP stack
304  * @param[in] context Pointer to the TCP/IP stack context
305  * @return Error code
306  **/
307 
309 {
310  //Create a task
311  context->taskId = osCreateTask("TCP/IP", (OsTaskCode) netTaskEx, context,
312  &context->taskParams);
313 
314  //Unable to create the task?
315  if(context->taskId == OS_INVALID_TASK_ID)
316  return ERROR_OUT_OF_RESOURCES;
317 
318 #if (NET_RTOS_SUPPORT == DISABLED)
319  //The TCP/IP process is now running
321 #endif
322 
323  //Successful processing
324  return NO_ERROR;
325 }
326 
327 
328 /**
329  * @brief Seed the pseudo-random number generator
330  * @param[in] seed Pointer to the random seed
331  * @param[in] length Length of the random seed, in bytes
332  * @return Error code
333  **/
334 
335 error_t netSeedRand(const uint8_t *seed, size_t length)
336 {
337  size_t i;
338  size_t j;
339 
340  //Check parameters
341  if(seed == NULL || length == 0)
343 
344  //Get exclusive access
345  if(netTaskRunning)
346  {
348  }
349 
350  //Save random seed
351  for(i = 0, j = 0; i < NET_RAND_SEED_SIZE; i++)
352  {
353  //Copy current byte
354  netContext.randSeed[i] = seed[j];
355 
356  //Increment index and wrap around if necessary
357  if(++j >= length)
358  {
359  j = 0;
360  }
361  }
362 
363  //Initialize pseudo-random generator
364  netInitRand();
365 
366  //Release exclusive access
367  if(netTaskRunning)
368  {
370  }
371 
372  //Successful processing
373  return NO_ERROR;
374 }
375 
376 
377 /**
378  * @brief Generate a random 32-bit value
379  * @return Random value
380  **/
381 
382 uint32_t netGetRand(void)
383 {
384  uint32_t value;
385 
386  //Get exclusive access
387  if(netTaskRunning)
388  {
390  }
391 
392  //Generate a random 32-bit value
394 
395  //Release exclusive access
396  if(netTaskRunning)
397  {
399  }
400 
401  //Return the random value
402  return value;
403 }
404 
405 
406 /**
407  * @brief Generate a random value in the specified range
408  * @param[in] min Lower bound
409  * @param[in] max Upper bound
410  * @return Random value in the specified range
411  **/
412 
413 uint32_t netGetRandRange(uint32_t min, uint32_t max)
414 {
415  uint32_t value;
416 
417  //Get exclusive access
418  if(netTaskRunning)
419  {
421  }
422 
423  //Generate a random value in the specified range
424  value = netGenerateRandRange(min, max);
425 
426  //Release exclusive access
427  if(netTaskRunning)
428  {
430  }
431 
432  //Return the random value
433  return value;
434 }
435 
436 
437 /**
438  * @brief Get a string of random data
439  * @param[out] data Buffer where to store random data
440  * @param[in] length Number of random bytes to generate
441  **/
442 
443 void netGetRandData(uint8_t *data, size_t length)
444 {
445  //Get exclusive access
446  if(netTaskRunning)
447  {
449  }
450 
451  //Generate a random value in the specified range
453 
454  //Release exclusive access
455  if(netTaskRunning)
456  {
458  }
459 }
460 
461 
462 /**
463  * @brief Get default network interface
464  * @return Pointer to the default network interface to be used
465  **/
466 
468 {
469  //Default network interface
470  return &netInterface[0];
471 }
472 
473 
474 /**
475  * @brief Set MAC address
476  * @param[in] interface Pointer to the desired network interface
477  * @param[in] macAddr MAC address
478  * @return Error code
479  **/
480 
481 error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr)
482 {
483 #if (ETH_SUPPORT == ENABLED)
484  //Check parameters
485  if(interface == NULL || macAddr == NULL)
487 
488  //Get exclusive access
490 
491  //Set MAC address
492  interface->macAddr = *macAddr;
493 
494  //Generate the 64-bit interface identifier
495  macAddrToEui64(macAddr, &interface->eui64);
496 
497  //Release exclusive access
499 
500  //Successful processing
501  return NO_ERROR;
502 #else
503  //Not implemented
504  return ERROR_NOT_IMPLEMENTED;
505 #endif
506 }
507 
508 
509 /**
510  * @brief Retrieve MAC address
511  * @param[in] interface Pointer to the desired network interface
512  * @param[out] macAddr MAC address
513  * @return Error code
514  **/
515 
517 {
518 #if (ETH_SUPPORT == ENABLED)
519  NetInterface *logicalInterface;
520 
521  //Check parameters
522  if(interface == NULL || macAddr == NULL)
524 
525  //Get exclusive access
527 
528  //Point to the logical interface
529  logicalInterface = nicGetLogicalInterface(interface);
530 
531  //Get MAC address
532  *macAddr = logicalInterface->macAddr;
533 
534  //Release exclusive access
536 
537  //Successful processing
538  return NO_ERROR;
539 #else
540  //Not implemented
541  return ERROR_NOT_IMPLEMENTED;
542 #endif
543 }
544 
545 
546 /**
547  * @brief Set EUI-64 interface identifier
548  * @param[in] interface Pointer to the desired network interface
549  * @param[in] eui64 Interface identifier
550  * @return Error code
551  **/
552 
553 error_t netSetEui64(NetInterface *interface, const Eui64 *eui64)
554 {
555  //Check parameters
556  if(interface == NULL || eui64 == NULL)
558 
559  //Get exclusive access
561  //Set interface identifier
562  interface->eui64 = *eui64;
563  //Release exclusive access
565 
566  //Successful processing
567  return NO_ERROR;
568 }
569 
570 
571 /**
572  * @brief Retrieve EUI-64 interface identifier
573  * @param[in] interface Pointer to the desired network interface
574  * @param[out] eui64 Interface identifier
575  * @return Error code
576  **/
577 
579 {
580  NetInterface *logicalInterface;
581 
582  //Check parameters
583  if(interface == NULL || eui64 == NULL)
585 
586  //Get exclusive access
588 
589  //Point to the logical interface
590  logicalInterface = nicGetLogicalInterface(interface);
591 
592  //Get interface identifier
593  *eui64 = logicalInterface->eui64;
594 
595  //Release exclusive access
597 
598  //Successful processing
599  return NO_ERROR;
600 }
601 
602 
603 /**
604  * @brief Set interface identifier
605  * @param[in] interface Pointer to the desired network interface
606  * @param[in] id Unique number identifying the interface
607  * @return Error code
608  **/
609 
610 error_t netSetInterfaceId(NetInterface *interface, uint32_t id)
611 {
612  //Check parameters
613  if(interface == NULL)
615 
616  //Get exclusive access
618  //Set interface identifier
619  interface->id = id;
620  //Release exclusive access
622 
623  //Successful processing
624  return NO_ERROR;
625 }
626 
627 
628 /**
629  * @brief Set interface name
630  * @param[in] interface Pointer to the desired network interface
631  * @param[in] name NULL-terminated string that contains the interface name
632  * @return Error code
633  **/
634 
636 {
637  //Check parameters
638  if(interface == NULL || name == NULL)
640 
641  //Make sure the length of the interface name is acceptable
643  return ERROR_INVALID_LENGTH;
644 
645  //Get exclusive access
647  //Set interface name
648  osStrcpy(interface->name, name);
649  //Release exclusive access
651 
652  //Successful processing
653  return NO_ERROR;
654 }
655 
656 
657 /**
658  * @brief Set host name
659  * @param[in] interface Pointer to the desired network interface
660  * @param[in] name NULL-terminated string that contains the host name
661  * @return Error code
662  **/
663 
665 {
666  //Check parameters
667  if(interface == NULL || name == NULL)
669 
670  //Make sure the length of the host name is acceptable
672  return ERROR_INVALID_LENGTH;
673 
674  //Get exclusive access
676  //Set host name
677  osStrcpy(interface->hostname, name);
678  //Release exclusive access
680 
681  //Successful processing
682  return NO_ERROR;
683 }
684 
685 
686 /**
687  * @brief Specify VLAN identifier (802.1Q)
688  * @param[in] interface Pointer to the desired network interface
689  * @param[in] vlanId VLAN identifier
690  * @return Error code
691  **/
692 
693 error_t netSetVlanId(NetInterface *interface, uint16_t vlanId)
694 {
695 #if (ETH_VLAN_SUPPORT == ENABLED)
696  //Make sure the network interface is valid
697  if(interface == NULL)
699 
700  //The VID value FFF is reserved
701  if((vlanId & VLAN_VID_MASK) == VLAN_VID_MASK)
703 
704  //Get exclusive access
706  //Set VLAN identifier
707  interface->vlanId = vlanId;
708  //Release exclusive access
710 
711  //Successful processing
712  return NO_ERROR;
713 #else
714  //Not implemented
715  return ERROR_NOT_IMPLEMENTED;
716 #endif
717 }
718 
719 
720 /**
721  * @brief Specify VMAN identifier (802.1ad)
722  * @param[in] interface Pointer to the desired network interface
723  * @param[in] vmanId VMAN identifier
724  * @return Error code
725  **/
726 
727 error_t netSetVmanId(NetInterface *interface, uint16_t vmanId)
728 {
729 #if (ETH_VMAN_SUPPORT == ENABLED)
730  //Make sure the network interface is valid
731  if(interface == NULL)
733 
734  //The VID value FFF is reserved
735  if((vmanId & VLAN_VID_MASK) == VLAN_VID_MASK)
737 
738  //Get exclusive access
740  //Set VMAN identifier
741  interface->vmanId = vmanId;
742  //Release exclusive access
744 
745  //Successful processing
746  return NO_ERROR;
747 #else
748  //Not implemented
749  return ERROR_NOT_IMPLEMENTED;
750 #endif
751 }
752 
753 
754 /**
755  * @brief Attach a virtual interface to a given physical interface
756  * @param[in] interface Pointer to the virtual interface
757  * @param[in] physicalInterface physical interface on top of which the virtual
758  * interface will run
759  * @return Error code
760  **/
761 
763  NetInterface *physicalInterface)
764 {
765 #if (ETH_VIRTUAL_IF_SUPPORT == ENABLED || ETH_VLAN_SUPPORT == ENABLED || \
766  ETH_PORT_TAGGING_SUPPORT == ENABLED)
767  //Make sure the network interface is valid
768  if(interface == NULL)
770 
771  //Get exclusive access
773  //Bind the virtual interface to the physical interface
774  interface->parent = physicalInterface;
775  //Release exclusive access
777 
778  //Successful processing
779  return NO_ERROR;
780 #else
781  //Not implemented
782  return ERROR_NOT_IMPLEMENTED;
783 #endif
784 }
785 
786 
787 /**
788  * @brief Set Ethernet MAC driver
789  * @param[in] interface Pointer to the desired network interface
790  * @param[in] driver Ethernet MAC driver
791  * @return Error code
792  **/
793 
794 error_t netSetDriver(NetInterface *interface, const NicDriver *driver)
795 {
796  //Check parameters
797  if(interface == NULL || driver == NULL)
799 
800  //Get exclusive access
802  //Set Ethernet MAC driver
803  interface->nicDriver = driver;
804  //Release exclusive access
806 
807  //Successful processing
808  return NO_ERROR;
809 }
810 
811 
812 /**
813  * @brief Set Ethernet PHY driver
814  * @param[in] interface Pointer to the desired network interface
815  * @param[in] driver Ethernet PHY driver (can be NULL for MAC + PHY controller)
816  * @return Error code
817  **/
818 
819 error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver)
820 {
821 #if (ETH_SUPPORT == ENABLED)
822  //Check parameters
823  if(interface == NULL || driver == NULL)
825 
826  //Get exclusive access
828  //Set Ethernet PHY driver
829  interface->phyDriver = driver;
830  //Release exclusive access
832 
833  //Successful processing
834  return NO_ERROR;
835 #else
836  //Not implemented
837  return ERROR_NOT_IMPLEMENTED;
838 #endif
839 }
840 
841 
842 /**
843  * @brief Specify Ethernet PHY address
844  * @param[in] interface Pointer to the desired network interface
845  * @param[in] phyAddr PHY address
846  * @return Error code
847  **/
848 
849 error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr)
850 {
851 #if (ETH_SUPPORT == ENABLED)
852  //Make sure the network interface is valid
853  if(interface == NULL)
855 
856  //Make sure the PHY address is valid
857  if(phyAddr >= 32)
858  return ERROR_OUT_OF_RANGE;
859 
860  //Get exclusive access
862  //Set PHY address
863  interface->phyAddr = phyAddr;
864  //Release exclusive access
866 
867  //Successful processing
868  return NO_ERROR;
869 #else
870  //Not implemented
871  return ERROR_NOT_IMPLEMENTED;
872 #endif
873 }
874 
875 
876 /**
877  * @brief Set Ethernet switch driver
878  * @param[in] interface Pointer to the desired network interface
879  * @param[in] driver Ethernet switch driver
880  * @return Error code
881  **/
882 
884 {
885 #if (ETH_SUPPORT == ENABLED)
886  //Check parameters
887  if(interface == NULL || driver == NULL)
889 
890  //Get exclusive access
892  //Set Ethernet switch driver
893  interface->switchDriver = driver;
894  //Release exclusive access
896 
897  //Successful processing
898  return NO_ERROR;
899 #else
900  //Not implemented
901  return ERROR_NOT_IMPLEMENTED;
902 #endif
903 }
904 
905 
906 /**
907  * @brief Specify switch port
908  * @param[in] interface Pointer to the desired network interface
909  * @param[in] port Switch port identifier
910  * @return Error code
911  **/
912 
914 {
915 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
916  //Make sure the network interface is valid
917  if(interface == NULL)
919 
920  //Get exclusive access
922  //Set switch port identifier
923  interface->port = port;
924  //Release exclusive access
926 
927  //Successful processing
928  return NO_ERROR;
929 #else
930  //Not implemented
931  return ERROR_NOT_IMPLEMENTED;
932 #endif
933 }
934 
935 
936 /**
937  * @brief Set SMI driver
938  * @param[in] interface Pointer to the desired network interface
939  * @param[in] driver Underlying SMI driver
940  * @return Error code
941  **/
942 
943 error_t netSetSmiDriver(NetInterface *interface, const SmiDriver *driver)
944 {
945 #if (ETH_SUPPORT == ENABLED)
946  //Check parameters
947  if(interface == NULL || driver == NULL)
949 
950  //Get exclusive access
952  //Set SMI driver
953  interface->smiDriver = driver;
954  //Release exclusive access
956 
957  //Successful processing
958  return NO_ERROR;
959 #else
960  //Not implemented
961  return ERROR_NOT_IMPLEMENTED;
962 #endif
963 }
964 
965 
966 /**
967  * @brief Set SPI driver
968  * @param[in] interface Pointer to the desired network interface
969  * @param[in] driver Underlying SPI driver
970  * @return Error code
971  **/
972 
973 error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver)
974 {
975  //Check parameters
976  if(interface == NULL || driver == NULL)
978 
979  //Get exclusive access
981  //Set SPI driver
982  interface->spiDriver = driver;
983  //Release exclusive access
985 
986  //Successful processing
987  return NO_ERROR;
988 }
989 
990 
991 /**
992  * @brief Set UART driver
993  * @param[in] interface Pointer to the desired network interface
994  * @param[in] driver Underlying UART driver
995  * @return Error code
996  **/
997 
999 {
1000  //Check parameters
1001  if(interface == NULL || driver == NULL)
1002  return ERROR_INVALID_PARAMETER;
1003 
1004  //Get exclusive access
1006  //Set UART driver
1007  interface->uartDriver = driver;
1008  //Release exclusive access
1010 
1011  //Successful processing
1012  return NO_ERROR;
1013 }
1014 
1015 
1016 /**
1017  * @brief Set external interrupt line driver
1018  * @param[in] interface Pointer to the desired network interface
1019  * @param[in] driver Underlying SPI driver
1020  * @return Error code
1021  **/
1022 
1024 {
1025  //Check parameters
1026  if(interface == NULL || driver == NULL)
1027  return ERROR_INVALID_PARAMETER;
1028 
1029  //Get exclusive access
1031  //Set external interrupt line driver
1032  interface->extIntDriver = driver;
1033  //Release exclusive access
1035 
1036  //Successful processing
1037  return NO_ERROR;
1038 }
1039 
1040 
1041 /**
1042  * @brief Set administrative link state
1043  * @param[in] interface Pointer to the desired network interface
1044  * @param[in] linkState Administrative link state (up or down)
1045  * @return Error code
1046  **/
1047 
1049 {
1050  //Make sure the network interface is valid
1051  if(interface == NULL)
1052  return ERROR_INVALID_PARAMETER;
1053 
1054  //Get exclusive access
1056 
1057  //Any change detected?
1058  if(linkState != interface->linkState)
1059  {
1060  //Update link state
1061  interface->linkState = linkState;
1062  //Process link state change event
1063  netProcessLinkChange(interface);
1064  }
1065 
1066  //Release exclusive access
1068 
1069  //Successful processing
1070  return NO_ERROR;
1071 }
1072 
1073 
1074 /**
1075  * @brief Get link state
1076  * @param[in] interface Pointer to the desired network interface
1077  * @return Link state
1078  **/
1079 
1081 {
1082  bool_t linkState;
1083 
1084  //Make sure the network interface is valid
1085  if(interface != NULL)
1086  {
1087  //Get exclusive access
1089  //Retrieve link state
1090  linkState = interface->linkState;
1091  //Release exclusive access
1093  }
1094  else
1095  {
1096  //Unknown link state
1097  linkState = FALSE;
1098  }
1099 
1100  //Return link state
1101  return linkState;
1102 }
1103 
1104 
1105 /**
1106  * @brief Get link speed
1107  * @param[in] interface Pointer to the desired network interface
1108  * @return Link speed
1109  **/
1110 
1112 {
1113  uint_t linkSpeed;
1114 
1115  //Make sure the network interface is valid
1116  if(interface != NULL)
1117  {
1118  //Get exclusive access
1120  //Retrieve link speed
1121  linkSpeed = interface->linkSpeed;
1122  //Release exclusive access
1124  }
1125  else
1126  {
1127  //Unknown link speed
1128  linkSpeed = NIC_LINK_SPEED_UNKNOWN;
1129  }
1130 
1131  //Return link speed
1132  return linkSpeed;
1133 }
1134 
1135 
1136 /**
1137  * @brief Get duplex mode
1138  * @param[in] interface Pointer to the desired network interface
1139  * @return Duplex mode
1140  **/
1141 
1143 {
1144  NicDuplexMode duplexMode;
1145 
1146  //Make sure the network interface is valid
1147  if(interface != NULL)
1148  {
1149  //Get exclusive access
1151  //Retrieve duplex mode
1152  duplexMode = interface->duplexMode;
1153  //Release exclusive access
1155  }
1156  else
1157  {
1158  //Unknown duplex mode
1159  duplexMode = NIC_UNKNOWN_DUPLEX_MODE;
1160  }
1161 
1162  //Return duplex mode
1163  return duplexMode;
1164 }
1165 
1166 
1167 /**
1168  * @brief Enable promiscuous mode
1169  * @param[in] interface Pointer to the desired network interface
1170  * @param[in] enable Enable or disable promiscuous mode
1171  * @return Error code
1172  **/
1173 
1175 {
1176  //Make sure the network interface is valid
1177  if(interface == NULL)
1178  return ERROR_INVALID_PARAMETER;
1179 
1180 #if (ETH_SUPPORT == ENABLED)
1181  //Get exclusive access
1183  //Enable or disable promiscuous mode
1184  interface->promiscuous = enable;
1185  //Release exclusive access
1187 #endif
1188 
1189  //Successful processing
1190  return NO_ERROR;
1191 }
1192 
1193 
1194 /**
1195  * @brief Configure network interface
1196  * @param[in] interface Network interface to configure
1197  * @return Error code
1198  **/
1199 
1201 {
1202  error_t error;
1203 
1204  //Make sure the network interface is valid
1205  if(interface == NULL)
1206  return ERROR_INVALID_PARAMETER;
1207 
1208  //Get exclusive access
1210 
1211  //Disable hardware interrupts
1212  if(interface->nicDriver != NULL)
1213  interface->nicDriver->disableIrq(interface);
1214 
1215  //Start of exception handling block
1216  do
1217  {
1218  //Receive notifications when the transmitter is ready to send
1219  if(!osCreateEvent(&interface->nicTxEvent))
1220  {
1221  //Failed to create event object
1222  error = ERROR_OUT_OF_RESOURCES;
1223  //Stop immediately
1224  break;
1225  }
1226 
1227  //Valid NIC driver?
1228  if(interface->nicDriver != NULL)
1229  {
1230  //Network controller initialization
1231  error = interface->nicDriver->init(interface);
1232  //Any error to report?
1233  if(error)
1234  break;
1235  }
1236  else
1237  {
1238 #if (ETH_VIRTUAL_IF_SUPPORT == ENABLED || ETH_PORT_TAGGING_SUPPORT == ENABLED)
1239  NetInterface *physicalInterface;
1240 
1241  //Point to the physical interface
1242  physicalInterface = nicGetPhysicalInterface(interface);
1243 
1244  //Check whether the network interface is a virtual interface
1245  if(physicalInterface != interface)
1246  {
1247  //Valid MAC address assigned to the virtual interface?
1248  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1249  {
1250  //Configure the physical interface to accept the MAC address of
1251  //the virtual interface
1252  error = ethAcceptMacAddr(physicalInterface, &interface->macAddr);
1253  //Any error to report?
1254  if(error)
1255  break;
1256  }
1257  }
1258 #endif
1259  }
1260 
1261 #if (ETH_SUPPORT == ENABLED)
1262  //Ethernet related initialization
1263  error = ethInit(interface);
1264  //Any error to report?
1265  if(error)
1266  break;
1267 #endif
1268 
1269 #if (IPV4_SUPPORT == ENABLED)
1270  //IPv4 initialization
1271  error = ipv4Init(interface);
1272  //Any error to report?
1273  if(error)
1274  break;
1275 
1276 #if (ETH_SUPPORT == ENABLED)
1277  //ARP cache initialization
1278  error = arpInit(interface);
1279  //Any error to report?
1280  if(error)
1281  break;
1282 #endif
1283 
1284 #if (IPV4_SUPPORT == ENABLED && (IGMP_HOST_SUPPORT == ENABLED || \
1285  IGMP_ROUTER_SUPPORT == ENABLED || IGMP_SNOOPING_SUPPORT == ENABLED))
1286  //IGMP related initialization
1287  error = igmpInit(interface);
1288  //Any error to report?
1289  if(error)
1290  break;
1291 #endif
1292 
1293 #if (NBNS_CLIENT_SUPPORT == ENABLED || NBNS_RESPONDER_SUPPORT == ENABLED)
1294  //NetBIOS Name Service related initialization
1295  error = nbnsInit(interface);
1296  //Any error to report?
1297  if(error)
1298  break;
1299 #endif
1300 #endif
1301 
1302 #if (IPV6_SUPPORT == ENABLED)
1303  //IPv6 initialization
1304  error = ipv6Init(interface);
1305  //Any error to report?
1306  if(error)
1307  break;
1308 
1309 #if (IPV6_SUPPORT == ENABLED && NDP_SUPPORT == ENABLED)
1310  //NDP related initialization
1311  error = ndpInit(interface);
1312  //Any error to report?
1313  if(error)
1314  break;
1315 #endif
1316 
1317 #if (IPV6_SUPPORT == ENABLED && MLD_NODE_SUPPORT == ENABLED)
1318  //MLD related initialization
1319  error = mldInit(interface);
1320  //Any error to report?
1321  if(error)
1322  break;
1323 #endif
1324 #endif
1325 
1326 #if (MDNS_CLIENT_SUPPORT == ENABLED || MDNS_RESPONDER_SUPPORT == ENABLED)
1327  //mDNS related initialization
1328  error = mdnsInit(interface);
1329  //Any error to report?
1330  if(error)
1331  break;
1332 #endif
1333 
1334 #if (LLMNR_RESPONDER_SUPPORT == ENABLED)
1335  //LLMNR responder initialization
1336  error = llmnrResponderInit(interface);
1337  //Any error to report?
1338  if(error)
1339  break;
1340 #endif
1341 
1342  //End of exception handling block
1343  } while(0);
1344 
1345  //Check status code
1346  if(!error)
1347  {
1348  //Initialize pseudo-random generator
1349  netInitRand();
1350 
1351  //The network interface is now fully configured
1352  interface->configured = TRUE;
1353 
1354  //Check whether the TCP/IP process is running
1355  if(netTaskRunning)
1356  {
1357  //Interrupts can be safely enabled
1358  if(interface->nicDriver != NULL)
1359  interface->nicDriver->enableIrq(interface);
1360  }
1361  }
1362  else
1363  {
1364  //Clean up side effects before returning
1365  osDeleteEvent(&interface->nicTxEvent);
1366  }
1367 
1368  //Release exclusive access
1370 
1371  //Return status code
1372  return error;
1373 }
1374 
1375 
1376 /**
1377  * @brief Start network interface
1378  * @param[in] interface Network interface to start
1379  * @return Error code
1380  **/
1381 
1383 {
1384  error_t error;
1385 
1386  //Make sure the network interface is valid
1387  if(interface == NULL)
1388  return ERROR_INVALID_PARAMETER;
1389 
1390  //Initialize status code
1391  error = NO_ERROR;
1392 
1393  //Get exclusive access
1395 
1396 #if (ETH_SUPPORT == ENABLED)
1397  //Check whether the interface is enabled for operation
1398  if(!interface->configured)
1399  {
1400  NetInterface *physicalInterface;
1401 
1402  //Point to the physical interface
1403  physicalInterface = nicGetPhysicalInterface(interface);
1404 
1405  //Virtual interface?
1406  if(interface != physicalInterface)
1407  {
1408  //Valid MAC address assigned to the virtual interface?
1409  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1410  {
1411  //Configure the physical interface to accept the MAC address of
1412  //the virtual interface
1413  error = ethAcceptMacAddr(physicalInterface, &interface->macAddr);
1414  }
1415  }
1416  else
1417  {
1418 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
1419  //Valid switch driver?
1420  if(interface->switchDriver != NULL &&
1421  interface->switchDriver->init != NULL)
1422  {
1423  //Reconfigure switch operation
1424  error = interface->switchDriver->init(interface);
1425  }
1426 #endif
1427  //Check status code
1428  if(!error)
1429  {
1430  //Update the MAC filter
1431  error = nicUpdateMacAddrFilter(interface);
1432  }
1433  }
1434  }
1435 #endif
1436 
1437  //Enable network interface
1438  interface->configured = TRUE;
1439 
1440  //Check whether the TCP/IP process is running
1441  if(netTaskRunning)
1442  {
1443  //Interrupts can be safely enabled
1444  if(interface->nicDriver != NULL)
1445  interface->nicDriver->enableIrq(interface);
1446  }
1447 
1448  //Release exclusive access
1450 
1451  //Return status code
1452  return error;
1453 }
1454 
1455 
1456 /**
1457  * @brief Stop network interface
1458  * @param[in] interface Network interface to stop
1459  * @return Error code
1460  **/
1461 
1463 {
1464  NetInterface *physicalInterface;
1465 
1466  //Make sure the network interface is valid
1467  if(interface == NULL)
1468  return ERROR_INVALID_PARAMETER;
1469 
1470  //Get exclusive access
1472 
1473  //Point to the physical interface
1474  physicalInterface = nicGetPhysicalInterface(interface);
1475 
1476  //Check whether the interface is enabled for operation
1477  if(interface->configured)
1478  {
1479  //Update link state
1480  interface->linkState = FALSE;
1481  //Process link state change event
1482  netProcessLinkChange(interface);
1483 
1484  //Disable hardware interrupts
1485  if(interface->nicDriver != NULL)
1486  interface->nicDriver->disableIrq(interface);
1487 
1488  //Disable network interface
1489  interface->configured = FALSE;
1490 
1491  //Virtual interface?
1492  if(interface != physicalInterface)
1493  {
1494 #if (ETH_SUPPORT == ENABLED)
1495  //Valid MAC address assigned to the virtual interface?
1496  if(!macCompAddr(&interface->macAddr, &MAC_UNSPECIFIED_ADDR))
1497  {
1498  //Drop the corresponding address from the MAC filter table of
1499  //the physical interface
1500  ethDropMacAddr(physicalInterface, &interface->macAddr);
1501  }
1502 #endif
1503  }
1504  }
1505 
1506  //Release exclusive access
1508 
1509  //Successful operation
1510  return NO_ERROR;
1511 }
1512 
1513 
1514 /**
1515  * @brief TCP/IP events handling (deprecated)
1516  **/
1517 
1518 void netTask(void)
1519 {
1521 }
1522 
1523 
1524 /**
1525  * @brief TCP/IP events handling
1526  * @param[in] context Pointer to the TCP/IP stack context
1527  **/
1528 
1529 void netTaskEx(NetContext *context)
1530 {
1531  uint_t i;
1532  bool_t status;
1533  systime_t time;
1534  systime_t timeout;
1535  NetInterface *interface;
1536 
1537 #if (NET_RTOS_SUPPORT == ENABLED)
1538  //Task prologue
1539  osEnterTask();
1540 
1541  //Get exclusive access
1543 
1544  //The TCP/IP process is now running
1545  netTaskRunning = TRUE;
1546 
1547  //Loop through network interfaces
1548  for(i = 0; i < NET_INTERFACE_COUNT; i++)
1549  {
1550  //Point to the current network interface
1551  interface = &netInterface[i];
1552 
1553  //Check whether the interface is fully configured
1554  if(interface->configured)
1555  {
1556  //Interrupts can be safely enabled
1557  if(interface->nicDriver != NULL)
1558  {
1559  interface->nicDriver->enableIrq(interface);
1560  }
1561  }
1562  }
1563 
1564  //Release exclusive access
1566 
1567  //Main loop
1568  while(1)
1569  {
1570 #endif
1571  //Get current time
1572  time = osGetSystemTime();
1573 
1574  //Compute the maximum blocking time when waiting for an event
1575  if(timeCompare(time, netTimestamp) < 0)
1576  {
1577  timeout = netTimestamp - time;
1578  }
1579  else
1580  {
1581  timeout = 0;
1582  }
1583 
1584  //Receive notifications when a frame has been received, or the
1585  //link state of any network interfaces has changed
1586  status = osWaitForEvent(&netEvent, timeout);
1587 
1588  //Check whether the specified event is in signaled state
1589  if(status)
1590  {
1591  //Get exclusive access
1593 
1594  //Process events
1595  for(i = 0; i < NET_INTERFACE_COUNT; i++)
1596  {
1597  //Point to the current network interface
1598  interface = &netInterface[i];
1599 
1600  //Check whether a NIC event is pending
1601  if(interface->nicEvent)
1602  {
1603  //Acknowledge the event by clearing the flag
1604  interface->nicEvent = FALSE;
1605 
1606  //Valid NIC driver?
1607  if(interface->nicDriver != NULL)
1608  {
1609  //Disable hardware interrupts
1610  interface->nicDriver->disableIrq(interface);
1611  //Handle NIC events
1612  interface->nicDriver->eventHandler(interface);
1613  //Re-enable hardware interrupts
1614  interface->nicDriver->enableIrq(interface);
1615  }
1616  }
1617 
1618 #if (ETH_SUPPORT == ENABLED)
1619  //Check whether a PHY event is pending
1620  if(interface->phyEvent)
1621  {
1622  //Acknowledge the event by clearing the flag
1623  interface->phyEvent = FALSE;
1624 
1625  //Valid NIC driver?
1626  if(interface->nicDriver != NULL)
1627  {
1628  //Disable hardware interrupts
1629  interface->nicDriver->disableIrq(interface);
1630 
1631  //Valid Ethernet PHY or switch driver?
1632  if(interface->phyDriver != NULL)
1633  {
1634  //Handle events
1635  interface->phyDriver->eventHandler(interface);
1636  }
1637  else if(interface->switchDriver != NULL)
1638  {
1639  //Handle events
1640  interface->switchDriver->eventHandler(interface);
1641  }
1642  else
1643  {
1644  //The interface is not properly configured
1645  }
1646 
1647  //Re-enable hardware interrupts
1648  interface->nicDriver->enableIrq(interface);
1649  }
1650  }
1651 #endif
1652  }
1653 
1654  //Release exclusive access
1656  }
1657 
1658  //Get current time
1659  time = osGetSystemTime();
1660 
1661  //Check current time
1662  if(timeCompare(time, netTimestamp) >= 0)
1663  {
1664  //Get exclusive access
1666  //Handle periodic operations
1667  netTick();
1668  //Release exclusive access
1670 
1671  //Next event
1673  }
1674 #if (NET_RTOS_SUPPORT == ENABLED)
1675  }
1676 #endif
1677 }
error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr)
Add a unicast/multicast address to the MAC filter table.
Definition: ethernet.c:594
IPv6 (Internet Protocol Version 6)
error_t netInit(void)
Initialize TCP/IP stack (deprecated)
Definition: net.c:96
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
systime_t ipv4FragTickCounter
Definition: ipv4_frag.c:57
#define netTimestamp
Definition: net_legacy.h:198
String manipulation helper functions.
int bool_t
Definition: compiler_port.h:53
@ ERROR_OUT_OF_RANGE
Definition: error.h:137
@ NIC_LINK_SPEED_UNKNOWN
Definition: nic.h:110
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
error_t netSetSpiDriver(NetInterface *interface, const SpiDriver *driver)
Set SPI driver.
Definition: net.c:973
#define netEvent
Definition: net_legacy.h:196
uint32_t netGetRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net.c:413
error_t netSetDriver(NetInterface *interface, const NicDriver *driver)
Set Ethernet MAC driver.
Definition: net.c:794
error_t ipv4InitRouting(void)
#define netMutex
Definition: net_legacy.h:195
Eui64
Definition: ethernet.h:210
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:946
error_t netSetUartDriver(NetInterface *interface, const UartDriver *driver)
Set UART driver.
Definition: net.c:998
systime_t arpTickCounter
Definition: arp.c:51
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
error_t netSeedRand(const uint8_t *seed, size_t length)
Seed the pseudo-random number generator.
Definition: net.c:335
WebSocket API (client and server)
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
void netTaskEx(NetContext *context)
TCP/IP events handling.
Definition: net.c:1529
UART driver.
Definition: nic.h:385
uint8_t data[]
Definition: ethernet.h:222
#define OS_INVALID_TASK_ID
External interrupt line driver.
Definition: nic.h:398
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
SMI driver.
Definition: nic.h:357
systime_t mldTickCounter
Definition: mld_common.c:63
systime_t dnsSdResponderTickCounter
#define NET_INTERFACE_COUNT
Definition: net.h:114
systime_t nicTickCounter
Definition: nic.c:44
IPv6 routing.
IGMP snooping switch.
char_t name[]
error_t arpInit(NetInterface *interface)
ARP cache initialization.
Definition: arp.c:60
error_t netSetSwitchDriver(NetInterface *interface, const SwitchDriver *driver)
Set Ethernet switch driver.
Definition: net.c:883
NetContext netContext
Definition: net.c:74
error_t netSetMacAddr(NetInterface *interface, const MacAddr *macAddr)
Set MAC address.
Definition: net.c:481
#define NET_TICK_INTERVAL
Definition: net.h:175
SPI driver.
Definition: nic.h:369
#define osStrlen(s)
Definition: os_port.h:165
error_t netSetParentInterface(NetInterface *interface, NetInterface *physicalInterface)
Attach a virtual interface to a given physical interface.
Definition: net.c:762
error_t ipv6Init(NetInterface *interface)
IPv6 related initialization.
Definition: ipv6.c:96
Helper functions for DHCPv6 client.
#define VLAN_VID_MASK
Definition: ethernet.h:124
#define NET_TASK_STACK_SIZE
Definition: net.h:163
#define timeCompare(t1, t2)
Definition: os_port.h:40
Helper functions for DHCP client.
systime_t ipv6FragTickCounter
Definition: ipv6_frag.c:47
uint_t netGetLinkSpeed(NetInterface *interface)
Get link speed.
Definition: net.c:1111
Helper functions for DHCP server.
Ethernet.
error_t webSocketInit(void)
WebSocket related initialization.
Definition: web_socket.c:60
error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr)
Remove a unicast/multicast address from the MAC filter table.
Definition: ethernet.c:666
Router advertisement service.
systime_t pppTickCounter
Definition: ppp.c:53
Definitions common to mDNS client and mDNS responder.
IGMP router.
uint32_t netGenerateRand(void)
Generate a random 32-bit value.
Definition: net_misc.c:922
systime_t igmpTickCounter
Definition: igmp_common.c:63
OsTaskParameters taskParams
Task parameters.
Definition: net.h:323
systime_t ndpTickCounter
Definition: ndp.c:59
#define FALSE
Definition: os_port.h:46
systime_t dhcpServerTickCounter
Helper functions for TCP.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:85
uint32_t netGenerateRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:948
error_t
Error codes.
Definition: error.h:43
#define netInterface
Definition: net_legacy.h:199
#define osSprintf(dest,...)
Definition: os_port.h:231
void(* OsTaskCode)(void *arg)
Task routine.
Definitions common to NBNS client and NBNS responder.
DNS-SD responder (DNS-Based Service Discovery)
error_t netSetPhyAddr(NetInterface *interface, uint8_t phyAddr)
Specify Ethernet PHY address.
Definition: net.c:849
error_t mdnsInit(NetInterface *interface)
mDNS related initialization
Definition: mdns_common.c:69
NBNS client (NetBIOS Name Service)
error_t netSetEui64(NetInterface *interface, const Eui64 *eui64)
Set EUI-64 interface identifier.
Definition: net.c:553
error_t netConfigInterface(NetInterface *interface)
Configure network interface.
Definition: net.c:1200
error_t udpInit(void)
UDP related initialization.
Definition: udp.c:63
systime_t dhcpv6ClientTickCounter
void osDeleteEvent(OsEvent *event)
Delete an event object.
#define NetInterface
Definition: net.h:36
error_t memPoolInit(void)
Memory pool initialization.
Definition: net_mem.c:70
void netGetDefaultSettings(NetSettings *settings)
Initialize settings with default values.
Definition: net.c:82
@ ERROR_INVALID_LENGTH
Definition: error.h:111
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:467
OsTaskId taskId
Task identifier.
Definition: net.h:324
error_t netSetSmiDriver(NetInterface *interface, const SmiDriver *driver)
Set SMI driver.
Definition: net.c:943
error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr)
Retrieve MAC address.
Definition: net.c:516
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
TCP/IP stack context.
Definition: net.h:319
error_t netSetPhyDriver(NetInterface *interface, const PhyDriver *driver)
Set Ethernet PHY driver.
Definition: net.c:819
void netGenerateRandData(uint8_t *data, size_t length)
Get a string of random data.
Definition: net_misc.c:975
mDNS client (Multicast DNS)
error_t netInitEx(NetContext *context, const NetSettings *settings)
Initialize TCP/IP stack.
Definition: net.c:126
error_t netSetLinkState(NetInterface *interface, bool_t linkState)
Set administrative link state.
Definition: net.c:1048
IGMP host.
uint8_t length
Definition: tcp.h:368
void netProcessLinkChange(NetInterface *interface)
Process link state change event.
Definition: net_misc.c:201
#define osEnterTask()
error_t igmpInit(NetInterface *interface)
IGMP initialization.
Definition: igmp_common.c:72
uint32_t netGetRand(void)
Generate a random 32-bit value.
Definition: net.c:382
MacAddr
Definition: ethernet.h:195
NDP (Neighbor Discovery Protocol)
error_t nicUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Definition: nic.c:353
error_t netEnablePromiscuousMode(NetInterface *interface, bool_t enable)
Enable promiscuous mode.
Definition: net.c:1174
DNS client (Domain Name System)
TCP/IP raw sockets.
systime_t tcpTickCounter
Definition: tcp.c:49
uint32_t systime_t
System time.
uint16_t port
Definition: dns_common.h:267
MLD node (Multicast Listener Discovery for IPv6)
char char_t
Definition: compiler_port.h:48
DNS cache management.
uint32_t time
error_t netSetInterfaceId(NetInterface *interface, uint32_t id)
Set interface identifier.
Definition: net.c:610
error_t ndpInit(NetInterface *interface)
Neighbor cache initialization.
Definition: ndp.c:68
Ethernet switch driver.
Definition: nic.h:325
error_t tcpInit(void)
TCP related initialization.
Definition: tcp.c:60
void netInitRand(void)
Initialize random number generator.
Definition: net_misc.c:866
#define NET_MAX_IF_NAME_LEN
Definition: net.h:142
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
void netTick(void)
Manage TCP/IP timers.
Definition: net_misc.c:416
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
error_t netSetVmanId(NetInterface *interface, uint16_t vmanId)
Specify VMAN identifier (802.1ad)
Definition: net.c:727
NicDuplexMode
Duplex mode.
Definition: nic.h:122
#define netTaskRunning
Definition: net_legacy.h:197
systime_t autoIpTickCounter
Definition: auto_ip_misc.c:47
bool_t osCreateEvent(OsEvent *event)
Create an event object.
systime_t dhcpClientTickCounter
uint8_t value[]
Definition: tcp.h:369
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
error_t netSetVlanId(NetInterface *interface, uint16_t vlanId)
Specify VLAN identifier (802.1Q)
Definition: net.c:693
IPv4 routing.
error_t netStopInterface(NetInterface *interface)
Stop network interface.
Definition: net.c:1462
error_t netSetHostname(NetInterface *interface, const char_t *name)
Set host name.
Definition: net.c:664
Socket API.
@ NIC_UNKNOWN_DUPLEX_MODE
Definition: nic.h:123
error_t ipv6InitRouting(void)
Initialize IPv6 routing table.
Definition: ipv6_routing.c:57
error_t ethInit(NetInterface *interface)
Ethernet related initialization.
Definition: ethernet.c:64
error_t nbnsInit(NetInterface *interface)
NBNS related initialization.
Definition: nbns_common.c:53
Helper functions for Auto-IP.
#define NET_RAND_SEED_SIZE
Definition: net.h:156
LLMNR responder (Link-Local Multicast Name Resolution)
error_t netGetEui64(NetInterface *interface, Eui64 *eui64)
Retrieve EUI-64 interface identifier.
Definition: net.c:578
NicDuplexMode netGetDuplexMode(NetInterface *interface)
Get duplex mode.
Definition: net.c:1142
IPv4 (Internet Protocol Version 4)
OsTaskParameters task
Task parameters.
Definition: net.h:310
error_t netSetSwitchPort(NetInterface *interface, uint8_t port)
Specify switch port.
Definition: net.c:913
error_t mldInit(NetInterface *interface)
MLD initialization.
Definition: mld_common.c:72
#define TCP_INITIAL_RTO
Definition: tcp.h:117
TCP timer management.
TCP/IP stack settings.
Definition: net.h:309
unsigned int uint_t
Definition: compiler_port.h:50
error_t netStartInterface(NetInterface *interface)
Start network interface.
Definition: net.c:1382
uint8_t randSeed[NET_RAND_SEED_SIZE]
Random seed.
Definition: net.h:327
#define osMemset(p, value, length)
Definition: os_port.h:135
TCP/IP stack core.
NetInterface * nicGetLogicalInterface(NetInterface *interface)
Retrieve logical interface.
Definition: nic.c:53
error_t netSetExtIntDriver(NetInterface *interface, const ExtIntDriver *driver)
Set external interrupt line driver.
Definition: net.c:1023
NIC driver.
Definition: nic.h:286
error_t dnsInit(void)
DNS cache initialization.
Definition: dns_cache.c:59
void netTask(void)
TCP/IP events handling (deprecated)
Definition: net.c:1518
bool_t netGetLinkState(NetInterface *interface)
Get link state.
Definition: net.c:1080
void netGetRandData(uint8_t *data, size_t length)
Get a string of random data.
Definition: net.c:443
#define osStrcpy(s1, s2)
Definition: os_port.h:207
error_t llmnrResponderInit(NetInterface *interface)
LLMNR responder initialization.
systime_t mdnsResponderTickCounter
ARP (Address Resolution Protocol)
#define NET_TASK_PRIORITY
Definition: net.h:170
error_t ipv4Init(NetInterface *interface)
IPv4 related initialization.
Definition: ipv4.c:80
error_t socketInit(void)
Socket related initialization.
Definition: socket.c:85
systime_t ndpRouterAdvTickCounter
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define NET_MAX_HOSTNAME_LEN
Definition: net.h:149
NBNS responder (NetBIOS Name Service)
error_t netStart(NetContext *context)
Start TCP/IP stack.
Definition: net.c:308
mDNS responder (Multicast DNS)
systime_t osGetSystemTime(void)
Retrieve system time.
systime_t dnsTickCounter
Definition: dns_cache.c:49
error_t netSetInterfaceName(NetInterface *interface, const char_t *name)
Set interface name.
Definition: net.c:635