Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #9 from measurement-kit/feature/as_prefix
Browse files Browse the repository at this point in the history
API CHANGE: let lookup_asn return a AS-prefixed string
  • Loading branch information
bassosimone authored Feb 5, 2019
2 parents b4cdebe + fd184ae commit c5ef899
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion mkmmdb-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ int main(int, char **argv) {
if (!ok) exit(EXIT_FAILURE); \
} while (0)
LOOKUP(country_db, cc);
LOOKUP(asn_db, asn);
LOOKUP(asn_db, asn2);
LOOKUP(asn_db, org);
}
18 changes: 11 additions & 7 deletions mkmmdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ class Handle {
bool lookup_cc(const std::string &ip, std::string &cc,
std::vector<std::string> &logs) noexcept;

/// lookup_asn is like lookup_cc except that it looks up the autonomous
/// system number (ASN), which will be saved into the @p cc string.
bool lookup_asn(const std::string &ip, std::string &asn,
std::vector<std::string> &logs) noexcept;
/// lookup_asn2 is like lookup_cc except that it looks up the autonomous
/// system number (ASN), which will be saved into the @p cc string. Please,
/// note that the ASN will be in the format `AS<number>`.
bool lookup_asn2(const std::string &ip, std::string &asn,
std::vector<std::string> &logs) noexcept;

/// lookup_org is like lookup_cc except that it looks up the autonomous
/// system organization name, which will be saved into @p org.
Expand Down Expand Up @@ -271,12 +272,15 @@ bool Handle::Impl::finish_lookup_asn(
if (!ok) {
return false;
}
asn = std::to_string(data.uint32);
// Historical note: since version v0.4.0, the ASN that we return is
// prefixed with `"AS"`. For this reason we did change the name of
// the publicly exposed API from `lookup_asn` to `lookup_asn2`.
asn = std::string{"AS"} + std::to_string(data.uint32);
return true;
}

bool Handle::lookup_asn(const std::string &ip, std::string &asn,
std::vector<std::string> &logs) noexcept {
bool Handle::lookup_asn2(const std::string &ip, std::string &asn,
std::vector<std::string> &logs) noexcept {
return impl->lookup(
ip, logs, [&](MMDB_entry_s *entry) {
return impl->finish_lookup_asn(entry, asn, logs);
Expand Down
6 changes: 3 additions & 3 deletions tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST_CASE("When MMDB_lookup_string fails with mmdb_error") {
mk::mmdb::Handle handle;
std::string asn;
REQUIRE(handle.open("asn.mmdb", logs) == true);
REQUIRE(handle.lookup_asn("8.8.8.8", asn, logs) == false);
REQUIRE(handle.lookup_asn2("8.8.8.8", asn, logs) == false);
});
}

Expand Down Expand Up @@ -71,15 +71,15 @@ TEST_CASE("When lookup_cc fails because MMDB provides us a bad value") {
});
}

TEST_CASE("When lookup_asn fails because MMDB provides us a bad value") {
TEST_CASE("When lookup_asn2 fails because MMDB provides us a bad value") {
MKMOCK_WITH_ENABLED_HOOK(
finish_lookup_asn_check, false,
{
std::vector<std::string> logs;
mk::mmdb::Handle handle;
std::string asn;
REQUIRE(handle.open("asn.mmdb", logs) == true);
REQUIRE(handle.lookup_asn("8.8.8.8", asn, logs) == false);
REQUIRE(handle.lookup_asn2("8.8.8.8", asn, logs) == false);
});
}

Expand Down

0 comments on commit c5ef899

Please sign in to comment.