BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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
graphene::net::stcp_socket::get
void get(char &c)
Definition: stcp_socket.hpp:57
graphene::net::stcp_socket::flush
virtual void flush()
Definition: stcp_socket.cpp:166
graphene::net::stcp_socket::close
virtual void close()
Definition: stcp_socket.cpp:172
graphene::net::stcp_socket
Definition: stcp_socket.hpp:35
fc::aes_encoder
Definition: aes.hpp:13
graphene::net::stcp_socket::writesome
virtual size_t writesome(const char *buffer, size_t len)
Definition: stcp_socket.cpp:129
graphene::net::stcp_socket::stcp_socket
stcp_socket()
Definition: stcp_socket.cpp:39
graphene::net::stcp_socket::eof
virtual bool eof() const
Definition: stcp_socket.cpp:124
graphene::net::stcp_socket::get_shared_secret
fc::sha512 get_shared_secret() const
Definition: stcp_socket.hpp:58
fc::ip::endpoint
Definition: ip.hpp:65
fc::ecc::private_key
an elliptic curve private key.
Definition: elliptic.hpp:89
fc::sha512
Definition: sha512.hpp:9
graphene::net::stcp_socket_ptr
std::shared_ptr< stcp_socket > stcp_socket_ptr
Definition: stcp_socket.hpp:75
graphene::net::stcp_socket::~stcp_socket
~stcp_socket()
Definition: stcp_socket.cpp:47
fc::istream::read
istream & read(char *buf, size_t len)
Definition: iostream.cpp:274
fc::tcp_socket
Definition: tcp_socket.hpp:14
graphene::net::stcp_socket::readsome
virtual size_t readsome(char *buffer, size_t max)
Definition: stcp_socket.cpp:88
elliptic.hpp
graphene::net::stcp_socket::accept
void accept()
Definition: stcp_socket.cpp:180
fc::aes_decoder
Definition: aes.hpp:26
graphene::net::stcp_socket::get_socket
fc::tcp_socket & get_socket()
Definition: stcp_socket.hpp:40
graphene::net::stcp_socket::connect_to
void connect_to(const fc::ip::endpoint &remote_endpoint)
Definition: stcp_socket.cpp:72
aes.hpp
tcp_socket.hpp
fc::iostream
Definition: iostream.hpp:61
graphene
Definition: api.cpp:48
graphene::net::stcp_socket::bind
void bind(const fc::ip::endpoint &local_endpoint)
Definition: stcp_socket.cpp:78