4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
34 #include <sys/queue.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
39 #define L2CAP_SOCKET_CHECKED
40 #include <bluetooth.h>
47 #include "uuid-private.h"
50 * Prepare SDP Service Search Response
54 server_prepare_service_search_response(server_p srv, int32_t fd)
56 uint8_t const *req = srv->req + sizeof(sdp_pdu_t);
57 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len;
58 uint8_t *rsp = srv->fdidx[fd].rsp;
59 uint8_t const *rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
62 provider_t *provider = NULL;
63 int32_t type, ssplen, rsp_limit, rcount, cslen, cs;
64 uint128_t uuid, puuid;
67 * Minimal SDP Service Search Request
70 * uuid16 value16 - 3 bytes ServiceSearchPattern
71 * value16 - 2 bytes MaximumServiceRecordCount
72 * value8 - 1 byte ContinuationState
75 if (req_end - req < 8)
76 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
78 /* Get size of ServiceSearchPattern */
83 SDP_GET8(ssplen, req);
87 SDP_GET16(ssplen, req);
91 SDP_GET32(ssplen, req);
95 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
97 ptr = (uint8_t *) req + ssplen;
99 /* Get MaximumServiceRecordCount */
100 if (ptr + 2 > req_end)
101 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
103 SDP_GET16(rsp_limit, ptr);
105 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
107 /* Get ContinuationState */
108 if (ptr + 1 > req_end)
109 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
111 SDP_GET8(cslen, ptr);
113 if (cslen != 2 || req_end - ptr != 2)
114 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
120 /* Process the request. First, check continuation state */
121 if (srv->fdidx[fd].rsp_cs != cs)
122 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
123 if (srv->fdidx[fd].rsp_size > 0)
127 * Service Search Response format
129 * value16 - 2 bytes TotalServiceRecordCount (not incl.)
130 * value16 - 2 bytes CurrentServiceRecordCount (not incl.)
131 * value32 - 4 bytes handle
134 * Calculate how many record handles we can fit
135 * in our reply buffer and adjust rlimit.
139 rcount = (rsp_end - ptr) / 4;
140 if (rcount < rsp_limit)
143 /* Look for the record handles */
144 for (rcount = 0; ssplen > 0 && rcount < rsp_limit; ) {
149 case SDP_DATA_UUID16:
151 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
153 memcpy(&uuid, &uuid_base, sizeof(uuid));
159 case SDP_DATA_UUID32:
161 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
163 memcpy(&uuid, &uuid_base, sizeof(uuid));
171 case SDP_DATA_UUID128:
173 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
175 memcpy(uuid.b, req, 16);
181 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
185 for (provider = provider_get_first();
186 provider != NULL && rcount < rsp_limit;
187 provider = provider_get_next(provider)) {
188 if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
191 memcpy(&puuid, &uuid_base, sizeof(puuid));
192 puuid.b[2] = provider->profile->uuid >> 8;
193 puuid.b[3] = provider->profile->uuid;
195 if (memcmp(&uuid, &puuid, sizeof(uuid)) == 0 ||
196 memcmp(&uuid, &uuid_public_browse_group, sizeof(uuid)) == 0) {
197 SDP_PUT32(provider->handle, ptr);
203 /* Set reply size (not counting PDU header and continuation state) */
204 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 4;
205 srv->fdidx[fd].rsp_size = ptr - rsp;
206 srv->fdidx[fd].rsp_cs = 0;
212 * Send SDP Service Search Response
216 server_send_service_search_response(server_p srv, int32_t fd)
218 uint8_t *rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
219 uint8_t *rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
227 /* First update continuation state (assume we will send all data) */
228 size = rsp_end - rsp;
229 srv->fdidx[fd].rsp_cs += size;
231 if (size + 1 > srv->fdidx[fd].rsp_limit) {
233 * We need to split out response. Add 3 more bytes for the
234 * continuation state and move rsp_end and rsp_cs backwards.
237 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
239 srv->fdidx[fd].rsp_cs -= 4;
243 cs[1] = srv->fdidx[fd].rsp_cs >> 8;
244 cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
248 assert(rsp_end >= rsp);
250 rcounts[0] = srv->fdidx[fd].rsp_size / 4; /* TotalServiceRecordCount */
251 rcounts[1] = (rsp_end - rsp) / 4; /* CurrentServiceRecordCount */
253 pdu.pid = SDP_PDU_SERVICE_SEARCH_RESPONSE;
254 pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
255 pdu.len = htons(sizeof(rcounts) + rcounts[1] * 4 + 1 + cs[0]);
257 rcounts[0] = htons(rcounts[0]);
258 rcounts[1] = htons(rcounts[1]);
260 iov[0].iov_base = &pdu;
261 iov[0].iov_len = sizeof(pdu);
263 iov[1].iov_base = rcounts;
264 iov[1].iov_len = sizeof(rcounts);
266 iov[2].iov_base = rsp;
267 iov[2].iov_len = rsp_end - rsp;
269 iov[3].iov_base = cs;
270 iov[3].iov_len = 1 + cs[0];
273 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
274 } while (size < 0 && errno == EINTR);
276 /* Check if we have sent (or failed to sent) last response chunk */
277 if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
278 srv->fdidx[fd].rsp_cs = 0;
279 srv->fdidx[fd].rsp_size = 0;
280 srv->fdidx[fd].rsp_limit = 0;
283 return ((size < 0)? errno : 0);