Hazelcast C++ Client
 All Classes Functions Variables Enumerations Enumerator Pages
SSLSocket.h
1 /*
2  * Copyright (c) 2008-2017, 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::io_service &ioService,
58  asio::ssl::context &sslContext);
59 
63  virtual ~SSLSocket();
64 
70  int connect(int timeoutInMillis);
71 
78  int send(const void *buffer, int len) const;
79 
87  int receive(void *buffer, int len, int flag = 0) const;
88 
92  int getSocketId() const;
93 
97  void setRemoteEndpoint(const client::Address &address);
98 
102  const client::Address &getRemoteEndpoint() const;
103 
108  void close();
109 
110  client::Address getAddress() const;
111 
112  void setBlocking(bool blocking);
113 
117  std::vector<SSLSocket::CipherInfo> getCiphers() const;
118  private:
119  SSLSocket(const Socket &rhs);
120 
121  SSLSocket &operator=(const Socket &rhs);
122 
127  int handleError(const std::string &source, size_t numBytes, const asio::error_code &error) const;
128 
129  void checkDeadline(const asio::error_code &ec);
130 
131  client::Address remoteEndpoint;
132 
133  asio::io_service &ioService;
134  asio::ssl::context &sslContext;
135  std::auto_ptr<asio::ssl::stream<asio::ip::tcp::socket> > socket;
136  asio::deadline_timer deadline;
137  };
138 
139  std::ostream &operator<<(std::ostream &out, const SSLSocket::CipherInfo &info);
140  }
141  }
142  }
143 }
144 
145 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
146 #pragma warning(pop)
147 #endif
148 
149 #endif /* HZ_BUILD_WITH_SSL */
150 
151 #endif /* HAZELCAST_CLIENT_INTERNAL_SOCKET_SSLSOCKET_H_ */