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: sar.c,v 1.2 2004/01/08 23:46:51 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>
41 #include <stdio.h> /* for NULL */
47 * Prepare SDP attr/value pair. Check if profile implements the attribute
48 * and if so call the attribute value function.
50 * uint16 value16 - 3 bytes (attribute)
51 * value - N bytes (value)
55 server_prepare_attr_value_pair(
56 provider_p const provider, uint16_t attr,
57 uint8_t *buf, uint8_t const * const eob)
59 profile_attr_create_p cf = profile_get_attr(provider->profile, attr);
63 return (0); /* no attribute */
68 SDP_PUT8(SDP_DATA_UINT16, buf);
71 len = cf(buf, eob, (uint8_t const *) provider, sizeof(*provider));
79 * seq16 value16 - 3 bytes
80 * attr value - 3+ bytes
85 server_prepare_attr_list(provider_p const provider,
86 uint8_t const *req, uint8_t const * const req_end,
87 uint8_t *rsp, uint8_t const * const rsp_end)
89 uint8_t *ptr = rsp + 3;
90 int32_t type, hi, lo, len;
95 while (req < req_end) {
100 if (req + 2 > req_end)
107 case SDP_DATA_UINT32:
108 if (req + 4 > req_end)
120 for (; lo <= hi; lo ++) {
121 len = server_prepare_attr_value_pair(provider, lo, ptr, rsp_end);
129 len = ptr - rsp; /* we put this much bytes in rsp */
131 /* Fix SEQ16 header for the rsp */
132 SDP_PUT8(SDP_DATA_SEQ16, rsp);
133 SDP_PUT16(len - 3, rsp);
139 * Prepare SDP Service Attribute Response
143 server_prepare_service_attribute_response(server_p srv, int32_t fd)
145 uint8_t const *req = srv->req + sizeof(sdp_pdu_t);
146 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len;
147 uint8_t *rsp = srv->fdidx[fd].rsp;
148 uint8_t const *rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
151 provider_t *provider = NULL;
153 int32_t type, rsp_limit, aidlen, cslen, cs;
156 * Minimal Service Attribute Request request
158 * value32 - 4 bytes ServiceRecordHandle
159 * value16 - 2 bytes MaximumAttributeByteCount
160 * seq8 len8 - 2 bytes
161 * uint16 value16 - 3 bytes AttributeIDList
162 * value8 - 1 byte ContinuationState
165 if (req_end - req < 12)
166 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
168 /* Get ServiceRecordHandle and MaximumAttributeByteCount */
169 SDP_GET32(handle, req);
170 SDP_GET16(rsp_limit, req);
172 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
174 /* Get size of AttributeIDList */
179 SDP_GET8(aidlen, req);
183 SDP_GET16(aidlen, req);
187 SDP_GET32(aidlen, req);
191 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
193 ptr = (uint8_t *) req + aidlen;
195 /* Get ContinuationState */
196 if (ptr + 1 > req_end)
197 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
199 SDP_GET8(cslen, ptr);
201 if (cslen != 2 || req_end - ptr != 2)
202 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
208 /* Process the request. First, check continuation state */
209 if (srv->fdidx[fd].rsp_cs != cs)
210 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
211 if (srv->fdidx[fd].rsp_size > 0)
214 /* Lookup record handle */
215 if ((provider = provider_by_handle(handle)) == NULL)
216 return (SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE);
219 * Service Attribute Response format
221 * value16 - 2 bytes AttributeListByteCount (not incl.)
222 * seq8 len16 - 3 bytes
223 * attr value - 3+ bytes AttributeList
227 cs = server_prepare_attr_list(provider, req, req+aidlen, rsp, rsp_end);
229 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
231 /* Set reply size (not counting PDU header and continuation state) */
232 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 2;
233 if (srv->fdidx[fd].rsp_limit > rsp_limit)
234 srv->fdidx[fd].rsp_limit = rsp_limit;
236 srv->fdidx[fd].rsp_size = cs;
237 srv->fdidx[fd].rsp_cs = 0;
243 * Send SDP Service [Search] Attribute Response
247 server_send_service_attribute_response(server_p srv, int32_t fd)
249 uint8_t *rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
250 uint8_t *rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
258 /* First update continuation state (assume we will send all data) */
259 size = rsp_end - rsp;
260 srv->fdidx[fd].rsp_cs += size;
262 if (size + 1 > srv->fdidx[fd].rsp_limit) {
264 * We need to split out response. Add 3 more bytes for the
265 * continuation state and move rsp_end and rsp_cs backwards.
268 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
270 srv->fdidx[fd].rsp_cs --;
274 cs[1] = srv->fdidx[fd].rsp_cs >> 8;
275 cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
279 assert(rsp_end >= rsp);
281 bcount = rsp_end - rsp;
283 if (((sdp_pdu_p)(srv->req))->pid == SDP_PDU_SERVICE_ATTRIBUTE_REQUEST)
284 pdu.pid = SDP_PDU_SERVICE_ATTRIBUTE_RESPONSE;
286 pdu.pid = SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_RESPONSE;
288 pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
289 pdu.len = htons(sizeof(bcount) + bcount + 1 + cs[0]);
291 bcount = htons(bcount);
293 iov[0].iov_base = &pdu;
294 iov[0].iov_len = sizeof(pdu);
296 iov[1].iov_base = &bcount;
297 iov[1].iov_len = sizeof(bcount);
299 iov[2].iov_base = rsp;
300 iov[2].iov_len = rsp_end - rsp;
302 iov[3].iov_base = cs;
303 iov[3].iov_len = 1 + cs[0];
306 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
307 } while (size < 0 && errno == EINTR);
309 /* Check if we have sent (or failed to sent) last response chunk */
310 if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
311 srv->fdidx[fd].rsp_cs = 0;
312 srv->fdidx[fd].rsp_size = 0;
313 srv->fdidx[fd].rsp_limit = 0;
316 return ((size < 0)? errno : 0);