1 #ifndef CRYPTOPP_SOCKETFT_H
2 #define CRYPTOPP_SOCKETFT_H
6 #ifdef SOCKETS_AVAILABLE
11 #ifdef USE_WINDOWS_STYLE_SOCKETS
12 # if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
13 # error Winsock 1 is not supported by this library. Please include this file or winsock2.h before windows.h.
19 #include <sys/types.h>
20 #include <sys/socket.h>
24 NAMESPACE_BEGIN(CryptoPP)
26 #ifdef USE_WINDOWS_STYLE_SOCKETS
27 typedef ::SOCKET socket_t;
30 const socket_t INVALID_SOCKET = -1;
32 const int SD_RECEIVE = 0;
33 const int SD_SEND = 1;
34 const int SD_BOTH = 2;
35 const int SOCKET_ERROR = -1;
39 typedef TYPE_OF_SOCKLEN_T socklen_t;
50 Err(socket_t s,
const std::string& operation,
int error);
51 socket_t GetSocket()
const {
return m_s;}
57 Socket(socket_t s = INVALID_SOCKET,
bool own=
false) : m_s(s), m_own(own) {}
61 bool GetOwnership()
const {
return m_own;}
62 void SetOwnership(
bool own) {m_own = own;}
64 operator socket_t() {
return m_s;}
65 socket_t GetSocket()
const {
return m_s;}
66 void AttachSocket(socket_t s,
bool own=
false);
67 socket_t DetachSocket();
70 void Create(
int nType = SOCK_STREAM);
71 void Bind(
unsigned int port,
const char *addr=NULL);
72 void Bind(
const sockaddr* psa, socklen_t saLen);
73 void Listen(
int backlog=5);
76 bool Connect(
const char *addr,
unsigned int port);
77 bool Connect(
const sockaddr* psa, socklen_t saLen);
78 bool Accept(
Socket& s, sockaddr *psa=NULL, socklen_t *psaLen=NULL);
79 void GetSockName(sockaddr *psa, socklen_t *psaLen);
80 void GetPeerName(sockaddr *psa, socklen_t *psaLen);
81 unsigned int Send(
const byte* buf,
size_t bufLen,
int flags=0);
82 unsigned int Receive(byte* buf,
size_t bufLen,
int flags=0);
83 void ShutDown(
int how = SD_SEND);
85 void IOCtl(
long cmd,
unsigned long *argp);
86 bool SendReady(
const timeval *timeout);
87 bool ReceiveReady(
const timeval *timeout);
89 virtual void HandleError(
const char *operation)
const;
90 void CheckAndHandleError_int(
const char *operation,
int result)
const
91 {
if (result == SOCKET_ERROR) HandleError(operation);}
92 void CheckAndHandleError(
const char *operation, socket_t result)
const
93 {
if (result == SOCKET_ERROR) HandleError(operation);}
94 #ifdef USE_WINDOWS_STYLE_SOCKETS
95 void CheckAndHandleError(
const char *operation, BOOL result)
const
96 {assert(result==TRUE || result==FALSE);
if (!result) HandleError(operation);}
97 void CheckAndHandleError(
const char *operation,
bool result)
const
98 {
if (!result) HandleError(operation);}
102 static unsigned int PortNameToNumber(
const char *name,
const char *protocol=
"tcp");
113 virtual void SocketChanged() {}
131 #ifdef USE_BERKELEY_STYLE_SOCKETS
132 bool MustWaitToReceive() {
return true;}
135 bool MustWaitForResult() {
return true;}
137 bool Receive(byte* buf,
size_t bufLen);
138 unsigned int GetReceiveResult();
139 bool EofReceived()
const {
return m_eofReceived;}
148 #ifdef USE_WINDOWS_STYLE_SOCKETS
149 WindowsHandle m_event;
150 OVERLAPPED m_overlapped;
151 bool m_resultPending;
154 unsigned int m_lastResult;
163 #ifdef USE_BERKELEY_STYLE_SOCKETS
164 bool MustWaitToSend() {
return true;}
167 bool MustWaitForResult() {
return true;}
168 bool MustWaitForEof() {
return true; }
171 void Send(
const byte* buf,
size_t bufLen);
172 unsigned int GetSendResult();
180 #ifdef USE_WINDOWS_STYLE_SOCKETS
181 WindowsHandle m_event;
182 OVERLAPPED m_overlapped;
183 bool m_resultPending;
186 unsigned int m_lastResult;
210 SocketSink(socket_t s=INVALID_SOCKET,
unsigned int maxBufferSize=0,
unsigned int autoFlushBound=16*1024)
213 void SendEof() {ShutDown(SD_SEND);}
222 #endif // #ifdef SOCKETS_AVAILABLE