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