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.
17 #include <arpa/inet.h>
24 #include <sys/types.h>
27 #include <loc/libloc.h>
28 #include <loc/format.h>
30 #include "libloc-private.h"
33 #include "stringpool.h"
44 // ASes in the database
48 struct loc_stringpool
* pool
;
51 LOC_EXPORT
int loc_database_new(struct loc_ctx
* ctx
, struct loc_database
** database
, size_t pool_size
) {
52 struct loc_database
* db
= calloc(1, sizeof(*db
));
57 db
->ctx
= loc_ref(ctx
);
60 DEBUG(db
->ctx
, "Database allocated at %p\n", db
);
63 int r
= loc_stringpool_new(db
->ctx
, &db
->pool
, pool_size
);
65 loc_database_unref(db
);
74 LOC_EXPORT
int loc_database_open(struct loc_ctx
* ctx
, struct loc_database
** database
, FILE* f
) {
75 int r
= loc_database_new(ctx
, database
, 0);
79 return loc_database_read(*database
, f
);
82 LOC_EXPORT
struct loc_database
* loc_database_ref(struct loc_database
* db
) {
88 static void loc_database_free(struct loc_database
* db
) {
89 DEBUG(db
->ctx
, "Releasing database %p\n", db
);
91 // Remove references to all ASes
93 for (unsigned int i
= 0; i
< db
->as_count
; i
++) {
94 loc_as_unref(db
->as
[i
]);
99 loc_stringpool_unref(db
->pool
);
109 LOC_EXPORT
struct loc_database
* loc_database_unref(struct loc_database
* db
) {
110 if (--db
->refcount
> 0)
113 loc_database_free(db
);
117 LOC_EXPORT
const char* loc_database_get_vendor(struct loc_database
* db
) {
118 return loc_stringpool_get(db
->pool
, db
->vendor
);
121 LOC_EXPORT
int loc_database_set_vendor(struct loc_database
* db
, const char* vendor
) {
122 // Add the string to the string pool
123 off_t offset
= loc_stringpool_add(db
->pool
, vendor
);
131 LOC_EXPORT
const char* loc_database_get_description(struct loc_database
* db
) {
132 return loc_stringpool_get(db
->pool
, db
->description
);
135 LOC_EXPORT
int loc_database_set_description(struct loc_database
* db
, const char* description
) {
136 // Add the string to the string pool
137 off_t offset
= loc_stringpool_add(db
->pool
, description
);
141 db
->description
= offset
;
145 LOC_EXPORT
size_t loc_database_count_as(struct loc_database
* db
) {
149 static int loc_database_has_as(struct loc_database
* db
, struct loc_as
* as
) {
150 for (unsigned int i
= 0; i
< db
->as_count
; i
++) {
151 if (loc_as_cmp(as
, db
->as
[i
]) == 0)
158 static int __loc_as_cmp(const void* as1
, const void* as2
) {
159 return loc_as_cmp(*(struct loc_as
**)as1
, *(struct loc_as
**)as2
);
162 static void loc_database_sort_ases(struct loc_database
* db
) {
163 qsort(db
->as
, db
->as_count
, sizeof(*db
->as
), __loc_as_cmp
);
166 static struct loc_as
* __loc_database_add_as(struct loc_database
* db
, struct loc_as
* as
) {
167 // Check if AS exists already
168 int i
= loc_database_has_as(db
, as
);
172 // Select already existing AS
175 return loc_as_ref(as
);
180 // Make space for the new entry
181 db
->as
= realloc(db
->as
, sizeof(*db
->as
) * db
->as_count
);
183 // Add the new entry at the end
184 db
->as
[db
->as_count
- 1] = loc_as_ref(as
);
187 loc_database_sort_ases(db
);
192 LOC_EXPORT
struct loc_as
* loc_database_add_as(struct loc_database
* db
, uint32_t number
) {
194 int r
= loc_as_new(db
->ctx
, db
->pool
, &as
, number
);
198 return __loc_database_add_as(db
, as
);
201 static int loc_database_read_magic(struct loc_database
* db
) {
202 struct loc_database_magic magic
;
205 size_t bytes_read
= fread(&magic
, 1, sizeof(magic
), db
->file
);
207 // Check if we have been able to read enough data
208 if (bytes_read
< sizeof(magic
)) {
209 ERROR(db
->ctx
, "Could not read enough data to validate magic bytes\n");
210 DEBUG(db
->ctx
, "Read %zu bytes, but needed %zu\n", bytes_read
, sizeof(magic
));
214 // Compare magic bytes
215 if (memcmp(LOC_DATABASE_MAGIC
, magic
.magic
, strlen(LOC_DATABASE_MAGIC
)) == 0) {
216 DEBUG(db
->ctx
, "Magic value matches\n");
219 db
->version
= ntohs(magic
.version
);
220 DEBUG(db
->ctx
, "Database version is %u\n", db
->version
);
225 ERROR(db
->ctx
, "Database format is not compatible\n");
231 static int loc_database_read_as_section_v0(struct loc_database
* db
,
232 off_t as_offset
, size_t as_length
) {
233 struct loc_database_as_v0 dbobj
;
235 // Read from the start of the section
236 int r
= fseek(db
->file
, as_offset
, SEEK_SET
);
241 size_t as_count
= as_length
/ sizeof(dbobj
);
242 for (unsigned int i
= 0; i
< as_count
; i
++) {
243 size_t bytes_read
= fread(&dbobj
, 1, sizeof(dbobj
), db
->file
);
244 if (bytes_read
< sizeof(dbobj
)) {
245 ERROR(db
->ctx
, "Could not read an AS object\n");
251 r
= loc_as_new_from_database_v0(db
->ctx
, db
->pool
, &as
, &dbobj
);
255 // Attach it to the database
256 as
= __loc_database_add_as(db
, as
);
260 INFO(db
->ctx
, "Read %zu ASes from the database\n", db
->as_count
);
265 static int loc_database_read_header_v0(struct loc_database
* db
) {
266 struct loc_database_header_v0 header
;
269 size_t size
= fread(&header
, 1, sizeof(header
), db
->file
);
271 if (size
< sizeof(header
)) {
272 ERROR(db
->ctx
, "Could not read enough data for header\n");
277 db
->vendor
= ntohl(header
.vendor
);
278 db
->description
= ntohl(header
.description
);
281 off_t pool_offset
= ntohl(header
.pool_offset
);
282 size_t pool_length
= ntohl(header
.pool_length
);
284 int r
= loc_stringpool_read(db
->pool
, db
->file
, pool_offset
, pool_length
);
289 off_t as_offset
= ntohl(header
.as_offset
);
290 size_t as_length
= ntohl(header
.as_length
);
292 r
= loc_database_read_as_section_v0(db
, as_offset
, as_length
);
299 static int loc_database_read_header(struct loc_database
* db
) {
300 switch (db
->version
) {
302 return loc_database_read_header_v0(db
);
305 ERROR(db
->ctx
, "Incompatible database version: %u\n", db
->version
);
310 LOC_EXPORT
int loc_database_read(struct loc_database
* db
, FILE* f
) {
311 // Copy the file pointer and work on that so we don't care if
312 // the calling function closes the file
318 // Retrieve a file pointer
319 db
->file
= fdopen(fd
, "r");
323 int r
= fseek(db
->file
, 0, SEEK_SET
);
328 r
= loc_database_read_magic(db
);
333 r
= loc_database_read_header(db
);
340 static void loc_database_make_magic(struct loc_database
* db
, struct loc_database_magic
* magic
) {
342 for (unsigned int i
= 0; i
< strlen(LOC_DATABASE_MAGIC
); i
++)
343 magic
->magic
[i
] = LOC_DATABASE_MAGIC
[i
];
346 magic
->version
= htons(LOC_DATABASE_VERSION
);
349 LOC_EXPORT
int loc_database_write(struct loc_database
* db
, FILE* f
) {
350 struct loc_database_magic magic
;
351 loc_database_make_magic(db
, &magic
);
354 struct loc_database_header_v0 header
;
355 header
.vendor
= htonl(db
->vendor
);
356 header
.description
= htonl(db
->description
);
361 // Start writing at the beginning of the file
362 r
= fseek(f
, 0, SEEK_SET
);
367 offset
+= fwrite(&magic
, 1, sizeof(magic
), f
);
369 // Skip the space we need to write the header later
370 r
= fseek(f
, sizeof(header
), SEEK_CUR
);
372 DEBUG(db
->ctx
, "Could not seek to position after header\n");
375 offset
+= sizeof(header
);
378 header
.as_offset
= htonl(offset
);
380 struct loc_database_as_v0 dbas
;
381 for (unsigned int i
= 0; i
< db
->as_count
; i
++) {
382 // Convert AS into database format
383 loc_as_to_database_v0(db
->as
[i
], &dbas
);
386 offset
+= fwrite(&dbas
, 1, sizeof(dbas
), f
);
388 header
.as_length
= htonl(db
->as_count
* sizeof(dbas
));
390 // Save the offset of the pool section
391 DEBUG(db
->ctx
, "Pool starts at %jd bytes\n", offset
);
392 header
.pool_offset
= htonl(offset
);
395 size_t pool_length
= loc_stringpool_write(db
->pool
, f
);
396 DEBUG(db
->ctx
, "Pool has a length of %zu bytes\n", pool_length
);
397 header
.pool_length
= htonl(pool_length
);
400 r
= fseek(f
, sizeof(magic
), SEEK_SET
);
404 offset
+= fwrite(&header
, 1, sizeof(header
), f
);