libi2ncommon: (gerd) replace CalculateNetworkAddresses code by IP_RANGE-based inline
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Fri, 27 Aug 2004 17:33:59 +0000 (17:33 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Fri, 27 Aug 2004 17:33:59 +0000 (17:33 +0000)
src/ipfunc.cpp
src/ipfunc.hxx

index ccefe95..9783249 100644 (file)
 using namespace std;
 using namespace ip_type;
 
-string CalculateNetworkAddresses (int mode, const string &ip, const string &netmask) 
-{
-    int IP[4];
-    int NETMASK[4];
-    int OUTPUT[4];
-
-    string rtn;
-
-    try {
-        string::size_type base = 0;
-        string::size_type pos = 0;
-
-        // split ip
-        for (int i = 0; i < 4; i++) {
-            pos = ip.find(".", base);
-
-            if (pos == string::npos)
-                pos = ip.length();
-            if (pos != string::npos && pos-base > 0) {
-                // get single IP fragment and convert to integer
-                string ipfrag (ip, base, pos-base);
-                istringstream in(ipfrag);
-                int tmp;
-                if (!in.eof() && in >> tmp)
-                    IP[i] = tmp;
-            }
-
-            // fix offset to leave out the "."
-            base = pos+1;
-        }
-
-        base = 0;
-        pos = 0;
-
-        // split netmask
-        for (int i = 0; i < 4; i++) {
-            pos = netmask.find(".", base);
-
-            if (pos == string::npos)
-                pos = netmask.length();
-            if (pos != string::npos && pos-base > 0) {
-                // get single IP fragment and convert to integer
-                string ipfrag (netmask, base, pos-base);
-                istringstream in(ipfrag);
-                int tmp;
-                if (!in.eof() && in >> tmp)
-                    NETMASK[i] = tmp;
-            }
-
-            // fix offset to leave out the "."
-            base = pos+1;
-        }
-    } catch (...) {}
-
-    if (mode == 0) {           // get network-base-address (by ANDing ip and netmask)
-        for (int i = 0; i < 4; i++)
-            OUTPUT[i] = IP[i]&NETMASK[i];
-    } else {                                   // calculate broadcast address
-        /*
-               how it works:
-                       -the broadcast address is an address with all host bits set
-
-                       -check all netmask bits -> if the bit is cleared -> bit is host bit
-                  -set all host bits in the IP adress to 1
-        */
-
-        // precopy IP to output
-        for (int i = 0; i < 4; i++)
-            OUTPUT[i] = IP[i];
-
-        for (int i = 0; i < 4; i++)
-            for (int j = 0; j < 8; j++) {
-                if (!(NETMASK[i]&1<<j))                // bit cleared?
-                    OUTPUT[i] = OUTPUT[i]|1<<j;        // set host bit
-            }
-    }
-
-    // convert final address back to string
-    ostringstream out;
-    out << OUTPUT[0] << "." << OUTPUT[1] << "." << OUTPUT[2] << "." << OUTPUT[3];
-    rtn = out.str();
-
-    return rtn;
-}
-
 // can decode IP, IP-IP (as range) and IP/MASK (as network)
 IP_RANGE::IP_RANGE(const std::string& ip)
 {
index 1972846..e525bdb 100644 (file)
@@ -77,6 +77,12 @@ inline std::string ipfunc_format_ip(unsigned int ip)
 
 // DEPRECATED!!! use IP_RANGE instead
 // mode 0: get network-base-address, other: broadcast-address
-std::string CalculateNetworkAddresses (int mode, const std::string &ip, const std::string &netmask);
+inline std::string CalculateNetworkAddresses (int mode, const std::string &ip, const std::string &netmask)
+{
+    if (mode==0)
+        return IP_RANGE::ip_string(IP_RANGE(ip_type::NETWORK,ip,netmask).get_base());
+    else
+        return IP_RANGE::ip_string(IP_RANGE(ip_type::NETWORK,ip,netmask).get_broadcast());
+}
 
 #endif