GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ripd/name2id.c Lines: 0 49 0.0 %
Date: 2017-11-07 Branches: 0 42 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: name2id.c,v 1.1 2007/01/08 13:01:10 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 "ripd.h"
29
30
#define	IDVAL_MAX	50000
31
32
struct n2id_label {
33
	TAILQ_ENTRY(n2id_label)	 entry;
34
	char			*name;
35
	u_int16_t		 id;
36
	int			 ref;
37
};
38
39
TAILQ_HEAD(n2id_labels, n2id_label);
40
41
u_int16_t	 _name2id(struct n2id_labels *, const char *);
42
const char	*_id2name(struct n2id_labels *, u_int16_t);
43
void		 _unref(struct n2id_labels *, u_int16_t);
44
void		 _ref(struct n2id_labels *, u_int16_t);
45
46
struct n2id_labels	rt_labels = TAILQ_HEAD_INITIALIZER(rt_labels);
47
48
u_int16_t
49
rtlabel_name2id(const char *name)
50
{
51
	return (_name2id(&rt_labels, name));
52
}
53
54
const char *
55
rtlabel_id2name(u_int16_t id)
56
{
57
	return (_id2name(&rt_labels, id));
58
}
59
60
void
61
rtlabel_unref(u_int16_t id)
62
{
63
	_unref(&rt_labels, id);
64
}
65
66
/*
67
void
68
rtlabel_ref(u_int16_t id)
69
{
70
	_ref(&rt_labels, id);
71
}
72
*/
73
74
u_int16_t
75
_name2id(struct n2id_labels *head, const char *name)
76
{
77
	struct n2id_label	*label, *p = NULL;
78
	u_int16_t		 new_id = 1;
79
80
	if (!name[0]) {
81
		errno = EINVAL;
82
		return (0);
83
	}
84
85
	TAILQ_FOREACH(label, head, entry)
86
		if (strcmp(name, label->name) == 0) {
87
			label->ref++;
88
			return (label->id);
89
		}
90
91
	/*
92
	 * to avoid fragmentation, we do a linear search from the beginning
93
	 * and take the first free slot we find. if there is none or the list
94
	 * is empty, append a new entry at the end.
95
	 */
96
97
	if (!TAILQ_EMPTY(head))
98
		for (p = TAILQ_FIRST(head); p != NULL &&
99
		    p->id == new_id; p = TAILQ_NEXT(p, entry))
100
			new_id = p->id + 1;
101
102
	if (new_id > IDVAL_MAX) {
103
		errno = ERANGE;
104
		return (0);
105
	}
106
107
	if ((label = calloc(1, sizeof(struct n2id_label))) == NULL)
108
		return (0);
109
	if ((label->name = strdup(name)) == NULL) {
110
		free(label);
111
		return (0);
112
	}
113
	label->id = new_id;
114
	label->ref++;
115
116
	if (p != NULL)	/* insert new entry before p */
117
		TAILQ_INSERT_BEFORE(p, label, entry);
118
	else		/* either list empty or no free slot in between */
119
		TAILQ_INSERT_TAIL(head, label, entry);
120
121
	return (label->id);
122
}
123
124
const char *
125
_id2name(struct n2id_labels *head, u_int16_t id)
126
{
127
	struct n2id_label	*label;
128
129
	if (id == 0)
130
		return ("");
131
132
	TAILQ_FOREACH(label, head, entry)
133
		if (label->id == id)
134
			return (label->name);
135
136
	return ("");
137
}
138
139
void
140
_unref(struct n2id_labels *head, u_int16_t id)
141
{
142
	struct n2id_label	*p, *next;
143
144
	if (id == 0)
145
		return;
146
147
	for (p = TAILQ_FIRST(head); p != NULL; p = next) {
148
		next = TAILQ_NEXT(p, entry);
149
		if (id == p->id) {
150
			if (--p->ref == 0) {
151
				TAILQ_REMOVE(head, p, entry);
152
				free(p->name);
153
				free(p);
154
			}
155
			break;
156
		}
157
	}
158
}
159
160
void
161
_ref(struct n2id_labels *head, u_int16_t id)
162
{
163
	struct n2id_label	*label;
164
165
	if (id == 0)
166
		return;
167
168
	TAILQ_FOREACH(label, head, entry)
169
		if (label->id == id) {
170
			++label->ref;
171
			break;
172
		}
173
}