1 /* 2 * sar.c 3 * 4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * $Id: sar.c,v 1.2 2004/01/08 23:46:51 max Exp $ 29 * $FreeBSD$ 30 */ 31 32 #include <sys/queue.h> 33 #include <sys/uio.h> 34 #include <netinet/in.h> 35 #include <arpa/inet.h> 36 #include <assert.h> 37 #define L2CAP_SOCKET_CHECKED 38 #include <bluetooth.h> 39 #include <errno.h> 40 #include <sdp.h> 41 #include <stdio.h> /* for NULL */ 42 #include "profile.h" 43 #include "provider.h" 44 #include "server.h" 45 46 /* 47 * Prepare SDP attr/value pair. Check if profile implements the attribute 48 * and if so call the attribute value function. 49 * 50 * uint16 value16 - 3 bytes (attribute) 51 * value - N bytes (value) 52 */ 53 54 static int32_t 55 server_prepare_attr_value_pair( 56 provider_p const provider, uint16_t attr, 57 uint8_t *buf, uint8_t const * const eob) 58 { 59 profile_attr_create_p cf = profile_get_attr(provider->profile, attr); 60 int32_t len; 61 62 if (cf == NULL) 63 return (0); /* no attribute */ 64 65 if (buf + 3 > eob) 66 return (-1); 67 68 SDP_PUT8(SDP_DATA_UINT16, buf); 69 SDP_PUT16(attr, buf); 70 71 len = cf(buf, eob, (uint8_t const *) provider, sizeof(*provider)); 72 if (len < 0) 73 return (-1); 74 75 return (3 + len); 76 } 77 78 /* 79 * seq16 value16 - 3 bytes 80 * attr value - 3+ bytes 81 * [ attr value ] 82 */ 83 84 int32_t 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) 88 { 89 uint8_t *ptr = rsp + 3; 90 int32_t type, hi, lo, len; 91 92 if (ptr > rsp_end) 93 return (-1); 94 95 while (req < req_end) { 96 SDP_GET8(type, req); 97 98 switch (type) { 99 case SDP_DATA_UINT16: 100 if (req + 2 > req_end) 101 return (-1); 102 103 SDP_GET16(lo, req); 104 hi = lo; 105 break; 106 107 case SDP_DATA_UINT32: 108 if (req + 4 > req_end) 109 return (-1); 110 111 SDP_GET16(lo, req); 112 SDP_GET16(hi, req); 113 break; 114 115 default: 116 return (-1); 117 /* NOT REACHED */ 118 } 119 120 for (; lo <= hi; lo ++) { 121 len = server_prepare_attr_value_pair(provider, lo, ptr, rsp_end); 122 if (len < 0) 123 return (-1); 124 125 ptr += len; 126 } 127 } 128 129 len = ptr - rsp; /* we put this much bytes in rsp */ 130 131 /* Fix SEQ16 header for the rsp */ 132 SDP_PUT8(SDP_DATA_SEQ16, rsp); 133 SDP_PUT16(len - 3, rsp); 134 135 return (len); 136 } 137 138 /* 139 * Prepare SDP Service Attribute Response 140 */ 141 142 int32_t 143 server_prepare_service_attribute_response(server_p srv, int32_t fd) 144 { 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; 149 150 uint8_t *ptr = NULL; 151 provider_t *provider = NULL; 152 uint32_t handle; 153 int32_t type, rsp_limit, aidlen, cslen, cs; 154 155 /* 156 * Minimal Service Attribute Request request 157 * 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 163 */ 164 165 if (req_end - req < 12) 166 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 167 168 /* Get ServiceRecordHandle and MaximumAttributeByteCount */ 169 SDP_GET32(handle, req); 170 SDP_GET16(rsp_limit, req); 171 if (rsp_limit <= 0) 172 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 173 174 /* Get size of AttributeIDList */ 175 aidlen = 0; 176 SDP_GET8(type, req); 177 switch (type) { 178 case SDP_DATA_SEQ8: 179 SDP_GET8(aidlen, req); 180 break; 181 182 case SDP_DATA_SEQ16: 183 SDP_GET16(aidlen, req); 184 break; 185 186 case SDP_DATA_SEQ32: 187 SDP_GET32(aidlen, req); 188 break; 189 } 190 if (aidlen <= 0) 191 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 192 193 ptr = (uint8_t *) req + aidlen; 194 195 /* Get ContinuationState */ 196 if (ptr + 1 > req_end) 197 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 198 199 SDP_GET8(cslen, ptr); 200 if (cslen != 0) { 201 if (cslen != 2 || req_end - ptr != 2) 202 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 203 204 SDP_GET16(cs, ptr); 205 } else 206 cs = 0; 207 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) 212 return (0); 213 214 /* Lookup record handle */ 215 if ((provider = provider_by_handle(handle)) == NULL) 216 return (SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE); 217 218 /* 219 * Service Attribute Response format 220 * 221 * value16 - 2 bytes AttributeListByteCount (not incl.) 222 * seq8 len16 - 3 bytes 223 * attr value - 3+ bytes AttributeList 224 * [ attr value ] 225 */ 226 227 cs = server_prepare_attr_list(provider, req, req+aidlen, rsp, rsp_end); 228 if (cs < 0) 229 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES); 230 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; 235 236 srv->fdidx[fd].rsp_size = cs; 237 srv->fdidx[fd].rsp_cs = 0; 238 239 return (0); 240 } 241 242 /* 243 * Send SDP Service [Search] Attribute Response 244 */ 245 246 int32_t 247 server_send_service_attribute_response(server_p srv, int32_t fd) 248 { 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; 251 252 struct iovec iov[4]; 253 sdp_pdu_t pdu; 254 uint16_t bcount; 255 uint8_t cs[3]; 256 int32_t size; 257 258 /* First update continuation state (assume we will send all data) */ 259 size = rsp_end - rsp; 260 srv->fdidx[fd].rsp_cs += size; 261 262 if (size + 1 > srv->fdidx[fd].rsp_limit) { 263 /* 264 * We need to split out response. Add 3 more bytes for the 265 * continuation state and move rsp_end and rsp_cs backwards. 266 */ 267 268 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) { 269 rsp_end --; 270 srv->fdidx[fd].rsp_cs --; 271 } 272 273 cs[0] = 2; 274 cs[1] = srv->fdidx[fd].rsp_cs >> 8; 275 cs[2] = srv->fdidx[fd].rsp_cs & 0xff; 276 } else 277 cs[0] = 0; 278 279 assert(rsp_end >= rsp); 280 281 bcount = rsp_end - rsp; 282 283 if (((sdp_pdu_p)(srv->req))->pid == SDP_PDU_SERVICE_ATTRIBUTE_REQUEST) 284 pdu.pid = SDP_PDU_SERVICE_ATTRIBUTE_RESPONSE; 285 else 286 pdu.pid = SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_RESPONSE; 287 288 pdu.tid = ((sdp_pdu_p)(srv->req))->tid; 289 pdu.len = htons(sizeof(bcount) + bcount + 1 + cs[0]); 290 291 bcount = htons(bcount); 292 293 iov[0].iov_base = &pdu; 294 iov[0].iov_len = sizeof(pdu); 295 296 iov[1].iov_base = &bcount; 297 iov[1].iov_len = sizeof(bcount); 298 299 iov[2].iov_base = rsp; 300 iov[2].iov_len = rsp_end - rsp; 301 302 iov[3].iov_base = cs; 303 iov[3].iov_len = 1 + cs[0]; 304 305 do { 306 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0])); 307 } while (size < 0 && errno == EINTR); 308 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; 314 } 315 316 return ((size < 0)? errno : 0); 317 } 318 319