/src/HashMapE.cpp

https://github.com/eglatz/HAPviewer · C++ · 404 lines · 271 code · 68 blank · 65 comment · 0 complexity · feaad1d1829e3f25986e2c21ef0f456b MD5 · raw file

  1. /**
  2. * \file HashMapE.cpp
  3. *
  4. * Extends HashMap.h with specialized classes.
  5. */
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <sys/socket.h>
  9. #include <assert.h>
  10. #include <boost/array.hpp>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include "HashMapE.h"
  14. #include "gutil.h"
  15. /* *************************************************************
  16. * Key type HashKeyIPv6: IPv6Pair key *
  17. * *************************************************************/
  18. HashKeyIPv6Pair::HashKeyIPv6Pair(const IPv6_addr & ip1, const IPv6_addr & ip2) {
  19. std::copy(ip1.begin(), ip1.end(), key.begin());
  20. std::copy(ip2.begin(), ip2.end(), key.begin() + sizeof(ip1));
  21. }
  22. CHashKey6_6::CHashKey6_6(const uint64_t val1, const IPv6_addr & val2) {
  23. std::fill(key.begin(), key.end(), 0);
  24. memcpy(key.begin(), &val1, sizeof(uint64_t));
  25. memcpy(key.begin() + 16, val2.begin(), sizeof(IPv6_addr));
  26. }
  27. CHashKey6_6::CHashKey6_6(const uint32_t val1, const uint32_t val2) {
  28. // |4 bytes: val1|12 bytes: 0|4 bytes: val2|12 bytes: 0|
  29. std::fill(key.begin(), key.end(), 0);
  30. memcpy(key.begin(), &val1, sizeof(uint32_t));
  31. memcpy(key.begin() + 16, &val2, sizeof(uint32_t));
  32. }
  33. CHashKey6_6::CHashKey6_6(const uint64_t val1, const uint64_t val2) {
  34. std::fill(key.begin(), key.end(), 0);
  35. memcpy(key.begin(), &val1, sizeof(uint64_t));
  36. memcpy(key.begin() + 16, &val2, sizeof(uint64_t));
  37. }
  38. HashKeyIPv6Pair::HashKeyIPv6Pair(const HashKeyIPv6Pair& b) {
  39. key = b.key;
  40. }
  41. HashKeyIPv6Pair::~HashKeyIPv6Pair() {
  42. // nothing to do in here
  43. }
  44. size_t HashKeyIPv6Pair::size() const {
  45. return key.size();
  46. }
  47. const HashKeyIPv6Pair::key_type & HashKeyIPv6Pair::getkey() const {
  48. return key;
  49. }
  50. std::string HashKeyIPv6Pair::printkey() const {
  51. char tmpip1[INET6_ADDRSTRLEN];
  52. char tmpip2[INET6_ADDRSTRLEN];
  53. std::stringstream ss;
  54. ss << tmpip1;
  55. ss << " ";
  56. ss << tmpip2;
  57. return ss.str();
  58. }
  59. /* *************************************************************
  60. * Key type HashKeyIPv6_3T: IPv6 Three-Tuple key *
  61. * *************************************************************/
  62. HashKeyIPv6_3T::HashKeyIPv6_3T(const IPv6_addr & IP, const uint8_t & protocol, const uint16_t & port) {
  63. std::copy(IP.begin(), IP.end(), key.begin());
  64. key[16] = protocol;
  65. memcpy(&(key[17]), &port, 2);
  66. // *((uint32_t *)(&(key[0]))) = *IP;
  67. // *((uint8_t *)(&(key[4]))) = *protocol;
  68. // *((uint16_t *)(&(key[5]))) = *port;
  69. }
  70. HashKeyIPv6_3T::~HashKeyIPv6_3T() {
  71. // nothing to do in here
  72. }
  73. size_t HashKeyIPv6_3T::size() const {
  74. return key.size();
  75. }
  76. HashKeyIPv6_3T::HashKeyIPv6_3T(const HashKeyIPv6_3T &b) {
  77. key = b.key;
  78. }
  79. const HashKeyIPv6_3T::key_type & HashKeyIPv6_3T::getkey() const {
  80. return key;
  81. }
  82. std::string HashKeyIPv6_3T::printkey() const {
  83. IPv6_addr addr(0);
  84. std::copy(key.begin(), key.begin()+sizeof(IPv6_addr), addr.begin());
  85. uint8_t protocol = key[16];
  86. uint16_t port;
  87. memcpy(&port, &(key[17]), sizeof(uint16_t));
  88. std::stringstream ss;
  89. ss << "ip: ";
  90. ss << addr;
  91. ss << " proto: ";
  92. ss << (int)protocol;
  93. ss << " port: ";
  94. ss << port;
  95. return ss.str();
  96. }
  97. /* *************************************************************
  98. * Key type HashKeyIPv6_4T: IPv6 Four-Tuple key *
  99. * *************************************************************/
  100. HashKeyIPv6_4T::HashKeyIPv6_4T(const IPv6_addr & IP, const uint8_t & protocol, const uint16_t & port, const uint8_t & flowtype) {
  101. std::copy(IP.begin(), IP.end(), key.begin());
  102. memcpy(key.begin() + 16, &protocol, 1);
  103. memcpy(key.begin() + 17, &port, 2);
  104. memcpy(key.begin() + 19, &flowtype, 1);
  105. }
  106. HashKeyIPv6_4T::~HashKeyIPv6_4T() {
  107. // nothing to do in here
  108. }
  109. size_t HashKeyIPv6_4T::size() const {
  110. return key.size();
  111. }
  112. HashKeyIPv6_4T::HashKeyIPv6_4T(const HashKeyIPv6_4T &b) {
  113. // memcpy(key, b.key, 8);
  114. key = b.key;
  115. }
  116. const HashKeyIPv6_4T::key_type & HashKeyIPv6_4T::getkey() const {
  117. return key;
  118. }
  119. std::string HashKeyIPv6_4T::printkey() const {
  120. IPv6_addr ip;
  121. uint8_t protocol;
  122. uint16_t port;
  123. uint8_t flowtype;
  124. std::copy(key.begin(), key.begin()+sizeof(IPv6_addr), ip.begin());
  125. memcpy(&protocol, key.begin() + 16, sizeof(uint8_t));
  126. memcpy(&port, key.begin() + 17, sizeof(uint16_t));
  127. memcpy(&flowtype, key.begin() + 19, sizeof(uint8_t));
  128. std::stringstream ss;
  129. ss << "ip: ";
  130. ss << ip;
  131. ss << "proto: ";
  132. ss << (int)protocol;
  133. ss << "port: ";
  134. ss << port;
  135. ss << "flowtype: ";
  136. ss << (int)flowtype;
  137. return ss.str();
  138. }
  139. /* *************************************************************
  140. * Key type HashKeyIPv6_5T_2: IPv6 Five-Tuple key *
  141. * *************************************************************/
  142. HashKeyIPv6_5T_2::HashKeyIPv6_5T_2(const IPv6_addr & localIP, const IPv6_addr & remoteIP, const uint8_t & protocol, const uint16_t & port, const uint8_t & flowtype) {
  143. std::copy(localIP.begin(), localIP.end(), key.begin());
  144. std::copy(remoteIP.begin(), remoteIP.end(), key.begin() + sizeof(IPv6_addr));
  145. memcpy(key.begin() + sizeof(IPv6_addr) + sizeof(IPv6_addr), &protocol, sizeof(uint8_t));
  146. memcpy(key.begin() + sizeof(uint8_t) + sizeof(IPv6_addr) + sizeof(IPv6_addr), &port, sizeof(uint16_t));
  147. memcpy(key.begin() + sizeof(uint16_t) + sizeof(uint8_t) + sizeof(IPv6_addr) + sizeof(IPv6_addr), &flowtype, sizeof(uint8_t));
  148. }
  149. HashKeyIPv6_5T_2::~HashKeyIPv6_5T_2() {
  150. // nothing to do in here
  151. }
  152. size_t HashKeyIPv6_5T_2::size() const {
  153. return key.size();
  154. }
  155. HashKeyIPv6_5T_2::HashKeyIPv6_5T_2(const HashKeyIPv6_5T_2 &b) {
  156. key = b.key;
  157. }
  158. const HashKeyIPv6_5T_2::key_type & HashKeyIPv6_5T_2::getkey() const {
  159. return key;
  160. }
  161. std::string HashKeyIPv6_5T_2::printkey() const {
  162. IPv6_addr localip;
  163. IPv6_addr remoteip;
  164. uint8_t protocol;
  165. uint16_t port;
  166. uint8_t flowtype;
  167. std::copy(key.begin(), key.begin() + sizeof(IPv6_addr), localip.begin());
  168. std::copy(key.begin() + sizeof(IPv6_addr), key.begin() + sizeof(IPv6_addr) + sizeof(IPv6_addr), remoteip.begin());
  169. memcpy(&protocol, key.begin() + sizeof(IPv6_addr) + sizeof(IPv6_addr), sizeof(uint8_t));
  170. memcpy(&port, key.begin() + sizeof(uint8_t) + sizeof(IPv6_addr) + sizeof(IPv6_addr), sizeof(uint16_t));
  171. memcpy(&flowtype, key.begin() + sizeof(uint16_t) + sizeof(uint8_t) + sizeof(IPv6_addr) + sizeof(IPv6_addr), sizeof(uint8_t));
  172. std::stringstream ss;
  173. ss << "local_ip: ";
  174. ss << localip;
  175. ss << " remote_ip: ";
  176. ss << remoteip;
  177. ss << "proto: ";
  178. ss << (int)protocol;
  179. ss << "port: ";
  180. ss << port;
  181. ss << "flowtype: ";
  182. ss << (int)flowtype;
  183. return ss.str();
  184. }
  185. /* *************************************************************
  186. * Key type HashKeyIPv6_5T: IPv6 Five-Tuple key *
  187. * *************************************************************/
  188. HashKeyIPv6_5T::HashKeyIPv6_5T(const IPv6_addr & srcIP, const IPv6_addr & dstIP, const uint16_t & srcPort, const uint16_t & dstPort, const uint8_t & protocol) {
  189. std::copy(srcIP.begin(), srcIP.end(), key.begin());
  190. std::copy(dstIP.begin(), dstIP.end(), key.begin() + 16);
  191. memcpy(key.begin() + 32, &srcPort, 2);
  192. memcpy(key.begin() + 34, &dstPort, 2);
  193. memcpy(key.begin() + 36, &protocol, 1);
  194. // *((uint32_t *)(&(key[0]))) = *srcIP;
  195. // *((uint32_t *)(&(key[4]))) = *dstIP;
  196. // *((uint16_t *)(&(key[8]))) = *srcPort;
  197. // *((uint16_t *)(&(key[10]))) = *dstPort;
  198. // *((uint8_t *)(&(key[12]))) = *protocol;
  199. }
  200. HashKeyIPv6_5T::~HashKeyIPv6_5T() {
  201. // nothing to do in here
  202. }
  203. size_t HashKeyIPv6_5T::size() const {
  204. return key.size();
  205. }
  206. HashKeyIPv6_5T::HashKeyIPv6_5T(const HashKeyIPv6_5T &b) {
  207. key = b.key;
  208. }
  209. const HashKeyIPv6_5T::key_type & HashKeyIPv6_5T::getkey() const {
  210. return key;
  211. }
  212. std::string HashKeyIPv6_5T::printkey() const {
  213. char buf[128];
  214. char tmpip_src[INET6_ADDRSTRLEN];
  215. char tmpip_dst[INET6_ADDRSTRLEN];
  216. inet_ntop(AF_INET6, &key, tmpip_src, sizeof(tmpip_src));
  217. inet_ntop(AF_INET6, &key + 16, tmpip_dst, sizeof(tmpip_dst));
  218. sprintf(buf, "%u - %s:%u %s:%u", *((uint8_t *) (&(key[12]))), tmpip_src, *((uint16_t *) (&(key[8]))), tmpip_dst, *((uint16_t *) (&(key[10]))));
  219. return std::string(buf);
  220. }
  221. /* *************************************************************
  222. * Key type HashKeyIPv6_7T: IPv6 Seven-Tuple key *
  223. * *************************************************************/
  224. HashKeyIPv6_7T::HashKeyIPv6_7T(const IPv6_addr & srcIP, const IPv6_addr & dstIP, const uint16_t & srcPort, const uint16_t & dstPort, const uint8_t & protocol,
  225. const uint8_t & tos, const uint8_t & dir) {
  226. std::copy(srcIP.begin(), srcIP.end(), key.begin());
  227. std::copy(dstIP.begin(), dstIP.end(), key.begin() + 16);
  228. memcpy(key.begin() + 32, &srcPort, 2);
  229. memcpy(key.begin() + 34, &dstPort, 2);
  230. memcpy(key.begin() + 36, &protocol, 1);
  231. memcpy(key.begin() + 37, &tos, 1);
  232. memcpy(key.begin() + 38, &dir, 1);
  233. // *((uint32_t *)(&(key[0]))) = *srcIP;
  234. // *((uint32_t *)(&(key[4]))) = *dstIP;
  235. // *((uint16_t *)(&(key[8]))) = *srcPort;
  236. // *((uint16_t *)(&(key[10]))) = *dstPort;
  237. // *((uint8_t *)(&(key[12]))) = *protocol;
  238. // *((uint8_t *)(&(key[13]))) = *tos;
  239. // *((uint8_t *)(&(key[14]))) = *dir;
  240. }
  241. HashKeyIPv6_7T::~HashKeyIPv6_7T() {
  242. // nothing to do in here
  243. }
  244. size_t HashKeyIPv6_7T::size() const {
  245. return key.size();
  246. }
  247. HashKeyIPv6_7T::HashKeyIPv6_7T(const HashKeyIPv6_7T &b) {
  248. key = b.key;
  249. }
  250. const HashKeyIPv6_7T::key_type & HashKeyIPv6_7T::getkey() const {
  251. return key;
  252. }
  253. std::string HashKeyIPv6_7T::printkey() const {
  254. char buf[128];
  255. char tmpip_src[INET6_ADDRSTRLEN];
  256. char tmpip_dst[INET6_ADDRSTRLEN];
  257. inet_ntop(AF_INET6, &key, tmpip_src, sizeof(tmpip_src));
  258. inet_ntop(AF_INET6, &(key[16]), tmpip_dst, sizeof(tmpip_dst));
  259. sprintf(buf, "%u - %s:%u %s:%u (%u) (%u)", *((uint8_t *) (&(key[12]))), tmpip_src, *((uint16_t *) (&(key[8]))), tmpip_dst, *((uint16_t *) (&(key[10]))),
  260. *((uint8_t *) (&(key[13]))), *((uint8_t *) (&(key[14]))));
  261. return std::string(buf);
  262. }
  263. /* *************************************************************
  264. * Key type CHashKey4_4: 4 byte - 4 byte *
  265. * *************************************************************/
  266. CHashKey6_6::CHashKey6_6(const IPv6_addr & val1, const IPv6_addr & val2) {
  267. std::copy(val1.begin(), val1.end(), key.begin());
  268. std::copy(val2.begin(), val2.end(), key.begin() + 16);
  269. }
  270. CHashKey6_6::CHashKey6_6(const IPv6_addr & val1, const uint64_t val2) {
  271. std::fill(key.begin(), key.end(), 0);
  272. std::copy(val1.begin(), val1.end(), key.begin());
  273. memcpy(key.begin() + 16, &val2, sizeof(uint64_t));
  274. }
  275. CHashKey6_6::~CHashKey6_6() {
  276. // nothing to do in here
  277. }
  278. size_t CHashKey6_6::size() const {
  279. return key.size();
  280. }
  281. const CHashKey6_6::key_type & CHashKey6_6::getkey() const {
  282. return key;
  283. }
  284. CHashKey6_6::CHashKey6_6(const CHashKey6_6 &b) {
  285. key = b.key;
  286. }
  287. std::string CHashKey6_6::printkey() const {
  288. return util::bin2hexstring(&(key[0]), sizeof(key));;
  289. }
  290. /* *************************************************************
  291. * Key type CHashKey8: 8 tuple of 16 bit values *
  292. * *************************************************************/
  293. CHashKey8::CHashKey8(const boost::array<uint16_t, 8> & val) {
  294. *((int16_t *) (&(key[0]))) = val[0];
  295. *((int16_t *) (&(key[2]))) = val[1];
  296. *((int16_t *) (&(key[4]))) = val[2];
  297. *((int16_t *) (&(key[6]))) = val[3];
  298. *((int16_t *) (&(key[8]))) = val[4];
  299. *((int16_t *) (&(key[10]))) = val[5];
  300. *((int16_t *) (&(key[12]))) = val[6];
  301. *((int16_t *) (&(key[14]))) = val[7];
  302. }
  303. CHashKey8::~CHashKey8() {
  304. // nothing to do in here
  305. }
  306. size_t CHashKey8::size() const {
  307. return key.size();
  308. }
  309. const CHashKey8::key_type & CHashKey8::getkey() const {
  310. return key;
  311. }
  312. CHashKey8::CHashKey8(const CHashKey8 &b) {
  313. //memcpy(key, b.key, 16);
  314. key = b.key;
  315. }
  316. std::string CHashKey8::printkey() const {
  317. char buf[128];
  318. sprintf(buf, "%u - %u - %u - %u - %u - %u - %u - %u ", *((int16_t *) (&(key[0]))), *((int16_t *) (&(key[2]))), *((int16_t *) (&(key[4]))),
  319. *((int16_t *) (&(key[6]))), *((int16_t *) (&(key[8]))), *((int16_t *) (&(key[10]))), *((int16_t *) (&(key[12]))), *((int16_t *) (&(key[14]))));
  320. return std::string(buf);
  321. }
  322. const boost::array<uint16_t, 8> CHashKey8::getRoles() const {
  323. boost::array<uint16_t, 8> roles;
  324. memcpy(roles.begin(), key.begin(), sizeof(boost::array<uint16_t, 8>));
  325. /*
  326. roles[0] = (key[0]<<16)+key[1];
  327. roles[1] = (key[2]<<16)+key[3];
  328. roles[2] = (key[4]<<16)+key[5];
  329. roles[3] = (key[6]<<16)+key[7];
  330. roles[4] = (key[8]<<16)+key[9];
  331. roles[5] = (key[10]<<16)+key[11];
  332. roles[6] = (key[12]<<16)+key[13];
  333. roles[7] = (key[14]<<16)+key[15];
  334. */
  335. return roles;
  336. }