]> git.ipfire.org Git - people/ms/libloc.git/blob - src/as.c
as: Return NULL if name is not set
[people/ms/libloc.git] / src / as.c
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
17 #include <arpa/inet.h>
18 #include <errno.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21
22 #include <loc/libloc.h>
23 #include <loc/as.h>
24 #include <loc/format.h>
25 #include <loc/private.h>
26 #include <loc/stringpool.h>
27
28 struct loc_as {
29 struct loc_ctx* ctx;
30 int refcount;
31
32 struct loc_stringpool* pool;
33
34 uint32_t number;
35 off_t name;
36 };
37
38 LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_stringpool* pool, struct loc_as** as, uint32_t number) {
39 struct loc_as* a = calloc(1, sizeof(*a));
40 if (!a)
41 return -ENOMEM;
42
43 a->ctx = loc_ref(ctx);
44 a->refcount = 1;
45 a->pool = loc_stringpool_ref(pool);
46
47 a->number = number;
48
49 DEBUG(a->ctx, "AS%u allocated at %p\n", a->number, a);
50 *as = a;
51
52 return 0;
53 }
54
55 LOC_EXPORT struct loc_as* loc_as_ref(struct loc_as* as) {
56 as->refcount++;
57
58 return as;
59 }
60
61 static void loc_as_free(struct loc_as* as) {
62 DEBUG(as->ctx, "Releasing AS%u %p\n", as->number, as);
63
64 loc_stringpool_unref(as->pool);
65 loc_unref(as->ctx);
66
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) {
84 if (as->name)
85 return loc_stringpool_get(as->pool, as->name);
86
87 return NULL;
88 }
89
90 LOC_EXPORT int loc_as_set_name(struct loc_as* as, const char* name) {
91 // Add the string to the string pool
92 off_t offset = loc_stringpool_add(as->pool, name);
93 if (offset < 0)
94 return offset;
95
96 as->name = offset;
97 return 0;
98 }
99
100 LOC_EXPORT int loc_as_cmp(struct loc_as* as1, struct loc_as* as2) {
101 if (as1->number > as2->number)
102 return 1;
103
104 if (as1->number < as2->number)
105 return -1;
106
107 return 0;
108 }
109
110 int loc_as_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
111 struct loc_as** as, const struct loc_database_as_v0* dbobj) {
112 uint32_t number = ntohl(dbobj->number);
113
114 int r = loc_as_new(ctx, pool, as, number);
115 if (r)
116 return r;
117
118 (*as)->name = ntohl(dbobj->name);
119
120 return 0;
121 }
122
123 int loc_as_to_database_v0(struct loc_as* as, struct loc_database_as_v0* dbobj) {
124 dbobj->number = htonl(as->number);
125 dbobj->name = htonl(as->name);
126
127 return 0;
128 }