]>
git.ipfire.org Git - location/libloc.git/blob - src/as.c
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.
27 #include <libloc/libloc.h>
28 #include <libloc/as.h>
29 #include <libloc/compat.h>
30 #include <libloc/format.h>
31 #include <libloc/private.h>
32 #include <libloc/stringpool.h>
42 LOC_EXPORT
int loc_as_new(struct loc_ctx
* ctx
, struct loc_as
** as
, uint32_t number
) {
43 struct loc_as
* a
= calloc(1, sizeof(*a
));
47 a
->ctx
= loc_ref(ctx
);
53 DEBUG(a
->ctx
, "AS%u allocated at %p\n", a
->number
, a
);
59 LOC_EXPORT
struct loc_as
* loc_as_ref(struct loc_as
* as
) {
65 static void loc_as_free(struct loc_as
* as
) {
66 DEBUG(as
->ctx
, "Releasing AS%u %p\n", as
->number
, as
);
75 LOC_EXPORT
struct loc_as
* loc_as_unref(struct loc_as
* as
) {
76 if (--as
->refcount
> 0)
84 LOC_EXPORT
uint32_t loc_as_get_number(struct loc_as
* as
) {
88 LOC_EXPORT
const char* loc_as_get_name(struct loc_as
* as
) {
92 LOC_EXPORT
int loc_as_set_name(struct loc_as
* as
, const char* name
) {
97 as
->name
= strdup(name
);
104 LOC_EXPORT
int loc_as_cmp(struct loc_as
* as1
, struct loc_as
* as2
) {
105 if (as1
->number
> as2
->number
)
108 if (as1
->number
< as2
->number
)
114 int loc_as_new_from_database_v1(struct loc_ctx
* ctx
, struct loc_stringpool
* pool
,
115 struct loc_as
** as
, const struct loc_database_as_v1
* dbobj
) {
116 uint32_t number
= be32toh(dbobj
->number
);
118 int r
= loc_as_new(ctx
, as
, number
);
122 const char* name
= loc_stringpool_get(pool
, be32toh(dbobj
->name
));
123 r
= loc_as_set_name(*as
, name
);
132 int loc_as_to_database_v1(struct loc_as
* as
, struct loc_stringpool
* pool
,
133 struct loc_database_as_v1
* dbobj
) {
134 dbobj
->number
= htobe32(as
->number
);
136 // Save the name string in the string pool
137 off_t name
= loc_stringpool_add(pool
, as
->name
? as
->name
: "");
138 dbobj
->name
= htobe32(name
);
143 int loc_as_match_string(struct loc_as
* as
, const char* string
) {
144 // Match all AS when no search string is set
148 // Cannot match anything when name is not set
152 // Search if string is in name
153 if (strcasestr(as
->name
, string
) != NULL
)