server.c revision 1.3 1 1.3 plunky /* $NetBSD: server.c,v 1.3 2009/05/12 21:08:30 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.3 plunky __RCSID("$NetBSD: server.c,v 1.3 2009/05/12 21:08:30 plunky Exp $");
30 1.1 plunky
31 1.1 plunky #include <sys/ioctl.h>
32 1.1 plunky
33 1.1 plunky #include <bluetooth.h>
34 1.1 plunky #include <errno.h>
35 1.1 plunky #include <sdp.h>
36 1.1 plunky #include <unistd.h>
37 1.1 plunky
38 1.1 plunky #include "btpand.h"
39 1.1 plunky #include "bnep.h"
40 1.1 plunky
41 1.1 plunky static struct event server_ev;
42 1.3 plunky static int server_count;
43 1.1 plunky
44 1.1 plunky static void * server_ss;
45 1.1 plunky static uint32_t server_handle;
46 1.1 plunky
47 1.1 plunky static void server_open(void);
48 1.1 plunky static void server_read(int, short, void *);
49 1.3 plunky static void server_down(channel_t *);
50 1.3 plunky static void server_update(void);
51 1.1 plunky
52 1.1 plunky void
53 1.1 plunky server_init(void)
54 1.1 plunky {
55 1.1 plunky
56 1.1 plunky if (server_limit == 0)
57 1.1 plunky return;
58 1.1 plunky
59 1.3 plunky server_open();
60 1.3 plunky server_update();
61 1.1 plunky }
62 1.1 plunky
63 1.3 plunky /*
64 1.3 plunky * Start listening on server socket
65 1.3 plunky */
66 1.1 plunky static void
67 1.1 plunky server_open(void)
68 1.1 plunky {
69 1.1 plunky struct sockaddr_bt sa;
70 1.1 plunky uint16_t mru;
71 1.3 plunky int fd;
72 1.1 plunky
73 1.3 plunky fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
74 1.3 plunky if (fd == -1) {
75 1.1 plunky log_err("Could not open L2CAP socket: %m");
76 1.1 plunky exit(EXIT_FAILURE);
77 1.1 plunky }
78 1.1 plunky
79 1.1 plunky memset(&sa, 0, sizeof(sa));
80 1.1 plunky sa.bt_family = AF_BLUETOOTH;
81 1.1 plunky sa.bt_len = sizeof(sa);
82 1.1 plunky sa.bt_psm = l2cap_psm;
83 1.1 plunky bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
84 1.3 plunky if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
85 1.1 plunky log_err("Could not bind server socket: %m");
86 1.1 plunky exit(EXIT_FAILURE);
87 1.1 plunky }
88 1.1 plunky
89 1.3 plunky if (setsockopt(fd, BTPROTO_L2CAP,
90 1.1 plunky SO_L2CAP_LM, &l2cap_mode, sizeof(l2cap_mode)) == -1) {
91 1.1 plunky log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
92 1.1 plunky exit(EXIT_FAILURE);
93 1.1 plunky }
94 1.1 plunky
95 1.1 plunky mru = BNEP_MTU_MIN;
96 1.3 plunky if (setsockopt(fd, BTPROTO_L2CAP,
97 1.1 plunky SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
98 1.1 plunky log_err("Could not set L2CAP IMTU (%d): %m", mru);
99 1.1 plunky exit(EXIT_FAILURE);
100 1.1 plunky }
101 1.1 plunky
102 1.3 plunky if (listen(fd, 0) == -1) {
103 1.1 plunky log_err("Could not listen on server socket: %m");
104 1.1 plunky exit(EXIT_FAILURE);
105 1.1 plunky }
106 1.1 plunky
107 1.3 plunky event_set(&server_ev, fd, EV_READ | EV_PERSIST, server_read, NULL);
108 1.1 plunky if (event_add(&server_ev, NULL) == -1) {
109 1.1 plunky log_err("Could not add server event: %m");
110 1.1 plunky exit(EXIT_FAILURE);
111 1.1 plunky }
112 1.1 plunky
113 1.1 plunky log_info("server socket open");
114 1.1 plunky }
115 1.1 plunky
116 1.1 plunky /*
117 1.1 plunky * handle connection request
118 1.1 plunky */
119 1.1 plunky static void
120 1.1 plunky server_read(int s, short ev, void *arg)
121 1.1 plunky {
122 1.1 plunky struct sockaddr_bt ra, la;
123 1.1 plunky channel_t *chan;
124 1.1 plunky socklen_t len;
125 1.1 plunky int fd, n;
126 1.1 plunky uint16_t mru, mtu;
127 1.1 plunky
128 1.3 plunky assert(server_count < server_limit);
129 1.3 plunky
130 1.1 plunky len = sizeof(ra);
131 1.1 plunky fd = accept(s, (struct sockaddr *)&ra, &len);
132 1.1 plunky if (fd == -1)
133 1.1 plunky return;
134 1.1 plunky
135 1.1 plunky n = 1;
136 1.1 plunky if (ioctl(fd, FIONBIO, &n) == -1) {
137 1.1 plunky log_err("Could not set NonBlocking IO: %m");
138 1.1 plunky close(fd);
139 1.1 plunky return;
140 1.1 plunky }
141 1.1 plunky
142 1.1 plunky len = sizeof(mru);
143 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
144 1.1 plunky log_err("Could not get L2CAP IMTU: %m");
145 1.1 plunky close(fd);
146 1.1 plunky return;
147 1.1 plunky }
148 1.1 plunky if(mru < BNEP_MTU_MIN) {
149 1.1 plunky log_err("L2CAP IMTU too small (%d)", mru);
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(mtu);
155 1.1 plunky if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
156 1.1 plunky log_err("Could not get L2CAP OMTU: %m");
157 1.1 plunky close(fd);
158 1.1 plunky return;
159 1.1 plunky }
160 1.1 plunky if (mtu < BNEP_MTU_MIN) {
161 1.1 plunky log_err("L2CAP OMTU too small (%d)", mtu);
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(n);
167 1.1 plunky if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
168 1.1 plunky log_err("Could not get socket send buffer size: %m");
169 1.1 plunky close(fd);
170 1.1 plunky return;
171 1.1 plunky }
172 1.1 plunky
173 1.1 plunky if (n < (mtu * 2)) {
174 1.1 plunky n = mtu * 2;
175 1.1 plunky if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
176 1.1 plunky log_err("Could not set socket send buffer size (%d): %m", n);
177 1.1 plunky close(fd);
178 1.1 plunky return;
179 1.1 plunky }
180 1.1 plunky }
181 1.1 plunky
182 1.1 plunky n = mtu;
183 1.1 plunky if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
184 1.1 plunky log_err("Could not set socket low water mark (%d): %m", n);
185 1.1 plunky close(fd);
186 1.1 plunky return;
187 1.1 plunky }
188 1.1 plunky
189 1.1 plunky len = sizeof(la);
190 1.1 plunky if (getsockname(fd, (struct sockaddr *)&la, &len) == -1) {
191 1.1 plunky log_err("Could not get socket address: %m");
192 1.1 plunky close(fd);
193 1.1 plunky return;
194 1.1 plunky }
195 1.1 plunky
196 1.1 plunky log_info("Accepted connection from %s", bt_ntoa(&ra.bt_bdaddr, NULL));
197 1.1 plunky
198 1.1 plunky chan = channel_alloc();
199 1.1 plunky if (chan == NULL) {
200 1.1 plunky close(fd);
201 1.1 plunky return;
202 1.1 plunky }
203 1.1 plunky
204 1.1 plunky chan->send = bnep_send;
205 1.1 plunky chan->recv = bnep_recv;
206 1.3 plunky chan->down = server_down;
207 1.1 plunky chan->mru = mru;
208 1.1 plunky chan->mtu = mtu;
209 1.1 plunky b2eaddr(chan->raddr, &ra.bt_bdaddr);
210 1.1 plunky b2eaddr(chan->laddr, &la.bt_bdaddr);
211 1.1 plunky chan->state = CHANNEL_WAIT_CONNECT_REQ;
212 1.1 plunky channel_timeout(chan, 10);
213 1.1 plunky if (!channel_open(chan, fd)) {
214 1.1 plunky chan->state = CHANNEL_CLOSED;
215 1.1 plunky channel_free(chan);
216 1.1 plunky close(fd);
217 1.1 plunky return;
218 1.1 plunky }
219 1.3 plunky
220 1.3 plunky if (++server_count == server_limit) {
221 1.3 plunky log_info("Server limit reached, closing server socket");
222 1.3 plunky event_del(&server_ev);
223 1.3 plunky close(s);
224 1.3 plunky }
225 1.3 plunky
226 1.3 plunky server_update();
227 1.3 plunky }
228 1.3 plunky
229 1.3 plunky /*
230 1.3 plunky * Shut down a server channel, we need to update the service record and
231 1.3 plunky * may want to restart accepting connections on the server socket
232 1.3 plunky */
233 1.3 plunky static void
234 1.3 plunky server_down(channel_t *chan)
235 1.3 plunky {
236 1.3 plunky
237 1.3 plunky assert(server_count > 0);
238 1.3 plunky
239 1.3 plunky channel_close(chan);
240 1.3 plunky
241 1.3 plunky if (server_count-- == server_limit)
242 1.3 plunky server_open();
243 1.3 plunky
244 1.3 plunky server_update();
245 1.1 plunky }
246 1.1 plunky
247 1.1 plunky static void
248 1.3 plunky server_update(void)
249 1.1 plunky {
250 1.1 plunky sdp_nap_profile_t p;
251 1.1 plunky int rv;
252 1.1 plunky
253 1.3 plunky if (service_name == NULL)
254 1.3 plunky return;
255 1.3 plunky
256 1.1 plunky if (server_ss == NULL) {
257 1.1 plunky server_ss = sdp_open_local(control_path);
258 1.1 plunky if (server_ss == NULL || sdp_error(server_ss) != 0) {
259 1.1 plunky log_err("failed to contact SDP server");
260 1.1 plunky return;
261 1.1 plunky }
262 1.1 plunky }
263 1.1 plunky
264 1.2 plunky memset(&p, 0, sizeof(p));
265 1.2 plunky p.psm = l2cap_psm;
266 1.3 plunky p.load_factor = (UINT8_MAX - server_count * UINT8_MAX / server_limit);
267 1.2 plunky p.security_description = (l2cap_mode == 0 ? 0x0000 : 0x0001);
268 1.1 plunky
269 1.1 plunky if (server_handle)
270 1.1 plunky rv = sdp_change_service(server_ss, server_handle,
271 1.1 plunky (uint8_t *)&p, sizeof(p));
272 1.1 plunky else
273 1.1 plunky rv = sdp_register_service(server_ss, service_class,
274 1.1 plunky &local_bdaddr, (uint8_t *)&p, sizeof(p), &server_handle);
275 1.1 plunky
276 1.1 plunky if (rv != 0) {
277 1.1 plunky errno = sdp_error(server_ss);
278 1.1 plunky log_err("%s: %m", service_name);
279 1.1 plunky exit(EXIT_FAILURE);
280 1.1 plunky }
281 1.1 plunky }
282