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