Lesser General Public License for more details.
*/
-#include <errno.h>
#include <stdlib.h>
#include <libloc/as.h>
struct loc_as** elements = reallocarray(list->elements,
list->elements_size + size, sizeof(*list->elements));
if (!elements)
- return -errno;
+ return 1;
list->elements = elements;
list->elements_size += size;
struct loc_as_list** list) {
struct loc_as_list* l = calloc(1, sizeof(*l));
if (!l)
- return -ENOMEM;
+ return 1;
l->ctx = loc_ref(ctx);
l->refcount = 1;
LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_as** as, uint32_t number) {
struct loc_as* a = calloc(1, sizeof(*a));
if (!a)
- return -ENOMEM;
+ return 1;
a->ctx = loc_ref(ctx);
a->refcount = 1;
struct loc_country** elements = reallocarray(list->elements,
list->elements_size + size, sizeof(*list->elements));
if (!elements)
- return -errno;
+ return 1;
list->elements = elements;
list->elements_size += size;
// Ignore invalid country codes which would never match
if (errno == EINVAL)
return 0;
- else
- return r;
+
+ return r;
}
r = loc_country_list_contains(list, country);
struct loc_country* c = calloc(1, sizeof(*c));
if (!c)
- return -ENOMEM;
+ return 1;
c->ctx = loc_ref(ctx);
c->refcount = 1;
MAP_SHARED, fileno(db->f), as_offset);
if (db->as_v1 == MAP_FAILED)
- return -errno;
+ return 1;
}
db->as_count = as_length / sizeof(*db->as_v1);
MAP_SHARED, fileno(db->f), network_nodes_offset);
if (db->network_nodes_v1 == MAP_FAILED)
- return -errno;
+ return 1;
}
db->network_nodes_count = network_nodes_length / sizeof(*db->network_nodes_v1);
MAP_SHARED, fileno(db->f), networks_offset);
if (db->networks_v1 == MAP_FAILED)
- return -errno;
+ return 1;
}
db->networks_count = networks_length / sizeof(*db->networks_v1);
MAP_SHARED, fileno(db->f), countries_offset);
if (db->countries_v1 == MAP_FAILED)
- return -errno;
+ return 1;
}
db->countries_count = countries_length / sizeof(*db->countries_v1);
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
-#include <errno.h>
#include <string.h>
#include <ctype.h>
LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
struct loc_ctx* c = calloc(1, sizeof(*c));
if (!c)
- return -ENOMEM;
+ return 1;
c->refcount = 1;
c->log_fn = log_stderr;
struct loc_network** elements = reallocarray(list->elements,
list->elements_size + size, sizeof(*list->elements));
if (!elements)
- return -errno;
+ return 1;
list->elements = elements;
list->elements_size += size;
}
struct loc_network* n = calloc(1, sizeof(*n));
- if (!n) {
- errno = ENOMEM;
+ if (!n)
return 1;
- }
n->ctx = loc_ref(ctx);
n->refcount = 1;
int r = loc_network_new(ctx, network, address, prefix);
if (r) {
- ERROR(ctx, "Could not allocate a new network: %s", strerror(-r));
+ ERROR(ctx, "Could not allocate a new network: %m\n");
return r;
}
int loc_network_tree_new(struct loc_ctx* ctx, struct loc_network_tree** tree) {
struct loc_network_tree* t = calloc(1, sizeof(*t));
if (!t)
- return -ENOMEM;
+ return 1;
t->ctx = loc_ref(ctx);
t->refcount = 1;
struct loc_as_list* asns;
r = loc_as_list_new(loc_ctx, &asns);
if (r) {
- PyErr_SetString(PyExc_SystemError, "Could not create AS list");
+ PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
struct loc_as* as;
r = loc_as_new(loc_ctx, &as, number);
if (r) {
- PyErr_SetString(PyExc_SystemError, "Could not create AS");
+ PyErr_SetFromErrno(PyExc_OSError);
loc_as_list_unref(asns);
loc_as_unref(as);
r = loc_as_list_append(asns, as);
if (r) {
- PyErr_SetString(PyExc_SystemError, "Could not append AS to the list");
+ PyErr_SetFromErrno(PyExc_OSError);
loc_as_list_unref(asns);
loc_as_unref(as);
r = loc_database_enumerator_set_asns(enumerator, asns);
if (r) {
- PyErr_SetFromErrno(PyExc_SystemError);
+ PyErr_SetFromErrno(PyExc_OSError);
loc_as_list_unref(asns);
return NULL;
r = loc_database_enumerator_set_flag(enumerator, flags);
if (r) {
- PyErr_SetFromErrno(PyExc_SystemError);
+ PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
}
r = loc_database_enumerator_set_family(enumerator, family);
if (r) {
- PyErr_SetFromErrno(PyExc_SystemError);
+ PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
}
};
static off_t loc_stringpool_get_offset(struct loc_stringpool* pool, const char* pos) {
- if (pos < pool->data)
- return -EFAULT;
+ if (pos < pool->data) {
+ errno = EFAULT;
+ return -1;
+ }
- if (pos > (pool->data + pool->length))
- return -EFAULT;
+ if (pos > (pool->data + pool->length)) {
+ errno = EFAULT;
+ return -1;
+ }
return pos - pool->data;
}
// Reallocate data section
pool->data = realloc(pool->data, length);
if (!pool->data)
- return -ENOMEM;
+ return 1;
pool->length = length;
}
static off_t loc_stringpool_append(struct loc_stringpool* pool, const char* string) {
- if (!string)
- return -EINVAL;
+ if (!string) {
+ errno = EINVAL;
+ return -1;
+ }
DEBUG(pool->ctx, "Appending '%s' to string pool at %p\n", string, pool);
// Make sure we have enough space
int r = loc_stringpool_grow(pool, pool->length + strlen(string) + 1);
- if (r) {
- errno = r;
+ if (r)
return -1;
- }
off_t offset = loc_stringpool_get_offset(pool, pool->pos);
}
static int loc_stringpool_mmap(struct loc_stringpool* pool, FILE* f, size_t length, off_t offset) {
- if (pool->mode != STRINGPOOL_MMAP)
- return -EINVAL;
+ if (pool->mode != STRINGPOOL_MMAP) {
+ errno = EINVAL;
+ return 1;
+ }
DEBUG(pool->ctx, "Reading string pool starting from %jd (%zu bytes)\n", (intmax_t)offset, length);
}
static off_t loc_stringpool_find(struct loc_stringpool* pool, const char* s) {
- if (!s || !*s)
- return -EINVAL;
+ if (!s || !*s) {
+ errno = EINVAL;
+ return -1;
+ }
off_t offset = 0;
while (offset < pool->length) {
offset = loc_stringpool_get_next_offset(pool, offset);
}
- return -ENOENT;
+ // Nothing found
+ errno = ENOENT;
+ return -1;
}
off_t loc_stringpool_add(struct loc_stringpool* pool, const char* string) {
err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
if (err) {
- fprintf(stderr, "Could not write database: %s\n", strerror(-err));
+ fprintf(stderr, "Could not write database: %m\n");
exit(EXIT_FAILURE);
}
struct loc_database* db;
err = loc_database_new(ctx, &db, f);
if (err) {
- fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+ fprintf(stderr, "Could not open database: %m\n");
exit(EXIT_FAILURE);
}
err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
if (err) {
- fprintf(stderr, "Could not write database: %s\n", strerror(-err));
+ fprintf(stderr, "Could not write database: %m\n");
exit(EXIT_FAILURE);
}
loc_writer_unref(writer);
struct loc_database* db;
err = loc_database_new(ctx, &db, f);
if (err) {
- fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+ fprintf(stderr, "Could not open database: %m\n");
exit(EXIT_FAILURE);
}
err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
if (err) {
- fprintf(stderr, "Could not write database: %s\n", strerror(err));
+ fprintf(stderr, "Could not write database: %m\n");
exit(EXIT_FAILURE);
}
loc_writer_unref(writer);
struct loc_database* db;
err = loc_database_new(ctx, &db, f);
if (err) {
- fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+ fprintf(stderr, "Could not open database: %m\n");
exit(EXIT_FAILURE);
}
err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
if (err) {
- fprintf(stderr, "Could not write database: %s\n", strerror(-err));
+ fprintf(stderr, "Could not write database: %m\n");
exit(EXIT_FAILURE);
}
loc_writer_unref(writer);
struct loc_database* db;
err = loc_database_new(ctx, &db, f);
if (err) {
- fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+ fprintf(stderr, "Could not open database: %m\n");
exit(EXIT_FAILURE);
}
err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
if (err) {
- fprintf(stderr, "Could not write database: %s\n", strerror(err));
+ fprintf(stderr, "Could not write database: %m\n");
exit(EXIT_FAILURE);
}
loc_writer_unref(writer);
struct loc_database* db;
err = loc_database_new(ctx, &db, f);
if (err) {
- fprintf(stderr, "Could not open database: %s\n", strerror(-err));
+ fprintf(stderr, "Could not open database: %m\n");
exit(EXIT_FAILURE);
}
// Append a string
off_t pos = loc_stringpool_add(pool, "ABC");
if (pos < 0) {
- fprintf(stderr, "Could not add string: %s\n", strerror(-pos));
+ fprintf(stderr, "Could not add string: %m\n");
exit(EXIT_FAILURE);
}
free(string);
if (pos < 0) {
- fprintf(stderr, "Could not add string %d: %s\n", i, strerror(-pos));
+ fprintf(stderr, "Could not add string %d: %m\n", i);
exit(EXIT_FAILURE);
}
}
FILE* fkey1, FILE* fkey2) {
struct loc_writer* w = calloc(1, sizeof(*w));
if (!w)
- return -ENOMEM;
+ return 1;
w->ctx = loc_ref(ctx);
w->refcount = 1;
if (ferror(f)) {
ERROR(writer->ctx, "Error reading from file: %m\n");
- r = errno;
+ r = 1;
goto END;
}