Introduce object to store an AS
[people/ms/libloc.git] / src / as.c
CommitLineData
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
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/format.h>
24
25#include "libloc-private.h"
26#include "as.h"
27#include "stringpool.h"
28
29struct loc_as {
30 struct loc_ctx* ctx;
31 int refcount;
32
33 struct loc_stringpool* pool;
34
35 uint32_t number;
36 off_t name;
37};
38
39LOC_EXPORT int loc_as_new(struct loc_ctx* ctx, struct loc_stringpool* pool, struct loc_as** as, uint32_t number) {
40 struct loc_as* a = calloc(1, sizeof(*a));
41 if (!a)
42 return -ENOMEM;
43
44 a->ctx = loc_ref(ctx);
45 a->refcount = 1;
46 a->pool = loc_stringpool_ref(pool);
47
48 a->number = number;
49
50 DEBUG(a->ctx, "AS%u allocated at %p\n", a->number, a);
51 *as = a;
52
53 return 0;
54}
55
56LOC_EXPORT struct loc_as* loc_as_ref(struct loc_as* as) {
57 as->refcount++;
58
59 return as;
60}
61
62static void loc_as_free(struct loc_as* as) {
63 DEBUG(as->ctx, "Releasing AS%u %p\n", as->number, as);
64
65 loc_stringpool_unref(as->pool);
66 loc_unref(as->ctx);
67
68 free(as);
69}
70
71LOC_EXPORT struct loc_as* loc_as_unref(struct loc_as* as) {
72 if (--as->refcount > 0)
73 return NULL;
74
75 loc_as_free(as);
76
77 return NULL;
78}
79
80LOC_EXPORT uint32_t loc_as_get_number(struct loc_as* as) {
81 return as->number;
82}
83
84LOC_EXPORT const char* loc_as_get_name(struct loc_as* as) {
85 return loc_stringpool_get(as->pool, as->name);
86}
87
88LOC_EXPORT int loc_as_set_name(struct loc_as* as, const char* name) {
89 // Add the string to the string pool
90 off_t offset = loc_stringpool_add(as->pool, name);
91 if (offset < 0)
92 return offset;
93
94 as->name = offset;
95 return 0;
96}
97
98LOC_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
108int loc_as_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
109 struct loc_as** as, const struct loc_database_as_v0* dbobj) {
110 uint32_t number = ntohl(dbobj->number);
111
112 int r = loc_as_new(ctx, pool, as, number);
113 if (r)
114 return r;
115
116 (*as)->name = ntohl(dbobj->name);
117
118 return 0;
119}
120
121int loc_as_to_database_v0(struct loc_as* as, struct loc_database_as_v0* dbobj) {
122 dbobj->number = htonl(as->number);
123 dbobj->name = htonl(as->name);
124
125 return 0;
126}