server.c revision 1.2 1 1.2 plunky /* $NetBSD: server.c,v 1.2 2007/02/22 20:49:32 plunky Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Itronix Inc.
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Redistribution and use in source and binary forms, with or without
8 1.1 gdamore * modification, are permitted provided that the following conditions
9 1.1 gdamore * are met:
10 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
11 1.1 gdamore * notice, this list of conditions and the following disclaimer.
12 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
14 1.1 gdamore * documentation and/or other materials provided with the distribution.
15 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse
16 1.1 gdamore * or promote products derived from this software without specific
17 1.1 gdamore * prior written permission.
18 1.1 gdamore *
19 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
20 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
23 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE.
30 1.1 gdamore */
31 1.1 gdamore /*
32 1.1 gdamore * server.c
33 1.1 gdamore *
34 1.1 gdamore * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin (at) yahoo.com>
35 1.1 gdamore * All rights reserved.
36 1.1 gdamore *
37 1.1 gdamore * Redistribution and use in source and binary forms, with or without
38 1.1 gdamore * modification, are permitted provided that the following conditions
39 1.1 gdamore * are met:
40 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
41 1.1 gdamore * notice, this list of conditions and the following disclaimer.
42 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
44 1.1 gdamore * documentation and/or other materials provided with the distribution.
45 1.1 gdamore *
46 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 1.1 gdamore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 1.1 gdamore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 1.1 gdamore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 1.1 gdamore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 1.1 gdamore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 1.1 gdamore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 1.1 gdamore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 1.1 gdamore * SUCH DAMAGE.
57 1.1 gdamore *
58 1.2 plunky * $Id: server.c,v 1.2 2007/02/22 20:49:32 plunky Exp $
59 1.1 gdamore * $FreeBSD: src/usr.sbin/bluetooth/sdpd/server.c,v 1.2 2005/12/06 17:56:36 emax Exp $
60 1.1 gdamore */
61 1.1 gdamore
62 1.1 gdamore #include <sys/cdefs.h>
63 1.2 plunky __RCSID("$NetBSD: server.c,v 1.2 2007/02/22 20:49:32 plunky Exp $");
64 1.1 gdamore
65 1.1 gdamore #include <sys/param.h>
66 1.1 gdamore #include <sys/select.h>
67 1.1 gdamore #include <sys/stat.h>
68 1.1 gdamore #include <sys/queue.h>
69 1.1 gdamore #include <sys/ucred.h>
70 1.1 gdamore #include <sys/un.h>
71 1.1 gdamore #include <netinet/in.h>
72 1.1 gdamore #include <arpa/inet.h>
73 1.1 gdamore #include <assert.h>
74 1.1 gdamore #include <bluetooth.h>
75 1.1 gdamore #include <errno.h>
76 1.1 gdamore #include <pwd.h>
77 1.1 gdamore #include <sdp.h>
78 1.1 gdamore #include <stdio.h>
79 1.1 gdamore #include <stdlib.h>
80 1.1 gdamore #include <string.h>
81 1.1 gdamore #include <unistd.h>
82 1.1 gdamore #include "log.h"
83 1.1 gdamore #include "profile.h"
84 1.1 gdamore #include "provider.h"
85 1.1 gdamore #include "server.h"
86 1.1 gdamore
87 1.1 gdamore static void server_accept_client (server_p srv, int32_t fd);
88 1.1 gdamore static int32_t server_process_request (server_p srv, int32_t fd);
89 1.1 gdamore static int32_t server_send_error_response (server_p srv, int32_t fd,
90 1.1 gdamore uint16_t error);
91 1.1 gdamore static void server_close_fd (server_p srv, int32_t fd);
92 1.1 gdamore
93 1.1 gdamore /*
94 1.1 gdamore * Initialize server
95 1.1 gdamore */
96 1.1 gdamore
97 1.1 gdamore int32_t
98 1.1 gdamore server_init(server_p srv, char const *control)
99 1.1 gdamore {
100 1.1 gdamore struct sockaddr_un un;
101 1.1 gdamore struct sockaddr_bt l2;
102 1.1 gdamore socklen_t size;
103 1.1 gdamore int32_t unsock, l2sock;
104 1.1 gdamore uint16_t imtu;
105 1.1 gdamore int opt;
106 1.1 gdamore
107 1.1 gdamore assert(srv != NULL);
108 1.1 gdamore assert(control != NULL);
109 1.1 gdamore
110 1.1 gdamore memset(srv, 0, sizeof(srv));
111 1.1 gdamore
112 1.1 gdamore /* Open control socket */
113 1.1 gdamore if (unlink(control) < 0 && errno != ENOENT) {
114 1.1 gdamore log_crit("Could not unlink(%s). %s (%d)",
115 1.1 gdamore control, strerror(errno), errno);
116 1.1 gdamore return (-1);
117 1.1 gdamore }
118 1.1 gdamore
119 1.1 gdamore unsock = socket(PF_LOCAL, SOCK_STREAM, 0);
120 1.1 gdamore if (unsock < 0) {
121 1.1 gdamore log_crit("Could not create control socket. %s (%d)",
122 1.1 gdamore strerror(errno), errno);
123 1.1 gdamore return (-1);
124 1.1 gdamore }
125 1.1 gdamore
126 1.1 gdamore opt = 1;
127 1.1 gdamore if (setsockopt(unsock, 0, LOCAL_CREDS, &opt, sizeof(opt)) < 0)
128 1.1 gdamore log_crit("Warning: No credential checks on control socket");
129 1.1 gdamore
130 1.1 gdamore memset(&un, 0, sizeof(un));
131 1.1 gdamore un.sun_len = sizeof(un);
132 1.1 gdamore un.sun_family = AF_LOCAL;
133 1.1 gdamore strlcpy(un.sun_path, control, sizeof(un.sun_path));
134 1.1 gdamore
135 1.1 gdamore if (bind(unsock, (struct sockaddr *) &un, sizeof(un)) < 0) {
136 1.1 gdamore log_crit("Could not bind control socket. %s (%d)",
137 1.1 gdamore strerror(errno), errno);
138 1.1 gdamore close(unsock);
139 1.1 gdamore return (-1);
140 1.1 gdamore }
141 1.1 gdamore
142 1.1 gdamore if (chmod(control, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) < 0) {
143 1.1 gdamore log_crit("Could not change permissions on control socket. " \
144 1.1 gdamore "%s (%d)", strerror(errno), errno);
145 1.1 gdamore close(unsock);
146 1.1 gdamore return (-1);
147 1.1 gdamore }
148 1.1 gdamore
149 1.1 gdamore if (listen(unsock, 10) < 0) {
150 1.1 gdamore log_crit("Could not listen on control socket. %s (%d)",
151 1.1 gdamore strerror(errno), errno);
152 1.1 gdamore close(unsock);
153 1.1 gdamore return (-1);
154 1.1 gdamore }
155 1.1 gdamore
156 1.1 gdamore /* Open L2CAP socket */
157 1.1 gdamore l2sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
158 1.1 gdamore if (l2sock < 0) {
159 1.1 gdamore log_crit("Could not create L2CAP socket. %s (%d)",
160 1.1 gdamore strerror(errno), errno);
161 1.1 gdamore close(unsock);
162 1.1 gdamore return (-1);
163 1.1 gdamore }
164 1.1 gdamore
165 1.1 gdamore size = sizeof(imtu);
166 1.1 gdamore if (getsockopt(l2sock, BTPROTO_L2CAP, SO_L2CAP_IMTU, &imtu, &size) < 0) {
167 1.1 gdamore log_crit("Could not get L2CAP IMTU. %s (%d)",
168 1.1 gdamore strerror(errno), errno);
169 1.1 gdamore close(unsock);
170 1.1 gdamore close(l2sock);
171 1.1 gdamore return (-1);
172 1.1 gdamore }
173 1.1 gdamore
174 1.1 gdamore memset(&l2, 0, sizeof(l2));
175 1.1 gdamore l2.bt_len = sizeof(l2);
176 1.1 gdamore l2.bt_family = AF_BLUETOOTH;
177 1.1 gdamore l2.bt_psm = L2CAP_PSM_SDP;
178 1.1 gdamore bdaddr_copy(&l2.bt_bdaddr, BDADDR_ANY);
179 1.1 gdamore
180 1.1 gdamore if (bind(l2sock, (struct sockaddr *) &l2, sizeof(l2)) < 0) {
181 1.1 gdamore log_crit("Could not bind L2CAP socket. %s (%d)",
182 1.1 gdamore strerror(errno), errno);
183 1.1 gdamore close(unsock);
184 1.1 gdamore close(l2sock);
185 1.1 gdamore return (-1);
186 1.1 gdamore }
187 1.1 gdamore
188 1.1 gdamore if (listen(l2sock, 10) < 0) {
189 1.1 gdamore log_crit("Could not listen on L2CAP socket. %s (%d)",
190 1.1 gdamore strerror(errno), errno);
191 1.1 gdamore close(unsock);
192 1.1 gdamore close(l2sock);
193 1.1 gdamore return (-1);
194 1.1 gdamore }
195 1.1 gdamore
196 1.1 gdamore /* Allocate incoming buffer */
197 1.1 gdamore srv->imtu = (imtu > SDP_LOCAL_MTU)? imtu : SDP_LOCAL_MTU;
198 1.1 gdamore srv->req = (uint8_t *) calloc(srv->imtu, sizeof(srv->req[0]));
199 1.1 gdamore if (srv->req == NULL) {
200 1.1 gdamore log_crit("Could not allocate request buffer");
201 1.1 gdamore close(unsock);
202 1.1 gdamore close(l2sock);
203 1.1 gdamore return (-1);
204 1.1 gdamore }
205 1.1 gdamore
206 1.1 gdamore /* Allocate memory for descriptor index */
207 1.1 gdamore srv->fdidx = (fd_idx_p) calloc(FD_SETSIZE, sizeof(srv->fdidx[0]));
208 1.1 gdamore if (srv->fdidx == NULL) {
209 1.1 gdamore log_crit("Could not allocate fd index");
210 1.1 gdamore free(srv->req);
211 1.1 gdamore close(unsock);
212 1.1 gdamore close(l2sock);
213 1.1 gdamore return (-1);
214 1.1 gdamore }
215 1.1 gdamore
216 1.1 gdamore /* Register Service Discovery profile (attach it to control socket) */
217 1.1 gdamore if (provider_register_sd(unsock) < 0) {
218 1.1 gdamore log_crit("Could not register Service Discovery profile");
219 1.1 gdamore free(srv->fdidx);
220 1.1 gdamore free(srv->req);
221 1.1 gdamore close(unsock);
222 1.1 gdamore close(l2sock);
223 1.1 gdamore return (-1);
224 1.1 gdamore }
225 1.1 gdamore
226 1.1 gdamore /*
227 1.1 gdamore * If we got here then everything is fine. Add both control sockets
228 1.1 gdamore * to the index.
229 1.1 gdamore */
230 1.1 gdamore
231 1.1 gdamore FD_ZERO(&srv->fdset);
232 1.1 gdamore srv->maxfd = (unsock > l2sock)? unsock : l2sock;
233 1.1 gdamore
234 1.1 gdamore FD_SET(unsock, &srv->fdset);
235 1.1 gdamore srv->fdidx[unsock].valid = 1;
236 1.1 gdamore srv->fdidx[unsock].server = 1;
237 1.1 gdamore srv->fdidx[unsock].control = 1;
238 1.1 gdamore srv->fdidx[unsock].priv = 0;
239 1.1 gdamore srv->fdidx[unsock].rsp_cs = 0;
240 1.1 gdamore srv->fdidx[unsock].rsp_size = 0;
241 1.1 gdamore srv->fdidx[unsock].rsp_limit = 0;
242 1.1 gdamore srv->fdidx[unsock].omtu = SDP_LOCAL_MTU;
243 1.1 gdamore srv->fdidx[unsock].rsp = NULL;
244 1.1 gdamore
245 1.1 gdamore FD_SET(l2sock, &srv->fdset);
246 1.1 gdamore srv->fdidx[l2sock].valid = 1;
247 1.1 gdamore srv->fdidx[l2sock].server = 1;
248 1.1 gdamore srv->fdidx[l2sock].control = 0;
249 1.1 gdamore srv->fdidx[l2sock].priv = 0;
250 1.1 gdamore srv->fdidx[l2sock].rsp_cs = 0;
251 1.1 gdamore srv->fdidx[l2sock].rsp_size = 0;
252 1.1 gdamore srv->fdidx[l2sock].rsp_limit = 0;
253 1.1 gdamore srv->fdidx[l2sock].omtu = 0; /* unknown */
254 1.1 gdamore srv->fdidx[l2sock].rsp = NULL;
255 1.1 gdamore
256 1.1 gdamore return (0);
257 1.1 gdamore }
258 1.1 gdamore
259 1.1 gdamore /*
260 1.1 gdamore * Shutdown server
261 1.1 gdamore */
262 1.1 gdamore
263 1.1 gdamore void
264 1.1 gdamore server_shutdown(server_p srv)
265 1.1 gdamore {
266 1.1 gdamore int fd;
267 1.1 gdamore
268 1.1 gdamore assert(srv != NULL);
269 1.1 gdamore
270 1.1 gdamore for (fd = 0; fd < srv->maxfd + 1; fd ++)
271 1.1 gdamore if (srv->fdidx[fd].valid)
272 1.1 gdamore server_close_fd(srv, fd);
273 1.1 gdamore
274 1.1 gdamore free(srv->req);
275 1.1 gdamore free(srv->fdidx);
276 1.1 gdamore
277 1.1 gdamore memset(srv, 0, sizeof(*srv));
278 1.1 gdamore }
279 1.1 gdamore
280 1.1 gdamore /*
281 1.1 gdamore * Do one server iteration
282 1.1 gdamore */
283 1.1 gdamore
284 1.1 gdamore int32_t
285 1.1 gdamore server_do(server_p srv)
286 1.1 gdamore {
287 1.1 gdamore fd_set fdset;
288 1.1 gdamore int32_t n, fd;
289 1.1 gdamore
290 1.1 gdamore assert(srv != NULL);
291 1.1 gdamore
292 1.1 gdamore /* Copy cached version of the fd set and call select */
293 1.1 gdamore memcpy(&fdset, &srv->fdset, sizeof(fdset));
294 1.1 gdamore n = select(srv->maxfd + 1, &fdset, NULL, NULL, NULL);
295 1.1 gdamore if (n < 0) {
296 1.1 gdamore if (errno == EINTR)
297 1.1 gdamore return (0);
298 1.1 gdamore
299 1.1 gdamore log_err("Could not select(%d, %p). %s (%d)",
300 1.1 gdamore srv->maxfd + 1, &fdset, strerror(errno), errno);
301 1.1 gdamore
302 1.1 gdamore return (-1);
303 1.1 gdamore }
304 1.1 gdamore
305 1.1 gdamore /* Process descriptors */
306 1.1 gdamore for (fd = 0; fd < srv->maxfd + 1 && n > 0; fd ++) {
307 1.1 gdamore if (!FD_ISSET(fd, &fdset))
308 1.1 gdamore continue;
309 1.1 gdamore
310 1.1 gdamore assert(srv->fdidx[fd].valid);
311 1.1 gdamore n --;
312 1.1 gdamore
313 1.1 gdamore if (srv->fdidx[fd].server)
314 1.1 gdamore server_accept_client(srv, fd);
315 1.1 gdamore else if (server_process_request(srv, fd) != 0)
316 1.1 gdamore server_close_fd(srv, fd);
317 1.1 gdamore }
318 1.1 gdamore
319 1.1 gdamore return (0);
320 1.1 gdamore
321 1.1 gdamore }
322 1.1 gdamore
323 1.1 gdamore /*
324 1.1 gdamore * Accept new client connection and register it with index
325 1.1 gdamore */
326 1.1 gdamore
327 1.1 gdamore static void
328 1.1 gdamore server_accept_client(server_p srv, int32_t fd)
329 1.1 gdamore {
330 1.1 gdamore uint8_t *rsp = NULL;
331 1.1 gdamore socklen_t size;
332 1.1 gdamore int32_t cfd;
333 1.1 gdamore uint16_t omtu;
334 1.1 gdamore
335 1.1 gdamore do {
336 1.1 gdamore cfd = accept(fd, NULL, NULL);
337 1.1 gdamore } while (cfd < 0 && errno == EINTR);
338 1.1 gdamore
339 1.1 gdamore if (cfd < 0) {
340 1.1 gdamore log_err("Could not accept connection on %s socket. %s (%d)",
341 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
342 1.1 gdamore strerror(errno), errno);
343 1.1 gdamore return;
344 1.1 gdamore }
345 1.1 gdamore
346 1.1 gdamore assert(!FD_ISSET(cfd, &srv->fdset));
347 1.1 gdamore assert(!srv->fdidx[cfd].valid);
348 1.1 gdamore
349 1.1 gdamore if (!srv->fdidx[fd].control) {
350 1.1 gdamore /* Get local BD_ADDR */
351 1.1 gdamore size = sizeof(srv->req_sa);
352 1.1 gdamore if (getsockname(cfd,(struct sockaddr*)&srv->req_sa, &size) < 0) {
353 1.1 gdamore log_err("Could not get local BD_ADDR. %s (%d)",
354 1.1 gdamore strerror(errno), errno);
355 1.1 gdamore close(cfd);
356 1.1 gdamore return;
357 1.1 gdamore }
358 1.1 gdamore
359 1.1 gdamore /* Get outgoing MTU */
360 1.1 gdamore size = sizeof(omtu);
361 1.1 gdamore if (getsockopt(cfd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &omtu, &size) < 0) {
362 1.1 gdamore log_err("Could not get L2CAP OMTU. %s (%d)",
363 1.1 gdamore strerror(errno), errno);
364 1.1 gdamore close(cfd);
365 1.1 gdamore return;
366 1.1 gdamore }
367 1.1 gdamore
368 1.1 gdamore /*
369 1.1 gdamore * The maximum size of the L2CAP packet is 65536 bytes.
370 1.1 gdamore * The minimum L2CAP MTU is 43 bytes. That means we need
371 1.1 gdamore * 65536 / 43 = ~1524 chunks to transfer maximum packet
372 1.1 gdamore * size with minimum MTU. The "rsp_cs" field in fd_idx_t
373 1.1 gdamore * is 11 bit wide that gives us upto 2048 chunks.
374 1.1 gdamore */
375 1.1 gdamore
376 1.1 gdamore if (omtu < L2CAP_MTU_MINIMUM) {
377 1.1 gdamore log_err("L2CAP OMTU is too small (%d bytes)", omtu);
378 1.1 gdamore close(cfd);
379 1.1 gdamore return;
380 1.1 gdamore }
381 1.1 gdamore } else {
382 1.1 gdamore bdaddr_copy(&srv->req_sa.bt_bdaddr, BDADDR_ANY);
383 1.1 gdamore omtu = srv->fdidx[fd].omtu;
384 1.1 gdamore }
385 1.1 gdamore
386 1.1 gdamore /*
387 1.1 gdamore * Allocate buffer. This is an overkill, but we can not know how
388 1.1 gdamore * big our reply is going to be.
389 1.1 gdamore */
390 1.1 gdamore
391 1.1 gdamore rsp = (uint8_t *) calloc(L2CAP_MTU_MAXIMUM, sizeof(rsp[0]));
392 1.1 gdamore if (rsp == NULL) {
393 1.1 gdamore log_crit("Could not allocate response buffer");
394 1.1 gdamore close(cfd);
395 1.1 gdamore return;
396 1.1 gdamore }
397 1.1 gdamore
398 1.1 gdamore /* Add client descriptor to the index */
399 1.1 gdamore FD_SET(cfd, &srv->fdset);
400 1.1 gdamore if (srv->maxfd < cfd)
401 1.1 gdamore srv->maxfd = cfd;
402 1.1 gdamore srv->fdidx[cfd].valid = 1;
403 1.1 gdamore srv->fdidx[cfd].server = 0;
404 1.1 gdamore srv->fdidx[cfd].control = srv->fdidx[fd].control;
405 1.1 gdamore srv->fdidx[cfd].priv = 0;
406 1.1 gdamore srv->fdidx[cfd].rsp_cs = 0;
407 1.1 gdamore srv->fdidx[cfd].rsp_size = 0;
408 1.1 gdamore srv->fdidx[cfd].rsp_limit = 0;
409 1.1 gdamore srv->fdidx[cfd].omtu = omtu;
410 1.1 gdamore srv->fdidx[cfd].rsp = rsp;
411 1.1 gdamore }
412 1.1 gdamore
413 1.1 gdamore /*
414 1.1 gdamore * Process request from the client
415 1.1 gdamore */
416 1.1 gdamore
417 1.1 gdamore static int32_t
418 1.1 gdamore server_process_request(server_p srv, int32_t fd)
419 1.1 gdamore {
420 1.1 gdamore uint8_t ctl[128];
421 1.1 gdamore sdp_pdu_p pdu = (sdp_pdu_p) srv->req;
422 1.1 gdamore struct msghdr msg;
423 1.1 gdamore struct iovec iov;
424 1.1 gdamore int32_t len, error;
425 1.1 gdamore struct cmsghdr *cmsg;
426 1.1 gdamore struct sockcred *cred;
427 1.1 gdamore
428 1.1 gdamore assert(srv->imtu > 0);
429 1.1 gdamore assert(srv->req != NULL);
430 1.1 gdamore assert(FD_ISSET(fd, &srv->fdset));
431 1.1 gdamore assert(srv->fdidx[fd].valid);
432 1.1 gdamore assert(!srv->fdidx[fd].server);
433 1.1 gdamore assert(srv->fdidx[fd].rsp != NULL);
434 1.1 gdamore assert(srv->fdidx[fd].omtu >= L2CAP_MTU_MINIMUM);
435 1.1 gdamore
436 1.1 gdamore iov.iov_base = srv->req;
437 1.1 gdamore iov.iov_len = srv->imtu;
438 1.1 gdamore
439 1.1 gdamore msg.msg_name = NULL;
440 1.1 gdamore msg.msg_namelen = 0;
441 1.1 gdamore msg.msg_iov = &iov;
442 1.1 gdamore msg.msg_iovlen = 1;
443 1.1 gdamore msg.msg_control = ctl;
444 1.1 gdamore msg.msg_controllen = sizeof(ctl);
445 1.1 gdamore msg.msg_flags = 0;
446 1.1 gdamore
447 1.1 gdamore do {
448 1.1 gdamore len = recvmsg(fd, &msg, 0);
449 1.1 gdamore } while (len < 0 && errno == EINTR);
450 1.1 gdamore
451 1.1 gdamore if (len < 0) {
452 1.1 gdamore log_err("Could not receive SDP request from %s socket. %s (%d)",
453 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
454 1.1 gdamore strerror(errno), errno);
455 1.1 gdamore return (-1);
456 1.1 gdamore }
457 1.1 gdamore if (len == 0) {
458 1.1 gdamore log_info("Client on %s socket has disconnected",
459 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP");
460 1.1 gdamore return (-1);
461 1.1 gdamore }
462 1.1 gdamore
463 1.1 gdamore if ((cmsg = CMSG_FIRSTHDR(&msg)) != NULL
464 1.1 gdamore && cmsg->cmsg_level == SOL_SOCKET
465 1.1 gdamore && cmsg->cmsg_type == SCM_CREDS
466 1.2 plunky && cmsg->cmsg_len >= CMSG_LEN(SOCKCREDSIZE(0))
467 1.1 gdamore && (cred = (struct sockcred *)CMSG_DATA(cmsg)) != NULL
468 1.1 gdamore && (cred->sc_uid == 0 || cred->sc_euid == 0))
469 1.1 gdamore srv->fdidx[fd].priv = 1;
470 1.2 plunky
471 1.2 plunky if (len >= sizeof(*pdu)
472 1.2 plunky && (sizeof(*pdu) + (pdu->len = ntohs(pdu->len))) == len) {
473 1.1 gdamore switch (pdu->pid) {
474 1.1 gdamore case SDP_PDU_SERVICE_SEARCH_REQUEST:
475 1.1 gdamore error = server_prepare_service_search_response(srv, fd);
476 1.1 gdamore break;
477 1.1 gdamore
478 1.1 gdamore case SDP_PDU_SERVICE_ATTRIBUTE_REQUEST:
479 1.1 gdamore error = server_prepare_service_attribute_response(srv, fd);
480 1.1 gdamore break;
481 1.1 gdamore
482 1.1 gdamore case SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST:
483 1.1 gdamore error = server_prepare_service_search_attribute_response(srv, fd);
484 1.1 gdamore break;
485 1.1 gdamore
486 1.1 gdamore case SDP_PDU_SERVICE_REGISTER_REQUEST:
487 1.1 gdamore error = server_prepare_service_register_response(srv, fd);
488 1.1 gdamore break;
489 1.1 gdamore
490 1.1 gdamore case SDP_PDU_SERVICE_UNREGISTER_REQUEST:
491 1.1 gdamore error = server_prepare_service_unregister_response(srv, fd);
492 1.1 gdamore break;
493 1.1 gdamore
494 1.1 gdamore case SDP_PDU_SERVICE_CHANGE_REQUEST:
495 1.1 gdamore error = server_prepare_service_change_response(srv, fd);
496 1.1 gdamore break;
497 1.1 gdamore
498 1.1 gdamore default:
499 1.1 gdamore error = SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
500 1.1 gdamore break;
501 1.1 gdamore }
502 1.1 gdamore } else
503 1.1 gdamore error = SDP_ERROR_CODE_INVALID_PDU_SIZE;
504 1.1 gdamore
505 1.1 gdamore if (error == 0) {
506 1.1 gdamore switch (pdu->pid) {
507 1.1 gdamore case SDP_PDU_SERVICE_SEARCH_REQUEST:
508 1.1 gdamore error = server_send_service_search_response(srv, fd);
509 1.1 gdamore break;
510 1.1 gdamore
511 1.1 gdamore case SDP_PDU_SERVICE_ATTRIBUTE_REQUEST:
512 1.1 gdamore error = server_send_service_attribute_response(srv, fd);
513 1.1 gdamore break;
514 1.1 gdamore
515 1.1 gdamore case SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST:
516 1.1 gdamore error = server_send_service_search_attribute_response(srv, fd);
517 1.1 gdamore break;
518 1.1 gdamore
519 1.1 gdamore case SDP_PDU_SERVICE_REGISTER_REQUEST:
520 1.1 gdamore error = server_send_service_register_response(srv, fd);
521 1.1 gdamore break;
522 1.1 gdamore
523 1.1 gdamore case SDP_PDU_SERVICE_UNREGISTER_REQUEST:
524 1.1 gdamore error = server_send_service_unregister_response(srv, fd);
525 1.1 gdamore break;
526 1.1 gdamore
527 1.1 gdamore case SDP_PDU_SERVICE_CHANGE_REQUEST:
528 1.1 gdamore error = server_send_service_change_response(srv, fd);
529 1.1 gdamore break;
530 1.1 gdamore
531 1.1 gdamore default:
532 1.1 gdamore error = SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
533 1.1 gdamore break;
534 1.1 gdamore }
535 1.1 gdamore
536 1.1 gdamore if (error != 0)
537 1.1 gdamore log_err("Could not send SDP response to %s socket, " \
538 1.1 gdamore "pdu->pid=%d, pdu->tid=%d, error=%d",
539 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
540 1.1 gdamore pdu->pid, ntohs(pdu->tid), error);
541 1.1 gdamore } else {
542 1.1 gdamore log_err("Could not process SDP request from %s socket, " \
543 1.1 gdamore "pdu->pid=%d, pdu->tid=%d, pdu->len=%d, len=%d, " \
544 1.1 gdamore "error=%d",
545 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
546 1.1 gdamore pdu->pid, ntohs(pdu->tid), pdu->len, len, error);
547 1.1 gdamore
548 1.1 gdamore error = server_send_error_response(srv, fd, error);
549 1.1 gdamore if (error != 0)
550 1.1 gdamore log_err("Could not send SDP error response to %s " \
551 1.1 gdamore "socket, pdu->pid=%d, pdu->tid=%d, error=%d",
552 1.1 gdamore srv->fdidx[fd].control? "control" : "L2CAP",
553 1.1 gdamore pdu->pid, ntohs(pdu->tid), error);
554 1.1 gdamore }
555 1.1 gdamore
556 1.1 gdamore /* On error forget response (if any) */
557 1.1 gdamore if (error != 0) {
558 1.1 gdamore srv->fdidx[fd].rsp_cs = 0;
559 1.1 gdamore srv->fdidx[fd].rsp_size = 0;
560 1.1 gdamore srv->fdidx[fd].rsp_limit = 0;
561 1.1 gdamore }
562 1.1 gdamore
563 1.1 gdamore return (error);
564 1.1 gdamore }
565 1.1 gdamore
566 1.1 gdamore /*
567 1.1 gdamore * Send SDP_Error_Response PDU
568 1.1 gdamore */
569 1.1 gdamore
570 1.1 gdamore static int32_t
571 1.1 gdamore server_send_error_response(server_p srv, int32_t fd, uint16_t error)
572 1.1 gdamore {
573 1.1 gdamore int32_t size;
574 1.1 gdamore
575 1.1 gdamore struct {
576 1.1 gdamore sdp_pdu_t pdu;
577 1.1 gdamore uint16_t error;
578 1.1 gdamore } __attribute__ ((packed)) rsp;
579 1.1 gdamore
580 1.1 gdamore /* Prepare and send SDP error response */
581 1.1 gdamore rsp.pdu.pid = SDP_PDU_ERROR_RESPONSE;
582 1.1 gdamore rsp.pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
583 1.1 gdamore rsp.pdu.len = htons(sizeof(rsp.error));
584 1.1 gdamore rsp.error = htons(error);
585 1.1 gdamore
586 1.1 gdamore do {
587 1.1 gdamore size = write(fd, &rsp, sizeof(rsp));
588 1.1 gdamore } while (size < 0 && errno == EINTR);
589 1.1 gdamore
590 1.1 gdamore return ((size < 0)? errno : 0);
591 1.1 gdamore }
592 1.1 gdamore
593 1.1 gdamore /*
594 1.1 gdamore * Close descriptor and remove it from index
595 1.1 gdamore */
596 1.1 gdamore
597 1.1 gdamore static void
598 1.1 gdamore server_close_fd(server_p srv, int32_t fd)
599 1.1 gdamore {
600 1.1 gdamore provider_p provider = NULL, provider_next = NULL;
601 1.1 gdamore
602 1.1 gdamore assert(FD_ISSET(fd, &srv->fdset));
603 1.1 gdamore assert(srv->fdidx[fd].valid);
604 1.1 gdamore
605 1.1 gdamore close(fd);
606 1.1 gdamore
607 1.1 gdamore FD_CLR(fd, &srv->fdset);
608 1.1 gdamore if (fd == srv->maxfd)
609 1.1 gdamore srv->maxfd --;
610 1.1 gdamore
611 1.1 gdamore if (srv->fdidx[fd].rsp != NULL)
612 1.1 gdamore free(srv->fdidx[fd].rsp);
613 1.1 gdamore
614 1.1 gdamore memset(&srv->fdidx[fd], 0, sizeof(srv->fdidx[fd]));
615 1.1 gdamore
616 1.1 gdamore for (provider = provider_get_first();
617 1.1 gdamore provider != NULL;
618 1.1 gdamore provider = provider_next) {
619 1.1 gdamore provider_next = provider_get_next(provider);
620 1.1 gdamore
621 1.1 gdamore if (provider->fd == fd)
622 1.1 gdamore provider_unregister(provider);
623 1.1 gdamore }
624 1.1 gdamore }
625