2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
25 #include <sys/types.h>
29 #include <loc/libloc.h>
31 #include <loc/database.h>
32 #include <loc/format.h>
33 #include <loc/private.h>
34 #include <loc/stringpool.h>
45 // ASes in the database
46 struct loc_database_as_v0
* as_v0
;
49 struct loc_stringpool
* pool
;
52 static int loc_database_read_magic(struct loc_database
* db
, FILE* f
) {
53 struct loc_database_magic magic
;
56 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), f
);
58 // Check if we have been able to read enough data
59 if (bytes_read
< sizeof(magic
)) {
60 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
61 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
65 // Compare magic bytes
66 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
67 DEBUG(db
->ctx
, "Magic value matches\n");
70 db
->version
= be16toh(magic
.version
);
71 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
76 ERROR(db
->ctx
, "Database format is not compatible\n");
82 static int loc_database_read_as_section_v0(struct loc_database
* db
,
83 FILE* f
, off_t as_offset
, size_t as_length
) {
84 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", as_offset
, as_length
);
87 db
->as_v0
= mmap(NULL
, as_length
, PROT_READ
,
88 MAP_SHARED
, fileno(f
), as_offset
);
90 if (db
->as_v0
== MAP_FAILED
)
94 db
->as_count
= as_length
/ sizeof(*db
->as_v0
);
96 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
101 static int loc_database_read_header_v0(struct loc_database
* db
, FILE* f
) {
102 struct loc_database_header_v0 header
;
105 size_t size
= fread(&header
, 1, sizeof(header
), f
);
107 if (size
< sizeof(header
)) {
108 ERROR(db
->ctx
, "Could not read enough data for header\n");
113 db
->created_at
= be64toh(header
.created_at
);
114 db
->vendor
= be32toh(header
.vendor
);
115 db
->description
= be32toh(header
.description
);
118 off_t pool_offset
= be32toh(header
.pool_offset
);
119 size_t pool_length
= be32toh(header
.pool_length
);
121 int r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
122 f
, pool_length
, pool_offset
);
127 off_t as_offset
= be32toh(header
.as_offset
);
128 size_t as_length
= be32toh(header
.as_length
);
130 r
= loc_database_read_as_section_v0(db
, f
, as_offset
, as_length
);
137 static int loc_database_read_header(struct loc_database
* db
, FILE* f
) {
138 switch (db
->version
) {
140 return loc_database_read_header_v0(db
, f
);
143 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
148 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
149 clock_t start
= clock();
152 int r
= loc_database_read_magic(db
, f
);
157 r
= loc_database_read_header(db
, f
);
161 clock_t end
= clock();
163 INFO(db
->ctx
, "Opened database in %.8fs\n",
164 (double)(end
- start
) / CLOCKS_PER_SEC
);
169 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
170 // Fail on invalid file handle
174 struct loc_database
* db
= calloc(1, sizeof(*db
));
179 db
->ctx
= loc_ref(ctx
);
182 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
184 int r
= loc_database_read(db
, f
);
186 loc_database_unref(db
);
195 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
201 static void loc_database_free(struct loc_database
* db
) {
202 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
206 int r
= munmap(db
->as_v0
, db
->as_count
* sizeof(*db
->as_v0
));
208 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
211 loc_stringpool_unref(db
->pool
);
217 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
218 if (--db
->refcount
> 0)
221 loc_database_free(db
);
225 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
226 return db
->created_at
;
229 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
230 return loc_stringpool_get(db
->pool
, db
->vendor
);
233 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
234 return loc_stringpool_get(db
->pool
, db
->description
);
237 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
241 // Returns the AS at position pos
242 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
243 if ((size_t)pos
>= db
->as_count
)
246 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", pos
);
249 switch (db
->version
) {
251 r
= loc_as_new_from_database_v0(db
->ctx
, db
->pool
, as
, db
->as_v0
+ pos
);
259 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
265 // Performs a binary search to find the AS in the list
266 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
268 off_t hi
= db
->as_count
- 1;
271 clock_t start
= clock();
274 off_t i
= (lo
+ hi
) / 2;
276 // Fetch AS in the middle between lo and hi
277 int r
= loc_database_fetch_as(db
, as
, i
);
281 // Check if this is a match
282 uint32_t as_number
= loc_as_get_number(*as
);
283 if (as_number
== number
) {
284 clock_t end
= clock();
286 // Log how fast this has been
287 DEBUG(db
->ctx
, "Found AS%u in %.8fs\n", as_number
,
288 (double)(end
- start
) / CLOCKS_PER_SEC
);
293 // If it wasn't, we release the AS and
294 // adjust our search pointers
297 if (as_number
< number
) {