1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/*
* Ouroboros - Copyright (C) 2016 - 2026
*
* Names registered with the local IPCP
*
* Dimitri Staessens <dimitri@ouroboros.rocks>
* Sander Vrijders <sander@ouroboros.rocks>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., http://www.fsf.org/about/contact/.
*/
#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 200112L
#endif
#define OUROBOROS_PREFIX "local-reg"
#include <ouroboros/hash.h>
#include <ouroboros/list.h>
#include <ouroboros/logs.h>
#include "reg.h"
#include "ipcp.h"
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
struct reg_entry {
struct list_head list;
uint8_t * hash;
};
struct reg {
struct list_head names;
pthread_rwlock_t lock;
};
static struct reg_entry * reg_entry_create(uint8_t * hash)
{
struct reg_entry * entry;
entry = malloc(sizeof(*entry));
if (entry == NULL)
return NULL;
list_head_init(&entry->list);
entry->hash = hash;
return entry;
}
static void reg_entry_destroy(struct reg_entry * entry)
{
assert(entry);
free(entry->hash);
free(entry);
}
/* Call with the lock held. */
static struct reg_entry * reg_find(struct reg * reg,
const uint8_t * hash)
{
struct list_head * p;
list_for_each(p, ®->names) {
struct reg_entry * e;
e = list_entry(p, struct reg_entry, list);
if (memcmp(e->hash, hash, ipcp_dir_hash_len()) == 0)
return e;
}
return NULL;
}
struct reg * reg_create(void)
{
struct reg * reg;
reg = malloc(sizeof(*reg));
if (reg == NULL)
goto fail_malloc;
list_head_init(®->names);
if (pthread_rwlock_init(®->lock, NULL) < 0)
goto fail_lock;
return reg;
fail_lock:
free(reg);
fail_malloc:
return NULL;
}
void reg_destroy(struct reg * reg)
{
if (reg == NULL)
return;
pthread_rwlock_wrlock(®->lock);
while (!list_is_empty(®->names)) {
struct reg_entry * e;
e = list_first_entry(®->names, struct reg_entry, list);
list_del(&e->list);
reg_entry_destroy(e);
}
pthread_rwlock_unlock(®->lock);
pthread_rwlock_destroy(®->lock);
free(reg);
}
int reg_add(struct reg * reg,
const uint8_t * hash)
{
struct reg_entry * entry;
uint8_t * dup;
assert(reg);
assert(hash);
pthread_rwlock_wrlock(®->lock);
if (reg_find(reg, hash) != NULL) {
pthread_rwlock_unlock(®->lock);
log_dbg(HASH_FMT32 " was already registered.",
HASH_VAL32(hash));
return 0;
}
dup = ipcp_hash_dup(hash);
if (dup == NULL)
goto fail;
entry = reg_entry_create(dup);
if (entry == NULL) {
free(dup);
goto fail;
}
list_add(&entry->list, ®->names);
pthread_rwlock_unlock(®->lock);
return 0;
fail:
pthread_rwlock_unlock(®->lock);
return -1;
}
int reg_del(struct reg * reg,
const uint8_t * hash)
{
struct reg_entry * e;
if (reg == NULL)
return -1;
pthread_rwlock_wrlock(®->lock);
e = reg_find(reg, hash);
if (e == NULL) {
pthread_rwlock_unlock(®->lock);
return 0; /* nothing to do */
}
list_del(&e->list);
pthread_rwlock_unlock(®->lock);
reg_entry_destroy(e);
return 0;
}
bool reg_has(struct reg * reg,
const uint8_t * hash)
{
bool ret;
assert(reg);
assert(hash);
pthread_rwlock_rdlock(®->lock);
ret = reg_find(reg, hash) != NULL;
pthread_rwlock_unlock(®->lock);
return ret;
}
|