xref: /dragonfly/usr.sbin/sdpd/ssar.c (revision 62f7f702)
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.1 2008/01/06 21:51:30 hasso 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 <bluetooth.h>
37 #include <sdp.h>
38 #include <string.h>
39 #include "profile.h"
40 #include "provider.h"
41 #include "server.h"
42 #include "uuid-private.h"
43 
44 /*
45  * Prepare SDP Service Search Attribute Response
46  */
47 
48 int32_t
49 server_prepare_service_search_attribute_response(server_p srv, int32_t fd)
50 {
51 	uint8_t const	*req = srv->req + sizeof(sdp_pdu_t);
52 	uint8_t const	*req_end = req + ((sdp_pdu_p)(srv->req))->len;
53 	uint8_t		*rsp = srv->fdidx[fd].rsp;
54 	uint8_t const	*rsp_end = rsp + L2CAP_MTU_MAXIMUM;
55 
56 	uint8_t const	*aidptr = NULL;
57 
58 	provider_t	*provider = NULL;
59 	int32_t		 type, rsp_limit, ucount, aidlen, cslen, cs;
60 	uint128_t	 ulist[12];
61 
62 	/*
63 	 * Minimal Service Search Attribute Request request
64 	 *
65 	 * seq8 len8		- 2 bytes
66 	 *	uuid16 value16  - 3 bytes ServiceSearchPattern
67 	 * value16		- 2 bytes MaximumAttributeByteCount
68 	 * seq8 len8		- 2 bytes
69 	 *	uint16 value16	- 3 bytes AttributeIDList
70 	 * value8		- 1 byte  ContinuationState
71 	 */
72 
73 	/* Get ServiceSearchPattern */
74 	ucount = server_get_service_search_pattern(&req, req_end, ulist);
75 	if (ucount < 1 || ucount > 12)
76 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
77 
78 	/* Get MaximumAttributeByteCount */
79 	if (req + 2 > req_end)
80 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
81 
82 	SDP_GET16(rsp_limit, req);
83 	if (rsp_limit <= 0)
84 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
85 
86 	/* Get size of AttributeIDList */
87 	if (req + 1 > req_end)
88 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
89 
90 	aidlen = 0;
91 	SDP_GET8(type, req);
92 	switch (type) {
93 	case SDP_DATA_SEQ8:
94 		if (req + 1 > req_end)
95 			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
96 
97 		SDP_GET8(aidlen, req);
98 		break;
99 
100 	case SDP_DATA_SEQ16:
101 		if (req + 2 > req_end)
102 			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
103 
104 		SDP_GET16(aidlen, req);
105 		break;
106 
107 	case SDP_DATA_SEQ32:
108 		if (req + 4 > req_end)
109 			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
110 
111 		SDP_GET32(aidlen, req);
112 		break;
113 	}
114 	if (aidlen <= 0)
115 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
116 
117 	aidptr = req;
118 	req += aidlen;
119 
120 	/* Get ContinuationState */
121 	if (req + 1 > req_end)
122 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
123 
124 	SDP_GET8(cslen, req);
125 	if (cslen == 2 && req + 2 == req_end)
126 		SDP_GET16(cs, req);
127 	else if (cslen == 0 && req == req_end)
128 		cs = 0;
129 	else
130 		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
131 
132 	/* Process the request. First, check continuation state */
133 	if (srv->fdidx[fd].rsp_cs != cs)
134 		return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
135 	if (srv->fdidx[fd].rsp_size > 0)
136 		return (0);
137 
138 	/*
139 	 * Service Search Attribute Response format
140 	 *
141 	 * value16		- 2 bytes  AttributeListByteCount (not incl.)
142 	 * seq8 len16		- 3 bytes
143 	 *	attr list	- 3+ bytes AttributeLists
144 	 *	[ attr list ]
145 	 */
146 
147 	rsp += 3;	/* leave space for sequence header */
148 
149 	for (provider = provider_get_first();
150 	     provider != NULL;
151 	     provider = provider_get_next(provider)) {
152 		if (!provider_match_bdaddr(provider, &srv->req_sa.bt_bdaddr))
153 			continue;
154 
155 		if (!provider_match_uuid(provider, ulist, ucount))
156 			continue;
157 
158 		cs = server_prepare_attr_list(provider,
159 			aidptr, aidptr + aidlen, rsp, rsp_end);
160 		if (cs < 0)
161 			return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
162 
163 		rsp += cs;
164 	}
165 
166 	/* Set reply size (not counting PDU header and continuation state) */
167 	srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 2;
168 	if (srv->fdidx[fd].rsp_limit > rsp_limit)
169 		srv->fdidx[fd].rsp_limit = rsp_limit;
170 
171 	srv->fdidx[fd].rsp_size = rsp - srv->fdidx[fd].rsp;
172 	srv->fdidx[fd].rsp_cs = 0;
173 
174 	/* Fix AttributeLists sequence header */
175 	rsp = srv->fdidx[fd].rsp;
176 	SDP_PUT8(SDP_DATA_SEQ16, rsp);
177 	SDP_PUT16(srv->fdidx[fd].rsp_size - 3, rsp);
178 
179 	return (0);
180 }
181