xref: /freebsd/lib/libsdp/util.c (revision ad9f4e6351fb23ee81bc940638d20af3ca7c278d)
1 /*-
2  * util.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $Id: util.c,v 1.5 2003/09/08 02:29:35 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <netinet/in.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <stdio.h>
38 #include <sdp.h>
39 
40 /*
41  * SDP attribute description
42  */
43 
44 struct sdp_attr_desc {
45 	uint32_t	 attr;
46 	char const	*desc;
47 };
48 typedef struct sdp_attr_desc	sdp_attr_desc_t;
49 typedef struct sdp_attr_desc *	sdp_attr_desc_p;
50 
51 static sdp_attr_desc_t	sdp_uuids_desc[] = {
52 { SDP_UUID_PROTOCOL_SDP, "SDP", },
53 { SDP_UUID_PROTOCOL_UDP, "UDP", },
54 { SDP_UUID_PROTOCOL_RFCOMM, "RFCOMM", },
55 { SDP_UUID_PROTOCOL_TCP, "TCP", },
56 { SDP_UUID_PROTOCOL_TCS_BIN, "TCS BIN", },
57 { SDP_UUID_PROTOCOL_TCS_AT, "TCS AT", },
58 { SDP_UUID_PROTOCOL_OBEX, "OBEX", },
59 { SDP_UUID_PROTOCOL_IP, "IP", },
60 { SDP_UUID_PROTOCOL_FTP, "FTP", },
61 { SDP_UUID_PROTOCOL_HTTP, "HTTP", },
62 { SDP_UUID_PROTOCOL_WSP, "WSP", },
63 { SDP_UUID_PROTOCOL_BNEP, "BNEP", },
64 { SDP_UUID_PROTOCOL_UPNP, "UPNP", },
65 { SDP_UUID_PROTOCOL_HIDP, "HIDP", },
66 { SDP_UUID_PROTOCOL_HARDCOPY_CONTROL_CHANNEL, "Hardcopy Control Channel", },
67 { SDP_UUID_PROTOCOL_HARDCOPY_DATA_CHANNEL, "Hardcopy Data Channel", },
68 { SDP_UUID_PROTOCOL_HARDCOPY_NOTIFICATION, "Hardcopy Notification", },
69 { SDP_UUID_PROTOCOL_AVCTP, "AVCTP", },
70 { SDP_UUID_PROTOCOL_AVDTP, "AVDTP", },
71 { SDP_UUID_PROTOCOL_CMPT, "CMPT", },
72 { SDP_UUID_PROTOCOL_UDI_C_PLANE, "UDI C-Plane", },
73 { SDP_UUID_PROTOCOL_L2CAP, "L2CAP", },
74 /* Service Class IDs/Bluetooth Profile IDs */
75 { SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER, "Service Discovery Server", },
76 { SDP_SERVICE_CLASS_BROWSE_GROUP_DESCRIPTOR, "Browse Group Descriptor", },
77 { SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP, "Public Browse Group", },
78 { SDP_SERVICE_CLASS_SERIAL_PORT, "Serial Port", },
79 { SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP, "LAN Access Using PPP", },
80 { SDP_SERVICE_CLASS_DIALUP_NETWORKING, "Dial-Up Networking", },
81 { SDP_SERVICE_CLASS_IR_MC_SYNC, "IrMC Sync", },
82 { SDP_SERVICE_CLASS_OBEX_OBJECT_PUSH, "OBEX Object Push", },
83 { SDP_SERVICE_CLASS_OBEX_FILE_TRANSFER, "OBEX File Transfer", },
84 { SDP_SERVICE_CLASS_IR_MC_SYNC_COMMAND, "IrMC Sync Command", },
85 { SDP_SERVICE_CLASS_HEADSET, "Headset", },
86 { SDP_SERVICE_CLASS_CORDLESS_TELEPHONY, "Cordless Telephony", },
87 { SDP_SERVICE_CLASS_AUDIO_SOURCE, "Audio Source", },
88 { SDP_SERVICE_CLASS_AUDIO_SINK, "Audio Sink", },
89 { SDP_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET, "A/V Remote Control Target", },
90 { SDP_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION, "Advanced Audio Distribution", },
91 { SDP_SERVICE_CLASS_AV_REMOTE_CONTROL, "A/V Remote Control", },
92 { SDP_SERVICE_CLASS_VIDEO_CONFERENCING, "Video Conferencing", },
93 { SDP_SERVICE_CLASS_INTERCOM, "Intercom", },
94 { SDP_SERVICE_CLASS_FAX, "Fax", },
95 { SDP_SERVICE_CLASS_HEADSET_AUDIO_GATEWAY, "Headset Audio Gateway", },
96 { SDP_SERVICE_CLASS_WAP, "WAP", },
97 { SDP_SERVICE_CLASS_WAP_CLIENT, "WAP Client", },
98 { SDP_SERVICE_CLASS_PANU, "PANU", },
99 { SDP_SERVICE_CLASS_NAP, "Network Access Point", },
100 { SDP_SERVICE_CLASS_GN, "GN", },
101 { SDP_SERVICE_CLASS_DIRECT_PRINTING, "Direct Printing", },
102 { SDP_SERVICE_CLASS_REFERENCE_PRINTING, "Reference Printing", },
103 { SDP_SERVICE_CLASS_IMAGING, "Imaging", },
104 { SDP_SERVICE_CLASS_IMAGING_RESPONDER, "Imaging Responder", },
105 { SDP_SERVICE_CLASS_IMAGING_AUTOMATIC_ARCHIVE, "Imaging Automatic Archive", },
106 { SDP_SERVICE_CLASS_IMAGING_REFERENCED_OBJECTS, "Imaging Referenced Objects", },
107 { SDP_SERVICE_CLASS_HANDSFREE, "Handsfree", },
108 { SDP_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, "Handsfree Audio Gateway", },
109 { SDP_SERVICE_CLASS_DIRECT_PRINTING_REFERENCE_OBJECTS, "Direct Printing Reference Objects", },
110 { SDP_SERVICE_CLASS_REFLECTED_UI, "Reflected UI", },
111 { SDP_SERVICE_CLASS_BASIC_PRINTING, "Basic Printing", },
112 { SDP_SERVICE_CLASS_PRINTING_STATUS, "Printing Status", },
113 { SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE, "Human Interface Device", },
114 { SDP_SERVICE_CLASS_HARDCOPY_CABLE_REPLACEMENT, "Hardcopy Cable Replacement", },
115 { SDP_SERVICE_CLASS_HCR_PRINT, "HCR Print", },
116 { SDP_SERVICE_CLASS_HCR_SCAN, "HCR Scan", },
117 { SDP_SERVICE_CLASS_COMMON_ISDN_ACCESS, "Common ISDN Access", },
118 { SDP_SERVICE_CLASS_VIDEO_CONFERENCING_GW, "Video Conferencing Gateway", },
119 { SDP_SERVICE_CLASS_UDI_MT, "UDI MT", },
120 { SDP_SERVICE_CLASS_UDI_TA, "UDI TA", },
121 { SDP_SERVICE_CLASS_AUDIO_VIDEO, "Audio/Video", },
122 { SDP_SERVICE_CLASS_SIM_ACCESS, "SIM Access", },
123 { SDP_SERVICE_CLASS_PHONEBOOK_ACCESS_PCE, "Phonebook Access - PCE", },
124 { SDP_SERVICE_CLASS_PHONEBOOK_ACCESS_PSE, "Phonebook Access - PSE", },
125 { SDP_SERVICE_CLASS_PHONEBOOK_ACCESS, "Phonebook Access", },
126 { SDP_SERVICE_CLASS_PNP_INFORMATION, "PNP Information", },
127 { SDP_SERVICE_CLASS_GENERIC_NETWORKING, "Generic Networking", },
128 { SDP_SERVICE_CLASS_GENERIC_FILE_TRANSFER, "Generic File Transfer", },
129 { SDP_SERVICE_CLASS_GENERIC_AUDIO, "Generic Audio", },
130 { SDP_SERVICE_CLASS_GENERIC_TELEPHONY, "Generic Telephony", },
131 { SDP_SERVICE_CLASS_UPNP, "UPNP", },
132 { SDP_SERVICE_CLASS_UPNP_IP, "UPNP IP", },
133 { SDP_SERVICE_CLASS_ESDP_UPNP_IP_PAN, "ESDP UPNP IP PAN", },
134 { SDP_SERVICE_CLASS_ESDP_UPNP_IP_LAP, "ESDP UPNP IP LAP", },
135 { SDP_SERVICE_CLASS_ESDP_UPNP_L2CAP, "ESDP UPNP L2CAP", },
136 { SDP_SERVICE_CLASS_VIDEO_SOURCE, "Video Source", },
137 { SDP_SERVICE_CLASS_VIDEO_SINK, "Video Sink", },
138 { SDP_SERVICE_CLASS_VIDEO_DISTRIBUTION, "Video Distribution", },
139 { 0xffff, NULL, }
140 };
141 
142 static sdp_attr_desc_t	sdp_attrs_desc[] = {
143 { SDP_ATTR_SERVICE_RECORD_HANDLE,
144   "Record handle",
145   },
146 { SDP_ATTR_SERVICE_CLASS_ID_LIST,
147   "Service Class ID list",
148   },
149 { SDP_ATTR_SERVICE_RECORD_STATE,
150   "Service Record State",
151   },
152 { SDP_ATTR_SERVICE_ID,
153   "Service ID",
154   },
155 { SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
156   "Protocol Descriptor List",
157   },
158 { SDP_ATTR_BROWSE_GROUP_LIST,
159   "Browse Group List",
160   },
161 { SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST,
162   "Language Base Attribute ID List",
163   },
164 { SDP_ATTR_SERVICE_INFO_TIME_TO_LIVE,
165   "Service Info Time-To-Live",
166   },
167 { SDP_ATTR_SERVICE_AVAILABILITY,
168   "Service Availability",
169   },
170 { SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
171   "Bluetooh Profile Descriptor List",
172   },
173 { SDP_ATTR_DOCUMENTATION_URL,
174   "Documentation URL",
175   },
176 { SDP_ATTR_CLIENT_EXECUTABLE_URL,
177   "Client Executable URL",
178   },
179 { SDP_ATTR_ICON_URL,
180   "Icon URL",
181   },
182 { SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS,
183   "Additional Protocol Descriptor Lists" },
184 { SDP_ATTR_GROUP_ID,
185 /*SDP_ATTR_IP_SUBNET,
186   SDP_ATTR_VERSION_NUMBER_LIST*/
187   "Group ID/IP Subnet/Version Number List",
188   },
189 { SDP_ATTR_SERVICE_DATABASE_STATE,
190   "Service Database State",
191   },
192 { SDP_ATTR_SERVICE_VERSION,
193   "Service Version",
194   },
195 { SDP_ATTR_EXTERNAL_NETWORK,
196 /*SDP_ATTR_NETWORK,
197   SDP_ATTR_SUPPORTED_DATA_STORES_LIST*/
198   "External Network/Network/Supported Data Stores List",
199   },
200 { SDP_ATTR_FAX_CLASS1_SUPPORT,
201 /*SDP_ATTR_REMOTE_AUDIO_VOLUME_CONTROL*/
202   "Fax Class1 Support/Remote Audio Volume Control",
203   },
204 { SDP_ATTR_FAX_CLASS20_SUPPORT,
205 /*SDP_ATTR_SUPPORTED_FORMATS_LIST*/
206   "Fax Class20 Support/Supported Formats List",
207   },
208 { SDP_ATTR_FAX_CLASS2_SUPPORT,
209   "Fax Class2 Support",
210   },
211 { SDP_ATTR_AUDIO_FEEDBACK_SUPPORT,
212   "Audio Feedback Support",
213   },
214 { SDP_ATTR_NETWORK_ADDRESS,
215   "Network Address",
216   },
217 { SDP_ATTR_WAP_GATEWAY,
218   "WAP Gateway",
219   },
220 { SDP_ATTR_HOME_PAGE_URL,
221   "Home Page URL",
222   },
223 { SDP_ATTR_WAP_STACK_TYPE,
224   "WAP Stack Type",
225   },
226 { SDP_ATTR_SECURITY_DESCRIPTION,
227   "Security Description",
228   },
229 { SDP_ATTR_NET_ACCESS_TYPE,
230   "Net Access Type",
231   },
232 { SDP_ATTR_MAX_NET_ACCESS_RATE,
233   "Max Net Access Rate",
234   },
235 { SDP_ATTR_IPV4_SUBNET,
236   "IPv4 Subnet",
237   },
238 { SDP_ATTR_IPV6_SUBNET,
239   "IPv6 Subnet",
240   },
241 { SDP_ATTR_SUPPORTED_CAPABALITIES,
242   "Supported Capabalities",
243   },
244 { SDP_ATTR_SUPPORTED_FEATURES,
245   "Supported Features",
246   },
247 { SDP_ATTR_SUPPORTED_FUNCTIONS,
248   "Supported Functions",
249   },
250 { SDP_ATTR_TOTAL_IMAGING_DATA_CAPACITY,
251   "Total Imaging Data Capacity",
252   },
253 { SDP_ATTR_SUPPORTED_REPOSITORIES,
254   "Supported Repositories",
255   },
256 { 0xffff, NULL, }
257 };
258 
259 char const *
260 sdp_attr2desc(uint16_t attr)
261 {
262 	register sdp_attr_desc_p	a = sdp_attrs_desc;
263 
264 	for (; a->desc != NULL; a++)
265 		if (attr == a->attr)
266 			break;
267 
268 	return ((a->desc != NULL)? a->desc : "Unknown");
269 }
270 
271 char const *
272 sdp_uuid2desc(uint16_t uuid)
273 {
274 	register sdp_attr_desc_p	a = sdp_uuids_desc;
275 
276 	for (; a->desc != NULL; a++)
277 		if (uuid == a->attr)
278 			break;
279 
280 	return ((a->desc != NULL)? a->desc : "Unknown");
281 }
282 
283 void
284 sdp_print(uint32_t level, uint8_t const *start, uint8_t const *end)
285 {
286 	union {
287 		int8_t		int8;
288 		int16_t		int16;
289 		int32_t		int32;
290 		int64_t		int64;
291 		int128_t	int128;
292 		uint8_t		uint8;
293 		uint16_t	uint16;
294 		uint32_t	uint32;
295 		uint64_t	uint64;
296 	}			value;
297 	uint8_t			type;
298 	uint32_t		i;
299 
300 	if (start == NULL || end == NULL)
301 		return;
302 
303 	while (start < end) {
304 		for (i = 0; i < level; i++)
305 			printf("\t");
306 
307 		SDP_GET8(type, start);
308 
309 		switch (type) {
310 		case SDP_DATA_NIL:
311 			printf("nil\n");
312 			break;
313 
314 		case SDP_DATA_UINT8:
315 			SDP_GET8(value.uint8, start);
316 			printf("uint8 %u\n", value.uint8);
317 			break;
318 		case SDP_DATA_UINT16:
319 			SDP_GET16(value.uint16, start);
320 			printf("uint16 %u\n", value.uint16);
321 			break;
322 		case SDP_DATA_UINT32:
323 			SDP_GET32(value.uint32, start);
324 			printf("uint32 %u\n", value.uint32);
325 			break;
326 		case SDP_DATA_UINT64:
327 			SDP_GET64(value.uint64, start);
328 			printf("uint64 %ju\n", value.uint64);
329 			break;
330 
331 		case SDP_DATA_UINT128:
332 		case SDP_DATA_INT128:
333 			SDP_GET128(&value.int128, start);
334 			printf("u/int128 %#8.8x%8.8x%8.8x%8.8x\n",
335 				*(uint32_t *)&value.int128.b[0],
336 				*(uint32_t *)&value.int128.b[4],
337 				*(uint32_t *)&value.int128.b[8],
338 				*(uint32_t *)&value.int128.b[12]);
339 			break;
340 
341 		case SDP_DATA_UUID128:
342 			SDP_GET_UUID128(&value.int128, start);
343 			printf("uuid128 %#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
344 				ntohl(*(uint32_t *)&value.int128.b[0]),
345 				ntohs(*(uint16_t *)&value.int128.b[4]),
346 				ntohs(*(uint16_t *)&value.int128.b[6]),
347 				ntohs(*(uint16_t *)&value.int128.b[8]),
348 				ntohs(*(uint16_t *)&value.int128.b[10]),
349 				ntohl(*(uint32_t *)&value.int128.b[12]));
350 			break;
351 
352 		case SDP_DATA_INT8:
353 			SDP_GET8(value.int8, start);
354 			printf("int8 %d\n", value.int8);
355 			break;
356 		case SDP_DATA_INT16:
357 			SDP_GET16(value.int16, start);
358 			printf("int16 %d\n", value.int16);
359 			break;
360 		case SDP_DATA_INT32:
361 			SDP_GET32(value.int32, start);
362 			printf("int32 %d\n", value.int32);
363 			break;
364 		case SDP_DATA_INT64:
365 			SDP_GET64(value.int64, start);
366 			printf("int64 %ju\n", value.int64);
367 			break;
368 
369 		case SDP_DATA_UUID16:
370 			SDP_GET16(value.uint16, start);
371 			printf("uuid16 %#4.4x - %s\n", value.uint16,
372 				sdp_uuid2desc(value.uint16));
373 			break;
374 		case SDP_DATA_UUID32:
375 			SDP_GET32(value.uint32, start);
376 			printf("uuid32 %#8.8x\n", value.uint32);
377 			break;
378 
379 		case SDP_DATA_STR8:
380 			SDP_GET8(value.uint8, start);
381 			printf("str8 %*.*s\n", value.uint8, value.uint8, start);
382 			start += value.uint8;
383 			break;
384 		case SDP_DATA_STR16:
385 			SDP_GET16(value.uint16, start);
386 			printf("str16 %*.*s\n", value.uint16, value.uint16, start);
387 			start += value.uint16;
388 			break;
389 		case SDP_DATA_STR32:
390 			SDP_GET32(value.uint32, start);
391 			printf("str32 %*.*s\n", value.uint32, value.uint32, start);
392 			start += value.uint32;
393 			break;
394 
395 		case SDP_DATA_BOOL:
396 			SDP_GET8(value.uint8, start);
397 			printf("bool %d\n", value.uint8);
398 			break;
399 
400 		case SDP_DATA_SEQ8:
401 			SDP_GET8(value.uint8, start);
402 			printf("seq8 %d\n", value.uint8);
403 			sdp_print(level + 1, start, start + value.uint8);
404 			start += value.uint8;
405 			break;
406 		case SDP_DATA_SEQ16:
407 			SDP_GET16(value.uint16, start);
408 			printf("seq16 %d\n", value.uint16);
409 			sdp_print(level + 1, start, start + value.uint16);
410 			start += value.uint16;
411 			break;
412 		case SDP_DATA_SEQ32:
413 			SDP_GET32(value.uint32, start);
414 			printf("seq32 %d\n", value.uint32);
415 			sdp_print(level + 1, start, start + value.uint32);
416 			start += value.uint32;
417 			break;
418 
419 		case SDP_DATA_ALT8:
420 			SDP_GET8(value.uint8, start);
421 			printf("alt8 %d\n", value.uint8);
422 			sdp_print(level + 1, start, start + value.uint8);
423 			start += value.uint8;
424 			break;
425 		case SDP_DATA_ALT16:
426 			SDP_GET16(value.uint16, start);
427 			printf("alt16 %d\n", value.uint16);
428 			sdp_print(level + 1, start, start + value.uint16);
429 			start += value.uint16;
430 			break;
431 		case SDP_DATA_ALT32:
432 			SDP_GET32(value.uint32, start);
433 			printf("alt32 %d\n", value.uint32);
434 			sdp_print(level + 1, start, start + value.uint32);
435 			start += value.uint32;
436 			break;
437 
438 		case SDP_DATA_URL8:
439 			SDP_GET8(value.uint8, start);
440 			printf("url8 %*.*s\n", value.uint8, value.uint8, start);
441 			start += value.uint8;
442 			break;
443 		case SDP_DATA_URL16:
444 			SDP_GET16(value.uint16, start);
445 			printf("url16 %*.*s\n", value.uint16, value.uint16, start);
446 			start += value.uint16;
447 			break;
448 		case SDP_DATA_URL32:
449 			SDP_GET32(value.uint32, start);
450 			printf("url32 %*.*s\n", value.uint32, value.uint32, start);
451 			start += value.uint32;
452 			break;
453 
454 		default:
455 			printf("unknown data type: %#02x\n", *start ++);
456 			break;
457 		}
458 	}
459 }
460 
461