Hazelcast C++ Client
SSLSocket.h
1 /*
2  * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef HAZELCAST_CLIENT_INTERNAL_SOCKET_SSLSOCKET_H_
17 #define HAZELCAST_CLIENT_INTERNAL_SOCKET_SSLSOCKET_H_
18 
19 #ifdef HZ_BUILD_WITH_SSL
20 
21 #include <asio.hpp>
22 #include <asio/ssl.hpp>
23 
24 #include "hazelcast/client/Socket.h"
25 #include "hazelcast/client/Address.h"
26 #include "hazelcast/util/AtomicBoolean.h"
27 
28 #if !defined(MSG_NOSIGNAL)
29 # define MSG_NOSIGNAL 0
30 #endif
31 
32 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
33 #pragma warning(push)
34 #pragma warning(disable: 4251) //for dll export
35 #pragma warning(disable: 4003) //for not enough actual parameters for macro 'min' in asio wait_traits
36 #endif
37 
38 namespace hazelcast {
39  namespace client {
40  namespace internal {
41  namespace socket {
45  class HAZELCAST_API SSLSocket : public Socket {
46  public:
47  struct CipherInfo {
48  std::string name;
49  int numberOfBits;
50  std::string version;
51  std::string description;
52  };
53 
57  SSLSocket(const client::Address &address, asio::ssl::context &sslContext);
58 
62  virtual ~SSLSocket();
63 
69  int connect(int timeoutInMillis);
70 
78  int send(const void *buffer, int len, int flag = 0);
79 
87  int receive(void *buffer, int len, int flag = 0);
88 
92  int getSocketId() const;
93 
98  void close();
99 
100  client::Address getAddress() const;
101 
102  void setBlocking(bool blocking);
103 
107  std::vector<SSLSocket::CipherInfo> getCiphers() const;
108 
109  std::auto_ptr<Address> localSocketAddress() const;
110  private:
111  SSLSocket(const Socket &rhs);
112 
113  SSLSocket &operator=(const Socket &rhs);
114 
115  class ReadHandler {
116  public:
117  ReadHandler(size_t &numRead, asio::error_code &ec);
118 
119  void operator()(const asio::error_code &err, std::size_t bytes_transferred);
120 
121  size_t &getNumRead() const;
122 
123  asio::error_code &getErrorCode() const;
124 
125  private:
126  size_t &numRead;
127  asio::error_code &errorCode;
128  };
129 
134  int handleError(const std::string &source, size_t numBytes, const asio::error_code &error) const;
135 
136  void handleConnect(const asio::error_code &error);
137 
138  void checkDeadline(const asio::error_code &ec);
139 
140  client::Address remoteEndpoint;
141 
142  asio::io_service ioService;
143  asio::ssl::context &sslContext;
144  std::auto_ptr<asio::ssl::stream<asio::ip::tcp::socket> > socket;
145  asio::deadline_timer deadline;
146  asio::error_code errorCode;
147  int socketId;
148  };
149 
150  std::ostream &operator<<(std::ostream &out, const SSLSocket::CipherInfo &info);
151  }
152  }
153  }
154 }
155 
156 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
157 #pragma warning(pop)
158 #endif
159 
160 #endif /* HZ_BUILD_WITH_SSL */
161 
162 #endif /* HAZELCAST_CLIENT_INTERNAL_SOCKET_SSLSOCKET_H_ */
Definition: MapEntryView.h:32