]>
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 | ||
29bd8211 | 17 | #include <endian.h> |
a5db3e49 MT |
18 | #include <errno.h> |
19 | #include <stdint.h> | |
20 | #include <stdlib.h> | |
29bd8211 | 21 | #include <string.h> |
a5db3e49 MT |
22 | |
23 | #include <loc/libloc.h> | |
9fc7f001 | 24 | #include <loc/as.h> |
a5db3e49 | 25 | #include <loc/format.h> |
9fc7f001 MT |
26 | #include <loc/private.h> |
27 | #include <loc/stringpool.h> | |
a5db3e49 MT |
28 | |
29 | struct loc_as { | |
30 | struct loc_ctx* ctx; | |
31 | int refcount; | |
32 | ||
a5db3e49 | 33 | uint32_t number; |
29bd8211 | 34 | char* name; |
a5db3e49 MT |
35 | }; |
36 | ||
29bd8211 | 37 | LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_as** as, uint32_t number) { |
a5db3e49 MT |
38 | struct loc_as* a = calloc(1, sizeof(*a)); |
39 | if (!a) | |
40 | return -ENOMEM; | |
41 | ||
42 | a->ctx = loc_ref(ctx); | |
43 | a->refcount = 1; | |
a5db3e49 MT |
44 | |
45 | a->number = number; | |
29bd8211 | 46 | a->name = NULL; |
a5db3e49 MT |
47 | |
48 | DEBUG(a->ctx, "AS%u allocated at %p\n", a->number, a); | |
49 | *as = a; | |
50 | ||
51 | return 0; | |
52 | } | |
53 | ||
54 | LOC_EXPORT struct loc_as* loc_as_ref(struct loc_as* as) { | |
55 | as->refcount++; | |
56 | ||
57 | return as; | |
58 | } | |
59 | ||
60 | static void loc_as_free(struct loc_as* as) { | |
61 | DEBUG(as->ctx, "Releasing AS%u %p\n", as->number, as); | |
62 | ||
29bd8211 MT |
63 | if (as->name) |
64 | free(as->name); | |
a5db3e49 | 65 | |
29bd8211 | 66 | loc_unref(as->ctx); |
a5db3e49 MT |
67 | free(as); |
68 | } | |
69 | ||
70 | LOC_EXPORT struct loc_as* loc_as_unref(struct loc_as* as) { | |
71 | if (--as->refcount > 0) | |
72 | return NULL; | |
73 | ||
74 | loc_as_free(as); | |
75 | ||
76 | return NULL; | |
77 | } | |
78 | ||
79 | LOC_EXPORT uint32_t loc_as_get_number(struct loc_as* as) { | |
80 | return as->number; | |
81 | } | |
82 | ||
83 | LOC_EXPORT const char* loc_as_get_name(struct loc_as* as) { | |
29bd8211 | 84 | return as->name; |
a5db3e49 MT |
85 | } |
86 | ||
87 | LOC_EXPORT int loc_as_set_name(struct loc_as* as, const char* name) { | |
29bd8211 | 88 | as->name = strdup(name); |
a5db3e49 | 89 | |
a5db3e49 MT |
90 | return 0; |
91 | } | |
92 | ||
93 | LOC_EXPORT int loc_as_cmp(struct loc_as* as1, struct loc_as* as2) { | |
94 | if (as1->number > as2->number) | |
95 | return 1; | |
96 | ||
97 | if (as1->number < as2->number) | |
98 | return -1; | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
103 | int loc_as_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool, | |
104 | struct loc_as** as, const struct loc_database_as_v0* dbobj) { | |
29bd8211 | 105 | uint32_t number = be32toh(dbobj->number); |
a5db3e49 | 106 | |
29bd8211 | 107 | int r = loc_as_new(ctx, as, number); |
a5db3e49 MT |
108 | if (r) |
109 | return r; | |
110 | ||
29bd8211 MT |
111 | const char* name = loc_stringpool_get(pool, be32toh(dbobj->name)); |
112 | r = loc_as_set_name(*as, name); | |
113 | if (r) { | |
114 | loc_as_unref(*as); | |
115 | return r; | |
116 | } | |
a5db3e49 MT |
117 | |
118 | return 0; | |
119 | } | |
120 | ||
29bd8211 MT |
121 | int loc_as_to_database_v0(struct loc_as* as, struct loc_stringpool* pool, |
122 | struct loc_database_as_v0* dbobj) { | |
123 | dbobj->number = htobe32(as->number); | |
124 | ||
125 | // Save the name string in the string pool | |
126 | off_t name = loc_stringpool_add(pool, as->name ? as->name : ""); | |
127 | dbobj->name = htobe32(name); | |
a5db3e49 MT |
128 | |
129 | return 0; | |
130 | } |