1 #ifndef CRYPTOPP_SOCKETFT_H
2 #define CRYPTOPP_SOCKETFT_H
6 #if !defined(NO_OS_DEPENDENCE) && defined(SOCKETS_AVAILABLE)
12 #ifdef USE_WINDOWS_STYLE_SOCKETS
13 # if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
14 # error Winsock 1 is not supported by this library. Please include this file or winsock2.h before windows.h.
21 #include <sys/types.h>
22 #include <sys/socket.h>
26 NAMESPACE_BEGIN(CryptoPP)
28 #ifdef USE_WINDOWS_STYLE_SOCKETS
29 typedef ::SOCKET socket_t;
32 const socket_t INVALID_SOCKET = -1;
34 const int SD_RECEIVE = 0;
35 const int SD_SEND = 1;
36 const int SD_BOTH = 2;
37 const int SOCKET_ERROR = -1;
41 typedef TYPE_OF_SOCKLEN_T socklen_t;
52 Err(socket_t s,
const std::string& operation,
int error);
53 socket_t GetSocket()
const {
return m_s;}
59 Socket(socket_t s = INVALID_SOCKET,
bool own=
false) : m_s(s), m_own(own) {}
63 bool GetOwnership()
const {
return m_own;}
64 void SetOwnership(
bool own) {m_own = own;}
66 operator socket_t() {
return m_s;}
67 socket_t GetSocket()
const {
return m_s;}
68 void AttachSocket(socket_t s,
bool own=
false);
69 socket_t DetachSocket();
72 void Create(
int nType = SOCK_STREAM);
73 void Bind(
unsigned int port,
const char *addr=NULL);
74 void Bind(
const sockaddr* psa, socklen_t saLen);
75 void Listen(
int backlog=5);
78 bool Connect(
const char *addr,
unsigned int port);
79 bool Connect(
const sockaddr* psa, socklen_t saLen);
80 bool Accept(
Socket& s, sockaddr *psa=NULL, socklen_t *psaLen=NULL);
81 void GetSockName(sockaddr *psa, socklen_t *psaLen);
82 void GetPeerName(sockaddr *psa, socklen_t *psaLen);
83 unsigned int Send(
const byte* buf,
size_t bufLen,
int flags=0);
84 unsigned int Receive(byte* buf,
size_t bufLen,
int flags=0);
85 void ShutDown(
int how = SD_SEND);
87 void IOCtl(
long cmd,
unsigned long *argp);
88 bool SendReady(
const timeval *timeout);
89 bool ReceiveReady(
const timeval *timeout);
91 virtual void HandleError(
const char *operation)
const;
92 void CheckAndHandleError_int(
const char *operation,
int result)
const
93 {
if (result == SOCKET_ERROR) HandleError(operation);}
94 void CheckAndHandleError(
const char *operation, socket_t result)
const
95 {
if (result == static_cast<socket_t>(SOCKET_ERROR)) HandleError(operation);}
96 #ifdef USE_WINDOWS_STYLE_SOCKETS
97 void CheckAndHandleError(
const char *operation, BOOL result)
const
98 {
if (!result) HandleError(operation);}
99 void CheckAndHandleError(
const char *operation,
bool result)
const
100 {
if (!result) HandleError(operation);}
104 static unsigned int PortNameToNumber(
const char *name,
const char *protocol=
"tcp");
115 virtual void SocketChanged() {}
133 #ifdef USE_BERKELEY_STYLE_SOCKETS
134 bool MustWaitToReceive() {
return true;}
137 bool MustWaitForResult() {
return true;}
139 bool Receive(byte* buf,
size_t bufLen);
140 unsigned int GetReceiveResult();
141 bool EofReceived()
const {
return m_eofReceived;}
149 #ifdef USE_WINDOWS_STYLE_SOCKETS
151 OVERLAPPED m_overlapped;
153 bool m_resultPending;
155 unsigned int m_lastResult;
166 #ifdef USE_BERKELEY_STYLE_SOCKETS
167 bool MustWaitToSend() {
return true;}
170 bool MustWaitForResult() {
return true;}
171 bool MustWaitForEof() {
return true; }
174 void Send(
const byte* buf,
size_t bufLen);
175 unsigned int GetSendResult();
183 #ifdef USE_WINDOWS_STYLE_SOCKETS
185 OVERLAPPED m_overlapped;
187 bool m_resultPending;
189 unsigned int m_lastResult;
213 SocketSink(socket_t s=INVALID_SOCKET,
unsigned int maxBufferSize=0,
unsigned int autoFlushBound=16*1024)
216 void SendEof() {ShutDown(SD_SEND);}
225 #endif // SOCKETS_AVAILABLE
227 #endif // CRYPTOPP_SOCKETFT_H
Base class for all exceptions thrown by the library.
static unsigned int PortNameToNumber(const char *name, const char *protocol="tcp")
look up the port number given its name, returns 0 if not found
container of wait objects
socket-based implementation of NetworkSource
The operating system reported an error.
void PumpAll()
Pump all data to attached transformation.
unsigned int GetMaxWaitObjectCount() const
Maximum number of wait objects that this object can return.
Abstract base classes that provide a uniform interface to this library.
static void ShutdownSockets()
calls WSACleanup for Windows Sockets
Library configuration file.
unsigned int GetMaxWaitObjectCount() const
Maximum number of wait objects that this object can return.
exception thrown by Socket class
static void StartSockets()
start Windows Sockets 2
socket-based implementation of NetworkSink
Classes for an unlimited queue to store bytes.
static void SetLastError(int errorCode)
sets errno or calls WSASetLastError
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
wrapper for Windows or Berkeley Sockets
bool Receive(byte *buf, size_t bufLen)
receive data from network source, returns whether result is immediately available ...
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.
static int GetLastError()
returns errno or WSAGetLastError
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.