1124758Semax/*
2124758Semax * srr.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: srr.c,v 1.1 2004/01/13 01:54:39 max Exp $
29124758Semax * $FreeBSD: releng/10.3/usr.sbin/bluetooth/sdpd/srr.c 153176 2005-12-06 17:56:36Z emax $
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"
44124758Semax
45124758Semax/*
46124758Semax * Prepare Service Register response
47124758Semax */
48124758Semax
49124758Semaxint32_t
50124758Semaxserver_prepare_service_register_response(server_p srv, int32_t fd)
51124758Semax{
52124758Semax	uint8_t const	*req = srv->req + sizeof(sdp_pdu_t);
53124758Semax	uint8_t const	*req_end = req + ((sdp_pdu_p)(srv->req))->len;
54124758Semax	uint8_t		*rsp = srv->fdidx[fd].rsp;
55124758Semax
56124758Semax	profile_t	*profile = NULL;
57124758Semax	provider_t	*provider = NULL;
58124758Semax	bdaddr_t	*bdaddr = NULL;
59124758Semax	int32_t		 uuid;
60124758Semax
61124758Semax	/*
62124758Semax	 * Minimal Service Register Request
63124758Semax	 *
64124758Semax	 * value16	- uuid 2 bytes
65124758Semax	 * bdaddr	- BD_ADDR 6 bytes
66124758Semax	 */
67124758Semax
68153176Semax	if (!srv->fdidx[fd].control ||
69153176Semax	    !srv->fdidx[fd].priv || req_end - req < 8)
70124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
71124758Semax
72124758Semax	/* Get ServiceClass UUID */
73124758Semax	SDP_GET16(uuid, req);
74124758Semax
75124758Semax	/* Get BD_ADDR */
76124758Semax	bdaddr = (bdaddr_p) req;
77124758Semax	req += sizeof(*bdaddr);
78124758Semax
79124758Semax	/* Lookup profile descriptror */
80124758Semax	profile = profile_get_descriptor(uuid);
81124758Semax	if (profile == NULL)
82124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
83124758Semax
84124758Semax	/* Validate user data */
85124758Semax	if (req_end - req < profile->dsize ||
86124758Semax	    profile->valid == NULL ||
87124758Semax	    (profile->valid)(req, req_end - req) == 0)
88124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
89124758Semax
90124758Semax	/* Register provider */
91124758Semax	provider = provider_register(profile, bdaddr, fd, req, req_end - req);
92124758Semax	if (provider == NULL)
93124758Semax		return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
94124758Semax
95124758Semax	SDP_PUT16(0, rsp);
96124758Semax	SDP_PUT32(provider->handle, rsp);
97124758Semax
98124758Semax	/* Set reply size */
99124758Semax	srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t);
100124758Semax	srv->fdidx[fd].rsp_size = rsp - srv->fdidx[fd].rsp;
101124758Semax	srv->fdidx[fd].rsp_cs = 0;
102124758Semax
103124758Semax	return (0);
104124758Semax}
105124758Semax
106124758Semax/*
107124758Semax * Send Service Register Response
108124758Semax */
109124758Semax
110124758Semaxint32_t
111124758Semaxserver_send_service_register_response(server_p srv, int32_t fd)
112124758Semax{
113124758Semax	struct iovec	iov[2];
114124758Semax	sdp_pdu_t	pdu;
115124758Semax	int32_t		size;
116124758Semax
117124758Semax	assert(srv->fdidx[fd].rsp_size < srv->fdidx[fd].rsp_limit);
118124758Semax
119124758Semax	pdu.pid = SDP_PDU_ERROR_RESPONSE;
120124758Semax	pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
121124758Semax	pdu.len = htons(srv->fdidx[fd].rsp_size);
122124758Semax
123124758Semax	iov[0].iov_base = &pdu;
124124758Semax	iov[0].iov_len = sizeof(pdu);
125124758Semax
126124758Semax	iov[1].iov_base = srv->fdidx[fd].rsp;
127124758Semax	iov[1].iov_len = srv->fdidx[fd].rsp_size;
128124758Semax
129124758Semax	do {
130124758Semax		size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
131124758Semax	} while (size < 0 && errno == EINTR);
132124758Semax
133124758Semax	srv->fdidx[fd].rsp_cs = 0;
134124758Semax	srv->fdidx[fd].rsp_size = 0;
135124758Semax	srv->fdidx[fd].rsp_limit = 0;
136124758Semax
137124758Semax	return ((size < 0)? errno : 0);
138124758Semax}
139124758Semax
140