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