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