1 /* $NetBSD: bluetooth.c,v 1.1 2006/06/19 15:44:36 gdamore Exp $ */
2 /* $DragonFly: src/lib/libbluetooth/bluetooth.c,v 1.1 2008/01/03 11:47:52 hasso Exp $ */
7 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $Id: bluetooth.c,v 1.1 2006/06/19 15:44:36 gdamore Exp $
32 * $FreeBSD: src/lib/libbluetooth/bluetooth.c,v 1.2 2004/03/05 08:10:17 markm Exp $
35 #include <bluetooth.h>
40 #define _PATH_BT_HOSTS "/etc/bluetooth/hosts"
41 #define _PATH_BT_PROTOCOLS "/etc/bluetooth/protocols"
44 static FILE *hostf
= NULL
;
45 static int host_stayopen
= 0;
46 static struct hostent host
;
47 static bdaddr_t host_addr
;
48 static char *host_addr_ptrs
[2];
49 static char *host_aliases
[MAXALIASES
];
51 static FILE *protof
= NULL
;
52 static int proto_stayopen
= 0;
53 static struct protoent proto
;
54 static char *proto_aliases
[MAXALIASES
];
56 static char buf
[BUFSIZ
+ 1];
58 static int bt_hex_byte (char const *str
);
59 static int bt_hex_nibble (char nibble
);
62 bt_gethostbyname(char const *name
)
67 bt_sethostent(host_stayopen
);
68 while ((p
= bt_gethostent()) != NULL
) {
69 if (strcasecmp(p
->h_name
, name
) == 0)
71 for (cp
= p
->h_aliases
; *cp
!= 0; cp
++)
72 if (strcasecmp(*cp
, name
) == 0)
82 bt_gethostbyaddr(void const *addr
, socklen_t len
, int type
)
86 if (type
!= AF_BLUETOOTH
|| len
!= sizeof(bdaddr_t
)) {
87 h_errno
= NO_RECOVERY
;
91 bt_sethostent(host_stayopen
);
92 while ((p
= bt_gethostent()) != NULL
)
93 if (p
->h_addrtype
== type
&& memcmp(p
->h_addr
, addr
, len
) == 0)
106 hostf
= fopen(_PATH_BT_HOSTS
, "r");
109 h_errno
= NETDB_INTERNAL
;
113 if ((p
= fgets(buf
, sizeof(buf
), hostf
)) == NULL
) {
114 h_errno
= HOST_NOT_FOUND
;
119 if ((cp
= strpbrk(p
, "#\n")) == NULL
)
122 if ((cp
= strpbrk(p
, " \t")) == NULL
)
125 if (bt_aton(p
, &host_addr
) == 0)
127 host_addr_ptrs
[0] = (char *) &host_addr
;
128 host_addr_ptrs
[1] = NULL
;
129 host
.h_addr_list
= host_addr_ptrs
;
130 host
.h_length
= sizeof(host_addr
);
131 host
.h_addrtype
= AF_BLUETOOTH
;
132 while (*cp
== ' ' || *cp
== '\t')
135 q
= host
.h_aliases
= host_aliases
;
136 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
138 while (cp
!= NULL
&& *cp
!= 0) {
139 if (*cp
== ' ' || *cp
== '\t') {
143 if (q
< &host_aliases
[MAXALIASES
- 1])
145 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
149 h_errno
= NETDB_SUCCESS
;
155 bt_sethostent(int stayopen
)
158 hostf
= fopen(_PATH_BT_HOSTS
, "r");
162 host_stayopen
= stayopen
;
168 if (hostf
!= NULL
&& host_stayopen
== 0) {
169 (void) fclose(hostf
);
175 bt_getprotobyname(char const *name
)
180 bt_setprotoent(proto_stayopen
);
181 while ((p
= bt_getprotoent()) != NULL
) {
182 if (strcmp(p
->p_name
, name
) == 0)
184 for (cp
= p
->p_aliases
; *cp
!= 0; cp
++)
185 if (strcmp(*cp
, name
) == 0)
195 bt_getprotobynumber(int num
)
199 bt_setprotoent(proto_stayopen
);
200 while ((p
= bt_getprotoent()) != NULL
)
201 if (p
->p_proto
== num
)
214 protof
= fopen(_PATH_BT_PROTOCOLS
, "r");
219 if ((p
= fgets(buf
, sizeof(buf
), protof
)) == NULL
)
223 if ((cp
= strpbrk(p
, "#\n")) == NULL
)
227 if ((cp
= strpbrk(p
, " \t")) == NULL
)
230 while (*cp
== ' ' || *cp
== '\t')
232 if ((p
= strpbrk(cp
, " \t")) != NULL
)
234 proto
.p_proto
= (int)strtol(cp
, NULL
, 0);
235 q
= proto
.p_aliases
= proto_aliases
;
238 while (cp
!= NULL
&& *cp
!= 0) {
239 if (*cp
== ' ' || *cp
== '\t') {
243 if (q
< &proto_aliases
[MAXALIASES
- 1])
245 if ((cp
= strpbrk(cp
, " \t")) != NULL
)
255 bt_setprotoent(int stayopen
)
258 protof
= fopen(_PATH_BT_PROTOCOLS
, "r");
262 proto_stayopen
= stayopen
;
268 if (protof
!= NULL
) {
269 (void) fclose(protof
);
275 bt_ntoa(bdaddr_t
const *ba
, char *str
)
277 static char buffer
[24];
282 sprintf(str
, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
283 ba
->b
[5], ba
->b
[4], ba
->b
[3], ba
->b
[2], ba
->b
[1], ba
->b
[0]);
289 bt_aton(char const *str
, bdaddr_t
*ba
)
294 memset(ba
, 0, sizeof(*ba
));
296 for (i
= 5, end
= strchr(str
, ':');
297 i
> 0 && *str
!= '\0' && end
!= NULL
;
298 i
--, str
= end
+ 1, end
= strchr(str
, ':')) {
301 b
= bt_hex_nibble(str
[0]);
305 b
= bt_hex_byte(str
);
319 if (i
!= 0 || end
!= NULL
|| *str
== 0)
322 switch (strlen(str
)) {
324 b
= bt_hex_nibble(str
[0]);
328 b
= bt_hex_byte(str
);
345 bt_hex_byte(char const *str
)
349 if ((n1
= bt_hex_nibble(str
[0])) < 0)
352 if ((n2
= bt_hex_nibble(str
[1])) < 0)
355 return ((((n1
& 0x0f) << 4) | (n2
& 0x0f)) & 0xff);
359 bt_hex_nibble(char nibble
)
361 if ('0' <= nibble
&& nibble
<= '9')
362 return (nibble
- '0');
364 if ('a' <= nibble
&& nibble
<= 'f')
365 return (nibble
- 'a' + 0xa);
367 if ('A' <= nibble
&& nibble
<= 'F')
368 return (nibble
- 'A' + 0xa);