Skip to content

Instantly share code, notes, and snippets.

@zakird
Last active December 23, 2015 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zakird/6710089 to your computer and use it in GitHub Desktop.
Save zakird/6710089 to your computer and use it in GitHub Desktop.
IPv4 Address Bitmap
// 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