Choco OS  V.0.16.9.0
Join to the chocolate world
oc_net.h
Go to the documentation of this file.
1 
33 #ifndef SYSTEM_CORE_INC_NET_OC_NET_H_
34 #define SYSTEM_CORE_INC_NET_OC_NET_H_
35 
36 #include <oc_memory.h>
37 #include <oc_stdio.h>
38 #include <oc_baudrate.h>
39 #include <oc_compiler.h>
40 #include <oc_string.h>
41 #include <oc_stdlib.h>
42 #include <oc_array.h>
43 #include <oc_debug.h>
44 #include <oc_memman.h>
45 #include <oc_math.h>
46 
52 #define _________________________________________DEFINITIONS_SECTION________________________________________________________________________
53 
54 // +----------------------------------------------------------------+
55 // | IEEE 802.3 |
56 // +----------------------------------------------------------------+
57 #define IEEE_802_3_MAC_ADDRESS_LENGTH 6
58 #define IEEE_802_3_ETHERNET_MTU 1500
59 #define IEEE_802_3_MINIMUM_PAYLOAD 42
60 
61 // +----------------------------------------------------------------+
62 // | SELECTED STD |
63 // +----------------------------------------------------------------+
64 #define MAC_ADDRESS_LENGTH IEEE_802_3_MAC_ADDRESS_LENGTH
65 #define NETWORK_MTU IEEE_802_3_ETHERNET_MTU
66 #define MINIMUM_PAYLOAD IEEE_802_3_MINIMUM_PAYLOAD
67 #define IPv4_MAXIMUM_DATA_LENGTH (NETWORK_MTU - sizeof(oC_Net_Ipv4PacketHeader_t))
68 #define IPv6_MAXIMUM_DATA_LENGTH (NETWORK_MTU - sizeof(oC_Net_Ipv6PacketHeader_t))
69 
70 // +----------------------------------------------------------------+
71 // | OTHER NET DEFINITIONS |
72 // +----------------------------------------------------------------+
73 #define IFNAMSIZ 64
74 #define MAXIMUM_NETWORK_INTERFACES 0xFFFFFFFF
75 #define MAXIMUM_FRIENDLY_INTERFACE_NAME 100
76 #define DHCP_SERVER_NAME_LENGTH 64
77 #define MAXIMUM_PACKET_SIZE UINT16_MAX
78 
79 #undef _________________________________________DEFINITIONS_SECTION________________________________________________________________________
80 
86 #define _________________________________________FUNCTION_LIKE_MACROS_SECTION_______________________________________________________________
87 
88 #define IP(A,B,C,D) ( ( (D) << 0 ) | ( (C) << 8 ) | ( (B) << 16 ) | ( (A) << 24 ))
89 
90 #undef _________________________________________FUNCTION_LIKE_MACROS_SECTION_______________________________________________________________
91 
92 
98 #define _________________________________________TYPES_SECTION______________________________________________________________________________
99 
100 //==========================================================================================================================================
105 //==========================================================================================================================================
106 typedef uint8_t oC_Net_MacAddress_t[MAC_ADDRESS_LENGTH];
107 
108 //==========================================================================================================================================
113 //==========================================================================================================================================
114 typedef struct
115 {
116  union
117  {
118  uint8_t Address;
120  };
121  bool Filled;
123 
124 //==========================================================================================================================================
131 //==========================================================================================================================================
132 typedef uint32_t oC_Net_Ipv4_t;
133 
134 //==========================================================================================================================================
141 //==========================================================================================================================================
142 typedef struct PACKED
143 {
144  uint64_t LowPart;
145  uint64_t HighPart;
146 } oC_Net_Ipv6_t ;
147 
148 //==========================================================================================================================================
154 //==========================================================================================================================================
155 typedef enum
156 {
165 
166 //==========================================================================================================================================
173 //==========================================================================================================================================
174 typedef enum
175 {
180 
181 //==========================================================================================================================================
188 //==========================================================================================================================================
189 typedef enum
190 {
195 
196 //==========================================================================================================================================
201 //==========================================================================================================================================
202 typedef enum
203 {
207 
208 //==========================================================================================================================================
215 //==========================================================================================================================================
216 typedef struct PACKED
217 {
218 #ifdef LITTLE_ENDIAN
219  uint32_t Version:4;
220  uint32_t IHL:4;
221  uint32_t QoS:8;
222  uint32_t Length:16;
223  uint32_t ID:16;
224  uint32_t FragmentOffset:13;
225  uint32_t MF:1;
226  uint32_t DF:1;
227  uint32_t Reserved:1;
228  uint32_t TTL:8;
229  uint32_t Protocol:8;
230  uint32_t Checksum:16;
233 #elif defined(BIG_ENDIAN)
234 # error The structure is not defined for BIG_ENDIAN
235 #else
236 # error Endianess is not defined
237 #endif
239 
240 //==========================================================================================================================================
247 //==========================================================================================================================================
248 typedef struct PACKED
249 {
250 #ifdef LITTLE_ENDIAN
251  uint32_t Version:4;
252  uint32_t TrafficClass:8;
253  uint32_t FlowLabel:20;
254  uint32_t PayloadLength:16;
255  uint32_t NextHeader:8;
256  uint32_t HopLimit:8;
259 #elif defined(BIG_ENDIAN)
260 # error The structure is not defined for BIG_ENDIAN
261 #else
262 # error Endianess is not defined
263 #endif
265 
266 //==========================================================================================================================================
273 //==========================================================================================================================================
274 typedef struct PACKED
275 {
277  uint8_t Data[IPv4_MAXIMUM_DATA_LENGTH];
279 
280 //==========================================================================================================================================
287 //==========================================================================================================================================
288 typedef struct PACKED
289 {
291  uint8_t Data[IPv6_MAXIMUM_DATA_LENGTH];
293 
294 //==========================================================================================================================================
301 //==========================================================================================================================================
302 typedef union
303 {
307 
308 //==========================================================================================================================================
313 //==========================================================================================================================================
314 typedef struct
315 {
318  union
319  {
322  };
323  uint16_t Size;
326 
327 //==========================================================================================================================================
332 //==========================================================================================================================================
333 typedef enum
334 {
342 
345 
347 
350 
353 
354 //==========================================================================================================================================
359 //==========================================================================================================================================
360 typedef enum
361 {
365 
366 //==========================================================================================================================================
373 //==========================================================================================================================================
374 typedef uint32_t oC_Net_InterfaceIndex_t;
375 
376 //==========================================================================================================================================
383 //==========================================================================================================================================
384 typedef char oC_Net_InterfaceName_t[IFNAMSIZ];
385 
386 //==========================================================================================================================================
392 //==========================================================================================================================================
393 typedef char oC_Net_FriendlyInterfaceName_t[MAXIMUM_FRIENDLY_INTERFACE_NAME];
394 
395 //==========================================================================================================================================
402 //==========================================================================================================================================
403 typedef struct
404 {
408 
409 //==========================================================================================================================================
416 //==========================================================================================================================================
417 typedef uint16_t oC_Net_Port_t;
418 
419 //==========================================================================================================================================
426 //==========================================================================================================================================
427 typedef enum
428 {
432 
433 //==========================================================================================================================================
440 //==========================================================================================================================================
441 typedef struct
442 {
444  union
445  {
448  };
452 
453 //==========================================================================================================================================
458 //==========================================================================================================================================
459 typedef uint32_t oC_Net_Cost_t;
460 
461 //==========================================================================================================================================
468 //==========================================================================================================================================
469 typedef struct
470 {
475  char DhcpServerName[DHCP_SERVER_NAME_LENGTH];
478  oC_MemorySize_t MTU;
480  uint8_t DefaultTTL;
482  oC_Time_t LeaseTime;
483  oC_Timestamp_t IpExpiredTimestamp;
486 
487 //==========================================================================================================================================
494 //==========================================================================================================================================
495 typedef struct
496 {
499 
500 //==========================================================================================================================================
507 //==========================================================================================================================================
508 typedef struct
509 {
512  const char * DriverName;
514  oC_MemorySize_t TransmittedBytes;
515  oC_MemorySize_t ReceivedBytes;
520  oC_BaudRate_t BaudRate;
522 
523  struct
524  {
527  } NetworkLayer;
528 } oC_Net_Info_t;
529 
530 #undef _________________________________________TYPES_SECTION______________________________________________________________________________
531 
537 #define _________________________________________INLINE_SECTION_____________________________________________________________________________
538 
539 //==========================================================================================================================================
544 //==========================================================================================================================================
545 static inline uint16_t oC_Net_ConvertUint16ToNetworkEndianess(uint16_t v)
546 {
547 #if defined(LITTLE_ENDIAN)
548  return (v >> 8) | (v << 8);
549 #elif defined(BIG_ENDIAN)
550  return v;
551 #else
552 # error Unknown endianess
553 #endif
554 }
555 
556 //==========================================================================================================================================
561 //==========================================================================================================================================
562 static inline uint32_t oC_Net_ConvertUint32ToNetworkEndianess(uint32_t v)
563 {
564 #if defined(LITTLE_ENDIAN)
566 #elif defined(BIG_ENDIAN)
567  return v;
568 #else
569 # error Unknown endianess
570 #endif
571 }
572 
573 //==========================================================================================================================================
578 //==========================================================================================================================================
579 static inline uint16_t oC_Net_ConvertUint16FromNetworkEndianess(uint16_t v)
580 {
582 }
583 
584 //==========================================================================================================================================
589 //==========================================================================================================================================
590 static inline uint32_t oC_Net_ConvertUint32FromNetworkEndianess(uint32_t v)
591 {
593 }
594 
595 //==========================================================================================================================================
600 //==========================================================================================================================================
601 static inline void oC_Net_ConvertBufferToNetworkEndianess( void * Buffer , uint16_t Size )
602 {
603  uint16_t * array = Buffer;
604  uint16_t arraySize = Size / sizeof(uint16_t);
605 
606  if(
607  oC_SaveIfFalse("oC_Net_ConvertBufferToNetworkEndianess: " , isram(Buffer) , oC_ErrorCode_AddressNotInRam )
608  && oC_SaveIfFalse("oC_Net_ConvertBufferToNetworkEndianess: (size is 0)" , Size > 0 , oC_ErrorCode_SizeNotCorrect )
609  && oC_SaveIfFalse("oC_Net_ConvertBufferToNetworkEndianess: (size not aligned to 2 bytes)" , Size % sizeof(uint16_t) == 0 , oC_ErrorCode_SizeNotAligned )
610  )
611  {
612  for(uint16_t i = 0 ; i < arraySize ; i++)
613  {
614  array[i] = oC_Net_ConvertUint16ToNetworkEndianess(array[i]);
615  }
616  }
617 }
618 
619 //==========================================================================================================================================
624 //==========================================================================================================================================
625 static inline void oC_Net_ConvertBufferFromNetworkEndianess( void * Buffer , uint16_t Size )
626 {
627  uint16_t * array = Buffer;
628  uint16_t arraySize = Size / sizeof(uint16_t);
629 
630  if(
631  oC_SaveIfFalse("oC_Net_ConvertBufferFromNetworkEndianess: " , isram(Buffer) , oC_ErrorCode_AddressNotInRam )
632  && oC_SaveIfFalse("oC_Net_ConvertBufferFromNetworkEndianess: (size is 0)" , Size > 0 , oC_ErrorCode_SizeNotCorrect )
633  && oC_SaveIfFalse("oC_Net_ConvertBufferFromNetworkEndianess: (size not aligned to 2 bytes)" , Size % sizeof(uint16_t) == 0 , oC_ErrorCode_SizeNotAligned )
634  )
635  {
636  for(uint16_t i = 0 ; i < arraySize ; i++)
637  {
638  array[i] = oC_Net_ConvertUint16FromNetworkEndianess(array[i]);
639  }
640  }
641 }
642 
643 //==========================================================================================================================================
658 //==========================================================================================================================================
659 static inline int oC_Net_PrepareInterfaceName( oC_Net_InterfaceName_t *outName , const char * Prefix , oC_Net_InterfaceIndex_t Index )
660 {
661  return sprintf_s( *outName , sizeof(*outName) , "%s#%X" , Prefix , Index);
662 }
663 
664 //==========================================================================================================================================
676 //==========================================================================================================================================
677 static inline int oC_Net_MacAddressFromString( const char * MacAddressString , oC_Net_MacAddress_t outMacAddress )
678 {
679  return sscanf(MacAddressString,"%x:%x:%x:%x:%x:%x" , &outMacAddress[0], &outMacAddress[1], &outMacAddress[2],
680  &outMacAddress[3], &outMacAddress[4], &outMacAddress[5]);
681 }
682 
683 //==========================================================================================================================================
688 //==========================================================================================================================================
689 static inline const char * oC_Net_GetProtocolName( oC_Net_Protocol_t Protocol )
690 {
691  const char * name = "unknown";
692 
693  switch(Protocol)
694  {
695  case oC_Net_Protocol_ICMP : name = "[ICMP ] Internet Control Message Protocol "; break;
696  case oC_Net_Protocol_IGMP : name = "[IGMP ] Internet Group Management Protocol "; break;
697  case oC_Net_Protocol_TCP : name = "[TCP ] Transmission Control Protocol "; break;
698  case oC_Net_Protocol_UDP : name = "[UDP ] User Datagram Protocol "; break;
699  case oC_Net_Protocol_ENCAP : name = "[ENCAP] IPv6 encapsulation protocol "; break;
700  case oC_Net_Protocol_OSPF : name = "[OSPF ] Open Shortest Path First "; break;
701  case oC_Net_Protocol_SCTP : name = "[SCTP ] Stream Control Transmission Protocol"; break;
702  default:
703  break;
704  }
705 
706 
707  return name;
708 }
709 
710 //==========================================================================================================================================
724 //==========================================================================================================================================
725 static inline oC_ErrorCode_t oC_Net_HardwareAddressToString( const oC_Net_HardwareAddress_t * HardwareAddress , oC_Net_HardwareType_t Type , char * outString , oC_MemorySize_t Size )
726 {
727  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
728 
729  if(
730  ErrorCondition( isaddresscorrect(HardwareAddress) , oC_ErrorCode_WrongAddress )
731  && ErrorCondition( HardwareAddress->Filled , oC_ErrorCode_HardwareAddressIsEmpty )
732  && ErrorCondition( isram(outString) , oC_ErrorCode_OutputAddressNotInRAM )
733  && ErrorCondition( Size > 0 , oC_ErrorCode_SizeNotCorrect )
734  )
735  {
736  switch(Type)
737  {
739  sprintf_s(outString,Size,"%02X:%02X:%02X:%02X:%02X:%02X",
740  HardwareAddress->MacAddress[0],
741  HardwareAddress->MacAddress[1],
742  HardwareAddress->MacAddress[2],
743  HardwareAddress->MacAddress[3],
744  HardwareAddress->MacAddress[4],
745  HardwareAddress->MacAddress[5]
746  );
747  errorCode = oC_ErrorCode_None;
748  break;
749  default:
750  errorCode = oC_ErrorCode_UnknownHardwareType;
751  break;
752  }
753  }
754 
755  return errorCode;
756 }
757 
758 //==========================================================================================================================================
771 //==========================================================================================================================================
772 static inline oC_ErrorCode_t oC_Net_Ipv4AddressToString( oC_Net_Ipv4_t Address, char * outString , oC_MemorySize_t Size )
773 {
774  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
775 
776  if(
777  ErrorCondition( isram(outString) , oC_ErrorCode_OutputAddressNotInRAM )
778  && ErrorCondition( Size > 0 , oC_ErrorCode_SizeNotCorrect )
779  )
780  {
781  sprintf_s(outString, Size, "%d.%d.%d.%d",
782  (Address & 0xFF000000) >> 24,
783  (Address & 0x00FF0000) >> 16,
784  (Address & 0x0000FF00) >> 8,
785  (Address & 0x000000FF) >> 0
786  );
787  errorCode = oC_ErrorCode_None;
788  }
789 
790  return errorCode;
791 }
792 
793 //==========================================================================================================================================
806 //==========================================================================================================================================
807 static inline oC_ErrorCode_t oC_Net_AddressToString( const oC_Net_Address_t * Address, char * outString , oC_MemorySize_t Size )
808 {
809  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
810 
811  if(
812  ErrorCondition( isaddresscorrect(Address) , oC_ErrorCode_WrongAddress )
813  && ErrorCondition( isram(outString) , oC_ErrorCode_OutputAddressNotInRAM )
814  && ErrorCondition( Size > 0 , oC_ErrorCode_SizeNotCorrect )
815  )
816  {
817  if(Address->Type == oC_Net_AddressType_IPv4)
818  {
819  errorCode = oC_Net_Ipv4AddressToString(Address->IPv4,outString,Size);
820  }
821  else if(Address->Type == oC_Net_AddressType_IPv6)
822  {
823  errorCode = oC_ErrorCode_NotImplemented;
824  }
825  else
826  {
827  errorCode = oC_ErrorCode_UnknownAddressType;
828  }
829  }
830 
831  return errorCode;
832 }
833 
834 //==========================================================================================================================================
846 //==========================================================================================================================================
847 static inline oC_ErrorCode_t oC_Net_AddressFromString( const char * Address , oC_Net_Address_t * outAddress )
848 {
849  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
850 
851  if(
852  ErrorCondition( isaddresscorrect(Address) , oC_ErrorCode_WrongAddress )
853  && ErrorCondition( isram(outAddress) , oC_ErrorCode_OutputAddressNotInRAM )
854  )
855  {
856  uint8_t bytesArray[16] = {0};
857  oC_Net_Port_t port = 0;
858 
859  if(
860  ErrorCode( sscanf(Address, "%d.%d.%d.%d", &bytesArray[0], &bytesArray[1], &bytesArray[2], &bytesArray[3] ) )
861  || ErrorCode( sscanf(Address, "%d.%d.%d.%d:%d", &bytesArray[0], &bytesArray[1], &bytesArray[2], &bytesArray[3], &port ) )
862  )
863  {
864  outAddress->Type = oC_Net_AddressType_IPv4;
865  outAddress->Port = port;
866  outAddress->IPv4 = (bytesArray[0] << 24)
867  | (bytesArray[1] << 16)
868  | (bytesArray[2] << 8)
869  | (bytesArray[3] << 0);
870  errorCode = oC_ErrorCode_None;
871  }
872  else if(
873  ErrorCode( sscanf(Address, "%x:%x:%x:%x:%x:%x:%x:%x", &bytesArray[0], &bytesArray[1], &bytesArray[2], &bytesArray[3],
874  &bytesArray[4], &bytesArray[5], &bytesArray[6], &bytesArray[7]) )
875  )
876  {
877  outAddress->Type = oC_Net_AddressType_IPv6;
878  outAddress->Port = port;
879  outAddress->IPv6.LowPart = (((uint64_t)bytesArray[0] ) << 0 )
880  | (((uint64_t)bytesArray[1] ) << 8 )
881  | (((uint64_t)bytesArray[2] ) << 16)
882  | (((uint64_t)bytesArray[3] ) << 24)
883  | (((uint64_t)bytesArray[4] ) << 32)
884  | (((uint64_t)bytesArray[5] ) << 40)
885  | (((uint64_t)bytesArray[6] ) << 48)
886  | (((uint64_t)bytesArray[7] ) << 56);
887  outAddress->IPv6.HighPart = (((uint64_t)bytesArray[8] ) << 0 )
888  | (((uint64_t)bytesArray[9] ) << 8 )
889  | (((uint64_t)bytesArray[10]) << 16)
890  | (((uint64_t)bytesArray[11]) << 24)
891  | (((uint64_t)bytesArray[12]) << 32)
892  | (((uint64_t)bytesArray[13]) << 40)
893  | (((uint64_t)bytesArray[14]) << 48)
894  | (((uint64_t)bytesArray[15]) << 56);
895  errorCode = oC_ErrorCode_None;
896  }
897  else
898  {
899  errorCode = oC_ErrorCode_IpAddressNotCorrect;
900  }
901  }
902 
903  return errorCode;
904 }
905 
906 //==========================================================================================================================================
917 //==========================================================================================================================================
918 static inline bool oC_Net_IsAddressCorrect( const oC_Net_Address_t * Address )
919 {
920  bool correct = false;
921 
922  if(isaddresscorrect(Address))
923  {
924  if(Address->Type == oC_Net_AddressType_IPv4)
925  {
926  correct = true;
927  }
928  else if(Address->Type == oC_Net_AddressType_IPv6)
929  {
930  correct = true;
931  }
932  }
933 
934  return correct;
935 }
936 
937 //==========================================================================================================================================
948 //==========================================================================================================================================
949 static inline bool oC_Net_IsAddressEmpty( const oC_Net_Address_t * Address )
950 {
951  bool empty = true;
952 
953  if(isaddresscorrect(Address))
954  {
955  if(Address->Type == oC_Net_AddressType_IPv4)
956  {
957  empty = Address->IPv4 == 0;
958  }
959  else if(Address->Type == oC_Net_AddressType_IPv6)
960  {
961  empty = Address->IPv6.LowPart == 0 && Address->IPv6.HighPart == 0;
962  }
963  }
964 
965  return empty;
966 }
967 
968 //==========================================================================================================================================
981 //==========================================================================================================================================
983 {
984  return (IP & Mask) == (Subnet & Mask);
985 }
986 
987 //==========================================================================================================================================
1000 //==========================================================================================================================================
1001 static inline bool oC_Net_IsAddressInSubnet( const oC_Net_Address_t * Address, const oC_Net_Address_t * Subnet , const oC_Net_Address_t * Mask )
1002 {
1003  bool belong = false;
1004 
1005  if(isaddresscorrect(Address) && isaddresscorrect(Subnet) && isaddresscorrect(Mask) && Address->Type == Subnet->Type)
1006  {
1007  if(Address->Type == oC_Net_AddressType_IPv4)
1008  {
1009  belong = oC_Net_Ipv4_IsAddressInSubnet(Address->IPv4,Subnet->IPv4,Mask->IPv4);
1010  }
1011  else if(Address->Type == oC_Net_AddressType_IPv6)
1012  {
1013  oC_SaveError("Net::IsAddressInSubnet - IPv6 version - ", oC_ErrorCode_NotImplemented);
1014  }
1015  }
1016 
1017  return belong;
1018 }
1019 
1020 //==========================================================================================================================================
1032 //==========================================================================================================================================
1033 static inline bool oC_Net_AreAddressesTheSame( const oC_Net_Address_t * Address1 , const oC_Net_Address_t * Address2 )
1034 {
1035  bool same = false;
1036 
1037  if(isaddresscorrect(Address1) && isaddresscorrect(Address2) && Address1->Type == Address2->Type)
1038  {
1039  if(Address1->Type == oC_Net_AddressType_IPv4)
1040  {
1041  same = Address1->IPv4 == Address2->IPv4;
1042  }
1043  else if(Address1->Type == oC_Net_AddressType_IPv6)
1044  {
1045  same = memcmp(&Address1->IPv6,&Address2->IPv6,sizeof(oC_Net_Ipv6_t)) == 0;
1046  }
1047  }
1048 
1049  return same;
1050 }
1051 
1052 //==========================================================================================================================================
1057 //==========================================================================================================================================
1059 {
1060  return Type == oC_Net_PacketType_IPv4 ? (MAXIMUM_PACKET_SIZE - sizeof(oC_Net_Ipv4PacketHeader_t)) :
1061  Type == oC_Net_PacketType_IPv6 ? (MAXIMUM_PACKET_SIZE - sizeof(oC_Net_Ipv6PacketHeader_t)) : 0;
1062 }
1063 
1064 //==========================================================================================================================================
1075 //==========================================================================================================================================
1076 static inline uint16_t oC_Net_GetPacketSize( const oC_Net_Packet_t * Packet , bool WithHeader )
1077 {
1078  uint16_t size = 0;
1079 
1080  if(isaddresscorrect(Packet))
1081  {
1082  if(Packet->IPv4.Header.Version == oC_Net_PacketType_IPv4)
1083  {
1084  size = (uint16_t)Packet->IPv4.Header.Length;
1085 
1086  if( WithHeader == false )
1087  {
1088  size -= Packet->IPv4.Header.IHL * sizeof(uint32_t);
1089  }
1090  }
1091  else if(Packet->IPv4.Header.Version == oC_Net_PacketType_IPv6)
1092  {
1093  size = (uint16_t)Packet->IPv6.Header.PayloadLength + sizeof(oC_Net_Ipv6PacketHeader_t);
1094  if(WithHeader)
1095  {
1096  size += sizeof(oC_Net_Ipv6PacketHeader_t);
1097  }
1098  }
1099  }
1100 
1101  return size;
1102 }
1103 
1104 //==========================================================================================================================================
1109 //==========================================================================================================================================
1111 {
1112  bool success = false;
1113 
1114  if(isaddresscorrect(Packet))
1115  {
1116  if(Packet->IPv4.Header.Version == oC_Net_PacketType_IPv4)
1117  {
1118  success = true;
1119 
1120  uint32_t ihl = Packet->IPv4.Header.IHL;
1121 
1122  Packet->IPv4.Header.IHL = Packet->IPv4.Header.Version;
1123  Packet->IPv4.Header.Version = ihl;
1125 
1128 
1129  uint16_t * word = (void*)Packet;
1130 
1131  word[3] = oC_Net_ConvertUint16FromNetworkEndianess(word[3]); /* Offset & Flags */
1132  }
1133  else if(Packet->IPv6.Header.Version == oC_Net_PacketType_IPv6)
1134  {
1135  kdebuglog(oC_LogType_Error, "oC_Net_ConvertHeaderToNetworkEndianess: not implemented for IPv6 yet\n");
1136  }
1137  else
1138  {
1139  kdebuglog(oC_LogType_Error, "oC_Net_ConvertHeaderToNetworkEndianess: incorrect packet type %d\n", Packet->IPv6.Header.Version);
1140  }
1141  }
1142 
1143  return success;
1144 }
1145 
1146 //==========================================================================================================================================
1151 //==========================================================================================================================================
1153 {
1154  bool success = false;
1155 
1156  if(isaddresscorrect(Packet))
1157  {
1158  /* When header is not converted, IHL stores version of the header */
1159  if(Packet->IPv4.Header.IHL == oC_Net_PacketType_IPv4)
1160  {
1161  success = true;
1162 
1163  /* When header is not converted Version stores IHL of the header */
1164  uint32_t ihl = Packet->IPv4.Header.Version;
1165 
1166  Packet->IPv4.Header.Version = Packet->IPv4.Header.IHL;
1167  Packet->IPv4.Header.IHL = ihl;
1169 
1172 
1173  uint16_t * word = (void*)Packet;
1174 
1175  word[3] = oC_Net_ConvertUint16FromNetworkEndianess(word[3]); /* Offset & Flags */
1176  }
1177  /* When header is not converted, IHL stores version of the header */
1178  else if(Packet->IPv4.Header.IHL == oC_Net_PacketType_IPv6)
1179  {
1180  kdebuglog(oC_LogType_Error, "not implemented for IPv6 yet\n");
1181  }
1182  else
1183  {
1184  kdebuglog(oC_LogType_Error, "oC_Net_ConvertHeaderFromNetworkEndianess: incorrect packet type %d\n", Packet->IPv4.Header.IHL);
1185  }
1186  }
1187 
1188  return success;
1189 }
1190 
1191 
1192 
1193 //==========================================================================================================================================
1198 //==========================================================================================================================================
1199 static inline oC_MemorySize_t oC_Net_GetHardwareAddressSize( oC_Net_HardwareType_t Type )
1200 {
1201  oC_MemorySize_t size = 0;
1202 
1203  switch(Type)
1204  {
1205  case oC_Net_HardwareType_Ethernet: size = sizeof(oC_Net_MacAddress_t); break;
1206  default: size = 0; break;
1207  }
1208 
1209  return size;
1210 }
1211 
1212 //==========================================================================================================================================
1217 //==========================================================================================================================================
1218 static inline oC_MemorySize_t oC_Net_GetAddressSize( const oC_Net_Address_t * Address )
1219 {
1220  oC_MemorySize_t size = 0;
1221 
1222  if(isaddresscorrect(Address))
1223  {
1224  switch(Address->Type)
1225  {
1226  case oC_Net_AddressType_IPv4: size = sizeof(oC_Net_Ipv4_t); break;
1227  case oC_Net_AddressType_IPv6: size = sizeof(oC_Net_Ipv6_t); break;
1228  }
1229  }
1230 
1231  return size;
1232 }
1233 
1234 //==========================================================================================================================================
1239 //==========================================================================================================================================
1240 static inline const char * oC_Net_GetHardwareTypeName( oC_Net_HardwareType_t Type )
1241 {
1242  const char * name = "unknown";
1243 
1244  switch(Type)
1245  {
1246  case oC_Net_HardwareType_Ethernet: name = "ethernet"; break;
1247  default: name = "unhandled in oC_Net_GetHardwareTypeName";
1248  }
1249 
1250  return name;
1251 }
1252 
1253 //==========================================================================================================================================
1258 //==========================================================================================================================================
1259 static inline oC_ErrorCode_t oC_Net_ReadLayerName( oC_Net_Layer_t Layer , char * outName, oC_MemorySize_t Size )
1260 {
1261  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1262 
1263  if(ErrorCondition(isram(outName), oC_ErrorCode_OutputAddressNotInRAM))
1264  {
1265  oC_Net_Layer_t mainLayer = Layer & oC_Net_Layer_MainLayerMask;
1266  oC_Net_Layer_t subLayer = Layer & oC_Net_Layer_SubLayerMask;
1267 
1268  const char * mainLayerName = "unknownMainLayer";
1269 
1270  switch(mainLayer)
1271  {
1272  default: break;
1273  case oC_Net_Layer_Physical : mainLayerName = "Physical"; break;
1274  case oC_Net_Layer_DataLink : mainLayerName = "DataLink"; break;
1275  case oC_Net_Layer_Network : mainLayerName = "Network"; break;
1276  case oC_Net_Layer_Transport : mainLayerName = "Transport"; break;
1277  case oC_Net_Layer_Session : mainLayerName = "Session"; break;
1278  case oC_Net_Layer_Presentation: mainLayerName = "Presentation"; break;
1279  case oC_Net_Layer_Application : mainLayerName = "Application"; break;
1280  }
1281 
1282  errorCode = sprintf_s(outName,Size,"%s:%d", mainLayerName, subLayer);
1283  }
1284 
1285  return errorCode;
1286 }
1287 
1288 //==========================================================================================================================================
1293 //==========================================================================================================================================
1294 static inline oC_ErrorCode_t oC_Net_ReadLayerByName( oC_Net_Layer_t * outLayer, const char * Name )
1295 {
1296  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1297 
1298  if(
1299  ErrorCondition( isram(outLayer) , oC_ErrorCode_OutputAddressNotInRAM )
1300  && ErrorCondition( isaddresscorrect(Name) , oC_ErrorCode_WrongAddress )
1301  )
1302  {
1303  uint32_t subLayer = 0;
1304  static const struct
1305  {
1306  const char * Name;
1307  oC_Net_Layer_t Layer;
1308  } layerStrings[] = {
1309  { .Name = "PHY" , .Layer = oC_Net_Layer_PHY } ,
1310  { .Name = "LLC" , .Layer = oC_Net_Layer_LLC } ,
1311  { .Name = "MAC" , .Layer = oC_Net_Layer_MAC } ,
1312  { .Name = "Netif" , .Layer = oC_Net_Layer_Netif } ,
1313  { .Name = "Physical" , .Layer = oC_Net_Layer_Physical } ,
1314  { .Name = "DataLink" , .Layer = oC_Net_Layer_DataLink } ,
1315  { .Name = "Network" , .Layer = oC_Net_Layer_Network } ,
1316  { .Name = "Transport" , .Layer = oC_Net_Layer_Transport } ,
1317  { .Name = "Session" , .Layer = oC_Net_Layer_Session } ,
1318  { .Name = "Presentation" , .Layer = oC_Net_Layer_Presentation } ,
1319  { .Name = "Application" , .Layer = oC_Net_Layer_Application } ,
1320  };
1321 
1322 
1323  errorCode = oC_ErrorCode_UnknownLayer;
1324 
1325  oC_ARRAY_FOREACH_IN_ARRAY(layerStrings,layer)
1326  {
1327  if(string_contains(Name,layer->Name,false))
1328  {
1329  *outLayer = layer->Layer;
1330  errorCode = oC_ErrorCode_None;
1331  }
1332  }
1333 
1334  if(
1335  string_contains(Name,":",true)
1336  && ErrorCode(sscanf(Name,"%*s:%u", &subLayer))
1337  )
1338  {
1339  *outLayer |= subLayer;
1340  }
1341  }
1342 
1343  return errorCode;
1344 }
1345 
1346 //==========================================================================================================================================
1352 //==========================================================================================================================================
1353 static inline bool oC_Net_IsMainLayer( oC_Net_Layer_t Layer , oC_Net_Layer_t MainLayer )
1354 {
1355  return (Layer & oC_Net_Layer_MainLayerMask) == MainLayer;
1356 }
1357 
1358 //==========================================================================================================================================
1369 //==========================================================================================================================================
1371 {
1373 
1374  if(isaddresscorrect(Packet))
1375  {
1376  type = (oC_Net_PacketType_t)Packet->IPv4.Header.Version;
1377  }
1378 
1379  return type;
1380 }
1381 
1382 //==========================================================================================================================================
1387 //==========================================================================================================================================
1388 static inline bool oC_Net_Packet_SetSize( oC_Net_Packet_t * Packet , uint16_t Size )
1389 {
1390  bool success = false;
1391 
1392  if(isram(Packet) && Size > 0 && Size <= oC_Net_GetMaximumPacketDataSize(oC_Net_Packet_GetType(Packet)))
1393  {
1394  if(Packet->IPv4.Header.Version == oC_Net_PacketType_IPv4)
1395  {
1396  Packet->IPv4.Header.Length = Size + (Packet->IPv4.Header.IHL * sizeof(uint32_t));
1397  success = true;
1398  }
1399  else if(Packet->IPv4.Header.Version == oC_Net_PacketType_IPv6)
1400  {
1401 
1402  kdebuglog(oC_LogType_Error, "oC_Net_SetPacketSize:: IPv6 is not ready yet\n", Packet->IPv4.Header.Version);
1403  }
1404  else
1405  {
1406  kdebuglog(oC_LogType_Error, "oC_Net_SetPacketSize:: incorrect packet version: %d\n", Packet->IPv4.Header.Version);
1407  }
1408  }
1409 
1410  return success;
1411 }
1412 
1413 //==========================================================================================================================================
1418 //==========================================================================================================================================
1419 static inline void * oC_Net_Packet_GetDataReference( oC_Net_Packet_t * Packet )
1420 {
1421  void * data = NULL;
1422 
1423  if(isaddresscorrect(Packet))
1424  {
1426 
1427  if(type == oC_Net_PacketType_IPv4)
1428  {
1429  data = Packet->IPv4.Data;
1430  }
1431  else if(type == oC_Net_PacketType_IPv6)
1432  {
1433  data = Packet->IPv6.Data;
1434  }
1435  }
1436 
1437  return data;
1438 }
1439 
1440 //==========================================================================================================================================
1445 //==========================================================================================================================================
1447 {
1448  if(isram(Packet))
1449  {
1450  if(Packet->IPv4.Header.Version == oC_Net_PacketType_IPv4)
1451  {
1452  Packet->IPv4.Header.Protocol = Source->Protocol;
1453 
1454  if(isaddresscorrect(Source) && Source->Type == oC_Net_AddressType_IPv4)
1455  {
1456  Packet->IPv4.Header.SourceIp = Source->IPv4;
1457  }
1458  if(isaddresscorrect(Destination) && Destination->Type == oC_Net_AddressType_IPv4)
1459  {
1460  Packet->IPv4.Header.DestinationIp = Destination->IPv4;
1461  }
1462  }
1463  else if(Packet->IPv4.Header.Version == oC_Net_PacketType_IPv6)
1464  {
1465  if(isaddresscorrect(Source) && Source->Type == oC_Net_AddressType_IPv6)
1466  {
1467  Packet->IPv6.Header.Source.HighPart = Source->IPv6.HighPart;
1468  Packet->IPv6.Header.Source.LowPart = Source->IPv6.LowPart;
1469  }
1470  if(isaddresscorrect(Destination) && Destination->Type == oC_Net_AddressType_IPv6)
1471  {
1472  Packet->IPv6.Header.Destination.HighPart = Destination->IPv6.HighPart;
1473  Packet->IPv6.Header.Destination.LowPart = Destination->IPv6.LowPart;
1474  }
1475  }
1476  }
1477 }
1478 
1479 //==========================================================================================================================================
1484 //==========================================================================================================================================
1485 static inline void oC_Net_Packet_SetData( oC_Net_Packet_t * Packet , const void * Data , uint16_t Size )
1486 {
1488 
1489  if(isram(Packet) && isaddresscorrect(Data) && Size > 0 && Size <= oC_Net_GetMaximumPacketDataSize(type))
1490  {
1491  if(type == oC_Net_PacketType_IPv4)
1492  {
1493  oC_Net_Packet_SetSize(Packet,Size);
1494  memcpy( Packet->IPv4.Data, Data, Size );
1495  }
1496  else if(type == oC_Net_PacketType_IPv6)
1497  {
1498  Packet->IPv6.Header.PayloadLength = Size;
1499  memcpy( Packet->IPv6.Data, Data, Size );
1500  }
1501 
1502  }
1503 }
1504 
1505 //==========================================================================================================================================
1510 //==========================================================================================================================================
1511 static inline oC_Net_Packet_t* oC_Net_Packet_New( Allocator_t Allocator , oC_Net_PacketType_t Type , void * Data , uint16_t Size )
1512 {
1513  oC_Net_Packet_t * packet = NULL;
1514 
1515  if(
1516  oC_SaveIfFalse( "Net::New-Packet - Allocator is not correct - " , isaddresscorrect(Allocator) , oC_ErrorCode_WrongAddress )
1517  && oC_SaveIfFalse( "Net::New-Packet - Type is not correct - " , Type == oC_Net_PacketType_IPv4
1518  || Type == oC_Net_PacketType_IPv6 , oC_ErrorCode_TypeNotCorrect )
1519  && oC_SaveIfFalse( "Net::New-Packet - Size is not correct - " , Size > 0 , oC_ErrorCode_SizeNotCorrect )
1520  && oC_SaveIfFalse( "Net::New-Packet - Size is not correct - " , Size <= oC_Net_GetMaximumPacketDataSize(Type) , oC_ErrorCode_SizeTooBig )
1521  && oC_SaveIfFalse( "Net::New-Packet - Data address is not correct - " , Data == NULL || isaddresscorrect(Data) , oC_ErrorCode_SizeNotCorrect )
1522  )
1523  {
1524  uint16_t headerSize = Type == oC_Net_PacketType_IPv4 ? sizeof(oC_Net_Ipv4PacketHeader_t) :
1525  Type == oC_Net_PacketType_IPv6 ? sizeof(oC_Net_Ipv6PacketHeader_t) : 0;
1526  uint16_t packetSize = oC_MAX(headerSize + Size, sizeof(oC_Net_Packet_t));
1527 
1528  if(oC_SaveIfFalse("Net::New-Packet - Fatal error - ", packetSize > Size , oC_ErrorCode_SizeTooBig))
1529  {
1530  packet = kmalloc( packetSize , Allocator, AllocationFlags_ZeroFill );
1531 
1532  if(oC_SaveIfFalse("Net::New-Packet - cannot allocate memory for a packet - ", packet != NULL , oC_ErrorCode_AllocationError ))
1533  {
1534  packet->IPv4.Header.Version = Type;
1535 
1536  if(Type == oC_Net_PacketType_IPv4)
1537  {
1538  packet->IPv4.Header.IHL = 5;
1539  }
1540 
1541  oC_Net_Packet_SetSize(packet,Size);
1542 
1543  if(Data != NULL)
1544  {
1545  oC_Net_Packet_SetData(packet,Data,Size);
1546  }
1547  }
1548 
1549  }
1550  }
1551 
1552  return packet;
1553 }
1554 
1555 //==========================================================================================================================================
1560 //==========================================================================================================================================
1561 static inline bool oC_Net_Packet_Delete( oC_Net_Packet_t ** Packet )
1562 {
1563  bool success = false;
1564 
1565  if(isram(Packet) && isram(*Packet))
1566  {
1567  success = kfree(*Packet,AllocationFlags_Default);
1568  *Packet = NULL;
1569  }
1570 
1571  return success;
1572 }
1573 
1574 //==========================================================================================================================================
1580 //==========================================================================================================================================
1581 static inline uint16_t oC_Net_CalculateChecksum( const void * Buffer , uint16_t Size , uint16_t InitialValue , bool ConvertEndianess , bool PrepareChecksumToSend )
1582 {
1583  uint32_t checksum = InitialValue;
1584 
1585  if(isaddresscorrect(Buffer) && Size > 0)
1586  {
1587  const uint16_t * array = Buffer;
1588  uint16_t count = Size / sizeof(uint16_t);
1589 
1590  for(uint16_t i = 0 ; i < count ; i++)
1591  {
1592  uint16_t valueInNetworkEndianess = ConvertEndianess ? oC_Net_ConvertUint16ToNetworkEndianess( array[i] ) : array[i];
1593 
1594  checksum += valueInNetworkEndianess;
1595 
1596  while(checksum >> 16)
1597  {
1598  checksum = (checksum & 0xFFFF) + (checksum >> 16);
1599  }
1600  }
1601 
1602  if(Size % sizeof(uint16_t))
1603  {
1604  if(ConvertEndianess)
1605  {
1606  checksum += (((uint8_t*)Buffer)[Size-1]) << 8;
1607  }
1608  else
1609  {
1610  checksum += ((uint8_t*)Buffer)[Size-1];
1611  }
1612  }
1613 
1614  while(checksum >> 16)
1615  {
1616  checksum = (checksum & 0xFFFF) + (checksum >> 16);
1617  }
1618 
1619  if(PrepareChecksumToSend)
1620  {
1621  checksum = (~checksum) & 0xFFFF;
1622 
1623  if(checksum == 0)
1624  {
1625  checksum = 0xFFFF;
1626  }
1627  }
1628  }
1629 
1630  return (uint16_t)checksum;
1631 }
1632 
1633 //==========================================================================================================================================
1638 //==========================================================================================================================================
1639 static inline uint16_t oC_Net_Packet_CalculateChecksum( oC_Net_Packet_t * Packet )
1640 {
1641  uint16_t checksum = 0;
1642 
1643  if(isaddresscorrect(Packet))
1644  {
1646 
1647  if(type == oC_Net_PacketType_IPv4)
1648  {
1649  uint16_t storedChecksum = Packet->IPv4.Header.Checksum;
1650  uint16_t headerSize = Packet->IPv4.Header.IHL * sizeof(uint32_t);
1651  Packet->IPv4.Header.Checksum = 0;
1652 
1654  checksum = oC_Net_CalculateChecksum(Packet, headerSize, 0, false, true);
1656 
1657  Packet->IPv4.Header.Checksum = storedChecksum;
1658 
1659  }
1660  }
1661 
1662  return checksum;
1663 }
1664 
1665 #undef _________________________________________INLINE_SECTION_____________________________________________________________________________
1666 
1667 
1668 #endif /* SYSTEM_CORE_INC_NET_OC_NET_H_ */
oC_Net_Ipv4_t SourceIp
Source IP address.
Definition: oc_net.h:231
static bool oC_Net_Packet_SetSize(oC_Net_Packet_t *Packet, uint16_t Size)
sets size of the packet
Definition: oc_net.h:1388
static oC_MemorySize_t oC_Net_GetAddressSize(const oC_Net_Address_t *Address)
returns size of IP address
Definition: oc_net.h:1218
Internet Group Management Protocol.
Definition: oc_net.h:158
Packet in type IPv4.
Definition: oc_net.h:177
The data link layer or layer 2 is the second layer of the seven-layer OSI model of computer networkin...
Definition: oc_net.h:336
static void oC_Net_ConvertBufferFromNetworkEndianess(void *Buffer, uint16_t Size)
converts buffer from network endianess
Definition: oc_net.h:625
static bool oC_Net_AreAddressesTheSame(const oC_Net_Address_t *Address1, const oC_Net_Address_t *Address2)
returns true if both pointers stores the same address
Definition: oc_net.h:1033
oC_Net_Ipv4_t NetIP
IP of the local network (router IP)
Definition: oc_net.h:472
uint32_t Length
Length of the packet in bytes.
Definition: oc_net.h:222
oC_Net_InterfaceIndex_t InterfaceIndex
Index of the interface in the driver.
Definition: oc_net.h:511
Basic math operations.
FILE__DESCRIPTION
struct PACKED oC_Net_Ipv6Packet_t
stores IPv6 packet
uint64_t HighPart
High 8 bytes of the IPv6 address.
Definition: oc_net.h:145
The file with interface for the GPIO driver.
static int oC_Net_MacAddressFromString(const char *MacAddressString, oC_Net_MacAddress_t outMacAddress)
reads MAC address from the string
Definition: oc_net.h:677
uint32_t QoS
Quality of Service or Type of Service - describes the priority of the packet
Definition: oc_net.h:221
oC_Net_Protocol_t Protocol
IP Protocol.
Definition: oc_net.h:449
oC_MemorySize_t TransmittedBytes
Number of transmitted bytes.
Definition: oc_net.h:514
uint32_t HopLimit
The same as TTL from the IPv4 header - time of packet&#39;s live.
Definition: oc_net.h:256
In the seven-layer OSI model of computer networking, the presentation layer is layer 6 and serves as ...
Definition: oc_net.h:340
static oC_Net_Packet_t * oC_Net_Packet_New(Allocator_t Allocator, oC_Net_PacketType_t Type, void *Data, uint16_t Size)
allocates memory for a packet
Definition: oc_net.h:1511
In the seven-layer OSI model of computer networking, the network layer is layer 3.
Definition: oc_net.h:337
Mask to get the sublayer of the OSI model.
Definition: oc_net.h:344
static uint16_t oC_Net_ConvertUint16FromNetworkEndianess(uint16_t v)
converts uint16_t from network byte order
Definition: oc_net.h:579
oC_Net_Ipv4PacketHeader_t Header
Header of the IPv4 packet.
Definition: oc_net.h:276
identifier for allocations
Definition: oc_stdlib.h:159
User Datagram Protocol.
Definition: oc_net.h:160
uint32_t DF
Don&#39;t Fragment - packet can be fragmented or not
Definition: oc_net.h:226
oC_Time_t LeaseTime
IP lease time.
Definition: oc_net.h:482
stores network interface informations
Definition: oc_net.h:508
oC_Net_MacAddress_t MacAddress
MAC address.
Definition: oc_net.h:119
static oC_ErrorCode_t oC_Net_ReadLayerByName(oC_Net_Layer_t *outLayer, const char *Name)
returns network OSI layer according to its name
Definition: oc_net.h:1294
oC_Net_Protocol_t
stores the protocol number for the IP headers The type is for storing protocol number. The values can be written indirectly to the IP headers.
Definition: oc_net.h:155
uint32_t HardwareAddressLength
Number of bytes in the HW address.
Definition: oc_net.h:519
Invalid value.
Definition: oc_net.h:176
The file contains definitions for the compiler, that helps to manage errors, etc. ...
static void oC_Net_Packet_SetAddress(oC_Net_Packet_t *Packet, const oC_Net_Address_t *Source, const oC_Net_Address_t *Destination)
sets addresses in the packet
Definition: oc_net.h:1446
Packet in type IPv6.
Definition: oc_net.h:178
const oC_Net_Packet_t * ConstPacket
Pointer to the packet to send (for sending)
Definition: oc_net.h:321
IPv6 encapsulation protocol.
Definition: oc_net.h:161
oC_Net_Ipv6PacketHeader_t Header
Header of the IPv6 packet.
Definition: oc_net.h:290
stores status of network queue
Definition: oc_net.h:403
stores network address
Definition: oc_net.h:441
char oC_Net_FriendlyInterfaceName_t[MAXIMUM_FRIENDLY_INTERFACE_NAME]
type for storing friendly name string
Definition: oc_net.h:393
oC_Net_LinkStatus_t LinkStatus
Network interface link status (UP/DOWN)
Definition: oc_net.h:513
oC_Net_AddressType_t
stores type of the network address
Definition: oc_net.h:427
static bool oC_Net_IsAddressEmpty(const oC_Net_Address_t *Address)
returns true if the given address is empty
Definition: oc_net.h:949
static bool oC_Net_ConvertHeaderToNetworkEndianess(oC_Net_Packet_t *Packet)
converts header to network endianess
Definition: oc_net.h:1110
static bool oC_Net_Ipv4_IsAddressInSubnet(oC_Net_Ipv4_t IP, oC_Net_Ipv4_t Subnet, oC_Net_Ipv4_t Mask)
checks if the IPv4 address belongs to the given subnetwork
Definition: oc_net.h:982
oC_Net_Ipv4_t DnsIP
IP of the local DNS server.
Definition: oc_net.h:477
Invalid type.
Definition: oc_net.h:204
bool Filled
True if the address is filled.
Definition: oc_net.h:121
oC_Net_PacketType_t
stores type of the packet
Definition: oc_net.h:174
uint32_t NumberOfElementsInRxQueue
Number of elements in queue ready to receive.
Definition: oc_net.h:406
An application layer is an abstraction layer that specifies the shared protocols and interface method...
Definition: oc_net.h:341
const char * DriverName
Name of the driver that handles the network interface.
Definition: oc_net.h:512
char oC_Net_InterfaceName_t[IFNAMSIZ]
stores name of the network interface
Definition: oc_net.h:384
uint32_t oC_Net_Ipv4_t
stores IPv4 address
Definition: oc_net.h:132
static bool oC_Net_IsAddressInSubnet(const oC_Net_Address_t *Address, const oC_Net_Address_t *Subnet, const oC_Net_Address_t *Mask)
checks if the address is in subnet
Definition: oc_net.h:1001
static oC_Net_PacketType_t oC_Net_Packet_GetType(const oC_Net_Packet_t *Packet)
returns type of the packet
Definition: oc_net.h:1370
static oC_ErrorCode_t oC_Net_ReadLayerName(oC_Net_Layer_t Layer, char *outName, oC_MemorySize_t Size)
reads network layer name
Definition: oc_net.h:1259
static uint16_t oC_Net_GetMaximumPacketDataSize(oC_Net_PacketType_t Type)
returns maximum size of the packet data
Definition: oc_net.h:1058
oC_Net_HardwareAddress_t Destination
Hardware Destination Address.
Definition: oc_net.h:316
uint8_t oC_Net_MacAddress_t[MAC_ADDRESS_LENGTH]
stores MAC address
Definition: oc_net.h:106
Transmission Control Protocol.
Definition: oc_net.h:159
Mask to get the main layer of the OSI model.
Definition: oc_net.h:343
oC_Net_HardwareType_t
stores hardware type of the network interface
Definition: oc_net.h:202
PHY layer.
Definition: oc_net.h:346
oC_Net_Ipv4_t NtpIP
Main Network Time Protocol Server available in the subnetwork.
Definition: oc_net.h:481
static uint16_t oC_Net_ConvertUint16ToNetworkEndianess(uint16_t v)
converts uint16_t to network byte order
Definition: oc_net.h:545
oC_Net_Ipv4_t Netmask
Local network mask.
Definition: oc_net.h:473
static oC_ErrorCode_t oC_Net_HardwareAddressToString(const oC_Net_HardwareAddress_t *HardwareAddress, oC_Net_HardwareType_t Type, char *outString, oC_MemorySize_t Size)
prints HW address to the string
Definition: oc_net.h:725
oC_Net_HardwareAddress_t HardwareAddress
Hardware address.
Definition: oc_net.h:517
uint8_t DefaultTTL
Default TTL value for the IPv4 packets.
Definition: oc_net.h:480
oC_Net_InterfaceName_t InterfaceName
Name of the interface.
Definition: oc_net.h:510
oC_Net_Ipv4_t BroadcastIP
IP that should be used for broadcasting.
Definition: oc_net.h:479
uint32_t oC_Net_Cost_t
stores cost of the network connection
Definition: oc_net.h:459
uint32_t Reserved
This is always 0.
Definition: oc_net.h:227
oC_MemorySize_t MTU
MTU (Maximum Transmission Unit) for the local network.
Definition: oc_net.h:478
uint8_t Address
Universal HW Address pointer.
Definition: oc_net.h:118
oC_BaudRate_t BaudRate
Baud rate of the interface.
Definition: oc_net.h:520
Logical link control sublayer - The uppermost sublayer, LLC, multiplexes protocols running atop the d...
Definition: oc_net.h:348
uint32_t PayloadLength
Size of the payload in octets (bytes)
Definition: oc_net.h:254
uint32_t FragmentOffset
Field to identify position of fragment within original packet.
Definition: oc_net.h:224
oC_Net_Ipv4Info_t IPv4
IPv4 informations.
Definition: oc_net.h:525
stores network packet
Definition: oc_net.h:302
oC_Net_Ipv4_t IPv4
Address in IPv4 version.
Definition: oc_net.h:446
uint32_t ID
ID value for reconstruction the packet from segments.
Definition: oc_net.h:223
In the seven-layer OSI model of computer networking, the physical layer or layer 1 is the first and l...
Definition: oc_net.h:335
Network interface layer - sublayer of the Network layer.
Definition: oc_net.h:351
static oC_ErrorCode_t oC_Net_AddressToString(const oC_Net_Address_t *Address, char *outString, oC_MemorySize_t Size)
prints IP to the string
Definition: oc_net.h:807
uint16_t Size
Size Data in frame.
Definition: oc_net.h:323
uint32_t FlowLabel
Originally created for tracking, now it&#39;s only a hint for routers and switches.
Definition: oc_net.h:253
static uint32_t oC_Net_ConvertUint32FromNetworkEndianess(uint32_t v)
converts uint32_t from network byte order
Definition: oc_net.h:590
oC_Net_HardwareType_t HardwareType
Type of the hardware device.
Definition: oc_net.h:516
oC_Timestamp_t IpExpiredTimestamp
Timestamp of IP expiration.
Definition: oc_net.h:483
stores informations about the frame to send
Definition: oc_net.h:314
static bool oC_Net_ConvertHeaderFromNetworkEndianess(oC_Net_Packet_t *Packet)
converts header from network endianess
Definition: oc_net.h:1152
uint32_t IHL
4 bits, that contain Internet Header Length, which is the length of the header in multiples of 4 (num...
Definition: oc_net.h:220
uint32_t NextHeader
Specifies the type of the next header in the packet (TCP/UDP,etc)
Definition: oc_net.h:255
static void oC_Net_Packet_SetData(oC_Net_Packet_t *Packet, const void *Data, uint16_t Size)
sets payload in the packet
Definition: oc_net.h:1485
MAC may refer to the sublayer that determines who is allowed to access the media at any one time (e...
Definition: oc_net.h:349
uint32_t oC_Net_InterfaceIndex_t
stores index of the interface
Definition: oc_net.h:374
In the seven-layer OSI model of computer networking, the session layer is layer 5.
Definition: oc_net.h:339
oC_Net_Ipv6_t IPv6
Address in IPv6 version.
Definition: oc_net.h:447
static const char * oC_Net_GetProtocolName(oC_Net_Protocol_t Protocol)
returns user friendly name of the protocol
Definition: oc_net.h:689
struct PACKED oC_Net_Ipv6_t
stores IPv6 address
oC_MemorySize_t ReceivedBytes
Number of received bytes.
Definition: oc_net.h:515
Internet Control Message Protocol.
Definition: oc_net.h:157
stores hardware address
Definition: oc_net.h:114
Frame stores IPv6 packet.
Definition: oc_net.h:192
static bool oC_Net_IsAddressCorrect(const oC_Net_Address_t *Address)
returns true if the given address is correct
Definition: oc_net.h:918
static void oC_Net_ConvertBufferToNetworkEndianess(void *Buffer, uint16_t Size)
converts buffer to network endianess
Definition: oc_net.h:601
oC_Net_Ipv6Info_t IPv6
IPv6 informations.
Definition: oc_net.h:526
uint64_t LowPart
Low 8 bytes of the IPv6 address.
Definition: oc_net.h:144
oC_Net_HardwareAddress_t Source
Hardware Source Address.
Definition: oc_net.h:317
static uint32_t oC_Net_ConvertUint32ToNetworkEndianess(uint32_t v)
converts uint32_t to network byte order
Definition: oc_net.h:562
static uint16_t oC_Net_GetPacketSize(const oC_Net_Packet_t *Packet, bool WithHeader)
returns size of the packet
Definition: oc_net.h:1076
static const char * oC_Net_GetHardwareTypeName(oC_Net_HardwareType_t Type)
returns name of the hardware type
Definition: oc_net.h:1240
uint32_t TTL
Time to live - number of hops the packet is allowed to pass before it dies.
Definition: oc_net.h:228
Open Shortest Path First.
Definition: oc_net.h:162
uint8_t Data[IPv4_MAXIMUM_DATA_LENGTH]
Data of the packet.
Definition: oc_net.h:277
IP address in version 4.
Definition: oc_net.h:429
In computer networking, the transport layer is a conceptual division of methods in the layered archit...
Definition: oc_net.h:338
oC_Net_Ipv6_t Source
Source IP address.
Definition: oc_net.h:257
oC_Net_Ipv4Packet_t IPv4
Packet in format IPv4.
Definition: oc_net.h:304
oC_Net_Packet_t * Packet
Pointer to the packet to send.
Definition: oc_net.h:320
Static array definitions.
oC_Net_FrameType_t FrameType
Type of the frame.
Definition: oc_net.h:324
uint32_t Checksum
Header checksum - number used for errors detection.
Definition: oc_net.h:230
static int oC_Net_PrepareInterfaceName(oC_Net_InterfaceName_t *outName, const char *Prefix, oC_Net_InterfaceIndex_t Index)
prepares interface name string
Definition: oc_net.h:659
The file with memory manager interface.
static oC_MemorySize_t oC_Net_GetHardwareAddressSize(oC_Net_HardwareType_t Type)
returns size of the hardware address (in bytes)
Definition: oc_net.h:1199
Frame stores IPv4 packet.
Definition: oc_net.h:191
uint32_t NumberOfElementsInTxQueue
Number of elements in queue to send.
Definition: oc_net.h:405
Network interface is DOWN (cable is not connected)
Definition: oc_net.h:363
static void * oC_Net_Packet_GetDataReference(oC_Net_Packet_t *Packet)
returns reference to the payload in the packet
Definition: oc_net.h:1419
uint32_t Version
4 bits, that contain the version, that specified if it&#39;s an IPv4 or IPv6 packet
Definition: oc_net.h:219
static bool oC_Net_IsMainLayer(oC_Net_Layer_t Layer, oC_Net_Layer_t MainLayer)
returns true if Layer is part of the MainLayer
Definition: oc_net.h:1353
The file with interface for string library.
uint32_t Protocol
Contains selected protocol (TCP/UDP/ICMP,etc).
Definition: oc_net.h:229
struct PACKED oC_Net_Ipv4PacketHeader_t
contains IPv4 packet header
Stream Control Transmission Protocol.
Definition: oc_net.h:163
uint32_t MF
More Fragments - whether more fragments of a packet follow
Definition: oc_net.h:225
static uint16_t oC_Net_CalculateChecksum(const void *Buffer, uint16_t Size, uint16_t InitialValue, bool ConvertEndianess, bool PrepareChecksumToSend)
Definition: oc_net.h:1581
oC_Net_Ipv6_t IPv6
IP of the network interface.
Definition: oc_net.h:497
static const oC_Allocator_t Allocator
Definition: oc_eth.c:152
static bool oC_Net_Packet_Delete(oC_Net_Packet_t **Packet)
releases memory allocated for a packet
Definition: oc_net.h:1561
oC_Net_QueueStatus_t QueueStatus
Current status of network interface&#39;s queue.
Definition: oc_net.h:521
oC_Net_Ipv4_t GatewayIP
Default gateway IP.
Definition: oc_net.h:476
oC_Net_Ipv4_t DestinationIp
Destination IP address.
Definition: oc_net.h:232
struct PACKED oC_Net_Ipv4Packet_t
stores IPv4 packet
static uint16_t oC_Net_Packet_CalculateChecksum(oC_Net_Packet_t *Packet)
calculates checksum for a packet (it only calculates it and does not fill it!)
Definition: oc_net.h:1639
static oC_ErrorCode_t oC_Net_Ipv4AddressToString(oC_Net_Ipv4_t Address, char *outString, oC_MemorySize_t Size)
prints IPv4 address to the string
Definition: oc_net.h:772
uint16_t oC_Net_Port_t
Port of the network address.
Definition: oc_net.h:417
static oC_ErrorCode_t oC_Net_AddressFromString(const char *Address, oC_Net_Address_t *outAddress)
reads IP address from the string
Definition: oc_net.h:847
uint32_t TrafficClass
The bits of this field hold two values.
Definition: oc_net.h:252
oC_Net_Ipv4_t IP
IP of the network interface.
Definition: oc_net.h:471
Network interface is UP (cable is connected)
Definition: oc_net.h:362
stores network interface IPv4 informations
Definition: oc_net.h:469
oC_Net_HardwareAddress_t HardwareBroadcastAddress
Hardware address that should be used for broadcast.
Definition: oc_net.h:518
stores network interface IPv6 informations
Definition: oc_net.h:495
oC_Net_LinkStatus_t
stores network interface link status
Definition: oc_net.h:360
oC_Net_AddressType_t Type
Type of the address stored inside.
Definition: oc_net.h:443
oC_Net_Ipv6Packet_t IPv6
Packet in format IPv6.
Definition: oc_net.h:305
struct PACKED oC_Net_Ipv6PacketHeader_t
contains IPv6 packet header
Address resolution packet.
Definition: oc_net.h:193
IP address in version 6.
Definition: oc_net.h:430
oC_Net_FrameType_t
stores type of the frame
Definition: oc_net.h:189
oC_Net_Port_t Port
Port of the address.
Definition: oc_net.h:450
oC_Net_Ipv6_t Destination
Destination IP address.
Definition: oc_net.h:258
oC_Net_Ipv4_t DhcpIP
IP of the local DHCP server.
Definition: oc_net.h:474
oC_Net_HardwareAddress_t HardwareRouterAddress
HW address of the local router.
Definition: oc_net.h:484
The file with standard input/output operations.
stores IPv6 address
Definition: oc_net.h:142
#define NULL
pointer to a zero
Definition: oc_null.h:37
oC_Net_Layer_t
stores layer of the OSI model
Definition: oc_net.h:333