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