utilities.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Created by Aidmar
  2. #include "utilities.h"
  3. /**
  4. * Split a string.
  5. * @param str string to be splitted
  6. * @param delimiter delimiter to use in splitting
  7. * @return vector of substrings
  8. */
  9. /*std::vector<std::string> split(std::string str, char delimiter) {
  10. std::vector<std::string> internal;
  11. std::stringstream ss(str); // Turn the string into a stream.
  12. std::string tok;
  13. while(getline(ss, tok, delimiter)) {
  14. internal.push_back(tok);
  15. }
  16. return internal;
  17. }*/
  18. void split_str(const std::string& s, char delim,std::vector<std::string>& v) {
  19. auto i = 0;
  20. auto pos = s.find(delim);
  21. while (pos != std::string::npos) {
  22. v.push_back(s.substr(i, pos-i));
  23. i = ++pos;
  24. pos = s.find(delim, pos);
  25. if (pos == std::string::npos)
  26. v.push_back(s.substr(i, s.length()));
  27. }
  28. }
  29. /**
  30. * Get the class (A,B,C,D,E) of IP address.
  31. * @param ipAddress IP that we get its class
  32. */
  33. std::string getIPv4Class(std::string ipAddress){
  34. std::string ipClass="Unknown";
  35. std::vector<std::string> ipBytes;
  36. split_str(ipAddress, '.',ipBytes);
  37. //std::cout<< ipAddress << "\n";
  38. if(ipBytes.size()>1){
  39. int b1 = std::stoi(ipBytes[0]);
  40. int b2 = std::stoi(ipBytes[1]);
  41. if(b1 >= 1 && b1 <= 126){
  42. if(b1 == 10)
  43. ipClass = "A-private";
  44. else
  45. ipClass = "A";
  46. }
  47. else if(b1 == 127){
  48. ipClass = "A-unused"; // cannot be used and is reserved for loopback and diagnostic functions.
  49. }
  50. else if (b1 >= 128 && b1 <= 191){
  51. if(b1 == 172 && b2 >= 16 && b2 <= 31)
  52. ipClass = "B-private";
  53. else
  54. ipClass = "B";
  55. }
  56. else if (b1 >= 192 && b1 <= 223){
  57. if(b1 == 192 && b2 == 168)
  58. ipClass = "C-private";
  59. else
  60. ipClass = "C";
  61. }
  62. else if (b1 >= 224 && b1 <= 239)
  63. ipClass = "D"; // Reserved for Multicasting
  64. else if (b1 >= 240 && b1 <= 254)
  65. ipClass = "E"; // Experimental; used for research
  66. }
  67. /*
  68. // Could be done by using libtin IPv4Address
  69. IPv4Range range = IPv4Address("192.168.1.0") / 24;
  70. range.contains("192.168.1.250"); // Yey, it belongs to this network
  71. range.contains("192.168.0.100"); // NOPE
  72. */
  73. return ipClass;
  74. }
  75. /**
  76. * Get closest index for element in vector.
  77. * @param v vector
  78. * @param refElem element that we search for or for closest element
  79. */
  80. int getClosestIndex(std::vector<std::chrono::microseconds> v, std::chrono::microseconds refElem)
  81. {
  82. auto i = std::min_element(begin(v), end(v), [=] (std::chrono::microseconds x, std::chrono::microseconds y)
  83. {
  84. return std::abs((x - refElem).count()) < std::abs((y - refElem).count());
  85. });
  86. return std::distance(begin(v), i);
  87. }