From: Michael Tremer Date: Mon, 8 Jan 2018 17:57:46 +0000 (+0000) Subject: Add license attribute to the database X-Git-Tag: 0.9.0~104 X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Flibloc.git;a=commitdiff_plain;h=4bf49d00974267b132a1c5bbc487570d10af1324 Add license attribute to the database Signed-off-by: Michael Tremer --- diff --git a/src/database.c b/src/database.c index 73d2394..a6e7f18 100644 --- a/src/database.c +++ b/src/database.c @@ -44,6 +44,7 @@ struct loc_database { time_t created_at; off_t vendor; off_t description; + off_t license; // ASes in the database struct loc_database_as_v0* as_v0; @@ -173,6 +174,7 @@ static int loc_database_read_header_v0(struct loc_database* db, FILE* f) { db->created_at = be64toh(header.created_at); db->vendor = be32toh(header.vendor); db->description = be32toh(header.description); + db->license = be32toh(header.license); // Open pool off_t pool_offset = be32toh(header.pool_offset); @@ -317,6 +319,10 @@ LOC_EXPORT const char* loc_database_get_description(struct loc_database* db) { return loc_stringpool_get(db->pool, db->description); } +LOC_EXPORT const char* loc_database_get_license(struct loc_database* db) { + return loc_stringpool_get(db->pool, db->license); +} + LOC_EXPORT size_t loc_database_count_as(struct loc_database* db) { return db->as_count; } diff --git a/src/libloc.sym b/src/libloc.sym index 9005010..d6028dc 100644 --- a/src/libloc.sym +++ b/src/libloc.sym @@ -42,6 +42,7 @@ global: loc_database_created_at; loc_database_get_as; loc_database_get_description; + loc_database_get_license; loc_database_get_vendor; loc_database_lookup; loc_database_lookup_from_string; @@ -64,10 +65,12 @@ global: loc_writer_add_as; loc_writer_add_network; loc_writer_get_description; + loc_writer_get_license; loc_writer_get_vendor; loc_writer_new; loc_writer_ref; loc_writer_set_description; + loc_writer_set_license; loc_writer_set_vendor; loc_writer_unref; loc_writer_write; diff --git a/src/loc/database.h b/src/loc/database.h index 42ca034..2f46e61 100644 --- a/src/loc/database.h +++ b/src/loc/database.h @@ -33,6 +33,7 @@ struct loc_database* loc_database_unref(struct loc_database* db); time_t loc_database_created_at(struct loc_database* db); const char* loc_database_get_vendor(struct loc_database* db); const char* loc_database_get_description(struct loc_database* db); +const char* loc_database_get_license(struct loc_database* db); int loc_database_get_as(struct loc_database* db, struct loc_as** as, uint32_t number); size_t loc_database_count_as(struct loc_database* db); diff --git a/src/loc/format.h b/src/loc/format.h index bfd81ba..8309d1c 100644 --- a/src/loc/format.h +++ b/src/loc/format.h @@ -44,6 +44,9 @@ struct loc_database_header_v0 { // Description of the database uint32_t description; + // License of the database + uint32_t license; + // Tells us where the ASes start uint32_t as_offset; uint32_t as_length; diff --git a/src/loc/writer.h b/src/loc/writer.h index 9b05ec7..11ab2f2 100644 --- a/src/loc/writer.h +++ b/src/loc/writer.h @@ -34,6 +34,8 @@ const char* loc_writer_get_vendor(struct loc_writer* writer); int loc_writer_set_vendor(struct loc_writer* writer, const char* vendor); const char* loc_writer_get_description(struct loc_writer* writer); int loc_writer_set_description(struct loc_writer* writer, const char* description); +const char* loc_writer_get_license(struct loc_writer* writer); +int loc_writer_set_license(struct loc_writer* writer, const char* license); int loc_writer_add_as(struct loc_writer* writer, struct loc_as** as, uint32_t number); int loc_writer_add_network(struct loc_writer* writer, struct loc_network** network, const char* string); diff --git a/src/python/database.c b/src/python/database.c index f04a44d..d41da47 100644 --- a/src/python/database.c +++ b/src/python/database.c @@ -80,6 +80,12 @@ static PyObject* Database_get_vendor(DatabaseObject* self) { return PyUnicode_FromString(vendor); } +static PyObject* Database_get_license(DatabaseObject* self) { + const char* license = loc_database_get_license(self->db); + + return PyUnicode_FromString(license); +} + static PyObject* Database_get_created_at(DatabaseObject* self) { time_t created_at = loc_database_created_at(self->db); @@ -169,6 +175,13 @@ static struct PyGetSetDef Database_getsetters[] = { NULL, NULL, }, + { + "license", + (getter)Database_get_license, + NULL, + NULL, + NULL, + }, { "vendor", (getter)Database_get_vendor, diff --git a/src/python/writer.c b/src/python/writer.c index 9a37b0e..e21e95b 100644 --- a/src/python/writer.c +++ b/src/python/writer.c @@ -82,6 +82,24 @@ static int Writer_set_description(WriterObject* self, PyObject* value) { return 0; } +static PyObject* Writer_get_license(WriterObject* self) { + const char* license = loc_writer_get_license(self->writer); + + return PyUnicode_FromString(license); +} + +static int Writer_set_license(WriterObject* self, PyObject* value) { + const char* license = PyUnicode_AsUTF8(value); + + int r = loc_writer_set_license(self->writer, license); + if (r) { + PyErr_Format(PyExc_ValueError, "Could not set license: %s", license); + return r; + } + + return 0; +} + static PyObject* Writer_add_as(WriterObject* self, PyObject* args) { struct loc_as* as; uint32_t number = 0; @@ -172,6 +190,13 @@ static struct PyGetSetDef Writer_getsetters[] = { NULL, NULL, }, + { + "license", + (getter)Writer_get_license, + (setter)Writer_set_license, + NULL, + NULL, + }, { "vendor", (getter)Writer_get_vendor, diff --git a/src/test-database.c b/src/test-database.c index b8890cb..74e83ee 100644 --- a/src/test-database.c +++ b/src/test-database.c @@ -35,6 +35,7 @@ const char* DESCRIPTION = "tempor felis enim. Integer congue nisi in maximus pretium. " "Pellentesque et turpis elementum, luctus mi at, interdum erat. " "Maecenas ut venenatis nunc."; +const char* LICENSE = "CC"; int main(int argc, char** argv) { int err; @@ -82,6 +83,22 @@ int main(int argc, char** argv) { exit(EXIT_FAILURE); } + // Set a license + err = loc_writer_set_license(writer, LICENSE); + if (err) { + fprintf(stderr, "Could not set license\n"); + exit(EXIT_FAILURE); + } + + // Retrieve license + const char* license = loc_writer_get_license(writer); + if (license) { + printf("License is: %s\n", license); + } else { + fprintf(stderr, "Could not retrieve license\n"); + exit(EXIT_FAILURE); + } + FILE* f = fopen("test.db", "w"); if (!f) { fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); diff --git a/src/writer.c b/src/writer.c index 1867e90..12a2c52 100644 --- a/src/writer.c +++ b/src/writer.c @@ -36,6 +36,7 @@ struct loc_writer { struct loc_stringpool* pool; off_t vendor; off_t description; + off_t license; struct loc_as** as; size_t as_count; @@ -130,6 +131,20 @@ LOC_EXPORT int loc_writer_set_description(struct loc_writer* writer, const char* return 0; } +LOC_EXPORT const char* loc_writer_get_license(struct loc_writer* writer) { + return loc_stringpool_get(writer->pool, writer->license); +} + +LOC_EXPORT int loc_writer_set_license(struct loc_writer* writer, const char* license) { + // Add the string to the string pool + off_t offset = loc_stringpool_add(writer->pool, license); + if (offset < 0) + return offset; + + writer->license = offset; + return 0; +} + static int __loc_as_cmp(const void* as1, const void* as2) { return loc_as_cmp(*(struct loc_as**)as1, *(struct loc_as**)as2); } @@ -401,6 +416,7 @@ LOC_EXPORT int loc_writer_write(struct loc_writer* writer, FILE* f) { struct loc_database_header_v0 header; header.vendor = htobe32(writer->vendor); header.description = htobe32(writer->description); + header.license = htobe32(writer->license); time_t now = time(NULL); header.created_at = htobe64(now);