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: ssar.c,v 1.4 2004/01/12 22:54:31 max Exp $
34 #include <sys/queue.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
42 #include "uuid-private.h"
45 int32_t server_prepare_attr_list(provider_p const provider,
46 uint8_t const *req, uint8_t const * const req_end,
47 uint8_t *rsp, uint8_t const * const rsp_end);
50 * Prepare SDP Service Search Attribute Response
54 server_prepare_service_search_attribute_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;
61 uint8_t const *sspptr = NULL, *aidptr = NULL;
64 provider_t *provider = NULL;
65 int32_t type, rsp_limit, ssplen, aidlen, cslen, cs;
66 uint128_t uuid, puuid;
69 * Minimal Service Search Attribute Request request
72 * uuid16 value16 - 3 bytes ServiceSearchPattern
73 * value16 - 2 bytes MaximumAttributeByteCount
75 * uint16 value16 - 3 bytes AttributeIDList
76 * value8 - 1 byte ContinuationState
79 if (req_end - req < 13)
80 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
82 /* Get size of ServiceSearchPattern */
87 SDP_GET8(ssplen, req);
91 SDP_GET16(ssplen, req);
95 SDP_GET32(ssplen, req);
99 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
104 /* Get MaximumAttributeByteCount */
105 if (req + 2 > req_end)
106 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
108 SDP_GET16(rsp_limit, req);
110 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
112 /* Get size of AttributeIDList */
113 if (req + 1 > req_end)
114 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
120 if (req + 1 > req_end)
121 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
123 SDP_GET8(aidlen, req);
127 if (req + 2 > req_end)
128 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
130 SDP_GET16(aidlen, req);
134 if (req + 4 > req_end)
135 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
137 SDP_GET32(aidlen, req);
141 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
146 /* Get ContinuationState */
147 if (req + 1 > req_end)
148 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
150 SDP_GET8(cslen, req);
152 if (cslen != 2 || req_end - req != 2)
153 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
159 /* Process the request. First, check continuation state */
160 if (srv->fdidx[fd].rsp_cs != cs)
161 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
162 if (srv->fdidx[fd].rsp_size > 0)
166 * Service Search Attribute Response format
168 * value16 - 2 bytes AttributeListByteCount (not incl.)
169 * seq8 len16 - 3 bytes
170 * attr list - 3+ bytes AttributeLists
177 SDP_GET8(type, sspptr);
181 case SDP_DATA_UUID16:
183 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
185 memcpy(&uuid, &uuid_base, sizeof(uuid));
186 uuid.b[2] = *sspptr ++;
187 uuid.b[3] = *sspptr ++;
191 case SDP_DATA_UUID32:
193 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
195 memcpy(&uuid, &uuid_base, sizeof(uuid));
196 uuid.b[0] = *sspptr ++;
197 uuid.b[1] = *sspptr ++;
198 uuid.b[2] = *sspptr ++;
199 uuid.b[3] = *sspptr ++;
203 case SDP_DATA_UUID128:
205 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
207 memcpy(uuid.b, sspptr, 16);
213 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
217 for (provider = provider_get_first();
219 provider = provider_get_next(provider)) {
220 if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
223 memcpy(&puuid, &uuid_base, sizeof(puuid));
224 puuid.b[2] = provider->profile->uuid >> 8;
225 puuid.b[3] = provider->profile->uuid;
227 if (memcmp(&uuid, &puuid, sizeof(uuid)) != 0 &&
228 memcmp(&uuid, &uuid_public_browse_group, sizeof(uuid)) != 0)
231 cs = server_prepare_attr_list(provider,
232 aidptr, aidptr + aidlen, ptr, rsp_end);
234 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
240 /* Set reply size (not counting PDU header and continuation state) */
241 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 2;
242 if (srv->fdidx[fd].rsp_limit > rsp_limit)
243 srv->fdidx[fd].rsp_limit = rsp_limit;
245 srv->fdidx[fd].rsp_size = ptr - rsp;
246 srv->fdidx[fd].rsp_cs = 0;
248 /* Fix AttributeLists sequence header */
250 SDP_PUT8(SDP_DATA_SEQ16, ptr);
251 SDP_PUT16(srv->fdidx[fd].rsp_size - 3, ptr);