]>
git.ipfire.org Git - location/libloc.git/blob - src/as-list.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2020 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.
21 #include <loc/as-list.h>
22 #include <loc/private.h>
28 struct loc_as
* list
[1024];
33 LOC_EXPORT
int loc_as_list_new(struct loc_ctx
* ctx
,
34 struct loc_as_list
** list
) {
35 struct loc_as_list
* l
= calloc(1, sizeof(*l
));
39 l
->ctx
= loc_ref(ctx
);
42 // Do not allow this list to grow larger than this
45 DEBUG(l
->ctx
, "AS list allocated at %p\n", l
);
51 LOC_EXPORT
struct loc_as_list
* loc_as_list_ref(struct loc_as_list
* list
) {
57 static void loc_as_list_free(struct loc_as_list
* list
) {
58 DEBUG(list
->ctx
, "Releasing AS list at %p\n", list
);
60 loc_as_list_clear(list
);
66 LOC_EXPORT
struct loc_as_list
* loc_as_list_unref(struct loc_as_list
* list
) {
70 if (--list
->refcount
> 0)
73 loc_as_list_free(list
);
77 LOC_EXPORT
size_t loc_as_list_size(struct loc_as_list
* list
) {
81 LOC_EXPORT
int loc_as_list_empty(struct loc_as_list
* list
) {
82 return list
->size
== 0;
85 LOC_EXPORT
void loc_as_list_clear(struct loc_as_list
* list
) {
86 for (unsigned int i
= 0; i
< list
->size
; i
++)
87 loc_as_unref(list
->list
[i
]);
90 LOC_EXPORT
struct loc_as
* loc_as_list_get(struct loc_as_list
* list
, size_t index
) {
92 if (index
>= list
->size
)
95 return loc_as_ref(list
->list
[index
]);
98 LOC_EXPORT
int loc_as_list_append(
99 struct loc_as_list
* list
, struct loc_as
* as
) {
100 if (loc_as_list_contains(list
, as
))
103 // Check if we have space left
104 if (list
->size
== list
->max_size
) {
105 ERROR(list
->ctx
, "%p: Could not append AS to the list. List full\n", list
);
109 DEBUG(list
->ctx
, "%p: Appending AS %p to list\n", list
, as
);
111 list
->list
[list
->size
++] = loc_as_ref(as
);
116 LOC_EXPORT
int loc_as_list_contains(
117 struct loc_as_list
* list
, struct loc_as
* as
) {
118 for (unsigned int i
= 0; i
< list
->size
; i
++) {
119 if (loc_as_cmp(as
, list
->list
[i
]) == 0)
126 LOC_EXPORT
int loc_as_list_contains_number(
127 struct loc_as_list
* list
, uint32_t number
) {
130 int r
= loc_as_new(list
->ctx
, &as
, number
);
134 r
= loc_as_list_contains(list
, as
);