Crypto++
5.6.4
Free C++ class library of cryptographic schemes
|
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.
20 #include <sys/types.h>
21 #include <sys/socket.h>
27 #ifdef USE_WINDOWS_STYLE_SOCKETS
28 typedef ::SOCKET socket_t;
31 const socket_t INVALID_SOCKET = -1;
33 const int SD_RECEIVE = 0;
34 const int SD_SEND = 1;
35 const int SD_BOTH = 2;
36 const int SOCKET_ERROR = -1;
40 typedef TYPE_OF_SOCKLEN_T socklen_t;
51 Err(socket_t s,
const std::string& operation,
int error);
52 socket_t GetSocket()
const {
return m_s;}
58 Socket(socket_t s = INVALID_SOCKET,
bool own=
false) : m_s(s), m_own(own) {}
62 bool GetOwnership()
const {
return m_own;}
63 void SetOwnership(
bool own) {m_own = own;}
65 operator socket_t() {
return m_s;}
66 socket_t GetSocket()
const {
return m_s;}
67 void AttachSocket(socket_t s,
bool own=
false);
68 socket_t DetachSocket();
71 void Create(
int nType = SOCK_STREAM);
72 void Bind(
unsigned int port,
const char *addr=NULL);
73 void Bind(
const sockaddr* psa, socklen_t saLen);
74 void Listen(
int backlog=5);
77 bool Connect(
const char *addr,
unsigned int port);
78 bool Connect(
const sockaddr* psa, socklen_t saLen);
79 bool Accept(
Socket& s, sockaddr *psa=NULL, socklen_t *psaLen=NULL);
80 void GetSockName(sockaddr *psa, socklen_t *psaLen);
81 void GetPeerName(sockaddr *psa, socklen_t *psaLen);
82 unsigned int Send(
const byte* buf,
size_t bufLen,
int flags=0);
83 unsigned int Receive(
byte* buf,
size_t bufLen,
int flags=0);
84 void ShutDown(
int how = SD_SEND);
86 void IOCtl(
long cmd,
unsigned long *argp);
87 bool SendReady(
const timeval *timeout);
88 bool ReceiveReady(
const timeval *timeout);
90 virtual void HandleError(
const char *operation)
const;
91 void CheckAndHandleError_int(
const char *operation,
int result)
const
92 {
if (result == SOCKET_ERROR) HandleError(operation);}
93 void CheckAndHandleError(
const char *operation, socket_t result)
const
94 {
if (result ==
static_cast<socket_t
>(SOCKET_ERROR)) HandleError(operation);}
95 #ifdef USE_WINDOWS_STYLE_SOCKETS
96 void CheckAndHandleError(
const char *operation, BOOL result)
const
97 {assert(result==TRUE || result==FALSE);
if (!result) HandleError(operation);}
98 void CheckAndHandleError(
const char *operation,
bool result)
const
99 {
if (!result) HandleError(operation);}
103 static unsigned int PortNameToNumber(
const char *name,
const char *protocol=
"tcp");
114 virtual void SocketChanged() {}
132 #ifdef USE_BERKELEY_STYLE_SOCKETS
133 bool MustWaitToReceive() {
return true;}
136 bool MustWaitForResult() {
return true;}
138 bool Receive(
byte* buf,
size_t bufLen);
139 unsigned int GetReceiveResult();
140 bool EofReceived()
const {
return m_eofReceived;}
149 #ifdef USE_WINDOWS_STYLE_SOCKETS
151 OVERLAPPED m_overlapped;
152 bool m_resultPending;
155 unsigned int m_lastResult;
164 #ifdef USE_BERKELEY_STYLE_SOCKETS
165 bool MustWaitToSend() {
return true;}
168 bool MustWaitForResult() {
return true;}
169 bool MustWaitForEof() {
return true; }
172 void Send(
const byte* buf,
size_t bufLen);
173 unsigned int GetSendResult();
181 #ifdef USE_WINDOWS_STYLE_SOCKETS
183 OVERLAPPED m_overlapped;
184 bool m_resultPending;
187 unsigned int m_lastResult;
211 SocketSink(socket_t s=INVALID_SOCKET,
unsigned int maxBufferSize=0,
unsigned int autoFlushBound=16*1024)
214 void SendEof() {ShutDown(SD_SEND);}
223 #endif // SOCKETS_AVAILABLE
225 #endif // CRYPTOPP_SOCKETFT_H
The operating system reported an error.
static unsigned int PortNameToNumber(const char *name, const char *protocol="tcp")
look up the port number given its name, returns 0 if not found
socket-based implementation of NetworkSink
static void StartSockets()
start Windows Sockets 2
unsigned int GetMaxWaitObjectCount() const
Maximum number of wait objects that this object can return.
bool Receive(byte *buf, size_t bufLen)
receive data from network source, returns whether result is immediately available
socket-based implementation of NetworkSource
exception thrown by Socket class
Base class for all exceptions thrown by the library.
void PumpAll()
Pump all data to attached transformation.
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.
static void ShutdownSockets()
calls WSACleanup for Windows Sockets
Classes for an unlimited queue to store bytes.
unsigned int GetMaxWaitObjectCount() const
Maximum number of wait objects that this object can return.
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.
static int GetLastError()
returns errno or WSAGetLastError
static void SetLastError(int errorCode)
sets errno or calls WSASetLastError
wrapper for Windows or Berkeley Sockets
Crypto++ library namespace.
Library configuration file.
container of wait objects
Abstract base classes that provide a uniform interface to this library.