update rfcomm_sppd to use the new Service Discovery API

This commit is contained in:
plunky 2009-05-12 18:43:35 +00:00
parent 1b7404b7aa
commit a092292999
4 changed files with 207 additions and 404 deletions

View File

@ -1,13 +1,11 @@
# $NetBSD: Makefile,v 1.4 2009/05/12 13:13:34 plunky Exp $ # $NetBSD: Makefile,v 1.5 2009/05/12 18:43:35 plunky Exp $
USE_FORT?= yes # network client/server USE_FORT?= yes # network client/server
PROG= rfcomm_sppd PROG= rfcomm_sppd
SRCS= rfcomm_sppd.c rfcomm_sdp.c SRCS= rfcomm_sppd.c
DPADD+= ${LIBBLUETOOTH} DPADD+= ${LIBBLUETOOTH}
LDADD+= -lbluetooth LDADD+= -lbluetooth
CPPFLAGS+= -DSDP_COMPAT
.include <bsd.prog.mk> .include <bsd.prog.mk>

View File

@ -1,300 +0,0 @@
/* $NetBSD: rfcomm_sdp.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $ */
/*-
* Copyright (c) 2006 Itronix Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of Itronix Inc. may not be used to endorse
* or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* rfcomm_sdp.c
*
* Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: rfcomm_sdp.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $
* $FreeBSD: src/usr.bin/bluetooth/rfcomm_sppd/rfcomm_sdp.c,v 1.2 2004/04/09 23:26:16 emax Exp $
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: rfcomm_sdp.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $");
#include <bluetooth.h>
#include <errno.h>
#include <sdp.h>
#include <stdio.h>
#include "rfcomm_sdp.h"
#undef PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE
#define PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE 256
#undef PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE
#define PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE 12
static int rfcomm_proto_list_parse (uint8_t const *start, uint8_t const *end,
uint8_t *channel, int *error);
/*
* Lookup RFCOMM channel number in the Protocol Descriptor List
*/
#undef rfcomm_channel_lookup_exit
#define rfcomm_channel_lookup_exit(e) { \
if (error != NULL) \
*error = (e); \
if (ss != NULL) { \
sdp_close(ss); \
ss = NULL; \
} \
return (((e) == 0)? 0 : -1); \
}
int
rfcomm_channel_lookup(bdaddr_t const *local, bdaddr_t const *remote,
int service, uint8_t *channel, int *error)
{
uint8_t buffer[PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE];
void *ss = NULL;
uint16_t serv = (uint16_t) service;
uint32_t attr = SDP_ATTR_RANGE(
SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
sdp_attr_t proto = { SDP_ATTR_INVALID,0,sizeof(buffer),buffer };
uint32_t type, len;
if (local == NULL)
local = BDADDR_ANY;
if (remote == NULL || channel == NULL)
rfcomm_channel_lookup_exit(EINVAL);
if ((ss = sdp_open(local, remote)) == NULL)
rfcomm_channel_lookup_exit(ENOMEM);
if (sdp_error(ss) != 0)
rfcomm_channel_lookup_exit(sdp_error(ss));
if (sdp_search(ss, 1, &serv, 1, &attr, 1, &proto) != 0)
rfcomm_channel_lookup_exit(sdp_error(ss));
if (proto.flags != SDP_ATTR_OK)
rfcomm_channel_lookup_exit(ENOATTR);
sdp_close(ss);
ss = NULL;
/*
* If it is possible for more than one kind of protocol stack to be
* used to gain access to the service, the ProtocolDescriptorList
* takes the form of a data element alternative. We always use the
* first protocol stack.
*
* A minimal Protocol Descriptor List for RFCOMM based service would
* look like
*
* seq8 len8 - 2 bytes
* seq8 len8 - 2 bytes
* uuid16 value16 - 3 bytes L2CAP
* seq8 len8 - 2 bytes
* uuid16 value16 - 3 bytes RFCOMM
* uint8 value8 - 2 bytes RFCOMM param #1
* =========
* 14 bytes
*
* Lets not count first [seq8 len8] wrapper, so the minimal size of
* the Protocol Descriptor List (the data we are actually interested
* in) for RFCOMM based service would be 12 bytes.
*/
if (proto.vlen < PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE)
rfcomm_channel_lookup_exit(EINVAL);
SDP_GET8(type, proto.value);
if (type == SDP_DATA_ALT8) {
SDP_GET8(len, proto.value);
} else if (type == SDP_DATA_ALT16) {
SDP_GET16(len, proto.value);
} else if (type == SDP_DATA_ALT32) {
SDP_GET32(len, proto.value);
} else
len = 0;
if (len > 0)
SDP_GET8(type, proto.value);
switch (type) {
case SDP_DATA_SEQ8:
SDP_GET8(len, proto.value);
break;
case SDP_DATA_SEQ16:
SDP_GET16(len, proto.value);
break;
case SDP_DATA_SEQ32:
SDP_GET32(len, proto.value);
break;
default:
rfcomm_channel_lookup_exit(ENOATTR);
/* NOT REACHED */
}
if (len < PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE)
rfcomm_channel_lookup_exit(EINVAL);
return (rfcomm_proto_list_parse(proto.value,
buffer + proto.vlen, channel, error));
}
/*
* Parse protocol descriptor list
*
* The ProtocolDescriptorList attribute describes one or more protocol
* stacks that may be used to gain access to the service described by
* the service record. If the ProtocolDescriptorList describes a single
* stack, it takes the form of a data element sequence in which each
* element of the sequence is a protocol descriptor.
*/
#undef rfcomm_proto_list_parse_exit
#define rfcomm_proto_list_parse_exit(e) { \
if (error != NULL) \
*error = (e); \
return (((e) == 0)? 0 : -1); \
}
static int
rfcomm_proto_list_parse(uint8_t const *start, uint8_t const *end,
uint8_t *channel, int *error)
{
int type, len, value;
while (start < end) {
/*
* Parse protocol descriptor
*
* A protocol descriptor identifies a communications protocol
* and provides protocol specific parameters. A protocol
* descriptor is represented as a data element sequence. The
* first data element in the sequence must be the UUID that
* identifies the protocol. Additional data elements optionally
* provide protocol specific information, such as the L2CAP
* protocol/service multiplexer (PSM) and the RFCOMM server
* channel number (CN).
*/
/* We must have at least one byte (type) */
if (end - start < 1)
rfcomm_proto_list_parse_exit(EINVAL)
SDP_GET8(type, start);
switch (type) {
case SDP_DATA_SEQ8:
SDP_GET8(len, start);
break;
case SDP_DATA_SEQ16:
SDP_GET16(len, start);
break;
case SDP_DATA_SEQ32:
SDP_GET32(len, start);
break;
default:
rfcomm_proto_list_parse_exit(ENOATTR)
/* NOT REACHED */
}
/* We must have at least 3 bytes (type + UUID16) */
if (end - start < 3)
rfcomm_proto_list_parse_exit(EINVAL);
/* Get protocol UUID */
SDP_GET8(type, start); len -= sizeof(uint8_t);
switch (type) {
case SDP_DATA_UUID16:
SDP_GET16(value, start); len -= sizeof(uint16_t);
if (value != SDP_UUID_PROTOCOL_RFCOMM)
goto next_protocol;
break;
case SDP_DATA_UUID32: /* XXX FIXME can we have 32-bit UUID */
case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
default:
rfcomm_proto_list_parse_exit(ENOATTR);
/* NOT REACHED */
}
/*
* First protocol specific parameter for RFCOMM procotol must
* be uint8 that represents RFCOMM channel number. So we must
* have at least two bytes.
*/
if (end - start < 2)
rfcomm_proto_list_parse_exit(EINVAL);
SDP_GET8(type, start);
if (type != SDP_DATA_UINT8)
rfcomm_proto_list_parse_exit(ENOATTR);
SDP_GET8(*channel, start);
rfcomm_proto_list_parse_exit(0);
/* NOT REACHED */
next_protocol:
start += len;
}
/*
* If we got here then it means we could not find RFCOMM protocol
* descriptor, but the reply format was actually valid.
*/
rfcomm_proto_list_parse_exit(ENOATTR);
}

View File

@ -1,39 +0,0 @@
/* $NetBSD: rfcomm_sdp.h,v 1.1 2006/06/19 15:44:56 gdamore Exp $ */
/*-
* Copyright (c) 2006 Itronix Inc.
* All rights reserved.
*
* Written by Iain Hibbert for Itronix Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of Itronix Inc. may not be used to endorse
* or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _RFCOMM_SDP_H_
#define _RFCOMM_SDP_H_
int rfcomm_channel_lookup(bdaddr_t const *, bdaddr_t const *, int, uint8_t *, int *);
#endif /* _RFCOMM_SDP_H */

View File

@ -1,4 +1,4 @@
/* $NetBSD: rfcomm_sppd.c,v 1.9 2008/07/21 14:19:25 lukem Exp $ */ /* $NetBSD: rfcomm_sppd.c,v 1.10 2009/05/12 18:43:35 plunky Exp $ */
/*- /*-
* Copyright (c) 2006 Itronix Inc. * Copyright (c) 2006 Itronix Inc.
@ -29,8 +29,7 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
/* /*
* rfcomm_sppd.c * Copyright (c) 2009 The NetBSD Foundation, Inc.
*
* Copyright (c) 2007 Iain Hibbert * Copyright (c) 2007 Iain Hibbert
* Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com> * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
* All rights reserved. * All rights reserved.
@ -58,11 +57,12 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2007 Iain Hibbert.\ __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc.\
Copyright (c) 2007 Iain Hibbert.\
Copyright (c) 2006 Itronix, Inc.\ Copyright (c) 2006 Itronix, Inc.\
Copyright (c) 2003 Maksim Yevmenkin m_evmenkin@yahoo.com.\ Copyright (c) 2003 Maksim Yevmenkin m_evmenkin@yahoo.com.\
All rights reserved."); All rights reserved.");
__RCSID("$NetBSD: rfcomm_sppd.c,v 1.9 2008/07/21 14:19:25 lukem Exp $"); __RCSID("$NetBSD: rfcomm_sppd.c,v 1.10 2009/05/12 18:43:35 plunky Exp $");
#include <bluetooth.h> #include <bluetooth.h>
#include <ctype.h> #include <ctype.h>
@ -84,14 +84,13 @@ __RCSID("$NetBSD: rfcomm_sppd.c,v 1.9 2008/07/21 14:19:25 lukem Exp $");
#include <netbt/rfcomm.h> #include <netbt/rfcomm.h>
#include "rfcomm_sdp.h"
#define max(a, b) ((a) > (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b))
int open_tty(const char *); int open_tty(const char *);
int open_client(bdaddr_t *, bdaddr_t *, int, const char *); int open_client(bdaddr_t *, bdaddr_t *, int, const char *);
int open_server(bdaddr_t *, uint8_t, int, const char *); int open_server(bdaddr_t *, uint8_t, int, const char *);
void copy_data(int, int); void copy_data(int, int);
int channel_lookup(const bdaddr_t *, const bdaddr_t *, uint16_t, uintmax_t *);
void sighandler(int); void sighandler(int);
void usage(void); void usage(void);
void reset_tio(void); void reset_tio(void);
@ -100,27 +99,17 @@ int done; /* got a signal */
struct termios tio; /* stored termios for reset on exit */ struct termios tio; /* stored termios for reset on exit */
struct service { struct service {
const char *name; const char * name;
const char *description; const char * description;
uint16_t class; uint16_t class;
int pdulen;
} services[] = { } services[] = {
{ "DUN", "Dialup Networking", { "DUN", "Dialup Networking",
SDP_SERVICE_CLASS_DIALUP_NETWORKING, SDP_SERVICE_CLASS_DIALUP_NETWORKING },
sizeof(struct sdp_dun_profile) { "LAN", "LAN access using PPP",
}, SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP },
{ "LAN", "Lan access using PPP",
SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
sizeof(struct sdp_lan_profile)
},
{ "SP", "Serial Port", { "SP", "Serial Port",
SDP_SERVICE_CLASS_SERIAL_PORT, SDP_SERVICE_CLASS_SERIAL_PORT },
sizeof(struct sdp_sp_profile) { NULL, NULL, 0 }
},
{ NULL, NULL,
0,
0
}
}; };
int int
@ -335,26 +324,30 @@ open_client(bdaddr_t *laddr, bdaddr_t *raddr, int lm, const char *service)
struct service *s; struct service *s;
struct linger l; struct linger l;
char *ep; char *ep;
int fd; int fd, error;
uint8_t channel; uintmax_t channel;
for (s = services ; ; s++) { for (s = services ; ; s++) {
if (s->name == NULL) { if (s->name == NULL) {
channel = strtoul(service, &ep, 10); channel = strtoul(service, &ep, 10);
if (*ep != '\0' || channel < 1 || channel > 30) if (*ep != '\0')
errx(EXIT_FAILURE, "Invalid service: %s", service); errx(EXIT_FAILURE, "Unknown service: %s", service);
break; break;
} }
if (strcasecmp(s->name, service) == 0) { if (strcasecmp(s->name, service) == 0) {
if (rfcomm_channel_lookup(laddr, raddr, s->class, &channel, &errno) < 0) error = channel_lookup(laddr, raddr, s->class, &channel);
err(EXIT_FAILURE, "%s", s->name); if (error != 0)
errx(EXIT_FAILURE, "%s: %s", s->name, strerror(error));
break; break;
} }
} }
if (channel < RFCOMM_CHANNEL_MIN || channel > RFCOMM_CHANNEL_MAX)
errx(EXIT_FAILURE, "Invalid channel %"PRIuMAX, channel);
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
sa.bt_len = sizeof(sa); sa.bt_len = sizeof(sa);
sa.bt_family = AF_BLUETOOTH; sa.bt_family = AF_BLUETOOTH;
@ -380,40 +373,42 @@ open_client(bdaddr_t *laddr, bdaddr_t *raddr, int lm, const char *service)
bdaddr_copy(&sa.bt_bdaddr, raddr); bdaddr_copy(&sa.bt_bdaddr, raddr);
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
err(EXIT_FAILURE, "connect(%s, %d)", bt_ntoa(raddr, NULL), err(EXIT_FAILURE, "connect(%s, %"PRIuMAX")", bt_ntoa(raddr, NULL),
channel); channel);
return fd; return fd;
} }
/*
* In all the profiles we currently support registering, the channel
* is the first octet in the PDU, and it seems all the rest can be
* zero, so we just use an array of uint8_t big enough to store the
* largest, currently LAN. See <sdp.h> for definitions..
*/
#define pdu_len sizeof(struct sdp_lan_profile)
int int
open_server(bdaddr_t *laddr, uint8_t channel, int lm, const char *service) open_server(bdaddr_t *laddr, uint8_t channel, int lm, const char *service)
{ {
uint8_t buffer[256];
struct sockaddr_bt sa; struct sockaddr_bt sa;
struct service *s;
struct linger l; struct linger l;
socklen_t len; socklen_t len;
void *ss; sdp_session_t ss;
int sv, fd, n; sdp_data_t rec;
uint8_t pdu[pdu_len]; int sv, fd;
memset(&sa, 0, sizeof(sa)); for (s = services; ; s++) {
sa.bt_len = sizeof(sa); if (s->name == NULL)
sa.bt_family = AF_BLUETOOTH; usage();
bdaddr_copy(&sa.bt_bdaddr, laddr);
sa.bt_channel = channel;
if (strcasecmp(s->name, service) == 0)
break;
}
/* Open server socket */
sv = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); sv = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (sv < 0) if (sv < 0)
err(EXIT_FAILURE, "socket()"); err(EXIT_FAILURE, "socket()");
memset(&sa, 0, sizeof(sa));
sa.bt_len = sizeof(sa);
sa.bt_family = AF_BLUETOOTH;
sa.bt_channel = channel;
bdaddr_copy(&sa.bt_bdaddr, laddr);
if (bind(sv, (struct sockaddr *)&sa, sizeof(sa)) < 0) if (bind(sv, (struct sockaddr *)&sa, sizeof(sa)) < 0)
err(EXIT_FAILURE, "bind(%s, %d)", bt_ntoa(laddr, NULL), err(EXIT_FAILURE, "bind(%s, %d)", bt_ntoa(laddr, NULL),
channel); channel);
@ -424,28 +419,74 @@ open_server(bdaddr_t *laddr, uint8_t channel, int lm, const char *service)
if (listen(sv, 1) < 0) if (listen(sv, 1) < 0)
err(EXIT_FAILURE, "listen()"); err(EXIT_FAILURE, "listen()");
/* Register service with SDP server */ /* Build SDP record */
for (n = 0 ; ; n++) { rec.next = buffer;
if (services[n].name == NULL) rec.end = buffer + sizeof(buffer);
usage();
if (strcasecmp(services[n].name, service) == 0) sdp_put_uint16(&rec, SDP_ATTR_SERVICE_RECORD_HANDLE);
break; sdp_put_uint32(&rec, 0x00000000);
sdp_put_uint16(&rec, SDP_ATTR_SERVICE_CLASS_ID_LIST);
sdp_put_seq(&rec, 3);
sdp_put_uuid16(&rec, s->class);
sdp_put_uint16(&rec, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
sdp_put_seq(&rec, 12);
sdp_put_seq(&rec, 3);
sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_L2CAP);
sdp_put_seq(&rec, 5);
sdp_put_uuid16(&rec, SDP_UUID_PROTOCOL_RFCOMM);
sdp_put_uint8(&rec, channel);
sdp_put_uint16(&rec, SDP_ATTR_BROWSE_GROUP_LIST);
sdp_put_seq(&rec, 3);
sdp_put_uuid16(&rec, SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
sdp_put_uint16(&rec, SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST);
sdp_put_seq(&rec, 9);
sdp_put_uint16(&rec, 0x656e); /* "en" */
sdp_put_uint16(&rec, 106); /* UTF-8 */
sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID);
if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) {
sdp_put_uint16(&rec, SDP_ATTR_SERVICE_AVAILABILITY);
sdp_put_uint8(&rec, 0x00);
} }
memset(pdu, 0, pdu_len); sdp_put_uint16(&rec, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
pdu[0] = channel; sdp_put_seq(&rec, 8);
sdp_put_seq(&rec, 6);
sdp_put_uuid16(&rec, s->class);
sdp_put_uint16(&rec, 0x0100); /* v1.0 */
sdp_put_uint16(&rec, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
+ SDP_ATTR_SERVICE_NAME_OFFSET);
sdp_put_str(&rec, s->description, -1);
if (s->class == SDP_SERVICE_CLASS_DIALUP_NETWORKING) {
sdp_put_uint16(&rec, SDP_ATTR_AUDIO_FEEDBACK_SUPPORT);
sdp_put_bool(&rec, false);
}
#if 0
if (s->class == SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP) {
sdp_put_uint16(&rec, SDP_ATTR_IP_SUBNET); /* TODO */
sdp_put_str(&rec, "0.0.0.0/0", -1);
}
#endif
rec.end = rec.next;
rec.next = buffer;
/* Register service with SDP server */
ss = sdp_open_local(NULL); ss = sdp_open_local(NULL);
if (ss == NULL || (errno = sdp_error(ss)) != 0) if (ss == NULL)
err(EXIT_FAILURE, "sdp_open_local"); err(EXIT_FAILURE, "sdp_open_local");
if (sdp_register_service(ss, services[n].class, laddr, if (!sdp_record_insert(ss, laddr, NULL, &rec))
pdu, services[n].pdulen, NULL) != 0) { err(EXIT_FAILURE, "sdp_record_insert");
errno = sdp_error(ss);
err(EXIT_FAILURE, "sdp_register_service");
}
/* Accept client connection */
len = sizeof(sa); len = sizeof(sa);
fd = accept(sv, (struct sockaddr *)&sa, &len); fd = accept(sv, (struct sockaddr *)&sa, &len);
if (fd < 0) if (fd < 0)
@ -485,6 +526,109 @@ copy_data(int src, int dst)
} }
} }
int
channel_lookup(bdaddr_t const *laddr, bdaddr_t const *raddr,
uint16_t class, uintmax_t *channel)
{
uint8_t buffer[6]; /* SSP (3 bytes) + AIL (3 bytes) */
sdp_session_t ss;
sdp_data_t ail, ssp, rsp, rec, value, pdl, seq;
uint16_t attr;
bool rv;
seq.next = buffer;
seq.end = buffer + sizeof(buffer);
/*
* build ServiceSearchPattern (3 bytes)
*/
ssp.next = seq.next;
sdp_put_uuid16(&seq, class);
ssp.end = seq.next;
/*
* build AttributeIDList (3 bytes)
*/
ail.next = seq.next;
sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
ail.end = seq.next;
ss = sdp_open(laddr, raddr);
if (ss == NULL)
return errno;
rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp);
if (!rv) {
sdp_close(ss);
return errno;
}
/*
* The response will be a list of records that matched our
* ServiceSearchPattern, where each record is a sequence
* containing a single ProtocolDescriptorList attribute and
* value
*
* seq
* uint16 ProtocolDescriptorList
* value
* seq
* uint16 ProtocolDescriptorList
* value
*
* If the ProtocolDescriptorList describes a single stack,
* the attribute value takes the form of a single Data Element
* Sequence where each member is a protocol descriptor.
*
* seq
* list
*
* If it is possible for more than one kind of protocol
* stack to be used to gain access to the service, the
* ProtocolDescriptorList takes the form of a Data Element
* Alternative where each member is a Data Element Sequence
* describing an alternative protocol stack.
*
* alt
* seq
* list
* seq
* list
*
* Each protocol stack description contains a sequence for each
* protocol, where each sequence contains the protocol UUID as
* the first element, and any ProtocolSpecificParameters. We are
* interested in the RFCOMM channel number, stored as parameter#1.
*
* seq
* uuid L2CAP
* uint16 psm
* seq
* uuid RFCOMM
* uint8 channel
*/
rv = false;
while (!rv && sdp_get_seq(&rsp, &rec)) {
if (!sdp_get_attr(&rec, &attr, &value)
|| attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
continue;
sdp_get_alt(&value, &value); /* strip any alt container */
while (!rv && sdp_get_seq(&value, &pdl)) {
if (sdp_get_seq(&pdl, &seq)
&& sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
&& sdp_get_seq(&pdl, &seq)
&& sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_RFCOMM)
&& sdp_get_uint(&seq, channel))
rv = true;
}
}
sdp_close(ss);
return (rv) ? 0 : ENOATTR;
}
void void
sighandler(int s) sighandler(int s)
{ {