Last active
December 23, 2015 23:29
-
-
Save zakird/6710089 to your computer and use it in GitHub Desktop.
IPv4 Address Bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bitmap of observed IP addresses | |
static uint64_t *ip_seen = NULL; | |
static const int IP_SEEN_SIZE = 0x4000000; // == 2^32/64 | |
// check if we've received a response from this address previously | |
static inline int check_ip(uint32_t ip) | |
{ | |
return (ip_seen[ip >> 6] >> (ip & 0x3F)) & 1; | |
} | |
// set that we've received a response from the address | |
static inline void set_ip(uint32_t ip) | |
{ | |
ip_seen[ip >> 6] |= (uint64_t)1 << (ip & 0x3F); | |
} | |
inline uint32_t ipconv(char *ip) | |
{ | |
struct in_addr a; | |
inet_aton(ip, &a); | |
return (uint32_t) a.s_addr; | |
} | |
int main(void) | |
{ | |
ip_seen = calloc(IP_SEEN_SIZE, sizeof(uint64_t)); | |
assert(ip_seen); | |
for (...) { | |
uint32_t ip = ... | |
if (!check_ip(ip)) { | |
set_ip(ip); | |
fprintf(out, "%s\n", line); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment