libi2ncommon: (tomj) added resolve_ip function from backup_push
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 23 Nov 2004 10:51:29 +0000 (10:51 +0000)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 23 Nov 2004 10:51:29 +0000 (10:51 +0000)
src/ipfunc.cpp
src/ipfunc.hxx

index 0830c9e..185e938 100644 (file)
@@ -11,6 +11,7 @@
 #include <stdexcept>
 
 #include <arpa/inet.h>
+#include <netdb.h>
 
 #include <ipfunc.hxx>
 
@@ -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));
+}
index 567e92c..7470e91 100644 (file)
@@ -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