1 /* $OpenBSD: name2id.c,v 1.12 2022/06/16 15:30:12 claudio 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/socket.h>
21
22 #include <net/route.h>
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "bgpd.h"
29
30 #define IDVAL_MAX 50000
31
32 struct n2id_label {
33 TAILQ_ENTRY(n2id_label) entry;
34 char *name;
35 uint16_t id;
36 int ref;
37 };
38
39 TAILQ_HEAD(n2id_labels, n2id_label);
40
41 uint16_t _name2id(struct n2id_labels *, const char *);
42 const char *_id2name(struct n2id_labels *, uint16_t);
43 void _unref(struct n2id_labels *, uint16_t);
44 uint16_t _ref(struct n2id_labels *, uint16_t);
45
46 struct n2id_labels rt_labels = TAILQ_HEAD_INITIALIZER(rt_labels);
47 struct n2id_labels pftable_labels = TAILQ_HEAD_INITIALIZER(pftable_labels);
48
49 uint16_t
rtlabel_name2id(const char * name)50 rtlabel_name2id(const char *name)
51 {
52 return (_name2id(&rt_labels, name));
53 }
54
55 const char *
rtlabel_id2name(uint16_t id)56 rtlabel_id2name(uint16_t id)
57 {
58 return (_id2name(&rt_labels, id));
59 }
60
61 void
rtlabel_unref(uint16_t id)62 rtlabel_unref(uint16_t id)
63 {
64 _unref(&rt_labels, id);
65 }
66
67 uint16_t
rtlabel_ref(uint16_t id)68 rtlabel_ref(uint16_t id)
69 {
70 return (_ref(&rt_labels, id));
71 }
72
73 uint16_t
pftable_name2id(const char * name)74 pftable_name2id(const char *name)
75 {
76 return (_name2id(&pftable_labels, name));
77 }
78
79 const char *
pftable_id2name(uint16_t id)80 pftable_id2name(uint16_t id)
81 {
82 return (_id2name(&pftable_labels, id));
83 }
84
85 void
pftable_unref(uint16_t id)86 pftable_unref(uint16_t id)
87 {
88 _unref(&pftable_labels, id);
89 }
90
91 uint16_t
pftable_ref(uint16_t id)92 pftable_ref(uint16_t id)
93 {
94 return (_ref(&pftable_labels, id));
95 }
96
97 /*
98 * Try to convert a name into id. If something fails 0 is returned which
99 * is the ID of the empty label.
100 */
101 uint16_t
_name2id(struct n2id_labels * head,const char * name)102 _name2id(struct n2id_labels *head, const char *name)
103 {
104 struct n2id_label *label, *p = NULL;
105 uint16_t new_id = 1;
106
107 if (!name[0])
108 return (0);
109
110 TAILQ_FOREACH(label, head, entry)
111 if (strcmp(name, label->name) == 0) {
112 label->ref++;
113 return (label->id);
114 }
115
116 /*
117 * to avoid fragmentation, we do a linear search from the beginning
118 * and take the first free slot we find. if there is none or the list
119 * is empty, append a new entry at the end.
120 */
121
122 if (!TAILQ_EMPTY(head))
123 for (p = TAILQ_FIRST(head); p != NULL &&
124 p->id == new_id; p = TAILQ_NEXT(p, entry))
125 new_id = p->id + 1;
126
127 if (new_id > IDVAL_MAX)
128 return (0);
129
130 if ((label = calloc(1, sizeof(struct n2id_label))) == NULL)
131 return (0);
132 if ((label->name = strdup(name)) == NULL) {
133 free(label);
134 return (0);
135 }
136 label->id = new_id;
137 label->ref++;
138
139 if (p != NULL) /* insert new entry before p */
140 TAILQ_INSERT_BEFORE(p, label, entry);
141 else /* either list empty or no free slot in between */
142 TAILQ_INSERT_TAIL(head, label, entry);
143
144 return (label->id);
145 }
146
147 const char *
_id2name(struct n2id_labels * head,uint16_t id)148 _id2name(struct n2id_labels *head, uint16_t id)
149 {
150 struct n2id_label *label;
151
152 if (id == 0)
153 return ("");
154
155 TAILQ_FOREACH(label, head, entry)
156 if (label->id == id)
157 return (label->name);
158
159 return ("");
160 }
161
162 void
_unref(struct n2id_labels * head,uint16_t id)163 _unref(struct n2id_labels *head, uint16_t id)
164 {
165 struct n2id_label *p, *next;
166
167 if (id == 0)
168 return;
169
170 for (p = TAILQ_FIRST(head); p != NULL; p = next) {
171 next = TAILQ_NEXT(p, entry);
172 if (id == p->id) {
173 if (--p->ref == 0) {
174 TAILQ_REMOVE(head, p, entry);
175 free(p->name);
176 free(p);
177 }
178 break;
179 }
180 }
181 }
182
183 uint16_t
_ref(struct n2id_labels * head,uint16_t id)184 _ref(struct n2id_labels *head, uint16_t id)
185 {
186 struct n2id_label *label;
187
188 if (id == 0)
189 return (0);
190
191 TAILQ_FOREACH(label, head, entry)
192 if (label->id == id) {
193 ++label->ref;
194 return (id);
195 }
196
197 /* id not found, treat like no id */
198 return (0);
199 }
200