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