Blob


1 /* $OpenBSD: name2id.c,v 1.4 2015/01/22 17:42:09 reyk Exp $ */
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 */
19 #include <sys/types.h>
20 #include <sys/queue.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
26 #include "relayd.h"
28 #define IDVAL_MAX 50000
30 struct n2id_label {
31 TAILQ_ENTRY(n2id_label) entry;
32 char *name;
33 u_int16_t id;
34 int ref;
35 };
37 TAILQ_HEAD(n2id_labels, n2id_label);
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);
44 struct n2id_labels relay_labels = TAILQ_HEAD_INITIALIZER(relay_labels);
45 struct n2id_labels relay_tags = TAILQ_HEAD_INITIALIZER(relay_tags);
47 u_int16_t
48 tag_name2id(const char *name)
49 {
50 return (_name2id(&relay_tags, name));
51 }
53 const char *
54 tag_id2name(u_int16_t id)
55 {
56 return (_id2name(&relay_tags, id));
57 }
59 void
60 tag_unref(u_int16_t id)
61 {
62 _unref(&relay_tags, id);
63 }
65 void
66 tag_ref(u_int16_t id)
67 {
68 _ref(&relay_tags, id);
69 }
71 u_int16_t
72 label_name2id(const char *name)
73 {
74 return (_name2id(&relay_labels, name));
75 }
77 const char *
78 label_id2name(u_int16_t id)
79 {
80 return (_id2name(&relay_labels, id));
81 }
83 void
84 label_unref(u_int16_t id)
85 {
86 _unref(&relay_labels, id);
87 }
89 void
90 label_ref(u_int16_t id)
91 {
92 _ref(&relay_labels, id);
93 }
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;
101 if (!name[0]) {
102 errno = EINVAL;
103 return (0);
106 TAILQ_FOREACH(label, head, entry)
107 if (strcmp(name, label->name) == 0) {
108 label->ref++;
109 return (label->id);
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 */
118 if (!TAILQ_EMPTY(head))
119 for (p = TAILQ_FIRST(head); p != NULL &&
120 p->id == new_id; p = TAILQ_NEXT(p, entry))
121 new_id = p->id + 1;
123 if (new_id > IDVAL_MAX) {
124 errno = ERANGE;
125 return (0);
128 if ((label = calloc(1, sizeof(struct n2id_label))) == NULL)
129 return (0);
130 if ((label->name = strdup(name)) == NULL) {
131 free(label);
132 return (0);
134 label->id = new_id;
135 label->ref++;
137 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 TAILQ_INSERT_TAIL(head, label, entry);
142 return (label->id);
145 const char *
146 _id2name(struct n2id_labels *head, u_int16_t id)
148 struct n2id_label *label;
150 if (id == 0)
151 return ("");
153 TAILQ_FOREACH(label, head, entry)
154 if (label->id == id)
155 return (label->name);
157 return ("");
160 void
161 _unref(struct n2id_labels *head, u_int16_t id)
163 struct n2id_label *p, *next;
165 if (id == 0)
166 return;
168 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
169 next = TAILQ_NEXT(p, entry);
170 if (id == p->id) {
171 if (--p->ref == 0) {
172 TAILQ_REMOVE(head, p, entry);
173 free(p->name);
174 free(p);
176 break;
181 void
182 _ref(struct n2id_labels *head, u_int16_t id)
184 struct n2id_label *label;
186 if (id == 0)
187 return;
189 TAILQ_FOREACH(label, head, entry)
190 if (label->id == id) {
191 ++label->ref;
192 break;