- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gresolverinfo.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
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 // gresolverinfo.cpp
19 //
20
21 #include "gdef.h"
22 #include "gnet.h"
23 #include "gstr.h"
24 #include "gresolverinfo.h"
25 #include "gresolver.h"
26 #include "gassert.h"
27
28 std::string GNet::ResolverInfo::sockless( const std::string & s )
29 {
30 // "far-host:far-port@sockserver-host:sockserver-port"
31 return G::Str::tail( s , s.find('@') , s ) ;
32 }
33
34 bool GNet::ResolverInfo::socked( const std::string & s , std::string & far_host , unsigned int & far_port )
35 {
36 std::string::size_type pos = s.find('@') ;
37 if( pos != std::string::npos )
38 {
39 std::string ss = G::Str::head( s , pos ) ;
40 far_host = G::Str::head( ss , ss.rfind(':') ) ;
41 far_port = G::Str::toUInt( G::Str::tail( ss , ss.rfind(':') ) ) ;
42 }
43 return pos != std::string::npos ;
44 }
45
46 std::string GNet::ResolverInfo::part( const std::string & s , bool first )
47 {
48 std::string host_part ;
49 std::string service_part ;
50 if( !Resolver::parse(s,host_part,service_part) )
51 throw InvalidFormat( s ) ;
52 return first ? host_part : service_part ;
53 }
54
55 GNet::ResolverInfo::ResolverInfo( const std::string & host_and_service ) :
56 m_host(part(sockless(host_and_service),true)) ,
57 m_service(part(sockless(host_and_service),false)) ,
58 m_address_valid(false) ,
59 m_address(Address::invalidAddress()) ,
60 m_update_time(0U) ,
61 m_socks(false) ,
62 m_socks_far_port(0U)
63 {
64 m_socks = socked( host_and_service , m_socks_far_host , m_socks_far_port ) ;
65 }
66
67 GNet::ResolverInfo::ResolverInfo( const std::string & host , const std::string & service ) :
68 m_host(host) ,
69 m_service(service) ,
70 m_address_valid(false) ,
71 m_address(Address::invalidAddress()) ,
72 m_update_time(0U) ,
73 m_socks(false) ,
74 m_socks_far_port(0U)
75 {
76 }
77
78 std::string GNet::ResolverInfo::host() const
79 {
80 return m_host ;
81 }
82
83 std::string GNet::ResolverInfo::service() const
84 {
85 return m_service ;
86 }
87
88 bool GNet::ResolverInfo::hasAddress() const
89 {
90 return m_address_valid ;
91 }
92
93 GNet::Address GNet::ResolverInfo::address() const
94 {
95 return m_address ;
96 }
97
98 void GNet::ResolverInfo::update( const Address & address , const std::string & name )
99 {
100 m_address = address ;
101 m_address_valid = true ;
102 m_canonical_name = name ;
103 m_update_time = G::DateTime::now() ;
104 }
105
106 std::string GNet::ResolverInfo::name() const
107 {
108 return m_canonical_name ;
109 }
110
111 std::string GNet::ResolverInfo::str() const
112 {
113 return m_host + ":" + m_service ;
114 }
115
116 std::string GNet::ResolverInfo::displayString( bool simple ) const
117 {
118 std::string s = m_host + ":" + m_service ;
119 if( simple && hasAddress() )
120 {
121 s = address().displayString() ;
122 }
123 else
124 {
125 if( hasAddress() )
126 s.append( std::string() + " [" + address().displayString() + "]" ) ;
127 if( !name().empty() )
128 s.append( std::string() + " (" + name() + ")" ) ;
129 }
130 return s ;
131 }
132
133 G::DateTime::EpochTime GNet::ResolverInfo::updateTime() const
134 {
135 return m_update_time ;
136 }
137
138 bool GNet::ResolverInfo::socks() const
139 {
140 return m_socks ;
141 }
142
143 unsigned int GNet::ResolverInfo::socksFarPort() const
144 {
145 return m_socks_far_port ;
146 }
147
148 std::string GNet::ResolverInfo::socksFarHost() const
149 {
150 return m_socks_far_host ;
151 }
152
G::DateTime::now
static EpochTime now()
Returns the current epoch time.
Definition: gdatetime.cpp:34
GNet::ResolverInfo::update
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: gresolverinfo.cpp:98
G::DateTime::EpochTime
std::time_t EpochTime
Definition: gdatetime.h:41
G::Str::toUInt
static unsigned int toUInt(const std::string &s, bool limited=false)
Converts string 's' to an unsigned int.
Definition: gstr.cpp:346
gresolverinfo.h
GNet::ResolverInfo::socksFarHost
std::string socksFarHost() const
Returns the port for the socks far server.
Definition: gresolverinfo.cpp:148
GNet::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
md5::size_type
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
G::Str::tail
static std::string tail(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:842
GNet::ResolverInfo::ResolverInfo
ResolverInfo(const std::string &host, const std::string &service)
Constructor.
Definition: gresolverinfo.cpp:67
GNet::ResolverInfo::socksFarPort
unsigned int socksFarPort() const
Returns the port number for the socks far server.
Definition: gresolverinfo.cpp:143
GNet::ResolverInfo::address
Address address() const
Returns the remote address.
Definition: gresolverinfo.cpp:93
GNet::ResolverInfo::hasAddress
bool hasAddress() const
Returns true after update() has been called.
Definition: gresolverinfo.cpp:88
GNet::ResolverInfo::socks
bool socks() const
Returns true if using socks.
Definition: gresolverinfo.cpp:138
gstr.h
GNet::Resolver::parse
static bool parse(const std::string &in, std::string &host_or_address, std::string &service_or_port)
Parses a string that contains a hostname or ip address plus a server name or port number...
Definition: gresolver.cpp:61
gassert.h
G::Str::head
static std::string head(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
Definition: gstr.cpp:834
GNet::ResolverInfo::str
std::string str() const
Returns a string representation of the host and service names that can be passed to the Resolver's re...
Definition: gresolverinfo.cpp:111
GNet::ResolverInfo::displayString
std::string displayString(bool simple=false) const
Returns a string representation for logging and debug.
Definition: gresolverinfo.cpp:116
GNet::ResolverInfo::service
std::string service() const
Returns the remote service name, as passed in to the constructor.
Definition: gresolverinfo.cpp:83
GNet::ResolverInfo::updateTime
G::DateTime::EpochTime updateTime() const
Returns the time of the last update().
Definition: gresolverinfo.cpp:133
GNet::ResolverInfo::name
std::string name() const
Returns the remote canonical name.
Definition: gresolverinfo.cpp:106
GNet::ResolverInfo::host
std::string host() const
Returns the remote host name, as passed in to the constructor.
Definition: gresolverinfo.cpp:78
gresolver.h