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