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>
30 #include <loc/format.h>
32 #include "libloc-private.h"
35 #include "stringpool.h"
46 // ASes in the database
47 struct loc_database_as_v0
* as_v0
;
50 struct loc_stringpool
* pool
;
53 static int loc_database_read_magic(struct loc_database
* db
, FILE* f
) {
54 struct loc_database_magic magic
;
57 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), f
);
59 // Check if we have been able to read enough data
60 if (bytes_read
< sizeof(magic
)) {
61 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
62 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
66 // Compare magic bytes
67 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
68 DEBUG(db
->ctx
, "Magic value matches\n");
71 db
->version
= be16toh(magic
.version
);
72 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
77 ERROR(db
->ctx
, "Database format is not compatible\n");
83 static int loc_database_read_as_section_v0(struct loc_database
* db
,
84 FILE* f
, off_t as_offset
, size_t as_length
) {
85 DEBUG(db
->ctx
, "Reading AS section from %jd (%zu bytes)\n", as_offset
, as_length
);
88 db
->as_v0
= mmap(NULL
, as_length
, PROT_READ
,
89 MAP_SHARED
, fileno(f
), as_offset
);
91 if (db
->as_v0
== MAP_FAILED
)
95 db
->as_count
= as_length
/ sizeof(*db
->as_v0
);
97 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
102 static int loc_database_read_header_v0(struct loc_database
* db
, FILE* f
) {
103 struct loc_database_header_v0 header
;
106 size_t size
= fread(&header
, 1, sizeof(header
), f
);
108 if (size
< sizeof(header
)) {
109 ERROR(db
->ctx
, "Could not read enough data for header\n");
114 db
->created_at
= be64toh(header
.created_at
);
115 db
->vendor
= be32toh(header
.vendor
);
116 db
->description
= be32toh(header
.description
);
119 off_t pool_offset
= be32toh(header
.pool_offset
);
120 size_t pool_length
= be32toh(header
.pool_length
);
122 int r
= loc_stringpool_open(db
->ctx
, &db
->pool
,
123 f
, pool_length
, pool_offset
);
128 off_t as_offset
= be32toh(header
.as_offset
);
129 size_t as_length
= be32toh(header
.as_length
);
131 r
= loc_database_read_as_section_v0(db
, f
, as_offset
, as_length
);
138 static int loc_database_read_header(struct loc_database
* db
, FILE* f
) {
139 switch (db
->version
) {
141 return loc_database_read_header_v0(db
, f
);
144 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
149 static int loc_database_read(struct loc_database
* db
, FILE* f
) {
150 clock_t start
= clock();
153 int r
= loc_database_read_magic(db
, f
);
158 r
= loc_database_read_header(db
, f
);
162 clock_t end
= clock();
164 INFO(db
->ctx
, "Opened database in %.8fs\n",
165 (double)(end
- start
) / CLOCKS_PER_SEC
);
170 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
171 // Fail on invalid file handle
175 struct loc_database
* db
= calloc(1, sizeof(*db
));
180 db
->ctx
= loc_ref(ctx
);
183 DEBUG(db
->ctx
, "Database object allocated at %p\n", db
);
185 int r
= loc_database_read(db
, f
);
187 loc_database_unref(db
);
196 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
202 static void loc_database_free(struct loc_database
* db
) {
203 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
207 int r
= munmap(db
->as_v0
, db
->as_count
* sizeof(*db
->as_v0
));
209 ERROR(db
->ctx
, "Could not unmap AS section: %s\n", strerror(errno
));
212 loc_stringpool_unref(db
->pool
);
218 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
219 if (--db
->refcount
> 0)
222 loc_database_free(db
);
226 LOC_EXPORT
time_t loc_database_created_at(struct loc_database
* db
) {
227 return db
->created_at
;
230 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
231 return loc_stringpool_get(db
->pool
, db
->vendor
);
234 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
235 return loc_stringpool_get(db
->pool
, db
->description
);
238 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
242 // Returns the AS at position pos
243 static int loc_database_fetch_as(struct loc_database
* db
, struct loc_as
** as
, off_t pos
) {
244 if ((size_t)pos
>= db
->as_count
)
247 DEBUG(db
->ctx
, "Fetching AS at position %jd\n", pos
);
250 switch (db
->version
) {
252 r
= loc_as_new_from_database_v0(db
->ctx
, db
->pool
, as
, db
->as_v0
+ pos
);
260 DEBUG(db
->ctx
, "Got AS%u\n", loc_as_get_number(*as
));
266 // Performs a binary search to find the AS in the list
267 LOC_EXPORT
int loc_database_get_as(struct loc_database
* db
, struct loc_as
** as
, uint32_t number
) {
269 off_t hi
= db
->as_count
- 1;
272 clock_t start
= clock();
275 off_t i
= (lo
+ hi
) / 2;
277 // Fetch AS in the middle between lo and hi
278 int r
= loc_database_fetch_as(db
, as
, i
);
282 // Check if this is a match
283 uint32_t as_number
= loc_as_get_number(*as
);
284 if (as_number
== number
) {
285 clock_t end
= clock();
287 // Log how fast this has been
288 DEBUG(db
->ctx
, "Found AS%u in %.8fs\n", as_number
,
289 (double)(end
- start
) / CLOCKS_PER_SEC
);
294 // If it wasn't, we release the AS and
295 // adjust our search pointers
298 if (as_number
< number
) {