VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
glocal.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 Location location( hostname() , "0" ) ;
43  if( !location.resolved() )
44  {
45  std::string error = Resolver::resolve( location ) ;
46  if( !error.empty() )
47  return hostname() + ".localnet" ;
48  }
49  return location.name() ;
50 }
51 
53 {
54  return m_name_override_set ? m_name_override : resolvedHostname() ;
55 }
56 
57 void GNet::Local::canonicalName( const std::string & name_override )
58 {
59  m_name_override = name_override ;
60  m_name_override_set = true ;
61 }
62 
63 std::string GNet::Local::name()
64 {
65  return canonicalName().empty() ? hostname() : canonicalName() ;
66 }
67 
68 bool GNet::Local::isLocal( const Address & address , std::string & reason )
69 {
70  // TODO use getifaddrs(3) and GetAdaptersAddresses()
71 
72  // local if a loopback address
73  if( address.isLoopback() )
74  return true ;
75 
76  // ipv6 is easier - no need to use dns
77  if( address.family() == Address::Family::ipv6() )
78  return address.isLocal( reason ) ;
79 
80  // look up and cache the hostname() addresses
81  typedef std::vector<Address> List ;
82  static List list ;
83  static bool done = false ;
84  if( !done )
85  {
86  list = Resolver::resolve( hostname() , "0" , AF_INET ) ;
87  done = true ;
88  }
89 
90  // local if a hostname() address
91  for( List::iterator p = list.begin() ; p != list.end() ; ++p )
92  {
93  if( (*p).sameHostPart(address) )
94  return true ;
95  }
96 
97  // format a reason string
98  std::stringstream ss ;
99  if( list.empty() )
100  {
101  ss << address.hostPartString() << " is not a loopback address" ;
102  }
103  else
104  {
105  ss << address.hostPartString() << " is not a loopback address or " ;
106  const char * sep = "" ;
107  for( List::iterator p = list.begin() ; p != list.end() ; ++p , sep = " or " )
108  {
109  ss << sep << (*p).hostPartString() ;
110  }
111  }
112  reason = ss.str() ;
113 
114  return false ;
115 }
116 
117 /// \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:52
static std::string hostname()
Returns the local hostname.
Definition: glocal.cpp:32
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:55
bool isLoopback() const
Returns true if this is a loopback address.
std::string hostname()
Returns the hostname.
static std::string name()
Returns canonicalName(), or hostname() if canonicalName() is the empty string.
Definition: glocal.cpp:63
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:51
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:68
bool isLocal(std::string &reason) const
Returns true if this seems to be a local address.
static std::string resolve(Location &)
Does syncronous name resolution.
Definition: gresolver.cpp:385