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