NCBI C++ Toolkit Cross Reference

C++/src/util/smalldns.cpp


  1 /*  $Id: smalldns.cpp 150242 2009-01-22 17:56:37Z gouriano $
  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 <util/smalldns.hpp>
 35 #include <corelib/ncbistr.hpp>
 36 #include <corelib/ncbireg.hpp>
 37 #include <util/error_codes.hpp>
 38 
 39 #if defined(NCBI_OS_MSWIN)
 40 #  include <winsock2.h>
 41 #elif defined(NCBI_OS_UNIX)
 42 #  include <unistd.h>
 43 #  include <netdb.h>
 44 #else
 45 #  error "Unsupported platform"
 46 #endif
 47 #include <errno.h>
 48 
 49 
 50 #define NCBI_USE_ERRCODE_X   Util_DNS
 51 
 52 
 53 BEGIN_NCBI_SCOPE
 54 
 55 
 56 string CSmallDNS::sm_localHostName;
 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 
 86 CSmallDNS::~CSmallDNS()
 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 
120 string CSmallDNS::GetLocalHost(void)
121 {
122     if ( sm_localHostName.empty() ) {
123 #if !defined(MAXHOSTNAMELEN)
124 #  define MAXHOSTNAMELEN 256
125 #endif
126         char buffer[MAXHOSTNAMELEN];
127         buffer[0] = buffer[MAXHOSTNAMELEN-1] = '\0';
128         errno = 0;
129         if ( gethostname(buffer, (int)sizeof(buffer)) == 0 ) {
130             if ( buffer[MAXHOSTNAMELEN - 1] ) {
131                 ERR_POST_X(3, Warning <<
132                     "CSmallDNS: Host name buffer too small");
133             } else {
134                 char* dot_pos = strstr(buffer, ".");
135                 if ( dot_pos ) {
136                     dot_pos[0] = '\0';
137                 }
138                 sm_localHostName = buffer;
139             }
140         } else {
141             ERR_POST_X(4, Warning <<
142                 "CSmallDNS: Cannot detect host name, errno:" << errno);
143         }
144     }
145     return sm_localHostName;
146 }
147 
148 
149 string CSmallDNS::LocalResolveDNS(const string& host) const
150 {
151     if ( IsValidIP(host) ) {
152         return host;
153     }
154     map<string, string>::const_iterator it = m_map.find(host);
155     if ( it != m_map.end() ) {
156         return it->second;
157     }
158     return kEmptyStr;
159 }
160 
161 
162 string CSmallDNS::LocalBackResolveDNS(const string& ip) const
163 {
164     if ( !IsValidIP(ip) ) {
165         return kEmptyStr;
166     }
167     map<string, string>::const_iterator it = m_map.find(ip);
168     if ( it != m_map.end() ) {
169         return it->second;
170     }
171     return kEmptyStr;
172 }
173 
174 
175 END_NCBI_SCOPE
176 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.