pub struct Locations { /* private fields */ }
Expand description
A database in libloc format. Main struct of this crate.
Implementations§
source§impl Locations
impl Locations
sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Locations, OpenError>
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)));
sourcepub fn created_at(&self) -> DateTime<Utc>
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");
sourcepub fn vendor(&self) -> &str
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");
sourcepub fn description(&self) -> &str
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");
sourcepub fn license(&self) -> &str
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");
sourcepub fn as_(&self, asn: u32) -> Option<As<'_>>
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));
sourcepub fn lookup(&self, addr: IpAddr) -> Option<Network<'_>>
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));
sourcepub fn lookup_v4(&self, addr: Ipv4Addr) -> Option<NetworkV4<'_>>
pub fn lookup_v4(&self, addr: Ipv4Addr) -> Option<NetworkV4<'_>>
Look up network information for an IPv4 address.
See Locations::lookup
.
sourcepub fn lookup_v6(&self, addr: Ipv6Addr) -> Option<NetworkV6<'_>>
pub fn lookup_v6(&self, addr: Ipv6Addr) -> Option<NetworkV6<'_>>
Look up network information for an IPv6 address.
See Locations::lookup
.
sourcepub fn country(&self, code: &str) -> Option<Country<'_>>
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));