From: Thomas Jarosch Date: Tue, 23 Nov 2004 10:51:29 +0000 (+0000) Subject: libi2ncommon: (tomj) added resolve_ip function from backup_push X-Git-Tag: v2.6~221 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=e9852ad9469a160ba8befeb24f8ccbb0bb528224;p=libi2ncommon libi2ncommon: (tomj) added resolve_ip function from backup_push --- diff --git a/src/ipfunc.cpp b/src/ipfunc.cpp index 0830c9e..185e938 100644 --- a/src/ipfunc.cpp +++ b/src/ipfunc.cpp @@ -11,6 +11,7 @@ #include #include +#include #include @@ -563,3 +564,26 @@ std::string IP_RANGE::ip_num_string(unsigned int ip) return target; } + +string IP_RANGE::resolve_ip(const string &iporname) +{ + struct in_addr ip_adr; + struct hostent *dnsdata; + + if (inet_aton(iporname.c_str(),&ip_adr) != 0) + { + // is already a ip + return iporname; + } + + // we need dns + dnsdata=gethostbyname(iporname.c_str()); + + if (dnsdata==NULL) + throw dns_exception(hstrerror(h_errno)); + + if (dnsdata->h_addr_list == NULL || *(dnsdata->h_addr_list) == NULL) + throw dns_exception("no corresponding ip found"); + + return inet_ntoa(*(struct in_addr*)(*dnsdata->h_addr_list)); +} diff --git a/src/ipfunc.hxx b/src/ipfunc.hxx index 567e92c..7470e91 100644 --- a/src/ipfunc.hxx +++ b/src/ipfunc.hxx @@ -78,6 +78,15 @@ class IP_RANGE static unsigned int calc_netmask_from_cidr(unsigned int cidr); static std::string ip_string(unsigned int ip); static std::string ip_num_string(unsigned int ip); + static std::string resolve_ip(const std::string &iporname); +}; + +class dns_exception : public std::runtime_error +{ + public: + dns_exception(const std::string &what) + : std::runtime_error(what) + {} }; // DEPRECATED!!! use IP_RANGE instead