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