Struct libloc::Locations

source ·
pub struct Locations { /* private fields */ }
Expand description

A database in libloc format. Main struct of this crate.

Implementations§

source§

impl Locations

source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Locations, OpenError>

Open a database in libloc format.

§Safety

This memory-maps the database. This is efficient, but you must make sure that it’s not modified during the usage. See the safety discussion of the Mmap struct of memmap2.

§Errors

Errors can occur when the specified database file cannot be opened for reading (e.g. because it does not exist), this is communicated via the OpenError::Open variant.

Additionally, if the opened file is not in a format valid for this crate, it is likely that the OpenError::InvalidMagic variant is returned.

If the database is obviously corrupt, e.g. truncated, other errors might be returned.

§Examples
use libloc::Locations;

let locations = Locations::open("example-location.db")?;

// IO errors while opening the file are reported via the `Open(_)`
// variant.
assert!(matches!(Locations::open("non-existing"), Err(libloc::OpenError::Open(_))));

// Files that are not in the required format are likely to give the
// `InvalidMagic` error.
assert!(matches!(Locations::open("Cargo.toml"), Err(libloc::OpenError::InvalidMagic)));
source

pub fn created_at(&self) -> DateTime<Utc>

The database creation time.

use libloc::Locations;

let locations = Locations::open("example-location.db")?;
assert_eq!(locations.created_at().to_string(), "2024-02-06 22:30:29 UTC");
source

pub fn vendor(&self) -> &str

The vendor of the database.

use libloc::Locations;

let locations = Locations::open("example-location.db")?;
assert_eq!(locations.vendor(), "IPFire Project");
source

pub fn description(&self) -> &str

The description of the database.

use libloc::Locations;

let locations = Locations::open("example-location.db")?;
assert_eq!(locations.description(), "This is a geo location database");
source

pub fn license(&self) -> &str

The license of the database.

use libloc::Locations;

let locations = Locations::open("example-location.db")?;
assert_eq!(locations.license(), "CC");
source

pub fn as_(&self, asn: u32) -> Option<As<'_>>

Look up an AS (autonomous system) by its ASN (number).

Returns None if it does not appear in the database.

use libloc::Locations;

let locations = Locations::open("example-location.db")?;
assert_eq!(locations.as_(204867).unwrap().name(), "Lightning Wire Labs GmbH");
assert!(matches!(locations.as_(0), None));
source

pub fn lookup(&self, addr: IpAddr) -> Option<Network<'_>>

Look up network information for an IP address.

use libloc::Locations;

let locations = Locations::open("example-location.db")?;
assert_eq!(locations.lookup("2a07:1c44:5800::1".parse().unwrap()).unwrap().asn(), 204867);
assert!(matches!(locations.lookup("127.0.0.1".parse().unwrap()), None));
source

pub fn lookup_v4(&self, addr: Ipv4Addr) -> Option<NetworkV4<'_>>

Look up network information for an IPv4 address.

See Locations::lookup.

source

pub fn lookup_v6(&self, addr: Ipv6Addr) -> Option<NetworkV6<'_>>

Look up network information for an IPv6 address.

See Locations::lookup.

source

pub fn country(&self, code: &str) -> Option<Country<'_>>

Look up a country by its ISO 3166-1 alpha-2 code.

use libloc::Locations;

let locations = Locations::open("example-location.db")?;
assert_eq!(locations.country("DE").unwrap().name(), "Germany");
assert!(matches!(locations.country("XX"), None));

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedDestructor for T
where T: 'static,