4 * SPDX-License-Identifier: BSD-2-Clause
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: ssar.c,v 1.4 2004/01/12 22:54:31 max Exp $
33 #include <sys/queue.h>
34 #define L2CAP_SOCKET_CHECKED
35 #include <bluetooth.h>
41 #include "uuid-private.h"
44 int32_t server_prepare_attr_list(provider_p const provider,
45 uint8_t const *req, uint8_t const * const req_end,
46 uint8_t *rsp, uint8_t const * const rsp_end);
49 * Scan an attribute for matching UUID.
52 server_search_uuid_sub(uint8_t *buf, uint8_t const * const eob, const uint128_t *uuid)
66 SDP_GET16(value, buf);
68 memcpy(&duuid, &uuid_base, sizeof(duuid));
69 duuid.b[2] = value >> 8 & 0xff;
70 duuid.b[3] = value & 0xff;
72 if (memcmp(&duuid, uuid, sizeof(duuid)) == 0)
78 SDP_GET32(value, buf);
79 memcpy(&duuid, &uuid_base, sizeof(duuid));
80 duuid.b[0] = value >> 24 & 0xff;
81 duuid.b[1] = value >> 16 & 0xff;
82 duuid.b[2] = value >> 8 & 0xff;
83 duuid.b[3] = value & 0xff;
85 if (memcmp(&duuid, uuid, sizeof(duuid)) == 0)
88 case SDP_DATA_UUID128:
91 SDP_GET_UUID128(&duuid, buf);
93 if (memcmp(&duuid, uuid, sizeof(duuid)) == 0)
101 case SDP_DATA_UINT16:
106 case SDP_DATA_UINT32:
111 case SDP_DATA_UINT64:
115 case SDP_DATA_UINT128:
116 case SDP_DATA_INT128:
122 SDP_GET8(value, buf);
128 SDP_GET16(value, buf);
129 if (value > (eob - buf))
136 SDP_GET32(value, buf);
137 if (value > (eob - buf))
152 * Search a provider for matching UUID in its attributes.
155 server_search_uuid(provider_p const provider, const uint128_t *uuid)
161 for (attr = provider->profile->attrs; attr->create != NULL; attr++) {
163 len = attr->create(buffer, buffer + sizeof(buffer),
164 (const uint8_t *)provider->profile, sizeof(*provider->profile));
167 if (server_search_uuid_sub(buffer, buffer + len, uuid) == 0)
174 * Prepare SDP Service Search Attribute Response
178 server_prepare_service_search_attribute_response(server_p srv, int32_t fd)
180 uint8_t const *req = srv->req + sizeof(sdp_pdu_t);
181 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len;
182 uint8_t *rsp = srv->fdidx[fd].rsp;
183 uint8_t const *rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
185 uint8_t const *sspptr = NULL, *aidptr = NULL;
188 provider_t *provider = NULL;
189 int32_t type, rsp_limit, ssplen, aidlen, cslen, cs;
190 uint128_t uuid, puuid;
193 * Minimal Service Search Attribute Request request
195 * seq8 len8 - 2 bytes
196 * uuid16 value16 - 3 bytes ServiceSearchPattern
197 * value16 - 2 bytes MaximumAttributeByteCount
198 * seq8 len8 - 2 bytes
199 * uint16 value16 - 3 bytes AttributeIDList
200 * value8 - 1 byte ContinuationState
203 if (req_end - req < 13)
204 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
206 /* Get size of ServiceSearchPattern */
211 SDP_GET8(ssplen, req);
215 SDP_GET16(ssplen, req);
219 SDP_GET32(ssplen, req);
223 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
228 /* Get MaximumAttributeByteCount */
229 if (req + 2 > req_end)
230 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
232 SDP_GET16(rsp_limit, req);
234 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
236 /* Get size of AttributeIDList */
237 if (req + 1 > req_end)
238 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
244 if (req + 1 > req_end)
245 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
247 SDP_GET8(aidlen, req);
251 if (req + 2 > req_end)
252 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
254 SDP_GET16(aidlen, req);
258 if (req + 4 > req_end)
259 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
261 SDP_GET32(aidlen, req);
265 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
270 /* Get ContinuationState */
271 if (req + 1 > req_end)
272 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
274 SDP_GET8(cslen, req);
276 if (cslen != 2 || req_end - req != 2)
277 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
283 /* Process the request. First, check continuation state */
284 if (srv->fdidx[fd].rsp_cs != cs)
285 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
286 if (srv->fdidx[fd].rsp_size > 0)
290 * Service Search Attribute Response format
292 * value16 - 2 bytes AttributeListByteCount (not incl.)
293 * seq8 len16 - 3 bytes
294 * attr list - 3+ bytes AttributeLists
301 SDP_GET8(type, sspptr);
305 case SDP_DATA_UUID16:
307 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
309 memcpy(&uuid, &uuid_base, sizeof(uuid));
310 uuid.b[2] = *sspptr ++;
311 uuid.b[3] = *sspptr ++;
315 case SDP_DATA_UUID32:
317 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
319 memcpy(&uuid, &uuid_base, sizeof(uuid));
320 uuid.b[0] = *sspptr ++;
321 uuid.b[1] = *sspptr ++;
322 uuid.b[2] = *sspptr ++;
323 uuid.b[3] = *sspptr ++;
327 case SDP_DATA_UUID128:
329 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
331 memcpy(uuid.b, sspptr, 16);
337 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
341 for (provider = provider_get_first();
343 provider = provider_get_next(provider)) {
344 if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
347 memcpy(&puuid, &uuid_base, sizeof(puuid));
348 puuid.b[2] = provider->profile->uuid >> 8;
349 puuid.b[3] = provider->profile->uuid;
351 if (memcmp(&uuid, &puuid, sizeof(uuid)) != 0 &&
352 memcmp(&uuid, &uuid_public_browse_group, sizeof(uuid)) != 0 &&
353 server_search_uuid(provider, &uuid) != 0)
356 cs = server_prepare_attr_list(provider,
357 aidptr, aidptr + aidlen, ptr, rsp_end);
359 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
365 /* Set reply size (not counting PDU header and continuation state) */
366 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 2;
367 if (srv->fdidx[fd].rsp_limit > rsp_limit)
368 srv->fdidx[fd].rsp_limit = rsp_limit;
370 srv->fdidx[fd].rsp_size = ptr - rsp;
371 srv->fdidx[fd].rsp_cs = 0;
373 /* Fix AttributeLists sequence header */
375 SDP_PUT8(SDP_DATA_SEQ16, ptr);
376 SDP_PUT16(srv->fdidx[fd].rsp_size - 3, ptr);