1330449Seadler/*-
2124758Semax * srr.c
3124758Semax *
4330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5330449Seadler *
6124758Semax * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7124758Semax * All rights reserved.
8124758Semax *
9124758Semax * Redistribution and use in source and binary forms, with or without
10124758Semax * modification, are permitted provided that the following conditions
11124758Semax * are met:
12124758Semax * 1. Redistributions of source code must retain the above copyright
13124758Semax *    notice, this list of conditions and the following disclaimer.
14124758Semax * 2. Redistributions in binary form must reproduce the above copyright
15124758Semax *    notice, this list of conditions and the following disclaimer in the
16124758Semax *    documentation and/or other materials provided with the distribution.
17124758Semax *
18124758Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19124758Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20124758Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21124758Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22124758Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23124758Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24124758Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25124758Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26124758Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27124758Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28124758Semax * SUCH DAMAGE.
29124758Semax *
30124758Semax * $Id: srr.c,v 1.1 2004/01/13 01:54:39 max Exp $
31124758Semax * $FreeBSD: stable/11/usr.sbin/bluetooth/sdpd/srr.c 330449 2018-03-05 07:26:05Z eadler $
32124758Semax */
33124758Semax
34124758Semax#include <sys/queue.h>
35124758Semax#include <sys/uio.h>
36124758Semax#include <netinet/in.h>
37124758Semax#include <arpa/inet.h>
38124758Semax#include <assert.h>
39281210Stakawata#define L2CAP_SOCKET_CHECKED
40124758Semax#include <bluetooth.h>
41124758Semax#include <errno.h>
42124758Semax#include <sdp.h>
43124758Semax#include <string.h>
44124758Semax#include "profile.h"
45124758Semax#include "provider.h"
46124758Semax#include "server.h"
47124758Semax
48124758Semax/*
49124758Semax * Prepare Service Register response
50124758Semax */
51124758Semax
52124758Semaxint32_t
53124758Semaxserver_prepare_service_register_response(server_p srv, int32_t fd)
54124758Semax{
55124758Semax	uint8_t const	*req = srv->req + sizeof(sdp_pdu_t);
56124758Semax	uint8_t const	*req_end = req + ((sdp_pdu_p)(srv->req))->len;
57124758Semax	uint8_t		*rsp = srv->fdidx[fd].rsp;
58124758Semax
59124758Semax	profile_t	*profile = NULL;
60124758Semax	provider_t	*provider = NULL;
61124758Semax	bdaddr_t	*bdaddr = NULL;
62124758Semax	int32_t		 uuid;
63124758Semax
64124758Semax	/*
65124758Semax	 * Minimal Service Register Request
66124758Semax	 *
67124758Semax	 * value16	- uuid 2 bytes
68124758Semax	 * bdaddr	- BD_ADDR 6 bytes
69124758Semax	 */
70124758Semax
71153176Semax	if (!srv->fdidx[fd].control ||
72153176Semax	    !srv->fdidx[fd].priv || req_end - req < 8)
73124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
74124758Semax
75124758Semax	/* Get ServiceClass UUID */
76124758Semax	SDP_GET16(uuid, req);
77124758Semax
78124758Semax	/* Get BD_ADDR */
79124758Semax	bdaddr = (bdaddr_p) req;
80124758Semax	req += sizeof(*bdaddr);
81124758Semax
82124758Semax	/* Lookup profile descriptror */
83124758Semax	profile = profile_get_descriptor(uuid);
84124758Semax	if (profile == NULL)
85124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
86124758Semax
87124758Semax	/* Validate user data */
88124758Semax	if (req_end - req < profile->dsize ||
89124758Semax	    profile->valid == NULL ||
90124758Semax	    (profile->valid)(req, req_end - req) == 0)
91124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
92124758Semax
93124758Semax	/* Register provider */
94124758Semax	provider = provider_register(profile, bdaddr, fd, req, req_end - req);
95124758Semax	if (provider == NULL)
96124758Semax		return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
97124758Semax
98124758Semax	SDP_PUT16(0, rsp);
99124758Semax	SDP_PUT32(provider->handle, rsp);
100124758Semax
101124758Semax	/* Set reply size */
102124758Semax	srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t);
103124758Semax	srv->fdidx[fd].rsp_size = rsp - srv->fdidx[fd].rsp;
104124758Semax	srv->fdidx[fd].rsp_cs = 0;
105124758Semax
106124758Semax	return (0);
107124758Semax}
108124758Semax
109124758Semax/*
110124758Semax * Send Service Register Response
111124758Semax */
112124758Semax
113124758Semaxint32_t
114124758Semaxserver_send_service_register_response(server_p srv, int32_t fd)
115124758Semax{
116124758Semax	struct iovec	iov[2];
117124758Semax	sdp_pdu_t	pdu;
118124758Semax	int32_t		size;
119124758Semax
120124758Semax	assert(srv->fdidx[fd].rsp_size < srv->fdidx[fd].rsp_limit);
121124758Semax
122124758Semax	pdu.pid = SDP_PDU_ERROR_RESPONSE;
123124758Semax	pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
124124758Semax	pdu.len = htons(srv->fdidx[fd].rsp_size);
125124758Semax
126124758Semax	iov[0].iov_base = &pdu;
127124758Semax	iov[0].iov_len = sizeof(pdu);
128124758Semax
129124758Semax	iov[1].iov_base = srv->fdidx[fd].rsp;
130124758Semax	iov[1].iov_len = srv->fdidx[fd].rsp_size;
131124758Semax
132124758Semax	do {
133124758Semax		size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
134124758Semax	} while (size < 0 && errno == EINTR);
135124758Semax
136124758Semax	srv->fdidx[fd].rsp_cs = 0;
137124758Semax	srv->fdidx[fd].rsp_size = 0;
138124758Semax	srv->fdidx[fd].rsp_limit = 0;
139124758Semax
140124758Semax	return ((size < 0)? errno : 0);
141124758Semax}
142124758Semax
143