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.
21 #include <sys/queue.h>
28 #include <loc/libloc.h>
30 #include <loc/compat.h>
31 #include <loc/country.h>
32 #include <loc/format.h>
33 #include <loc/network.h>
34 #include <loc/private.h>
35 #include <loc/writer.h>
41 struct loc_stringpool
* pool
;
49 struct loc_country
** countries
;
50 size_t countries_count
;
52 struct loc_network_tree
* networks
;
55 LOC_EXPORT
int loc_writer_new(struct loc_ctx
* ctx
, struct loc_writer
** writer
) {
56 struct loc_writer
* w
= calloc(1, sizeof(*w
));
60 w
->ctx
= loc_ref(ctx
);
63 int r
= loc_stringpool_new(ctx
, &w
->pool
);
69 // Initialize the network tree
70 r
= loc_network_tree_new(ctx
, &w
->networks
);
80 LOC_EXPORT
struct loc_writer
* loc_writer_ref(struct loc_writer
* writer
) {
86 static void loc_writer_free(struct loc_writer
* writer
) {
87 DEBUG(writer
->ctx
, "Releasing writer at %p\n", writer
);
90 for (unsigned int i
= 0; i
< writer
->as_count
; i
++) {
91 loc_as_unref(writer
->as
[i
]);
94 // Release network tree
96 loc_network_tree_unref(writer
->networks
);
98 // Unref the string pool
99 loc_stringpool_unref(writer
->pool
);
101 loc_unref(writer
->ctx
);
105 LOC_EXPORT
struct loc_writer
* loc_writer_unref(struct loc_writer
* writer
) {
106 if (--writer
->refcount
> 0)
109 loc_writer_free(writer
);
114 LOC_EXPORT
const char* loc_writer_get_vendor(struct loc_writer
* writer
) {
115 return loc_stringpool_get(writer
->pool
, writer
->vendor
);
118 LOC_EXPORT
int loc_writer_set_vendor(struct loc_writer
* writer
, const char* vendor
) {
119 // Add the string to the string pool
120 off_t offset
= loc_stringpool_add(writer
->pool
, vendor
);
124 writer
->vendor
= offset
;
128 LOC_EXPORT
const char* loc_writer_get_description(struct loc_writer
* writer
) {
129 return loc_stringpool_get(writer
->pool
, writer
->description
);
132 LOC_EXPORT
int loc_writer_set_description(struct loc_writer
* writer
, const char* description
) {
133 // Add the string to the string pool
134 off_t offset
= loc_stringpool_add(writer
->pool
, description
);
138 writer
->description
= offset
;
142 LOC_EXPORT
const char* loc_writer_get_license(struct loc_writer
* writer
) {
143 return loc_stringpool_get(writer
->pool
, writer
->license
);
146 LOC_EXPORT
int loc_writer_set_license(struct loc_writer
* writer
, const char* license
) {
147 // Add the string to the string pool
148 off_t offset
= loc_stringpool_add(writer
->pool
, license
);
152 writer
->license
= offset
;
156 static int __loc_as_cmp(const void* as1
, const void* as2
) {
157 return loc_as_cmp(*(struct loc_as
**)as1
, *(struct loc_as
**)as2
);
160 LOC_EXPORT
int loc_writer_add_as(struct loc_writer
* writer
, struct loc_as
** as
, uint32_t number
) {
161 int r
= loc_as_new(writer
->ctx
, as
, number
);
165 // We have a new AS to add
169 writer
->as
= realloc(writer
->as
, sizeof(*writer
->as
) * writer
->as_count
);
173 // Add as last element
174 writer
->as
[writer
->as_count
- 1] = loc_as_ref(*as
);
177 qsort(writer
->as
, writer
->as_count
, sizeof(*writer
->as
), __loc_as_cmp
);
182 LOC_EXPORT
int loc_writer_add_network(struct loc_writer
* writer
, struct loc_network
** network
, const char* string
) {
185 // Create a new network object
186 r
= loc_network_new_from_string(writer
->ctx
, network
, string
);
190 // Add it to the local tree
191 return loc_network_tree_add_network(writer
->networks
, *network
);
194 static int __loc_country_cmp(const void* country1
, const void* country2
) {
195 return loc_country_cmp(*(struct loc_country
**)country1
, *(struct loc_country
**)country2
);
198 LOC_EXPORT
int loc_writer_add_country(struct loc_writer
* writer
, struct loc_country
** country
, const char* country_code
) {
199 int r
= loc_country_new(writer
->ctx
, country
, country_code
);
203 // We have a new country to add
204 writer
->countries_count
++;
207 writer
->countries
= realloc(writer
->countries
, sizeof(*writer
->countries
) * writer
->countries_count
);
208 if (!writer
->countries
)
211 // Add as last element
212 writer
->countries
[writer
->countries_count
- 1] = loc_country_ref(*country
);
215 qsort(writer
->countries
, writer
->countries_count
, sizeof(*writer
->countries
), __loc_country_cmp
);
220 static void make_magic(struct loc_writer
* writer
, struct loc_database_magic
* magic
) {
222 for (unsigned int i
= 0; i
< strlen(LOC_DATABASE_MAGIC
); i
++)
223 magic
->magic
[i
] = LOC_DATABASE_MAGIC
[i
];
226 magic
->version
= htobe16(LOC_DATABASE_VERSION
);
229 static void align_page_boundary(off_t
* offset
, FILE* f
) {
230 // Move to next page boundary
231 while (*offset
% LOC_DATABASE_PAGE_SIZE
> 0)
232 *offset
+= fwrite("", 1, 1, f
);
235 static int loc_database_write_pool(struct loc_writer
* writer
,
236 struct loc_database_header_v0
* header
, off_t
* offset
, FILE* f
) {
237 // Save the offset of the pool section
238 DEBUG(writer
->ctx
, "Pool starts at %jd bytes\n", *offset
);
239 header
->pool_offset
= htobe32(*offset
);
242 size_t pool_length
= loc_stringpool_write(writer
->pool
, f
);
243 *offset
+= pool_length
;
245 DEBUG(writer
->ctx
, "Pool has a length of %zu bytes\n", pool_length
);
246 header
->pool_length
= htobe32(pool_length
);
251 static int loc_database_write_as_section(struct loc_writer
* writer
,
252 struct loc_database_header_v0
* header
, off_t
* offset
, FILE* f
) {
253 DEBUG(writer
->ctx
, "AS section starts at %jd bytes\n", *offset
);
254 header
->as_offset
= htobe32(*offset
);
256 size_t as_length
= 0;
258 struct loc_database_as_v0 as
;
259 for (unsigned int i
= 0; i
< writer
->as_count
; i
++) {
260 // Convert AS into database format
261 loc_as_to_database_v0(writer
->as
[i
], writer
->pool
, &as
);
264 *offset
+= fwrite(&as
, 1, sizeof(as
), f
);
265 as_length
+= sizeof(as
);
268 DEBUG(writer
->ctx
, "AS section has a length of %zu bytes\n", as_length
);
269 header
->as_length
= htobe32(as_length
);
271 align_page_boundary(offset
, f
);
277 TAILQ_ENTRY(node
) nodes
;
279 struct loc_network_tree_node
* node
;
281 // Indices of the child nodes
286 static struct node
* make_node(struct loc_network_tree_node
* node
) {
287 struct node
* n
= malloc(sizeof(*n
));
291 n
->node
= loc_network_tree_node_ref(node
);
292 n
->index_zero
= n
->index_one
= 0;
297 static void free_node(struct node
* node
) {
298 loc_network_tree_node_unref(node
->node
);
304 TAILQ_ENTRY(network
) networks
;
306 struct loc_network
* network
;
309 static struct network
* make_network(struct loc_network
* network
) {
310 struct network
* n
= malloc(sizeof(*n
));
314 n
->network
= loc_network_ref(network
);
319 static void free_network(struct network
* network
) {
320 loc_network_unref(network
->network
);
325 static int loc_database_write_networks(struct loc_writer
* writer
,
326 struct loc_database_header_v0
* header
, off_t
* offset
, FILE* f
) {
327 // Write the network tree
328 DEBUG(writer
->ctx
, "Network tree starts at %jd bytes\n", *offset
);
329 header
->network_tree_offset
= htobe32(*offset
);
331 size_t network_tree_length
= 0;
332 size_t network_data_length
= 0;
335 struct node
* child_node
;
338 uint32_t network_index
= 0;
340 struct loc_database_network_v0 db_network
;
341 struct loc_database_network_node_v0 db_node
;
343 // Initialize queue for nodes
344 TAILQ_HEAD(node_t
, node
) nodes
;
347 // Initialize queue for networks
348 TAILQ_HEAD(network_t
, network
) networks
;
349 TAILQ_INIT(&networks
);
352 struct loc_network_tree_node
* root
= loc_network_tree_get_root(writer
->networks
);
353 node
= make_node(root
);
355 TAILQ_INSERT_TAIL(&nodes
, node
, nodes
);
357 while (!TAILQ_EMPTY(&nodes
)) {
358 // Pop first node in list
359 node
= TAILQ_FIRST(&nodes
);
360 TAILQ_REMOVE(&nodes
, node
, nodes
);
362 DEBUG(writer
->ctx
, "Processing node %p\n", node
);
365 struct loc_network_tree_node
* node_zero
= loc_network_tree_node_get(node
->node
, 0);
367 node
->index_zero
= ++index
;
369 child_node
= make_node(node_zero
);
370 loc_network_tree_node_unref(node_zero
);
372 TAILQ_INSERT_TAIL(&nodes
, child_node
, nodes
);
375 struct loc_network_tree_node
* node_one
= loc_network_tree_node_get(node
->node
, 1);
377 node
->index_one
= ++index
;
379 child_node
= make_node(node_one
);
380 loc_network_tree_node_unref(node_one
);
382 TAILQ_INSERT_TAIL(&nodes
, child_node
, nodes
);
385 // Prepare what we are writing to disk
386 db_node
.zero
= htobe32(node
->index_zero
);
387 db_node
.one
= htobe32(node
->index_one
);
389 if (loc_network_tree_node_is_leaf(node
->node
)) {
390 struct loc_network
* network
= loc_network_tree_node_get_network(node
->node
);
392 // Append network to be written out later
393 struct network
* nw
= make_network(network
);
394 TAILQ_INSERT_TAIL(&networks
, nw
, networks
);
396 db_node
.network
= htobe32(network_index
++);
397 loc_network_unref(network
);
399 db_node
.network
= htobe32(0xffffffff);
402 // Write the current node
403 DEBUG(writer
->ctx
, "Writing node %p (0 = %d, 1 = %d)\n",
404 node
, node
->index_zero
, node
->index_one
);
406 *offset
+= fwrite(&db_node
, 1, sizeof(db_node
), f
);
407 network_tree_length
+= sizeof(db_node
);
412 loc_network_tree_node_unref(root
);
414 header
->network_tree_length
= htobe32(network_tree_length
);
416 align_page_boundary(offset
, f
);
418 DEBUG(writer
->ctx
, "Networks data section starts at %jd bytes\n", *offset
);
419 header
->network_data_offset
= htobe32(*offset
);
421 // We have now written the entire tree and have all networks
422 // in a queue in order as they are indexed
423 while (!TAILQ_EMPTY(&networks
)) {
424 struct network
* nw
= TAILQ_FIRST(&networks
);
425 TAILQ_REMOVE(&networks
, nw
, networks
);
427 // Prepare what we are writing to disk
428 int r
= loc_network_to_database_v0(nw
->network
, &db_network
);
432 *offset
+= fwrite(&db_network
, 1, sizeof(db_network
), f
);
433 network_data_length
+= sizeof(db_network
);
438 header
->network_data_length
= htobe32(network_data_length
);
440 align_page_boundary(offset
, f
);
445 static int loc_database_write_countries(struct loc_writer
* writer
,
446 struct loc_database_header_v0
* header
, off_t
* offset
, FILE* f
) {
447 DEBUG(writer
->ctx
, "Countries section starts at %jd bytes\n", *offset
);
448 header
->countries_offset
= htobe32(*offset
);
450 size_t countries_length
= 0;
452 struct loc_database_country_v0 country
;
453 for (unsigned int i
= 0; i
< writer
->countries_count
; i
++) {
454 // Convert country into database format
455 loc_country_to_database_v0(writer
->countries
[i
], writer
->pool
, &country
);
458 *offset
+= fwrite(&country
, 1, sizeof(country
), f
);
459 countries_length
+= sizeof(country
);
462 DEBUG(writer
->ctx
, "Countries section has a length of %zu bytes\n", countries_length
);
463 header
->countries_length
= htobe32(countries_length
);
465 align_page_boundary(offset
, f
);
470 LOC_EXPORT
int loc_writer_write(struct loc_writer
* writer
, FILE* f
) {
471 struct loc_database_magic magic
;
472 make_magic(writer
, &magic
);
475 struct loc_database_header_v0 header
;
476 header
.vendor
= htobe32(writer
->vendor
);
477 header
.description
= htobe32(writer
->description
);
478 header
.license
= htobe32(writer
->license
);
480 time_t now
= time(NULL
);
481 header
.created_at
= htobe64(now
);
486 // Start writing at the beginning of the file
487 r
= fseek(f
, 0, SEEK_SET
);
492 offset
+= fwrite(&magic
, 1, sizeof(magic
), f
);
494 // Skip the space we need to write the header later
495 r
= fseek(f
, sizeof(header
), SEEK_CUR
);
497 DEBUG(writer
->ctx
, "Could not seek to position after header\n");
500 offset
+= sizeof(header
);
502 align_page_boundary(&offset
, f
);
505 r
= loc_database_write_as_section(writer
, &header
, &offset
, f
);
509 // Write all networks
510 r
= loc_database_write_networks(writer
, &header
, &offset
, f
);
515 r
= loc_database_write_countries(writer
, &header
, &offset
, f
);
520 r
= loc_database_write_pool(writer
, &header
, &offset
, f
);
525 r
= fseek(f
, sizeof(magic
), SEEK_SET
);
529 offset
+= fwrite(&header
, 1, sizeof(header
), f
);