Crypto++  5.6.5
Free C++ class library of cryptographic schemes
socketft.h
1 #ifndef CRYPTOPP_SOCKETFT_H
2 #define CRYPTOPP_SOCKETFT_H
3 
4 #include "config.h"
5 
6 #if !defined(NO_OS_DEPENDENCE) && defined(SOCKETS_AVAILABLE)
7 
8 #include "cryptlib.h"
9 #include "network.h"
10 #include "queue.h"
11 
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.
15 # endif
16 #include <winsock2.h>
17 #include <ws2tcpip.h>
18 #include "winpipes.h"
19 #else
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <unistd.h>
24 #endif
25 
26 NAMESPACE_BEGIN(CryptoPP)
27 
28 #ifdef USE_WINDOWS_STYLE_SOCKETS
29 typedef ::SOCKET socket_t;
30 #else
31 typedef int socket_t;
32 const socket_t INVALID_SOCKET = -1;
33 // cygwin 1.1.4 doesn't have SHUT_RD
34 const int SD_RECEIVE = 0;
35 const int SD_SEND = 1;
36 const int SD_BOTH = 2;
37 const int SOCKET_ERROR = -1;
38 #endif
39 
40 #ifndef socklen_t
41 typedef TYPE_OF_SOCKLEN_T socklen_t; // see config.h
42 #endif
43 
44 //! wrapper for Windows or Berkeley Sockets
45 class Socket
46 {
47 public:
48  //! exception thrown by Socket class
49  class Err : public OS_Error
50  {
51  public:
52  Err(socket_t s, const std::string& operation, int error);
53  socket_t GetSocket() const {return m_s;}
54 
55  private:
56  socket_t m_s;
57  };
58 
59  Socket(socket_t s = INVALID_SOCKET, bool own=false) : m_s(s), m_own(own) {}
60  Socket(const Socket &s) : m_s(s.m_s), m_own(false) {}
61  virtual ~Socket();
62 
63  bool GetOwnership() const {return m_own;}
64  void SetOwnership(bool own) {m_own = own;}
65 
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();
70  void CloseSocket();
71 
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);
76  // the next three functions return false if the socket is in nonblocking mode
77  // and the operation cannot be completed immediately
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);
86 
87  void IOCtl(long cmd, unsigned long *argp);
88  bool SendReady(const timeval *timeout);
89  bool ReceiveReady(const timeval *timeout);
90 
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);}
101 #endif
102 
103  //! look up the port number given its name, returns 0 if not found
104  static unsigned int PortNameToNumber(const char *name, const char *protocol="tcp");
105  //! start Windows Sockets 2
106  static void StartSockets();
107  //! calls WSACleanup for Windows Sockets
108  static void ShutdownSockets();
109  //! returns errno or WSAGetLastError
110  static int GetLastError();
111  //! sets errno or calls WSASetLastError
112  static void SetLastError(int errorCode);
113 
114 protected:
115  virtual void SocketChanged() {}
116 
117  socket_t m_s;
118  bool m_own;
119 };
120 
122 {
123 public:
125  ~SocketsInitializer() {try {Socket::ShutdownSockets();} catch (const Exception&) {CRYPTOPP_ASSERT(0);}}
126 };
127 
129 {
130 public:
132 
133 #ifdef USE_BERKELEY_STYLE_SOCKETS
134  bool MustWaitToReceive() {return true;}
135 #else
136  ~SocketReceiver();
137  bool MustWaitForResult() {return true;}
138 #endif
139  bool Receive(byte* buf, size_t bufLen);
140  unsigned int GetReceiveResult();
141  bool EofReceived() const {return m_eofReceived;}
142 
143  unsigned int GetMaxWaitObjectCount() const {return 1;}
144  void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
145 
146 private:
147  Socket &m_s;
148 
149 #ifdef USE_WINDOWS_STYLE_SOCKETS
150  WindowsHandle m_event;
151  OVERLAPPED m_overlapped;
152  DWORD m_lastResult;
153  bool m_resultPending;
154 #else
155  unsigned int m_lastResult;
156 #endif
157 
158  bool m_eofReceived;
159 };
160 
162 {
163 public:
164  SocketSender(Socket &s);
165 
166 #ifdef USE_BERKELEY_STYLE_SOCKETS
167  bool MustWaitToSend() {return true;}
168 #else
169  ~SocketSender();
170  bool MustWaitForResult() {return true;}
171  bool MustWaitForEof() { return true; }
172  bool EofSent();
173 #endif
174  void Send(const byte* buf, size_t bufLen);
175  unsigned int GetSendResult();
176  void SendEof();
177 
178  unsigned int GetMaxWaitObjectCount() const {return 1;}
179  void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack);
180 
181 private:
182  Socket &m_s;
183 #ifdef USE_WINDOWS_STYLE_SOCKETS
184  WindowsHandle m_event;
185  OVERLAPPED m_overlapped;
186  DWORD m_lastResult;
187  bool m_resultPending;
188 #else
189  unsigned int m_lastResult;
190 #endif
191 };
192 
193 //! socket-based implementation of NetworkSource
194 class SocketSource : public NetworkSource, public Socket
195 {
196 public:
197  SocketSource(socket_t s = INVALID_SOCKET, bool pumpAll = false, BufferedTransformation *attachment = NULL)
198  : NetworkSource(attachment), Socket(s), m_receiver(*this)
199  {
200  if (pumpAll)
201  PumpAll();
202  }
203 
204 private:
205  NetworkReceiver & AccessReceiver() {return m_receiver;}
206  SocketReceiver m_receiver;
207 };
208 
209 //! socket-based implementation of NetworkSink
210 class SocketSink : public NetworkSink, public Socket
211 {
212 public:
213  SocketSink(socket_t s=INVALID_SOCKET, unsigned int maxBufferSize=0, unsigned int autoFlushBound=16*1024)
214  : NetworkSink(maxBufferSize, autoFlushBound), Socket(s), m_sender(*this) {}
215 
216  void SendEof() {ShutDown(SD_SEND);}
217 
218 private:
219  NetworkSender & AccessSender() {return m_sender;}
220  SocketSender m_sender;
221 };
222 
223 NAMESPACE_END
224 
225 #endif // SOCKETS_AVAILABLE
226 
227 #endif // CRYPTOPP_SOCKETFT_H
Network Source.
Definition: network.h:164
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
static unsigned int PortNameToNumber(const char *name, const char *protocol="tcp")
look up the port number given its name, returns 0 if not found
Definition: socketft.cpp:366
container of wait objects
Definition: wait.h:169
socket-based implementation of NetworkSource
Definition: socketft.h:194
The operating system reported an error.
Definition: cryptlib.h:219
void PumpAll()
Pump all data to attached transformation.
Definition: filters.h:1270
unsigned int GetMaxWaitObjectCount() const
Maximum number of wait objects that this object can return.
Definition: socketft.h:178
Abstract base classes that provide a uniform interface to this library.
static void ShutdownSockets()
calls WSACleanup for Windows Sockets
Definition: socketft.cpp:388
Library configuration file.
unsigned int GetMaxWaitObjectCount() const
Maximum number of wait objects that this object can return.
Definition: socketft.h:143
Interface for buffered transformations.
Definition: cryptlib.h:1352
exception thrown by Socket class
Definition: socketft.h:49
static void StartSockets()
start Windows Sockets 2
Definition: socketft.cpp:378
socket-based implementation of NetworkSink
Definition: socketft.h:210
Classes for an unlimited queue to store bytes.
static void SetLastError(int errorCode)
sets errno or calls WSASetLastError
Definition: socketft.cpp:406
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:62
wrapper for Windows or Berkeley Sockets
Definition: socketft.h:45
bool Receive(byte *buf, size_t bufLen)
receive data from network source, returns whether result is immediately available ...
Definition: socketft.cpp:447
Crypto++ library namespace.
Windows Handle.
Definition: winpipes.h:16
Network Receiver.
Definition: network.h:93
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.
Definition: socketft.cpp:477
static int GetLastError()
returns errno or WSAGetLastError
Definition: socketft.cpp:397
Network Sink.
Definition: network.h:187
Network Sender.
Definition: network.h:151
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.
Definition: socketft.cpp:583