39 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 42 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 49 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 57 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 65 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 75 .FirstDynamicPortNumber = oC_Tcp_Port_NumberOfSpecialPorts ,
79 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 87 #define _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ 120 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
125 ErrorCondition(
oC_Module_IsTurnedOn(oC_Module_PortMan) , oC_ErrorCode_RequiredModuleNotEnabled )
126 && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
130 Servers = oC_List_New(&
Allocator, AllocationFlags_Default);
131 Connections = oC_List_New(&
Allocator, AllocationFlags_Default);
132 ModuleBusy = oC_Mutex_New( oC_Mutex_Type_Recursive, &
Allocator, AllocationFlags_Default );
135 ErrorCondition( Servers !=
NULL , oC_ErrorCode_AllocationError )
136 && ErrorCondition( Connections !=
NULL , oC_ErrorCode_AllocationError )
137 && ErrorCondition( ModuleBusy !=
NULL , oC_ErrorCode_AllocationError )
141 errorCode = oC_ErrorCode_None;
145 bool listServerListDeleted = Servers ==
NULL || oC_List_Delete ( Servers , AllocationFlags_Default );
146 bool listConnectionsListDeleted = Connections ==
NULL || oC_List_Delete ( Connections , AllocationFlags_Default );
147 bool mutexDeleted = ModuleBusy ==
NULL || oC_Mutex_Delete( &ModuleBusy , AllocationFlags_Default );
149 oC_SaveIfFalse(
"TCP::TurnOn ", listServerListDeleted && listConnectionsListDeleted && mutexDeleted , oC_ErrorCode_ReleaseError );
187 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
188 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
193 ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
194 && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
195 && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
201 bool allServersDeleted =
true;
202 bool allConnectionsDeleted =
true;
204 foreach(Servers,server)
206 allServersDeleted =
oC_Tcp_Server_Delete( &server, oC_KTime_CalculateTimeout(endTimestamp) ) && allServersDeleted;
209 foreach(Connections,connection)
211 allConnectionsDeleted = oC_Tcp_Connection_Delete(&connection, oC_KTime_CalculateTimeout(endTimestamp)) && allConnectionsDeleted;
214 bool serversListDeleted = oC_List_Delete ( Servers , AllocationFlags_Default );
215 bool connectionsListDeleted = oC_List_Delete ( Connections , AllocationFlags_Default );
216 bool mutexDeleted = oC_Mutex_Delete( &ModuleBusy , AllocationFlags_Default );
219 ErrorCondition( serversListDeleted
220 && connectionsListDeleted
222 && allConnectionsDeleted
223 && mutexDeleted , oC_ErrorCode_ReleaseError)
226 errorCode = oC_ErrorCode_None;
263 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
264 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
269 ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
270 && ErrorCondition( iscurroot() , oC_ErrorCode_PermissionDenied )
271 && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
280 errorCode = oC_ErrorCode_None;
282 oC_Mutex_Give(ModuleBusy);
321 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
326 ErrorCondition( isram(Port) , oC_ErrorCode_AddressNotInRam )
327 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
335 errorCode = oC_ErrorCode_None;
373 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
380 ErrorCondition( port > 0 , oC_ErrorCode_PortNotCorrect )
381 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
445 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
446 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
451 ErrorCondition( isaddresscorrect(Destination) , oC_ErrorCode_WrongAddress )
453 && ErrorCondition( Destination->
Port > 0 , oC_ErrorCode_PortNotCorrect )
454 && ErrorCondition( isram(outConnection) , oC_ErrorCode_OutputAddressNotInRAM )
455 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
456 && ErrorCode (
oC_Tcp_ReservePort( &LocalPort, oC_KTime_CalculateTimeout(endTimestamp) ) )
457 && ErrorCondition( oC_Mutex_Take(ModuleBusy,oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
461 .InitialSequenceNumber = oC_DynamicConfig_GetValue( Tcp, InitialSequenceNumber ) ,
462 .InitialAcknowledgeNumber = 0 ,
463 .LocalWindowSize = oC_DynamicConfig_GetValue( Tcp, WindowSize ) ,
464 .LocalWindowScale = oC_DynamicConfig_GetValue( Tcp, WindowScale ) ,
465 .ConfirmationTimeout = oC_DynamicConfig_GetValue( Tcp, ConfirmationTimeout ) ,
466 .LocalAddress.Type = Destination->
Type ,
467 .LocalAddress.IPv6.HighPart = 0 ,
468 .LocalAddress.IPv6.LowPart = 0 ,
469 .LocalAddress.Port = LocalPort ,
473 bool added = oC_List_PushBack( Connections, connection, &
Allocator );
476 ErrorCondition( connection !=
NULL , oC_ErrorCode_AllocationError )
477 && ErrorCondition( added , oC_ErrorCode_CannotAddObjectToList )
478 && ErrorCode ( oC_Tcp_Connection_Connect(connection, oC_KTime_CalculateTimeout(endTimestamp)) )
481 *outConnection = connection;
482 errorCode = oC_ErrorCode_None;
486 bool removedFromList = added ==
false || oC_List_RemoveAll( Connections, connection );
487 bool deleted = connection ==
NULL || oC_Tcp_Connection_Delete( &connection, oC_KTime_CalculateTimeout(endTimestamp) );
489 oC_SaveIfFalse (
"Tcp::Connect ", removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
490 oC_SaveIfFalse (
"Tcp::Connect ", deleted , oC_ErrorCode_ReleaseError );
491 oC_SaveIfErrorOccur(
"Tcp::Connect ",
oC_Tcp_ReleasePort(LocalPort, oC_KTime_CalculateTimeout(endTimestamp)) );
494 oC_Mutex_Give(ModuleBusy);
534 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
535 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
540 ErrorCondition( isram(Connection) , oC_ErrorCode_AddressNotInRam )
541 && ErrorCondition( oC_Tcp_Connection_IsCorrect(*Connection) , oC_ErrorCode_ObjectNotCorrect )
542 && ErrorCondition( oC_Tcp_Connection_IsConnected(*Connection) , oC_ErrorCode_NotConnected )
543 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
544 && ErrorCondition( oC_Mutex_Take(ModuleBusy, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
548 bool foundOnList = oC_List_Contains( Connections, *Connection );
552 bzero(&localAddress,
sizeof(localAddress));
554 if(ErrorCondition( foundOnList || server !=
NULL, oC_ErrorCode_ObjectNotFoundOnList ))
556 errorCode = oC_ErrorCode_None;
559 bool removedFromList = foundOnList ==
false || oC_List_RemoveAll( Connections, connection );
564 ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
565 ErrorCode( oC_Tcp_Connection_ReadLocal( connection, &localAddress) )
568 ErrorCode ( oC_Tcp_Connection_Disconnect( connection, oC_KTime_CalculateTimeout(endTimestamp)) );
569 ErrorCondition( oC_Tcp_Connection_Delete ( Connection, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ReleaseError );
573 oC_Mutex_Give(ModuleBusy);
617 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
618 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
625 ErrorCondition( isaddresscorrect(Source) , oC_ErrorCode_WrongAddress )
627 && ErrorCondition( (port = Source->
Port) > 0 , oC_ErrorCode_PortNotCorrect )
628 && ErrorCondition( isram(outServer) , oC_ErrorCode_OutputAddressNotInRAM )
629 && ErrorCondition( MaxConnections > 0 , oC_ErrorCode_MaximumValueNotCorrect )
630 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
632 && ErrorCondition( oC_Mutex_Take(ModuleBusy , oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
636 bool pushed = oC_List_PushBack( Servers, server, &
Allocator );
639 ErrorCondition( server !=
NULL , oC_ErrorCode_AllocationError )
640 && ErrorCondition( pushed , oC_ErrorCode_CannotAddObjectToList )
645 errorCode = oC_ErrorCode_None;
649 bool removedFromList = pushed ==
false || oC_List_RemoveAll( Servers, server );
652 oC_SaveIfFalse (
"TCP::Listen ", removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
653 oC_SaveIfFalse (
"TCP::Listen ", deleted , oC_ErrorCode_ReleaseError );
654 oC_SaveIfErrorOccur (
"TCP::Listen ",
oC_Tcp_ReleasePort(port, oC_KTime_CalculateTimeout(endTimestamp)) );
656 oC_Mutex_Give(ModuleBusy);
695 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
696 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
701 ErrorCondition( isram(Server) , oC_ErrorCode_AddressNotInRam )
703 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
705 && ErrorCondition( oC_Mutex_Take(ModuleBusy , oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
709 bool foundOnList = oC_List_Contains(Servers,*Server);
711 if( ErrorCondition( foundOnList , oC_ErrorCode_ObjectNotFoundOnList ) )
713 errorCode = oC_ErrorCode_None;
718 bool removedFromList = oC_List_RemoveAll(Servers, *Server);
720 ErrorCondition (
oC_Tcp_Server_Delete(Server, oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ReleaseError );
721 ErrorCondition ( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
724 oC_Mutex_Give(ModuleBusy);
761 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
762 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
768 && ErrorCondition( isram(outConnection) , oC_ErrorCode_OutputAddressNotInRAM )
769 && ErrorCondition( Timeout >= 0 , oC_ErrorCode_TimeNotCorrect )
771 && ErrorCondition( oC_Mutex_Take(ModuleBusy , oC_KTime_CalculateTimeout(endTimestamp)) , oC_ErrorCode_ModuleBusy )
774 oC_Mutex_Give(ModuleBusy);
782 errorCode = oC_ErrorCode_None;
821 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
825 errorCode = oC_Tcp_Connection_Send(Connection, Buffer, Size, Timeout);
860 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
864 errorCode = oC_Tcp_Connection_Receive(Connection, outBuffer, Size, Timeout);
870 #undef _________________________________________FUNCTIONS_SECTION__________________________________________________________________________ 877 #define _________________________________________LOCALS_SECTION_____________________________________________________________________________ 888 foreach(Servers,server)
892 foundServer = server;
909 foreach(Servers, server)
913 foundServer = server;
930 foreach(Connections,connection)
932 if(oC_Tcp_Connection_GetProcess(connection) == Process)
934 foundConnection = connection;
939 return foundConnection;
949 oC_ErrorCode_t errorCode = oC_ErrorCode_None;
951 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
959 bool removedFromList = oC_List_RemoveAll(Servers,server);
961 ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
962 ErrorCondition(
oC_Tcp_Server_Delete( &server, oC_KTime_CalculateTimeout(endTimestamp) ), oC_ErrorCode_ReleaseError );
975 oC_ErrorCode_t errorCode = oC_ErrorCode_None;
977 oC_Timestamp_t endTimestamp = oC_KTime_GetTimestamp() + Timeout;
981 if(oC_Tcp_Connection_IsConnected(connection))
983 ErrorCode( oC_Tcp_Connection_Disconnect(connection, oC_KTime_CalculateTimeout(endTimestamp)) );
985 bool removedFromList = oC_List_RemoveAll(Connections,connection);
987 ErrorCondition( removedFromList , oC_ErrorCode_CannotRemoveObjectFromList );
988 ErrorCondition( oC_Tcp_Connection_Delete( &connection, oC_KTime_CalculateTimeout(endTimestamp) ), oC_ErrorCode_ReleaseError );
994 #undef _________________________________________LOCALS_SECTION_____________________________________________________________________________ oC_ErrorCode_t oC_Tcp_StopListen(oC_Tcp_Server_t *Server, oC_Time_t Timeout)
stops listen at the given TCP port
uint32_t oC_PortMan_Port_t
stores the port number
oC_Tcp_Server_t oC_Tcp_Server_New(const oC_Net_Address_t *ListenAddress, uint32_t MaxConnections)
creates TCP server
oC_Process_t oC_Tcp_Server_GetProcess(oC_Tcp_Server_t Server)
returns process associated with the server
oC_ErrorCode_t oC_Tcp_Server_Run(oC_Tcp_Server_t Server)
starts the TCP server
stores TCP connection data
bool oC_Tcp_Server_ContainsConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection)
checks if the server contains the connection
static oC_Tcp_Connection_t GetConnectionCreatedBy(oC_Process_t Process)
returns first connection that has been created by the given process
identifier for allocations
oC_ErrorCode_t oC_PortMan_RegisterModule(oC_Module_t Module, const oC_PortMan_Config_t *Config, oC_Time_t Timeout)
registers module in the port manager
oC_ErrorCode_t oC_Tcp_Server_RemoveConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection)
removes connection from server's connections list
oC_ErrorCode_t oC_Tcp_Server_AcceptConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t Connection, oC_Time_t Timeout)
accepts TCP connection
static oC_Tcp_Server_t GetServerCreatedBy(oC_Process_t Process)
returns first server that has been created by the given process
oC_ErrorCode_t oC_Tcp_TurnOn(oC_Time_t Timeout)
turns on TCP module
bool oC_Tcp_Server_IsRunning(oC_Tcp_Server_t Server)
checks if the TCP server is already running
oC_Tcp_Connection_t oC_Tcp_Connection_New(const oC_Tcp_Connection_Config_t *Config)
allocates memory for a new TCP connection object
oC_ErrorCode_t oC_Tcp_Disconnect(oC_Tcp_Connection_t *Connection, oC_Time_t Timeout)
disconnects TCP connection
oC_Tcp_Port_t
stores TCP port
Module for managing port reservations.
oC_ErrorCode_t oC_Tcp_ReservePort(oC_Tcp_Port_t *Port, oC_Time_t Timeout)
reserves TCP port
Transmission Control Protocol.
oC_ErrorCode_t oC_PortMan_ReleaseAllPortsOf(oC_Module_t Module, oC_Process_t Process, oC_Time_t Timeout)
releases all ports reserved by a given process
oC_ErrorCode_t oC_Tcp_Accept(oC_Tcp_Server_t Server, oC_Tcp_Connection_t *outConnection, oC_Time_t Timeout)
waits for new connection and accepts it
bool oC_Tcp_IsPortReservedBy(oC_Tcp_Port_t Port, oC_Process_t Process)
checks if the port is reserved by the given process
bool oC_Tcp_IsPortReserved(oC_Tcp_Port_t Port)
checks if the port is reserved
The file with interface for the module library.
static bool oC_Module_IsTurnedOn(oC_Module_t Module)
checks if the module is turned on
oC_ErrorCode_t oC_Tcp_Server_WaitForConnection(oC_Tcp_Server_t Server, oC_Tcp_Connection_t *outConnection, oC_Time_t Timeout)
Waits for new TCP connection.
oC_ErrorCode_t oC_Tcp_CloseProcess(oC_Process_t Process, oC_Time_t Timeout)
closes all objects related with the process
oC_PortMan_Port_t MaximumPortNumber
Maximum number of correct port.
The file with list library.
Handles configuration of the Dynamic.
static oC_Tcp_Server_t GetServerWithConnection(oC_Tcp_Connection_t Connection)
searches for a server that contains the given connection
oC_Tcp_Port_t oC_Tcp_Server_GetPort(oC_Tcp_Server_t Server)
returns local port of the server
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 void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
bool oC_PortMan_IsPortReservedBy(oC_Module_t Module, oC_PortMan_Port_t Port, oC_Process_t Process)
checks if the given port is reserved by the given process
bool oC_PortMan_IsPortReserved(oC_Module_t Module, oC_PortMan_Port_t Port)
returns true if port is reserved
bool oC_Tcp_Server_IsCorrect(oC_Tcp_Server_t Server)
checks if the TCP server object is correct
oC_ErrorCode_t oC_Tcp_Send(oC_Tcp_Connection_t Connection, const void *Buffer, oC_MemorySize_t Size, oC_Time_t Timeout)
sends data by using TCP connection
oC_ErrorCode_t oC_PortMan_UnregisterModule(oC_Module_t Module, oC_Time_t Timeout)
unregisters module in the port manager
oC_ErrorCode_t oC_Tcp_Server_Stop(oC_Tcp_Server_t Server)
stops the TCP server
stores configuration of the module
#define oC_uint16_MAX
maximum value for uint16_t type
static oC_List(oC_Tcp_Server_t)
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
oC_ErrorCode_t oC_Tcp_Listen(const oC_Net_Address_t *Source, oC_Tcp_Server_t *outServer, uint32_t MaxConnections, oC_Time_t Timeout)
starts a server that listen at the given address
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
oC_ErrorCode_t oC_Tcp_Connect(const oC_Net_Address_t *Destination, oC_Tcp_Port_t LocalPort, oC_Tcp_Connection_t *outConnection, oC_Time_t Timeout)
connects to the remote TCP server
static oC_ErrorCode_t CloseConnectionsCreatedBy(oC_Process_t Process, oC_Time_t Timeout)
closes all connections that has been created by the given process
static oC_ErrorCode_t CloseServersCreatedBy(oC_Process_t Process, oC_Time_t Timeout)
closes all servers that has been created by the given process
oC_ErrorCode_t oC_Tcp_ReleasePort(oC_Tcp_Port_t Port, oC_Time_t Timeout)
releases TCP port
oC_ErrorCode_t oC_PortMan_ReservePort(oC_Module_t Module, oC_PortMan_Port_t *Port, oC_Time_t Timeout)
reserves a port of the given module
oC_ErrorCode_t oC_Tcp_TurnOff(oC_Time_t Timeout)
turns off TCP module
oC_ErrorCode_t oC_PortMan_ReleasePort(oC_Module_t Module, oC_PortMan_Port_t Port, oC_Time_t Timeout)
releases port
oC_ErrorCode_t oC_Tcp_Receive(oC_Tcp_Connection_t Connection, void *outBuffer, oC_MemorySize_t Size, oC_Time_t Timeout)
receives data by using TCP connection
oC_Net_AddressType_t Type
Type of the address stored inside.
oC_Net_Port_t Port
Port of the address.
bool oC_Tcp_Server_Delete(oC_Tcp_Server_t *Server, oC_Time_t Timeout)
deletes TCP server
#define NULL
pointer to a zero
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off