1 /* $NetBSD: sar.c,v 1.1 2006/06/19 15:44:56 gdamore Exp $ */
2 /* $DragonFly: src/usr.sbin/sdpd/sar.c,v 1.1 2008/01/06 21:51:30 hasso Exp $ */
7 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $Id: sar.c,v 1.2 2007/11/30 07:39:37 griffin Exp $
32 * $FreeBSD: src/usr.sbin/bluetooth/sdpd/sar.c,v 1.2 2004/02/26 20:44:55 emax Exp $
35 #include <sys/queue.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
40 #include <bluetooth.h>
43 #include <stdio.h> /* for NULL */
49 * Prepare SDP attr/value pair. Check if profile implements the attribute
50 * and if so call the attribute value function.
52 * uint16 value16 - 3 bytes (attribute)
53 * value - N bytes (value)
57 server_prepare_attr_value_pair(
58 provider_p const provider, uint16_t attr,
59 uint8_t *buf, uint8_t const * const eob)
61 profile_attr_create_p cf = profile_get_attr(provider->profile, attr);
65 return (0); /* no attribute */
70 SDP_PUT8(SDP_DATA_UINT16, buf);
73 len = cf(buf, eob, (uint8_t const *) provider, sizeof(*provider));
81 * seq16 value16 - 3 bytes
82 * attr value - 3+ bytes
87 server_prepare_attr_list(provider_p const provider,
88 uint8_t const *req, uint8_t const * const req_end,
89 uint8_t *rsp, uint8_t const * const rsp_end)
91 uint8_t *ptr = rsp + 3;
92 int32_t type, hi, lo, len;
97 while (req < req_end) {
101 case SDP_DATA_UINT16:
102 if (req + 2 > req_end)
109 case SDP_DATA_UINT32:
110 if (req + 4 > req_end)
122 for (; lo <= hi; lo ++) {
123 len = server_prepare_attr_value_pair(provider, lo, ptr, rsp_end);
131 len = ptr - rsp; /* we put this much bytes in rsp */
133 /* Fix SEQ16 header for the rsp */
134 SDP_PUT8(SDP_DATA_SEQ16, rsp);
135 SDP_PUT16(len - 3, rsp);
141 * Prepare SDP Service Attribute Response
145 server_prepare_service_attribute_response(server_p srv, int32_t fd)
147 uint8_t const *req = srv->req + sizeof(sdp_pdu_t);
148 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len;
149 uint8_t *rsp = srv->fdidx[fd].rsp;
150 uint8_t const *rsp_end = rsp + L2CAP_MTU_MAXIMUM;
152 uint8_t const *ptr = NULL;
153 provider_t *provider = NULL;
155 int32_t type, rsp_limit, aidlen, cslen, cs;
158 * Minimal Service Attribute Request request
160 * value32 - 4 bytes ServiceRecordHandle
161 * value16 - 2 bytes MaximumAttributeByteCount
162 * seq8 len8 - 2 bytes
163 * uint16 value16 - 3 bytes AttributeIDList
164 * value8 - 1 byte ContinuationState
167 if (req_end - req < 12)
168 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
170 /* Get ServiceRecordHandle and MaximumAttributeByteCount */
171 SDP_GET32(handle, req);
172 SDP_GET16(rsp_limit, req);
174 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
176 /* Get size of AttributeIDList */
181 SDP_GET8(aidlen, req);
185 SDP_GET16(aidlen, req);
189 SDP_GET32(aidlen, req);
193 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
195 ptr = (uint8_t const *) req + aidlen;
197 /* Get ContinuationState */
198 if (ptr + 1 > req_end)
199 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
201 SDP_GET8(cslen, ptr);
203 if (cslen != 2 || req_end - ptr != 2)
204 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
210 /* Process the request. First, check continuation state */
211 if (srv->fdidx[fd].rsp_cs != cs)
212 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
213 if (srv->fdidx[fd].rsp_size > 0)
216 /* Lookup record handle */
217 if ((provider = provider_by_handle(handle)) == NULL)
218 return (SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE);
221 * Service Attribute Response format
223 * value16 - 2 bytes AttributeListByteCount (not incl.)
224 * seq8 len16 - 3 bytes
225 * attr value - 3+ bytes AttributeList
229 cs = server_prepare_attr_list(provider, req, req+aidlen, rsp, rsp_end);
231 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
233 /* Set reply size (not counting PDU header and continuation state) */
234 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 2;
235 if (srv->fdidx[fd].rsp_limit > rsp_limit)
236 srv->fdidx[fd].rsp_limit = rsp_limit;
238 srv->fdidx[fd].rsp_size = cs;
239 srv->fdidx[fd].rsp_cs = 0;
245 * Send SDP Service [Search] Attribute Response
249 server_send_service_attribute_response(server_p srv, int32_t fd)
251 uint8_t *rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
252 uint8_t *rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
260 /* First update continuation state (assume we will send all data) */
261 size = rsp_end - rsp;
262 srv->fdidx[fd].rsp_cs += size;
264 if (size + 1 > srv->fdidx[fd].rsp_limit) {
266 * We need to split out response. Add 3 more bytes for the
267 * continuation state and move rsp_end and rsp_cs backwards.
270 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
272 srv->fdidx[fd].rsp_cs --;
276 cs[1] = srv->fdidx[fd].rsp_cs >> 8;
277 cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
281 assert(rsp_end >= rsp);
283 bcount = rsp_end - rsp;
285 if (((sdp_pdu_p)(srv->req))->pid == SDP_PDU_SERVICE_ATTRIBUTE_REQUEST)
286 pdu.pid = SDP_PDU_SERVICE_ATTRIBUTE_RESPONSE;
288 pdu.pid = SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_RESPONSE;
290 pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
291 pdu.len = htons(sizeof(bcount) + bcount + 1 + cs[0]);
293 bcount = htons(bcount);
295 iov[0].iov_base = &pdu;
296 iov[0].iov_len = sizeof(pdu);
298 iov[1].iov_base = &bcount;
299 iov[1].iov_len = sizeof(bcount);
301 iov[2].iov_base = rsp;
302 iov[2].iov_len = rsp_end - rsp;
304 iov[3].iov_base = cs;
305 iov[3].iov_len = 1 + cs[0];
308 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
309 } while (size < 0 && errno == EINTR);
311 /* Check if we have sent (or failed to sent) last response chunk */
312 if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
313 srv->fdidx[fd].rsp_cs = 0;
314 srv->fdidx[fd].rsp_size = 0;
315 srv->fdidx[fd].rsp_limit = 0;
318 return ((size < 0)? errno : 0);