6 * Copyright (c) 2001-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: bluetooth.c,v 1.3 2003/05/20 23:04:30 max Exp $
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
39 #define _PATH_BT_HOSTS "/etc/bluetooth/hosts"
40 #define _PATH_BT_PROTOCOLS "/etc/bluetooth/protocols"
43 static FILE *hostf = NULL;
44 static int host_stayopen = 0;
45 static struct hostent host;
46 static bdaddr_t host_addr;
47 static char *host_addr_ptrs[2];
48 static char *host_aliases[MAXALIASES];
50 static FILE *protof = NULL;
51 static int proto_stayopen = 0;
52 static struct protoent proto;
53 static char *proto_aliases[MAXALIASES];
55 static char buf[BUFSIZ + 1];
57 static int bt_hex_byte (char const *str);
58 static int bt_hex_nibble (char nibble);
61 bt_gethostbyname(char const *name)
66 bt_sethostent(host_stayopen);
67 while ((p = bt_gethostent()) != NULL) {
68 if (strcasecmp(p->h_name, name) == 0)
70 for (cp = p->h_aliases; *cp != NULL; cp++)
71 if (strcasecmp(*cp, name) == 0)
81 bt_gethostbyaddr(char const *addr, int len, int type)
85 if (type != AF_BLUETOOTH || len != sizeof(bdaddr_t)) {
86 h_errno = NO_RECOVERY;
90 bt_sethostent(host_stayopen);
91 while ((p = bt_gethostent()) != NULL)
92 if (p->h_addrtype == type && bcmp(p->h_addr, addr, len) == 0)
105 hostf = fopen(_PATH_BT_HOSTS, "r");
108 h_errno = NETDB_INTERNAL;
112 if ((p = fgets(buf, sizeof(buf), hostf)) == NULL) {
113 h_errno = HOST_NOT_FOUND;
118 if ((cp = strpbrk(p, "#\n")) == NULL)
121 if ((cp = strpbrk(p, " \t")) == NULL)
124 if (bt_aton(p, &host_addr) == 0)
126 host_addr_ptrs[0] = (char *) &host_addr;
127 host_addr_ptrs[1] = NULL;
128 host.h_addr_list = host_addr_ptrs;
129 host.h_length = sizeof(host_addr);
130 host.h_addrtype = AF_BLUETOOTH;
131 while (*cp == ' ' || *cp == '\t')
134 q = host.h_aliases = host_aliases;
135 if ((cp = strpbrk(cp, " \t")) != NULL)
137 while (cp != NULL && *cp != 0) {
138 if (*cp == ' ' || *cp == '\t') {
142 if (q < &host_aliases[MAXALIASES - 1])
144 if ((cp = strpbrk(cp, " \t")) != NULL)
148 h_errno = NETDB_SUCCESS;
154 bt_sethostent(int stayopen)
157 hostf = fopen(_PATH_BT_HOSTS, "r");
161 host_stayopen = stayopen;
167 if (hostf != NULL && host_stayopen == 0) {
168 (void) fclose(hostf);
174 bt_getprotobyname(char const *name)
179 bt_setprotoent(proto_stayopen);
180 while ((p = bt_getprotoent()) != NULL) {
181 if (strcmp(p->p_name, name) == 0)
183 for (cp = p->p_aliases; *cp != NULL; cp++)
184 if (strcmp(*cp, name) == 0)
194 bt_getprotobynumber(int proto)
198 bt_setprotoent(proto_stayopen);
199 while ((p = bt_getprotoent()) != NULL)
200 if (p->p_proto == proto)
213 protof = fopen(_PATH_BT_PROTOCOLS, "r");
218 if ((p = fgets(buf, sizeof(buf), protof)) == NULL)
222 if ((cp = strpbrk(p, "#\n")) == NULL)
226 if ((cp = strpbrk(p, " \t")) == NULL)
229 while (*cp == ' ' || *cp == '\t')
231 if ((p = strpbrk(cp, " \t")) != NULL)
233 proto.p_proto = atoi(cp);
234 q = proto.p_aliases = proto_aliases;
237 while (cp != NULL && *cp != 0) {
238 if (*cp == ' ' || *cp == '\t') {
242 if (q < &proto_aliases[MAXALIASES - 1])
244 if ((cp = strpbrk(cp, " \t")) != NULL)
254 bt_setprotoent(int stayopen)
257 protof = fopen(_PATH_BT_PROTOCOLS, "r");
261 proto_stayopen = stayopen;
267 if (protof != NULL) {
268 (void) fclose(protof);
274 bt_ntoa(bdaddr_t const *ba, char *str)
276 static char buffer[24];
281 sprintf(str, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
282 ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);
288 bt_aton(char const *str, bdaddr_t *ba)
293 memset(ba, 0, sizeof(*ba));
295 for (i = 5, end = strchr(str, ':');
296 i > 0 && *str != '\0' && end != NULL;
297 i --, str = end + 1, end = strchr(str, ':')) {
300 b = bt_hex_nibble(str[0]);
304 b = bt_hex_byte(str);
318 if (i != 0 || end != NULL || *str == 0)
321 switch (strlen(str)) {
323 b = bt_hex_nibble(str[0]);
327 b = bt_hex_byte(str);
344 bt_hex_byte(char const *str)
348 if ((n1 = bt_hex_nibble(str[0])) < 0)
351 if ((n2 = bt_hex_nibble(str[1])) < 0)
354 return ((((n1 & 0x0f) << 4) | (n2 & 0x0f)) & 0xff);
358 bt_hex_nibble(char nibble)
360 if ('0' <= nibble && nibble <= '9')
361 return (nibble - '0');
363 if ('a' <= nibble && nibble <= 'f')
364 return (nibble - 'a' + 0xa);
366 if ('A' <= nibble && nibble <= 'F')
367 return (nibble - 'A' + 0xa);