1 /*
2     Mosh: the mobile shell
3     Copyright 2012 Keith Winstein
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18     In addition, as a special exception, the copyright holders give
19     permission to link the code of portions of this program with the
20     OpenSSL library under certain conditions as described in each
21     individual source file, and distribute linked combinations including
22     the two.
23 
24     You must obey the GNU General Public License in all respects for all
25     of the code used other than OpenSSL. If you modify file(s) with this
26     exception, you may extend this exception to your version of the
27     file(s), but you are not obligated to do so. If you do not wish to do
28     so, delete this exception statement from your version. If you delete
29     this exception statement from all source files in the program, then
30     also delete it here.
31 */
32 
33 #ifndef NETWORK_TRANSPORT_HPP
34 #define NETWORK_TRANSPORT_HPP
35 
36 #include <string>
37 #include <signal.h>
38 #include <time.h>
39 #include <list>
40 #include <vector>
41 
42 #include "network.h"
43 #include "transportsender.h"
44 #include "transportfragment.h"
45 
46 
47 namespace Network {
48   template <class MyState, class RemoteState>
49   class Transport
50   {
51   private:
52     /* the underlying, encrypted network connection */
53     Connection connection;
54 
55     /* sender side */
56     TransportSender<MyState> sender;
57 
58     /* helper methods for recv() */
59     void process_throwaway_until( uint64_t throwaway_num );
60 
61     /* simple receiver */
62     list< TimestampedState<RemoteState> > received_states;
63     uint64_t receiver_quench_timer;
64     RemoteState last_receiver_state; /* the state we were in when user last queried state */
65     FragmentAssembly fragments;
66     unsigned int verbose;
67 
68   public:
69     Transport( MyState &initial_state, RemoteState &initial_remote,
70 	       const char *desired_ip, const char *desired_port );
71     Transport( MyState &initial_state, RemoteState &initial_remote,
72 	       const char *key_str, const char *ip, const char *port );
73 
74     /* Send data or an ack if necessary. */
tick(void)75     void tick( void ) { sender.tick(); }
76 
77     /* Returns the number of ms to wait until next possible event. */
wait_time(void)78     int wait_time( void ) { return sender.wait_time(); }
79 
80     /* Blocks waiting for a packet. */
81     void recv( void );
82 
83     /* Find diff between last receiver state and current remote state, then rationalize states. */
84     string get_remote_diff( void );
85 
86     /* Shut down other side of connection. */
87     /* Illegal to change current_state after this. */
start_shutdown(void)88     void start_shutdown( void ) { sender.start_shutdown(); }
shutdown_in_progress(void)89     bool shutdown_in_progress( void ) const { return sender.get_shutdown_in_progress(); }
shutdown_acknowledged(void)90     bool shutdown_acknowledged( void ) const { return sender.get_shutdown_acknowledged(); }
shutdown_ack_timed_out(void)91     bool shutdown_ack_timed_out( void ) const { return sender.shutdown_ack_timed_out(); }
has_remote_addr(void)92     bool has_remote_addr( void ) const { return connection.get_has_remote_addr(); }
93 
94     /* Other side has requested shutdown and we have sent one ACK */
counterparty_shutdown_ack_sent(void)95     bool counterparty_shutdown_ack_sent( void ) const { return sender.get_counterparty_shutdown_acknowledged(); }
96 
port(void)97     std::string port( void ) const { return connection.port(); }
get_key(void)98     string get_key( void ) const { return connection.get_key(); }
99 
get_current_state(void)100     MyState &get_current_state( void ) { return sender.get_current_state(); }
set_current_state(const MyState & x)101     void set_current_state( const MyState &x ) { sender.set_current_state( x ); }
102 
get_remote_state_num(void)103     uint64_t get_remote_state_num( void ) const { return received_states.back().num; }
104 
get_latest_remote_state(void)105     const TimestampedState<RemoteState> & get_latest_remote_state( void ) const { return received_states.back(); }
106 
fds(void)107     const std::vector< int > fds( void ) const { return connection.fds(); }
108 
set_verbose(unsigned int s_verbose)109     void set_verbose( unsigned int s_verbose ) { sender.set_verbose( s_verbose ); verbose = s_verbose; }
110 
set_send_delay(int new_delay)111     void set_send_delay( int new_delay ) { sender.set_send_delay( new_delay ); }
112 
get_sent_state_acked_timestamp(void)113     uint64_t get_sent_state_acked_timestamp( void ) const { return sender.get_sent_state_acked_timestamp(); }
get_sent_state_acked(void)114     uint64_t get_sent_state_acked( void ) const { return sender.get_sent_state_acked(); }
get_sent_state_last(void)115     uint64_t get_sent_state_last( void ) const { return sender.get_sent_state_last(); }
116 
send_interval(void)117     unsigned int send_interval( void ) const { return sender.send_interval(); }
118 
get_remote_addr(void)119     const Addr &get_remote_addr( void ) const { return connection.get_remote_addr(); }
get_remote_addr_len(void)120     socklen_t get_remote_addr_len( void ) const { return connection.get_remote_addr_len(); }
121 
get_send_error(void)122     std::string &get_send_error( void ) { return connection.get_send_error(); }
123   };
124 }
125 
126 #endif
127