6 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
8 * Copyright (c) 2001-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: bluetooth.c,v 1.3 2003/05/20 23:04:30 max Exp $
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
41 #define _PATH_BT_HOSTS "/etc/bluetooth/hosts"
42 #define _PATH_BT_PROTOCOLS "/etc/bluetooth/protocols"
45 static FILE *hostf = NULL;
46 static int host_stayopen = 0;
47 static struct hostent host;
48 static bdaddr_t host_addr;
49 static char *host_addr_ptrs[2];
50 static char *host_aliases[MAXALIASES];
52 static FILE *protof = NULL;
53 static int proto_stayopen = 0;
54 static struct protoent proto;
55 static char *proto_aliases[MAXALIASES];
57 static char buf[BUFSIZ + 1];
59 static int bt_hex_byte (char const *str);
60 static int bt_hex_nibble (char nibble);
63 bt_gethostbyname(char const *name)
68 bt_sethostent(host_stayopen);
69 while ((p = bt_gethostent()) != NULL) {
70 if (strcasecmp(p->h_name, name) == 0)
72 for (cp = p->h_aliases; *cp != NULL; cp++)
73 if (strcasecmp(*cp, name) == 0)
83 bt_gethostbyaddr(char const *addr, int len, int type)
87 if (type != AF_BLUETOOTH || len != sizeof(bdaddr_t)) {
88 h_errno = NO_RECOVERY;
92 bt_sethostent(host_stayopen);
93 while ((p = bt_gethostent()) != NULL)
94 if (p->h_addrtype == type && bcmp(p->h_addr, addr, len) == 0)
107 hostf = fopen(_PATH_BT_HOSTS, "r");
110 h_errno = NETDB_INTERNAL;
114 if ((p = fgets(buf, sizeof(buf), hostf)) == NULL) {
115 h_errno = HOST_NOT_FOUND;
120 if ((cp = strpbrk(p, "#\n")) == NULL)
123 if ((cp = strpbrk(p, " \t")) == NULL)
126 if (bt_aton(p, &host_addr) == 0)
128 host_addr_ptrs[0] = (char *) &host_addr;
129 host_addr_ptrs[1] = NULL;
130 host.h_addr_list = host_addr_ptrs;
131 host.h_length = sizeof(host_addr);
132 host.h_addrtype = AF_BLUETOOTH;
133 while (*cp == ' ' || *cp == '\t')
136 q = host.h_aliases = host_aliases;
137 if ((cp = strpbrk(cp, " \t")) != NULL)
139 while (cp != NULL && *cp != 0) {
140 if (*cp == ' ' || *cp == '\t') {
144 if (q < &host_aliases[MAXALIASES - 1])
146 if ((cp = strpbrk(cp, " \t")) != NULL)
150 h_errno = NETDB_SUCCESS;
156 bt_sethostent(int stayopen)
159 hostf = fopen(_PATH_BT_HOSTS, "r");
163 host_stayopen = stayopen;
169 if (hostf != NULL && host_stayopen == 0) {
170 (void) fclose(hostf);
176 bt_getprotobyname(char const *name)
181 bt_setprotoent(proto_stayopen);
182 while ((p = bt_getprotoent()) != NULL) {
183 if (strcmp(p->p_name, name) == 0)
185 for (cp = p->p_aliases; *cp != NULL; cp++)
186 if (strcmp(*cp, name) == 0)
196 bt_getprotobynumber(int proto)
200 bt_setprotoent(proto_stayopen);
201 while ((p = bt_getprotoent()) != NULL)
202 if (p->p_proto == proto)
215 protof = fopen(_PATH_BT_PROTOCOLS, "r");
220 if ((p = fgets(buf, sizeof(buf), protof)) == NULL)
224 if ((cp = strpbrk(p, "#\n")) == NULL)
228 if ((cp = strpbrk(p, " \t")) == NULL)
231 while (*cp == ' ' || *cp == '\t')
233 if ((p = strpbrk(cp, " \t")) != NULL)
235 proto.p_proto = atoi(cp);
236 q = proto.p_aliases = proto_aliases;
239 while (cp != NULL && *cp != 0) {
240 if (*cp == ' ' || *cp == '\t') {
244 if (q < &proto_aliases[MAXALIASES - 1])
246 if ((cp = strpbrk(cp, " \t")) != NULL)
256 bt_setprotoent(int stayopen)
259 protof = fopen(_PATH_BT_PROTOCOLS, "r");
263 proto_stayopen = stayopen;
269 if (protof != NULL) {
270 (void) fclose(protof);
276 bt_ntoa(bdaddr_t const *ba, char *str)
278 static char buffer[24];
283 sprintf(str, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
284 ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);
290 bt_aton(char const *str, bdaddr_t *ba)
295 memset(ba, 0, sizeof(*ba));
297 for (i = 5, end = strchr(str, ':');
298 i > 0 && *str != '\0' && end != NULL;
299 i --, str = end + 1, end = strchr(str, ':')) {
302 b = bt_hex_nibble(str[0]);
306 b = bt_hex_byte(str);
320 if (i != 0 || end != NULL || *str == 0)
323 switch (strlen(str)) {
325 b = bt_hex_nibble(str[0]);
329 b = bt_hex_byte(str);
346 bt_hex_byte(char const *str)
350 if ((n1 = bt_hex_nibble(str[0])) < 0)
353 if ((n2 = bt_hex_nibble(str[1])) < 0)
356 return ((((n1 & 0x0f) << 4) | (n2 & 0x0f)) & 0xff);
360 bt_hex_nibble(char nibble)
362 if ('0' <= nibble && nibble <= '9')
363 return (nibble - '0');
365 if ('a' <= nibble && nibble <= 'f')
366 return (nibble - 'a' + 0xa);
368 if ('A' <= nibble && nibble <= 'F')
369 return (nibble - 'A' + 0xa);