E-MailRelay

glocal.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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/// \file glocal.cpp
19///
20
21#include "gdef.h"
22#include "glocal.h"
23#include "ghostname.h"
24#include "gresolver.h"
25#include "glog.h"
26#include <sstream>
27
28std::string GNet::Local::m_name_override ;
29 bool GNet::Local::m_name_override_set = false ;
30
31std::string GNet::Local::hostname()
32{
33 std::string name = G::hostname() ;
34 if( name.empty() )
35 return "localhost" ;
36 return name ;
37}
38
39std::string GNet::Local::resolvedHostname()
40{
41 static std::string result ;
42 static bool first = true ;
43 if( first )
44 {
45 first = false ;
46 static Location location( hostname().append(":0") ) ;
47 bool ok = Resolver::resolve(location).empty() && !location.name().empty() ;
48 result = ok ? location.name() : (hostname()+".localnet") ;
49 }
50 return result ;
51}
52
53std::string GNet::Local::canonicalName()
54{
55 return m_name_override_set ? m_name_override : resolvedHostname() ;
56}
57
58 void GNet::Local::canonicalName( const std::string & name_override )
59{
60 m_name_override = name_override ;
61 m_name_override_set = true ;
62}
63
64 bool GNet::Local::isLocal( const Address & address , std::string & reason )
65{
66 // by inspection wrt RFC-1918, RFC-6890 etc
67 return address.isLocal( reason ) ;
68}
69
70 bool GNet::Local::isLocal( const Address & address )
71{
72 std::string reason ;
73 return isLocal( address , reason ) ;
74}
75
GNet::Address
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:53
GNet::Address::isLocal
bool isLocal(std::string &reason) const
Returns true if this seems to be a 'local' address, ie.
Definition: gaddress.cpp:297
GNet::Local::isLocal
static bool isLocal(const Address &, std::string &reason)
Returns true if the given address appears to be 'local', or a helpful error message if not.
Definition: glocal.cpp:64
GNet::Local::canonicalName
static std::string canonicalName()
Returns the canonical network name assiciated with hostname().
Definition: glocal.cpp:53
GNet::Local::hostname
static std::string hostname()
Returns the local hostname. Returns "localhost" on error.
Definition: glocal.cpp:31
GNet::Location
A class that represents the remote target for out-going client connections.
Definition: glocation.h:71
GNet::Resolver::resolve
static std::string resolve(Location &)
Does synchronous name resolution.
Definition: gresolver.cpp:194
ghostname.h
glocal.h
glog.h
gresolver.h
G::hostname
std::string hostname()
Returns the hostname.
Definition: ghostname_unix.cpp:27