24 #include <arpa/inet.h>
25 #include <sys/socket.h>
34 using namespace SGCTP;
45 struct timeval __tTimeval;
47 __tTimeval.tv_usec = 1000000 * (int)modf( _fdTimeout, &__fdSeconds );
48 __tTimeval.tv_sec = (int)__fdSeconds;
50 SOL_SOCKET, SO_RCVTIMEO,
51 &__tTimeval,
sizeof( __tTimeval ) );
53 SOL_SOCKET, SO_SNDTIMEO,
54 &__tTimeval,
sizeof( __tTimeval ) );
66 int __iReturn =
::send( _iSocket, _pBuffer, _iSize, _iFlags );
78 int __iReturn =
::recv( _iSocket, _pBuffer, _iSize, _iFlags );
91 const CData &_roData )
192 int __iPayloadSize = 0;
223 *((uint32_t*)(
pucBuffer+__iPayloadSize)) =
226 *((uint32_t*)(
pucBuffer+__iPayloadSize)) =
231 __iReturn =
send( _iSocket,
pucBuffer, __iPayloadSize, MSG_EOR );
232 if( __iReturn != __iPayloadSize )
242 bool __bSendAuthPayload =
false;
255 __iError = __iReturn;
258 __iReturn =
send( _iSocket,
280 __iError = __iReturn;
292 __bSendAuthPayload =
true;
304 if( __bSendAuthPayload )
307 __oData.
setID(
"#AUTH" );
308 __iReturn =
serialize( _iSocket, __oData );
311 __iError = __iReturn;
322 __iError = __iReturn;
325 if( strcmp( __oData.
getID(),
"#OK" ) )
364 int __iPayloadSize = 0;
372 if( __iReturn != 15 )
382 const unsigned char *__pucHandshakeBuffer =
pullBuffer( 15 );
385 if( memcmp( __pucHandshakeBuffer,
"SGCTP", 5 ) )
393 uint8_t __ui8tProtocolVersion =
394 *((uint8_t*)(__pucHandshakeBuffer+__iPayloadSize));
403 uint8_t __ui8tPayloadType =
404 *((uint8_t*)(__pucHandshakeBuffer+__iPayloadSize));
408 uint64_t __ui64tPrincipalID =
409 ntohl( *((uint32_t*)(__pucHandshakeBuffer+__iPayloadSize)) );
411 __ui64tPrincipalID |=
412 ntohl( *((uint32_t*)(__pucHandshakeBuffer+__iPayloadSize)) );
430 __iError = __iReturn;
435 bool __bRecvAuthPayload =
false;
436 switch( __ui8tPayloadType )
454 memcpy( __pucNonceClient,
466 __iError = __iReturn;
471 __iReturn =
serialize( _iSocket, __oData );
474 __iError = __iReturn;
484 __bRecvAuthPayload =
true;
497 if( __bRecvAuthPayload )
501 if( __iReturn < 0 || strcmp( __oData.
getID(),
"#AUTH" ) )
504 __oData.
setID(
"#KO" );
517 __oData.
setID(
"#OK" );
518 __iReturn =
serialize( _iSocket, __oData );
521 __iError = __iReturn;
virtual void setTimeout(int _iSocket, double _fdTimeout)
Set the transmission (send/receive) timeout, in seconds.
static const uint8_t PROTOCOL_VERSION
Protocol version.
int makeCryptoKey(const unsigned char *_pucPassword, int _iPasswordLength, const unsigned char *_pucNonce)
Create cryptographic key (and seal)
uint64_t getID() const
Returns the principal ID.
virtual int serialize(int _iDescriptor, const CData &_roData)
Serialize the given SGCTP data to the given descriptor.
const uint16_t getDataSize() const
Return the data size.
virtual int recv(int _iSocket, void *_pBuffer, int _iSize, int _iFlags)
Receive data from the given descriptor.
void setID(const char *_pcID)
Set the ID string (max. 127 characters)
int sendHandshake(int _iSocket)
Send the TCP handshake.
const char * getPassword() const
Returns the principal password.
uint16_t setData(const unsigned char *_pucData, uint16_t _ui16tDataSize)
Set the data (max. 32767 symbols)
virtual int send(int _iSocket, const void *_pBuffer, int _iSize, int _iFlags)
Send data to the given descriptor.
virtual int alloc()
Allocate resources required for data transmission (un-/serialization)
CPrincipal oPrincipal
Principal.
virtual void setTimeout(int _iDescriptor, double _fdTimeout)
Set the transmission (send/receive) timeout, in seconds.
void reset(bool _bDataFree=true)
Reset (undefine) all data.
AES128-encrypted payload.
virtual int initPayload(uint8_t _ui8tPayloadType=PAYLOAD_RAW)
Associate and initialize payload object.
static const uint16_t CRYPTO_NONCE_SIZE
const unsigned char * pullBuffer(int _iSize)
Pull data from the transmission buffer.
int recvBuffer(int _iDescriptor, int _iSize)
Receive data from the given descriptor (and push them on the data buffer)
void erasePassword()
Erases the principal password (and nullifies memory)
CPayload * poPayload
Associated payload object.
EPayloadType ePayloadType
Associated payload type.
int recvHandshake(int _iSocket)
Receive the TCP handshake (and initialize internal resources: principal/payload)
CPrincipal * usePrincipal()
Return the principal (pointer)
void resetBuffer()
Reset the data buffer (clear all data)
unsigned char * pucBuffer
Transmission buffer.
static int makeCryptoNonce(unsigned char *_pucNonce)
Create cryptographic nonce.
virtual int unserialize(int _iDescriptor, CData *_poData, int _iMaxSize=0)
Unserialize the SGCTP data from the given descriptor.
void getData(unsigned char *_pucData, uint16_t *_pui16tDataSize) const
Return the data (max. 32767 symbols)
const char * getID() const
Return the ID string.
int read(const char *_pcPrincipalsPath, uint64_t _ui64tID)
Retrieves the principal paremeters from the given file.
const char * pcPrincipalsPath
Principals (database) path (pointer to existing variable)
virtual int serialize(int _iSocket, const CData &_roData)
Serialize the given SGCTP data to the given descriptor.
virtual int unserialize(int _iSocket, CData *_poData, int _iMaxSize=0)
Unserialize the SGCTP data from the given descriptor.
AES128-encrypted SGCTP payload.
int allocBuffer()
Allocate resources required for data transmission.