]>
Commit | Line | Data |
---|---|---|
a5db3e49 MT |
1 | /* |
2 | libloc - A library to determine the location of someone on the Internet | |
3 | ||
4 | Copyright (C) 2017 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 | ||
d3d8ede6 | 17 | #include <ctype.h> |
a5db3e49 MT |
18 | #include <errno.h> |
19 | #include <stdint.h> | |
20 | #include <stdlib.h> | |
29bd8211 | 21 | #include <string.h> |
a5db3e49 | 22 | |
42f3ccd7 MT |
23 | #ifdef HAVE_ENDIAN_H |
24 | # include <endian.h> | |
25 | #endif | |
26 | ||
a5db3e49 | 27 | #include <loc/libloc.h> |
9fc7f001 | 28 | #include <loc/as.h> |
42f3ccd7 | 29 | #include <loc/compat.h> |
a5db3e49 | 30 | #include <loc/format.h> |
9fc7f001 MT |
31 | #include <loc/private.h> |
32 | #include <loc/stringpool.h> | |
a5db3e49 MT |
33 | |
34 | struct loc_as { | |
35 | struct loc_ctx* ctx; | |
36 | int refcount; | |
37 | ||
a5db3e49 | 38 | uint32_t number; |
29bd8211 | 39 | char* name; |
a5db3e49 MT |
40 | }; |
41 | ||
29bd8211 | 42 | LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_as** as, uint32_t number) { |
a5db3e49 MT |
43 | struct loc_as* a = calloc(1, sizeof(*a)); |
44 | if (!a) | |
45 | return -ENOMEM; | |
46 | ||
47 | a->ctx = loc_ref(ctx); | |
48 | a->refcount = 1; | |
a5db3e49 MT |
49 | |
50 | a->number = number; | |
29bd8211 | 51 | a->name = NULL; |
a5db3e49 MT |
52 | |
53 | DEBUG(a->ctx, "AS%u allocated at %p\n", a->number, a); | |
54 | *as = a; | |
55 | ||
56 | return 0; | |
57 | } | |
58 | ||
59 | LOC_EXPORT struct loc_as* loc_as_ref(struct loc_as* as) { | |
60 | as->refcount++; | |
61 | ||
62 | return as; | |
63 | } | |
64 | ||
65 | static void loc_as_free(struct loc_as* as) { | |
66 | DEBUG(as->ctx, "Releasing AS%u %p\n", as->number, as); | |
67 | ||
29bd8211 MT |
68 | if (as->name) |
69 | free(as->name); | |
a5db3e49 | 70 | |
29bd8211 | 71 | loc_unref(as->ctx); |
a5db3e49 MT |
72 | free(as); |
73 | } | |
74 | ||
75 | LOC_EXPORT struct loc_as* loc_as_unref(struct loc_as* as) { | |
76 | if (--as->refcount > 0) | |
77 | return NULL; | |
78 | ||
79 | loc_as_free(as); | |
80 | ||
81 | return NULL; | |
82 | } | |
83 | ||
84 | LOC_EXPORT uint32_t loc_as_get_number(struct loc_as* as) { | |
85 | return as->number; | |
86 | } | |
87 | ||
88 | LOC_EXPORT const char* loc_as_get_name(struct loc_as* as) { | |
29bd8211 | 89 | return as->name; |
a5db3e49 MT |
90 | } |
91 | ||
92 | LOC_EXPORT int loc_as_set_name(struct loc_as* as, const char* name) { | |
29bd8211 | 93 | as->name = strdup(name); |
a5db3e49 | 94 | |
a5db3e49 MT |
95 | return 0; |
96 | } | |
97 | ||
98 | LOC_EXPORT int loc_as_cmp(struct loc_as* as1, struct loc_as* as2) { | |
99 | if (as1->number > as2->number) | |
100 | return 1; | |
101 | ||
102 | if (as1->number < as2->number) | |
103 | return -1; | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
b904896a MT |
108 | int loc_as_new_from_database_v1(struct loc_ctx* ctx, struct loc_stringpool* pool, |
109 | struct loc_as** as, const struct loc_database_as_v1* dbobj) { | |
29bd8211 | 110 | uint32_t number = be32toh(dbobj->number); |
a5db3e49 | 111 | |
29bd8211 | 112 | int r = loc_as_new(ctx, as, number); |
a5db3e49 MT |
113 | if (r) |
114 | return r; | |
115 | ||
29bd8211 MT |
116 | const char* name = loc_stringpool_get(pool, be32toh(dbobj->name)); |
117 | r = loc_as_set_name(*as, name); | |
118 | if (r) { | |
119 | loc_as_unref(*as); | |
120 | return r; | |
121 | } | |
a5db3e49 MT |
122 | |
123 | return 0; | |
124 | } | |
125 | ||
b904896a MT |
126 | int loc_as_to_database_v1(struct loc_as* as, struct loc_stringpool* pool, |
127 | struct loc_database_as_v1* dbobj) { | |
29bd8211 MT |
128 | dbobj->number = htobe32(as->number); |
129 | ||
130 | // Save the name string in the string pool | |
131 | off_t name = loc_stringpool_add(pool, as->name ? as->name : ""); | |
132 | dbobj->name = htobe32(name); | |
a5db3e49 MT |
133 | |
134 | return 0; | |
135 | } | |
d3d8ede6 MT |
136 | |
137 | int loc_as_match_string(struct loc_as* as, const char* string) { | |
e2d3860b MT |
138 | // Match all AS when no search string is set |
139 | if (!string) | |
140 | return 1; | |
141 | ||
d3d8ede6 | 142 | // Search if string is in name |
273948cf MT |
143 | if (strcasestr(as->name, string) != NULL) |
144 | return 1; | |
d3d8ede6 | 145 | |
273948cf | 146 | return 0; |
d3d8ede6 | 147 | } |