Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CODEX_SSL::SSLSocket Class Reference

This class handles sockets using Secure Sockets Layer (SSL). More...

#include <SSLSocket.h>

Inheritance diagram for CODEX_SSL::SSLSocket:

Inheritance graph
[legend]
Collaboration diagram for CODEX_SSL::SSLSocket:

Collaboration graph
[legend]
List of all members.

Public Types

typedef std::queue< Message * > MsgQueueType
 Outgoing messages are kept in a queue.
enum  StateType { kRead, kWrite, kError }
 Type of state to check.

Public Member Functions

 SSLSocket (SSL_CTX *ctx, int domain=PF_INET, int type=SOCK_STREAM, int protocol=0, bool blocking=false)
 Constructor for SSL sockets.
 SSLSocket (const SSLSocket &aOther)
 Copy constructor.
virtual ~SSLSocket ()
 Destructor.
int set_fd (fd_set *fd_bitmap, StateType s) const
 Fill file descriptor bitmap.
bool isset_fd (const fd_set *fd_bitmap, StateType s) const
 Check file descriptor bitmap.
size_t readFrom (void *output, size_t maxSize=1024) const
 This reimplements CODEX_Quorum::SocketBase::readFrom() to handle OpenSSL's I/O.
Socket Creation and Maintenance
These functions, including constructors and a destructor, create, maintain, and destroy sockets.

In addition to construction, this also includes setting up sockets to receive incoming connections and establishing incoming and outgoing connections.

virtual void setup (int port, int backlog)
 This version of SocketBase::setup() assumes an IPv4 socket.
virtual void setup (struct sockaddr *my_addr, socklen_t addrlen, int backlog)
 This version of SocketBase::setup() allows for other varieties of sockets aside from IPv4.
virtual void connect (const RemoteServer &server)
 This replaces the standard C connect(2) function, calling the standard function in the base class (with all arguments handled transparently to the user) and allowing refinements in derived classes.
virtual SocketBaseaccept ()
 This replaces the standard C accept(2) function, calling the standard function in the base class (with all arguments handled transparently to the user) and allowing refinements in derived classes.
Socket I/O
These functions handle basic socket I/O.

virtual size_t readAll (Message &msg, size_t length=0) const
 This method reads an entire message from the socket, using the message length information encoded in the packet.
virtual void writeTo (const Message &input) const
 Write data to a socket.
virtual int set_fd (fd_set *fd_bitmap, StateType s) const
 Fill file descriptor bitmap.
virtual bool isset_fd (const fd_set *fd_bitmap, StateType s) const
 Check file descriptor bitmap.
virtual void flush () const
 Force a blocking write of the internal buffer.

Protected Member Functions

CODEX_Quorum::SocketBaseclone ()
 Subclass-aware object duplication.
void connect (const CODEX_Quorum::RemoteServer &server)
 This reimplements CODEX_Quorum::SocketBase::connect() to add SSL configuration.
void finish_accept ()
 This reimplements CODEX_Quorum::SocketBase::finish_accept() to add SSL configuration.
int internal_write (const unsigned char *output, size_t maxSize) const
 This reimplements CODEX_Quorum::SocketBase::internal_write() to handle OpenSSL's I/O.
Protected Setup and Maintenance Methods
These methods implement the functionality of their non-protected counterparts.

This allows derived classes to use the public methods without having to fully reimplement the base class methods.

virtual SocketBaseprotected_accept ()
 
Exceptions:
QSESocketBaseAcceptFailed 

virtual void protected_bind (int port)
virtual void protected_bind (struct sockaddr *my_addr, socklen_t addrlen)
 
Exceptions:
QSESocketBaseCannotBind 

virtual void protected_listen (int backlog)
 
Exceptions:
QSESocketBaseListenFailed 

void setSocket (int socketFD)
void setBacklog (int backlog)
void setPort (int port)
Protected Accessors
These accessor methods allow derived classes to read, but not modify, private member data.

int domain () const
int type () const
int protocol () const
bool blocking () const
int port () const
int backlog () const
int socket () const

Protected Attributes

MsgQueueType m_msgQueue
 Message queue, which allows us to do in-order non-blocking writes.
unsigned int m_msgOffset
 Offset into the buffer of the first message on the queue.

Detailed Description

This class handles sockets using Secure Sockets Layer (SSL).

It is important to note that this class does not handle all aspects of setting up SSL, only the specific socket actions. Initializing SSL, maintaining certificates, and so forth will still need to be taken care of externally.

Definition at line 42 of file SSLSocket.h.


Constructor & Destructor Documentation

SSLSocket::SSLSocket SSL_CTX *  ctx,
int  domain = PF_INET,
int  type = SOCK_STREAM,
int  protocol = 0,
bool  blocking = false
 

Constructor for SSL sockets.

Parameters:
ctx SSL context structure describing the environment relevant to SSL operations. The context must be created before passing it to SSLSocket. This class does not take possession of the memory, since multiple sockets may be created using the same context.
domain see SocketBase::SocketBase
type see SocketBase::SocketBase
protocol see SocketBase::SocketBase
blocking see SocketBase::SocketBase

Definition at line 31 of file SSLSocket.cc.

Referenced by clone().


Member Function Documentation

SocketBase * SocketBase::accept  )  [virtual, inherited]
 

This replaces the standard C accept(2) function, calling the standard function in the base class (with all arguments handled transparently to the user) and allowing refinements in derived classes.

The connection information is held within the member data of SocketBase. As the standard accept(2) returns a new socket filehandle, this version acts as a clone function. The bulk of the work is handled in the protected member function SocketBase::protected_accept(), which should be called by derived-class reimplementations of SocketBase::accept().

Returns:
pointer to a SocketBase containing connection information. This should be deleted by the caller when no longer needed.
Exceptions:
QSESocketBaseAcceptFailed if the call to accept(2) fails. Note that this might not be a fatal error for a non-blocking socket, in which case an invocation may look like:
try
{
   SocketBase* newConnection = listeningSocket->accept();
   // read from socket newConnection
}
catch( QSESocketBaseAcceptFailed& err )
{
   // no data to read
}

Reimplemented in CODEX_Quorum::LoopbackSocket.

Definition at line 97 of file Socket.cc.

References CODEX_Quorum::SocketBase::protected_accept().

Referenced by CODEX_Quorum::EchoServer::processRequest().

SocketBase * SSLSocket::clone  )  [protected, virtual]
 

Subclass-aware object duplication.

This is similar to a copy constructor except that, as a virtual function, it can be overridden in order for derived classes to return copies of themselves that have the correct type, yet which use an identical interface from the base class. Each class in the SocketBase hierarchy overrides this method to use its own copy constructor.

Reimplemented from CODEX_Quorum::SocketBase.

Definition at line 216 of file SSLSocket.cc.

References SSLSocket().

void SocketBase::connect const RemoteServer server  )  [virtual, inherited]
 

This replaces the standard C connect(2) function, calling the standard function in the base class (with all arguments handled transparently to the user) and allowing refinements in derived classes.

Parameters:
server Object defining the server to which this socket should connect
Return values:
true if connection is successful
false if connection fails

Reimplemented in CODEX_Quorum::LoopbackSocket.

Definition at line 88 of file Socket.cc.

Referenced by connect(), and CODEX_Quorum::RemoteServer::socket().

void SocketBase::flush  )  const [virtual, inherited]
 

Force a blocking write of the internal buffer.

Reads are not affected. Single-threaded servers should not call this, but it's fine for multi-threaded servers and clients that want to block until they get a response.

Reimplemented in CODEX_Quorum::LoopbackSocket.

Definition at line 282 of file Socket.cc.

References CODEX_Quorum::Message::buffer(), CODEX_Quorum::SocketBase::internal_write(), CODEX_Quorum::Message::length(), CODEX_Quorum::SocketBase::m_msgOffset, and CODEX_Quorum::SocketBase::m_msgQueue.

Referenced by CODEX_Quorum::RemoteServer::flushSocket().

bool SocketBase::isset_fd const fd_set *  fd_bitmap,
StateType  s
const [virtual, inherited]
 

Check file descriptor bitmap.

Parameters:
fd_bitmap bitmap of file descriptors to check. This is typically a fd_set returned by select(2), and will have the bit corresponding to the socket file descriptor set to 1 if the relevant action is appropriate.
s state of the socket to be tested. This allows derived classes to manipulate the return value appropriately when there is additional socket state to consider.
Return values:
true the bit is set
false the bit is not set

Definition at line 242 of file Socket.cc.

References CODEX_Quorum::Message::buffer(), CODEX_Quorum::SocketBase::internal_write(), CODEX_Quorum::Message::length(), CODEX_Quorum::SocketBase::m_msgOffset, CODEX_Quorum::SocketBase::m_msgQueue, and CODEX_Quorum::SocketBase::set_fd().

Referenced by isset_fd(), CODEX_Quorum::RemoteServer::isset_fd(), CODEX_Quorum::LocalServer::isset_fd(), CODEX_Quorum::EchoServer::processRequest(), and CODEX_Quorum::SocketBase::readAll().

bool SSLSocket::isset_fd const fd_set *  fd_bitmap,
StateType  s
const
 

Check file descriptor bitmap.

The base-class method will be called initially to determine if the socket needs to be examined.

Parameters:
fd_bitmap bitmap of file descriptors to check. This is typically a fd_set returned by select(2), and will have the bit corresponding to the socket file descriptor set to 1 if the relevant action is appropriate.
s state of the socket to be tested. Because SSL maintains state for whether a socket expects a read or a write, so this parameter is used to determine whether the socket is in fact ready for use or whether the socket activity reported by select(2) indicates a state change.
Return values:
true the bit is set
false the bit is not set

Definition at line 91 of file SSLSocket.cc.

References CODEX_Quorum::SocketBase::isset_fd().

void SocketBase::protected_bind struct sockaddr *  my_addr,
socklen_t  addrlen
[protected, virtual, inherited]
 

Exceptions:
QSESocketBaseCannotBind 

Bug:
This may not be the right place to call setsockopt(2), and it may not always be appropriate to call it.

Todo:
Make a real setsockopt interface.

Definition at line 402 of file Socket.cc.

Referenced by CODEX_Quorum::SocketBase::setup().

size_t SocketBase::readAll Message msg,
size_t  length = 0
const [virtual, inherited]
 

This method reads an entire message from the socket, using the message length information encoded in the packet.

If reading the entire message would block, the available data is put into msg and the remaining number of bytes to be read is returned. The message passed in will be appended to, so in the case where a read would have blocked, the Message can be passed to readAll() again when more data becomes available.

Parameters:
msg Buffer for the message data
length Number of bytes to read, or 0 if unknown
Returns:
Number of bytes left to read in this message
Exceptions:
QSESocketBaseSocketClosed The socket is closed.
QSESocketBaseMessageTooLong The incoming message has a length field greater than 4GB.
QSESocketBaseMessageTooShort The buffer read from the socket is not long enough to contain any data.
QSESocketBaseReadFailed select(2) returned an error or readFrom() threw it.

Definition at line 119 of file Socket.cc.

References CODEX_Quorum::SocketBase::isset_fd(), CODEX_Quorum::SocketBase::readFrom(), and CODEX_Quorum::SocketBase::set_fd().

Referenced by CODEX_Quorum::RemoteServer::receiveFrom().

int SocketBase::set_fd fd_set *  fd_bitmap,
StateType  s
const [virtual, inherited]
 

Fill file descriptor bitmap.

Parameters:
fd_bitmap bitmap of file descriptors to modify. The bit corresponding to the socket file descriptor will be set to 1.
s state of the socket to be tested. This allows derived classes to manipulate the bitmap appropriately when there is additional socket state to consider.
Returns:
file descriptor of the socket

Definition at line 235 of file Socket.cc.

Referenced by CODEX_Quorum::SocketBase::isset_fd(), CODEX_Quorum::EchoServer::processRequest(), CODEX_Quorum::SocketBase::readAll(), set_fd(), CODEX_Quorum::RemoteServer::set_fd(), and CODEX_Quorum::LocalServer::set_fd().

int SSLSocket::set_fd fd_set *  fd_bitmap,
StateType  s
const
 

Fill file descriptor bitmap.

The base-class method will be called if it is determined that setting the bitmap is appropriate.

Parameters:
fd_bitmap bitmap of file descriptors to modify. The bit corresponding to the socket file descriptor will be set to 1.
s state of the socket to be tested. Because SSL maintains state for whether a socket expects a read or a write, passing in the StateType allows us to set the bitmap or not according to whether a read or write is currently permitted.
Returns:
file descriptor of the socket

Definition at line 65 of file SSLSocket.cc.

References CODEX_Quorum::SocketBase::set_fd().

void SocketBase::setup struct sockaddr *  my_addr,
socklen_t  addrlen,
int  backlog
[virtual, inherited]
 

This version of SocketBase::setup() allows for other varieties of sockets aside from IPv4.

The arguments provided conform to the arguments of bind(2) and listen(2).

Reimplemented in CODEX_Quorum::LoopbackSocket.

Definition at line 79 of file Socket.cc.

References CODEX_Quorum::SocketBase::protected_bind(), and CODEX_Quorum::SocketBase::protected_listen().

void SocketBase::setup int  port,
int  backlog
[virtual, inherited]
 

This version of SocketBase::setup() assumes an IPv4 socket.

It will use AF_INET and INADDR_ANY when bind(2)ing. Protected member functions SocketBase::protected_bind() and SocketBase::protected_listen() are used for configuration, so that derived classes may reimplement them.

Parameters:
port Local port on which to bind.
backlog Maximum number of pending connections in the queue.
Exceptions:
QSESocketBaseCannotBind if the call to bind(2) fails.
QSESocketBaseListenFailed if the call to listen(2) fails.

Reimplemented in CODEX_Quorum::LoopbackSocket.

Definition at line 72 of file Socket.cc.

References CODEX_Quorum::SocketBase::protected_bind(), and CODEX_Quorum::SocketBase::protected_listen().

Referenced by CODEX_Quorum::LocalServer::enable().

void SocketBase::writeTo const Message input  )  const [virtual, inherited]
 

Write data to a socket.

Parameters:
input Message to queue for writing.
Exceptions:
QSESocketBaseWriteFailed An error occurred while writing.

Definition at line 212 of file Socket.cc.

References CODEX_Quorum::Message::fill(), and CODEX_Quorum::SocketBase::m_msgQueue.

Referenced by CODEX_Quorum::EchoServer::processRequest(), and CODEX_Quorum::RemoteServer::sendTo().


The documentation for this class was generated from the following files:
Generated on Fri May 6 17:42:50 2005 for COrnell Data EXchange (CODEX) by  doxygen 1.4.1