1 |
|
|
/* $OpenBSD: name2id.c,v 1.4 2015/01/22 17:42:09 reyk Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2004, 2005 Henning Brauer <henning@openbsd.org> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN |
15 |
|
|
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
16 |
|
|
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
#include <sys/queue.h> |
21 |
|
|
|
22 |
|
|
#include <stdlib.h> |
23 |
|
|
#include <string.h> |
24 |
|
|
#include <errno.h> |
25 |
|
|
|
26 |
|
|
#include "relayd.h" |
27 |
|
|
|
28 |
|
|
#define IDVAL_MAX 50000 |
29 |
|
|
|
30 |
|
|
struct n2id_label { |
31 |
|
|
TAILQ_ENTRY(n2id_label) entry; |
32 |
|
|
char *name; |
33 |
|
|
u_int16_t id; |
34 |
|
|
int ref; |
35 |
|
|
}; |
36 |
|
|
|
37 |
|
|
TAILQ_HEAD(n2id_labels, n2id_label); |
38 |
|
|
|
39 |
|
|
u_int16_t _name2id(struct n2id_labels *, const char *); |
40 |
|
|
const char *_id2name(struct n2id_labels *, u_int16_t); |
41 |
|
|
void _unref(struct n2id_labels *, u_int16_t); |
42 |
|
|
void _ref(struct n2id_labels *, u_int16_t); |
43 |
|
|
|
44 |
|
|
struct n2id_labels relay_labels = TAILQ_HEAD_INITIALIZER(relay_labels); |
45 |
|
|
struct n2id_labels relay_tags = TAILQ_HEAD_INITIALIZER(relay_tags); |
46 |
|
|
|
47 |
|
|
u_int16_t |
48 |
|
|
tag_name2id(const char *name) |
49 |
|
|
{ |
50 |
|
176 |
return (_name2id(&relay_tags, name)); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
const char * |
54 |
|
|
tag_id2name(u_int16_t id) |
55 |
|
|
{ |
56 |
|
|
return (_id2name(&relay_tags, id)); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
void |
60 |
|
|
tag_unref(u_int16_t id) |
61 |
|
|
{ |
62 |
|
176 |
_unref(&relay_tags, id); |
63 |
|
88 |
} |
64 |
|
|
|
65 |
|
|
void |
66 |
|
|
tag_ref(u_int16_t id) |
67 |
|
|
{ |
68 |
|
|
_ref(&relay_tags, id); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
u_int16_t |
72 |
|
|
label_name2id(const char *name) |
73 |
|
|
{ |
74 |
|
48 |
return (_name2id(&relay_labels, name)); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
const char * |
78 |
|
|
label_id2name(u_int16_t id) |
79 |
|
|
{ |
80 |
|
|
return (_id2name(&relay_labels, id)); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
void |
84 |
|
|
label_unref(u_int16_t id) |
85 |
|
|
{ |
86 |
|
80 |
_unref(&relay_labels, id); |
87 |
|
40 |
} |
88 |
|
|
|
89 |
|
|
void |
90 |
|
|
label_ref(u_int16_t id) |
91 |
|
|
{ |
92 |
|
32 |
_ref(&relay_labels, id); |
93 |
|
16 |
} |
94 |
|
|
|
95 |
|
|
u_int16_t |
96 |
|
|
_name2id(struct n2id_labels *head, const char *name) |
97 |
|
|
{ |
98 |
|
|
struct n2id_label *label, *p = NULL; |
99 |
|
|
u_int16_t new_id = 1; |
100 |
|
|
|
101 |
✗✓ |
224 |
if (!name[0]) { |
102 |
|
|
errno = EINVAL; |
103 |
|
|
return (0); |
104 |
|
|
} |
105 |
|
|
|
106 |
✓✓ |
272 |
TAILQ_FOREACH(label, head, entry) |
107 |
✓✓ |
64 |
if (strcmp(name, label->name) == 0) { |
108 |
|
40 |
label->ref++; |
109 |
|
40 |
return (label->id); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
/* |
113 |
|
|
* to avoid fragmentation, we do a linear search from the beginning |
114 |
|
|
* and take the first free slot we find. if there is none or the list |
115 |
|
|
* is empty, append a new entry at the end. |
116 |
|
|
*/ |
117 |
|
|
|
118 |
✓✓ |
72 |
if (!TAILQ_EMPTY(head)) |
119 |
✓✓✓✗
|
80 |
for (p = TAILQ_FIRST(head); p != NULL && |
120 |
|
32 |
p->id == new_id; p = TAILQ_NEXT(p, entry)) |
121 |
|
16 |
new_id = p->id + 1; |
122 |
|
|
|
123 |
✗✓ |
72 |
if (new_id > IDVAL_MAX) { |
124 |
|
|
errno = ERANGE; |
125 |
|
|
return (0); |
126 |
|
|
} |
127 |
|
|
|
128 |
✗✓ |
72 |
if ((label = calloc(1, sizeof(struct n2id_label))) == NULL) |
129 |
|
|
return (0); |
130 |
✗✓ |
72 |
if ((label->name = strdup(name)) == NULL) { |
131 |
|
|
free(label); |
132 |
|
|
return (0); |
133 |
|
|
} |
134 |
|
72 |
label->id = new_id; |
135 |
|
72 |
label->ref++; |
136 |
|
|
|
137 |
✗✓ |
72 |
if (p != NULL) /* insert new entry before p */ |
138 |
|
|
TAILQ_INSERT_BEFORE(p, label, entry); |
139 |
|
|
else /* either list empty or no free slot in between */ |
140 |
|
72 |
TAILQ_INSERT_TAIL(head, label, entry); |
141 |
|
|
|
142 |
|
72 |
return (label->id); |
143 |
|
112 |
} |
144 |
|
|
|
145 |
|
|
const char * |
146 |
|
|
_id2name(struct n2id_labels *head, u_int16_t id) |
147 |
|
|
{ |
148 |
|
|
struct n2id_label *label; |
149 |
|
|
|
150 |
|
|
if (id == 0) |
151 |
|
|
return (""); |
152 |
|
|
|
153 |
|
|
TAILQ_FOREACH(label, head, entry) |
154 |
|
|
if (label->id == id) |
155 |
|
|
return (label->name); |
156 |
|
|
|
157 |
|
|
return (""); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
void |
161 |
|
|
_unref(struct n2id_labels *head, u_int16_t id) |
162 |
|
|
{ |
163 |
|
|
struct n2id_label *p, *next; |
164 |
|
|
|
165 |
✗✓ |
256 |
if (id == 0) |
166 |
|
|
return; |
167 |
|
|
|
168 |
✓✗ |
272 |
for (p = TAILQ_FIRST(head); p != NULL; p = next) { |
169 |
|
136 |
next = TAILQ_NEXT(p, entry); |
170 |
✓✓ |
136 |
if (id == p->id) { |
171 |
✓✓ |
128 |
if (--p->ref == 0) { |
172 |
✓✓ |
216 |
TAILQ_REMOVE(head, p, entry); |
173 |
|
72 |
free(p->name); |
174 |
|
72 |
free(p); |
175 |
|
72 |
} |
176 |
|
|
break; |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
256 |
} |
180 |
|
|
|
181 |
|
|
void |
182 |
|
|
_ref(struct n2id_labels *head, u_int16_t id) |
183 |
|
|
{ |
184 |
|
|
struct n2id_label *label; |
185 |
|
|
|
186 |
✗✓ |
32 |
if (id == 0) |
187 |
|
|
return; |
188 |
|
|
|
189 |
✓✗ |
32 |
TAILQ_FOREACH(label, head, entry) |
190 |
✓✗ |
16 |
if (label->id == id) { |
191 |
|
16 |
++label->ref; |
192 |
|
16 |
break; |
193 |
|
|
} |
194 |
|
32 |
} |