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>
25 #include <loc/format.h>
26 #include <loc/writer.h>
28 #include "libloc-private.h"
35 struct loc_stringpool
* pool
;
43 LOC_EXPORT
int loc_writer_new(struct loc_ctx
* ctx
, struct loc_writer
** writer
) {
44 struct loc_writer
* w
= calloc(1, sizeof(*w
));
48 w
->ctx
= loc_ref(ctx
);
51 int r
= loc_stringpool_new(ctx
, &w
->pool
);
61 LOC_EXPORT
struct loc_writer
* loc_writer_ref(struct loc_writer
* writer
) {
67 static void loc_writer_free(struct loc_writer
* writer
) {
68 DEBUG(writer
->ctx
, "Releasing writer at %p\n", writer
);
71 for (unsigned int i
= 0; i
< writer
->as_count
; i
++) {
72 loc_as_unref(writer
->as
[i
]);
75 // Unref the string pool
76 loc_stringpool_unref(writer
->pool
);
78 loc_unref(writer
->ctx
);
82 LOC_EXPORT
struct loc_writer
* loc_writer_unref(struct loc_writer
* writer
) {
83 if (--writer
->refcount
> 0)
86 loc_writer_free(writer
);
91 LOC_EXPORT
const char* loc_writer_get_vendor(struct loc_writer
* writer
) {
92 return loc_stringpool_get(writer
->pool
, writer
->vendor
);
95 LOC_EXPORT
int loc_writer_set_vendor(struct loc_writer
* writer
, const char* vendor
) {
96 // Add the string to the string pool
97 off_t offset
= loc_stringpool_add(writer
->pool
, vendor
);
101 writer
->vendor
= offset
;
105 LOC_EXPORT
const char* loc_writer_get_description(struct loc_writer
* writer
) {
106 return loc_stringpool_get(writer
->pool
, writer
->description
);
109 LOC_EXPORT
int loc_writer_set_description(struct loc_writer
* writer
, const char* description
) {
110 // Add the string to the string pool
111 off_t offset
= loc_stringpool_add(writer
->pool
, description
);
115 writer
->description
= offset
;
119 static int __loc_as_cmp(const void* as1
, const void* as2
) {
120 return loc_as_cmp(*(struct loc_as
**)as1
, *(struct loc_as
**)as2
);
123 LOC_EXPORT
int loc_writer_add_as(struct loc_writer
* writer
, struct loc_as
** as
, uint32_t number
) {
124 int r
= loc_as_new(writer
->ctx
, writer
->pool
, as
, number
);
128 // We have a new AS to add
132 writer
->as
= realloc(writer
->as
, sizeof(*writer
->as
) * writer
->as_count
);
136 // Add as last element
137 writer
->as
[writer
->as_count
- 1] = loc_as_ref(*as
);
140 qsort(writer
->as
, writer
->as_count
, sizeof(*writer
->as
), __loc_as_cmp
);
145 static void make_magic(struct loc_writer
* writer
, struct loc_database_magic
* magic
) {
147 for (unsigned int i
= 0; i
< strlen(LOC_DATABASE_MAGIC
); i
++)
148 magic
->magic
[i
] = LOC_DATABASE_MAGIC
[i
];
151 magic
->version
= htons(LOC_DATABASE_VERSION
);
154 static void align_page_boundary(off_t
* offset
, FILE* f
) {
155 // Move to next page boundary
156 while (*offset
% LOC_DATABASE_PAGE_SIZE
> 0)
157 *offset
+= fwrite("", 1, 1, f
);
160 static int loc_database_write_pool(struct loc_writer
* writer
,
161 struct loc_database_header_v0
* header
, off_t
* offset
, FILE* f
) {
162 // Save the offset of the pool section
163 DEBUG(writer
->ctx
, "Pool starts at %jd bytes\n", *offset
);
164 header
->pool_offset
= htonl(*offset
);
167 size_t pool_length
= loc_stringpool_write(writer
->pool
, f
);
168 *offset
+= pool_length
;
170 DEBUG(writer
->ctx
, "Pool has a length of %zu bytes\n", pool_length
);
171 header
->pool_length
= htonl(pool_length
);
176 static int loc_database_write_as_section(struct loc_writer
* writer
,
177 struct loc_database_header_v0
* header
, off_t
* offset
, FILE* f
) {
178 DEBUG(writer
->ctx
, "AS section starts at %jd bytes\n", *offset
);
179 header
->as_offset
= htonl(*offset
);
181 size_t as_length
= 0;
183 struct loc_database_as_v0 as
;
184 for (unsigned int i
= 0; i
< writer
->as_count
; i
++) {
185 // Convert AS into database format
186 loc_as_to_database_v0(writer
->as
[i
], &as
);
189 offset
+= fwrite(&as
, 1, sizeof(as
), f
);
190 as_length
+= sizeof(as
);
193 DEBUG(writer
->ctx
, "AS section has a length of %zu bytes\n", as_length
);
194 header
->as_length
= htonl(as_length
);
199 LOC_EXPORT
int loc_writer_write(struct loc_writer
* writer
, FILE* f
) {
200 struct loc_database_magic magic
;
201 make_magic(writer
, &magic
);
204 struct loc_database_header_v0 header
;
205 header
.vendor
= htonl(writer
->vendor
);
206 header
.description
= htonl(writer
->description
);
208 time_t now
= time(NULL
);
209 header
.created_at
= htobe64(now
);
214 // Start writing at the beginning of the file
215 r
= fseek(f
, 0, SEEK_SET
);
220 offset
+= fwrite(&magic
, 1, sizeof(magic
), f
);
222 // Skip the space we need to write the header later
223 r
= fseek(f
, sizeof(header
), SEEK_CUR
);
225 DEBUG(writer
->ctx
, "Could not seek to position after header\n");
228 offset
+= sizeof(header
);
230 align_page_boundary(&offset
, f
);
233 r
= loc_database_write_as_section(writer
, &header
, &offset
, f
);
237 align_page_boundary(&offset
, f
);
240 r
= loc_database_write_pool(writer
, &header
, &offset
, f
);
245 r
= fseek(f
, sizeof(magic
), SEEK_SET
);
249 offset
+= fwrite(&header
, 1, sizeof(header
), f
);