- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gresolver.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 // gresolver.cpp
19 //
20
21 #include "gdef.h"
22 #include "gresolver.h"
23 #include "gstr.h"
24 #include "gdebug.h"
25 #include "glog.h"
26
27 std::string GNet::Resolver::resolve( ResolverInfo & in , bool udp )
28 {
29 // service
30
31 unsigned int port = 0U ;
32 std::string service_name = in.service() ;
33 if( !service_name.empty() && G::Str::isNumeric(service_name) )
34 {
35 if( ! G::Str::isUInt(service_name) )
36 return "silly port number" ;
37
38 port = G::Str::toUInt(service_name) ;
39 if( ! Address::validPort(port) )
40 return "invalid port number" ;
41 }
42 else
43 {
44 std::string error ;
45 port = resolveService( in.service() , udp , error ) ;
46 if( !error.empty() )
47 return error ;
48 }
49
50 // host
51
52 ResolverInfo result( in ) ; // copy to ensure atomic update
53 std::string host_error = resolveHost( in.host() , port , result ) ;
54 if( !host_error.empty() )
55 return host_error ;
56
57 in = result ;
58 return std::string() ;
59 }
60
61 bool GNet::Resolver::parse( const std::string & s , std::string & host , std::string & service )
62 {
63 std::string::size_type pos = s.rfind( ':' ) ;
64 if( pos == std::string::npos || pos == 0U || (pos+1U) == s.length() )
65 return false ;
66
67 host = s.substr(0U,pos) ;
68 service = s.substr(pos+1U) ;
69 return true ;
70 }
71
glog.h
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
GNet::ResolverInfo
A class that holds a host/service name pair and optionally the results of a name-to-address lookup...
Definition: gresolverinfo.h:48
md5::size_type
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
GNet::Resolver::resolve
static std::string resolve(ResolverInfo &host_and_service, bool udp=false)
Does syncronous name resolution.
Definition: gresolver.cpp:27
G::Str::isNumeric
static bool isNumeric(const std::string &s, bool allow_minus_sign=false)
Returns true if every character is a decimal digit.
Definition: gstr.cpp:152
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
GNet::Address::validPort
static bool validPort(unsigned int n)
Returns true if the port number is within the valid range.
Definition: gaddress_ipv4.cpp:621
GNet::ResolverInfo::service
std::string service() const
Returns the remote service name, as passed in to the constructor.
Definition: gresolverinfo.cpp:83
G::Str::isUInt
static bool isUInt(const std::string &s)
Returns true if the string can be converted into an unsigned integer without throwing an exception...
Definition: gstr.cpp:190
GNet::ResolverInfo::host
std::string host() const
Returns the remote host name, as passed in to the constructor.
Definition: gresolverinfo.cpp:78
gdebug.h
gresolver.h