View Source locus_mmdb (locus v2.3.7)

API for working with MMDB

Link to this section Summary

Functions

Looks up for an entry matching Address within Database
Unpacks an EncodedDatabase binary into a ready-to-use Database.

Unpacks a single TreeAndDataSection binary into separate TreeData and DataSection binaries, leveraging Metadata to calculate the size of TreeData.

Link to this section Types

Specs

database() ::
    #{metadata := locus_mmdb_metadata:t(), tree := locus_mmdb_tree:t(), data_section := binary()}.
Link to this type

failed_to_unpack_tree_data_and_data_section/0

View Source

Specs

failed_to_unpack_tree_data_and_data_section() ::
    {intermediate_128bits_of_zero_not_found_after_tree,
     {{not_zeroes, binary()},
      {at_offset, non_neg_integer()},
      {with_metadata, locus_mmdb_metadata:t()}}} |
    {missing_data_after_tree,
     {{required, {128, bits}},
      {but_got, {0..127, bits}},
      {at_offset, non_neg_integer()},
      {with_metadata, locus_mmdb_metadata:t()}}} |
    {not_enough_data_for_tree,
     {{required, {pos_integer(), bytes}},
      {but_got, {non_neg_integer(), bytes}},
      {with_metadata, locus_mmdb_metadata:t()}}}.

Specs

unpack_error() ::
    {bad_metadata, locus_mmdb_metadata:parse_or_validation_error()} |
    failed_to_unpack_tree_data_and_data_section() |
    {bad_tree,
     {{because, locus_mmdb_tree:bad_tree_error()}, {with_metadata, locus_mmdb_metadata:t()}}}.

Link to this section Functions

Link to this function

lookup_address(Address, Database)

View Source

Specs

lookup_address(Address, Database) -> {ok, Entry} | not_found | {error, ErrorReason}
                  when
                      Address :: inet:ip_address() | string() | unicode:unicode_binary(),
                      Database :: database(),
                      Entry :: locus_mmdb_data:value(),
                      ErrorReason ::
                          database_unknown | database_not_loaded |
                          {invalid_address, Address} |
                          ipv4_database.
Looks up for an entry matching Address within Database
Link to this function

parse_all_data_section_values(Filename)

View Source

Specs

parse_all_data_section_values(file:name_all()) -> [locus_mmdb_data_raw:value()].
Link to this function

parse_data_section_value(Filename, Index)

View Source

Specs

Link to this function

unpack_database(EncodedDatabase)

View Source

Specs

unpack_database(EncodedDatabase) -> {ok, Database} | {error, ErrorReason}
                   when
                       EncodedDatabase :: binary(),
                       Database :: database(),
                       ErrorReason :: unpack_error().
Unpacks an EncodedDatabase binary into a ready-to-use Database.
Link to this function

unpack_tree_data_and_data_section(Metadata, TreeAndDataSection)

View Source

Specs

unpack_tree_data_and_data_section(Metadata, TreeAndDataSection) ->
                                     {ok, TreeData, DataSection} | {error, Reason}
                                     when
                                         Metadata :: locus_mmdb_metadata:t(),
                                         TreeAndDataSection :: binary(),
                                         TreeData :: binary(),
                                         DataSection :: binary(),
                                         Reason :: failed_to_unpack_tree_data_and_data_section().

Unpacks a single TreeAndDataSection binary into separate TreeData and DataSection binaries, leveraging Metadata to calculate the size of TreeData.

You can then use TreeData on your own or leverage it to instantiate a new locus_mmdb_tree:t(), through locus_mmdb_tree:new/5.

As for DataSection, you can also either use it on your own or pass it to the API defined under locus_mmdb_data_codec.

For how to obtain Metadata and TreeAndDataSection in the first place, see locus_mmdb_metadata:parse_and_validate/1.