38 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 53 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 60 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 66 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 73 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 82 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 89 #define _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ 111 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
113 oC_IntMan_EnterCriticalSection();
118 ReservedTypes = oC_List_New(&Allocator, AllocationFlags_Default);
119 TypeReleased = oC_Event_New( oC_Event_State_Inactive, &Allocator, AllocationFlags_Default );
120 ModuleBusy = oC_Mutex_New( oC_Mutex_Type_Normal , &Allocator, AllocationFlags_Default);
123 ErrorCondition( ReservedTypes !=
NULL , oC_ErrorCode_AllocationError )
124 && ErrorCondition( ModuleBusy !=
NULL , oC_ErrorCode_AllocationError )
125 && ErrorCondition( TypeReleased !=
NULL , oC_ErrorCode_AllocationError )
129 errorCode = oC_ErrorCode_None;
133 bool listDeleted = oC_List_Delete( TypeReleased , AllocationFlags_Default);
134 bool mutexDeleted = oC_Mutex_Delete(&ModuleBusy , AllocationFlags_Default);
135 bool eventDeleted = oC_Event_Delete(&TypeReleased , AllocationFlags_Default);
137 oC_SaveIfFalse(
"ICMP:TurnOn - Cannot delete list " , listDeleted , oC_ErrorCode_ReleaseError );
138 oC_SaveIfFalse(
"ICMP:TurnOn - Cannot delete mutex " , mutexDeleted , oC_ErrorCode_ReleaseError );
139 oC_SaveIfFalse(
"ICMP:TurnOn - Cannot delete event" , eventDeleted , oC_ErrorCode_ReleaseError );
142 oC_IntMan_ExitCriticalSection();
167 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
169 oC_IntMan_EnterCriticalSection();
176 bool allPacketsReleased =
true;
178 bool listDeleted = oC_List_Delete ( ReservedTypes , AllocationFlags_Default );
179 bool eventDeleted = oC_Event_Delete( &TypeReleased , AllocationFlags_Default );
180 bool mutexDeleted = oC_Mutex_Delete( &ModuleBusy , AllocationFlags_Default );
183 ErrorCondition( listDeleted , oC_ErrorCode_ReleaseError )
184 && ErrorCondition( allPacketsReleased , oC_ErrorCode_ReleaseError )
185 && ErrorCondition( eventDeleted , oC_ErrorCode_ReleaseError )
186 && ErrorCondition( mutexDeleted , oC_ErrorCode_ReleaseError )
190 errorCode = oC_ErrorCode_None;
193 oC_IntMan_ExitCriticalSection();
213 bool reserved =
false;
217 && oC_SaveIfFalse(
"UDP::IsPortReserved - ", Type != oC_Icmp_Type_Empty , oC_ErrorCode_TypeNotCorrect )
218 && oC_SaveIfFalse(
"UDP::IsPortReserved - ", Process ==
NULL || oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect )
222 reserved = reservation !=
NULL;
254 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
258 && ErrorCondition( Type >= 0 , oC_ErrorCode_TypeNotCorrect )
259 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
262 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
264 while(oC_KTime_GetTimestamp() <= endTimestamp)
266 if( ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy ) )
272 if(ErrorCondition( reservation !=
NULL , oC_ErrorCode_AllocationError ))
274 reservation->Busy = oC_Mutex_New( oC_Mutex_Type_Normal , &Allocator, AllocationFlags_Default );
276 if( ErrorCondition( reservation->Busy !=
NULL , oC_ErrorCode_AllocationError ) )
278 reservation->Type = Type;
279 reservation->Process = getcurprocess();
281 bool addedToList = oC_List_PushBack(ReservedTypes, reservation, &Allocator);
283 if( ErrorCondition( addedToList, oC_ErrorCode_CannotAddObjectToList ) )
285 errorCode = oC_ErrorCode_None;
289 oC_SaveIfFalse(
"ICMP:Reserve - cannot release reservation memory ", kfree(reservation, AllocationFlags_Default), oC_ErrorCode_ReleaseError);
294 oC_SaveIfFalse(
"ICMP:Reserve - cannot release reservation memory ", kfree(reservation, AllocationFlags_Default), oC_ErrorCode_ReleaseError);
299 oC_Mutex_Give(ModuleBusy);
304 oC_Mutex_Give(ModuleBusy);
306 if(
oC_Event_WaitForState(TypeReleased,Type,oC_Event_StateMask_Full, oC_KTime_CalculateTimeout(endTimestamp)) ==
false)
308 errorCode = oC_ErrorCode_Timeout;
349 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
350 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
354 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
355 && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleBusy )
361 ErrorCondition( reservation !=
NULL , oC_ErrorCode_TypeNotReserved )
362 && ErrorCondition( reservation->Process == getcurprocess() , oC_ErrorCode_TypeReservedByDifferentProcess )
363 && ErrorCondition( oC_Mutex_Take(reservation->Busy,oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_TypeBusy )
366 bool mutexDeleted = oC_Mutex_Delete(&reservation->Busy, AllocationFlags_Default);
367 bool deletedFromList = oC_List_RemoveAll(ReservedTypes,reservation);
368 bool deleted = kfree(reservation,AllocationFlags_Default);
371 ErrorCondition( mutexDeleted , oC_ErrorCode_CannotDeleteObject )
372 && ErrorCondition( deletedFromList , oC_ErrorCode_CannotRemoveObjectFromList )
373 && ErrorCondition( deleted , oC_ErrorCode_ReleaseError )
376 oC_Event_SetState(TypeReleased,Type);
377 errorCode = oC_ErrorCode_None;
381 oC_Mutex_Give(ModuleBusy);
416 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
417 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
421 && ErrorCondition( getcuruser() == getrootuser() , oC_ErrorCode_PossibleOnlyForRoot )
422 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
423 && ErrorCondition( oC_Mutex_Take(ModuleBusy,Timeout) , oC_ErrorCode_ModuleBusy )
428 errorCode = oC_ErrorCode_None;
432 && ErrorCondition( oC_KTime_GetTimestamp() <= endTimestamp , oC_ErrorCode_Timeout )
435 reservation->Process = getcurprocess();
437 ErrorCode(
oC_Icmp_ReleaseType( reservation->Type, oC_KTime_CalculateTimeout(endTimestamp) ) );
442 oC_Mutex_Give(ModuleBusy);
471 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
475 && ErrorCondition( isram(Packet) , oC_ErrorCode_AddressNotInRam )
476 && ErrorCondition( isram(outChecksum) , oC_ErrorCode_OutputAddressNotInRAM )
481 if( ErrorCondition(isram(datagram), oC_ErrorCode_PacketNotCorrect) )
484 errorCode = oC_ErrorCode_None;
519 oC_SaveIfFalse(
"Icmp::PacketNew: " ,
oC_Module_IsTurnedOn(oC_Module_Icmp) , oC_ErrorCode_ModuleNotStartedYet )
522 && oC_SaveIfFalse(
"Icmp::PacketNew: Size is not correct - " , Size > 0 , oC_ErrorCode_SizeNotCorrect )
523 && oC_SaveIfFalse(
"Icmp::PacketNew: Data address incorrect - " , Data ==
NULL || isaddresscorrect(Data) , oC_ErrorCode_WrongAddress )
524 && oC_SaveIfFalse(
"Icmp::PacketNew: Allocator incorrect - " , isaddresscorrect(PacketAllocator) , oC_ErrorCode_AllocatorNotCorrect )
529 if(oC_SaveIfFalse(
"Icmp::PacketNew: Cannot allocate memory for a packet - ", packet !=
NULL , oC_ErrorCode_AllocationError))
538 void * dataDestination = datagram;
542 memcpy(dataDestination,Data,Size);
557 bool success =
false;
561 oC_SaveIfFalse(
"ICMP::SetSize - " , isram(Packet) , oC_ErrorCode_AddressNotInRam )
562 && oC_SaveIfFalse(
"ICMP::SetSize - " , Size > 0 , oC_ErrorCode_SizeNotCorrect )
563 && oC_SaveIfFalse(
"ICMP::SetSize - " , packetSize > Size , oC_ErrorCode_SizeTooBig )
579 bool success =
false;
581 if(isram(Packet) && isram(*Packet))
583 success = kfree(*Packet,AllocationFlags_Default);
599 if(isaddresscorrect(datagram))
601 data = &datagram->Message;
644 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
645 oC_Timestamp_t timestamp = oC_KTime_GetTimestamp();
646 oC_Timestamp_t endTimestamp = timestamp + Timeout;
652 && ErrorCondition( isaddresscorrect(Destination) , oC_ErrorCode_WrongAddress )
654 && ErrorCondition( isram(Packet) , oC_ErrorCode_AddressNotInRam )
655 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
665 ErrorCondition(
oC_Netif_IsCorrect(Netif) , oC_ErrorCode_CannotFindNetifForTheAddress )
666 && ErrorCondition( datagram !=
NULL , oC_ErrorCode_PacketNotCorrect )
667 && ErrorCondition( ((
oC_Net_PacketType_t)addressType) == packetType , oC_ErrorCode_TypeNotCorrect )
690 oC_SaveError(
"UDP::Send - Function is not implemented for IPv6!", oC_ErrorCode_NotImplemented);
744 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
745 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
751 && ErrorCondition( isram(outPacket) , oC_ErrorCode_OutputAddressNotInRAM )
752 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
753 && ErrorCondition( oC_Mutex_Take(ModuleBusy,oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
756 oC_Mutex_Give(ModuleBusy);
761 ErrorCondition( reservation !=
NULL , oC_ErrorCode_TypeNotReserved )
762 && ErrorCondition( reservation->Process == getcurprocess() , oC_ErrorCode_TypeReservedByDifferentProcess )
763 && ErrorCondition( oC_Mutex_Take( reservation->Busy , oC_KTime_CalculateTimeout(endTimestamp) ) , oC_ErrorCode_TypeBusy )
769 memset(&addressFilter, 0 ,
sizeof(addressFilter));
775 errorCode = oC_ErrorCode_None;
778 oC_Mutex_Give(reservation->Busy);
787 #undef _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ 794 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 805 foreach(ReservedTypes,reservation)
808 (reservation->Type == Type && Type != oC_Icmp_Type_Empty )
809 || (reservation->Process == Process && Process !=
NULL )
812 foundReservation = reservation;
816 return foundReservation;
826 uint32_t checksum = 0;
837 return (uint16_t)checksum;
847 bool expected =
false;
849 if(isram(ReceivedPacket) && isaddresscorrect(ExpectedType))
857 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ oC_Net_Ipv4_t SourceIp
Source IP address.
static bool oC_Net_Packet_SetSize(oC_Net_Packet_t *Packet, uint16_t Size)
sets size of the packet
oC_ErrorCode_t oC_Icmp_Send(oC_Netif_t Netif, const oC_Net_Address_t *Destination, oC_Icmp_Packet_t *Packet, oC_Time_t Timeout)
sends ICMP packet
bool oC_Event_WaitForState(oC_Event_t Event, oC_Event_State_t State, oC_Event_StateMask_t StateMask, oC_Time_t Timeout)
bool oC_Icmp_IsTypeReserved(oC_Icmp_Type_t Type, oC_Process_t Process)
checks if the given ICMP type is reserved
uint32_t QoS
Quality of Service or Type of Service - describes the priority of the packet
oC_Net_Protocol_t Protocol
IP Protocol.
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
static ReservationData_t * FindReservation(oC_Icmp_Type_t Type, oC_Process_t Process)
finds reservation of port or process
oC_Net_Ipv4PacketHeader_t Header
Header of the IPv4 packet.
identifier for allocations
uint32_t DF
Don't Fragment - packet can be fragmented or not
bool oC_MemMan_FreeAllMemoryOfAllocator(Allocator_t Allocator)
release all memory of allocator
oC_ErrorCode_t oC_NetifMan_ReceivePacket(const oC_Net_Address_t *Address, oC_Net_Packet_t *outPacket, oC_Time_t Timeout, oC_Netif_t *outNetif, oC_NetifMan_PacketFilterFunction_t FilterFunction, const void *Parameter)
receives packet from the network
oC_Net_Packet_t Packet
IP packet.
stores ICMP packet (datagram + IP header)
oC_Icmp_Type_t
stores ICMP message type
Stores interface of netif manager module.
oC_Net_AddressType_t
stores type of the network address
static bool PacketFilterFunction(oC_Icmp_Packet_t *ReceivedPacket, oC_Icmp_Type_t *ExpectedType, oC_Netif_t Netif)
checks if the packet that we received is that we expect
void * oC_Icmp_Packet_GetMessageReference(oC_Icmp_Packet_t *Packet)
returns reference to the message inside ICMP packet
oC_Net_PacketType_t
stores type of the packet
oC_Icmp_Datagram_t IcmpDatagram
ICMP data in the IPv4 packet.
bool oC_Netif_IsCorrect(oC_Netif_t Netif)
checks if the Netif object is correct
static oC_Net_PacketType_t oC_Net_Packet_GetType(const oC_Net_Packet_t *Packet)
returns type of the packet
oC_ErrorCode_t oC_Icmp_Receive(oC_Netif_t Netif, oC_Icmp_Packet_t *outPacket, oC_Icmp_Type_t Type, oC_Time_t Timeout)
receives ICMP packet
oC_ErrorCode_t oC_Icmp_CalculateChecksum(oC_Icmp_Packet_t *Packet, uint16_t *outChecksum)
Calculates checksum for ICMP packet.
The file with interface for the module library.
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
bool oC_Icmp_Packet_Delete(oC_Icmp_Packet_t **Packet)
release memory allocated for a ICMP packet
oC_Net_Ipv4_t oC_Netif_GetIpv4Address(oC_Netif_t Netif)
returns address of the network interface
uint32_t FragmentOffset
Field to identify position of fragment within original packet.
oC_Icmp_Packet_t * oC_Icmp_Packet_New(Allocator_t PacketAllocator, oC_Net_PacketType_t Type, oC_Icmp_Type_t IcmpType, uint8_t Code, const void *Data, uint16_t Size)
allocates memory for a ICMP packet
oC_Net_Ipv4_t IPv4
Address in IPv4 version.
uint32_t ID
ID value for reconstruction the packet from segments.
oC_ErrorCode_t oC_Icmp_ReserveType(oC_Icmp_Type_t Type, oC_Time_t Timeout)
reserves a ICMP type
The file with interface for interrupt manager.
bool(* oC_NetifMan_PacketFilterFunction_t)(oC_Net_Packet_t *ReceivedPacket, const void *Parameter, oC_Netif_t Netif)
stores pointer to the function to filter packets
uint32_t IHL
4 bits, that contain Internet Header Length, which is the length of the header in multiples of 4 (num...
oC_Net_Ipv6_t IPv6
Address in IPv6 version.
Internet Control Message Protocol.
static bool oC_Net_IsAddressCorrect(const oC_Net_Address_t *Address)
returns true if the given address is correct
The file with interface for mutex managing.
static uint16_t oC_Net_GetPacketSize(const oC_Net_Packet_t *Packet, bool WithHeader)
returns size of the packet
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
oC_Icmp_Header_t Header
Header of the ICMP packet.
uint32_t TTL
Time to live - number of hops the packet is allowed to pass before it dies.
oC_Net_Ipv6_t Source
Source IP address.
oC_Net_Ipv4Packet_t IPv4
Packet in format IPv4.
oC_ErrorCode_t oC_Icmp_TurnOff(void)
turns off ICMP module
uint32_t Checksum
Header checksum - number used for errors detection.
oC_Netif_t oC_NetifMan_GetNetifForAddress(const oC_Net_Address_t *Destination)
returns network interface according to destination address
static void * oC_Net_Packet_GetDataReference(oC_Net_Packet_t *Packet)
returns reference to the payload in the packet
uint32_t Protocol
Contains selected protocol (TCP/UDP/ICMP,etc).
oC_ErrorCode_t oC_Icmp_TurnOn(void)
turns on ICMP module
uint32_t MF
More Fragments - whether more fragments of a packet follow
static uint16_t oC_Net_CalculateChecksum(const void *Buffer, uint16_t Size, uint16_t InitialValue, bool ConvertEndianess, bool PrepareChecksumToSend)
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
static const oC_Allocator_t Allocator
static uint16_t CalculateChecksum(oC_Icmp_Packet_t *Packet)
calculates checksum for a packet
File with interface for ICMP.
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
oC_ErrorCode_t oC_Netif_SendPacket(oC_Netif_t Netif, oC_Net_Packet_t *Packet, oC_Time_t Timeout)
sends packet by using the network interface
oC_Net_Ipv4_t DestinationIp
Destination IP address.
bool oC_Netif_ReadIpv6Address(oC_Netif_t Netif, oC_Net_Ipv6_t *outAddress)
reads IPv6 address of the network interface
oC_ErrorCode_t oC_Icmp_ReleaseAllTypesReservedBy(oC_Process_t Process, oC_Time_t Timeout)
releases all types reserved by the process
oC_Net_AddressType_t Type
Type of the address stored inside.
oC_Net_Ipv6Packet_t IPv6
Packet in format IPv6.
oC_Net_Ipv6_t Destination
Destination IP address.
oC_ErrorCode_t oC_Icmp_ReleaseType(oC_Icmp_Type_t Type, oC_Time_t Timeout)
releases a type
bool oC_Icmp_Packet_SetSize(oC_Icmp_Packet_t *Packet, uint16_t Size)
sets size of the packet
#define NULL
pointer to a zero
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off