RTP Trace System  1.0
Coral::Socket Class Reference

Socket. More...

#include <socket.h>

List of all members.

Public Types

enum  SocketCommunicationDomain {
  UndefinedSocketCommunicationDomain = -1, IP = 255, IPv4 = AF_INET, IPv6 = AF_INET6,
  Unix = AF_UNIX
}
enum  SocketType {
  UndefinedSocketType = -1, UDP = SOCK_DGRAM, Datagram = SOCK_DGRAM, TCP = SOCK_STREAM,
  Stream = SOCK_STREAM, Raw = SOCK_RAW, RDM = SOCK_RDM, SeqPacket = SOCK_SEQPACKET
}
enum  SocketProtocol { UndefinedSocketProtocol = -1, Default = 0, ICMPv4 = IPPROTO_ICMP, ICMPv6 = IPPROTO_ICMPV6 }

Public Member Functions

 Socket ()
 Socket (const SocketCommunicationDomain communicationDomain, const SocketType socketType, const SocketProtocol socketProtocol=Default)
virtual ~Socket ()
bool create (const SocketCommunicationDomain communicationDomain=IP, const SocketType socketType=TCP, const SocketProtocol socketProtocol=Default)
void close ()
void shutdown (const cardinal shutdownLevel)
card64 getBytesSent () const
card64 getBytesReceived () const
void resetBytesSent ()
void resetBytesReceived ()
bool ready () const
bool bind (const SocketAddress &address=InternetAddress())
bool listen (const cardinal backlog=5)
Socketaccept ()
bool connect (const SocketAddress &address, const card8 trafficClass=0)
integer getLastError ()
integer getSocketOption (const cardinal level, const cardinal optionNumber, void *optionValue, socklen_t *optionLength) const
cardinal getSoLinger () const
bool getSoReuseAddress () const
bool getSoBroadcast () const
bool getTCPNoDelay () const
bool getBlockingMode ()
integer setSocketOption (const cardinal level, const cardinal optionNumber, const void *optionValue, const socklen_t optionLength)
void setSoLinger (const bool on, const cardinal linger)
void setSoReuseAddress (const bool on)
void setSoBroadcast (const bool on)
void setTCPNoDelay (const bool on)
void setBlockingMode (const bool on)
card32 getSendFlowLabel () const
card8 getSendTrafficClass () const
card32 getReceivedFlowLabel () const
card8 getReceivedTrafficClass () const
virtual ssize_t sendTo (const void *buffer, const size_t length, const cardinal flags, const SocketAddress &receiver, const card8 trafficClass=0)
virtual ssize_t send (const void *buffer, const size_t length, const cardinal flags=0, const card8 trafficClass=0)
virtual ssize_t receiveFrom (void *buffer, const size_t length, SocketAddress &sender, const cardinal flags=0)
virtual ssize_t receive (void *buffer, const size_t length, const cardinal flags=0)
virtual ssize_t read (void *buffer, const size_t length)
virtual ssize_t write (const void *buffer, const size_t length)
integer fcntl (const integer cmd, const long arg=0)
integer ioctl (const integer request, const void *argp)
bool getSocketAddress (SocketAddress &address) const
bool getPeerAddress (SocketAddress &address) const
InternetFlow allocFlow (const InternetAddress &address, const card32 flowLabel=0, const card8 shareLevel=IPV6_FL_S_PROCESS)
void freeFlow (InternetFlow &flow)
bool renewFlow (InternetFlow &flow, const cardinal expires, const cardinal linger=6)
bool renewFlow (const cardinal expires, const cardinal linger=6)
virtual void setTrafficConstraint (const card8 trafficClass, const card64 bandwidth, const double bufferDelay)
virtual void flush ()
int getSystemSocketDescriptor () const

Static Public Member Functions

static bool bindInternetSocketPair (Socket &senderSocket, Socket &receiverSocket, const InternetAddress &receiver=InternetAddress())

Static Public Attributes

static const cardinal MinAutoSelectPort = 16384
static const cardinal MaxAutoSelectPort = 65535

Protected Member Functions

void init ()
bool setTOS (const card8 trafficClass)
ssize_t recvFrom (int fd, void *buf, const size_t len, const integer flags, struct sockaddr *addr, size_t *addrlen)

Protected Attributes

card64 BytesSent
card64 BytesReceived
card32 SendFlow
card32 ReceivedFlow
cardinal Backlog
cardinal LastError
int SocketDescriptor
sockaddr * Destination
SocketCommunicationDomain CommunicationDomain
SocketType Type
SocketProtocol Protocol

Friends

class TrafficShaper

Detailed Description

Socket.

This class manages a socket. IPv6 support is automatically available, when supported by the system.

Author:
Thomas Dreibholz
Version:
1.0

Member Enumeration Documentation

Enumerator:
UndefinedSocketCommunicationDomain 
IP 
IPv4 
IPv6 
Unix 
Enumerator:
UndefinedSocketProtocol 
Default 
ICMPv4 
ICMPv6 
Enumerator:
UndefinedSocketType 
UDP 
Datagram 
TCP 
Stream 
Raw 
RDM 
SeqPacket 

Constructor & Destructor Documentation

Constructor.

Coral::Socket::Socket ( const SocketCommunicationDomain  communicationDomain,
const SocketType  socketType,
const SocketProtocol  socketProtocol = Default 
)

Constructor for a new socket. For automatic usage of IPv6 when available, set communication domain to IP. Use IPv4/IPv6 only if a special protocol version is necessary! The creation success can be checked using ready() method.

Parameters:
communicationDomainCommunication domain (e.g. IP).
socketTypeSocket type (e.g. TCP, UDP).
socketProtocolSocket protocol (e.g. Default).
See also:
ready
Coral::Socket::~Socket ( ) [virtual]

Destructor.


Member Function Documentation

Accept a connection.

Returns:
New socket.
InternetFlow Coral::Socket::allocFlow ( const InternetAddress address,
const card32  flowLabel = 0,
const card8  shareLevel = IPV6_FL_S_PROCESS 
)

Allocate a new flow to a given destination. A InternetFlow object is returned, the value flow.getFlowLabel() will not be 0, if the allocFlow() call was successful.

Parameters:
addressAddress of the destination.
flowLabelFlowlabel; 0 for random value.
shareLevelShare level for flow label.
Returns:
InternetFlow.
bool Coral::Socket::bind ( const SocketAddress address = InternetAddress())

Bind socket to given address. If address is null address, then INADDR_ANY and an automatically selected port will be used.

Parameters:
addressSocket address.
Returns:
true on success; false otherwise.
bool Coral::Socket::bindInternetSocketPair ( Socket senderSocket,
Socket receiverSocket,
const InternetAddress receiver = InternetAddress() 
) [static]

Bind a pair of internet sockets to a given address and port number x and x + 1. x will be a random number, if given port number is 0.

Parameters:
senderSocketFirst socket.
receiverSocketSecond socket.
receiverAddress (e.g ipv6-localhost:0) or NULL for Any address.

Close socket.

bool Coral::Socket::connect ( const SocketAddress address,
const card8  trafficClass = 0 
)

Connect socket to given address. A value for traffic class is supported if the connection is an IPv6 connection; otherwise it is ignored.

Parameters:
addressAddress.
trafficClassTraffic class of the connection (IPv6 only!)
Returns:
true on success; false otherwise.
bool Coral::Socket::create ( const SocketCommunicationDomain  communicationDomain = IP,
const SocketType  socketType = TCP,
const SocketProtocol  socketProtocol = Default 
)

Close existing socket and create new socket. For automatic usage of IPv6 when available, set communication domain to IP. Use IPv4/IPv6 only if a special protocol version is necessary!

Parameters:
communicationDomainCommunication domain (e.g. IP).
socketTypeSocket type (e.g. TCP, UDP).
socketProtocolSocket protocol (e.g. Default).
Returns:
true, if creation was sucessful; false otherwise.
integer Coral::Socket::fcntl ( const integer  cmd,
const long  arg = 0 
) [inline]

Wrapper for fcntl().

Parameters:
cmdCommand.
argArgument.
Returns:
Result of fcntl() call.
void Coral::Socket::flush ( ) [virtual]

Flush traffic shaper buffer. Note: This functionality has to be implemented by subclasses!

Free a flow.

Parameters:
flowFlow to be freed.

Check, if blocking mode is on.

Returns:
true, if blocking mode is on; false otherwise.

Get number of bytes received.

Returns:
Number of bytes received.
card64 Coral::Socket::getBytesSent ( ) const [inline]

Get number of bytes sent.

Returns:
Number of bytes sent.

Get last error code. It will be reset to 0 after copying.

Returns:
Last error code.
bool Coral::Socket::getPeerAddress ( SocketAddress address) const

Get the peer's address. Note: A socket has to be connected to a peer first to get a peer address!

Parameters:
addressReference to SocketAddress to write address to.
Returns:
true, if call was successful; false otherwise.
See also:
bind
connect
getSocketAddress

Get last received flow label.

Returns:
Last received flow label or 0, if there is no flow label.

Get last received traffic class.

Returns:
Last received traffic class or 0, if there is no traffic class.

Get flow label of the connection.

Returns:
Flow label of the connection or 0, if there is no flow label.
See also:
connect

Get traffic class of the connection.

Returns:
Traffic class of the connection or 0, if there is no traffic class.
See also:
connect

Get SO_BROADCAST option of socket.

Returns:
SO_BROADCAST value.

Get the socket's address. Note: A socket has to be bound to an address and port or connected to a peer first to let the socket have an address!

Parameters:
addressReference to SocketAddress to write address to.
Returns:
true, if call was successful; false otherwise.
See also:
bind
connect
getPeerAddress
integer Coral::Socket::getSocketOption ( const cardinal  level,
const cardinal  optionNumber,
void *  optionValue,
socklen_t *  optionLength 
) const [inline]

Get socket option (wrapper for getsockopt());

Parameters:
levelLevel (e.g. SOL_SOCKET).
optionNumberOption (e.g. SO_REUSEADDR).
optionValueMemory to store option got from getsockopt().
optionLengthMemory with size of option memory.
Returns:
Result from getsockopt().

Get SO_LINGER option of socket.

Returns:
SO_LINGER value.

Get SO_REUSEADDR option of socket.

Returns:
SO_REUSEADDR value.

Get system's socket descriptor. Warning: It is not recommended to manipulate the socket directly. Use Socket's methods instead.

Returns:
Socket descriptor.

Get TCP_NODELAY option of socket.

Returns:
TCP_NODELAY value.
void Coral::Socket::init ( ) [protected]
integer Coral::Socket::ioctl ( const integer  request,
const void *  argp 
) [inline]

Wrapper for ioctl().

Parameters:
requestRequest.
argpArgument.
Returns:
Result of ioctl() call.
bool Coral::Socket::listen ( const cardinal  backlog = 5)

Set socket to listen mode with given backlog (queue length for sockets waiting for acception).

Parameters:
backlogBacklog.
Returns:
true on success; false otherwise.
ssize_t Coral::Socket::read ( void *  buffer,
const size_t  length 
) [virtual]

Wrapper for read().

Parameters:
bufferBuffer to read data to.
lengthMaximum length of data to be received.
Returns:
Bytes read or error code < 0.
bool Coral::Socket::ready ( ) const [inline]

Check, if socket is ready.

Returns:
true, if socket is ready; false otherwise.
ssize_t Coral::Socket::receive ( void *  buffer,
const size_t  length,
const cardinal  flags = 0 
) [virtual]

Wrapper for recv().

Parameters:
bufferBuffer to read data to.
lengthMaximum length of data to be received.
flagsFlags for recv().
Returns:
Bytes read or error code < 0.
ssize_t Coral::Socket::receiveFrom ( void *  buffer,
const size_t  length,
SocketAddress sender,
const cardinal  flags = 0 
) [virtual]

Wrapper for recvfrom().

Parameters:
bufferBuffer to receive data to.
lengthMaximum length of data to be received.
senderAddress to store sender's address.
flagsFlags for recvfrom().
Returns:
Bytes received or error code < 0.
ssize_t Coral::Socket::recvFrom ( int  fd,
void *  buf,
const size_t  len,
const integer  flags,
struct sockaddr *  addr,
size_t *  addrlen 
) [protected]
bool Coral::Socket::renewFlow ( InternetFlow flow,
const cardinal  expires,
const cardinal  linger = 6 
)

Renew a flow label allocation with given expires and linger (default 6) values. The expires value gives the seconds to go until the flow label expires, the linger value gives the timeout in seconds the freed flow label cannot be allocated again.

Parameters:
flowFlow to be renewed.
expiresSeconds until the flow label expires.
lingerLinger (default 6).
Returns:
true on success; false otherwise.
bool Coral::Socket::renewFlow ( const cardinal  expires,
const cardinal  linger = 6 
)

Renew current flow's flow label allocation with given expires and linger (default 6) values.

Parameters:
expiresSeconds until the flow label expires.
lingerLinger (default 6).
Returns:
true on success; false otherwise.

Reset number of bytes received.

void Coral::Socket::resetBytesSent ( ) [inline]

Reset number of bytes sent.

ssize_t Coral::Socket::send ( const void *  buffer,
const size_t  length,
const cardinal  flags = 0,
const card8  trafficClass = 0 
) [virtual]

Wrapper for send(). send() will set the packet's traffic class, if trafficClass is not 0. In this case, the packet will be sent by sendto() to the destination address, the socket is connected to!

Parameters:
bufferBuffer with data to send.
lengthLength of data to send.
flagsFlags for sendto().
trafficClassTraffic class for packet.
Returns:
Bytes sent or error code < 0.
ssize_t Coral::Socket::sendTo ( const void *  buffer,
const size_t  length,
const cardinal  flags,
const SocketAddress receiver,
const card8  trafficClass = 0 
) [virtual]

Wrapper for sendto(). sendto() will set the packet's traffic class, if trafficClass is not 0.

Parameters:
bufferBuffer with data to send.
lengthLength of data to send.
flagsFlags for sendto().
receiverAddress of receiver.
Returns:
Bytes sent or error code < 0.
void Coral::Socket::setBlockingMode ( const bool  on)

Set blocking mode.

Parameters:
onTrue to set blocking mode, false to unset.
void Coral::Socket::setSoBroadcast ( const bool  on)

Set SO_BROADCAST option of socket.

Parameters:
ontrue to set SO_BROADCAST on; false otherwise.
integer Coral::Socket::setSocketOption ( const cardinal  level,
const cardinal  optionNumber,
const void *  optionValue,
const socklen_t  optionLength 
) [inline]

Get socket option (wrapper for getsockopt());

Parameters:
levelLevel (e.g. SOL_SOCKET).
optionNumberOption (e.g. SO_REUSEADDR).
optionValueMemory with option.
optionLengthLength of option memory.
Returns:
Result from setsockopt().
void Coral::Socket::setSoLinger ( const bool  on,
const cardinal  linger 
)

Set SO_LINGER option of socket.

Parameters:
ontrue to set linger on; false otherwise.
lingerSO_LINGER in seconds.
void Coral::Socket::setSoReuseAddress ( const bool  on)

Set SO_REUSEADDR option of socket.

Parameters:
ontrue to set SO_REUSEADDR on; false otherwise.
void Coral::Socket::setTCPNoDelay ( const bool  on)

Set TCP_NODELAY option of socket.

Parameters:
ontrue to set TCP_NODELAY on; false otherwise.
bool Coral::Socket::setTOS ( const card8  trafficClass) [protected]
void Coral::Socket::setTrafficConstraint ( const card8  trafficClass,
const card64  bandwidth,
const double  bufferDelay 
) [virtual]

Set traffic constraint to given byte rate. Packets exceeding the given constraint will be dropped. Note: This functionality has to be implemented by subclasses!

Parameters:
trafficClassTraffic class.
bandwidthBandwidth.
bufferDelayMaximum buffer delay in microseconds.
void Coral::Socket::shutdown ( const cardinal  shutdownLevel)

Shutdown full-duplex connection partial or completely. SHUT_RD - further receives will be disallowed. SHUT_WR - further sends will be disallowed. SHUT_RDWR - further sends and receives will be disallowed.

Parameters:
shutdownLevelSHUT_RD, SHUT_WR, SHUT_RDWR.
ssize_t Coral::Socket::write ( const void *  buffer,
const size_t  length 
) [virtual]

Wrapper for write().

Parameters:
bufferBuffer with data to write
lengthLength of data to write
Returns:
Bytes sent or error code < 0.

Friends And Related Function Documentation

friend class TrafficShaper [friend]

Member Data Documentation

sockaddr* Coral::Socket::Destination [protected]

Maximum port number for bind()'s automatic port selection.

See also:
bind

Minimum port number for bind()'s automatic port selection.

See also:
bind

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines