Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / lib / libbluetooth / bluetooth.c
blobf206aeeee4d8e61d44f6f61d2eafda3534342ae0
1 /*
2 * bluetooth.c
4 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
28 * $Id: bluetooth.c,v 1.3 2003/05/20 23:04:30 max Exp $
29 * $FreeBSD$
32 #include <bluetooth.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #define _PATH_BT_HOSTS "/etc/bluetooth/hosts"
38 #define _PATH_BT_PROTOCOLS "/etc/bluetooth/protocols"
39 #define MAXALIASES 35
41 static FILE *hostf = NULL;
42 static int host_stayopen = 0;
43 static struct hostent host;
44 static bdaddr_t host_addr;
45 static char *host_addr_ptrs[2];
46 static char *host_aliases[MAXALIASES];
48 static FILE *protof = NULL;
49 static int proto_stayopen = 0;
50 static struct protoent proto;
51 static char *proto_aliases[MAXALIASES];
53 static char buf[BUFSIZ + 1];
55 static int bt_hex_byte (char const *str);
56 static int bt_hex_nibble (char nibble);
58 struct hostent *
59 bt_gethostbyname(char const *name)
61 struct hostent *p;
62 char **cp;
64 bt_sethostent(host_stayopen);
65 while ((p = bt_gethostent()) != NULL) {
66 if (strcasecmp(p->h_name, name) == 0)
67 break;
68 for (cp = p->h_aliases; *cp != 0; cp++)
69 if (strcasecmp(*cp, name) == 0)
70 goto found;
72 found:
73 bt_endhostent();
75 return (p);
78 struct hostent *
79 bt_gethostbyaddr(char const *addr, int len, int type)
81 struct hostent *p;
83 if (type != AF_BLUETOOTH || len != sizeof(bdaddr_t)) {
84 h_errno = NO_RECOVERY;
85 return (NULL);
88 bt_sethostent(host_stayopen);
89 while ((p = bt_gethostent()) != NULL)
90 if (p->h_addrtype == type && bcmp(p->h_addr, addr, len) == 0)
91 break;
92 bt_endhostent();
94 return (p);
97 struct hostent *
98 bt_gethostent(void)
100 char *p, *cp, **q;
102 if (hostf == NULL)
103 hostf = fopen(_PATH_BT_HOSTS, "r");
105 if (hostf == NULL) {
106 h_errno = NETDB_INTERNAL;
107 return (NULL);
109 again:
110 if ((p = fgets(buf, sizeof(buf), hostf)) == NULL) {
111 h_errno = HOST_NOT_FOUND;
112 return (NULL);
114 if (*p == '#')
115 goto again;
116 if ((cp = strpbrk(p, "#\n")) == NULL)
117 goto again;
118 *cp = 0;
119 if ((cp = strpbrk(p, " \t")) == NULL)
120 goto again;
121 *cp++ = 0;
122 if (bt_aton(p, &host_addr) == 0)
123 goto again;
124 host_addr_ptrs[0] = (char *) &host_addr;
125 host_addr_ptrs[1] = NULL;
126 host.h_addr_list = host_addr_ptrs;
127 host.h_length = sizeof(host_addr);
128 host.h_addrtype = AF_BLUETOOTH;
129 while (*cp == ' ' || *cp == '\t')
130 cp++;
131 host.h_name = cp;
132 q = host.h_aliases = host_aliases;
133 if ((cp = strpbrk(cp, " \t")) != NULL)
134 *cp++ = 0;
135 while (cp != NULL && *cp != 0) {
136 if (*cp == ' ' || *cp == '\t') {
137 cp++;
138 continue;
140 if (q < &host_aliases[MAXALIASES - 1])
141 *q++ = cp;
142 if ((cp = strpbrk(cp, " \t")) != NULL)
143 *cp++ = 0;
145 *q = NULL;
146 h_errno = NETDB_SUCCESS;
148 return (&host);
151 void
152 bt_sethostent(int stayopen)
154 if (hostf == NULL)
155 hostf = fopen(_PATH_BT_HOSTS, "r");
156 else
157 rewind(hostf);
159 host_stayopen = stayopen;
162 void
163 bt_endhostent(void)
165 if (hostf != NULL && host_stayopen == 0) {
166 (void) fclose(hostf);
167 hostf = NULL;
171 struct protoent *
172 bt_getprotobyname(char const *name)
174 struct protoent *p;
175 char **cp;
177 bt_setprotoent(proto_stayopen);
178 while ((p = bt_getprotoent()) != NULL) {
179 if (strcmp(p->p_name, name) == 0)
180 break;
181 for (cp = p->p_aliases; *cp != 0; cp++)
182 if (strcmp(*cp, name) == 0)
183 goto found;
185 found:
186 bt_endprotoent();
188 return (p);
191 struct protoent *
192 bt_getprotobynumber(int proto)
194 struct protoent *p;
196 bt_setprotoent(proto_stayopen);
197 while ((p = bt_getprotoent()) != NULL)
198 if (p->p_proto == proto)
199 break;
200 bt_endprotoent();
202 return (p);
205 struct protoent *
206 bt_getprotoent(void)
208 char *p, *cp, **q;
210 if (protof == NULL)
211 protof = fopen(_PATH_BT_PROTOCOLS, "r");
213 if (protof == NULL)
214 return (NULL);
215 again:
216 if ((p = fgets(buf, sizeof(buf), protof)) == NULL)
217 return (NULL);
218 if (*p == '#')
219 goto again;
220 if ((cp = strpbrk(p, "#\n")) == NULL)
221 goto again;
222 *cp = '\0';
223 proto.p_name = p;
224 if ((cp = strpbrk(p, " \t")) == NULL)
225 goto again;
226 *cp++ = '\0';
227 while (*cp == ' ' || *cp == '\t')
228 cp++;
229 if ((p = strpbrk(cp, " \t")) != NULL)
230 *p++ = '\0';
231 proto.p_proto = atoi(cp);
232 q = proto.p_aliases = proto_aliases;
233 if (p != NULL) {
234 cp = p;
235 while (cp != NULL && *cp != 0) {
236 if (*cp == ' ' || *cp == '\t') {
237 cp++;
238 continue;
240 if (q < &proto_aliases[MAXALIASES - 1])
241 *q++ = cp;
242 if ((cp = strpbrk(cp, " \t")) != NULL)
243 *cp++ = '\0';
246 *q = NULL;
248 return (&proto);
251 void
252 bt_setprotoent(int stayopen)
254 if (protof == NULL)
255 protof = fopen(_PATH_BT_PROTOCOLS, "r");
256 else
257 rewind(protof);
259 proto_stayopen = stayopen;
262 void
263 bt_endprotoent(void)
265 if (protof != NULL) {
266 (void) fclose(protof);
267 protof = NULL;
271 char const *
272 bt_ntoa(bdaddr_t const *ba, char *str)
274 static char buffer[24];
276 if (str == NULL)
277 str = buffer;
279 sprintf(str, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
280 ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);
282 return (str);
286 bt_aton(char const *str, bdaddr_t *ba)
288 int i, b;
289 char *end = NULL;
291 memset(ba, 0, sizeof(*ba));
293 for (i = 5, end = strchr(str, ':');
294 i > 0 && *str != '\0' && end != NULL;
295 i --, str = end + 1, end = strchr(str, ':')) {
296 switch (end - str) {
297 case 1:
298 b = bt_hex_nibble(str[0]);
299 break;
301 case 2:
302 b = bt_hex_byte(str);
303 break;
305 default:
306 b = -1;
307 break;
310 if (b < 0)
311 return (0);
313 ba->b[i] = b;
316 if (i != 0 || end != NULL || *str == 0)
317 return (0);
319 switch (strlen(str)) {
320 case 1:
321 b = bt_hex_nibble(str[0]);
322 break;
324 case 2:
325 b = bt_hex_byte(str);
326 break;
328 default:
329 b = -1;
330 break;
333 if (b < 0)
334 return (0);
336 ba->b[i] = b;
338 return (1);
341 static int
342 bt_hex_byte(char const *str)
344 int n1, n2;
346 if ((n1 = bt_hex_nibble(str[0])) < 0)
347 return (-1);
349 if ((n2 = bt_hex_nibble(str[1])) < 0)
350 return (-1);
352 return ((((n1 & 0x0f) << 4) | (n2 & 0x0f)) & 0xff);
355 static int
356 bt_hex_nibble(char nibble)
358 if ('0' <= nibble && nibble <= '9')
359 return (nibble - '0');
361 if ('a' <= nibble && nibble <= 'f')
362 return (nibble - 'a' + 0xa);
364 if ('A' <= nibble && nibble <= 'F')
365 return (nibble - 'A' + 0xa);
367 return (-1);