1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use crate::{
    error::Error,
    ip2location::{db::LocationDB, record::LocationRecord},
    ip2proxy::{db::ProxyDB, record::ProxyRecord},
};
use memmap::Mmap;
use std::{
    fs::File,
    io::{Read, Seek, SeekFrom},
    net::{IpAddr, Ipv6Addr},
    path::{Path, PathBuf},
};

// Constants for IPV6 Address
pub const FROM_6TO4: u128 = 0x2002_0000_0000_0000_0000_0000_0000_0000;
pub const TO_6TO4: u128 = 0x2002_ffff_ffff_ffff_ffff_ffff_ffff_ffff;
pub const FROM_TEREDO: u128 = 0x2001_0000_0000_0000_0000_0000_0000_0000;
pub const TO_TEREDO: u128 = 0x2001_0000_ffff_ffff_ffff_ffff_ffff_ffff;

#[derive(Debug)]
pub enum DB {
    LocationDb(LocationDB),
    ProxyDb(ProxyDB),
}

#[derive(Debug)]
pub enum Record {
    LocationDb(LocationRecord),
    ProxyDb(ProxyRecord),
}

#[derive(Debug)]
pub(crate) enum Source {
    File(PathBuf, File),
    Mmap(PathBuf, Mmap),
}

impl std::fmt::Display for Source {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::Mmap(ff, _) => write!(f, "{}", ff.display()),
            Self::File(ff, _) => write!(f, "{}", ff.display()),
        }
    }
}

impl Source {
    pub fn read_u8(&mut self, offset: u64) -> Result<u8, Error> {
        match self {
            Source::File(_, f) => {
                f.seek(SeekFrom::Start(offset - 1))?;
                let mut buf = [0_u8; 1];
                f.read_exact(&mut buf)?;
                Ok(buf[0])
            }
            Source::Mmap(_, m) => Ok(m[(offset - 1) as usize]),
        }
    }

    pub fn read_u32(&mut self, offset: u64) -> Result<u32, Error> {
        match self {
            Source::File(_, f) => {
                f.seek(SeekFrom::Start(offset - 1))?;
                let mut buf = [0_u8; 4];
                f.read_exact(&mut buf)?;
                let result = u32::from_ne_bytes(buf);
                Ok(result)
            }
            Source::Mmap(_, m) => {
                let mut buf = [0_u8; 4];
                buf[0] = m[(offset - 1) as usize];
                buf[1] = m[offset as usize];
                buf[2] = m[(offset + 1) as usize];
                buf[3] = m[(offset + 2) as usize];
                let result = u32::from_ne_bytes(buf);
                Ok(result)
            }
        }
    }

    pub fn read_f32(&mut self, offset: u64) -> Result<f32, Error> {
        match self {
            Source::File(_, f) => {
                f.seek(SeekFrom::Start(offset - 1))?;
                let mut buf = [0_u8; 4];
                f.read_exact(&mut buf)?;
                let result = f32::from_ne_bytes(buf);
                Ok(result)
            }
            Source::Mmap(_, m) => {
                let mut buf = [0_u8; 4];
                buf[0] = m[(offset - 1) as usize];
                buf[1] = m[offset as usize];
                buf[2] = m[(offset + 1) as usize];
                buf[3] = m[(offset + 2) as usize];
                let result = f32::from_ne_bytes(buf);
                Ok(result)
            }
        }
    }

    pub fn read_str(&mut self, offset: u64) -> Result<String, Error> {
        let len = self.read_u8(offset + 1)? as usize;
        match self {
            Source::File(_, f) => {
                f.seek(SeekFrom::Start(offset + 1))?;
                let mut buf = vec![0_u8; len];
                f.read_exact(&mut buf)?;
                let s = String::from_utf8(buf)?;
                Ok(s)
            }
            Source::Mmap(_, m) => {
                let mut buf = vec![0_u8; len];
                for i in 0..len {
                    buf[i] = m[(offset + 1) as usize + i];
                }
                let s = String::from_utf8(buf)?;
                Ok(s)
            }
        }
    }

    pub fn read_ipv6(&mut self, offset: u64) -> Result<Ipv6Addr, Error> {
        let mut buf = [0_u8; 16];
        let mut i = 0;
        let mut j = 15;
        while i < 16 {
            buf[i] = self.read_u8(offset + j)?;
            i += 1;
            j = j.saturating_sub(1);
        }
        let result = Ipv6Addr::from(buf);
        Ok(result)
    }
}

impl DB {
    /// Consume the unopened db and open the file.
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<DB, Error> {
        //! Loads a Ip2Location/Ip2Proxy Database .bin file from path
        //!
        //! ## Example usage
        //!
        //!```rust
        //! use ip2location::LocationDB;
        //!
        //! let mut db = LocationDB::from_file("data/IP2LOCATION-LITE-DB1.BIN").unwrap();
        //!```
        if !path.as_ref().exists() {
            return Err(Error::IoError(
                "Error opening DB file: No such file or directory".to_string(),
            ));
        }

        if let Ok(location_db) = LocationDB::from_file(&path) {
            Ok(DB::LocationDb(location_db))
        } else if let Ok(proxy_db) = ProxyDB::from_file(&path) {
            Ok(DB::ProxyDb(proxy_db))
        } else {
            Err(Error::UnknownDb)
        }
    }

    /// Consume the unopened db and mmap the file.
    pub fn from_file_mmap<P: AsRef<Path>>(path: P) -> Result<DB, Error> {
        //! Loads a Ip2Location/Ip2Proxy Database .bin file from path using
        //! mmap (memap) feature.
        //!
        //! ## Example usage
        //!
        //!```rust
        //! use ip2location::DB;
        //!
        //! let mut db = DB::from_file_mmap("data/IP2PROXY-IP-COUNTRY.BIN").unwrap();
        //!```
        if !path.as_ref().exists() {
            return Err(Error::IoError(
                "Error opening DB file: No such file or directory".to_string(),
            ));
        }

        if let Ok(location_db) = LocationDB::from_file_mmap(&path) {
            Ok(DB::LocationDb(location_db))
        } else if let Ok(proxy_db) = ProxyDB::from_file_mmap(&path) {
            Ok(DB::ProxyDb(proxy_db))
        } else {
            Err(Error::UnknownDb)
        }
    }

    pub fn print_db_info(&self) {
        //! Prints the DB Information of Ip2Location/Ip2Proxy to console
        //!
        //! ## Example usage
        //!
        //! ```rust
        //! use ip2location::DB;
        //!
        //! let mut db = DB::from_file_mmap("data/IP2LOCATION-LITE-DB1.BIN").unwrap();
        //! db.print_db_info();
        //! ```
        match self {
            Self::LocationDb(db) => db.print_db_info(),
            Self::ProxyDb(db) => db.print_db_info(),
        }
    }

    pub fn ip_lookup(&mut self, ip: IpAddr) -> Result<Record, Error> {
        //! Lookup for the given IPv4 or IPv6 and returns the
        //! Geo information or Proxy Information
        //!
        //! ## Example usage
        //!
        //!```rust
        //! use ip2location::{DB, Record};
        //!
        //! let mut db = DB::from_file("data/IP2LOCATION-LITE-DB1.IPV6.BIN").unwrap();
        //! let geo_info = db.ip_lookup("2a01:cb08:8d14::".parse().unwrap()).unwrap();
        //! println!("{:#?}", geo_info);
        //! let record = if let Record::LocationDb(rec) = geo_info {
        //!   Some(rec)
        //! } else { None };
        //! let geo_info = record.unwrap();
        //! assert!(!geo_info.country.is_none());
        //! assert_eq!(geo_info.country.unwrap().short_name, "FR")
        //!```
        match self {
            Self::LocationDb(db) => Ok(Record::LocationDb(db.ip_lookup(ip)?)),
            Self::ProxyDb(db) => Ok(Record::ProxyDb(db.ip_lookup(ip)?)),
        }
    }
}