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 421 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().

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

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         int n = sctp_connectx(GetSocket(), ad, 1, NULL);
00271         if (n == -1)
00272         {
00273                 Handler().LogError(this, "SctpSocket", -1, "sctp_connectx() failed", LOG_LEVEL_ERROR);
00274         }
00275         else
00276         {
00277                 SetConnecting();
00278         }
00279         return n;
00280 }

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

Get peer addresses of an association.

Definition at line 284 of file SctpSocket.cpp.

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

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

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

Get all bound addresses of an association.

Definition at line 302 of file SctpSocket.cpp.

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

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

int SctpSocket::PeelOff ( sctp_assoc_t  id  ) 

sctp_peeloff

Definition at line 320 of file SctpSocket.cpp.

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

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

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 372 of file SctpSocket.cpp.

Referenced by OnRead().

00373 {
00374 }

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 495 of file SctpSocket.cpp.

00496 {
00497         return IPPROTO_SCTP;
00498 }

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

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

Reimplemented from Socket.

Definition at line 336 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.

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

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

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

Reimplemented from Socket.

Definition at line 377 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.

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

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