client.c revision 1.3 1 1.3 plunky /* $NetBSD: client.c,v 1.3 2009/05/12 21:08:30 plunky Exp $ */
2 1.1 plunky
3 1.1 plunky /*-
4 1.1 plunky * Copyright (c) 2008 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.3 plunky __RCSID("$NetBSD: client.c,v 1.3 2009/05/12 21:08:30 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 #include "sdp.h"
39 1.1 plunky
40 1.3 plunky static void client_down(channel_t *);
41 1.1 plunky static void client_query(void);
42 1.1 plunky
43 1.1 plunky void
44 1.1 plunky client_init(void)
45 1.1 plunky {
46 1.1 plunky struct sockaddr_bt sa;
47 1.1 plunky channel_t *chan;
48 1.1 plunky socklen_t len;
49 1.1 plunky int fd;
50 1.1 plunky uint16_t mru, mtu;
51 1.1 plunky
52 1.1 plunky if (bdaddr_any(&remote_bdaddr))
53 1.1 plunky return;
54 1.1 plunky
55 1.1 plunky if (service_name)
56 1.1 plunky client_query();
57 1.1 plunky
58 1.1 plunky fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
59 1.1 plunky if (fd == -1) {
60 1.1 plunky log_err("Could not open L2CAP socket: %m");
61 1.1 plunky exit(EXIT_FAILURE);
62 1.1 plunky }
63 1.1 plunky
64 1.1 plunky memset(&sa, 0, sizeof(sa));
65 1.1 plunky sa.bt_family = AF_BLUETOOTH;
66 1.1 plunky sa.bt_len = sizeof(sa);
67 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
68 1.1 plunky if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
69 1.1 plunky log_err("Could not bind client socket: %m");
70 1.1 plunky exit(EXIT_FAILURE);
71 1.1 plunky }
72 1.1 plunky
73 1.1 plunky if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_LM,
74 1.1 plunky &l2cap_mode, sizeof(l2cap_mode)) == -1) {
75 1.1 plunky log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
76 1.1 plunky exit(EXIT_FAILURE);
77 1.1 plunky }
78 1.1 plunky
79 1.1 plunky mru = BNEP_MTU_MIN;
80 1.1 plunky if (setsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU,
81 1.1 plunky &mru, sizeof(mru)) == -1) {
82 1.1 plunky log_err("Could not set L2CAP IMTU (%d): %m", mru);
83 1.1 plunky exit(EXIT_FAILURE);
84 1.1 plunky }
85 1.1 plunky
86 1.1 plunky log_info("Opening connection to service 0x%4.4x at %s",
87 1.1 plunky service_class, bt_ntoa(&remote_bdaddr, NULL));
88 1.1 plunky
89 1.1 plunky sa.bt_psm = l2cap_psm;
90 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &remote_bdaddr);
91 1.1 plunky if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
92 1.1 plunky log_err("Could not connect: %m");
93 1.1 plunky exit(EXIT_FAILURE);
94 1.1 plunky }
95 1.1 plunky
96 1.1 plunky len = sizeof(mru);
97 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
98 1.1 plunky log_err("Could not get IMTU: %m");
99 1.1 plunky exit(EXIT_FAILURE);
100 1.1 plunky }
101 1.1 plunky if (mru < BNEP_MTU_MIN) {
102 1.1 plunky log_err("L2CAP IMTU too small (%d)", mru);
103 1.1 plunky exit(EXIT_FAILURE);
104 1.1 plunky }
105 1.1 plunky
106 1.1 plunky len = sizeof(mtu);
107 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
108 1.1 plunky log_err("Could not get L2CAP OMTU: %m");
109 1.1 plunky exit(EXIT_FAILURE);
110 1.1 plunky }
111 1.1 plunky if (mtu < BNEP_MTU_MIN) {
112 1.1 plunky log_err("L2CAP OMTU too small (%d)", mtu);
113 1.1 plunky exit(EXIT_FAILURE);
114 1.1 plunky }
115 1.1 plunky
116 1.1 plunky chan = channel_alloc();
117 1.1 plunky if (chan == NULL)
118 1.1 plunky exit(EXIT_FAILURE);
119 1.1 plunky
120 1.1 plunky chan->send = bnep_send;
121 1.1 plunky chan->recv = bnep_recv;
122 1.3 plunky chan->down = client_down;
123 1.1 plunky chan->mru = mru;
124 1.1 plunky chan->mtu = mtu;
125 1.1 plunky b2eaddr(chan->raddr, &remote_bdaddr);
126 1.1 plunky b2eaddr(chan->laddr, &local_bdaddr);
127 1.1 plunky chan->state = CHANNEL_WAIT_CONNECT_RSP;
128 1.1 plunky channel_timeout(chan, 10);
129 1.1 plunky if (!channel_open(chan, fd))
130 1.1 plunky exit(EXIT_FAILURE);
131 1.1 plunky
132 1.1 plunky bnep_send_control(chan, BNEP_SETUP_CONNECTION_REQUEST,
133 1.1 plunky 2, service_class, SDP_SERVICE_CLASS_PANU);
134 1.1 plunky }
135 1.1 plunky
136 1.1 plunky static void
137 1.3 plunky client_down(channel_t *chan)
138 1.3 plunky {
139 1.3 plunky
140 1.3 plunky log_err("Client connection shut down, exiting");
141 1.3 plunky exit(EXIT_FAILURE);
142 1.3 plunky }
143 1.3 plunky
144 1.3 plunky static void
145 1.1 plunky client_query(void)
146 1.1 plunky {
147 1.1 plunky uint8_t buffer[512];
148 1.1 plunky sdp_attr_t attr;
149 1.1 plunky uint32_t range;
150 1.1 plunky void *ss;
151 1.1 plunky int rv;
152 1.1 plunky uint8_t *seq0, *seq1;
153 1.1 plunky
154 1.1 plunky attr.flags = SDP_ATTR_INVALID;
155 1.1 plunky attr.attr = 0;
156 1.1 plunky attr.vlen = sizeof(buffer);
157 1.1 plunky attr.value = buffer;
158 1.1 plunky
159 1.1 plunky range = SDP_ATTR_RANGE(SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
160 1.1 plunky SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
161 1.1 plunky
162 1.1 plunky ss = sdp_open(&local_bdaddr, &remote_bdaddr);
163 1.1 plunky if (ss == NULL || (errno = sdp_error(ss)) != 0) {
164 1.1 plunky log_err("%s: %m", service_name);
165 1.1 plunky exit(EXIT_FAILURE);
166 1.1 plunky }
167 1.1 plunky
168 1.1 plunky log_info("Searching for %s service at %s",
169 1.1 plunky service_name, bt_ntoa(&remote_bdaddr, NULL));
170 1.1 plunky
171 1.1 plunky rv = sdp_search(ss, 1, &service_class, 1, &range, 1, &attr);
172 1.1 plunky if (rv != 0) {
173 1.1 plunky log_err("%s: %s", service_name, strerror(sdp_error(ss)));
174 1.1 plunky exit(EXIT_FAILURE);
175 1.1 plunky }
176 1.1 plunky
177 1.1 plunky sdp_close(ss);
178 1.1 plunky
179 1.1 plunky if (attr.flags != SDP_ATTR_OK
180 1.1 plunky || attr.attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST) {
181 1.1 plunky log_err("%s service not found", service_name);
182 1.1 plunky exit(EXIT_FAILURE);
183 1.1 plunky }
184 1.1 plunky
185 1.1 plunky /*
186 1.1 plunky * we expect the following protocol descriptor list
187 1.1 plunky *
188 1.1 plunky * seq len
189 1.1 plunky * seq len
190 1.1 plunky * uuid value == L2CAP
191 1.1 plunky * uint16 value16 => PSM
192 1.1 plunky * seq len
193 1.1 plunky * uuid value == BNEP
194 1.1 plunky */
195 1.2 plunky if (_sdp_get_seq(&attr.value, attr.value + attr.vlen, &seq0)
196 1.2 plunky && _sdp_get_seq(&seq0, attr.value, &seq1)
197 1.2 plunky && _sdp_match_uuid16(&seq1, seq0, SDP_UUID_PROTOCOL_L2CAP)
198 1.2 plunky && _sdp_get_uint16(&seq1, seq0, &l2cap_psm)
199 1.2 plunky && _sdp_get_seq(&seq0, attr.value, &seq1)
200 1.2 plunky && _sdp_match_uuid16(&seq1, seq0, SDP_UUID_PROTOCOL_BNEP)) {
201 1.1 plunky log_info("Found PSM %d for service %s", l2cap_psm, service_name);
202 1.1 plunky return;
203 1.1 plunky }
204 1.1 plunky
205 1.1 plunky log_err("%s query failed", service_name);
206 1.1 plunky exit(EXIT_FAILURE);
207 1.1 plunky }
208