GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/relayd/name2id.c Lines: 42 61 68.9 %
Date: 2017-11-07 Branches: 25 42 59.5 %

Line Branch Exec Source
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
308
	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
308
	_unref(&relay_tags, id);
63
154
}
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
84
	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
140
	_unref(&relay_labels, id);
87
70
}
88
89
void
90
label_ref(u_int16_t id)
91
{
92
56
	_ref(&relay_labels, id);
93
28
}
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
392
	if (!name[0]) {
102
		errno = EINVAL;
103
		return (0);
104
	}
105
106
476
	TAILQ_FOREACH(label, head, entry)
107
112
		if (strcmp(name, label->name) == 0) {
108
70
			label->ref++;
109
70
			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
126
	if (!TAILQ_EMPTY(head))
119

140
		for (p = TAILQ_FIRST(head); p != NULL &&
120
56
		    p->id == new_id; p = TAILQ_NEXT(p, entry))
121
28
			new_id = p->id + 1;
122
123
126
	if (new_id > IDVAL_MAX) {
124
		errno = ERANGE;
125
		return (0);
126
	}
127
128
126
	if ((label = calloc(1, sizeof(struct n2id_label))) == NULL)
129
		return (0);
130
126
	if ((label->name = strdup(name)) == NULL) {
131
		free(label);
132
		return (0);
133
	}
134
126
	label->id = new_id;
135
126
	label->ref++;
136
137
126
	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
126
		TAILQ_INSERT_TAIL(head, label, entry);
141
142
126
	return (label->id);
143
196
}
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
448
	if (id == 0)
166
		return;
167
168
476
	for (p = TAILQ_FIRST(head); p != NULL; p = next) {
169
238
		next = TAILQ_NEXT(p, entry);
170
238
		if (id == p->id) {
171
224
			if (--p->ref == 0) {
172
378
				TAILQ_REMOVE(head, p, entry);
173
126
				free(p->name);
174
126
				free(p);
175
126
			}
176
			break;
177
		}
178
	}
179
448
}
180
181
void
182
_ref(struct n2id_labels *head, u_int16_t id)
183
{
184
	struct n2id_label	*label;
185
186
56
	if (id == 0)
187
		return;
188
189
56
	TAILQ_FOREACH(label, head, entry)
190
28
		if (label->id == id) {
191
28
			++label->ref;
192
28
			break;
193
		}
194
56
}