client.c revision 1.6 1 1.6 joerg /* $NetBSD: client.c,v 1.6 2011/08/29 20:38:55 joerg 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.6 joerg __RCSID("$NetBSD: client.c,v 1.6 2011/08/29 20:38:55 joerg 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.5 plunky int fd, bufsize;
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.5 plunky len = sizeof(bufsize);
106 1.5 plunky if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize, &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.5 plunky if (bufsize < 10 * mru) {
111 1.5 plunky bufsize = 10 * mru;
112 1.5 plunky if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize,
113 1.5 plunky sizeof(bufsize)) == -1)
114 1.5 plunky log_info("Could not increase SO_RCVBUF (from %d)",
115 1.5 plunky bufsize);
116 1.5 plunky }
117 1.5 plunky
118 1.1 plunky len = sizeof(mtu);
119 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
120 1.1 plunky log_err("Could not get L2CAP OMTU: %m");
121 1.1 plunky exit(EXIT_FAILURE);
122 1.1 plunky }
123 1.1 plunky if (mtu < BNEP_MTU_MIN) {
124 1.1 plunky log_err("L2CAP OMTU too small (%d)", mtu);
125 1.1 plunky exit(EXIT_FAILURE);
126 1.1 plunky }
127 1.1 plunky
128 1.1 plunky chan = channel_alloc();
129 1.1 plunky if (chan == NULL)
130 1.1 plunky exit(EXIT_FAILURE);
131 1.1 plunky
132 1.1 plunky chan->send = bnep_send;
133 1.1 plunky chan->recv = bnep_recv;
134 1.3 plunky chan->down = client_down;
135 1.1 plunky chan->mru = mru;
136 1.1 plunky chan->mtu = mtu;
137 1.1 plunky b2eaddr(chan->raddr, &remote_bdaddr);
138 1.1 plunky b2eaddr(chan->laddr, &local_bdaddr);
139 1.1 plunky chan->state = CHANNEL_WAIT_CONNECT_RSP;
140 1.1 plunky channel_timeout(chan, 10);
141 1.1 plunky if (!channel_open(chan, fd))
142 1.1 plunky exit(EXIT_FAILURE);
143 1.1 plunky
144 1.1 plunky bnep_send_control(chan, BNEP_SETUP_CONNECTION_REQUEST,
145 1.1 plunky 2, service_class, SDP_SERVICE_CLASS_PANU);
146 1.1 plunky }
147 1.1 plunky
148 1.1 plunky static void
149 1.3 plunky client_down(channel_t *chan)
150 1.3 plunky {
151 1.3 plunky
152 1.3 plunky log_err("Client connection shut down, exiting");
153 1.3 plunky exit(EXIT_FAILURE);
154 1.3 plunky }
155 1.3 plunky
156 1.3 plunky static void
157 1.1 plunky client_query(void)
158 1.1 plunky {
159 1.4 plunky uint8_t buf[12]; /* enough for SSP and AIL both */
160 1.4 plunky sdp_session_t ss;
161 1.4 plunky sdp_data_t ssp, ail, rsp, rec, value, pdl, seq;
162 1.4 plunky uintmax_t psm;
163 1.4 plunky uint16_t attr;
164 1.4 plunky bool rv;
165 1.1 plunky
166 1.1 plunky ss = sdp_open(&local_bdaddr, &remote_bdaddr);
167 1.4 plunky if (ss == NULL) {
168 1.4 plunky log_err("%s: %m", service_type);
169 1.1 plunky exit(EXIT_FAILURE);
170 1.1 plunky }
171 1.1 plunky
172 1.1 plunky log_info("Searching for %s service at %s",
173 1.4 plunky service_type, bt_ntoa(&remote_bdaddr, NULL));
174 1.1 plunky
175 1.4 plunky seq.next = buf;
176 1.4 plunky seq.end = buf + sizeof(buf);
177 1.1 plunky
178 1.4 plunky /*
179 1.4 plunky * build ServiceSearchPattern (9 bytes)
180 1.4 plunky *
181 1.4 plunky * uuid16 "service_class"
182 1.4 plunky * uuid16 L2CAP
183 1.4 plunky * uuid16 BNEP
184 1.4 plunky */
185 1.4 plunky ssp.next = seq.next;
186 1.4 plunky sdp_put_uuid16(&seq, service_class);
187 1.4 plunky sdp_put_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP);
188 1.4 plunky sdp_put_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP);
189 1.4 plunky ssp.end = seq.next;
190 1.1 plunky
191 1.4 plunky /*
192 1.4 plunky * build AttributeIDList (3 bytes)
193 1.4 plunky *
194 1.4 plunky * uint16 ProtocolDescriptorList
195 1.4 plunky */
196 1.4 plunky ail.next = seq.next;
197 1.4 plunky sdp_put_uint16(&seq, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
198 1.4 plunky ail.end = seq.next;
199 1.4 plunky
200 1.4 plunky rv = sdp_service_search_attribute(ss, &ssp, &ail, &rsp);
201 1.4 plunky if (!rv) {
202 1.4 plunky log_err("%s: %m", service_type);
203 1.1 plunky exit(EXIT_FAILURE);
204 1.1 plunky }
205 1.1 plunky
206 1.1 plunky /*
207 1.4 plunky * we expect the response to contain a list of records
208 1.4 plunky * containing a ProtocolDescriptorList. Find the first
209 1.4 plunky * one containing L2CAP and BNEP protocols and extract
210 1.4 plunky * the PSM.
211 1.1 plunky */
212 1.4 plunky rv = false;
213 1.4 plunky while (!rv && sdp_get_seq(&rsp, &rec)) {
214 1.4 plunky if (!sdp_get_attr(&rec, &attr, &value)
215 1.4 plunky || attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST)
216 1.4 plunky continue;
217 1.4 plunky
218 1.4 plunky sdp_get_alt(&value, &value); /* drop any alt header */
219 1.4 plunky while (!rv && sdp_get_seq(&value, &pdl)) {
220 1.4 plunky if (sdp_get_seq(&pdl, &seq)
221 1.4 plunky && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_L2CAP)
222 1.4 plunky && sdp_get_uint(&seq, &psm)
223 1.4 plunky && sdp_get_seq(&pdl, &seq)
224 1.4 plunky && sdp_match_uuid16(&seq, SDP_UUID_PROTOCOL_BNEP))
225 1.4 plunky rv = true;
226 1.4 plunky }
227 1.4 plunky }
228 1.4 plunky
229 1.4 plunky sdp_close(ss);
230 1.4 plunky
231 1.4 plunky if (!rv) {
232 1.4 plunky log_err("%s query failed", service_type);
233 1.4 plunky exit(EXIT_FAILURE);
234 1.1 plunky }
235 1.1 plunky
236 1.4 plunky l2cap_psm = (uint16_t)psm;
237 1.4 plunky log_info("Found PSM %u for service %s", l2cap_psm, service_type);
238 1.1 plunky }
239