Choco OS  V.0.16.9.0
Join to the chocolate world
oc_tcp_packet.c
Go to the documentation of this file.
1 
27 #include <oc_tcp.h>
28 
34 #define _________________________________________MACROS_SECTION_____________________________________________________________________________
35 
36 #define IsPacketTypeCorrect( PacketType ) ( (PacketType) == oC_Net_PacketType_IPv4 || (PacketType) == oC_Net_PacketType_IPv6 )
37 
38 #undef _________________________________________MACROS_SECTION_____________________________________________________________________________
39 
40 
46 #define _________________________________________TYPES_SECTION______________________________________________________________________________
47 
48 typedef struct
49 {
50  oC_Tcp_OptionKind_t Kind;
51  uint8_t Length;
52  uint8_t Data[4];
53 } Option_t;
54 
55 #undef _________________________________________TYPES_SECTION______________________________________________________________________________
56 
62 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
63 
64 static Option_t * GetNextOption ( Option_t * Option );
65 static Option_t * GetFirstOption ( oC_Tcp_Packet_t * Packet );
66 static Option_t * GetLastOption ( oC_Tcp_Packet_t * Packet );
67 static Option_t * GetOption ( oC_Tcp_Packet_t * Packet , oC_Tcp_OptionKind_t OptionKind );
68 static uint16_t CalculateChecksum ( oC_Tcp_Packet_t * Packet );
69 
70 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
71 
72 
78 #define _________________________________________INTERFACE_SECTION__________________________________________________________________________
79 
80 //==========================================================================================================================================
97 //==========================================================================================================================================
98 oC_Tcp_Packet_t * oC_Tcp_Packet_New( const oC_Net_Address_t * Source, const oC_Net_Address_t * Destination, uint16_t HeaderSize , const void * Data , uint16_t Size )
99 {
100  oC_Tcp_Packet_t * packet = NULL;
101  uint16_t packetSize = HeaderSize + Size + ( sizeof(uint32_t) - (HeaderSize % sizeof(uint32_t)));
102 
103  if(
104  oC_SaveIfFalse("TCP::Packet::New ", Data == NULL || isaddresscorrect(Data) , oC_ErrorCode_WrongAddress )
105  && oC_SaveIfFalse("TCP::Packet::New ", Data == NULL || Size > 0 , oC_ErrorCode_SizeNotCorrect )
106  && oC_SaveIfFalse("TCP::Packet::New ", isaddresscorrect(Source) , oC_ErrorCode_WrongAddress )
107  && oC_SaveIfFalse("TCP::Packet::New ", isaddresscorrect(Destination) , oC_ErrorCode_WrongAddress )
108  && oC_SaveIfFalse("TCP::Packet::New ", oC_Net_IsAddressCorrect(Source) , oC_ErrorCode_IpAddressNotCorrect )
109  && oC_SaveIfFalse("TCP::Packet::New ", oC_Net_IsAddressCorrect(Destination) , oC_ErrorCode_IpAddressNotCorrect )
110  && oC_SaveIfFalse("TCP::Packet::New ", Source->Type == Destination->Type , oC_ErrorCode_TypeNotCorrect )
111  && oC_SaveIfFalse("TCP::Packet::New ", packetSize > Size && packetSize >= HeaderSize , oC_ErrorCode_SizeTooBig )
112  && oC_SaveIfFalse("TCP::Packet::New ", HeaderSize >= sizeof(oC_Tcp_Header_t) , oC_ErrorCode_SizeNotCorrect )
113  )
114  {
115  packet = (oC_Tcp_Packet_t*)oC_Net_Packet_New( getcurallocator(), (oC_Net_PacketType_t)Source->Type, NULL, packetSize );
116 
117  if(oC_SaveIfFalse("TCP::Packet::New ", packet != NULL, oC_ErrorCode_AllocationError))
118  {
119  if(Source->Type == oC_Net_AddressType_IPv4)
120  {
121  packet->IPv4.IpHeader.Checksum = 0;
122  packet->IPv4.IpHeader.DF = 0;
123  packet->IPv4.IpHeader.FragmentOffset = 0;
124  packet->IPv4.IpHeader.IHL = 5;
125  packet->IPv4.IpHeader.SourceIp = Source->IPv4;
126  packet->IPv4.IpHeader.DestinationIp = Destination->IPv4;
127  packet->IPv4.IpHeader.Protocol = oC_Net_Protocol_TCP;
128 
129  packet->IPv4.TcpHeader.SourcePort = Source->Port;
130  packet->IPv4.TcpHeader.DestinationPort = Destination->Port;
131  packet->IPv4.TcpHeader.DataOffset = HeaderSize / sizeof(uint32_t) + ( HeaderSize % sizeof(uint32_t) != 0 ? 1 : 0 );
132 
133  if(Data != NULL)
134  {
135  memcpy( packet->IPv4.Data, Data , Size);
136  }
137  }
138  else
139  {
140  packet->IPv6.IpHeader.Source.HighPart = Source->IPv6.HighPart;
141  packet->IPv6.IpHeader.Source.LowPart = Source->IPv6.LowPart;
142 
143  packet->IPv6.IpHeader.Destination.HighPart = Destination->IPv6.HighPart;
144  packet->IPv6.IpHeader.Destination.LowPart = Destination->IPv6.LowPart;
145 
146  if(Data != NULL)
147  {
148  memcpy( packet->IPv6.Data, Data , Size);
149  }
150  }
151  }
152  }
153 
154  return packet;
155 }
156 
157 //==========================================================================================================================================
169 //==========================================================================================================================================
171 {
172  bool deleted = false;
173 
174  if(
175  oC_SaveIfFalse("TCP::Packet::Delete ", isram(Packet) , oC_ErrorCode_OutputAddressNotInRAM )
176  && oC_SaveIfFalse("TCP::Packet::Delete ", isram(*Packet) , oC_ErrorCode_AddressNotInRam )
177  )
178  {
179  deleted = oC_Net_Packet_Delete((oC_Net_Packet_t**)Packet);
180  }
181 
182  return deleted;
183 }
184 
185 //==========================================================================================================================================
189 //==========================================================================================================================================
190 oC_ErrorCode_t oC_Tcp_Packet_SetData( oC_Tcp_Packet_t * Packet , const void * Data , uint16_t Size )
191 {
192  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
193 
194  if(
195  ErrorCondition( isram(Packet) , oC_ErrorCode_AddressNotInRam )
196  && ErrorCondition( isaddresscorrect(Data) , oC_ErrorCode_WrongAddress )
197  )
198  {
199  oC_Tcp_Header_t * header = oC_Net_Packet_GetDataReference(&Packet->Packet);
200 
201  if( ErrorCondition( header != NULL, oC_ErrorCode_PacketNotCorrect ) )
202  {
203  oC_Net_Packet_SetSize(&Packet->Packet,Size + (header->DataOffset * sizeof(uint32_t)));
204 
205  void * data = NULL;
206  uint32_t * tcpPacketArray = (uint32_t*)header;
207 
208  data = &tcpPacketArray[header->DataOffset];
209 
210  memcpy(data, Data, Size);
211 
212  errorCode = oC_ErrorCode_None;
213  }
214  }
215 
216  return errorCode;
217 }
218 
219 //==========================================================================================================================================
223 //==========================================================================================================================================
224 oC_ErrorCode_t oC_Tcp_Packet_ReadData( oC_Tcp_Packet_t * Packet , void * outData , oC_MemorySize_t Size )
225 {
226  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
227 
228  if(
229  ErrorCondition( isram(Packet) , oC_ErrorCode_AddressNotInRam )
230  && ErrorCondition( isram(outData) , oC_ErrorCode_OutputAddressNotInRAM )
231  )
232  {
233  oC_Tcp_Header_t * header = oC_Net_Packet_GetDataReference(&Packet->Packet);
234 
235  if( ErrorCondition( header != NULL, oC_ErrorCode_PacketNotCorrect ) )
236  {
237  uint32_t dataSize = oC_Tcp_Packet_GetDataSize(Packet);
238  void * data = NULL;
239  uint32_t * tcpPacketArray = (uint32_t*)header;
240 
241  data = &tcpPacketArray[header->DataOffset];
242 
243  if(ErrorCondition( Size >= dataSize , oC_ErrorCode_OutputBufferTooSmall ))
244  {
245  memcpy(outData, data, dataSize);
246  errorCode = oC_ErrorCode_None;
247  }
248  }
249  }
250 
251  return errorCode;
252 }
253 
254 //==========================================================================================================================================
258 //==========================================================================================================================================
259 void oC_Tcp_Packet_SetSize( oC_Tcp_Packet_t * Packet , uint16_t Size )
260 {
261  if(isram(Packet))
262  {
263  oC_Tcp_Header_t * header = oC_Net_Packet_GetDataReference(&Packet->Packet);
264 
265  if( header != NULL )
266  {
267  oC_Net_Packet_SetSize(&Packet->Packet,Size + (header->DataOffset * sizeof(uint32_t)));
268  }
269  }
270 }
271 
272 //==========================================================================================================================================
282 //==========================================================================================================================================
284 {
285  void * data = NULL;
286 
287  if(oC_SaveIfFalse("TCP::Packet::GetDataRef " , isram(Packet) , oC_ErrorCode_AddressNotInRam ))
288  {
289  uint16_t packetSize = oC_Net_GetPacketSize(&Packet->Packet,false);
290  oC_Tcp_Header_t * header = oC_Net_Packet_GetDataReference(&Packet->Packet);
291 
292  if(
293  oC_SaveIfFalse("TCP::Packet::GetDataRef ", header != NULL , oC_ErrorCode_PacketNotCorrect )
294  && oC_SaveIfFalse("TCP::Packet::GetDataRef ", (packetSize > (header->DataOffset * sizeof(uint32_t))) , oC_ErrorCode_SizeNotCorrect )
295  )
296  {
297  uint32_t * tcpPacketArray = (uint32_t*)header;
298 
299  data = &tcpPacketArray[header->DataOffset];
300  }
301  }
302 
303  return data;
304 }
305 
306 //==========================================================================================================================================
316 //==========================================================================================================================================
318 {
319  uint16_t size = 0;
320 
321  if(oC_SaveIfFalse("Reference to the Packet", isram(Packet) , oC_ErrorCode_AddressNotInRam))
322  {
323  uint16_t packetSize = oC_Net_GetPacketSize(&Packet->Packet,false);
324  oC_Tcp_Header_t * header = oC_Net_Packet_GetDataReference(&Packet->Packet);
325 
326  if(
327  oC_SaveIfFalse("header is NULL" , header != NULL , oC_ErrorCode_PacketNotCorrect )
328  && oC_SaveIfFalse("packet size" , (packetSize >= (header->DataOffset * sizeof(uint32_t))) , oC_ErrorCode_SizeNotCorrect )
329  )
330  {
331  size = oC_Net_GetPacketSize(&Packet->Packet, false) - (header->DataOffset * sizeof(uint32_t));
332  }
333  else
334  {
335  kdebuglog(oC_LogType_Error, "TCP::Packet::GetDataSize - header = %p, packetSize = %d Bytes (data offset = %d)", header, packetSize, (header->DataOffset * sizeof(uint32_t)));
336  }
337  }
338  else
339  {
340  kdebuglog(oC_LogType_Error, "TCP::Packet::GetDataSize - Packet = %p", Packet);
341  }
342 
343  return size;
344 }
345 
346 //==========================================================================================================================================
359 //==========================================================================================================================================
360 bool oC_Tcp_Packet_AddOption( oC_Tcp_Packet_t * Packet , oC_Tcp_OptionKind_t OptionKind , const void * Data , uint8_t Size )
361 {
362  bool success = false;
363 
364  if(
365  oC_SaveIfFalse("TCP::Packet::AddOption ", isram(Packet) , oC_ErrorCode_AddressNotInRam )
366  && oC_SaveIfFalse("TCP::Packet::AddOption ", OptionKind <= oC_Tcp_OptionKind_Max , oC_ErrorCode_ValueTooBig )
367  && oC_SaveIfFalse("TCP::Packet::AddOption ", Size == 0 || isaddresscorrect(Data) , oC_ErrorCode_DataNotAvailable )
368  )
369  {
371  uint16_t packetSize = oC_Net_GetPacketSize((oC_Net_Packet_t*)Packet,false);
372  Option_t * lastOption = GetLastOption(Packet);
373 
374  if(
375  oC_SaveIfFalse("TCP::Packet::AddOption ", header != NULL , oC_ErrorCode_PacketNotCorrect )
376  && oC_SaveIfFalse("TCP::Packet::AddOption ", (packetSize > (header->DataOffset * sizeof(uint32_t))) , oC_ErrorCode_SizeNotCorrect )
377  && oC_SaveIfFalse("TCP::Packet::AddOption ", lastOption != NULL , oC_ErrorCode_NoSpaceAvailable )
378  )
379  {
380  lastOption->Kind = OptionKind;
381  lastOption->Length = Size + 2;
382 
383  memcpy(lastOption->Data, Data, Size);
384 
385  success = true;
386  }
387  }
388 
389  return success;
390 }
391 
392 //==========================================================================================================================================
414 //==========================================================================================================================================
415 oC_ErrorCode_t oC_Tcp_Packet_ReadOption( oC_Tcp_Packet_t * Packet , oC_Tcp_OptionKind_t OptionKind , void * outData , uint8_t * Size )
416 {
417  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
418 
419  if(
420  ErrorCondition( isram(Packet) , oC_ErrorCode_AddressNotInRam )
421  && ErrorCondition( OptionKind <= oC_Tcp_OptionKind_Max , oC_ErrorCode_ValueTooBig )
422  && ErrorCondition( isram(Size) , oC_ErrorCode_AddressNotInRam )
423  )
424  {
425  Option_t * option = GetOption(Packet,OptionKind);
426 
427  if(
428  ErrorCondition( option != NULL , oC_ErrorCode_OptionNotFound )
429  && ErrorCondition( option->Length >= 2 , oC_ErrorCode_OptionNotCorrect )
430  && ErrorCondition( option->Length == 2 || isram(outData) , oC_ErrorCode_OutputAddressNotInRAM )
431  )
432  {
433  if(ErrorCondition( *Size >= (option->Length - 2) , oC_ErrorCode_OutputArrayToSmall))
434  {
435  if(option->Length > 2)
436  {
437  memcpy(outData, option->Data, option->Length - 2);
438  }
439  errorCode = oC_ErrorCode_None;
440  }
441  *Size = option->Length;
442  }
443  }
444 
445  return errorCode;
446 }
447 
448 //==========================================================================================================================================
458 //==========================================================================================================================================
460 {
461  bool cleared = false;
462 
463  if(oC_SaveIfFalse("TCP::Packet::ClearOptions " , isram(Packet) , oC_ErrorCode_AddressNotInRam ))
464  {
466  uint16_t packetSize = oC_Net_GetPacketSize((oC_Net_Packet_t*)Packet,false);
467  Option_t * firstOption = GetFirstOption(Packet);
468  void * dataRef = oC_Tcp_Packet_GetDataReference(Packet);
469 
470  if(
471  oC_SaveIfFalse("TCP::Packet::ClearOption ", header != NULL , oC_ErrorCode_PacketNotCorrect )
472  && oC_SaveIfFalse("TCP::Packet::ClearOption ", (packetSize > (header->DataOffset * sizeof(uint32_t))) , oC_ErrorCode_SizeNotCorrect )
473  && oC_SaveIfFalse("TCP::Packet::ClearOption ", firstOption != NULL , oC_ErrorCode_OptionNotFound )
474  )
475  {
476  for(uint8_t * byte = (uint8_t*)firstOption; byte < ((uint8_t*)dataRef) ; byte++)
477  {
478  *byte = 0;
479  }
480 
481  cleared = true;
482  }
483 
484  }
485 
486  return cleared;
487 }
488 
489 //==========================================================================================================================================
493 //==========================================================================================================================================
495 {
496  uint16_t checksum = 0;
497 
498  if(isram(Packet))
499  {
500  checksum = CalculateChecksum(Packet);
501  }
502 
503  return checksum;
504 }
505 
506 //==========================================================================================================================================
510 //==========================================================================================================================================
512 {
514 }
515 
516 //==========================================================================================================================================
520 //==========================================================================================================================================
522 {
523  bool converted = false;
524 
525  if(isram(Packet))
526  {
528 
529  if(isram(header))
530  {
531  header->SourcePort = oC_Net_ConvertUint16FromNetworkEndianess(header->SourcePort);
532  header->DestinationPort = oC_Net_ConvertUint16FromNetworkEndianess(header->DestinationPort);
533  header->SequenceNumber = oC_Net_ConvertUint32FromNetworkEndianess(header->SequenceNumber);
534  header->AcknowledgmentNumber= oC_Net_ConvertUint32FromNetworkEndianess(header->AcknowledgmentNumber);
535  header->WindowSize = oC_Net_ConvertUint16FromNetworkEndianess(header->WindowSize);
536  header->Checksum = oC_Net_ConvertUint16FromNetworkEndianess(header->Checksum);
537  header->UrgentPointer = oC_Net_ConvertUint16FromNetworkEndianess(header->UrgentPointer);
538 
539  uint8_t * array = (uint8_t*)header;
540 
541  array[12] = ((array[12] << 4) & 0xF0) | ((array[12] >> 4) & 0x0F);
542  array[13] = ((array[13] & 0x80) >> 7)
543  | ((array[13] & 0x40) >> 5)
544  | ((array[13] & 0x20) >> 3)
545  | ((array[13] & 0x10) >> 1)
546  | ((array[13] & 0x08) << 1)
547  | ((array[13] & 0x04) << 3)
548  | ((array[13] & 0x02) << 5)
549  | ((array[13] & 0x01) << 7);
550 
551  converted = true;
552  }
553  }
554 
555  return converted;
556 }
557 
558 //==========================================================================================================================================
562 //==========================================================================================================================================
564 {
565  bool converted = false;
566 
567  if(isram(Packet))
568  {
570 
571  if(isram(header))
572  {
573  header->SourcePort = oC_Net_ConvertUint16ToNetworkEndianess(header->SourcePort);
574  header->DestinationPort = oC_Net_ConvertUint16ToNetworkEndianess(header->DestinationPort);
575  header->SequenceNumber = oC_Net_ConvertUint32ToNetworkEndianess(header->SequenceNumber);
576  header->AcknowledgmentNumber= oC_Net_ConvertUint32ToNetworkEndianess(header->AcknowledgmentNumber);
577  header->WindowSize = oC_Net_ConvertUint16ToNetworkEndianess(header->WindowSize);
578  header->Checksum = oC_Net_ConvertUint16ToNetworkEndianess(header->Checksum);
579  header->UrgentPointer = oC_Net_ConvertUint16ToNetworkEndianess(header->UrgentPointer);
580 
581  uint8_t * array = (uint8_t*)header;
582 
583  array[12] = ((array[12] << 4) & 0xF0) | ((array[12] >> 4) & 0x0F);
584  array[13] = ((array[13] & 0x80) >> 7)
585  | ((array[13] & 0x40) >> 5)
586  | ((array[13] & 0x20) >> 3)
587  | ((array[13] & 0x10) >> 1)
588  | ((array[13] & 0x08) << 1)
589  | ((array[13] & 0x04) << 3)
590  | ((array[13] & 0x02) << 5)
591  | ((array[13] & 0x01) << 7);
592 
593  converted = true;
594  }
595  }
596 
597  return converted;
598 }
599 
600 
601 #undef _________________________________________INTERFACE_SECTION__________________________________________________________________________
602 
608 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
609 
610 //==========================================================================================================================================
614 //==========================================================================================================================================
615 static Option_t * GetNextOption( Option_t * Option )
616 {
617  Option_t * nextOption = NULL;
618 
619  if(Option != NULL)
620  {
621  uint8_t * array = (uint8_t*)Option;
622 
623  nextOption = (Option_t*)&array[Option->Length + 2];
624  }
625 
626  return nextOption;
627 }
628 
629 //==========================================================================================================================================
633 //==========================================================================================================================================
635 {
636  Option_t * firstOption = NULL;
637  uint8_t * array = oC_Net_Packet_GetDataReference(&Packet->Packet);
638  void * dataRef = oC_Tcp_Packet_GetDataReference(Packet);
639 
640  if(array != NULL && dataRef != NULL)
641  {
642  firstOption = (Option_t*)&array[sizeof(oC_Tcp_Header_t)];
643 
644  if(firstOption >= ((Option_t*)dataRef))
645  {
646  firstOption = NULL;
647  }
648  }
649 
650  return firstOption;
651 }
652 
653 //==========================================================================================================================================
657 //==========================================================================================================================================
659 {
660  Option_t * lastOption = NULL;
661  Option_t * dataReference = oC_Tcp_Packet_GetDataReference(Packet);
662 
663  if(dataReference != NULL)
664  {
665  lastOption = GetFirstOption(Packet);
666 
667  while(lastOption != NULL && lastOption < dataReference && lastOption->Kind != oC_Tcp_OptionKind_End)
668  {
669  lastOption = GetNextOption(lastOption);
670  }
671 
672  if(lastOption >= dataReference)
673  {
674  lastOption = NULL;
675  }
676  }
677 
678  return lastOption;
679 }
680 
681 //==========================================================================================================================================
685 //==========================================================================================================================================
686 static Option_t * GetOption( oC_Tcp_Packet_t * Packet , oC_Tcp_OptionKind_t OptionKind )
687 {
688  Option_t * option = NULL;
689  Option_t * dataReference = oC_Tcp_Packet_GetDataReference(Packet);
690 
691  if(dataReference != NULL)
692  {
693  option = GetFirstOption(Packet);
694 
695  while(option != NULL && option < dataReference && option->Kind != oC_Tcp_OptionKind_End && option->Kind != OptionKind)
696  {
697  option = GetNextOption(option);
698  }
699 
700  /* Checking against NULL is done for prevention against reading option->Kind when option is NULL */
701  if(option >= dataReference || option == NULL || option->Kind != OptionKind)
702  {
703  option = NULL;
704  }
705  }
706 
707  return option;
708 }
709 
710 //==========================================================================================================================================
714 //==========================================================================================================================================
715 static uint16_t CalculateChecksum( oC_Tcp_Packet_t * Packet )
716 {
717  uint32_t checksum = 0;
718  oC_Net_PacketType_t packetType = oC_Net_Packet_GetType(&Packet->Packet);
719  uint16_t savedChecksum = 0;
720 
721  if(packetType == oC_Net_PacketType_IPv4)
722  {
723  savedChecksum = Packet->IPv4.TcpHeader.Checksum;
724 
725  uint16_t tcpPacketSize = oC_Net_GetPacketSize(&Packet->Packet, false);
726  uint32_t sourceAddress = oC_Net_ConvertUint32ToNetworkEndianess(Packet->IPv4.IpHeader.SourceIp);
727  uint32_t destinationAddress = oC_Net_ConvertUint32ToNetworkEndianess(Packet->IPv4.IpHeader.DestinationIp);
729 
730  checksum += (sourceAddress >> 16) & 0xFFFF;
731  checksum += (sourceAddress >> 0) & 0xFFFF;
732 
733  checksum += (destinationAddress >> 16) & 0xFFFF;
734  checksum += (destinationAddress >> 0) & 0xFFFF;
735 
736  checksum += protocol;
737 
738  checksum += oC_Net_ConvertUint16ToNetworkEndianess(tcpPacketSize);
739 
740  Packet->IPv4.TcpHeader.Checksum = 0;
741 
743 
744  while(checksum >> 16)
745  {
746  checksum = (checksum & 0xFFFF) + (checksum >> 16);
747  }
748 
749  checksum = oC_Net_CalculateChecksum(&Packet->IPv4.TcpHeader, tcpPacketSize, checksum, false, true);
750 
752 
753  Packet->IPv4.TcpHeader.Checksum = savedChecksum;
754  }
755 
756  return oC_Net_ConvertUint16FromNetworkEndianess((uint16_t)checksum);
757 }
758 
759 
760 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
761 
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
Packet in type IPv4.
Definition: oc_net.h:177
bool oC_Tcp_Packet_AddOption(oC_Tcp_Packet_t *Packet, oC_Tcp_OptionKind_t OptionKind, const void *Data, uint8_t Size)
Adds TCP option to the packet.
uint64_t HighPart
High 8 bytes of the IPv6 address.
Definition: oc_net.h:145
bool oC_Tcp_Packet_ConvertToNetworkEndianess(oC_Tcp_Packet_t *Packet)
converts packet to network endianess
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
static uint16_t oC_Net_ConvertUint16FromNetworkEndianess(uint16_t v)
converts uint16_t from network byte order
Definition: oc_net.h:579
static Option_t * GetFirstOption(oC_Tcp_Packet_t *Packet)
returns pointer to the first TCP option inside TCP packet
uint32_t DF
Don&#39;t Fragment - packet can be fragmented or not
Definition: oc_net.h:226
bool oC_Tcp_Packet_ConvertFromNetworkEndianess(oC_Tcp_Packet_t *Packet)
converts packet from network endianess
static Option_t * GetNextOption(Option_t *Option)
returns pointer to the next TCP option
uint16_t oC_Tcp_Packet_CalculateChecksum(oC_Tcp_Packet_t *Packet)
calculates checksum for TCP packet
oC_Tcp_Packet_t * oC_Tcp_Packet_New(const oC_Net_Address_t *Source, const oC_Net_Address_t *Destination, uint16_t HeaderSize, const void *Data, uint16_t Size)
Allocates memory for a TCP packet.
Definition: oc_tcp_packet.c:98
stores network address
Definition: oc_net.h:441
oC_Net_PacketType_t
stores type of the packet
Definition: oc_net.h:174
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
Transmission Control Protocol.
Definition: oc_net.h:159
static uint16_t oC_Net_ConvertUint16ToNetworkEndianess(uint16_t v)
converts uint16_t to network byte order
Definition: oc_net.h:545
bool oC_Tcp_Packet_ClearOptions(oC_Tcp_Packet_t *Packet)
clears options in the TCP packet
static Option_t * GetLastOption(oC_Tcp_Packet_t *Packet)
returns pointer to the last TCP option in the packet
uint32_t FragmentOffset
Field to identify position of fragment within original packet.
Definition: oc_net.h:224
stores network packet
Definition: oc_net.h:302
oC_Net_Ipv4_t IPv4
Address in IPv4 version.
Definition: oc_net.h:446
void oC_Tcp_Packet_SetSize(oC_Tcp_Packet_t *Packet, uint16_t Size)
sets size of the TCP packet
static uint32_t oC_Net_ConvertUint32FromNetworkEndianess(uint32_t v)
converts uint32_t from network byte order
Definition: oc_net.h:590
stores TCP header
Definition: oc_tcp.h:348
Option_t
stores options of the telnet
Definition: oc_telnet.c:135
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
oC_ErrorCode_t oC_Tcp_Packet_ReadData(oC_Tcp_Packet_t *Packet, void *outData, oC_MemorySize_t Size)
reads data of the TCP packet
oC_Net_Ipv6_t IPv6
Address in IPv6 version.
Definition: oc_net.h:447
static bool oC_Net_IsAddressCorrect(const oC_Net_Address_t *Address)
returns true if the given address is correct
Definition: oc_net.h:918
uint64_t LowPart
Low 8 bytes of the IPv6 address.
Definition: oc_net.h:144
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
IP address in version 4.
Definition: oc_net.h:429
oC_Net_Ipv6_t Source
Source IP address.
Definition: oc_net.h:257
oC_ErrorCode_t oC_Tcp_Packet_SetData(oC_Tcp_Packet_t *Packet, const void *Data, uint16_t Size)
sets data in the TCP packet
uint32_t Checksum
Header checksum - number used for errors detection.
Definition: oc_net.h:230
static uint16_t CalculateChecksum(oC_Tcp_Packet_t *Packet)
calculates checksum for a TCP packet
oC_Tcp_Header_t * oC_Tcp_Packet_GetHeaderReference(oC_Tcp_Packet_t *Packet)
returns TCP header reference
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 Protocol
Contains selected protocol (TCP/UDP/ICMP,etc).
Definition: oc_net.h:229
static uint16_t oC_Net_CalculateChecksum(const void *Buffer, uint16_t Size, uint16_t InitialValue, bool ConvertEndianess, bool PrepareChecksumToSend)
Definition: oc_net.h:1581
static bool oC_Net_Packet_Delete(oC_Net_Packet_t **Packet)
releases memory allocated for a packet
Definition: oc_net.h:1561
oC_ErrorCode_t oC_Tcp_Packet_ReadOption(oC_Tcp_Packet_t *Packet, oC_Tcp_OptionKind_t OptionKind, void *outData, uint8_t *Size)
reads option from the TCP packet
oC_Net_Ipv4_t DestinationIp
Destination IP address.
Definition: oc_net.h:232
void * oC_Tcp_Packet_GetDataReference(oC_Tcp_Packet_t *Packet)
returns pointer to the data inside the packet
uint16_t oC_Tcp_Packet_GetDataSize(oC_Tcp_Packet_t *Packet)
returns size of data section inside TCP packet
static Option_t * GetOption(oC_Tcp_Packet_t *Packet, oC_Tcp_OptionKind_t OptionKind)
searches for the given option in the TCP packet
bool oC_Tcp_Packet_Delete(oC_Tcp_Packet_t **Packet)
releases memory allocated for a packet
oC_Net_AddressType_t Type
Type of the address stored inside.
Definition: oc_net.h:443
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
#define NULL
pointer to a zero
Definition: oc_null.h:37