Logo
~Sockets~
~Examples~
~Contact~


SctpSocket Class Reference

#include <SctpSocket.h>

Inheritance diagram for SctpSocket:
Collaboration diagram for SctpSocket:

List of all members.


Public Member Functions

 SctpSocket (ISocketHandler &h, int type)
 SctpSocket constructor.
 ~SctpSocket ()
int Bind (const std::string &, port_t)
 bind()
int Bind (SocketAddress &)
int AddAddress (const std::string &, port_t)
 sctp_bindx()
int AddAddress (SocketAddress &)
int RemoveAddress (const std::string &, port_t)
 sctp_bindx()
int RemoveAddress (SocketAddress &)
int Open (const std::string &, port_t)
 connect()
int Open (SocketAddress &)
void OnConnectTimeout ()
 Connect timeout callback.
int AddConnection (const std::string &, port_t)
 sctp_connectx()
int AddConnection (SocketAddress &)
int getpaddrs (sctp_assoc_t id, std::list< std::string > &)
 Get peer addresses of an association.
int getladdrs (sctp_assoc_t id, std::list< std::string > &)
 Get all bound addresses of an association.
int PeelOff (sctp_assoc_t id)
 sctp_peeloff
virtual void OnReceiveMessage (const char *buf, size_t sz, struct sockaddr *sa, socklen_t sa_len, struct sctp_sndrcvinfo *sinfo, int msg_flags)=0
 recvmsg callback
void OnOptions (int, int, int, SOCKET)
 Called when a client socket is created, to set socket options.
virtual int Protocol ()
 Returns IPPROTO_TCP or IPPROTO_SCTP.

Protected Member Functions

 SctpSocket (const SctpSocket &s)
void OnRead ()
 Called when there is something to be read from the file descriptor.
void OnWrite ()
 Called when there is room for another write on the file descriptor.

Private Member Functions

SctpSocketoperator= (const SctpSocket &s)

Private Attributes

int m_type
 SCTP_STREAM or SCTP_SEQPACKET.
char * m_buf
 Temporary receive buffer.

Detailed Description

Definition at line 50 of file SctpSocket.h.


Constructor & Destructor Documentation

SctpSocket::SctpSocket ( ISocketHandler h,
int  type 
)

SctpSocket constructor.

Parameters:
h Owner
type SCTP_STREAM or SCTP_SEQPACKET

Definition at line 50 of file SctpSocket.cpp.

00050                                                  : StreamSocket(h)
00051 ,m_type(type)
00052 ,m_buf(new char[SCTP_BUFSIZE_READ])
00053 {
00054         if (type != SOCK_STREAM && type != SOCK_SEQPACKET)
00055         {
00056         }
00057 }

SctpSocket::~SctpSocket (  ) 

Definition at line 60 of file SctpSocket.cpp.

References m_buf.

00061 {
00062         delete[] m_buf;
00063 }

SctpSocket::SctpSocket ( const SctpSocket s  )  [inline, protected]

Definition at line 102 of file SctpSocket.h.

00102 : StreamSocket(s) {}


Member Function Documentation

int SctpSocket::Bind ( const std::string &  a,
port_t  p 
)

bind()

Definition at line 66 of file SctpSocket.cpp.

00067 {
00068 #ifdef ENABLE_IPV6
00069 #ifdef IPPROTO_IPV6
00070         if (IsIpv6())
00071         {
00072                 Ipv6Address ad(a, p);
00073                 return Bind(ad);
00074         }
00075 #endif
00076 #endif
00077         Ipv4Address ad(a, p);
00078         return Bind(ad);
00079 }

int SctpSocket::Bind ( SocketAddress ad  ) 

Definition at line 82 of file SctpSocket.cpp.

References Socket::Attach(), Socket::CreateSocket(), SocketAddress::GetFamily(), SocketAddress::GetPort(), Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, SocketAddress::IsValid(), Utility::l2string(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), and m_type.

00083 {
00084         if (!ad.IsValid())
00085         {
00086                 Handler().LogError(this, "SctpSocket", -1, "invalid address", LOG_LEVEL_ERROR);
00087                 return -1;
00088         }
00089         if (GetSocket() == INVALID_SOCKET)
00090         {
00091                 Attach(CreateSocket(ad.GetFamily(), m_type, "sctp"));
00092         }
00093         if (GetSocket() != INVALID_SOCKET)
00094         {
00095                 int n = bind(GetSocket(), ad, ad);
00096                 if (n == -1)
00097                 {
00098                         Handler().LogError(this, "SctpSocket", -1, "bind() failed", LOG_LEVEL_ERROR);
00099 #ifdef ENABLE_EXCEPTIONS
00100                         throw Exception("bind() failed for SctpSocket, port: " + Utility::l2string(ad.GetPort()));
00101 #endif
00102                 }
00103                 return n;
00104         }
00105         return -1;
00106 }

int SctpSocket::AddAddress ( const std::string &  a,
port_t  p 
)

sctp_bindx()

Definition at line 109 of file SctpSocket.cpp.

00110 {
00111 #ifdef ENABLE_IPV6
00112 #ifdef IPPROTO_IPV6
00113         if (IsIpv6())
00114         {
00115                 Ipv6Address ad(a, p);
00116                 return AddAddress(ad);
00117         }
00118 #endif
00119 #endif
00120         Ipv4Address ad(a, p);
00121         return AddAddress(ad);
00122 }

int SctpSocket::AddAddress ( SocketAddress ad  ) 

Definition at line 125 of file SctpSocket.cpp.

References Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, SocketAddress::IsValid(), LOG_LEVEL_ERROR, and ISocketHandler::LogError().

00126 {
00127         if (!ad.IsValid())
00128         {
00129                 Handler().LogError(this, "SctpSocket", -1, "invalid address", LOG_LEVEL_ERROR);
00130                 return -1;
00131         }
00132         if (GetSocket() == INVALID_SOCKET)
00133         {
00134                 Handler().LogError(this, "SctpSocket", -1, "AddAddress called with invalid file descriptor", LOG_LEVEL_ERROR);
00135                 return -1;
00136         }
00137         int n = sctp_bindx(GetSocket(), ad, ad, SCTP_BINDX_ADD_ADDR);
00138         if (n == -1)
00139         {
00140                 Handler().LogError(this, "SctpSocket", -1, "sctp_bindx() failed", LOG_LEVEL_ERROR);
00141         }
00142         return n;
00143 }

int SctpSocket::RemoveAddress ( const std::string &  a,
port_t  p 
)

sctp_bindx()

Definition at line 146 of file SctpSocket.cpp.

00147 {
00148 #ifdef ENABLE_IPV6
00149 #ifdef IPPROTO_IPV6
00150         if (IsIpv6())
00151         {
00152                 Ipv6Address ad(a, p);
00153                 return RemoveAddress(ad);
00154         }
00155 #endif
00156 #endif
00157         Ipv4Address ad(a, p);
00158         return RemoveAddress(ad);
00159 }

int SctpSocket::RemoveAddress ( SocketAddress ad  ) 

Definition at line 162 of file SctpSocket.cpp.

References Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, SocketAddress::IsValid(), LOG_LEVEL_ERROR, and ISocketHandler::LogError().

00163 {
00164         if (!ad.IsValid())
00165         {
00166                 Handler().LogError(this, "SctpSocket", -1, "invalid address", LOG_LEVEL_ERROR);
00167                 return -1;
00168         }
00169         if (GetSocket() == INVALID_SOCKET)
00170         {
00171                 Handler().LogError(this, "SctpSocket", -1, "RemoveAddress called with invalid file descriptor", LOG_LEVEL_ERROR);
00172                 return -1;
00173         }
00174         int n = sctp_bindx(GetSocket(), ad, ad, SCTP_BINDX_REM_ADDR);
00175         if (n == -1)
00176         {
00177                 Handler().LogError(this, "SctpSocket", -1, "sctp_bindx() failed", LOG_LEVEL_ERROR);
00178         }
00179         return n;
00180 }

int SctpSocket::Open ( const std::string &  a,
port_t  p 
)

connect()

Definition at line 183 of file SctpSocket.cpp.

00184 {
00185 #ifdef ENABLE_IPV6
00186 #ifdef IPPROTO_IPV6
00187         if (IsIpv6())
00188         {
00189                 Ipv6Address ad(a, p);
00190                 return Open(ad);
00191         }
00192 #endif
00193 #endif
00194         Ipv4Address ad(a, p);
00195         return Open(ad);
00196 }

int SctpSocket::Open ( SocketAddress ad  ) 

Definition at line 199 of file SctpSocket.cpp.

References Socket::Attach(), Socket::CreateSocket(), Errno, SocketAddress::GetFamily(), Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, SocketAddress::IsValid(), LOG_LEVEL_ERROR, LOG_LEVEL_INFO, ISocketHandler::LogError(), m_type, StreamSocket::SetConnecting(), Socket::SetNonblocking(), and StrError.

00200 {
00201         if (!ad.IsValid())
00202         {
00203                 Handler().LogError(this, "SctpSocket", -1, "invalid address", LOG_LEVEL_ERROR);
00204                 return -1;
00205         }
00206         if (GetSocket() == INVALID_SOCKET)
00207         {
00208                 Attach(CreateSocket(ad.GetFamily(), m_type, "sctp"));
00209         }
00210         if (GetSocket() != INVALID_SOCKET)
00211         {
00212                 if (!SetNonblocking(true))
00213                 {
00214                         return -1;
00215                 }
00216                 int n = connect(GetSocket(), ad, ad);
00217                 if (n == -1)
00218                 {
00219                         // check error code that means a connect is in progress
00220 #ifdef _WIN32
00221                         if (Errno == WSAEWOULDBLOCK)
00222 #else
00223                         if (Errno == EINPROGRESS)
00224 #endif
00225                         {
00226                                 Handler().LogError(this, "connect: connection pending", Errno, StrError(Errno), LOG_LEVEL_INFO);
00227                                 SetConnecting( true ); // this flag will control fd_set's
00228                         }
00229                         else
00230                         {
00231                                 Handler().LogError(this, "SctpSocket", -1, "connect() failed", LOG_LEVEL_ERROR);
00232                         }
00233                 }
00234                 return n;
00235         }
00236         return -1;
00237 }

void SctpSocket::OnConnectTimeout (  )  [virtual]

Connect timeout callback.

Todo:
state reason why connect failed

Todo:
state reason why connect failed

Reimplemented from Socket.

Definition at line 420 of file SctpSocket.cpp.

References StreamSocket::GetConnectionRetries(), StreamSocket::GetConnectionRetry(), Socket::Handler(), StreamSocket::IncreaseConnectionRetries(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), Socket::OnConnectFailed(), Socket::OnConnectRetry(), Socket::OnSocks4ConnectFailed(), Socket::SetCloseAndDelete(), StreamSocket::SetConnecting(), Socket::SetRetryClientConnect(), and Socket::Socks4().

00421 {
00422         Handler().LogError(this, "connect", -1, "connect timeout", LOG_LEVEL_FATAL);
00423 #ifdef ENABLE_SOCKS4
00424         if (Socks4())
00425         {
00426                 OnSocks4ConnectFailed();
00427                 // retry direct connection
00428         }
00429         else
00430 #endif
00431         if (GetConnectionRetry() == -1 ||
00432                 (GetConnectionRetry() && GetConnectionRetries() < GetConnectionRetry()) )
00433         {
00434                 IncreaseConnectionRetries();
00435                 // ask socket via OnConnectRetry callback if we should continue trying
00436                 if (OnConnectRetry())
00437                 {
00438                         SetRetryClientConnect();
00439                 }
00440                 else
00441                 {
00442                         SetCloseAndDelete( true );
00444                         OnConnectFailed();
00445                 }
00446         }
00447         else
00448         {
00449                 SetCloseAndDelete(true);
00451                 OnConnectFailed();
00452         }
00453         //
00454         SetConnecting(false);
00455 }

int SctpSocket::AddConnection ( const std::string &  a,
port_t  p 
)

sctp_connectx()

Definition at line 241 of file SctpSocket.cpp.

00242 {
00243 #ifdef ENABLE_IPV6
00244 #ifdef IPPROTO_IPV6
00245         if (IsIpv6())
00246         {
00247                 Ipv6Address ad(a, p);
00248                 return AddConnection(ad);
00249         }
00250 #endif
00251 #endif
00252         Ipv4Address ad(a, p);
00253         return AddConnection(ad);
00254 }

int SctpSocket::AddConnection ( SocketAddress ad  ) 

Definition at line 257 of file SctpSocket.cpp.

References Socket::GetSocket(), Socket::Handler(), INVALID_SOCKET, SocketAddress::IsValid(), LOG_LEVEL_ERROR, ISocketHandler::LogError(), and StreamSocket::SetConnecting().

00258 {
00259         if (!ad.IsValid())
00260         {
00261                 Handler().LogError(this, "SctpSocket", -1, "invalid address", LOG_LEVEL_ERROR);
00262                 return -1;
00263         }
00264         if (GetSocket() == INVALID_SOCKET)
00265         {
00266                 Handler().LogError(this, "SctpSocket", -1, "AddConnection called with invalid file descriptor", LOG_LEVEL_ERROR);
00267                 return -1;
00268         }
00269         int n = sctp_connectx(GetSocket(), ad, ad);
00270         if (n == -1)
00271         {
00272                 Handler().LogError(this, "SctpSocket", -1, "sctp_connectx() failed", LOG_LEVEL_ERROR);
00273         }
00274         else
00275         {
00276                 SetConnecting();
00277         }
00278         return n;
00279 }

int SctpSocket::getpaddrs ( sctp_assoc_t  id,
std::list< std::string > &  vec 
)

Get peer addresses of an association.

Definition at line 283 of file SctpSocket.cpp.

References Socket::GetSocket(), Socket::Handler(), LOG_LEVEL_WARNING, ISocketHandler::LogError(), and Utility::Sa2String().

00284 {
00285         struct sockaddr *p = NULL;
00286         int n = sctp_getpaddrs(GetSocket(), id, &p);
00287         if (!n || n == -1)
00288         {
00289                 Handler().LogError(this, "SctpSocket", -1, "sctp_getpaddrs failed", LOG_LEVEL_WARNING);
00290                 return n;
00291         }
00292         for (int i = 0; i < n; i++)
00293         {
00294                 vec.push_back(Utility::Sa2String(&p[i]));
00295         }
00296         sctp_freepaddrs(p);
00297         return n;
00298 }

int SctpSocket::getladdrs ( sctp_assoc_t  id,
std::list< std::string > &  vec 
)

Get all bound addresses of an association.

Definition at line 301 of file SctpSocket.cpp.

References Socket::GetSocket(), Socket::Handler(), LOG_LEVEL_WARNING, ISocketHandler::LogError(), and Utility::Sa2String().

00302 {
00303         struct sockaddr *p = NULL;
00304         int n = sctp_getladdrs(GetSocket(), id, &p);
00305         if (!n || n == -1)
00306         {
00307                 Handler().LogError(this, "SctpSocket", -1, "sctp_getladdrs failed", LOG_LEVEL_WARNING);
00308                 return n;
00309         }
00310         for (int i = 0; i < n; i++)
00311         {
00312                 vec.push_back(Utility::Sa2String(&p[i]));
00313         }
00314         sctp_freeladdrs(p);
00315         return n;
00316 }

int SctpSocket::PeelOff ( sctp_assoc_t  id  ) 

sctp_peeloff

Definition at line 319 of file SctpSocket.cpp.

References ISocketHandler::Add(), Socket::Attach(), Socket::Create(), Socket::GetSocket(), Socket::Handler(), LOG_LEVEL_WARNING, ISocketHandler::LogError(), and Socket::SetDeleteByHandler().

00320 {
00321         int n = sctp_peeloff(GetSocket(), id);
00322         if (n == -1)
00323         {
00324                 Handler().LogError(this, "SctpSocket", -1, "PeelOff failed", LOG_LEVEL_WARNING);
00325                 return -1;
00326         }
00327         Socket *p = Create();
00328         p -> Attach(n);
00329         p -> SetDeleteByHandler();
00330         Handler().Add(p);
00331         return n;
00332 }

void SctpSocket::OnReceiveMessage ( const char *  buf,
size_t  sz,
struct sockaddr *  sa,
socklen_t  sa_len,
struct sctp_sndrcvinfo *  sinfo,
int  msg_flags 
) [pure virtual]

recvmsg callback

Definition at line 371 of file SctpSocket.cpp.

Referenced by OnRead().

00372 {
00373 }

void SctpSocket::OnOptions ( int  family,
int  type,
int  protocol,
SOCKET  s 
) [inline, virtual]

Called when a client socket is created, to set socket options.

Parameters:
family AF_INET, AF_INET6, etc
type SOCK_STREAM, SOCK_DGRAM, etc
protocol Protocol number (tcp, udp, sctp, etc)
s Socket file descriptor

Implements Socket.

Definition at line 97 of file SctpSocket.h.

00097 {}

int SctpSocket::Protocol (  )  [virtual]

Returns IPPROTO_TCP or IPPROTO_SCTP.

Implements StreamSocket.

Definition at line 494 of file SctpSocket.cpp.

00495 {
00496         return IPPROTO_SCTP;
00497 }

void SctpSocket::OnRead (  )  [protected, virtual]

Called when there is something to be read from the file descriptor.

Reimplemented from Socket.

Definition at line 335 of file SctpSocket.cpp.

References Errno, Socket::GetSocket(), Socket::Handler(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_buf, OnReceiveMessage(), SCTP_BUFSIZE_READ, Socket::SetCloseAndDelete(), and StrError.

00336 {
00337 /*
00338         int sctp_recvmsg(int sd, void * msg, size_t * len,
00339                 struct sockaddr * from, socklen_t * fromlen,
00340                 struct sctp_sndrcvinfo * sinfo, int * msg_flags);
00341 
00342         DESCRIPTION
00343         sctp_recvmsg  is  a  wrapper library function that can be used to receive a message from a socket while using the advanced
00344         features of SCTP.  sd is the socket descriptor on which the message pointed to by msg of length len is received.
00345 
00346         If from is not NULL, the source address of the message is filled in. The argument fromlen  is  a  value-result  parameter.
00347         initialized  to  the  size  of the buffer associated with from , and modified on return to indicate the actual size of the
00348         address stored.
00349 
00350         sinfo is a pointer to a sctp_sndrcvinfo structure to be filled upon receipt of the message.  msg_flags is a pointer  to  a
00351         integer that is filled with any message flags like MSG_NOTIFICATION or MSG_EOR.
00352 
00353 */
00354         struct sockaddr sa;
00355         socklen_t sa_len = 0;
00356         struct sctp_sndrcvinfo sinfo;
00357         int flags = 0;
00358         int n = sctp_recvmsg(GetSocket(), m_buf, SCTP_BUFSIZE_READ, &sa, &sa_len, &sinfo, &flags);
00359         if (n == -1)
00360         {
00361                 Handler().LogError(this, "SctpSocket", Errno, StrError(Errno), LOG_LEVEL_FATAL);
00362                 SetCloseAndDelete();
00363         }
00364         else
00365         {
00366                 OnReceiveMessage(m_buf, n, &sa, sa_len, &sinfo, flags);
00367         }
00368 }

void SctpSocket::OnWrite (  )  [protected, virtual]

Called when there is room for another write on the file descriptor.

Reimplemented from Socket.

Definition at line 376 of file SctpSocket.cpp.

References StreamSocket::Connecting(), StreamSocket::GetConnectionRetries(), StreamSocket::GetConnectionRetry(), Socket::Handler(), Socket::IsDisableRead(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), Socket::OnConnectFailed(), Socket::OnSocks4ConnectFailed(), Socket::SetCallOnConnect(), Socket::SetCloseAndDelete(), StreamSocket::SetConnecting(), Socket::Socks4(), Socket::SoError(), and StrError.

00377 {
00378         if (Connecting())
00379         {
00380                 int err = SoError();
00381 
00382                 // don't reset connecting flag on error here, we want the OnConnectFailed timeout later on
00384                 if (!err) // ok
00385                 {
00386                         Set(!IsDisableRead(), false);
00387                         SetConnecting(false);
00388                         SetCallOnConnect();
00389                         return;
00390                 }
00391                 Handler().LogError(this, "sctp: connect failed", err, StrError(err), LOG_LEVEL_FATAL);
00392                 Set(false, false); // no more monitoring because connection failed
00393 
00394                 // failed
00395 #ifdef ENABLE_SOCKS4
00396                 if (Socks4())
00397                 {
00398                         OnSocks4ConnectFailed();
00399                         return;
00400                 }
00401 #endif
00402                 if (GetConnectionRetry() == -1 ||
00403                         (GetConnectionRetry() && GetConnectionRetries() < GetConnectionRetry()) )
00404                 {
00405                         // even though the connection failed at once, only retry after
00406                         // the connection timeout.
00407                         // should we even try to connect again, when CheckConnect returns
00408                         // false it's because of a connection error - not a timeout...
00409                         return;
00410                 }
00411                 SetConnecting(false);
00412                 SetCloseAndDelete( true );
00414                 OnConnectFailed();
00415                 return;
00416         }
00417 }

SctpSocket& SctpSocket::operator= ( const SctpSocket s  )  [inline, private]

Definition at line 107 of file SctpSocket.h.

00107 { return *this; }


Member Data Documentation

int SctpSocket::m_type [private]

SCTP_STREAM or SCTP_SEQPACKET.

Definition at line 108 of file SctpSocket.h.

Referenced by Bind(), and Open().

char* SctpSocket::m_buf [private]

Temporary receive buffer.

Definition at line 109 of file SctpSocket.h.

Referenced by OnRead(), and ~SctpSocket().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4