1124758Semax/*
2124758Semax * ssr.c
3124758Semax *
4124758Semax * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5124758Semax * All rights reserved.
6124758Semax *
7124758Semax * Redistribution and use in source and binary forms, with or without
8124758Semax * modification, are permitted provided that the following conditions
9124758Semax * are met:
10124758Semax * 1. Redistributions of source code must retain the above copyright
11124758Semax *    notice, this list of conditions and the following disclaimer.
12124758Semax * 2. Redistributions in binary form must reproduce the above copyright
13124758Semax *    notice, this list of conditions and the following disclaimer in the
14124758Semax *    documentation and/or other materials provided with the distribution.
15124758Semax *
16124758Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17124758Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18124758Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19124758Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20124758Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21124758Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22124758Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23124758Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24124758Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25124758Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26124758Semax * SUCH DAMAGE.
27124758Semax *
28124758Semax * $Id: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
29124758Semax * $FreeBSD$
30124758Semax */
31124758Semax
32124758Semax#include <sys/queue.h>
33124758Semax#include <sys/uio.h>
34124758Semax#include <netinet/in.h>
35124758Semax#include <arpa/inet.h>
36124758Semax#include <assert.h>
37124758Semax#include <bluetooth.h>
38124758Semax#include <errno.h>
39124758Semax#include <sdp.h>
40124758Semax#include <string.h>
41124758Semax#include "profile.h"
42124758Semax#include "provider.h"
43124758Semax#include "server.h"
44139721Semax#include "uuid-private.h"
45124758Semax
46124758Semax/*
47124758Semax * Prepare SDP Service Search Response
48124758Semax */
49124758Semax
50124758Semaxint32_t
51124758Semaxserver_prepare_service_search_response(server_p srv, int32_t fd)
52124758Semax{
53124758Semax	uint8_t const	*req = srv->req + sizeof(sdp_pdu_t);
54124758Semax	uint8_t const	*req_end = req + ((sdp_pdu_p)(srv->req))->len;
55124758Semax	uint8_t		*rsp = srv->fdidx[fd].rsp;
56124758Semax	uint8_t const	*rsp_end = rsp + NG_L2CAP_MTU_MAXIMUM;
57124758Semax
58124758Semax	uint8_t		*ptr = NULL;
59124758Semax	provider_t	*provider = NULL;
60139721Semax	int32_t		 type, ssplen, rsp_limit, rcount, cslen, cs;
61139721Semax	uint128_t	 uuid, puuid;
62124758Semax
63124758Semax	/*
64124758Semax	 * Minimal SDP Service Search Request
65124758Semax	 *
66124758Semax	 * seq8 len8		- 2 bytes
67124758Semax	 *	uuid16 value16	- 3 bytes ServiceSearchPattern
68124758Semax	 * value16		- 2 bytes MaximumServiceRecordCount
69124758Semax	 * value8		- 1 byte  ContinuationState
70124758Semax	 */
71124758Semax
72124758Semax	if (req_end - req < 8)
73124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
74124758Semax
75124758Semax	/* Get size of ServiceSearchPattern */
76124758Semax	ssplen = 0;
77124758Semax	SDP_GET8(type, req);
78124758Semax	switch (type) {
79124758Semax	case SDP_DATA_SEQ8:
80124758Semax		SDP_GET8(ssplen, req);
81124758Semax		break;
82124758Semax
83124758Semax	case SDP_DATA_SEQ16:
84124758Semax		SDP_GET16(ssplen, req);
85124758Semax		break;
86124758Semax
87124758Semax	case SDP_DATA_SEQ32:
88124758Semax		SDP_GET32(ssplen, req);
89124758Semax		break;
90124758Semax	}
91124758Semax	if (ssplen <= 0)
92124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
93124758Semax
94124758Semax	ptr = (uint8_t *) req + ssplen;
95124758Semax
96124758Semax	/* Get MaximumServiceRecordCount */
97124758Semax	if (ptr + 2 > req_end)
98124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
99124758Semax
100124758Semax	SDP_GET16(rsp_limit, ptr);
101124758Semax	if (rsp_limit <= 0)
102124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
103124758Semax
104124758Semax	/* Get ContinuationState */
105124758Semax	if (ptr + 1 > req_end)
106124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
107124758Semax
108124758Semax	SDP_GET8(cslen, ptr);
109124758Semax	if (cslen != 0) {
110124758Semax		if (cslen != 2 || req_end - ptr != 2)
111124758Semax			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
112124758Semax
113124758Semax		SDP_GET16(cs, ptr);
114124758Semax	} else
115124758Semax		cs = 0;
116124758Semax
117124758Semax	/* Process the request. First, check continuation state */
118124758Semax	if (srv->fdidx[fd].rsp_cs != cs)
119124758Semax		return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
120124758Semax	if (srv->fdidx[fd].rsp_size > 0)
121124758Semax		return (0);
122124758Semax
123124758Semax	/*
124124758Semax	 * Service Search Response format
125124758Semax	 *
126124758Semax	 * value16	- 2 bytes TotalServiceRecordCount (not incl.)
127124758Semax	 * value16	- 2 bytes CurrentServiceRecordCount (not incl.)
128124758Semax	 * value32	- 4 bytes handle
129124758Semax	 * [ value32 ]
130124758Semax	 *
131124758Semax	 * Calculate how many record handles we can fit
132124758Semax	 * in our reply buffer and adjust rlimit.
133124758Semax	 */
134124758Semax
135124758Semax	ptr = rsp;
136124758Semax	rcount = (rsp_end - ptr) / 4;
137124758Semax	if (rcount < rsp_limit)
138124758Semax		rsp_limit = rcount;
139124758Semax
140124758Semax	/* Look for the record handles */
141124758Semax	for (rcount = 0; ssplen > 0 && rcount < rsp_limit; ) {
142124758Semax		SDP_GET8(type, req);
143124758Semax		ssplen --;
144124758Semax
145124758Semax		switch (type) {
146124758Semax		case SDP_DATA_UUID16:
147124758Semax			if (ssplen < 2)
148124758Semax				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
149124758Semax
150139721Semax			memcpy(&uuid, &uuid_base, sizeof(uuid));
151139721Semax			uuid.b[2] = *req ++;
152139721Semax			uuid.b[3] = *req ++;
153124758Semax			ssplen -= 2;
154124758Semax			break;
155124758Semax
156139721Semax		case SDP_DATA_UUID32:
157139721Semax			if (ssplen < 4)
158139721Semax				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
159139721Semax
160139721Semax			memcpy(&uuid, &uuid_base, sizeof(uuid));
161139721Semax			uuid.b[0] = *req ++;
162139721Semax			uuid.b[1] = *req ++;
163139721Semax			uuid.b[2] = *req ++;
164139721Semax			uuid.b[3] = *req ++;
165139721Semax			ssplen -= 4;
166139721Semax			break;
167139721Semax
168139721Semax		case SDP_DATA_UUID128:
169139721Semax			if (ssplen < 16)
170139721Semax				return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
171139721Semax
172139721Semax			memcpy(uuid.b, req, 16);
173139721Semax			req += 16;
174139721Semax			ssplen -= 16;
175139721Semax			break;
176139721Semax
177124758Semax		default:
178124758Semax			return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
179124758Semax			/* NOT REACHED */
180124758Semax		}
181124758Semax
182124758Semax		for (provider = provider_get_first();
183124758Semax		     provider != NULL && rcount < rsp_limit;
184124758Semax		     provider = provider_get_next(provider)) {
185124758Semax			if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
186124758Semax				continue;
187124758Semax
188139721Semax			memcpy(&puuid, &uuid_base, sizeof(puuid));
189139721Semax			puuid.b[2] = provider->profile->uuid >> 8;
190139721Semax			puuid.b[3] = provider->profile->uuid;
191139721Semax
192139721Semax			if (memcmp(&uuid, &puuid, sizeof(uuid)) == 0 ||
193139721Semax			    memcmp(&uuid, &uuid_public_browse_group, sizeof(uuid)) == 0) {
194124758Semax				SDP_PUT32(provider->handle, ptr);
195124758Semax				rcount ++;
196124758Semax			}
197124758Semax		}
198124758Semax	}
199124758Semax
200124758Semax	/* Set reply size (not counting PDU header and continuation state) */
201124758Semax	srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 4;
202124758Semax	srv->fdidx[fd].rsp_size = ptr - rsp;
203124758Semax	srv->fdidx[fd].rsp_cs = 0;
204124758Semax
205124758Semax	return (0);
206124758Semax}
207124758Semax
208124758Semax/*
209124758Semax * Send SDP Service Search Response
210124758Semax */
211124758Semax
212124758Semaxint32_t
213124758Semaxserver_send_service_search_response(server_p srv, int32_t fd)
214124758Semax{
215124758Semax	uint8_t		*rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
216124758Semax	uint8_t		*rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
217124758Semax
218124758Semax	struct iovec	iov[4];
219124758Semax	sdp_pdu_t	pdu;
220124758Semax	uint16_t	rcounts[2];
221124758Semax	uint8_t		cs[3];
222124758Semax	int32_t		size;
223124758Semax
224124758Semax	/* First update continuation state (assume we will send all data) */
225124758Semax	size = rsp_end - rsp;
226124758Semax	srv->fdidx[fd].rsp_cs += size;
227124758Semax
228124758Semax	if (size + 1 > srv->fdidx[fd].rsp_limit) {
229124758Semax		/*
230124758Semax		 * We need to split out response. Add 3 more bytes for the
231124758Semax		 * continuation state and move rsp_end and rsp_cs backwards.
232124758Semax		 */
233124758Semax
234124758Semax		while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
235124758Semax			rsp_end -= 4;
236124758Semax			srv->fdidx[fd].rsp_cs -= 4;
237124758Semax		}
238124758Semax
239124758Semax		cs[0] = 2;
240124758Semax		cs[1] = srv->fdidx[fd].rsp_cs >> 8;
241124758Semax		cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
242124758Semax	} else
243124758Semax		cs[0] = 0;
244124758Semax
245124758Semax	assert(rsp_end >= rsp);
246124758Semax
247124758Semax	rcounts[0] = srv->fdidx[fd].rsp_size / 4; /* TotalServiceRecordCount */
248124758Semax	rcounts[1] = (rsp_end - rsp) / 4; /* CurrentServiceRecordCount */
249124758Semax
250124758Semax	pdu.pid = SDP_PDU_SERVICE_SEARCH_RESPONSE;
251124758Semax	pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
252124758Semax	pdu.len = htons(sizeof(rcounts) + rcounts[1] * 4 + 1 + cs[0]);
253124758Semax
254126245Semax	rcounts[0] = htons(rcounts[0]);
255126245Semax	rcounts[1] = htons(rcounts[1]);
256126245Semax
257124758Semax	iov[0].iov_base = &pdu;
258124758Semax	iov[0].iov_len = sizeof(pdu);
259124758Semax
260124758Semax	iov[1].iov_base = rcounts;
261124758Semax	iov[1].iov_len = sizeof(rcounts);
262124758Semax
263124758Semax	iov[2].iov_base = rsp;
264124758Semax	iov[2].iov_len = rsp_end - rsp;
265124758Semax
266124758Semax	iov[3].iov_base = cs;
267124758Semax	iov[3].iov_len = 1 + cs[0];
268124758Semax
269124758Semax	do {
270124758Semax		size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
271124758Semax	} while (size < 0 && errno == EINTR);
272124758Semax
273124758Semax	/* Check if we have sent (or failed to sent) last response chunk */
274124758Semax	if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
275124758Semax		srv->fdidx[fd].rsp_cs = 0;
276124758Semax		srv->fdidx[fd].rsp_size = 0;
277124758Semax		srv->fdidx[fd].rsp_limit = 0;
278124758Semax	}
279124758Semax
280124758Semax	return ((size < 0)? errno : 0);
281124758Semax}
282124758Semax
283