1 /* $NetBSD: ssar.c,v 1.2 2007/11/09 20:08:41 plunky Exp $ */ 2 /* $DragonFly: src/usr.sbin/sdpd/ssar.c,v 1.2 2008/05/19 10:19:49 corecode Exp $ */ 3 4 /* 5 * ssar.c 6 * 7 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 * 31 * $Id: ssar.c,v 1.2 2007/11/30 07:39:37 griffin Exp $ 32 * $FreeBSD: src/usr.sbin/bluetooth/sdpd/ssar.c,v 1.2 2005/01/05 18:37:37 emax Exp $ 33 */ 34 35 #include <sys/queue.h> 36 #include <sys/select.h> 37 #include <bluetooth.h> 38 #include <sdp.h> 39 #include <string.h> 40 #include "profile.h" 41 #include "provider.h" 42 #include "server.h" 43 #include "uuid-private.h" 44 45 /* 46 * Prepare SDP Service Search Attribute Response 47 */ 48 49 int32_t 50 server_prepare_service_search_attribute_response(server_p srv, int32_t fd) 51 { 52 uint8_t const *req = srv->req + sizeof(sdp_pdu_t); 53 uint8_t const *req_end = req + ((sdp_pdu_p)(srv->req))->len; 54 uint8_t *rsp = srv->fdidx[fd].rsp; 55 uint8_t const *rsp_end = rsp + L2CAP_MTU_MAXIMUM; 56 57 uint8_t const *aidptr = NULL; 58 59 provider_t *provider = NULL; 60 int32_t type, rsp_limit, ucount, aidlen, cslen, cs; 61 uint128_t ulist[12]; 62 63 /* 64 * Minimal Service Search Attribute Request request 65 * 66 * seq8 len8 - 2 bytes 67 * uuid16 value16 - 3 bytes ServiceSearchPattern 68 * value16 - 2 bytes MaximumAttributeByteCount 69 * seq8 len8 - 2 bytes 70 * uint16 value16 - 3 bytes AttributeIDList 71 * value8 - 1 byte ContinuationState 72 */ 73 74 /* Get ServiceSearchPattern */ 75 ucount = server_get_service_search_pattern(&req, req_end, ulist); 76 if (ucount < 1 || ucount > 12) 77 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 78 79 /* Get MaximumAttributeByteCount */ 80 if (req + 2 > req_end) 81 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 82 83 SDP_GET16(rsp_limit, req); 84 if (rsp_limit <= 0) 85 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 86 87 /* Get size of AttributeIDList */ 88 if (req + 1 > req_end) 89 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 90 91 aidlen = 0; 92 SDP_GET8(type, req); 93 switch (type) { 94 case SDP_DATA_SEQ8: 95 if (req + 1 > req_end) 96 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 97 98 SDP_GET8(aidlen, req); 99 break; 100 101 case SDP_DATA_SEQ16: 102 if (req + 2 > req_end) 103 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 104 105 SDP_GET16(aidlen, req); 106 break; 107 108 case SDP_DATA_SEQ32: 109 if (req + 4 > req_end) 110 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 111 112 SDP_GET32(aidlen, req); 113 break; 114 } 115 if (aidlen <= 0) 116 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 117 118 aidptr = req; 119 req += aidlen; 120 121 /* Get ContinuationState */ 122 if (req + 1 > req_end) 123 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 124 125 SDP_GET8(cslen, req); 126 if (cslen == 2 && req + 2 == req_end) 127 SDP_GET16(cs, req); 128 else if (cslen == 0 && req == req_end) 129 cs = 0; 130 else 131 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX); 132 133 /* Process the request. First, check continuation state */ 134 if (srv->fdidx[fd].rsp_cs != cs) 135 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE); 136 if (srv->fdidx[fd].rsp_size > 0) 137 return (0); 138 139 /* 140 * Service Search Attribute Response format 141 * 142 * value16 - 2 bytes AttributeListByteCount (not incl.) 143 * seq8 len16 - 3 bytes 144 * attr list - 3+ bytes AttributeLists 145 * [ attr list ] 146 */ 147 148 rsp += 3; /* leave space for sequence header */ 149 150 for (provider = provider_get_first(); 151 provider != NULL; 152 provider = provider_get_next(provider)) { 153 if (!provider_match_bdaddr(provider, &srv->req_sa.bt_bdaddr)) 154 continue; 155 156 if (!provider_match_uuid(provider, ulist, ucount)) 157 continue; 158 159 cs = server_prepare_attr_list(provider, 160 aidptr, aidptr + aidlen, rsp, rsp_end); 161 if (cs < 0) 162 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES); 163 164 rsp += cs; 165 } 166 167 /* Set reply size (not counting PDU header and continuation state) */ 168 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 2; 169 if (srv->fdidx[fd].rsp_limit > rsp_limit) 170 srv->fdidx[fd].rsp_limit = rsp_limit; 171 172 srv->fdidx[fd].rsp_size = rsp - srv->fdidx[fd].rsp; 173 srv->fdidx[fd].rsp_cs = 0; 174 175 /* Fix AttributeLists sequence header */ 176 rsp = srv->fdidx[fd].rsp; 177 SDP_PUT8(SDP_DATA_SEQ16, rsp); 178 SDP_PUT16(srv->fdidx[fd].rsp_size - 3, rsp); 179 180 return (0); 181 } 182