VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
glocation.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file glocation.h
19 ///
20 
21 #ifndef G_LOCATION__H
22 #define G_LOCATION__H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gaddress.h"
27 #include "gdatetime.h"
28 #include "gexception.h"
29 
30 namespace GNet
31 {
32  class Location ;
33  class Resolver ;
34 }
35 
36 /// \class GNet::Location
37 /// A class that holds a host/service name pair and the preferred address
38 /// family (if any), and also the results of a name-to-address lookup, ie. the
39 /// remote address and canonical host name. The actual name-to-address lookup
40 /// is done externally, and the results are deposited into the Location object
41 /// with update().
42 ///
43 /// An extended host/service format is used for SOCKS support: before the
44 /// "@" separator is the host/service pair passed verbatim to the socks
45 /// server for it to resolve (see socksFarHost()); after the "@" is the
46 /// host/service pair for the socks server itself, which we resolve and
47 /// connect to (see host()).
48 ///
49 /// \see GNet::ClientPtr
50 ///
52 {
53 public:
54  G_EXCEPTION( InvalidFormat , "invalid host:service format" ) ;
55 
56  Location( const std::string & host , const std::string & service , int family = AF_UNSPEC ) ;
57  ///< Constructor taking a host/service string.
58 
59  explicit Location( const std::string & location_string , int family = AF_UNSPEC ) ;
60  ///< Constructor taking a formatted host/service string, also
61  ///< supporting an extended format for socks. Throws if an
62  ///< invalid format.
63 
64  bool socks() const ;
65  ///< Returns true if a socks location.
66 
67  std::string host() const ;
68  ///< Returns the remote host name, as passed in to the constructor.
69 
70  std::string service() const ;
71  ///< Returns the remote service name, as passed in to the constructor.
72 
73  void resolveTrivially() ;
74  ///< If host() and service() are already in address format then do a trivial
75  ///< update() so that the location is immediately resolved(), albeit with an
76  ///< empty canonical name().
77 
78  void update( const Address & address , const std::string & canonical_name ) ;
79  ///< Updates the address and canonical name, typically after doing
80  ///< a name lookup on host() and service().
81 
82  int family() const ;
83  ///< Returns the preferred name resolution address family, or AF_UNSPEC.
84 
85  bool dgram() const ;
86  ///< Returns true if the name resolution should be specifically for datagram sockets.
87 
88  bool resolved() const ;
89  ///< Returns true after update() has been called.
90 
91  Address address() const ;
92  ///< Returns the remote address.
93 
94  std::string name() const ;
95  ///< Returns the remote canonical name. Returns the empty string if
96  ///< not available.
97 
98  std::string displayString() const ;
99  ///< Returns a string representation for logging and debug.
100 
101  G::EpochTime updateTime() const ;
102  ///< Returns the time of the last update() or zero if never update()d.
103 
104  unsigned int socksFarPort() const ;
105  ///< Returns the port number for the socks far server.
106  ///< Precondition: socks()
107 
108  std::string socksFarHost() const ;
109  ///< Returns the port for the socks far server.
110  ///< Precondition: socks()
111 
112 private:
113  static std::string head( const std::string & ) ;
114  static std::string tail( const std::string & ) ;
115  static bool socked( const std::string & , std::string & , unsigned int & ) ;
116  static std::string sockless( const std::string & ) ;
117 
118 private:
119  std::string m_host ;
120  std::string m_service ;
121  bool m_address_valid ;
122  Address m_address ;
123  int m_family ;
124  bool m_dgram ;
125  std::string m_canonical_name ;
126  G::EpochTime m_update_time ;
127  bool m_socks ;
128  std::string m_socks_far_host ;
129  unsigned int m_socks_far_port ;
130 } ;
131 
132 #endif
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:55
bool socks() const
Returns true if a socks location.
Definition: glocation.cpp:156
std::string displayString() const
Returns a string representation for logging and debug.
Definition: glocation.cpp:145
A class that holds a host/service name pair and the preferred address family (if any), and also the results of a name-to-address lookup, ie.
Definition: glocation.h:51
bool dgram() const
Returns true if the name resolution should be specifically for datagram sockets.
Definition: glocation.cpp:107
std::string name() const
Returns the remote canonical name.
Definition: glocation.cpp:140
void update(const Address &address, const std::string &canonical_name)
Updates the address and canonical name, typically after doing a name lookup on host() and service()...
Definition: glocation.cpp:129
Address address() const
Returns the remote address.
Definition: glocation.cpp:124
std::string socksFarHost() const
Returns the port for the socks far server.
Definition: glocation.cpp:166
bool resolved() const
Returns true after update() has been called.
Definition: glocation.cpp:119
std::string service() const
Returns the remote service name, as passed in to the constructor.
Definition: glocation.cpp:97
unsigned int socksFarPort() const
Returns the port number for the socks far server.
Definition: glocation.cpp:161
int family() const
Returns the preferred name resolution address family, or AF_UNSPEC.
Definition: glocation.cpp:102
Location(const std::string &host, const std::string &service, int family=AF_UNSPEC)
Constructor taking a host/service string.
Definition: glocation.cpp:33
void resolveTrivially()
If host() and service() are already in address format then do a trivial update() so that the location...
Definition: glocation.cpp:112
std::string host() const
Returns the remote host name, as passed in to the constructor.
Definition: glocation.cpp:92
G::EpochTime updateTime() const
Returns the time of the last update() or zero if never update()d.
Definition: glocation.cpp:151