1330449Seadler/*-
2124758Semax * scr.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: scr.c,v 1.1 2004/01/13 01:54:39 max Exp $
31124758Semax * $FreeBSD: stable/11/usr.sbin/bluetooth/sdpd/scr.c 330449 2018-03-05 07:26:05Z eadler $
32124758Semax */
33124758Semax
34124758Semax#include <sys/queue.h>
35281210Stakawata#define L2CAP_SOCKET_CHECKED
36124758Semax#include <bluetooth.h>
37124758Semax#include <errno.h>
38124758Semax#include <sdp.h>
39124758Semax#include <string.h>
40124758Semax#include "profile.h"
41124758Semax#include "provider.h"
42124758Semax#include "server.h"
43124758Semax
44124758Semax/*
45124758Semax * Prepare Service Change response
46124758Semax */
47124758Semax
48124758Semaxint32_t
49124758Semaxserver_prepare_service_change_response(server_p srv, int32_t fd)
50124758Semax{
51124758Semax	uint8_t const	*req = srv->req + sizeof(sdp_pdu_t);
52124758Semax	uint8_t const	*req_end = req + ((sdp_pdu_p)(srv->req))->len;
53124758Semax	uint8_t		*rsp = srv->fdidx[fd].rsp;
54124758Semax
55124758Semax	provider_t	*provider = NULL;
56124758Semax	uint32_t	 handle;
57124758Semax
58124758Semax	/*
59124758Semax	 * Minimal Service Change Request
60124758Semax	 *
61124758Semax	 * value32	- handle 4 bytes
62124758Semax	 */
63124758Semax
64153176Semax	if (!srv->fdidx[fd].control ||
65153176Semax	    !srv->fdidx[fd].priv || req_end - req < 4)
66124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
67124758Semax
68124758Semax	/* Get handle */
69124758Semax	SDP_GET32(handle, req);
70124758Semax
71124758Semax	/* Lookup provider */
72124758Semax	provider = provider_by_handle(handle);
73124758Semax	if (provider == NULL || provider->fd != fd)
74124758Semax		return (SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE);
75124758Semax
76124758Semax	/* Validate user data */
77124758Semax	if (req_end - req < provider->profile->dsize ||
78124758Semax	    provider->profile->valid == NULL ||
79124758Semax	    (provider->profile->valid)(req, req_end - req) == 0)
80124758Semax		return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
81124758Semax
82124758Semax	/* Update provider */
83124758Semax	if (provider_update(provider, req, req_end - req) < 0)
84124758Semax		return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES);
85124758Semax
86124758Semax	SDP_PUT16(0, rsp);
87124758Semax
88124758Semax	/* Set reply size */
89124758Semax	srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t);
90124758Semax	srv->fdidx[fd].rsp_size = rsp - srv->fdidx[fd].rsp;
91124758Semax	srv->fdidx[fd].rsp_cs = 0;
92124758Semax
93124758Semax	return (0);
94124758Semax}
95124758Semax
96