NCBI C++ ToolKit
smalldns.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: smalldns.cpp 83459 2018-08-23 16:11:13Z lavr $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Anton Golikov
27  *
28  * File Description:
29  * Resolve host name to IP address and back using preset ini-file
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbistr.hpp>
35 #include <corelib/ncbireg.hpp>
37 #include <util/smalldns.hpp>
38 #include <util/error_codes.hpp>
39 
40 #if defined(NCBI_OS_MSWIN)
41 # include <winsock2.h>
42 #elif defined(NCBI_OS_UNIX)
43 # include <unistd.h>
44 # include <netdb.h>
45 #else
46 # error "Unsupported platform"
47 #endif
48 #include <errno.h>
49 
50 
51 #define NCBI_USE_ERRCODE_X Util_DNS
52 
53 #define X_MAXHOSTNAMELEN 255
54 
55 
57 
58 
59 CSmallDNS::CSmallDNS(const string& local_hosts_file /* = "./hosts.ini" */)
60 {
61  const string section("LOCAL_DNS");
62 
63  CNcbiIfstream is(local_hosts_file.c_str());
64  if ( !is.good() ) {
65  ERR_POST_X(1, Error << "CSmallDNS: cannot open file: " << local_hosts_file);
66  return;
67  }
68  CNcbiRegistry reg(is);
69  list<string> items;
70 
71  reg.EnumerateEntries(section, &items);
72  ITERATE(list<string>, it, items) {
73  string val = reg.Get(section, *it);
74  if ( !IsValidIP(val) ) {
75  ERR_POST_X(2, Warning << "CSmallDNS: Bad IP address '" << val
76  << "' for " << *it);
77  } else {
78  m_map[*it] = val;
79  m_map[val] = *it;
80  }
81  }
82  is.close();
83 }
84 
85 
87 {
88  return;
89 }
90 
91 
92 bool CSmallDNS::IsValidIP(const string& ip)
93 {
94  list<string> dig;
95 
96  NStr::Split(ip, ".", dig);
97  if (dig.size() != 4) {
98  return false;
99  }
100  ITERATE(list<string>, it, dig) {
101  try {
102  unsigned long i = NStr::StringToULong(*it);
103  if ( i > 255 ) {
104  return false;
105  }
106  } catch(...) {
107  return false;
108  }
109  }
110  return true;
111 }
112 
113 
114 string CSmallDNS::GetLocalIP(void) const
115 {
116  return LocalResolveDNS(GetLocalHost());
117 }
118 
119 
121 {
122  static CSafeStatic<string> s_LocalHostName;
123 
124  if ( s_LocalHostName->empty() ) {
125  char buffer[X_MAXHOSTNAMELEN + 1];
126  buffer[0] = buffer[X_MAXHOSTNAMELEN] = '\0';
127  errno = 0;
128  if ( gethostname(buffer, (int) sizeof(buffer)) == 0 ) {
129  if ( buffer[X_MAXHOSTNAMELEN] ) {
130  ERR_POST_X(3, Warning <<
131  "CSmallDNS: Host name buffer too small");
132  } else {
133  char* dot_pos = strstr(buffer, ".");
134  if ( dot_pos ) {
135  dot_pos[0] = '\0';
136  }
137  *s_LocalHostName = buffer;
138  }
139  } else {
140  ERR_POST_X(4, Warning <<
141  "CSmallDNS: Cannot detect host name, errno:" << errno);
142  }
143  }
144  return s_LocalHostName.Get();
145 }
146 
147 
148 string CSmallDNS::LocalResolveDNS(const string& host) const
149 {
150  if ( IsValidIP(host) ) {
151  return host;
152  }
154  if ( it != m_map.end() ) {
155  return it->second;
156  }
157  return kEmptyStr;
158 }
159 
160 
161 string CSmallDNS::LocalBackResolveDNS(const string& ip) const
162 {
163  if ( !IsValidIP(ip) ) {
164  return kEmptyStr;
165  }
167  if ( it != m_map.end() ) {
168  return it->second;
169  }
170  return kEmptyStr;
171 }
172 
173 
CNcbiRegistry –.
Definition: ncbireg.hpp:913
T & Get(void)
Create the variable if not created yet, return the reference.
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
The NCBI C++ standard methods for dealing with std::string.
static const char ip[]
Definition: des.c:75
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define ERR_POST_X(err_subcode, message)
Error posting with default error code and given error subcode.
Definition: ncbidiag.hpp:550
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
CSmallDNS(const string &local_hosts_file="./hosts.ini")
Creates small DNS service from the registry-like file named by "local_hosts_file" using section "[LOC...
Definition: smalldns.cpp:59
string LocalResolveDNS(const string &hostname) const
Given host name "hostname", return its IP address in dot notation.
Definition: smalldns.cpp:148
map< string, string > m_map
Definition: smalldns.hpp:84
static bool IsValidIP(const string &ip)
Validate if "ip" contains a legal IP address in a dot notation (a la "255.255.255....
Definition: smalldns.cpp:92
string LocalBackResolveDNS(const string &ip) const
Given ip address in dot notation, return host name by look up in the registry.
Definition: smalldns.cpp:161
static string GetLocalHost(void)
Get local (current) host name (uname call).
Definition: smalldns.cpp:120
string GetLocalIP(void) const
Get local (current) host ip address from registry.
Definition: smalldns.cpp:114
~CSmallDNS()
Definition: smalldns.cpp:86
virtual const string & Get(const string &section, const string &name, TFlags flags=0) const
Get the parameter value.
Definition: ncbireg.cpp:262
virtual void EnumerateEntries(const string &section, list< string > *entries, TFlags flags=fAllLayers) const
Enumerate parameter names for a specified section.
Definition: ncbireg.cpp:514
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ifstream CNcbiIfstream
Portable alias for ifstream.
Definition: ncbistre.hpp:439
#define kEmptyStr
Definition: ncbistr.hpp:123
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3452
static unsigned long StringToULong(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to unsigned long.
Definition: ncbistr.cpp:665
Definition of all error codes used in util (xutil.lib).
int i
Static variables safety - create on demand, destroy on application termination.
Process information in the NCBI Registry, including working with configuration files.
static uint8_t * buffer
Definition: pcre2test.c:1016
#define X_MAXHOSTNAMELEN
Definition: smalldns.cpp:53
Resolve host name to ip address and back using preset ini-file.
Modified on Wed Sep 04 15:02:11 2024 by modify_doxy.py rev. 669887