1*f5ca43cbSplunky /* $NetBSD: client.c,v 1.7 2012/10/14 08:31:35 plunky Exp $ */
21fc74d21Splunky
31fc74d21Splunky /*-
4ab1f45acSplunky * Copyright (c) 2008-2009 Iain Hibbert
51fc74d21Splunky * All rights reserved.
61fc74d21Splunky *
71fc74d21Splunky * Redistribution and use in source and binary forms, with or without
81fc74d21Splunky * modification, are permitted provided that the following conditions
91fc74d21Splunky * are met:
101fc74d21Splunky * 1. Redistributions of source code must retain the above copyright
111fc74d21Splunky * notice, this list of conditions and the following disclaimer.
121fc74d21Splunky * 2. Redistributions in binary form must reproduce the above copyright
131fc74d21Splunky * notice, this list of conditions and the following disclaimer in the
141fc74d21Splunky * documentation and/or other materials provided with the distribution.
151fc74d21Splunky *
161fc74d21Splunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171fc74d21Splunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181fc74d21Splunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191fc74d21Splunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201fc74d21Splunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211fc74d21Splunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221fc74d21Splunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231fc74d21Splunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241fc74d21Splunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251fc74d21Splunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261fc74d21Splunky */
271fc74d21Splunky
281fc74d21Splunky #include <sys/cdefs.h>
29*f5ca43cbSplunky __RCSID("$NetBSD: client.c,v 1.7 2012/10/14 08:31:35 plunky Exp $");
301fc74d21Splunky
311fc74d21Splunky #include <bluetooth.h>
321fc74d21Splunky #include <errno.h>
331fc74d21Splunky #include <sdp.h>
341fc74d21Splunky #include <unistd.h>
351fc74d21Splunky
361fc74d21Splunky #include "btpand.h"
371fc74d21Splunky #include "bnep.h"
381fc74d21Splunky
39d6c1d36cSjoerg __dead static void client_down(channel_t *);
401fc74d21Splunky static void client_query(void);
411fc74d21Splunky
421fc74d21Splunky void
client_init(void)431fc74d21Splunky client_init(void)
441fc74d21Splunky {
451fc74d21Splunky struct sockaddr_bt sa;
461fc74d21Splunky channel_t *chan;
471fc74d21Splunky socklen_t len;
48*f5ca43cbSplunky int fd, n;
491fc74d21Splunky uint16_t mru, mtu;
501fc74d21Splunky
511fc74d21Splunky if (bdaddr_any(&remote_bdaddr))
521fc74d21Splunky return;
531fc74d21Splunky
54ab1f45acSplunky if (service_type)
551fc74d21Splunky client_query();
561fc74d21Splunky
571fc74d21Splunky fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
581fc74d21Splunky if (fd == -1) {
591fc74d21Splunky log_err("Could not open L2CAP socket: %m");
601fc74d21Splunky exit(EXIT_FAILURE);
611fc74d21Splunky }
621fc74d21Splunky
631fc74d21Splunky memset(&sa, 0, sizeof(sa));
641fc74d21Splunky sa.bt_family = AF_BLUETOOTH;
651fc74d21Splunky sa.bt_len = sizeof(sa);
661fc74d21Splunky bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
671fc74d21Splunky if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
681fc74d21Splunky log_err("Could not bind client socket: %m");
691fc74d21Splunky exit(EXIT_FAILURE);
701fc74d21Splunky }
711fc74d21Splunky
721fc74d21Splunky if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_LM,
731fc74d21Splunky &l2cap_mode, sizeof(l2cap_mode)) == -1) {
741fc74d21Splunky log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
751fc74d21Splunky exit(EXIT_FAILURE);
761fc74d21Splunky }
771fc74d21Splunky
781fc74d21Splunky mru = BNEP_MTU_MIN;
791fc74d21Splunky if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU,
801fc74d21Splunky &mru, sizeof(mru)) == -1) {
811fc74d21Splunky log_err("Could not set L2CAP IMTU (%d): %m", mru);
821fc74d21Splunky exit(EXIT_FAILURE);
831fc74d21Splunky }
841fc74d21Splunky
851fc74d21Splunky log_info("Opening connection to service 0x%4.4x at %s",
861fc74d21Splunky service_class, bt_ntoa(&remote_bdaddr, NULL));
871fc74d21Splunky
881fc74d21Splunky sa.bt_psm = l2cap_psm;
891fc74d21Splunky bdaddr_copy(&sa.bt_bdaddr, &remote_bdaddr);
901fc74d21Splunky if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
911fc74d21Splunky log_err("Could not connect: %m");
921fc74d21Splunky exit(EXIT_FAILURE);
931fc74d21Splunky }
941fc74d21Splunky
951fc74d21Splunky len = sizeof(mru);
961fc74d21Splunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
971fc74d21Splunky log_err("Could not get IMTU: %m");
981fc74d21Splunky exit(EXIT_FAILURE);
991fc74d21Splunky }
1001fc74d21Splunky if (mru < BNEP_MTU_MIN) {
1011fc74d21Splunky log_err("L2CAP IMTU too small (%d)", mru);
1021fc74d21Splunky exit(EXIT_FAILURE);
1031fc74d21Splunky }
1041fc74d21Splunky
105*f5ca43cbSplunky len = sizeof(n);
106*f5ca43cbSplunky if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
107585aadf0Splunky log_err("Could not read SO_RCVBUF");
108585aadf0Splunky exit(EXIT_FAILURE);
109585aadf0Splunky }
110*f5ca43cbSplunky if (n < 10 * mru) {
111*f5ca43cbSplunky n = 10 * mru;
112*f5ca43cbSplunky if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
113*f5ca43cbSplunky log_info("Could not increase SO_RCVBUF (to %d)", n);
114585aadf0Splunky }
115585aadf0Splunky
1161fc74d21Splunky len = sizeof(mtu);
1171fc74d21Splunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
1181fc74d21Splunky log_err("Could not get L2CAP OMTU: %m");
1191fc74d21Splunky exit(EXIT_FAILURE);
1201fc74d21Splunky }
1211fc74d21Splunky if (mtu < BNEP_MTU_MIN) {
1221fc74d21Splunky log_err("L2CAP OMTU too small (%d)", mtu);
1231fc74d21Splunky exit(EXIT_FAILURE);
1241fc74d21Splunky }
1251fc74d21Splunky
126*f5ca43cbSplunky len = sizeof(n);
127*f5ca43cbSplunky if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
128*f5ca43cbSplunky log_err("Could not get socket send buffer size: %m");
129*f5ca43cbSplunky close(fd);
130*f5ca43cbSplunky return;
131*f5ca43cbSplunky }
132*f5ca43cbSplunky if (n < (mtu * 2)) {
133*f5ca43cbSplunky n = mtu * 2;
134*f5ca43cbSplunky if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
135*f5ca43cbSplunky log_err("Could not set socket send buffer size (%d): %m", n);
136*f5ca43cbSplunky close(fd);
137*f5ca43cbSplunky return;
138*f5ca43cbSplunky }
139*f5ca43cbSplunky }
140*f5ca43cbSplunky n = mtu;
141*f5ca43cbSplunky if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
142*f5ca43cbSplunky log_err("Could not set socket low water mark (%d): %m", n);
143*f5ca43cbSplunky close(fd);
144*f5ca43cbSplunky return;
145*f5ca43cbSplunky }
146*f5ca43cbSplunky
1471fc74d21Splunky chan = channel_alloc();
1481fc74d21Splunky if (chan == NULL)
1491fc74d21Splunky exit(EXIT_FAILURE);
1501fc74d21Splunky
1511fc74d21Splunky chan->send = bnep_send;
1521fc74d21Splunky chan->recv = bnep_recv;
153493f204dSplunky chan->down = client_down;
1541fc74d21Splunky chan->mru = mru;
1551fc74d21Splunky chan->mtu = mtu;
1561fc74d21Splunky b2eaddr(chan->raddr, &remote_bdaddr);
1571fc74d21Splunky b2eaddr(chan->laddr, &local_bdaddr);
1581fc74d21Splunky chan->state = CHANNEL_WAIT_CONNECT_RSP;
1591fc74d21Splunky channel_timeout(chan, 10);
1601fc74d21Splunky if (!channel_open(chan, fd))
1611fc74d21Splunky exit(EXIT_FAILURE);
1621fc74d21Splunky
1631fc74d21Splunky bnep_send_control(chan, BNEP_SETUP_CONNECTION_REQUEST,
1641fc74d21Splunky 2, service_class, SDP_SERVICE_CLASS_PANU);
1651fc74d21Splunky }
1661fc74d21Splunky
1671fc74d21Splunky static void
client_down(channel_t * chan)168493f204dSplunky client_down(channel_t *chan)
169493f204dSplunky {
170493f204dSplunky
171493f204dSplunky log_err("Client connection shut down, exiting");
172493f204dSplunky exit(EXIT_FAILURE);
173493f204dSplunky }
174493f204dSplunky
175493f204dSplunky static void
client_query(void)1761fc74d21Splunky client_query(void)
1771fc74d21Splunky {
178ab1f45acSplunky uint8_t buf[12]; /* enough for SSP and AIL both */
179ab1f45acSplunky sdp_session_t ss;
180ab1f45acSplunky sdp_data_t ssp, ail, rsp, rec, value, pdl, seq;
181ab1f45acSplunky uintmax_t psm;
182ab1f45acSplunky uint16_t attr;
183ab1f45acSplunky bool rv;
1841fc74d21Splunky
1851fc74d21Splunky ss = sdp_open(&local_bdaddr, &remote_bdaddr);
186ab1f45acSplunky if (ss == NULL) {
187ab1f45acSplunky log_err("%s: %m", service_type);
1881fc74d21Splunky exit(EXIT_FAILURE);
1891fc74d21Splunky }
1901fc74d21Splunky
1911fc74d21Splunky log_info("Searching for %s service at %s",
192ab1f45acSplunky service_type, bt_ntoa(&remote_bdaddr, NULL));
1931fc74d21Splunky
194ab1f45acSplunky seq.next = buf;
195ab1f45acSplunky seq.end = buf + sizeof(buf);
1961fc74d21Splunky
197ab1f45acSplunky /*
198ab1f45acSplunky * build ServiceSearchPattern (9 bytes)
199ab1f45acSplunky *
200ab1f45acSplunky * uuid16 "service_class"
201ab1f45acSplunky * uuid16 L2CAP
202ab1f45acSplunky * uuid16 BNEP
203ab1f45acSplunky */
204ab1f45acSplunky ssp.next = seq.next;
205ab1f45acSplunky sdp_put_uuid16(&seq, service_class);
206ab1f45acSplunky sdp_put_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP);
207ab1f45acSplunky sdp_put_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP);
208ab1f45acSplunky ssp.end = seq.next;
2091fc74d21Splunky
210ab1f45acSplunky /*
211ab1f45acSplunky * build AttributeIDList (3 bytes)
212ab1f45acSplunky *
213ab1f45acSplunky * uint16 ProtocolDescriptorList
214ab1f45acSplunky */
215ab1f45acSplunky ail.next = seq.next;
216ab1f45acSplunky sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
217ab1f45acSplunky ail.end = seq.next;
218ab1f45acSplunky
219ab1f45acSplunky rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp);
220ab1f45acSplunky if (!rv) {
221ab1f45acSplunky log_err("%s: %m", service_type);
2221fc74d21Splunky exit(EXIT_FAILURE);
2231fc74d21Splunky }
2241fc74d21Splunky
2251fc74d21Splunky /*
226ab1f45acSplunky * we expect the response to contain a list of records
227ab1f45acSplunky * containing a ProtocolDescriptorList. Find the first
228ab1f45acSplunky * one containing L2CAP and BNEP protocols and extract
229ab1f45acSplunky * the PSM.
2301fc74d21Splunky */
231ab1f45acSplunky rv = false;
232ab1f45acSplunky while (!rv && sdp_get_seq(&rsp, &rec)) {
233ab1f45acSplunky if (!sdp_get_attr(&rec, &attr, &value)
234ab1f45acSplunky || attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
235ab1f45acSplunky continue;
236ab1f45acSplunky
237ab1f45acSplunky sdp_get_alt(&value, &value); /* drop any alt header */
238ab1f45acSplunky while (!rv && sdp_get_seq(&value, &pdl)) {
239ab1f45acSplunky if (sdp_get_seq(&pdl, &seq)
240ab1f45acSplunky && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
241ab1f45acSplunky && sdp_get_uint(&seq, &psm)
242ab1f45acSplunky && sdp_get_seq(&pdl, &seq)
243ab1f45acSplunky && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP))
244ab1f45acSplunky rv = true;
245ab1f45acSplunky }
2461fc74d21Splunky }
2471fc74d21Splunky
248ab1f45acSplunky sdp_close(ss);
249ab1f45acSplunky
250ab1f45acSplunky if (!rv) {
251ab1f45acSplunky log_err("%s query failed", service_type);
2521fc74d21Splunky exit(EXIT_FAILURE);
2531fc74d21Splunky }
254ab1f45acSplunky
255ab1f45acSplunky l2cap_psm = (uint16_t)psm;
256ab1f45acSplunky log_info("Found PSM %u for service %s", l2cap_psm, service_type);
257ab1f45acSplunky }
258