4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2001-2003 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: search.c,v 1.2 2003/09/04 22:12:13 max Exp $
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #define L2CAP_SOCKET_CHECKED
37 #include <bluetooth.h>
49 uint32_t plen, uint16_t const *pp,
50 uint32_t alen, uint32_t const *ap,
51 uint32_t vlen, sdp_attr_t *vp)
56 } __attribute__ ((packed)) xpdu;
58 sdp_session_p ss = (sdp_session_p) xss;
59 uint8_t *req = NULL, *rsp = NULL, *rsp_tmp = NULL;
66 if (ss->req == NULL || ss->rsp == NULL ||
67 plen == 0 || pp == NULL || alen == 0 || ap == NULL) {
74 /* Calculate ServiceSearchPattern length */
75 plen = plen * (sizeof(pp[0]) + 1);
77 /* Calculate AttributeIDList length */
78 for (len = 0, t = 0; t < alen; t ++) {
79 lo = (uint16_t) (ap[t] >> 16);
80 hi = (uint16_t) (ap[t]);
88 len += (sizeof(ap[t]) + 1);
90 len += (sizeof(lo) + 1);
94 /* Calculate length of the request */
95 len = plen + sizeof(uint8_t) + sizeof(uint16_t) +
96 /* ServiceSearchPattern */
98 /* MaximumAttributeByteCount */
99 alen + sizeof(uint8_t) + sizeof(uint16_t);
100 /* AttributeIDList */
102 if (ss->req_e - req < len) {
107 /* Put ServiceSearchPattern */
108 SDP_PUT8(SDP_DATA_SEQ16, req);
109 SDP_PUT16(plen, req);
110 for (; plen > 0; pp ++, plen -= (sizeof(pp[0]) + 1)) {
111 SDP_PUT8(SDP_DATA_UUID16, req);
115 /* Put MaximumAttributeByteCount */
116 SDP_PUT16(0xffff, req);
118 /* Put AttributeIDList */
119 SDP_PUT8(SDP_DATA_SEQ16, req);
120 SDP_PUT16(alen, req);
121 for (; alen > 0; ap ++) {
122 lo = (uint16_t) (*ap >> 16);
123 hi = (uint16_t) (*ap);
126 /* Put attribute range */
127 SDP_PUT8(SDP_DATA_UINT32, req);
129 alen -= (sizeof(ap[0]) + 1);
132 SDP_PUT8(SDP_DATA_UINT16, req);
134 alen -= (sizeof(lo) + 1);
138 /* Submit ServiceSearchAttributeRequest and wait for response */
144 uint8_t *req_cs = req;
146 /* Add continuation state (if any) */
147 if (ss->req_e - req_cs < ss->cslen + 1) {
152 SDP_PUT8(ss->cslen, req_cs);
154 memcpy(req_cs, ss->cs, ss->cslen);
158 /* Prepare SDP PDU header */
159 xpdu.pdu.pid = SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST;
160 xpdu.pdu.tid = htons(ss->tid);
161 xpdu.pdu.len = htons(req_cs - ss->req);
164 iov[0].iov_base = (void *) &xpdu;
165 iov[0].iov_len = sizeof(xpdu.pdu);
166 iov[1].iov_base = (void *) ss->req;
167 iov[1].iov_len = req_cs - ss->req;
170 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
171 } while (len < 0 && errno == EINTR);
179 iov[0].iov_base = (void *) &xpdu;
180 iov[0].iov_len = sizeof(xpdu);
181 iov[1].iov_base = (void *) rsp;
182 iov[1].iov_len = ss->imtu;
185 len = readv(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
186 } while (len < 0 && errno == EINTR);
192 if (len < sizeof(xpdu)) {
197 xpdu.pdu.tid = ntohs(xpdu.pdu.tid);
198 xpdu.pdu.len = ntohs(xpdu.pdu.len);
199 xpdu.len = ntohs(xpdu.len);
201 if (xpdu.pdu.pid == SDP_PDU_ERROR_RESPONSE ||
202 xpdu.pdu.tid != ss->tid ||
203 xpdu.pdu.len > len ||
204 xpdu.len > xpdu.pdu.len) {
212 /* Save continuation state (if any) */
215 if (ss->cslen > sizeof(ss->cs)) {
220 memcpy(ss->cs, rsp + 1, ss->cslen);
223 * Ensure that we always have ss->imtu bytes
224 * available in the ss->rsp buffer
227 if (ss->rsp_e - rsp <= ss->imtu) {
228 uint32_t size, offset;
230 size = ss->rsp_e - ss->rsp + ss->imtu;
231 offset = rsp - ss->rsp;
233 rsp_tmp = realloc(ss->rsp, size);
234 if (rsp_tmp == NULL) {
240 ss->rsp_e = ss->rsp + size;
241 rsp = ss->rsp + offset;
244 } while (ss->cslen > 0);
247 * If we got here then we have completed SDP transaction and now
248 * we must populate attribute values into vp array. At this point
249 * ss->rsp points to the beginning of the response and rsp points
250 * to the end of the response.
252 * From Bluetooth v1.1 spec page 364
254 * The AttributeLists is a data element sequence where each element
255 * in turn is a data element sequence representing an attribute list.
256 * Each attribute list contains attribute IDs and attribute values
257 * from one service record. The first element in each attribute list
258 * contains the attribute ID of the first attribute to be returned for
259 * that service record. The second element in each attribute list
260 * contains the corresponding attribute value. Successive pairs of
261 * elements in each attribute list contain additional attribute ID
262 * and value pairs. Only attributes that have non-null values within
263 * the service record and whose attribute IDs were specified in the
264 * SDP_ServiceSearchAttributeRequest are contained in the AttributeLists
265 * Neither an attribute ID nor attribute value is placed in
266 * AttributeLists for attributes in the service record that have no
267 * value. Within each attribute list, the attributes are listed in
268 * ascending order of attribute ID value.
276 /* Skip the first SEQ */
277 SDP_GET8(t, rsp_tmp);
280 SDP_GET8(len, rsp_tmp);
284 SDP_GET16(len, rsp_tmp);
288 SDP_GET32(len, rsp_tmp);
297 for (; rsp_tmp < rsp && vlen > 0; ) {
298 /* Get set of attributes for the next record */
299 SDP_GET8(t, rsp_tmp);
302 SDP_GET8(len, rsp_tmp);
306 SDP_GET16(len, rsp_tmp);
310 SDP_GET32(len, rsp_tmp);
319 /* Now rsp_tmp points to list of (attr,value) pairs */
320 for (; len > 0 && vlen > 0; vp ++, vlen --) {
322 SDP_GET8(t, rsp_tmp);
323 if (t != SDP_DATA_UINT16) {
327 SDP_GET16(vp->attr, rsp_tmp);
329 /* Attribute value */
330 switch (rsp_tmp[0]) {
338 alen = sizeof(uint8_t);
341 case SDP_DATA_UINT16:
343 case SDP_DATA_UUID16:
344 alen = sizeof(uint16_t);
347 case SDP_DATA_UINT32:
349 case SDP_DATA_UUID32:
350 alen = sizeof(uint32_t);
353 case SDP_DATA_UINT64:
355 alen = sizeof(uint64_t);
358 case SDP_DATA_UINT128:
359 case SDP_DATA_INT128:
360 case SDP_DATA_UUID128:
361 alen = sizeof(uint128_t);
368 alen = rsp_tmp[1] + sizeof(uint8_t);
375 alen = ((uint16_t)rsp_tmp[1] << 8)
376 | ((uint16_t)rsp_tmp[2]);
377 alen += sizeof(uint16_t);
384 alen = ((uint32_t)rsp_tmp[1] << 24)
385 | ((uint32_t)rsp_tmp[2] << 16)
386 | ((uint32_t)rsp_tmp[3] << 8)
387 | ((uint32_t)rsp_tmp[4]);
388 alen += sizeof(uint32_t);
397 alen += sizeof(uint8_t);
399 if (vp->value != NULL) {
400 if (alen <= vp->vlen) {
401 vp->flags = SDP_ATTR_OK;
404 vp->flags = SDP_ATTR_TRUNCATED;
406 memcpy(vp->value, rsp_tmp, vp->vlen);
408 vp->flags = SDP_ATTR_INVALID;
411 sizeof(uint8_t) + sizeof(uint16_t) +