- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

glocal.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 // glocal.cpp
19 //
20
21 #include "gdef.h"
22 #include "glocal.h"
23 #include "ghostname.h"
24 #include "gassert.h"
25 #include "gdebug.h"
26 #include <sstream>
27
28 std::string GNet::Local::m_fqdn ;
29 std::string GNet::Local::m_fqdn_override ;
30 bool GNet::Local::m_fqdn_override_set = false ;
31 GNet::Address GNet::Local::m_canonical_address(1U) ;
32
33 std::string GNet::Local::hostname()
34 {
35 std::string name = G::hostname() ;
36 if( name.empty() )
37 throw Error("hostname") ;
38 return name ;
39 }
40
41 GNet::Address GNet::Local::canonicalAddress()
42 {
43 if( m_canonical_address.port() == 1U )
44 {
45 m_canonical_address = canonicalAddressImp() ;
46 G_ASSERT( m_canonical_address.port() != 1U ) ;
47 }
48 return m_canonical_address ;
49 }
50
51 std::string GNet::Local::domainname()
52 {
53 std::string full = fqdn() ;
54 std::string::size_type pos = full.rfind( '.' ) ;
55 if( pos == std::string::npos )
56 throw Error( std::string() + "invalid fqdn: no dot in \"" + full + "\"" ) ;
57
58 G_DEBUG( "GNet::Local::domainname: \"" << full.substr(pos+1U) << "\"" ) ;
59 return full.substr( pos+1U ) ;
60 }
61
62 GNet::Address GNet::Local::localhostAddress()
63 {
64 return Address::localhost( 0U ) ;
65 }
66
67 std::string GNet::Local::fqdn()
68 {
69 if( m_fqdn_override_set )
70 m_fqdn = m_fqdn_override ;
71 else if( m_fqdn.empty() )
72 m_fqdn = fqdnImp() ;
73 return m_fqdn ;
74 }
75
76 void GNet::Local::fqdn( const std::string & override )
77 {
78 m_fqdn_override = override ;
79 m_fqdn_override_set = true ;
80 }
81
82 bool GNet::Local::isLocal( const Address & address )
83 {
84 std::string reason ;
85 return isLocal( address , reason ) ;
86 }
87
88 bool GNet::Local::isLocal( const Address & address , std::string & reason )
89 {
90 return address.isLocal(reason,canonicalAddress()) ;
91 }
92
GNet::Local::localhostAddress
static Address localhostAddress()
A convenience function that returns the "127.0.0.1:0" address.
Definition: glocal.cpp:62
GNet::Address::localhost
static Address localhost(unsigned int port=0U)
Returns a localhost ("loopback") address.
Definition: gaddress_ipv4.cpp:463
GNet::Local::hostname
static std::string hostname()
Returns the hostname.
Definition: glocal.cpp:33
GNet::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
GNet::Local::isLocal
static bool isLocal(const Address &)
Returns true if the given address appears to be local.
Definition: glocal.cpp:82
GNet::Local::fqdn
static std::string fqdn()
Returns the fully-qualified-domain-name.
Definition: glocal.cpp:67
md5::size_type
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
G::hostname
std::string hostname()
Returns the hostname.
Definition: ghostname_unix.cpp:27
GNet::Local::domainname
static std::string domainname()
Returns the fqdn()'s domainname.
Definition: glocal.cpp:51
ghostname.h
G_ASSERT
#define G_ASSERT(test)
Definition: gassert.h:30
gassert.h
G_DEBUG
#define G_DEBUG(expr)
Definition: glog.h:95
GNet::Address::isLocal
bool isLocal(std::string &reason) const
Returns true if the address is definitely local.
Definition: gaddress_ipv4.cpp:549
glocal.h
GNet::Local::canonicalAddress
static Address canonicalAddress()
Returns the canonical address associated with hostname().
Definition: glocal.cpp:41
gdebug.h