client.c revision 1.7 1 1.7 plunky /* $NetBSD: client.c,v 1.7 2012/10/14 08:31:35 plunky Exp $ */
2 1.1 plunky
3 1.1 plunky /*-
4 1.4 plunky * Copyright (c) 2008-2009 Iain Hibbert
5 1.1 plunky * All rights reserved.
6 1.1 plunky *
7 1.1 plunky * Redistribution and use in source and binary forms, with or without
8 1.1 plunky * modification, are permitted provided that the following conditions
9 1.1 plunky * are met:
10 1.1 plunky * 1. Redistributions of source code must retain the above copyright
11 1.1 plunky * notice, this list of conditions and the following disclaimer.
12 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 plunky * notice, this list of conditions and the following disclaimer in the
14 1.1 plunky * documentation and/or other materials provided with the distribution.
15 1.1 plunky *
16 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 plunky */
27 1.1 plunky
28 1.1 plunky #include <sys/cdefs.h>
29 1.7 plunky __RCSID("$NetBSD: client.c,v 1.7 2012/10/14 08:31:35 plunky Exp $");
30 1.1 plunky
31 1.1 plunky #include <bluetooth.h>
32 1.1 plunky #include <errno.h>
33 1.1 plunky #include <sdp.h>
34 1.1 plunky #include <unistd.h>
35 1.1 plunky
36 1.1 plunky #include "btpand.h"
37 1.1 plunky #include "bnep.h"
38 1.1 plunky
39 1.6 joerg __dead static void client_down(channel_t *);
40 1.1 plunky static void client_query(void);
41 1.1 plunky
42 1.1 plunky void
43 1.1 plunky client_init(void)
44 1.1 plunky {
45 1.1 plunky struct sockaddr_bt sa;
46 1.1 plunky channel_t *chan;
47 1.1 plunky socklen_t len;
48 1.7 plunky int fd, n;
49 1.1 plunky uint16_t mru, mtu;
50 1.1 plunky
51 1.1 plunky if (bdaddr_any(&remote_bdaddr))
52 1.1 plunky return;
53 1.1 plunky
54 1.4 plunky if (service_type)
55 1.1 plunky client_query();
56 1.1 plunky
57 1.1 plunky fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
58 1.1 plunky if (fd == -1) {
59 1.1 plunky log_err("Could not open L2CAP socket: %m");
60 1.1 plunky exit(EXIT_FAILURE);
61 1.1 plunky }
62 1.1 plunky
63 1.1 plunky memset(&sa, 0, sizeof(sa));
64 1.1 plunky sa.bt_family = AF_BLUETOOTH;
65 1.1 plunky sa.bt_len = sizeof(sa);
66 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
67 1.1 plunky if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
68 1.1 plunky log_err("Could not bind client socket: %m");
69 1.1 plunky exit(EXIT_FAILURE);
70 1.1 plunky }
71 1.1 plunky
72 1.1 plunky if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_LM,
73 1.1 plunky &l2cap_mode, sizeof(l2cap_mode)) == -1) {
74 1.1 plunky log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
75 1.1 plunky exit(EXIT_FAILURE);
76 1.1 plunky }
77 1.1 plunky
78 1.1 plunky mru = BNEP_MTU_MIN;
79 1.1 plunky if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU,
80 1.1 plunky &mru, sizeof(mru)) == -1) {
81 1.1 plunky log_err("Could not set L2CAP IMTU (%d): %m", mru);
82 1.1 plunky exit(EXIT_FAILURE);
83 1.1 plunky }
84 1.1 plunky
85 1.1 plunky log_info("Opening connection to service 0x%4.4x at %s",
86 1.1 plunky service_class, bt_ntoa(&remote_bdaddr, NULL));
87 1.1 plunky
88 1.1 plunky sa.bt_psm = l2cap_psm;
89 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &remote_bdaddr);
90 1.1 plunky if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
91 1.1 plunky log_err("Could not connect: %m");
92 1.1 plunky exit(EXIT_FAILURE);
93 1.1 plunky }
94 1.1 plunky
95 1.1 plunky len = sizeof(mru);
96 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
97 1.1 plunky log_err("Could not get IMTU: %m");
98 1.1 plunky exit(EXIT_FAILURE);
99 1.1 plunky }
100 1.1 plunky if (mru < BNEP_MTU_MIN) {
101 1.1 plunky log_err("L2CAP IMTU too small (%d)", mru);
102 1.1 plunky exit(EXIT_FAILURE);
103 1.1 plunky }
104 1.1 plunky
105 1.7 plunky len = sizeof(n);
106 1.7 plunky if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
107 1.5 plunky log_err("Could not read SO_RCVBUF");
108 1.5 plunky exit(EXIT_FAILURE);
109 1.5 plunky }
110 1.7 plunky if (n < 10 * mru) {
111 1.7 plunky n = 10 * mru;
112 1.7 plunky if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
113 1.7 plunky log_info("Could not increase SO_RCVBUF (to %d)", n);
114 1.5 plunky }
115 1.5 plunky
116 1.1 plunky len = sizeof(mtu);
117 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
118 1.1 plunky log_err("Could not get L2CAP OMTU: %m");
119 1.1 plunky exit(EXIT_FAILURE);
120 1.1 plunky }
121 1.1 plunky if (mtu < BNEP_MTU_MIN) {
122 1.1 plunky log_err("L2CAP OMTU too small (%d)", mtu);
123 1.1 plunky exit(EXIT_FAILURE);
124 1.1 plunky }
125 1.1 plunky
126 1.7 plunky len = sizeof(n);
127 1.7 plunky if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
128 1.7 plunky log_err("Could not get socket send buffer size: %m");
129 1.7 plunky close(fd);
130 1.7 plunky return;
131 1.7 plunky }
132 1.7 plunky if (n < (mtu * 2)) {
133 1.7 plunky n = mtu * 2;
134 1.7 plunky if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
135 1.7 plunky log_err("Could not set socket send buffer size (%d): %m", n);
136 1.7 plunky close(fd);
137 1.7 plunky return;
138 1.7 plunky }
139 1.7 plunky }
140 1.7 plunky n = mtu;
141 1.7 plunky if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
142 1.7 plunky log_err("Could not set socket low water mark (%d): %m", n);
143 1.7 plunky close(fd);
144 1.7 plunky return;
145 1.7 plunky }
146 1.7 plunky
147 1.1 plunky chan = channel_alloc();
148 1.1 plunky if (chan == NULL)
149 1.1 plunky exit(EXIT_FAILURE);
150 1.1 plunky
151 1.1 plunky chan->send = bnep_send;
152 1.1 plunky chan->recv = bnep_recv;
153 1.3 plunky chan->down = client_down;
154 1.1 plunky chan->mru = mru;
155 1.1 plunky chan->mtu = mtu;
156 1.1 plunky b2eaddr(chan->raddr, &remote_bdaddr);
157 1.1 plunky b2eaddr(chan->laddr, &local_bdaddr);
158 1.1 plunky chan->state = CHANNEL_WAIT_CONNECT_RSP;
159 1.1 plunky channel_timeout(chan, 10);
160 1.1 plunky if (!channel_open(chan, fd))
161 1.1 plunky exit(EXIT_FAILURE);
162 1.1 plunky
163 1.1 plunky bnep_send_control(chan, BNEP_SETUP_CONNECTION_REQUEST,
164 1.1 plunky 2, service_class, SDP_SERVICE_CLASS_PANU);
165 1.1 plunky }
166 1.1 plunky
167 1.1 plunky static void
168 1.3 plunky client_down(channel_t *chan)
169 1.3 plunky {
170 1.3 plunky
171 1.3 plunky log_err("Client connection shut down, exiting");
172 1.3 plunky exit(EXIT_FAILURE);
173 1.3 plunky }
174 1.3 plunky
175 1.3 plunky static void
176 1.1 plunky client_query(void)
177 1.1 plunky {
178 1.4 plunky uint8_t buf[12]; /* enough for SSP and AIL both */
179 1.4 plunky sdp_session_t ss;
180 1.4 plunky sdp_data_t ssp, ail, rsp, rec, value, pdl, seq;
181 1.4 plunky uintmax_t psm;
182 1.4 plunky uint16_t attr;
183 1.4 plunky bool rv;
184 1.1 plunky
185 1.1 plunky ss = sdp_open(&local_bdaddr, &remote_bdaddr);
186 1.4 plunky if (ss == NULL) {
187 1.4 plunky log_err("%s: %m", service_type);
188 1.1 plunky exit(EXIT_FAILURE);
189 1.1 plunky }
190 1.1 plunky
191 1.1 plunky log_info("Searching for %s service at %s",
192 1.4 plunky service_type, bt_ntoa(&remote_bdaddr, NULL));
193 1.1 plunky
194 1.4 plunky seq.next = buf;
195 1.4 plunky seq.end = buf + sizeof(buf);
196 1.1 plunky
197 1.4 plunky /*
198 1.4 plunky * build ServiceSearchPattern (9 bytes)
199 1.4 plunky *
200 1.4 plunky * uuid16 "service_class"
201 1.4 plunky * uuid16 L2CAP
202 1.4 plunky * uuid16 BNEP
203 1.4 plunky */
204 1.4 plunky ssp.next = seq.next;
205 1.4 plunky sdp_put_uuid16(&seq, service_class);
206 1.4 plunky sdp_put_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP);
207 1.4 plunky sdp_put_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP);
208 1.4 plunky ssp.end = seq.next;
209 1.1 plunky
210 1.4 plunky /*
211 1.4 plunky * build AttributeIDList (3 bytes)
212 1.4 plunky *
213 1.4 plunky * uint16 ProtocolDescriptorList
214 1.4 plunky */
215 1.4 plunky ail.next = seq.next;
216 1.4 plunky sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
217 1.4 plunky ail.end = seq.next;
218 1.4 plunky
219 1.4 plunky rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp);
220 1.4 plunky if (!rv) {
221 1.4 plunky log_err("%s: %m", service_type);
222 1.1 plunky exit(EXIT_FAILURE);
223 1.1 plunky }
224 1.1 plunky
225 1.1 plunky /*
226 1.4 plunky * we expect the response to contain a list of records
227 1.4 plunky * containing a ProtocolDescriptorList. Find the first
228 1.4 plunky * one containing L2CAP and BNEP protocols and extract
229 1.4 plunky * the PSM.
230 1.1 plunky */
231 1.4 plunky rv = false;
232 1.4 plunky while (!rv && sdp_get_seq(&rsp, &rec)) {
233 1.4 plunky if (!sdp_get_attr(&rec, &attr, &value)
234 1.4 plunky || attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
235 1.4 plunky continue;
236 1.4 plunky
237 1.4 plunky sdp_get_alt(&value, &value); /* drop any alt header */
238 1.4 plunky while (!rv && sdp_get_seq(&value, &pdl)) {
239 1.4 plunky if (sdp_get_seq(&pdl, &seq)
240 1.4 plunky && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
241 1.4 plunky && sdp_get_uint(&seq, &psm)
242 1.4 plunky && sdp_get_seq(&pdl, &seq)
243 1.4 plunky && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP))
244 1.4 plunky rv = true;
245 1.4 plunky }
246 1.4 plunky }
247 1.4 plunky
248 1.4 plunky sdp_close(ss);
249 1.4 plunky
250 1.4 plunky if (!rv) {
251 1.4 plunky log_err("%s query failed", service_type);
252 1.4 plunky exit(EXIT_FAILURE);
253 1.1 plunky }
254 1.1 plunky
255 1.4 plunky l2cap_psm = (uint16_t)psm;
256 1.4 plunky log_info("Found PSM %u for service %s", l2cap_psm, service_type);
257 1.1 plunky }
258