Advertisement
JeffBobbo

Untitled

Jul 22nd, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <stdlib.h> /* atoi */
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. string int2string(int num)
  9. {
  10.   ostringstream os;
  11.   os << num;
  12.   return os.str();
  13. }
  14.  
  15. string ip2string(vector<short> ip)
  16. {
  17.   string ret;
  18.   for (vector<short>::iterator i = ip.begin(); i != ip.end(); ++i)
  19.     ret += (i != ip.begin() ? "." : "") + int2string(*i);
  20.   return ret;
  21. }
  22. string ip2string(vector<unsigned char> ip)
  23. {
  24.   string ret;
  25.   for (vector<unsigned char>::iterator i = ip.begin(); i != ip.end(); ++i)
  26.     ret += (i != ip.begin() ? "." : "") + int2string(*i);
  27.   return ret;
  28. }
  29.  
  30. int IPv4Loopup(const vector<vector<unsigned char > >* addrList, vector<vector<unsigned char > >* matches, string* mask)
  31. {
  32.   if (!addrList) // no list to search
  33.     return -1;
  34.   if (!matches) // no place to store matches
  35.     return -1;
  36.   if (mask->length() == 0) // no mask
  37.     return -1;
  38.  
  39.   // parse the mask into something we can use
  40.   vector<short> useableMask;
  41.   size_t pos = 0;
  42.   size_t posEnd = 0;
  43.   while (posEnd != string::npos)
  44.   {
  45.     posEnd = mask->find(".", pos);
  46.     string byte = mask->substr(pos, posEnd-pos);
  47.     if (byte == "*")
  48.       useableMask.push_back(-1);
  49.     else
  50.       useableMask.push_back(atoi(byte.c_str()));
  51.     pos = posEnd + 1;
  52.   }
  53.  
  54.   // search through addrList for ones matching mask, if so, put it in matches
  55.   // return the number of matches
  56.   int numFound = 0;
  57.   for (vector<vector<unsigned char> >::const_iterator it = addrList->begin(); it != addrList->end(); ++it)
  58.   {
  59.     vector<unsigned char> ip = *it;
  60.     bool match = true;
  61.     for (int i = 0; i < ip.size(); ++i)
  62.     {
  63.       if (useableMask[i] == -1) // wild card, match away
  64.         continue;
  65.       if (useableMask[i] != ip[i]) // no match
  66.       {
  67.         match = false;
  68.         break;
  69.       }
  70.     }
  71.     if (match)
  72.     {
  73.       numFound++;
  74.       matches->push_back(ip);
  75.     }
  76.   }
  77.   return numFound;
  78. }
  79.  
  80. int main()
  81. {
  82.   vector<vector<unsigned char > > list;
  83.   vector<vector<unsigned char > > matches;
  84.  
  85.   vector<unsigned char> tmp;
  86.   tmp.push_back(192);
  87.   tmp.push_back(168);
  88.   tmp.push_back(100);
  89.   tmp.push_back(101);
  90.   list.push_back(tmp);
  91.  
  92.   tmp.clear();
  93.   tmp.push_back(192);
  94.   tmp.push_back(168);
  95.   tmp.push_back(100);
  96.   tmp.push_back(102);
  97.   list.push_back(tmp);
  98.  
  99.   tmp.clear();
  100.   tmp.push_back(127);
  101.   tmp.push_back(0);
  102.   tmp.push_back(0);
  103.   tmp.push_back(1);
  104.   list.push_back(tmp);
  105.  
  106.   string mask = "192.168.*.*";
  107.  
  108.   cout << "Found " << IPv4Loopup(&list, &matches, &mask) << " matches" << endl;
  109.   cout << "Mask: " << mask << endl;
  110.   cout << "Matches: ";
  111.   for (vector<vector<unsigned char> >::iterator i = matches.begin(); i != matches.end(); ++i)
  112.   {
  113.     cout << ip2string(*i) << " ";
  114.   }
  115.   cout << endl;
  116.   return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement