From a09229299982114666bb3f036f78a5c5d40a44e2 Mon Sep 17 00:00:00 2001 From: plunky Date: Tue, 12 May 2009 18:43:35 +0000 Subject: [PATCH] update rfcomm_sppd to use the new Service Discovery API --- usr.bin/rfcomm_sppd/Makefile | 6 +- usr.bin/rfcomm_sppd/rfcomm_sdp.c | 300 ------------------------------ usr.bin/rfcomm_sppd/rfcomm_sdp.h | 39 ---- usr.bin/rfcomm_sppd/rfcomm_sppd.c | 266 ++++++++++++++++++++------ 4 files changed, 207 insertions(+), 404 deletions(-) delete mode 100644 usr.bin/rfcomm_sppd/rfcomm_sdp.c delete mode 100644 usr.bin/rfcomm_sppd/rfcomm_sdp.h diff --git a/usr.bin/rfcomm_sppd/Makefile b/usr.bin/rfcomm_sppd/Makefile index f16149a54f18..92aededd5916 100644 --- a/usr.bin/rfcomm_sppd/Makefile +++ b/usr.bin/rfcomm_sppd/Makefile @@ -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 PROG= rfcomm_sppd -SRCS= rfcomm_sppd.c rfcomm_sdp.c +SRCS= rfcomm_sppd.c DPADD+= ${LIBBLUETOOTH} LDADD+= -lbluetooth -CPPFLAGS+= -DSDP_COMPAT - .include diff --git a/usr.bin/rfcomm_sppd/rfcomm_sdp.c b/usr.bin/rfcomm_sppd/rfcomm_sdp.c deleted file mode 100644 index c4207171931d..000000000000 --- a/usr.bin/rfcomm_sppd/rfcomm_sdp.c +++ /dev/null @@ -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 - * 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 -__RCSID("$NetBSD: rfcomm_sdp.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $"); - -#include -#include -#include -#include - -#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); -} diff --git a/usr.bin/rfcomm_sppd/rfcomm_sdp.h b/usr.bin/rfcomm_sppd/rfcomm_sdp.h deleted file mode 100644 index 0fbaf41b4c48..000000000000 --- a/usr.bin/rfcomm_sppd/rfcomm_sdp.h +++ /dev/null @@ -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 */ diff --git a/usr.bin/rfcomm_sppd/rfcomm_sppd.c b/usr.bin/rfcomm_sppd/rfcomm_sppd.c index 45bc25e582e4..65d48db22f23 100644 --- a/usr.bin/rfcomm_sppd/rfcomm_sppd.c +++ b/usr.bin/rfcomm_sppd/rfcomm_sppd.c @@ -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. @@ -29,8 +29,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ /* - * rfcomm_sppd.c - * + * Copyright (c) 2009 The NetBSD Foundation, Inc. * Copyright (c) 2007 Iain Hibbert * Copyright (c) 2003 Maksim Yevmenkin * All rights reserved. @@ -58,11 +57,12 @@ */ #include -__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) 2003 Maksim Yevmenkin m_evmenkin@yahoo.com.\ 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 #include @@ -84,14 +84,13 @@ __RCSID("$NetBSD: rfcomm_sppd.c,v 1.9 2008/07/21 14:19:25 lukem Exp $"); #include -#include "rfcomm_sdp.h" - #define max(a, b) ((a) > (b) ? (a) : (b)) int open_tty(const char *); int open_client(bdaddr_t *, bdaddr_t *, int, const char *); int open_server(bdaddr_t *, uint8_t, int, const char *); void copy_data(int, int); +int channel_lookup(const bdaddr_t *, const bdaddr_t *, uint16_t, uintmax_t *); void sighandler(int); void usage(void); void reset_tio(void); @@ -100,27 +99,17 @@ int done; /* got a signal */ struct termios tio; /* stored termios for reset on exit */ struct service { - const char *name; - const char *description; + const char * name; + const char * description; uint16_t class; - int pdulen; } services[] = { { "DUN", "Dialup Networking", - SDP_SERVICE_CLASS_DIALUP_NETWORKING, - sizeof(struct sdp_dun_profile) - }, - { "LAN", "Lan access using PPP", - SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP, - sizeof(struct sdp_lan_profile) - }, + SDP_SERVICE_CLASS_DIALUP_NETWORKING }, + { "LAN", "LAN access using PPP", + SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP }, { "SP", "Serial Port", - SDP_SERVICE_CLASS_SERIAL_PORT, - sizeof(struct sdp_sp_profile) - }, - { NULL, NULL, - 0, - 0 - } + SDP_SERVICE_CLASS_SERIAL_PORT }, + { NULL, NULL, 0 } }; int @@ -335,26 +324,30 @@ open_client(bdaddr_t *laddr, bdaddr_t *raddr, int lm, const char *service) struct service *s; struct linger l; char *ep; - int fd; - uint8_t channel; + int fd, error; + uintmax_t channel; for (s = services ; ; s++) { if (s->name == NULL) { channel = strtoul(service, &ep, 10); - if (*ep != '\0' || channel < 1 || channel > 30) - errx(EXIT_FAILURE, "Invalid service: %s", service); + if (*ep != '\0') + errx(EXIT_FAILURE, "Unknown service: %s", service); break; } if (strcasecmp(s->name, service) == 0) { - if (rfcomm_channel_lookup(laddr, raddr, s->class, &channel, &errno) < 0) - err(EXIT_FAILURE, "%s", s->name); + error = channel_lookup(laddr, raddr, s->class, &channel); + if (error != 0) + errx(EXIT_FAILURE, "%s: %s", s->name, strerror(error)); break; } } + if (channel < RFCOMM_CHANNEL_MIN || channel > RFCOMM_CHANNEL_MAX) + errx(EXIT_FAILURE, "Invalid channel %"PRIuMAX, channel); + memset(&sa, 0, sizeof(sa)); sa.bt_len = sizeof(sa); 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); 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); 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 for definitions.. - */ -#define pdu_len sizeof(struct sdp_lan_profile) - int open_server(bdaddr_t *laddr, uint8_t channel, int lm, const char *service) { + uint8_t buffer[256]; struct sockaddr_bt sa; + struct service *s; struct linger l; socklen_t len; - void *ss; - int sv, fd, n; - uint8_t pdu[pdu_len]; + sdp_session_t ss; + sdp_data_t rec; + int sv, fd; - memset(&sa, 0, sizeof(sa)); - sa.bt_len = sizeof(sa); - sa.bt_family = AF_BLUETOOTH; - bdaddr_copy(&sa.bt_bdaddr, laddr); - sa.bt_channel = channel; + for (s = services; ; s++) { + if (s->name == NULL) + usage(); + if (strcasecmp(s->name, service) == 0) + break; + } + + /* Open server socket */ sv = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); if (sv < 0) 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) err(EXIT_FAILURE, "bind(%s, %d)", bt_ntoa(laddr, NULL), channel); @@ -424,28 +419,74 @@ open_server(bdaddr_t *laddr, uint8_t channel, int lm, const char *service) if (listen(sv, 1) < 0) err(EXIT_FAILURE, "listen()"); - /* Register service with SDP server */ - for (n = 0 ; ; n++) { - if (services[n].name == NULL) - usage(); + /* Build SDP record */ + rec.next = buffer; + rec.end = buffer + sizeof(buffer); - if (strcasecmp(services[n].name, service) == 0) - break; + sdp_put_uint16(&rec, SDP_ATTR_SERVICE_RECORD_HANDLE); + 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); - pdu[0] = channel; + sdp_put_uint16(&rec, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); + 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); - if (ss == NULL || (errno = sdp_error(ss)) != 0) + if (ss == NULL) err(EXIT_FAILURE, "sdp_open_local"); - if (sdp_register_service(ss, services[n].class, laddr, - pdu, services[n].pdulen, NULL) != 0) { - errno = sdp_error(ss); - err(EXIT_FAILURE, "sdp_register_service"); - } + if (!sdp_record_insert(ss, laddr, NULL, &rec)) + err(EXIT_FAILURE, "sdp_record_insert"); + /* Accept client connection */ len = sizeof(sa); fd = accept(sv, (struct sockaddr *)&sa, &len); 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 sighandler(int s) {