]> git.ipfire.org Git - people/ms/libloc.git/blame - src/as-list.c
export: Change back to use Network objects
[people/ms/libloc.git] / src / as-list.c
CommitLineData
84a2f0c2
MT
1/*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2020 IPFire Development Team <info@ipfire.org>
5
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.
10
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.
15*/
16
17#include <errno.h>
18#include <stdlib.h>
19
20#include <loc/as.h>
21#include <loc/as-list.h>
22#include <loc/private.h>
23
24struct loc_as_list {
25 struct loc_ctx* ctx;
26 int refcount;
27
1a415f8c
MT
28 struct loc_as** elements;
29 size_t elements_size;
30
84a2f0c2 31 size_t size;
84a2f0c2
MT
32};
33
1a415f8c
MT
34static int loc_as_list_grow(struct loc_as_list* list, size_t size) {
35 DEBUG(list->ctx, "Growing AS list %p by %zu to %zu\n",
36 list, size, list->elements_size + size);
37
38 struct loc_as** elements = reallocarray(list->elements,
39 list->elements_size + size, sizeof(*list->elements));
40 if (!elements)
41 return -errno;
42
43 list->elements = elements;
44 list->elements_size += size;
45
46 return 0;
47}
48
84a2f0c2
MT
49LOC_EXPORT int loc_as_list_new(struct loc_ctx* ctx,
50 struct loc_as_list** list) {
51 struct loc_as_list* l = calloc(1, sizeof(*l));
52 if (!l)
53 return -ENOMEM;
54
55 l->ctx = loc_ref(ctx);
56 l->refcount = 1;
57
84a2f0c2
MT
58 DEBUG(l->ctx, "AS list allocated at %p\n", l);
59 *list = l;
60
61 return 0;
62}
63
64LOC_EXPORT struct loc_as_list* loc_as_list_ref(struct loc_as_list* list) {
65 list->refcount++;
66
67 return list;
68}
69
70static void loc_as_list_free(struct loc_as_list* list) {
71 DEBUG(list->ctx, "Releasing AS list at %p\n", list);
72
73 loc_as_list_clear(list);
74
75 loc_unref(list->ctx);
76 free(list);
77}
78
79LOC_EXPORT struct loc_as_list* loc_as_list_unref(struct loc_as_list* list) {
80 if (!list)
81 return NULL;
82
83 if (--list->refcount > 0)
84 return list;
85
86 loc_as_list_free(list);
87 return NULL;
88}
89
90LOC_EXPORT size_t loc_as_list_size(struct loc_as_list* list) {
91 return list->size;
92}
93
94LOC_EXPORT int loc_as_list_empty(struct loc_as_list* list) {
95 return list->size == 0;
96}
97
98LOC_EXPORT void loc_as_list_clear(struct loc_as_list* list) {
99 for (unsigned int i = 0; i < list->size; i++)
1a415f8c 100 loc_as_unref(list->elements[i]);
84a2f0c2
MT
101}
102
103LOC_EXPORT struct loc_as* loc_as_list_get(struct loc_as_list* list, size_t index) {
104 // Check index
105 if (index >= list->size)
106 return NULL;
107
1a415f8c 108 return loc_as_ref(list->elements[index]);
84a2f0c2
MT
109}
110
111LOC_EXPORT int loc_as_list_append(
112 struct loc_as_list* list, struct loc_as* as) {
113 if (loc_as_list_contains(list, as))
114 return 0;
115
116 // Check if we have space left
1a415f8c
MT
117 if (list->size >= list->elements_size) {
118 int r = loc_as_list_grow(list, 64);
119 if (r)
120 return r;
84a2f0c2
MT
121 }
122
123 DEBUG(list->ctx, "%p: Appending AS %p to list\n", list, as);
124
1a415f8c 125 list->elements[list->size++] = loc_as_ref(as);
84a2f0c2
MT
126
127 return 0;
128}
129
130LOC_EXPORT int loc_as_list_contains(
131 struct loc_as_list* list, struct loc_as* as) {
132 for (unsigned int i = 0; i < list->size; i++) {
1a415f8c 133 if (loc_as_cmp(as, list->elements[i]) == 0)
84a2f0c2
MT
134 return 1;
135 }
136
137 return 0;
138}
139
140LOC_EXPORT int loc_as_list_contains_number(
141 struct loc_as_list* list, uint32_t number) {
142 struct loc_as* as;
143
144 int r = loc_as_new(list->ctx, &as, number);
145 if (r)
146 return -1;
147
148 r = loc_as_list_contains(list, as);
149 loc_as_unref(as);
150
151 return r;
152}