sipxportlib  Version 3.3
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
OsNatConnectionSocket Class Reference

#include <OsNatConnectionSocket.h>

Inheritance diagram for OsNatConnectionSocket:
Inheritance graph
[legend]
Collaboration diagram for OsNatConnectionSocket:
Collaboration graph
[legend]

Public Member Functions

 OsNatConnectionSocket (int connectedSocketDescriptor, const RtpTcpRoles role)
 
 OsNatConnectionSocket (const char *szLocalIp, int connectedSocketDescriptor, const RtpTcpRoles role)
 
 OsNatConnectionSocket (int serverPort, const char *serverName, UtlBoolean blockingConnect, const char *localIp, const bool bConnect, const RtpTcpRoles role)
 
virtual ~OsNatConnectionSocket ()
 
virtual OsSocketgetSocket ()
 
virtual void setRole (const RtpTcpRoles role)
 
virtual const RtpTcpRoles getRole () const
 
virtual int read (char *buffer, int bufferLength)
 
virtual int read (char *buffer, int bufferLength, UtlString *ipAddress, int *port)
 
virtual int read (char *buffer, int bufferLength, struct in_addr *ipAddress, int *port)
 
virtual int read (char *buffer, int bufferLength, long waitMilliseconds)
 
virtual int write (const char *buffer, int bufferLength)
 
virtual int socketWrite (const char *buffer, int bufferLength, const char *ipAddress, int port, PacketType packetType=UNKNOWN_PACKET)
 
virtual int write (const char *buffer, int bufferLength, const char *ipAddress, int port)
 
virtual int write (const char *buffer, int bufferLength, long waitMilliseconds)
 
virtual void enableStun (const char *szStunServer, int stunPort, int iKeepAlive, int stunOptions, bool bReadFromSocket)
 
virtual void disableStun ()
 
virtual void enableTurn (const char *szTurnSever, int turnPort, int iKeepAlive, const char *username, const char *password, bool bReadFromSocket)
 
virtual void disableTurn ()
 
virtual void enableTransparentReads (bool bEnable)
 
virtual void addAlternateDestination (const char *szAddress, int iPort, int priority)
 
virtual void readyDestination (const char *szAddress, int iPort)
 
virtual void setNotifier (OsNotification *pNotification)
 
virtual UtlBoolean addCrLfKeepAlive (const char *szRemoteIp, const int remotePort, const int keepAliveSecs, OsNatKeepaliveListener *pListener)
 
virtual UtlBoolean removeCrLfKeepAlive (const char *szRemoteIp, const int remotePort)
 
virtual UtlBoolean addStunKeepAlive (const char *szRemoteIp, const int remotePort, const int keepAliveSecs, OsNatKeepaliveListener *pListener)
 
virtual UtlBoolean removeStunKeepAlive (const char *szRemoteIp, const int remotePort)
 
virtual UtlBoolean getMappedIp (UtlString *ip, int *port)
 
virtual UtlBoolean getRelayIp (UtlString *ip, int *port)
 
virtual UtlBoolean getBestDestinationAddress (UtlString &address, int &iPort)
 
virtual UtlBoolean applyDestinationAddress (const char *szAddress, int iPort)
 
const char * frameBuffer (TURN_FRAMING_TYPE type, const char *buffer, const int bufferLength, int &framedBufferLen)
 
virtual void destroy ()
 
virtual int clientConnect (const char *szServer, const int port)
 
virtual bool isClientConnected (const char *szServer, const int port)
 
virtual OsNatConnectionSocketgetClientConnection (const char *szServer, const int port)
 
- Public Member Functions inherited from OsConnectionSocket
 OsConnectionSocket (int remoteHostPort, const char *remoteHostName, UtlBoolean makeBlocking=TRUE, const char *localIp=NULL, const bool bConnect=true)
 
 OsConnectionSocket (int connectedSocketDescriptor)
 
 OsConnectionSocket (const char *localIp, int connectedSocketDescriptor)
 
virtual ~OsConnectionSocket ()
 
virtual int connect ()
 
virtual UtlBoolean reconnect ()
 
virtual OsSocket::IpProtocolSocketType getIpProtocol () const
 param: waitMilliseconds - The maximum number of milliseconds to block. This may be set to zero in which case it does not block. More...
 
virtual bool isEncrypted () const
 Is this connection encrypted using TLS/SSL? More...
 
virtual bool peerIdentity (UtlSList *altNames=NULL, UtlString *commonName=NULL) const
 Get any authenticated peer host names. More...
 
- Public Member Functions inherited from OsSocket
virtual UtlContainableType getContainableType () const
 Determine whether or not the values in a containable are comparable. More...
 
 OsSocket ()
 
virtual ~OsSocket ()
 
virtual void close ()
 param: waitMilliseconds - The maximum number of milliseconds to block. This may be set to zero in which case it does not block. More...
 
virtual void makeNonblocking ()
 
virtual void makeBlocking ()
 
virtual int getSocketDescriptor () const
 
virtual void getLocalHostIp (UtlString *localHostAddress) const
 
virtual const UtlStringgetLocalIp () const
 
virtual void setLocalIp (const UtlString &localIp)
 
virtual int getLocalHostPort () const
 
virtual void getRemoteHostName (UtlString *remoteHostName) const
 
virtual void getRemoteHostIp (struct in_addr *remoteHostAddress, int *remotePort=NULL)
 
virtual void getRemoteHostIp (UtlString *remoteHostAddress, int *remotePort=NULL)
 
virtual int getRemoteHostPort () const
 
virtual UtlBoolean isOk () const
 
virtual UtlBoolean isConnected () const
 
virtual UtlBoolean isReadyToReadEx (long waitMilliseconds, UtlBoolean &rSocketError) const
 
virtual UtlBoolean isReadyToRead (long waitMilliseconds=0) const
 
virtual UtlBoolean isReadyToWrite (long waitMilliseconds=0) const
 
- Public Member Functions inherited from UtlContainableAtomic
virtual unsigned hash () const
 Calculate a hash code for this object. More...
 
virtual int compareTo (UtlContainable const *) const
 Compare this object to another object. More...
 
- Public Member Functions inherited from UtlContainable
virtual ~UtlContainable ()
 
unsigned directHash () const
 Provides a hash function that uses the object pointer as the hash value. More...
 
virtual UtlBoolean isEqual (UtlContainable const *) const
 Test this object to another object for equality. More...
 
virtual UtlBoolean isInstanceOf (const UtlContainableType type) const
 Determine if this object is a derivative of the specified UtlContainableType. More...
 
- Public Member Functions inherited from OsNatSocketBaseImpl
 OsNatSocketBaseImpl ()
 
virtual ~OsNatSocketBaseImpl ()
 
virtual void setReadNotification (OsNotification *pNotification)
 
- Public Member Functions inherited from IStunSocket
virtual ~IStunSocket ()
 

Protected Member Functions

void setStunAddress (const UtlString &address, const int iPort)
 
void setTurnAddress (const UtlString &address, const int iPort)
 
void markStunFailure ()
 
void markStunSuccess (bool bAddressChanged)
 
void markTurnFailure ()
 
void markTurnSuccess ()
 
void evaluateDestinationAddress (const UtlString &address, int iPort, int priority)
 
virtual void handleFramedStream (char *pData, const int size, const char *receivedIp, const int port)
 
virtual bool handleUnframedBuffer (const TURN_FRAMING_TYPE type, const char *buff, const int buffSize, const char *receivedIp, const int port)
 
void addClientConnection (const char *ipAddress, const int port, OsNatConnectionSocket *pClient)
 
- Protected Member Functions inherited from OsConnectionSocket
bool initialize (const char *serverName, int serverPort, UtlBoolean blockingConnect)
 
- Protected Member Functions inherited from OsNatSocketBaseImpl
void markReadTime ()
 
void markWriteTime ()
 
virtual bool getFirstReadTime (OsDateTime &time)
 
virtual bool getLastReadTime (OsDateTime &time)
 
virtual bool getFirstWriteTime (OsDateTime &time)
 
virtual bool getLastWriteTime (OsDateTime &time)
 
virtual void handleStunMessage (char *pBuf, int length, UtlString &fromAddress, int fromPort)
 
virtual void handleTurnMessage (char *pBuf, int length, UtlString &fromAddress, int fromPort)
 
virtual int handleTurnDataIndication (char *buffer, int bufferLength, UtlString *pRecvFromIp, int *pRecvFromPort)
 
virtual bool handleSturnData (char *buffer, int &bufferLength, UtlString &receivedIp, int &receivedPort)
 

Protected Attributes

RtpTcpRoles mRole
 
OsRWMutex mRoleMutex
 
OsMutex mStreamHandlerMutex
 
bool mbTransparentReads
 
OsNatDatagramSocketmpDatagramSocket
 
UtlHashMap mClientConnectionSockets
 
- Protected Attributes inherited from OsSocket
OsAtomicLightInt socketDescriptor
 
int localHostPort
 
int remoteHostPort
 
UtlString mLocalIp
 
UtlString localHostName
 
UtlString remoteHostName
 
UtlString mRemoteIpAddress
 
UtlBoolean mIsConnected
 
- Protected Attributes inherited from OsNatSocketBaseImpl
unsigned int miRecordTimes
 
OsDateTime mFirstRead
 
OsDateTime mLastRead
 
OsDateTime mFirstWrite
 
OsDateTime mLastWrite
 
OsMutex mReadNotificationLock
 
OsNotificationmpReadNotification
 

Friends

class OsNatAgentTask
 

Additional Inherited Members

- Public Types inherited from OsSocket
enum  IpProtocolSocketType {
  UNKNOWN = -1, TCP = 0, UDP = 1, MULTICAST = 2,
  SSL_SOCKET = 3, CUSTOM = 4
}
 
- Static Public Member Functions inherited from OsSocket
static UtlBoolean socketInit ()
 
static unsigned long initDefaultAdapterID (UtlString &adapter_id)
 
static void setDefaultBindAddress (const unsigned long bind_address)
 
static const char * ipProtocolString (OsSocket::IpProtocolSocketType)
 return the string representation of the SocketProtocolType More...
 
static void getDomainName (UtlString &domain_name)
 
static unsigned long getDefaultBindAddress ()
 
static void getHostName (UtlString *hostName)
 
static void getHostIp (UtlString *hostAddress)
 
static UtlBoolean isIp4Address (const char *address)
 
static UtlBoolean isMcastAddr (const char *ipAddress)
 
static UtlBoolean isLocalHost (const char *hostAddress)
 
static UtlBoolean isSameHost (const char *host1, const char *host2)
 
static UtlBoolean getHostIpByName (const char *hostName, UtlString *hostAddress)
 
static void inet_ntoa_pt (struct in_addr input_address, UtlString &output_address)
 
static UtlBoolean isFramed (IpProtocolSocketType type)
 
- Static Public Member Functions inherited from UtlContainable
static unsigned stringHash (char const *value)
 Provides a hash function appropriate for null-terminated string values. More...
 
static UtlBoolean areSameTypes (const UtlContainableType type1, const UtlContainableType type2)
 Are UtlContainable types the same. More...
 
- Static Public Attributes inherited from OsSocket
static UtlBoolean socketInitialized = FALSE
 
- Static Public Attributes inherited from UtlContainable
static const UtlContainableType TYPE = "UtlContainable"
 
- Static Protected Attributes inherited from OsSocket
static const UtlContainableType TYPE = "OsSocket"
 Class type used for runtime checking. More...
 
static OsBSem mInitializeSem
 

Detailed Description

OsNatConnectionSocket extends an OsDatagramSocket by adding an integrated STUN and TURN client. If STUN or TURN is enabled, request will be send to the designated server every refresh period. The external addresses obtained by these mechanisms are retrieved by invoking getMappedIp and getRelayIp.

For this mechanism to work, someone must pump inbound socket data by calling one of the read() methods. Otherwise, the packets will not be received/processed. Internally, the implemenation peeks at the read data and passes the message to the OsNatAgentTask for processing.

Constructor & Destructor Documentation

OsNatConnectionSocket ( int  connectedSocketDescriptor,
const RtpTcpRoles  role 
)
OsNatConnectionSocket ( const char *  szLocalIp,
int  connectedSocketDescriptor,
const RtpTcpRoles  role 
)
OsNatConnectionSocket ( int  serverPort,
const char *  serverName,
UtlBoolean  blockingConnect,
const char *  localIp,
const bool  bConnect,
const RtpTcpRoles  role 
)
~OsNatConnectionSocket ( )
virtual

Standard Destructor

Member Function Documentation

OsSocket * getSocket ( )
virtual

Implements OsNatSocketBaseImpl.

void setRole ( const RtpTcpRoles  role)
virtual
const RtpTcpRoles getRole ( ) const
virtual
int read ( char *  buffer,
int  bufferLength 
)
virtual

Standard read, see OsDatagramSocket for details.

Reimplemented from OsConnectionSocket.

int read ( char *  buffer,
int  bufferLength,
UtlString ipAddress,
int *  port 
)
virtual

Standard read, see OsDatagramSocket for details.

Reimplemented from OsConnectionSocket.

int read ( char *  buffer,
int  bufferLength,
struct in_addr *  ipAddress,
int *  port 
)
virtual

Standard read, see OsDatagramSocket for details.

Reimplemented from OsSocket.

int read ( char *  buffer,
int  bufferLength,
long  waitMilliseconds 
)
virtual

Standard read, see OsDatagramSocket for details.

Reimplemented from OsConnectionSocket.

int write ( const char *  buffer,
int  bufferLength 
)
virtual

Standard write, see OsDatagramSocket for details.

Reimplemented from OsSocket.

int socketWrite ( const char *  buffer,
int  bufferLength,
const char *  ipAddress,
int  port,
PacketType  packetType = UNKNOWN_PACKET 
)
virtual

Standard write, see OsDatagramSocket for details.

Implements IStunSocket.

int write ( const char *  buffer,
int  bufferLength,
const char *  ipAddress,
int  port 
)
virtual

Standard write, see OsDatagramSocket for details.

Reimplemented from OsSocket.

int write ( const char *  buffer,
int  bufferLength,
long  waitMilliseconds 
)
virtual

Standard write, see OsDatagramSocket for details.

Reimplemented from OsSocket.

void enableStun ( const char *  szStunServer,
int  stunPort,
int  iKeepAlive,
int  stunOptions,
bool  bReadFromSocket 
)
virtual

Enable STUN. Enabling STUN will reset the the keep alive timer and will force a binding refresh.

Parameters
szStunServer
stunPort
iKeepAlive
stunOptions
bReadFromSocket

Implements IStunSocket.

void disableStun ( )
virtual

Disable STUN. Disabling STUN will stop all keep alives and cause getMappedIp to fail.

void enableTurn ( const char *  szTurnSever,
int  turnPort,
int  iKeepAlive,
const char *  username,
const char *  password,
bool  bReadFromSocket 
)
virtual

Enable STUN. Enabling STUN will reset the the keep alive timer and will force a binding refresh.

Parameters
szTurnSever
turnPort
iKeepAlive
username
password
bReadFromSocket

Implements IStunSocket.

void disableTurn ( )
virtual

Disable TURN. Disabling TURN will stop all keep alives and cause getRelayIp to fail.

void enableTransparentReads ( bool  bEnable)
virtual

When a stun packet is received this socket can either call read again to obtain the next packet or return with zero bytes read. By default the socket will transparently call Read again and will block until a non-stun packet is read. Calling this method will effect the next read – in other words it will not unblock an active read.

Parameters
bEnableTrue to enable transparent stun reads and block until a non-stun packet is received (default) or False to return with zero bytes read if a stun packet is received.

Implements IStunSocket.

void addAlternateDestination ( const char *  szAddress,
int  iPort,
int  priority 
)
virtual

Add an alternate destination to this OsNatConnectionSocket. Alternate destinations are tested by sending stun packets. If a stun response is received and the priority is greater than what has already been selected then that address is used.

Parameters
szAddressIP address of the alternate destination
iPortport number of the alternate destination
prioritypriority of the alternate where a higher number indicates a higher priority.

Implements IStunSocket.

void readyDestination ( const char *  szAddress,
int  iPort 
)
virtual

Prepares a destination under TURN usage.

Implements IStunSocket.

void setNotifier ( OsNotification pNotification)
virtual

Sets as notification event that is signaled upon the next successful stun response or on failure (did not receive a stun response within (STUN_ABORT_THRESHOLD * STUN_TIMEOUT_RESPONSE_MS). If a notification event was previous set either by calling this method or via the constructor, it will be overridden. If the initial STUN success/failure state has already been determined, this method is undefined.

Parameters
pNotificationNotification event signaled on success or failure.
UtlBoolean addCrLfKeepAlive ( const char *  szRemoteIp,
const int  remotePort,
const int  keepAliveSecs,
OsNatKeepaliveListener pListener 
)
virtual
UtlBoolean removeCrLfKeepAlive ( const char *  szRemoteIp,
const int  remotePort 
)
virtual
UtlBoolean addStunKeepAlive ( const char *  szRemoteIp,
const int  remotePort,
const int  keepAliveSecs,
OsNatKeepaliveListener pListener 
)
virtual
UtlBoolean removeStunKeepAlive ( const char *  szRemoteIp,
const int  remotePort 
)
virtual
UtlBoolean getMappedIp ( UtlString ip,
int *  port 
)
virtual

Return the external mapped IP address for this socket. This method will return false if stun is disabled, it was unable to retrieve a stun binding, or both the ip and port parameters are null.

Parameters
ipBuffer to place STUN-discovered IP address
portBuffer to place STUN-discovered port number

Implements IStunSocket.

UtlBoolean getRelayIp ( UtlString ip,
int *  port 
)
virtual

Return the external relay IP address for this socket. This method will return false if stun is disabled, it was unable to retrieve a turn allocation, or both the ip and port parameters are null.

Parameters
ipBuffer to place TURN-discovered IP address
portBuffer to place TURN-discovered port number

Implements IStunSocket.

UtlBoolean getBestDestinationAddress ( UtlString address,
int &  iPort 
)
virtual

TODO: DOCS

Implements IStunSocket.

UtlBoolean applyDestinationAddress ( const char *  szAddress,
int  iPort 
)
virtual

TODO: DOCS

Implements IStunSocket.

const char * frameBuffer ( TURN_FRAMING_TYPE  type,
const char *  buffer,
const int  bufferLength,
int &  framedBufferLen 
)

Applies framing to buffers sent over streaming connections to a TURN server. Framing prepends with a header like so:

0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Type | Reserved = 0 | Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Parameters
typeType of framed buffer - STUN or DATA
bufferBuffer to be framed.
bufferLengthLength of the buffer to be framed.
framedBufferLenOutput parameter for the length of the framed buffer.
Returns
Pointer to a newly allocated buffer. Must be freed by caller.
void destroy ( )
virtual

Cleanup routine.

Implements IStunSocket.

int clientConnect ( const char *  szServer,
const int  port 
)
virtual
bool isClientConnected ( const char *  szServer,
const int  port 
)
virtual
OsNatConnectionSocket * getClientConnection ( const char *  szServer,
const int  port 
)
virtual
void setStunAddress ( const UtlString address,
const int  iPort 
)
protectedvirtual

Set the STUN-derived address for this socket.

Parameters
addressSTUN-derived hostname/IP address
iPortSTUN-derived port address

Implements IStunSocket.

void setTurnAddress ( const UtlString address,
const int  iPort 
)
protectedvirtual

Set the TURN-dervied relay address for this socket.

Parameters
addressTURN-derived hostname/IP address
iPortTURN-derived port address

Implements IStunSocket.

void markStunFailure ( )
protectedvirtual

Report that a stun attempt failed.

Implements IStunSocket.

void markStunSuccess ( bool  bAddressChanged)
protectedvirtual

Report that a stun attempt succeeded.

Implements IStunSocket.

void markTurnFailure ( )
protectedvirtual

Report that a stun attempt failed.

Implements IStunSocket.

void markTurnSuccess ( )
protectedvirtual

Report that a stun attempt succeeded.

Implements IStunSocket.

void evaluateDestinationAddress ( const UtlString address,
int  iPort,
int  priority 
)
protectedvirtual

Reset the destination address for this OsNatConnectionSocket. This method is called by the OsStunAgentTask when a better address is found via STUN/ICE.

Parameters
addressThe new destination address
iPortThe new destination port
priorityPriority of the destination address

Implements IStunSocket.

void handleFramedStream ( char *  pData,
const int  size,
const char *  receivedIp,
const int  port 
)
protectedvirtual
bool handleUnframedBuffer ( const TURN_FRAMING_TYPE  type,
const char *  buff,
const int  buffSize,
const char *  receivedIp,
const int  port 
)
protectedvirtual
void addClientConnection ( const char *  ipAddress,
const int  port,
OsNatConnectionSocket pClient 
)
protected

Friends And Related Function Documentation

friend class OsNatAgentTask
friend

Member Data Documentation

RtpTcpRoles mRole
protected

active, passive, or actpass

OsRWMutex mRoleMutex
mutableprotected
OsMutex mStreamHandlerMutex
mutableprotected
bool mbTransparentReads
protected

Block until a non-stun/turn packet is read

OsNatDatagramSocket* mpDatagramSocket
protected

This is the connection socket's analogous UDP socket. Used for STUN and other requests that require UDP.

UtlHashMap mClientConnectionSockets
protected