Software and Services, Portable Libraries  2019.Mar.01
A library for managing digital certificates
SoftwareAndServices::Library::Common::TcpServer Class Reference

Implementation of a simple TCP server program. More...

#include "<SaS/Common/TcpServer.hpp>"

+ Inheritance diagram for SoftwareAndServices::Library::Common::TcpServer:
+ Collaboration diagram for SoftwareAndServices::Library::Common::TcpServer:

Public Member Functions

 TcpServer (Poll *PollPtr, const char *CertPemFileName=nullptr, const char *KeyPemFileName=nullptr)
 Constructor - TcpServer, Using provided Poll object. More...
 
virtual ~TcpServer ()
 Destructor - TcpServer. More...
 
int AddPort (uint16_t port)
 Add a new port to listen on. More...
 
int AddPort (const char *serviceName)
 Add a new port to listen on. More...
 
pthread_t AddService (const char *serviceName)
 If service name is provided, lookup service name and then call Server(unsigned short port) function. More...
 
void Close (uint16_t Port=0)
 Close down the server. More...
 
void CloseFd (int Fd)
 Close down the connection associated with Fd. More...
 
virtual InstanceCount_t decInstance () const
 Decrement the instance. More...
 
int GetAcceptFd (uint16_t Port) const
 
unsigned long GetCurrentFdMax () const
 WIN32 does not support this ability, so this does nothing on WIN32 systems. More...
 
Poll::PollFunc_t GetDefaultAcceptCb () const
 
virtual InstanceCount_t getInstance () const
 
unsigned long GetMaxFd () const
 WIN32 does not support this ability, so this does nothing on WIN32 systems. More...
 
unsigned int GetMaxThreads () const
 
bool GetPeerAddr (uint16_t Port, struct sockaddr_in &Results, socklen_t &AddrLen) const
 Get the peer address. More...
 
const struct sockaddr_in * GetServerAddr (uint16_t port) const
 
SSL * GetSsl (int Fd)
 Get the (SSL*) object for the given File Descriptor. More...
 
void * GetUserData () const
 
virtual InstanceCount_t incInstance () const
 Increment the instance. More...
 
void operator delete (void *ToFree)
 
PollPollFromFd (const int fd) const
 
bool Secure (const char *Private, const char *Public)
 Set the SSL certificate. More...
 
pthread_t Server (const short port)
 Start a new thread, listening on port. More...
 
pthread_t Server (const char *serviceName)
 If service name is provided, lookup service name and then call Server(unsigned short port) function. More...
 
pthread_t ServerTls (const short port)
 Start a new thread, listening on port. More...
 
void SetAcceptFd (const int sockfd, uint16_t Port)
 
void SetListenQSize (const unsigned int TheQSize)
 Set the TCP QSize. More...
 
void SetMaxFd (const unsigned long TheFdMax)
 Set the maximum file descriptor to use. More...
 
void SetMaxThreads (const unsigned int maxThreads)
 Set the maximum number of threads this server will allow at one time to service clients. More...
 
void SetUserData (const void *data)
 
void VerifyCallback (SSL_verify_cb Callback)
 Optionally set a certificate verification callback. More...
 
SSL_verify_cb VerifyCallback () const
 Get any SSL_verify_cb set. More...
 

Friends

void * newServerThread (void *Ptr)
 One thread per tcp port listened to. More...
 
void * PollThread (void *Ptr)
 One thread for doing poll. More...
 
int TcpServerAcceptCb (const int TheFd, const unsigned short Events, Poll::CallBacks *Cb, const unsigned int Len, const char *UserData)
 This is run as a thread and is called whenever a new connection is made to the server. More...
 

Detailed Description

Implementation of a simple TCP server program.

TcpServer1.png
TcpServer2.png

Definition at line 169 of file TcpServer.hpp.

Constructor & Destructor Documentation

◆ TcpServer()

SoftwareAndServices::Library::Common::TcpServer::TcpServer ( Poll PollPtr,
const char *  CertPemFileName = nullptr,
const char *  KeyPemFileName = nullptr 
)

Constructor - TcpServer, Using provided Poll object.

Parameters
PollPtrA pointer to the Poll object to use.
CertPemFileNameName of the PEM file containg the cert.
KeyPemFileNameName of the PEM file containg the key.

◆ ~TcpServer()

virtual SoftwareAndServices::Library::Common::TcpServer::~TcpServer ( )
virtual

Destructor - TcpServer.

Member Function Documentation

◆ AddPort() [1/2]

int SoftwareAndServices::Library::Common::TcpServer::AddPort ( uint16_t  port)

Add a new port to listen on.

This can be done after Server() is called.

Parameters
portThe TCP port to use.
Returns
the new accept fd.

◆ AddPort() [2/2]

int SoftwareAndServices::Library::Common::TcpServer::AddPort ( const char *  serviceName)

Add a new port to listen on.

This can be done after Server() is called.

Parameters
serviceNameThe name of the service.
Returns
the new accept fd.

◆ AddService()

pthread_t SoftwareAndServices::Library::Common::TcpServer::AddService ( const char *  serviceName)

If service name is provided, lookup service name and then call Server(unsigned short port) function.

This can be done after Server() is called.

Parameters
serviceNameThe name of the service to port ID to lookup.
Returns
The thread ID for the server.

◆ Close()

void SoftwareAndServices::Library::Common::TcpServer::Close ( uint16_t  Port = 0)

Close down the server.

Parameters
PortThe port number to close down. When Port is zero, close all ports.

◆ CloseFd()

void SoftwareAndServices::Library::Common::TcpServer::CloseFd ( int  Fd)

Close down the connection associated with Fd.

Parameters
FdThe file descriptor to close.

◆ decInstance()

virtual InstanceCount_t SoftwareAndServices::Library::Common::instance< TcpServer >::decInstance ( ) const
inlinevirtualinherited

Decrement the instance.

Returns
The instance count, after decrementing it.

Definition at line 129 of file instance.hpp.

◆ GetAcceptFd()

int SoftwareAndServices::Library::Common::TcpServer::GetAcceptFd ( uint16_t  Port) const
Parameters
PortGet the accept socket for the named port.
Returns
The socket FD for the accept socket.

◆ GetCurrentFdMax()

unsigned long SoftwareAndServices::Library::Common::TcpServer::GetCurrentFdMax ( ) const

WIN32 does not support this ability, so this does nothing on WIN32 systems.

Returns
The processes current fd max. WIN32 always returns zero.

◆ GetDefaultAcceptCb()

Poll::PollFunc_t SoftwareAndServices::Library::Common::TcpServer::GetDefaultAcceptCb ( ) const
Returns
The TCP servers default ACCEPT-Callback. (transport callback).

◆ getInstance()

virtual InstanceCount_t SoftwareAndServices::Library::Common::instance< TcpServer >::getInstance ( ) const
inlinevirtualinherited
Returns
The current instance count.

Definition at line 118 of file instance.hpp.

◆ GetMaxFd()

unsigned long SoftwareAndServices::Library::Common::TcpServer::GetMaxFd ( ) const

WIN32 does not support this ability, so this does nothing on WIN32 systems.

Returns
The current systems currently configured max fd. WIN32 always returns zero.

◆ GetMaxThreads()

unsigned int SoftwareAndServices::Library::Common::TcpServer::GetMaxThreads ( ) const
Returns
the maximum number of threads this server will allow at one time to service clients.

◆ GetPeerAddr()

bool SoftwareAndServices::Library::Common::TcpServer::GetPeerAddr ( uint16_t  Port,
struct sockaddr_in &  Results,
socklen_t &  AddrLen 
) const

Get the peer address.

Parameters
PortThe port number to check.
[out]ResultsThe (struct sockaddr) will be filled in with the address information.
[out]AddrLenThe length of the retured address.
Returns
true when sucessful and Results and AddrLen have been updated. When false, the valuse in Results and AddrLen are undefined.

◆ GetServerAddr()

const struct sockaddr_in* SoftwareAndServices::Library::Common::TcpServer::GetServerAddr ( uint16_t  port) const
Parameters
portThe port number to look for the address of.
Returns
The socket address of the server.

◆ GetSsl()

SSL* SoftwareAndServices::Library::Common::TcpServer::GetSsl ( int  Fd)

Get the (SSL*) object for the given File Descriptor.

Parameters
FdThe file descriptor to look up.
Returns
the matching (SSL*) for this Fd, or nullptr when none.

◆ GetUserData()

void* SoftwareAndServices::Library::Common::TcpServer::GetUserData ( ) const
Returns
Caller private data.

◆ incInstance()

virtual InstanceCount_t SoftwareAndServices::Library::Common::instance< TcpServer >::incInstance ( ) const
inlinevirtualinherited

Increment the instance.

Returns
The instance value, after incrementing it.

Definition at line 109 of file instance.hpp.

◆ PollFromFd()

Poll* SoftwareAndServices::Library::Common::TcpServer::PollFromFd ( const int  fd) const
Returns
The Poll structure that FD is in.

◆ Secure()

bool SoftwareAndServices::Library::Common::TcpServer::Secure ( const char *  Private,
const char *  Public 
)

Set the SSL certificate.

This MUST BE setup prior to calling Server().

Parameters
PrivateThe private key in PEM format.
PublicThe public key in PEM format.
Returns
true if the PEM data is acceptable.

◆ Server() [1/2]

pthread_t SoftwareAndServices::Library::Common::TcpServer::Server ( const short  port)

Start a new thread, listening on port.

Parameters
portThe TCP port to use.
Returns
The thread ID for the server.
Note
The calling application might need to block SIGPIPE before calling Server(), or specifically handle it when the other end of the TCP connection unexpectedly disconnects.
sigset_t SigSet;
sigemptyset(&SigSet);
sigaddset(&SigSet, SIGPIPE);
pthread_sigmask(SIG_BLOCK, &SigSet, nullptr);

◆ Server() [2/2]

pthread_t SoftwareAndServices::Library::Common::TcpServer::Server ( const char *  serviceName)

If service name is provided, lookup service name and then call Server(unsigned short port) function.

Parameters
serviceNameThe name of the service to port ID to lookup.
Returns
The thread ID for the server.

◆ ServerTls()

pthread_t SoftwareAndServices::Library::Common::TcpServer::ServerTls ( const short  port)

Start a new thread, listening on port.

Only accepting TLS connections.

Parameters
portThe TCP port to use.
Returns
The thread ID for the server.
Note
The calling application might need to block SIGPIPE before calling Server(), or specifically handle it when the other end of the TCP connection unexpectedly disconnects.
sigset_t SigSet;
sigemptyset(&SigSet);
sigaddset(&SigSet, SIGPIPE);
pthread_sigmask(SIG_BLOCK, &SigSet, nullptr);

◆ SetAcceptFd()

void SoftwareAndServices::Library::Common::TcpServer::SetAcceptFd ( const int  sockfd,
uint16_t  Port 
)
Parameters
PortSet the accept socket for the named port.
sockfdThe Accept FD to use.

◆ SetListenQSize()

void SoftwareAndServices::Library::Common::TcpServer::SetListenQSize ( const unsigned int  TheQSize)

Set the TCP QSize.

Parameters
TheQSizeThe TCP QSize to use.

◆ SetMaxFd()

void SoftwareAndServices::Library::Common::TcpServer::SetMaxFd ( const unsigned long  TheFdMax)

Set the maximum file descriptor to use.

ULONG_MAX sets it to system limit. The default value is whatever the default is for the system. WIN32 does not support this ability, so this does nothing on WIN32 systems.

Parameters
TheFdMaxThe maximum file descriptor to use.

◆ SetMaxThreads()

void SoftwareAndServices::Library::Common::TcpServer::SetMaxThreads ( const unsigned int  maxThreads)

Set the maximum number of threads this server will allow at one time to service clients.

Can be changed at any time. Existing threads will complete before exiting.

Parameters
maxThreadsThe maximum number of threads to start.

◆ SetUserData()

void SoftwareAndServices::Library::Common::TcpServer::SetUserData ( const void *  data)
Parameters
dataThe caller private data to use.

◆ VerifyCallback() [1/2]

void SoftwareAndServices::Library::Common::TcpServer::VerifyCallback ( SSL_verify_cb  Callback)

Optionally set a certificate verification callback.

Parameters
CallbackThe callback. Will be set with SSL_CTX_set_verify(). The callback must be similar to:
int VerifyCertificate(int PreVerify, X509_STORE_CTX * Store);

Where:

  • PreVerifyOk: is 1 when the certificate and chain has been checked as good so far. Or 0 when it has not.
  • Store: Is the store to check.
  • And Callback returns 1 when all is okay, else zero.
Note
MUST BE Called before any Open().

◆ VerifyCallback() [2/2]

SSL_verify_cb SoftwareAndServices::Library::Common::TcpServer::VerifyCallback ( ) const

Get any SSL_verify_cb set.

Returns
The verify callback that was set, or nullptr when none was set.

Friends And Related Function Documentation

◆ newServerThread

void* newServerThread ( void *  Ptr)
friend

One thread per tcp port listened to.

Parameters
PtrPointer to the thread object.
Returns
Nothing important, pthread_create requires it.

◆ PollThread

void* PollThread ( void *  Ptr)
friend

One thread for doing poll.

Parameters
PtrPointer to the thread object.
Returns
Nothing important. pthread_create requires it.

◆ TcpServerAcceptCb

int TcpServerAcceptCb ( const int  TheFd,
const unsigned short  Events,
Poll::CallBacks Cb,
const unsigned int  Len,
const char *  UserData 
)
friend

This is run as a thread and is called whenever a new connection is made to the server.

It is called by internal methods.

Parameters
TheFdThe file descriptor of that accept() uses.
EventsNot used here. From the Poll object.
CbThe user callback, or when none, the compiled in method that will gather the data.
LenThe lenght of data, when known.
UserDataA pointer to internal data used for housekeeping.
Returns
Always returns zero for TcpServer objects.
Note
Do not call this method. It will really mess up the internal implementation. It is in the private section for good reasons.

The documentation for this class was generated from the following file: