xref: /freebsd/usr.sbin/bluetooth/btpand/client.c (revision e8e8c939350bdf3c228a411caa9660c607c27a11)
1 /*	$NetBSD: client.c,v 1.2 2008/12/06 20:01:14 plunky Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Iain Hibbert
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /* $FreeBSD$ */
29 
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: client.c,v 1.2 2008/12/06 20:01:14 plunky Exp $");
32 
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
35 #include <errno.h>
36 #include <sdp.h>
37 #include <unistd.h>
38 
39 #include "btpand.h"
40 #include "bnep.h"
41 #include "sdp.h"
42 
43 static void client_query(void);
44 
45 void
46 client_init(void)
47 {
48 	struct sockaddr_l2cap sa;
49 	channel_t *chan;
50 	socklen_t len;
51 	int fd, n;
52 	uint16_t mru, mtu;
53 
54 	if (bdaddr_any(&remote_bdaddr))
55 		return;
56 
57 	if (service_name)
58 		client_query();
59 
60 	fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
61 	if (fd == -1) {
62 		log_err("Could not open L2CAP socket: %m");
63 		exit(EXIT_FAILURE);
64 	}
65 
66 	memset(&sa, 0, sizeof(sa));
67 	sa.l2cap_family = AF_BLUETOOTH;
68 	sa.l2cap_len = sizeof(sa);
69 	sa.l2cap_bdaddr_type = BDADDR_BREDR;
70 	sa.l2cap_cid = 0;
71 
72 	bdaddr_copy(&sa.l2cap_bdaddr, &local_bdaddr);
73 	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
74 		log_err("Could not bind client socket: %m");
75 		exit(EXIT_FAILURE);
76 	}
77 
78 	mru = BNEP_MTU_MIN;
79 	if (setsockopt(fd, SOL_L2CAP, SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
80 		log_err("Could not set L2CAP IMTU (%d): %m", mru);
81 		exit(EXIT_FAILURE);
82 	}
83 
84 	log_info("Opening connection to service 0x%4.4x at %s",
85 	    service_class, bt_ntoa(&remote_bdaddr, NULL));
86 
87 	sa.l2cap_psm = htole16(l2cap_psm);
88 	bdaddr_copy(&sa.l2cap_bdaddr, &remote_bdaddr);
89 	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
90 		log_err("Could not connect: %m");
91 		exit(EXIT_FAILURE);
92 	}
93 
94 	len = sizeof(mru);
95 	if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
96 		log_err("Could not get IMTU: %m");
97 		exit(EXIT_FAILURE);
98 	}
99 	if (mru < BNEP_MTU_MIN) {
100 		log_err("L2CAP IMTU too small (%d)", mru);
101 		exit(EXIT_FAILURE);
102 	}
103 
104 	len = sizeof(n);
105 	if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
106 		log_err("Could not read SO_RCVBUF");
107 		exit(EXIT_FAILURE);
108 	}
109 	if (n < (mru * 10)) {
110 		n = mru * 10;
111 		if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
112 			log_info("Could not increase SO_RCVBUF (from %d)", n);
113 	}
114 
115 	len = sizeof(mtu);
116 	if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
117 		log_err("Could not get L2CAP OMTU: %m");
118 		exit(EXIT_FAILURE);
119 	}
120 	if (mtu < BNEP_MTU_MIN) {
121 		log_err("L2CAP OMTU too small (%d)", mtu);
122 		exit(EXIT_FAILURE);
123 	}
124 
125 	len = sizeof(n);
126 	if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
127 		log_err("Could not get socket send buffer size: %m");
128 		close(fd);
129 		return;
130 	}
131 	if (n < (mtu * 2)) {
132 		n = mtu * 2;
133 		if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
134 			log_err("Could not set socket send buffer size (%d): %m", n);
135 			close(fd);
136 			return;
137 		}
138 	}
139 	n = mtu;
140 	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
141 		log_err("Could not set socket low water mark (%d): %m", n);
142 		close(fd);
143 		return;
144 	}
145 
146 	chan = channel_alloc();
147 	if (chan == NULL)
148 		exit(EXIT_FAILURE);
149 
150 	chan->send = bnep_send;
151 	chan->recv = bnep_recv;
152 	chan->mru = mru;
153 	chan->mtu = mtu;
154 	b2eaddr(chan->raddr, &remote_bdaddr);
155 	b2eaddr(chan->laddr, &local_bdaddr);
156 	chan->state = CHANNEL_WAIT_CONNECT_RSP;
157 	channel_timeout(chan, 10);
158 	if (!channel_open(chan, fd))
159 		exit(EXIT_FAILURE);
160 
161 	bnep_send_control(chan, BNEP_SETUP_CONNECTION_REQUEST,
162 	    2, service_class, SDP_SERVICE_CLASS_PANU);
163 }
164 
165 static void
166 client_query(void)
167 {
168 	uint8_t buffer[512];
169 	sdp_attr_t attr;
170 	uint32_t range;
171 	void *ss;
172 	int rv;
173 	uint8_t *seq0, *seq1;
174 
175 	attr.flags = SDP_ATTR_INVALID;
176 	attr.attr = 0;
177 	attr.vlen = sizeof(buffer);
178 	attr.value = buffer;
179 
180 	range = SDP_ATTR_RANGE(SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
181 			       SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
182 
183 	ss = sdp_open(&local_bdaddr, &remote_bdaddr);
184 	if (ss == NULL || (errno = sdp_error(ss)) != 0) {
185 		log_err("%s: %m", service_name);
186 		exit(EXIT_FAILURE);
187 	}
188 
189 	log_info("Searching for %s service at %s",
190 	    service_name, bt_ntoa(&remote_bdaddr, NULL));
191 
192 	rv = sdp_search(ss, 1, &service_class, 1, &range, 1, &attr);
193 	if (rv != 0) {
194 		log_err("%s: %s", service_name, strerror(sdp_error(ss)));
195 		exit(EXIT_FAILURE);
196 	}
197 
198 	sdp_close(ss);
199 
200 	if (attr.flags != SDP_ATTR_OK
201 	    || attr.attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST) {
202 		log_err("%s service not found", service_name);
203 		exit(EXIT_FAILURE);
204 	}
205 
206 	/*
207 	 * we expect the following protocol descriptor list
208 	 *
209 	 *	seq len
210 	 *	  seq len
211 	 *	    uuid value == L2CAP
212 	 *	    uint16 value16 => PSM
213 	 *	  seq len
214 	 *	    uuid value == BNEP
215 	 */
216 	if (_sdp_get_seq(&attr.value, attr.value + attr.vlen, &seq0)
217 	    && _sdp_get_seq(&seq0, attr.value, &seq1)
218 	    && _sdp_match_uuid16(&seq1, seq0, SDP_UUID_PROTOCOL_L2CAP)
219 	    && _sdp_get_uint16(&seq1, seq0, &l2cap_psm)
220 	    && _sdp_get_seq(&seq0, attr.value, &seq1)
221 	    && _sdp_match_uuid16(&seq1, seq0, SDP_UUID_PROTOCOL_BNEP)) {
222 		log_info("Found PSM %d for service %s", l2cap_psm, service_name);
223 		return;
224 	}
225 
226 	log_err("%s query failed", service_name);
227 	exit(EXIT_FAILURE);
228 }
229