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