common.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 #include <sstream>
00019 
00020 #include "common.h"
00021 
00022 std::string intToString(int i)
00023 {
00024   std::ostringstream stream;
00025   stream << i << std::flush;
00026   std::string str(stream.str());
00027   return str;
00028 }
00029 
00030 std::string vectorToString(IPAddressVector vec)
00031 {
00032     return vectorToString(vec, ", ");
00033 }
00034 
00035 std::string vectorToString(IPAddressVector vec, const char *delim)
00036 {
00037   std::ostringstream stream;
00038   for(unsigned int i = 0; i < vec.size(); i++)
00039   {
00040       stream << vec[i];
00041       if(i < vec.size() - 1)
00042         stream << delim;
00043   }
00044   stream << std::flush;
00045   std::string str(stream.str());
00046   return str;
00047 }
00048 
00049 std::string vectorToString(EroVector vec)
00050 {
00051     return vectorToString(vec, ", ");
00052 }
00053 
00054 std::string vectorToString(EroVector vec, const char *delim)
00055 {
00056     std::ostringstream stream;
00057     for(unsigned int i = 0; i < vec.size(); i++)
00058     {
00059         stream << vec[i].node;
00060 
00061         if(i < vec.size() - 1)
00062             stream << delim;
00063     }
00064     stream << std::flush;
00065     std::string str(stream.str());
00066     return str;
00067 }
00068 
00069 EroVector routeToEro(IPAddressVector rro)
00070 {
00071     EroVector ero;
00072 
00073     for(unsigned int i = 0; i < rro.size(); i++)
00074     {
00075         EroObj_t hop;
00076         hop.L = false;
00077         hop.node = rro[i];
00078         ero.push_back(hop);
00079     }
00080 
00081     return ero;
00082 }
00083 uint32 getLevel(IPvXAddress addr)
00084 {
00085     if (addr.isIPv6())
00086     {
00087         if (addr.get6().getScope()==IPv6Address::UNSPECIFIED || addr.get6().getScope()==IPv6Address::MULTICAST)
00088             return 0;
00089         if (addr.get6().getScope()==IPv6Address::LOOPBACK)
00090             return 1;
00091         if (addr.get6().getScope()==IPv6Address::LINK)
00092             return 2;
00093         if (addr.get6().getScope()==IPv6Address::SITE)
00094             return 3;
00095     }
00096     else
00097     {
00098         //Addresses usable with SCTP, but not as destination or source address
00099         if (addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("0.0.0.0"), IPAddress("255.0.0.0")) ||
00100             addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("224.0.0.0"), IPAddress("240.0.0.0")) ||
00101             addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("198.18.0.0"), IPAddress("255.255.255.0")) ||
00102             addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("192.88.99.0"), IPAddress("255.255.255.0")))
00103             return 0;
00104 
00105         //Loopback
00106         if (addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("127.0.0.0"), IPAddress("255.0.0.0")))
00107         return 1;
00108 
00109         //Link-local
00110         if (addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("169.254.0.0"), IPAddress("255.255.0.0")))
00111             return 2;
00112 
00113         //Private
00114         if (addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("10.0.0.0"), IPAddress("255.0.0.0")) ||
00115             addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("172.16.0.0"), IPAddress("255.240.0.0")) ||
00116             addr.get4().maskedAddrAreEqual(addr.get4(), IPAddress("192.168.0.0"), IPAddress("255.255.0.0")))
00117             return 3;
00118      }
00119     //Global
00120     return 4;
00121 }