xref: /freebsd/lib/libsdp/util.c (revision 8847579c57d6aff2b3371c707dce7a2cee8389aa)
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_PNP_INFORMATION, "PNP Information", },
121 { SDP_SERVICE_CLASS_GENERIC_NETWORKING, "Generic Networking", },
122 { SDP_SERVICE_CLASS_GENERIC_FILE_TRANSFER, "Generic File Transfer", },
123 { SDP_SERVICE_CLASS_GENERIC_AUDIO, "Generic Audio", },
124 { SDP_SERVICE_CLASS_GENERIC_TELEPHONY, "Generic Telephony", },
125 { SDP_SERVICE_CLASS_UPNP, "UPNP", },
126 { SDP_SERVICE_CLASS_UPNP_IP, "UPNP IP", },
127 { SDP_SERVICE_CLASS_ESDP_UPNP_IP_PAN, "ESDP UPNP IP PAN", },
128 { SDP_SERVICE_CLASS_ESDP_UPNP_IP_LAP, "ESDP UPNP IP LAP", },
129 { SDP_SERVICE_CLASS_ESDP_UPNP_L2CAP, "ESDP UPNP L2CAP", },
130 { 0xffff, NULL, }
131 };
132 
133 static sdp_attr_desc_t	sdp_attrs_desc[] = {
134 { SDP_ATTR_SERVICE_RECORD_HANDLE,
135   "Record handle",
136   },
137 { SDP_ATTR_SERVICE_CLASS_ID_LIST,
138   "Service Class ID list",
139   },
140 { SDP_ATTR_SERVICE_RECORD_STATE,
141   "Service Record State",
142   },
143 { SDP_ATTR_SERVICE_ID,
144   "Service ID",
145   },
146 { SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
147   "Protocol Descriptor List",
148   },
149 { SDP_ATTR_BROWSE_GROUP_LIST,
150   "Browse Group List",
151   },
152 { SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST,
153   "Language Base Attribute ID List",
154   },
155 { SDP_ATTR_SERVICE_INFO_TIME_TO_LIVE,
156   "Service Info Time-To-Live",
157   },
158 { SDP_ATTR_SERVICE_AVAILABILITY,
159   "Service Availability",
160   },
161 { SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
162   "Bluetooh Profile Descriptor List",
163   },
164 { SDP_ATTR_DOCUMENTATION_URL,
165   "Documentation URL",
166   },
167 { SDP_ATTR_CLIENT_EXECUTABLE_URL,
168   "Client Executable URL",
169   },
170 { SDP_ATTR_ICON_URL,
171   "Icon URL",
172   },
173 { SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS,
174   "Additional Protocol Descriptor Lists" },
175 { SDP_ATTR_GROUP_ID,
176 /*SDP_ATTR_IP_SUBNET,
177   SDP_ATTR_VERSION_NUMBER_LIST*/
178   "Group ID/IP Subnet/Version Number List",
179   },
180 { SDP_ATTR_SERVICE_DATABASE_STATE,
181   "Service Database State",
182   },
183 { SDP_ATTR_SERVICE_VERSION,
184   "Service Version",
185   },
186 { SDP_ATTR_EXTERNAL_NETWORK,
187 /*SDP_ATTR_NETWORK,
188   SDP_ATTR_SUPPORTED_DATA_STORES_LIST*/
189   "External Network/Network/Supported Data Stores List",
190   },
191 { SDP_ATTR_FAX_CLASS1_SUPPORT,
192 /*SDP_ATTR_REMOTE_AUDIO_VOLUME_CONTROL*/
193   "Fax Class1 Support/Remote Audio Volume Control",
194   },
195 { SDP_ATTR_FAX_CLASS20_SUPPORT,
196 /*SDP_ATTR_SUPPORTED_FORMATS_LIST*/
197   "Fax Class20 Support/Supported Formats List",
198   },
199 { SDP_ATTR_FAX_CLASS2_SUPPORT,
200   "Fax Class2 Support",
201   },
202 { SDP_ATTR_AUDIO_FEEDBACK_SUPPORT,
203   "Audio Feedback Support",
204   },
205 { SDP_ATTR_NETWORK_ADDRESS,
206   "Network Address",
207   },
208 { SDP_ATTR_WAP_GATEWAY,
209   "WAP Gateway",
210   },
211 { SDP_ATTR_HOME_PAGE_URL,
212   "Home Page URL",
213   },
214 { SDP_ATTR_WAP_STACK_TYPE,
215   "WAP Stack Type",
216   },
217 { SDP_ATTR_SECURITY_DESCRIPTION,
218   "Security Description",
219   },
220 { SDP_ATTR_NET_ACCESS_TYPE,
221   "Net Access Type",
222   },
223 { SDP_ATTR_MAX_NET_ACCESS_RATE,
224   "Max Net Access Rate",
225   },
226 { SDP_ATTR_IPV4_SUBNET,
227   "IPv4 Subnet",
228   },
229 { SDP_ATTR_IPV6_SUBNET,
230   "IPv6 Subnet",
231   },
232 { SDP_ATTR_SUPPORTED_CAPABALITIES,
233   "Supported Capabalities",
234   },
235 { SDP_ATTR_SUPPORTED_FEATURES,
236   "Supported Features",
237   },
238 { SDP_ATTR_SUPPORTED_FUNCTIONS,
239   "Supported Functions",
240   },
241 { SDP_ATTR_TOTAL_IMAGING_DATA_CAPACITY,
242   "Total Imaging Data Capacity",
243   },
244 { 0xffff, NULL, }
245 };
246 
247 char const *
248 sdp_attr2desc(uint16_t attr)
249 {
250 	register sdp_attr_desc_p	a = sdp_attrs_desc;
251 
252 	for (; a->desc != NULL; a++)
253 		if (attr == a->attr)
254 			break;
255 
256 	return ((a->desc != NULL)? a->desc : "Unknown");
257 }
258 
259 char const *
260 sdp_uuid2desc(uint16_t uuid)
261 {
262 	register sdp_attr_desc_p	a = sdp_uuids_desc;
263 
264 	for (; a->desc != NULL; a++)
265 		if (uuid == a->attr)
266 			break;
267 
268 	return ((a->desc != NULL)? a->desc : "Unknown");
269 }
270 
271 void
272 sdp_print(uint32_t level, uint8_t const *start, uint8_t const *end)
273 {
274 	union {
275 		int8_t		int8;
276 		int16_t		int16;
277 		int32_t		int32;
278 		int64_t		int64;
279 		int128_t	int128;
280 		uint8_t		uint8;
281 		uint16_t	uint16;
282 		uint32_t	uint32;
283 		uint64_t	uint64;
284 	}			value;
285 	uint8_t			type;
286 	uint32_t		i;
287 
288 	if (start == NULL || end == NULL)
289 		return;
290 
291 	while (start < end) {
292 		for (i = 0; i < level; i++)
293 			printf("\t");
294 
295 		SDP_GET8(type, start);
296 
297 		switch (type) {
298 		case SDP_DATA_NIL:
299 			printf("nil\n");
300 			break;
301 
302 		case SDP_DATA_UINT8:
303 			SDP_GET8(value.uint8, start);
304 			printf("uint8 %u\n", value.uint8);
305 			break;
306 		case SDP_DATA_UINT16:
307 			SDP_GET16(value.uint16, start);
308 			printf("uint16 %u\n", value.uint16);
309 			break;
310 		case SDP_DATA_UINT32:
311 			SDP_GET32(value.uint32, start);
312 			printf("uint32 %u\n", value.uint32);
313 			break;
314 		case SDP_DATA_UINT64:
315 			SDP_GET64(value.uint64, start);
316 			printf("uint64 %ju\n", value.uint64);
317 			break;
318 
319 		case SDP_DATA_UINT128:
320 		case SDP_DATA_INT128:
321 			SDP_GET128(&value.int128, start);
322 			printf("u/int128 %#8.8x%8.8x%8.8x%8.8x\n",
323 				*(uint32_t *)&value.int128.b[0],
324 				*(uint32_t *)&value.int128.b[4],
325 				*(uint32_t *)&value.int128.b[8],
326 				*(uint32_t *)&value.int128.b[12]);
327 			break;
328 
329 		case SDP_DATA_UUID128:
330 			SDP_GET_UUID128(&value.int128, start);
331 			printf("uuid128 %#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
332 				ntohl(*(uint32_t *)&value.int128.b[0]),
333 				ntohs(*(uint16_t *)&value.int128.b[4]),
334 				ntohs(*(uint16_t *)&value.int128.b[6]),
335 				ntohs(*(uint16_t *)&value.int128.b[8]),
336 				ntohs(*(uint16_t *)&value.int128.b[10]),
337 				ntohl(*(uint32_t *)&value.int128.b[12]));
338 			break;
339 
340 		case SDP_DATA_INT8:
341 			SDP_GET8(value.int8, start);
342 			printf("int8 %d\n", value.int8);
343 			break;
344 		case SDP_DATA_INT16:
345 			SDP_GET16(value.int16, start);
346 			printf("int16 %d\n", value.int16);
347 			break;
348 		case SDP_DATA_INT32:
349 			SDP_GET32(value.int32, start);
350 			printf("int32 %d\n", value.int32);
351 			break;
352 		case SDP_DATA_INT64:
353 			SDP_GET64(value.int64, start);
354 			printf("int64 %ju\n", value.int64);
355 			break;
356 
357 		case SDP_DATA_UUID16:
358 			SDP_GET16(value.uint16, start);
359 			printf("uuid16 %#4.4x - %s\n", value.uint16,
360 				sdp_uuid2desc(value.uint16));
361 			break;
362 		case SDP_DATA_UUID32:
363 			SDP_GET32(value.uint32, start);
364 			printf("uuid32 %#8.8x\n", value.uint32);
365 			break;
366 
367 		case SDP_DATA_STR8:
368 			SDP_GET8(value.uint8, start);
369 			printf("str8 %*.*s\n", value.uint8, value.uint8, start);
370 			start += value.uint8;
371 			break;
372 		case SDP_DATA_STR16:
373 			SDP_GET16(value.uint16, start);
374 			printf("str16 %*.*s\n", value.uint16, value.uint16, start);
375 			start += value.uint16;
376 			break;
377 		case SDP_DATA_STR32:
378 			SDP_GET32(value.uint32, start);
379 			printf("str32 %*.*s\n", value.uint32, value.uint32, start);
380 			start += value.uint32;
381 			break;
382 
383 		case SDP_DATA_BOOL:
384 			SDP_GET8(value.uint8, start);
385 			printf("bool %d\n", value.uint8);
386 			break;
387 
388 		case SDP_DATA_SEQ8:
389 			SDP_GET8(value.uint8, start);
390 			printf("seq8 %d\n", value.uint8);
391 			sdp_print(level + 1, start, start + value.uint8);
392 			start += value.uint8;
393 			break;
394 		case SDP_DATA_SEQ16:
395 			SDP_GET16(value.uint16, start);
396 			printf("seq16 %d\n", value.uint16);
397 			sdp_print(level + 1, start, start + value.uint16);
398 			start += value.uint16;
399 			break;
400 		case SDP_DATA_SEQ32:
401 			SDP_GET32(value.uint32, start);
402 			printf("seq32 %d\n", value.uint32);
403 			sdp_print(level + 1, start, start + value.uint32);
404 			start += value.uint32;
405 			break;
406 
407 		case SDP_DATA_ALT8:
408 			SDP_GET8(value.uint8, start);
409 			printf("alt8 %d\n", value.uint8);
410 			sdp_print(level + 1, start, start + value.uint8);
411 			start += value.uint8;
412 			break;
413 		case SDP_DATA_ALT16:
414 			SDP_GET16(value.uint16, start);
415 			printf("alt16 %d\n", value.uint16);
416 			sdp_print(level + 1, start, start + value.uint16);
417 			start += value.uint16;
418 			break;
419 		case SDP_DATA_ALT32:
420 			SDP_GET32(value.uint32, start);
421 			printf("alt32 %d\n", value.uint32);
422 			sdp_print(level + 1, start, start + value.uint32);
423 			start += value.uint32;
424 			break;
425 
426 		case SDP_DATA_URL8:
427 			SDP_GET8(value.uint8, start);
428 			printf("url8 %*.*s\n", value.uint8, value.uint8, start);
429 			start += value.uint8;
430 			break;
431 		case SDP_DATA_URL16:
432 			SDP_GET16(value.uint16, start);
433 			printf("url16 %*.*s\n", value.uint16, value.uint16, start);
434 			start += value.uint16;
435 			break;
436 		case SDP_DATA_URL32:
437 			SDP_GET32(value.uint32, start);
438 			printf("url32 %*.*s\n", value.uint32, value.uint32, start);
439 			start += value.uint32;
440 			break;
441 
442 		default:
443 			printf("unknown data type: %#02x\n", *start ++);
444 			break;
445 		}
446 	}
447 }
448 
449