Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. uint8_t* ip; // our external IPv4 or IPv6 address (network byte order)
  2. int num_octets; // the number of octets to consider in ip (4 or 8)
  3. uint8_t node_id[20]; // resulting node ID
  4.  
  5. uint8_t v4_mask[] = { 0x03, 0x0f, 0x3f, 0xff };
  6. uint8_t v6_mask[] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
  7. uint8_t* mask = num_octets == 4 ? v4_mask : v6_mask;
  8.  
  9. for (int i = 0; i < num_octets; ++i)
  10.         ip[i] &= mask[i];
  11.  
  12. uint32_t rand = std::rand() & 0xff;
  13. uint8_t r = rand & 0x7;
  14. ip[0] |= r << 5;
  15.  
  16. uint32_t crc = 0;
  17. crc = crc32c(crc, ip, num_octets);
  18.  
  19. // only take the top 21 bits from crc
  20. node_id[0] = (crc >> 24) & 0xff;
  21. node_id[1] = (crc >> 16) & 0xff;
  22. node_id[2] = ((crc >> 8) & 0xf8) | (std::rand() & 0x7);
  23. for (int i = 3; i < 19; ++i) node_id[i] = std::rand();
  24. node_id[19] = rand;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement