E-MailRelay
glocal.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 "gresolver.h"
25 #include "gassert.h"
26 #include "gdebug.h"
27 #include <sstream>
28 
29 std::string GNet::Local::m_name_override ;
30 bool GNet::Local::m_name_override_set = false ;
31 
32 std::string GNet::Local::hostname()
33 {
34  std::string name = G::hostname() ;
35  if( name.empty() )
36  return "localhost" ;
37  return name ;
38 }
39 
40 std::string GNet::Local::resolvedHostname()
41 {
42  static std::string result ;
43  static bool first = true ;
44  if( first )
45  {
46  first = false ;
47  static Location location( hostname() , "0" ) ;
48  bool ok = Resolver::resolve(location).empty() && !location.name().empty() ;
49  result = ok ? location.name() : (hostname()+".localnet") ;
50  }
51  return result ;
52 }
53 
55 {
56  return m_name_override_set ? m_name_override : resolvedHostname() ;
57 }
58 
59 void GNet::Local::canonicalName( const std::string & name_override )
60 {
61  m_name_override = name_override ;
62  m_name_override_set = true ;
63 }
64 
65 bool GNet::Local::isLocal( const Address & address , std::string & reason )
66 {
67  // TODO use getifaddrs(3) and GetAdaptersAddresses()
68 
69  // local if a loopback address
70  if( address.isLoopback() )
71  return true ;
72 
73  // ipv6 is easier - no need to use dns
74  if( address.family() == Address::Family::ipv6() )
75  return address.isLocal( reason ) ;
76 
77  // look up and cache the hostname() ipv4 addresses
78  typedef std::vector<Address> List ;
79  static List list ;
80  static bool done = false ;
81  if( !done )
82  {
83  list = Resolver::resolve( hostname() , "0" , AF_INET ) ;
84  done = true ;
85  }
86 
87  // local if a hostname() address
88  for( List::iterator p = list.begin() ; p != list.end() ; ++p )
89  {
90  if( (*p).sameHostPart(address) )
91  return true ;
92  }
93 
94  // format a reason string
95  std::stringstream ss ;
96  if( list.empty() )
97  {
98  ss << address.hostPartString() << " is not a loopback address" ;
99  }
100  else
101  {
102  ss << address.hostPartString() << " is not a loopback address or " ;
103  const char * sep = "" ;
104  for( List::iterator p = list.begin() ; p != list.end() ; ++p , sep = " or " )
105  {
106  ss << sep << (*p).hostPartString() ;
107  }
108  }
109  reason = ss.str() ;
110 
111  return false ;
112 }
113 
114 /// \file glocal.cpp
Family family() const
Returns the address family.
static std::string canonicalName()
Returns the canonical network name assiciated with hostname().
Definition: glocal.cpp:54
static std::string hostname()
Returns the local hostname. Returns "localhost" on error.
Definition: glocal.cpp:32
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
bool isLoopback() const
Returns true if this is a loopback address.
std::string hostname()
Returns the hostname.
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:50
std::string hostPartString() const
Returns a string which represents the network address for debugging and diagnostics purposes...
static bool isLocal(const Address &, std::string &reason)
Returns true if the given address appears to be associated with the local host, or a helpful error me...
Definition: glocal.cpp:65
bool isLocal(std::string &reason) const
Returns true if this seems to be a local address (for some definition of 'local').
static std::string resolve(Location &)
Does synchronous name resolution.
Definition: gresolver.cpp:177