My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
SshTunnel.h
Go to the documentation of this file.
1 // $Id: SshTunnel.h,v 1.2 2010/02/23 17:19:58 eml Exp $
2 
3 #ifndef GCP_UTIL_SSHTUNNEL_H
4 #define GCP_UTIL_SSHTUNNEL_H
5 
15 #include "gcp/util/common/Runnable.h"
16 
17 #include <iostream>
18 #include <string>
19 #include <sstream>
20 
21 namespace gcp {
22  namespace util {
23 
24  class CoProc;
25 
26  //-----------------------------------------------------------------------
27  // A class for opening an ssh tunnel to a port on a remote machine.
28  // The machine may or may not be behind a firewall (gateway) machine that
29  // allows only ssh connections.
30  //
31  // This class simply spawns a background thread that makes a
32  // system-level call to ssh that maps local ports to ports on a remote
33  // machine that may or not be behind a firewall.
34  //
35  // Note that for the connection to succeed, you must have set up
36  // ssh permissions on the remote machine to accept incoming ssh
37  // connections without prompting for a password
38  //-----------------------------------------------------------------------
39 
40  class SshTunnel {
41  public:
42 
43  // Constructors
44 
45 
46  // Constructor to ssh tunnel to a port (port) on a machine
47  // (host) behind a gateway (gateway) machine. If there is no
48  // gateway between us and the host, use the next constructor, or
49  // specify the same aregument for both
50 
51  SshTunnel(std::string gateway, std::string host, unsigned short port,
52  unsigned timeOutInSeconds=0);
53 
54  SshTunnel(std::string gateway, std::string host, unsigned short port, unsigned short hostPort,
55  unsigned timeOutInSeconds=0);
56 
57  // Constructor to tunnel to a port on the specified machine
58 
59  SshTunnel(std::string host, unsigned short port,
60  unsigned timeOutInSeconds=0);
61 
62  SshTunnel(std::string host, unsigned short port, unsigned short hostPort,
63  unsigned timeOutInSeconds=0);
64 
68  friend std::ostream& operator<<(std::ostream& os, SshTunnel& obj);
69 
73  virtual ~SshTunnel();
74 
75  bool succeeded();
76  std::string error();
77 
78  private:
79 
80  std::string host_;
81  std::string gateway_;
82  unsigned short port_;
83  unsigned short hostPort_;
84  std::ostringstream error_;
85  CoProc* proc_;
86  bool success_;
87 
88  void spawn();
89 
90  void initialize(std::string gateway, std::string host, unsigned short port, unsigned short hostPort,
91  unsigned timeOutInSeconds=0);
92 
93  void blockUntilSuccessOrTimeOut(unsigned timeOutInSeconds);
94 
95  void parseStdOutMsg(int fd, std::ostringstream& os, bool& connected, bool& stop);
96  void parseStdErrMsg(int fd, std::ostringstream& os, bool& connected, bool& stop);
97 
98  }; // End class SshTunnel
99 
100  } // End namespace util
101 } // End namespace gcp
102 
103 
104 
105 #endif // End #ifndef GCP_UTIL_SSHTUNNEL_H
friend std::ostream & operator<<(std::ostream &os, SshTunnel &obj)
Definition: SshTunnel.h:40
Definition: CoProc.h:27
SshTunnel(std::string gateway, std::string host, unsigned short port, unsigned timeOutInSeconds=0)
Definition: SshTunnel.cc:21
bool succeeded()
Definition: SshTunnel.cc:257
virtual ~SshTunnel()
Definition: SshTunnel.cc:246