4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
32 #include <sys/queue.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
37 #define L2CAP_SOCKET_CHECKED
38 #include <bluetooth.h>
45 #include "uuid-private.h"
48 * Prepare SDP Service Search Response
52 server_prepare_service_search_response(server_p srv, int32_t fd)
54 uint8_t const *req = srv->req + sizeof(sdp_pdu_t);
55 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len;
56 uint8_t *rsp = srv->fdidx[fd].rsp;
57 uint8_t const *rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
60 provider_t *provider = NULL;
61 int32_t type, ssplen, rsp_limit, rcount, cslen, cs;
62 uint128_t uuid, puuid;
65 * Minimal SDP Service Search Request
68 * uuid16 value16 - 3 bytes ServiceSearchPattern
69 * value16 - 2 bytes MaximumServiceRecordCount
70 * value8 - 1 byte ContinuationState
73 if (req_end - req < 8)
74 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
76 /* Get size of ServiceSearchPattern */
81 SDP_GET8(ssplen, req);
85 SDP_GET16(ssplen, req);
89 SDP_GET32(ssplen, req);
93 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
95 ptr = (uint8_t *) req + ssplen;
97 /* Get MaximumServiceRecordCount */
98 if (ptr + 2 > req_end)
99 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
101 SDP_GET16(rsp_limit, ptr);
103 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
105 /* Get ContinuationState */
106 if (ptr + 1 > req_end)
107 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
109 SDP_GET8(cslen, ptr);
111 if (cslen != 2 || req_end - ptr != 2)
112 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
118 /* Process the request. First, check continuation state */
119 if (srv->fdidx[fd].rsp_cs != cs)
120 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
121 if (srv->fdidx[fd].rsp_size > 0)
125 * Service Search Response format
127 * value16 - 2 bytes TotalServiceRecordCount (not incl.)
128 * value16 - 2 bytes CurrentServiceRecordCount (not incl.)
129 * value32 - 4 bytes handle
132 * Calculate how many record handles we can fit
133 * in our reply buffer and adjust rlimit.
137 rcount = (rsp_end - ptr) / 4;
138 if (rcount < rsp_limit)
141 /* Look for the record handles */
142 for (rcount = 0; ssplen > 0 && rcount < rsp_limit; ) {
147 case SDP_DATA_UUID16:
149 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
151 memcpy(&uuid, &uuid_base, sizeof(uuid));
157 case SDP_DATA_UUID32:
159 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
161 memcpy(&uuid, &uuid_base, sizeof(uuid));
169 case SDP_DATA_UUID128:
171 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
173 memcpy(uuid.b, req, 16);
179 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
183 for (provider = provider_get_first();
184 provider != NULL && rcount < rsp_limit;
185 provider = provider_get_next(provider)) {
186 if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
189 memcpy(&puuid, &uuid_base, sizeof(puuid));
190 puuid.b[2] = provider->profile->uuid >> 8;
191 puuid.b[3] = provider->profile->uuid;
193 if (memcmp(&uuid, &puuid, sizeof(uuid)) == 0 ||
194 memcmp(&uuid, &uuid_public_browse_group, sizeof(uuid)) == 0) {
195 SDP_PUT32(provider->handle, ptr);
201 /* Set reply size (not counting PDU header and continuation state) */
202 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 4;
203 srv->fdidx[fd].rsp_size = ptr - rsp;
204 srv->fdidx[fd].rsp_cs = 0;
210 * Send SDP Service Search Response
214 server_send_service_search_response(server_p srv, int32_t fd)
216 uint8_t *rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
217 uint8_t *rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
225 /* First update continuation state (assume we will send all data) */
226 size = rsp_end - rsp;
227 srv->fdidx[fd].rsp_cs += size;
229 if (size + 1 > srv->fdidx[fd].rsp_limit) {
231 * We need to split out response. Add 3 more bytes for the
232 * continuation state and move rsp_end and rsp_cs backwards.
235 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
237 srv->fdidx[fd].rsp_cs -= 4;
241 cs[1] = srv->fdidx[fd].rsp_cs >> 8;
242 cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
246 assert(rsp_end >= rsp);
248 rcounts[0] = srv->fdidx[fd].rsp_size / 4; /* TotalServiceRecordCount */
249 rcounts[1] = (rsp_end - rsp) / 4; /* CurrentServiceRecordCount */
251 pdu.pid = SDP_PDU_SERVICE_SEARCH_RESPONSE;
252 pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
253 pdu.len = htons(sizeof(rcounts) + rcounts[1] * 4 + 1 + cs[0]);
255 rcounts[0] = htons(rcounts[0]);
256 rcounts[1] = htons(rcounts[1]);
258 iov[0].iov_base = &pdu;
259 iov[0].iov_len = sizeof(pdu);
261 iov[1].iov_base = rcounts;
262 iov[1].iov_len = sizeof(rcounts);
264 iov[2].iov_base = rsp;
265 iov[2].iov_len = rsp_end - rsp;
267 iov[3].iov_base = cs;
268 iov[3].iov_len = 1 + cs[0];
271 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
272 } while (size < 0 && errno == EINTR);
274 /* Check if we have sent (or failed to sent) last response chunk */
275 if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
276 srv->fdidx[fd].rsp_cs = 0;
277 srv->fdidx[fd].rsp_size = 0;
278 srv->fdidx[fd].rsp_limit = 0;
281 return ((size < 0)? errno : 0);