1 #ifndef CRYPTOPP_SOCKETFT_H 2 #define CRYPTOPP_SOCKETFT_H 4 #ifdef SOCKETS_AVAILABLE 10 #ifdef USE_WINDOWS_STYLE_SOCKETS 11 # if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_) 12 # error Winsock 1 is not supported by this library. Please include this file or winsock2.h before windows.h. 18 #include <sys/types.h> 19 #include <sys/socket.h> 25 #ifdef USE_WINDOWS_STYLE_SOCKETS 26 typedef ::SOCKET socket_t;
29 const socket_t INVALID_SOCKET = -1;
31 const int SD_RECEIVE = 0;
32 const int SD_SEND = 1;
33 const int SD_BOTH = 2;
34 const int SOCKET_ERROR = -1;
38 typedef TYPE_OF_SOCKLEN_T socklen_t;
49 Err(socket_t s,
const std::string& operation,
int error);
50 socket_t GetSocket()
const {
return m_s;}
56 Socket(socket_t s = INVALID_SOCKET,
bool own=
false) : m_s(s), m_own(own) {}
57 Socket(
const Socket &s) : m_s(s.m_s), m_own(false) {}
60 bool GetOwnership()
const {
return m_own;}
61 void SetOwnership(
bool own) {m_own = own;}
63 operator socket_t() {
return m_s;}
64 socket_t GetSocket()
const {
return m_s;}
65 void AttachSocket(socket_t s,
bool own=
false);
66 socket_t DetachSocket();
69 void Create(
int nType = SOCK_STREAM);
70 void Bind(
unsigned int port,
const char *addr=NULL);
71 void Bind(
const sockaddr* psa, socklen_t saLen);
72 void Listen(
int backlog=5);
75 bool Connect(
const char *addr,
unsigned int port);
76 bool Connect(
const sockaddr* psa, socklen_t saLen);
77 bool Accept(Socket& s, sockaddr *psa=NULL, socklen_t *psaLen=NULL);
78 void GetSockName(sockaddr *psa, socklen_t *psaLen);
79 void GetPeerName(sockaddr *psa, socklen_t *psaLen);
80 unsigned int Send(
const byte* buf,
size_t bufLen,
int flags=0);
81 unsigned int Receive(byte* buf,
size_t bufLen,
int flags=0);
82 void ShutDown(
int how = SD_SEND);
84 void IOCtl(
long cmd,
unsigned long *argp);
85 bool SendReady(
const timeval *timeout);
86 bool ReceiveReady(
const timeval *timeout);
88 virtual void HandleError(
const char *operation)
const;
89 void CheckAndHandleError_int(
const char *operation,
int result)
const 90 {
if (result == SOCKET_ERROR) HandleError(operation);}
91 void CheckAndHandleError(
const char *operation, socket_t result)
const 92 {
if (result == static_cast<socket_t>(SOCKET_ERROR)) HandleError(operation);}
93 #ifdef USE_WINDOWS_STYLE_SOCKETS 94 void CheckAndHandleError(
const char *operation, BOOL result)
const 95 {assert(result==TRUE || result==FALSE);
if (!result) HandleError(operation);}
96 void CheckAndHandleError(
const char *operation,
bool result)
const 97 {
if (!result) HandleError(operation);}
101 static unsigned int PortNameToNumber(
const char *name,
const char *protocol=
"tcp");
103 static void StartSockets();
105 static void ShutdownSockets();
107 static int GetLastError();
109 static void SetLastError(
int errorCode);
112 virtual void SocketChanged() {}
118 class SocketsInitializer
121 SocketsInitializer() {Socket::StartSockets();}
122 ~SocketsInitializer() {
try {Socket::ShutdownSockets();}
catch (
const Exception&) {assert(0);}}
128 SocketReceiver(Socket &s);
130 #ifdef USE_BERKELEY_STYLE_SOCKETS 131 bool MustWaitToReceive() {
return true;}
134 bool MustWaitForResult() {
return true;}
136 bool Receive(byte* buf,
size_t bufLen);
137 unsigned int GetReceiveResult();
138 bool EofReceived()
const {
return m_eofReceived;}
140 unsigned int GetMaxWaitObjectCount()
const {
return 1;}
147 #ifdef USE_WINDOWS_STYLE_SOCKETS 148 WindowsHandle m_event;
149 OVERLAPPED m_overlapped;
150 bool m_resultPending;
153 unsigned int m_lastResult;
160 SocketSender(Socket &s);
162 #ifdef USE_BERKELEY_STYLE_SOCKETS 163 bool MustWaitToSend() {
return true;}
166 bool MustWaitForResult() {
return true;}
167 bool MustWaitForEof() {
return true; }
170 void Send(
const byte* buf,
size_t bufLen);
171 unsigned int GetSendResult();
174 unsigned int GetMaxWaitObjectCount()
const {
return 1;}
179 #ifdef USE_WINDOWS_STYLE_SOCKETS 180 WindowsHandle m_event;
181 OVERLAPPED m_overlapped;
182 bool m_resultPending;
185 unsigned int m_lastResult;
193 SocketSource(socket_t s = INVALID_SOCKET,
bool pumpAll =
false,
BufferedTransformation *attachment = NULL)
202 SocketReceiver m_receiver;
206 class SocketSink :
public NetworkSink,
public Socket
209 SocketSink(socket_t s=INVALID_SOCKET,
unsigned int maxBufferSize=0,
unsigned int autoFlushBound=16*1024)
210 :
NetworkSink(maxBufferSize, autoFlushBound), Socket(s), m_sender(*this) {}
212 void SendEof() {ShutDown(SD_SEND);}
216 SocketSender m_sender;
221 #endif // #ifdef SOCKETS_AVAILABLE
Base class for all exceptions thrown by Crypto++.
container of wait objects
The operating system reported an error.
Abstract base classes that provide a uniform interface to this library.
Classes for an unlimited queue to store bytes.
Crypto++ library namespace.