summaryrefslogtreecommitdiffstats
path: root/usr.bin/bluetooth/rfcomm_sppd/rfcomm_sdp.c
blob: 239597dc755d937cbfb5b3edf6f565c31369e8e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * rfcomm_sdp.c
 *
 * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: rfcomm_sdp.c,v 1.1 2003/09/07 18:15:55 max Exp $
 * $FreeBSD$
 */
#define L2CAP_SOCKET_CHECKED
#include <bluetooth.h>
#include <errno.h>
#include <sdp.h>
#include <stdio.h>

#undef	PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE
#define	PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE	256

#undef	PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE
#define	PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE	12

static int rfcomm_proto_list_parse (uint8_t const *start, uint8_t const *end,
					int *channel, int *error);

/*
 * Lookup RFCOMM channel number in the Protocol Descriptor List
 */

#undef	rfcomm_channel_lookup_exit
#define	rfcomm_channel_lookup_exit(e) { \
	if (error != NULL) \
		*error = (e); \
	if (ss != NULL) { \
		sdp_close(ss); \
		ss = NULL; \
	} \
	return (((e) == 0)? 0 : -1); \
}

int
rfcomm_channel_lookup(bdaddr_t const *local, bdaddr_t const *remote,
			int service, int *channel, int *error)
{
	uint8_t		 buffer[PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE];
	void		*ss    = NULL;
	uint16_t	 serv  = (uint16_t) service;
	uint32_t	 attr  = SDP_ATTR_RANGE(
					SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
					SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
	sdp_attr_t	 proto = { SDP_ATTR_INVALID,0,sizeof(buffer),buffer };
	uint32_t	 type, len;

	if (local == NULL)
		local = NG_HCI_BDADDR_ANY;
	if (remote == NULL || channel == NULL)
		rfcomm_channel_lookup_exit(EINVAL);

	if ((ss = sdp_open(local, remote)) == NULL)
		rfcomm_channel_lookup_exit(ENOMEM);
	if (sdp_error(ss) != 0)
		rfcomm_channel_lookup_exit(sdp_error(ss));

	if (sdp_search(ss, 1, &serv, 1, &attr, 1, &proto) != 0)
		rfcomm_channel_lookup_exit(sdp_error(ss));
	if (proto.flags != SDP_ATTR_OK)
		rfcomm_channel_lookup_exit(ENOATTR);

	sdp_close(ss);
	ss = NULL;

	/*
	 * If it is possible for more than one kind of protocol stack to be 
	 * used to gain access to the service, the ProtocolDescriptorList
	 * takes the form of a data element alternative. We always use the
	 * first protocol stack.
	 *
	 * A minimal Protocol Descriptor List for RFCOMM based service would
	 * look like
	 *
	 * seq8 len8			- 2 bytes
	 *	seq8 len8		- 2 bytes
	 *		uuid16 value16	- 3 bytes	L2CAP
	 *	seq8 len8		- 2 bytes
	 *		uuid16 value16	- 3 bytes	RFCOMM
	 *		uint8  value8	- 2 bytes	RFCOMM param #1 
	 *				=========
	 *				 14 bytes
	 *
	 * Lets not count first [seq8 len8] wrapper, so the minimal size of 
	 * the Protocol Descriptor List (the data we are actually interested
	 * in) for RFCOMM based service would be 12 bytes.
	 */

	if (proto.vlen < PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE)
		rfcomm_channel_lookup_exit(EINVAL);

	SDP_GET8(type, proto.value);

	if (type == SDP_DATA_ALT8) {
		SDP_GET8(len, proto.value);
	} else if (type == SDP_DATA_ALT16) {
		SDP_GET16(len, proto.value);
	} else if (type == SDP_DATA_ALT32) {
		SDP_GET32(len, proto.value);
	} else
		len = 0;

	if (len > 0)
		SDP_GET8(type, proto.value);

	switch (type) {
	case SDP_DATA_SEQ8:
		SDP_GET8(len, proto.value);
		break;

	case SDP_DATA_SEQ16:
		SDP_GET16(len, proto.value);
		break;

	case SDP_DATA_SEQ32:
		SDP_GET32(len, proto.value);
		break;

	default:
		rfcomm_channel_lookup_exit(ENOATTR);
		/* NOT REACHED */
	}

	if (len < PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE)
		rfcomm_channel_lookup_exit(EINVAL);

	return (rfcomm_proto_list_parse(proto.value,
					buffer + proto.vlen, channel, error));
}
	
/*
 * Parse protocol descriptor list
 *
 * The ProtocolDescriptorList attribute describes one or more protocol
 * stacks that may be used to gain access to the service described by 
 * the service record. If the ProtocolDescriptorList describes a single
 * stack, it takes the form of a data element sequence in which each 
 * element of the sequence is a protocol descriptor.
 */

#undef	rfcomm_proto_list_parse_exit
#define	rfcomm_proto_list_parse_exit(e) { \
	if (error != NULL) \
		*error = (e); \
	return (((e) == 0)? 0 : -1); \
}

static int
rfcomm_proto_list_parse(uint8_t const *start, uint8_t const *end,
			int *channel, int *error)
{
	int	type, len, value;

	while (start < end) {

		/* 
		 * Parse protocol descriptor
		 *
		 * A protocol descriptor identifies a communications protocol 
		 * and provides protocol specific parameters. A protocol 
		 * descriptor is represented as a data element sequence. The 
		 * first data element in the sequence must be the UUID that 
		 * identifies the protocol. Additional data elements optionally
		 * provide protocol specific information, such as the L2CAP 
		 * protocol/service multiplexer (PSM) and the RFCOMM server
		 * channel number (CN).
		 */

		/* We must have at least one byte (type) */
		if (end - start < 1)
			rfcomm_proto_list_parse_exit(EINVAL)

		SDP_GET8(type, start);
		switch (type) {
		case SDP_DATA_SEQ8:
			SDP_GET8(len, start);
			break;

		case SDP_DATA_SEQ16:
			SDP_GET16(len, start);
			break;

		case SDP_DATA_SEQ32:
			SDP_GET32(len, start);
			break;

		default:
			rfcomm_proto_list_parse_exit(ENOATTR)
			/* NOT REACHED */
		}

		/* We must have at least 3 bytes (type + UUID16) */
		if (end - start < 3)
			rfcomm_proto_list_parse_exit(EINVAL);

		/* Get protocol UUID */
		SDP_GET8(type, start); len -= sizeof(uint8_t);
		switch (type) {
		case SDP_DATA_UUID16:
			SDP_GET16(value, start); len -= sizeof(uint16_t);
			if (value != SDP_UUID_PROTOCOL_RFCOMM)
				goto next_protocol;
			break;

		case SDP_DATA_UUID32:  /* XXX FIXME can we have 32-bit UUID */
		case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
		default:
			rfcomm_proto_list_parse_exit(ENOATTR);
			/* NOT REACHED */
		}

		/*
		 * First protocol specific parameter for RFCOMM procotol must
		 * be uint8 that represents RFCOMM channel number. So we must
		 * have at least two bytes.
		 */

		if (end - start < 2)
			rfcomm_proto_list_parse_exit(EINVAL);

		SDP_GET8(type, start);
		if (type != SDP_DATA_UINT8)
			rfcomm_proto_list_parse_exit(ENOATTR);

		SDP_GET8(*channel, start);

		rfcomm_proto_list_parse_exit(0);
		/* NOT REACHED */
next_protocol:
		start += len;
	}

	/*
	 * If we got here then it means we could not find RFCOMM protocol 
	 * descriptor, but the reply format was actually valid.
	 */

	rfcomm_proto_list_parse_exit(ENOATTR);
}

OpenPOWER on IntegriCloud