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.
24 #include <loc/format.h>
25 #include <loc/writer.h>
27 #include "libloc-private.h"
34 struct loc_stringpool
* pool
;
42 LOC_EXPORT
int loc_writer_new(struct loc_ctx
* ctx
, struct loc_writer
** writer
) {
43 struct loc_writer
* w
= calloc(1, sizeof(*w
));
47 w
->ctx
= loc_ref(ctx
);
50 int r
= loc_stringpool_new(ctx
, &w
->pool
);
60 LOC_EXPORT
struct loc_writer
* loc_writer_ref(struct loc_writer
* writer
) {
66 static void loc_writer_free(struct loc_writer
* writer
) {
67 DEBUG(writer
->ctx
, "Releasing writer at %p\n", writer
);
70 for (unsigned int i
= 0; i
< writer
->as_count
; i
++) {
71 loc_as_unref(writer
->as
[i
]);
74 // Unref the string pool
75 loc_stringpool_unref(writer
->pool
);
77 loc_unref(writer
->ctx
);
81 LOC_EXPORT
struct loc_writer
* loc_writer_unref(struct loc_writer
* writer
) {
82 if (--writer
->refcount
> 0)
85 loc_writer_free(writer
);
90 LOC_EXPORT
const char* loc_writer_get_vendor(struct loc_writer
* writer
) {
91 return loc_stringpool_get(writer
->pool
, writer
->vendor
);
94 LOC_EXPORT
int loc_writer_set_vendor(struct loc_writer
* writer
, const char* vendor
) {
95 // Add the string to the string pool
96 off_t offset
= loc_stringpool_add(writer
->pool
, vendor
);
100 writer
->vendor
= offset
;
104 LOC_EXPORT
const char* loc_writer_get_description(struct loc_writer
* writer
) {
105 return loc_stringpool_get(writer
->pool
, writer
->description
);
108 LOC_EXPORT
int loc_writer_set_description(struct loc_writer
* writer
, const char* description
) {
109 // Add the string to the string pool
110 off_t offset
= loc_stringpool_add(writer
->pool
, description
);
114 writer
->description
= offset
;
118 static int __loc_as_cmp(const void* as1
, const void* as2
) {
119 return loc_as_cmp(*(struct loc_as
**)as1
, *(struct loc_as
**)as2
);
122 LOC_EXPORT
int loc_writer_add_as(struct loc_writer
* writer
, struct loc_as
** as
, uint32_t number
) {
123 int r
= loc_as_new(writer
->ctx
, writer
->pool
, as
, number
);
127 // We have a new AS to add
131 writer
->as
= realloc(writer
->as
, sizeof(*writer
->as
) * writer
->as_count
);
135 // Add as last element
136 writer
->as
[writer
->as_count
- 1] = loc_as_ref(*as
);
139 qsort(writer
->as
, writer
->as_count
, sizeof(*writer
->as
), __loc_as_cmp
);
144 static void make_magic(struct loc_writer
* writer
, struct loc_database_magic
* magic
) {
146 for (unsigned int i
= 0; i
< strlen(LOC_DATABASE_MAGIC
); i
++)
147 magic
->magic
[i
] = LOC_DATABASE_MAGIC
[i
];
150 magic
->version
= htobe16(LOC_DATABASE_VERSION
);
153 static void align_page_boundary(off_t
* offset
, FILE* f
) {
154 // Move to next page boundary
155 while (*offset
% LOC_DATABASE_PAGE_SIZE
> 0)
156 *offset
+= fwrite("", 1, 1, f
);
159 static int loc_database_write_pool(struct loc_writer
* writer
,
160 struct loc_database_header_v0
* header
, off_t
* offset
, FILE* f
) {
161 // Save the offset of the pool section
162 DEBUG(writer
->ctx
, "Pool starts at %jd bytes\n", *offset
);
163 header
->pool_offset
= htobe32(*offset
);
166 size_t pool_length
= loc_stringpool_write(writer
->pool
, f
);
167 *offset
+= pool_length
;
169 DEBUG(writer
->ctx
, "Pool has a length of %zu bytes\n", pool_length
);
170 header
->pool_length
= htobe32(pool_length
);
175 static int loc_database_write_as_section(struct loc_writer
* writer
,
176 struct loc_database_header_v0
* header
, off_t
* offset
, FILE* f
) {
177 DEBUG(writer
->ctx
, "AS section starts at %jd bytes\n", *offset
);
178 header
->as_offset
= htobe32(*offset
);
180 size_t as_length
= 0;
182 struct loc_database_as_v0 as
;
183 for (unsigned int i
= 0; i
< writer
->as_count
; i
++) {
184 // Convert AS into database format
185 loc_as_to_database_v0(writer
->as
[i
], &as
);
188 offset
+= fwrite(&as
, 1, sizeof(as
), f
);
189 as_length
+= sizeof(as
);
192 DEBUG(writer
->ctx
, "AS section has a length of %zu bytes\n", as_length
);
193 header
->as_length
= htobe32(as_length
);
198 LOC_EXPORT
int loc_writer_write(struct loc_writer
* writer
, FILE* f
) {
199 struct loc_database_magic magic
;
200 make_magic(writer
, &magic
);
203 struct loc_database_header_v0 header
;
204 header
.vendor
= htobe32(writer
->vendor
);
205 header
.description
= htobe32(writer
->description
);
207 time_t now
= time(NULL
);
208 header
.created_at
= htobe64(now
);
213 // Start writing at the beginning of the file
214 r
= fseek(f
, 0, SEEK_SET
);
219 offset
+= fwrite(&magic
, 1, sizeof(magic
), f
);
221 // Skip the space we need to write the header later
222 r
= fseek(f
, sizeof(header
), SEEK_CUR
);
224 DEBUG(writer
->ctx
, "Could not seek to position after header\n");
227 offset
+= sizeof(header
);
229 align_page_boundary(&offset
, f
);
232 r
= loc_database_write_as_section(writer
, &header
, &offset
, f
);
236 align_page_boundary(&offset
, f
);
239 r
= loc_database_write_pool(writer
, &header
, &offset
, f
);
244 r
= fseek(f
, sizeof(magic
), SEEK_SET
);
248 offset
+= fwrite(&header
, 1, sizeof(header
), f
);