server.c revision 1.6 1 1.6 plunky /* $NetBSD: server.c,v 1.6 2009/10/25 19:28:45 plunky Exp $ */
2 1.1 plunky
3 1.1 plunky /*-
4 1.3 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 plunky __RCSID("$NetBSD: server.c,v 1.6 2009/10/25 19:28:45 plunky Exp $");
30 1.1 plunky
31 1.1 plunky #include <sys/ioctl.h>
32 1.1 plunky
33 1.4 plunky #include <net/ethertypes.h>
34 1.4 plunky
35 1.1 plunky #include <bluetooth.h>
36 1.1 plunky #include <errno.h>
37 1.1 plunky #include <sdp.h>
38 1.1 plunky #include <unistd.h>
39 1.1 plunky
40 1.1 plunky #include "btpand.h"
41 1.1 plunky #include "bnep.h"
42 1.1 plunky
43 1.1 plunky static struct event server_ev;
44 1.3 plunky static int server_count;
45 1.1 plunky
46 1.4 plunky static sdp_session_t server_ss;
47 1.1 plunky static uint32_t server_handle;
48 1.4 plunky static sdp_data_t server_record;
49 1.4 plunky
50 1.4 plunky static char * server_ipv4_subnet;
51 1.4 plunky static char * server_ipv6_subnet;
52 1.4 plunky static uint16_t server_proto[] = { ETHERTYPE_IP, ETHERTYPE_ARP, ETHERTYPE_IPV6 };
53 1.4 plunky static size_t server_nproto = __arraycount(server_proto);
54 1.1 plunky
55 1.1 plunky static void server_open(void);
56 1.1 plunky static void server_read(int, short, void *);
57 1.3 plunky static void server_down(channel_t *);
58 1.3 plunky static void server_update(void);
59 1.4 plunky static void server_mkrecord(void);
60 1.1 plunky
61 1.1 plunky void
62 1.1 plunky server_init(void)
63 1.1 plunky {
64 1.1 plunky
65 1.1 plunky if (server_limit == 0)
66 1.1 plunky return;
67 1.1 plunky
68 1.3 plunky server_open();
69 1.3 plunky server_update();
70 1.1 plunky }
71 1.1 plunky
72 1.3 plunky /*
73 1.3 plunky * Start listening on server socket
74 1.3 plunky */
75 1.1 plunky static void
76 1.1 plunky server_open(void)
77 1.1 plunky {
78 1.1 plunky struct sockaddr_bt sa;
79 1.6 plunky socklen_t len;
80 1.1 plunky uint16_t mru;
81 1.3 plunky int fd;
82 1.1 plunky
83 1.3 plunky fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
84 1.3 plunky if (fd == -1) {
85 1.1 plunky log_err("Could not open L2CAP socket: %m");
86 1.1 plunky exit(EXIT_FAILURE);
87 1.1 plunky }
88 1.1 plunky
89 1.1 plunky memset(&sa, 0, sizeof(sa));
90 1.1 plunky sa.bt_family = AF_BLUETOOTH;
91 1.1 plunky sa.bt_len = sizeof(sa);
92 1.1 plunky sa.bt_psm = l2cap_psm;
93 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
94 1.3 plunky if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
95 1.1 plunky log_err("Could not bind server socket: %m");
96 1.1 plunky exit(EXIT_FAILURE);
97 1.1 plunky }
98 1.1 plunky
99 1.3 plunky if (setsockopt(fd, BTPROTO_L2CAP,
100 1.1 plunky SO_L2CAP_LM, &l2cap_mode, sizeof(l2cap_mode)) == -1) {
101 1.1 plunky log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
102 1.1 plunky exit(EXIT_FAILURE);
103 1.1 plunky }
104 1.6 plunky len = sizeof(l2cap_mode);
105 1.6 plunky getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_LM, &l2cap_mode, &len);
106 1.1 plunky
107 1.1 plunky mru = BNEP_MTU_MIN;
108 1.3 plunky if (setsockopt(fd, BTPROTO_L2CAP,
109 1.1 plunky SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
110 1.1 plunky log_err("Could not set L2CAP IMTU (%d): %m", mru);
111 1.1 plunky exit(EXIT_FAILURE);
112 1.1 plunky }
113 1.1 plunky
114 1.3 plunky if (listen(fd, 0) == -1) {
115 1.1 plunky log_err("Could not listen on server socket: %m");
116 1.1 plunky exit(EXIT_FAILURE);
117 1.1 plunky }
118 1.1 plunky
119 1.3 plunky event_set(&server_ev, fd, EV_READ | EV_PERSIST, server_read, NULL);
120 1.1 plunky if (event_add(&server_ev, NULL) == -1) {
121 1.1 plunky log_err("Could not add server event: %m");
122 1.1 plunky exit(EXIT_FAILURE);
123 1.1 plunky }
124 1.1 plunky
125 1.1 plunky log_info("server socket open");
126 1.1 plunky }
127 1.1 plunky
128 1.1 plunky /*
129 1.1 plunky * handle connection request
130 1.1 plunky */
131 1.1 plunky static void
132 1.1 plunky server_read(int s, short ev, void *arg)
133 1.1 plunky {
134 1.1 plunky struct sockaddr_bt ra, la;
135 1.1 plunky channel_t *chan;
136 1.1 plunky socklen_t len;
137 1.1 plunky int fd, n;
138 1.1 plunky uint16_t mru, mtu;
139 1.1 plunky
140 1.3 plunky assert(server_count < server_limit);
141 1.3 plunky
142 1.1 plunky len = sizeof(ra);
143 1.1 plunky fd = accept(s, (struct sockaddr *)&ra, &len);
144 1.1 plunky if (fd == -1)
145 1.1 plunky return;
146 1.1 plunky
147 1.1 plunky n = 1;
148 1.1 plunky if (ioctl(fd, FIONBIO, &n) == -1) {
149 1.1 plunky log_err("Could not set NonBlocking IO: %m");
150 1.1 plunky close(fd);
151 1.1 plunky return;
152 1.1 plunky }
153 1.1 plunky
154 1.1 plunky len = sizeof(mru);
155 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
156 1.1 plunky log_err("Could not get L2CAP IMTU: %m");
157 1.1 plunky close(fd);
158 1.1 plunky return;
159 1.1 plunky }
160 1.1 plunky if(mru < BNEP_MTU_MIN) {
161 1.1 plunky log_err("L2CAP IMTU too small (%d)", mru);
162 1.1 plunky close(fd);
163 1.1 plunky return;
164 1.1 plunky }
165 1.1 plunky
166 1.1 plunky len = sizeof(mtu);
167 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
168 1.1 plunky log_err("Could not get L2CAP OMTU: %m");
169 1.1 plunky close(fd);
170 1.1 plunky return;
171 1.1 plunky }
172 1.1 plunky if (mtu < BNEP_MTU_MIN) {
173 1.1 plunky log_err("L2CAP OMTU too small (%d)", mtu);
174 1.1 plunky close(fd);
175 1.1 plunky return;
176 1.1 plunky }
177 1.1 plunky
178 1.1 plunky len = sizeof(n);
179 1.1 plunky if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
180 1.1 plunky log_err("Could not get socket send buffer size: %m");
181 1.1 plunky close(fd);
182 1.1 plunky return;
183 1.1 plunky }
184 1.1 plunky
185 1.1 plunky if (n < (mtu * 2)) {
186 1.1 plunky n = mtu * 2;
187 1.1 plunky if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
188 1.1 plunky log_err("Could not set socket send buffer size (%d): %m", n);
189 1.1 plunky close(fd);
190 1.1 plunky return;
191 1.1 plunky }
192 1.1 plunky }
193 1.1 plunky
194 1.1 plunky n = mtu;
195 1.1 plunky if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
196 1.1 plunky log_err("Could not set socket low water mark (%d): %m", n);
197 1.1 plunky close(fd);
198 1.1 plunky return;
199 1.1 plunky }
200 1.1 plunky
201 1.1 plunky len = sizeof(la);
202 1.1 plunky if (getsockname(fd, (struct sockaddr *)&la, &len) == -1) {
203 1.1 plunky log_err("Could not get socket address: %m");
204 1.1 plunky close(fd);
205 1.1 plunky return;
206 1.1 plunky }
207 1.1 plunky
208 1.1 plunky log_info("Accepted connection from %s", bt_ntoa(&ra.bt_bdaddr, NULL));
209 1.1 plunky
210 1.1 plunky chan = channel_alloc();
211 1.1 plunky if (chan == NULL) {
212 1.1 plunky close(fd);
213 1.1 plunky return;
214 1.1 plunky }
215 1.1 plunky
216 1.1 plunky chan->send = bnep_send;
217 1.1 plunky chan->recv = bnep_recv;
218 1.3 plunky chan->down = server_down;
219 1.1 plunky chan->mru = mru;
220 1.1 plunky chan->mtu = mtu;
221 1.1 plunky b2eaddr(chan->raddr, &ra.bt_bdaddr);
222 1.1 plunky b2eaddr(chan->laddr, &la.bt_bdaddr);
223 1.1 plunky chan->state = CHANNEL_WAIT_CONNECT_REQ;
224 1.1 plunky channel_timeout(chan, 10);
225 1.1 plunky if (!channel_open(chan, fd)) {
226 1.1 plunky chan->state = CHANNEL_CLOSED;
227 1.1 plunky channel_free(chan);
228 1.1 plunky close(fd);
229 1.1 plunky return;
230 1.1 plunky }
231 1.3 plunky
232 1.3 plunky if (++server_count == server_limit) {
233 1.3 plunky log_info("Server limit reached, closing server socket");
234 1.3 plunky event_del(&server_ev);
235 1.3 plunky close(s);
236 1.3 plunky }
237 1.3 plunky
238 1.3 plunky server_update();
239 1.3 plunky }
240 1.3 plunky
241 1.3 plunky /*
242 1.3 plunky * Shut down a server channel, we need to update the service record and
243 1.3 plunky * may want to restart accepting connections on the server socket
244 1.3 plunky */
245 1.3 plunky static void
246 1.3 plunky server_down(channel_t *chan)
247 1.3 plunky {
248 1.3 plunky
249 1.3 plunky assert(server_count > 0);
250 1.3 plunky
251 1.3 plunky channel_close(chan);
252 1.3 plunky
253 1.3 plunky if (server_count-- == server_limit)
254 1.3 plunky server_open();
255 1.3 plunky
256 1.3 plunky server_update();
257 1.1 plunky }
258 1.1 plunky
259 1.1 plunky static void
260 1.3 plunky server_update(void)
261 1.1 plunky {
262 1.4 plunky bool rv;
263 1.1 plunky
264 1.4 plunky if (service_type == NULL)
265 1.3 plunky return;
266 1.3 plunky
267 1.1 plunky if (server_ss == NULL) {
268 1.1 plunky server_ss = sdp_open_local(control_path);
269 1.4 plunky if (server_ss == NULL) {
270 1.1 plunky log_err("failed to contact SDP server");
271 1.1 plunky return;
272 1.1 plunky }
273 1.1 plunky }
274 1.1 plunky
275 1.4 plunky server_mkrecord();
276 1.4 plunky
277 1.4 plunky if (server_handle == 0)
278 1.4 plunky rv = sdp_record_insert(server_ss, &local_bdaddr,
279 1.4 plunky &server_handle, &server_record);
280 1.1 plunky else
281 1.4 plunky rv = sdp_record_update(server_ss, server_handle,
282 1.4 plunky &server_record);
283 1.1 plunky
284 1.4 plunky if (!rv) {
285 1.4 plunky log_err("%s: %m", service_type);
286 1.1 plunky exit(EXIT_FAILURE);
287 1.1 plunky }
288 1.1 plunky }
289 1.4 plunky
290 1.4 plunky static void
291 1.4 plunky server_mkrecord(void)
292 1.4 plunky {
293 1.4 plunky static uint8_t data[256]; /* tis enough */
294 1.4 plunky sdp_data_t buf;
295 1.4 plunky size_t i;
296 1.4 plunky
297 1.4 plunky buf.next = data;
298 1.4 plunky buf.end = data + sizeof(data);
299 1.4 plunky
300 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_SERVICE_RECORD_HANDLE);
301 1.4 plunky sdp_put_uint32(&buf, 0x00000000);
302 1.4 plunky
303 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_SERVICE_CLASS_ID_LIST);
304 1.4 plunky sdp_put_seq(&buf, 3);
305 1.4 plunky sdp_put_uuid16(&buf, service_class);
306 1.4 plunky
307 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
308 1.4 plunky sdp_put_seq(&buf, 8 + 10 + 3 * server_nproto);
309 1.4 plunky sdp_put_seq(&buf, 6);
310 1.4 plunky sdp_put_uuid16(&buf, SDP_UUID_PROTOCOL_L2CAP);
311 1.4 plunky sdp_put_uint16(&buf, l2cap_psm);
312 1.4 plunky sdp_put_seq(&buf, 8 + 3 * server_nproto);
313 1.4 plunky sdp_put_uuid16(&buf, SDP_UUID_PROTOCOL_BNEP);
314 1.4 plunky sdp_put_uint16(&buf, 0x0100); /* v1.0 */
315 1.4 plunky sdp_put_seq(&buf, 3 * server_nproto);
316 1.4 plunky for (i = 0; i < server_nproto; i++)
317 1.4 plunky sdp_put_uint16(&buf, server_proto[i]);
318 1.4 plunky
319 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_BROWSE_GROUP_LIST);
320 1.4 plunky sdp_put_seq(&buf, 3);
321 1.4 plunky sdp_put_uuid16(&buf, SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
322 1.4 plunky
323 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST);
324 1.4 plunky sdp_put_seq(&buf, 9);
325 1.4 plunky sdp_put_uint16(&buf, 0x656e); /* "en" */
326 1.4 plunky sdp_put_uint16(&buf, 106); /* UTF-8 */
327 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID);
328 1.4 plunky
329 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_SERVICE_AVAILABILITY);
330 1.4 plunky sdp_put_uint8(&buf, (UINT8_MAX - server_count * UINT8_MAX / server_limit));
331 1.4 plunky
332 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
333 1.4 plunky sdp_put_seq(&buf, 8);
334 1.4 plunky sdp_put_seq(&buf, 6);
335 1.4 plunky sdp_put_uuid16(&buf, service_class);
336 1.4 plunky sdp_put_uint16(&buf, 0x0100); /* v1.0 */
337 1.4 plunky
338 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
339 1.4 plunky + SDP_ATTR_SERVICE_NAME_OFFSET);
340 1.4 plunky sdp_put_str(&buf, service_name, -1);
341 1.4 plunky
342 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID
343 1.4 plunky + SDP_ATTR_SERVICE_DESCRIPTION_OFFSET);
344 1.4 plunky sdp_put_str(&buf, service_desc, -1);
345 1.4 plunky
346 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_SECURITY_DESCRIPTION);
347 1.6 plunky sdp_put_uint16(&buf, (l2cap_mode & L2CAP_LM_AUTH) ? 0x0001 : 0x0000);
348 1.4 plunky
349 1.4 plunky if (service_class == SDP_SERVICE_CLASS_NAP) {
350 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_NET_ACCESS_TYPE);
351 1.4 plunky sdp_put_uint16(&buf, 0x0004); /* 10Mb Ethernet */
352 1.4 plunky
353 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_MAX_NET_ACCESS_RATE);
354 1.5 plunky sdp_put_uint32(&buf, IF_Mbps(10) / 8); /* octets/second */
355 1.4 plunky }
356 1.4 plunky
357 1.4 plunky if (service_class == SDP_SERVICE_CLASS_NAP
358 1.4 plunky || service_class == SDP_SERVICE_CLASS_GN) {
359 1.4 plunky if (server_ipv4_subnet) {
360 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_IPV4_SUBNET);
361 1.4 plunky sdp_put_str(&buf, server_ipv4_subnet, -1);
362 1.4 plunky }
363 1.4 plunky
364 1.4 plunky if (server_ipv6_subnet) {
365 1.4 plunky sdp_put_uint16(&buf, SDP_ATTR_IPV6_SUBNET);
366 1.4 plunky sdp_put_str(&buf, server_ipv6_subnet, -1);
367 1.4 plunky }
368 1.4 plunky }
369 1.4 plunky
370 1.4 plunky server_record.next = data;
371 1.4 plunky server_record.end = buf.next;
372 1.4 plunky }
373