BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
stcp_socket.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
26 #include <fc/crypto/aes.hpp>
27 #include <fc/crypto/elliptic.hpp>
28 
29 namespace graphene { namespace net {
30 
35 class stcp_socket : public virtual fc::iostream
36 {
37  public:
38  stcp_socket();
39  ~stcp_socket();
40  fc::tcp_socket& get_socket() { return _sock; }
41  void accept();
42 
43  void connect_to( const fc::ip::endpoint& remote_endpoint );
44  void bind( const fc::ip::endpoint& local_endpoint );
45 
46  virtual size_t readsome( char* buffer, size_t max );
47  virtual size_t readsome( const std::shared_ptr<char>& buf, size_t len, size_t offset );
48  virtual bool eof()const;
49 
50  virtual size_t writesome( const char* buffer, size_t len );
51  virtual size_t writesome( const std::shared_ptr<const char>& buf, size_t len, size_t offset );
52 
53  virtual void flush();
54  virtual void close();
55 
56  using istream::get;
57  void get( char& c ) { read( &c, 1 ); }
58  fc::sha512 get_shared_secret() const { return _shared_secret; }
59  private:
60  void do_key_exchange();
61 
62  fc::sha512 _shared_secret;
63  fc::ecc::private_key _priv_key;
64  fc::tcp_socket _sock;
65  fc::aes_encoder _send_aes;
66  fc::aes_decoder _recv_aes;
67  std::shared_ptr<char> _read_buffer;
68  std::shared_ptr<char> _write_buffer;
69 #ifndef NDEBUG
70  bool _read_buffer_in_use;
71  bool _write_buffer_in_use;
72 #endif
73 };
74 
75 typedef std::shared_ptr<stcp_socket> stcp_socket_ptr;
76 
77 } } // graphene::net
istream & read(char *buf, size_t len)
Definition: iostream.cpp:274
void connect_to(const fc::ip::endpoint &remote_endpoint)
Definition: stcp_socket.cpp:72
Definition: api.cpp:48
virtual bool eof() const
fc::tcp_socket & get_socket()
Definition: stcp_socket.hpp:40
virtual size_t writesome(const char *buffer, size_t len)
fc::sha512 get_shared_secret() const
Definition: stcp_socket.hpp:58
virtual size_t readsome(char *buffer, size_t max)
Definition: stcp_socket.cpp:88
void bind(const fc::ip::endpoint &local_endpoint)
Definition: stcp_socket.cpp:78
an elliptic curve private key.
Definition: elliptic.hpp:89
std::shared_ptr< stcp_socket > stcp_socket_ptr
Definition: stcp_socket.hpp:75