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

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

SctpSocket::~SctpSocket (  ) 

Definition at line 58 of file SctpSocket.cpp.

References m_buf.

00059 {
00060         delete[] m_buf;
00061 }

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

Definition at line 100 of file SctpSocket.h.

00100 : StreamSocket(s) {}


Member Function Documentation

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

bind()

Definition at line 64 of file SctpSocket.cpp.

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

int SctpSocket::Bind ( SocketAddress ad  ) 

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

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

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

sctp_bindx()

Definition at line 107 of file SctpSocket.cpp.

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

int SctpSocket::AddAddress ( SocketAddress ad  ) 

Definition at line 123 of file SctpSocket.cpp.

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

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

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

sctp_bindx()

Definition at line 144 of file SctpSocket.cpp.

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

int SctpSocket::RemoveAddress ( SocketAddress ad  ) 

Definition at line 160 of file SctpSocket.cpp.

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

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

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

connect()

Definition at line 181 of file SctpSocket.cpp.

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

int SctpSocket::Open ( SocketAddress ad  ) 

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

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

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 418 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(), StreamSocket::SetRetryClientConnect(), and Socket::Socks4().

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

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

sctp_connectx()

Definition at line 239 of file SctpSocket.cpp.

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

int SctpSocket::AddConnection ( SocketAddress ad  ) 

Definition at line 255 of file SctpSocket.cpp.

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

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

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

Get peer addresses of an association.

Definition at line 281 of file SctpSocket.cpp.

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

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

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

Get all bound addresses of an association.

Definition at line 299 of file SctpSocket.cpp.

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

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

int SctpSocket::PeelOff ( sctp_assoc_t  id  ) 

sctp_peeloff

Definition at line 317 of file SctpSocket.cpp.

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

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

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

Referenced by OnRead().

00370 {
00371 }

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 95 of file SctpSocket.h.

00095 {}

int SctpSocket::Protocol (  )  [virtual]

Returns IPPROTO_TCP or IPPROTO_SCTP.

Implements StreamSocket.

Definition at line 492 of file SctpSocket.cpp.

00493 {
00494         return IPPROTO_SCTP;
00495 }

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

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

Reimplemented from Socket.

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

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

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

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

Reimplemented from Socket.

Definition at line 374 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::Set(), StreamSocket::SetCallOnConnect(), Socket::SetCloseAndDelete(), StreamSocket::SetConnecting(), Socket::Socks4(), Socket::SoError(), and StrError.

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

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

Definition at line 105 of file SctpSocket.h.

00105 { return *this; }


Member Data Documentation

int SctpSocket::m_type [private]

SCTP_STREAM or SCTP_SEQPACKET.

Definition at line 106 of file SctpSocket.h.

Referenced by Bind(), and Open().

char* SctpSocket::m_buf [private]

Temporary receive buffer.

Definition at line 107 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