Skip to content

Instantly share code, notes, and snippets.

@autarch
Created July 11, 2013 19:27
Show Gist options
  • Save autarch/5978466 to your computer and use it in GitHub Desktop.
Save autarch/5978466 to your computer and use it in GitHub Desktop.
static int lookup(MMDB_root_entry_s * root, const char *ipstr)
{
struct in_addr ip;
struct in6_addr ip6;
int ai_flags = AI_NUMERICHOST|AI_V4MAPPED;
int status;
int depth = root->entry.mmdb->depth;
if (ipstr == NULL) {
croak("MaxMind::DB::Reader::XS address to lookup is NULL");
}
if (depth == 32) {
if (0 != MMDB_lookupaddressX(ipstr, AF_INET, ai_flags, &ip)) {
croak("MaxMind::DB::Reader::XS Invalid IPv4 Address");
}
status = MMDB_lookup_by_ipnum(htonl(ip.s_addr), root);
}
else {
if (0 != MMDB_lookupaddressX(ipstr, AF_INET6, ai_flags, &ip6)) {
croak("MaxMind::DB::Reader::XS Invalid IPv6 Address");
}
status = MMDB_lookup_by_ipnum_128(ip6, root);
}
if (status != MMDB_SUCCESS) {
croak("MaxMind::DB::Reader::XS lookup Err %d", status);
}
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment