4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: search.c,v 1.2 2003/09/08 17:35:15 max Exp $
34 #include <netinet/in.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
41 #include "sdpcontrol.h"
43 /* List of the attributes we are looking for */
44 static uint32_t attrs[] =
46 SDP_ATTR_RANGE( SDP_ATTR_SERVICE_RECORD_HANDLE,
47 SDP_ATTR_SERVICE_RECORD_HANDLE),
48 SDP_ATTR_RANGE( SDP_ATTR_SERVICE_CLASS_ID_LIST,
49 SDP_ATTR_SERVICE_CLASS_ID_LIST),
50 SDP_ATTR_RANGE( SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
51 SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST),
52 SDP_ATTR_RANGE( SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
53 SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST)
55 #define attrs_len (sizeof(attrs)/sizeof(attrs[0]))
57 /* Buffer for the attributes */
58 #define NRECS 25 /* request this much records from the SDP server */
59 #define BSIZE 256 /* one attribute buffer size */
60 static uint8_t buffer[NRECS * attrs_len][BSIZE];
63 static sdp_attr_t values[NRECS * attrs_len];
64 #define values_len (sizeof(values)/sizeof(values[0]))
67 * Print Service Class ID List
69 * The ServiceClassIDList attribute consists of a data element sequence in
70 * which each data element is a UUID representing the service classes that
71 * a given service record conforms to. The UUIDs are listed in order from
72 * the most specific class to the most general class. The ServiceClassIDList
73 * must contain at least one service class UUID.
77 print_service_class_id_list(uint8_t const *start, uint8_t const *end)
79 uint32_t type, len, value;
81 if (end - start < 2) {
82 fprintf(stderr, "Invalid Service Class ID List. " \
83 "Too short, len=%zd\n", end - start);
87 SDP_GET8(type, start);
94 SDP_GET16(len, start);
98 SDP_GET32(len, start);
102 fprintf(stderr, "Invalid Service Class ID List. " \
103 "Not a sequence, type=%#x\n", type);
108 if (len > (end - start)) {
109 fprintf(stderr, "Invalid Service Class ID List. " \
110 "Too long len=%d\n", len);
114 while (start < end) {
115 SDP_GET8(type, start);
117 case SDP_DATA_UUID16:
118 SDP_GET16(value, start);
119 fprintf(stdout, "\t%s (%#4.4x)\n",
120 sdp_uuid2desc(value), value);
123 case SDP_DATA_UUID32:
124 SDP_GET32(value, start);
125 fprintf(stdout, "\t%#8.8x\n", value);
128 case SDP_DATA_UUID128: {
131 SDP_GET_UUID128(&uuid, start);
132 fprintf(stdout, "\t%#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
133 ntohl(*(uint32_t *)&uuid.b[0]),
134 ntohs(*(uint16_t *)&uuid.b[4]),
135 ntohs(*(uint16_t *)&uuid.b[6]),
136 ntohs(*(uint16_t *)&uuid.b[8]),
137 ntohs(*(uint16_t *)&uuid.b[10]),
138 ntohl(*(uint32_t *)&uuid.b[12]));
142 fprintf(stderr, "Invalid Service Class ID List. " \
143 "Not a UUID, type=%#x\n", type);
148 } /* print_service_class_id_list */
151 * Print Protocol Descriptor List
153 * If the ProtocolDescriptorList describes a single stack, it takes the form
154 * of a data element sequence in which each element of the sequence is a
155 * protocol descriptor. Each protocol descriptor is, in turn, a data element
156 * sequence whose first element is a UUID identifying the protocol and whose
157 * successive elements are protocol-specific parameters. The protocol
158 * descriptors are listed in order from the lowest layer protocol to the
159 * highest layer protocol used to gain access to the service. If it is possible
160 * for more than one kind of protocol stack to be used to gain access to the
161 * service, the ProtocolDescriptorList takes the form of a data element
162 * alternative where each member is a data element sequence as described above.
166 print_protocol_descriptor(uint8_t const *start, uint8_t const *end)
175 uint32_t type, len, param;
177 /* Get Protocol UUID */
178 SDP_GET8(type, start);
180 case SDP_DATA_UUID16:
181 SDP_GET16(value.uint16, start);
182 fprintf(stdout, "\t%s (%#4.4x)\n", sdp_uuid2desc(value.uint16),
186 case SDP_DATA_UUID32:
187 SDP_GET32(value.uint32, start);
188 fprintf(stdout, "\t%#8.8x\n", value.uint32);
191 case SDP_DATA_UUID128:
192 SDP_GET_UUID128(&value.int128, start);
193 fprintf(stdout, "\t%#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
194 ntohl(*(uint32_t *)&value.int128.b[0]),
195 ntohs(*(uint16_t *)&value.int128.b[4]),
196 ntohs(*(uint16_t *)&value.int128.b[6]),
197 ntohs(*(uint16_t *)&value.int128.b[8]),
198 ntohs(*(uint16_t *)&value.int128.b[10]),
199 ntohl(*(uint32_t *)&value.int128.b[12]));
203 fprintf(stderr, "Invalid Protocol Descriptor. " \
204 "Not a UUID, type=%#x\n", type);
209 /* Protocol specific parameters */
210 for (param = 1; start < end; param ++) {
211 fprintf(stdout, "\t\tProtocol specific parameter #%d: ", param);
213 SDP_GET8(type, start);
216 fprintf(stdout, "nil\n");
222 SDP_GET8(value.uint8, start);
223 fprintf(stdout, "u/int8/bool %u\n", value.uint8);
226 case SDP_DATA_UINT16:
228 case SDP_DATA_UUID16:
229 SDP_GET16(value.uint16, start);
230 fprintf(stdout, "u/int/uuid16 %u\n", value.uint16);
233 case SDP_DATA_UINT32:
235 case SDP_DATA_UUID32:
236 SDP_GET32(value.uint32, start);
237 fprintf(stdout, "u/int/uuid32 %u\n", value.uint32);
240 case SDP_DATA_UINT64:
242 SDP_GET64(value.uint64, start);
243 fprintf(stdout, "u/int64 %ju\n", value.uint64);
246 case SDP_DATA_UINT128:
247 case SDP_DATA_INT128:
248 SDP_GET128(&value.int128, start);
249 fprintf(stdout, "u/int128 %#8.8x%8.8x%8.8x%8.8x\n",
250 *(uint32_t *)&value.int128.b[0],
251 *(uint32_t *)&value.int128.b[4],
252 *(uint32_t *)&value.int128.b[8],
253 *(uint32_t *)&value.int128.b[12]);
256 case SDP_DATA_UUID128:
257 SDP_GET_UUID128(&value.int128, start);
258 fprintf(stdout, "uuid128 %#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
259 ntohl(*(uint32_t *)&value.int128.b[0]),
260 ntohs(*(uint16_t *)&value.int128.b[4]),
261 ntohs(*(uint16_t *)&value.int128.b[6]),
262 ntohs(*(uint16_t *)&value.int128.b[8]),
263 ntohs(*(uint16_t *)&value.int128.b[10]),
264 ntohl(*(uint32_t *)&value.int128.b[12]));
269 SDP_GET8(len, start);
270 for (; start < end && len > 0; start ++, len --)
271 fprintf(stdout, "%c", *start);
272 fprintf(stdout, "\n");
277 SDP_GET16(len, start);
278 for (; start < end && len > 0; start ++, len --)
279 fprintf(stdout, "%c", *start);
280 fprintf(stdout, "\n");
285 SDP_GET32(len, start);
286 for (; start < end && len > 0; start ++, len --)
287 fprintf(stdout, "%c", *start);
288 fprintf(stdout, "\n");
293 SDP_GET8(len, start);
294 for (; start < end && len > 0; start ++, len --)
295 fprintf(stdout, "%#2.2x ", *start);
296 fprintf(stdout, "\n");
301 SDP_GET16(len, start);
302 for (; start < end && len > 0; start ++, len --)
303 fprintf(stdout, "%#2.2x ", *start);
304 fprintf(stdout, "\n");
309 SDP_GET32(len, start);
310 for (; start < end && len > 0; start ++, len --)
311 fprintf(stdout, "%#2.2x ", *start);
312 fprintf(stdout, "\n");
316 fprintf(stderr, "Invalid Protocol Descriptor. " \
317 "Unknown data type: %#02x\n", type);
322 } /* print_protocol_descriptor */
325 print_protocol_descriptor_list(uint8_t const *start, uint8_t const *end)
329 if (end - start < 2) {
330 fprintf(stderr, "Invalid Protocol Descriptor List. " \
331 "Too short, len=%zd\n", end - start);
335 SDP_GET8(type, start);
338 SDP_GET8(len, start);
342 SDP_GET16(len, start);
346 SDP_GET32(len, start);
350 fprintf(stderr, "Invalid Protocol Descriptor List. " \
351 "Not a sequence, type=%#x\n", type);
356 if (len > (end - start)) {
357 fprintf(stderr, "Invalid Protocol Descriptor List. " \
358 "Too long, len=%d\n", len);
362 while (start < end) {
363 SDP_GET8(type, start);
366 SDP_GET8(len, start);
370 SDP_GET16(len, start);
374 SDP_GET32(len, start);
378 fprintf(stderr, "Invalid Protocol Descriptor List. " \
379 "Not a sequence, type=%#x\n", type);
384 if (len > (end - start)) {
385 fprintf(stderr, "Invalid Protocol Descriptor List. " \
386 "Too long, len=%d\n", len);
390 print_protocol_descriptor(start, start + len);
393 } /* print_protocol_descriptor_list */
396 * Print Bluetooth Profile Descriptor List
398 * The BluetoothProfileDescriptorList attribute consists of a data element
399 * sequence in which each element is a profile descriptor that contains
400 * information about a Bluetooth profile to which the service represented by
401 * this service record conforms. Each profile descriptor is a data element
402 * sequence whose first element is the UUID assigned to the profile and whose
403 * second element is a 16-bit profile version number. Each version of a profile
404 * is assigned a 16-bit unsigned integer profile version number, which consists
405 * of two 8-bit fields. The higher-order 8 bits contain the major version
406 * number field and the lower-order 8 bits contain the minor version number
411 print_bluetooth_profile_descriptor_list(uint8_t const *start, uint8_t const *end)
413 uint32_t type, len, value;
415 if (end - start < 2) {
416 fprintf(stderr, "Invalid Bluetooth Profile Descriptor List. " \
417 "Too short, len=%zd\n", end - start);
421 SDP_GET8(type, start);
424 SDP_GET8(len, start);
428 SDP_GET16(len, start);
432 SDP_GET32(len, start);
436 fprintf(stderr, "Invalid Bluetooth Profile Descriptor List. " \
437 "Not a sequence, type=%#x\n", type);
442 if (len > (end - start)) {
443 fprintf(stderr, "Invalid Bluetooth Profile Descriptor List. " \
444 "Too long, len=%d\n", len);
448 while (start < end) {
449 SDP_GET8(type, start);
452 SDP_GET8(len, start);
456 SDP_GET16(len, start);
460 SDP_GET32(len, start);
464 fprintf(stderr, "Invalid Bluetooth Profile " \
465 "Descriptor List. " \
466 "Not a sequence, type=%#x\n", type);
471 if (len > (end - start)) {
472 fprintf(stderr, "Invalid Bluetooth Profile " \
473 "Descriptor List. " \
474 "Too long, len=%d\n", len);
479 SDP_GET8(type, start);
481 case SDP_DATA_UUID16:
482 SDP_GET16(value, start);
483 fprintf(stdout, "\t%s (%#4.4x) ",
484 sdp_uuid2desc(value), value);
487 case SDP_DATA_UUID32:
488 SDP_GET32(value, start);
489 fprintf(stdout, "\t%#8.8x ", value);
492 case SDP_DATA_UUID128: {
495 SDP_GET_UUID128(&uuid, start);
496 fprintf(stdout, "\t%#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x ",
497 ntohl(*(uint32_t *)&uuid.b[0]),
498 ntohs(*(uint16_t *)&uuid.b[4]),
499 ntohs(*(uint16_t *)&uuid.b[6]),
500 ntohs(*(uint16_t *)&uuid.b[8]),
501 ntohs(*(uint16_t *)&uuid.b[10]),
502 ntohl(*(uint32_t *)&uuid.b[12]));
506 fprintf(stderr, "Invalid Bluetooth Profile " \
507 "Descriptor List. " \
508 "Not a UUID, type=%#x\n", type);
514 SDP_GET8(type, start);
515 if (type != SDP_DATA_UINT16) {
516 fprintf(stderr, "Invalid Bluetooth Profile " \
517 "Descriptor List. " \
518 "Invalid version type=%#x\n", type);
522 SDP_GET16(value, start);
523 fprintf(stdout, "ver. %d.%d\n",
524 (value >> 8) & 0xff, value & 0xff);
526 } /* print_bluetooth_profile_descriptor_list */
528 /* Perform SDP search command */
530 do_sdp_search(void *xs, int argc, char **argv)
533 int32_t n, type, value;
536 /* Parse command line arguments */
539 n = strtoul(argv[0], &ep, 16);
541 switch (tolower(argv[0][0])) {
542 case 'c': /* CIP/CTP */
543 switch (tolower(argv[0][1])) {
545 service = SDP_SERVICE_CLASS_COMMON_ISDN_ACCESS;
549 service = SDP_SERVICE_CLASS_CORDLESS_TELEPHONY;
558 case 'd': /* DialUp Networking */
559 service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
562 case 'f': /* Fax/OBEX File Transfer */
563 switch (tolower(argv[0][1])) {
565 service = SDP_SERVICE_CLASS_FAX;
569 service = SDP_SERVICE_CLASS_OBEX_FILE_TRANSFER;
579 service = SDP_SERVICE_CLASS_GN;
582 case 'h': /* Headset/HID */
583 switch (tolower(argv[0][1])) {
585 service = SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE;
589 service = SDP_SERVICE_CLASS_HEADSET;
598 case 'l': /* LAN Access Using PPP */
599 service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
603 service = SDP_SERVICE_CLASS_NAP;
606 case 'o': /* OBEX Object Push */
607 service = SDP_SERVICE_CLASS_OBEX_OBJECT_PUSH;
610 case 's': /* Serial Port */
611 service = SDP_SERVICE_CLASS_SERIAL_PORT;
619 service = (uint16_t) n;
626 /* Initialize attribute values array */
627 for (n = 0; n < values_len; n ++) {
628 values[n].flags = SDP_ATTR_INVALID;
630 values[n].vlen = BSIZE;
631 values[n].value = buffer[n];
634 /* Do SDP Service Search Attribute Request */
635 n = sdp_search(xs, 1, &service, attrs_len, attrs, values_len, values);
639 /* Print attributes values */
640 for (n = 0; n < values_len; n ++) {
641 if (values[n].flags != SDP_ATTR_OK)
644 switch (values[n].attr) {
645 case SDP_ATTR_SERVICE_RECORD_HANDLE:
646 fprintf(stdout, "\n");
647 if (values[n].vlen == 5) {
648 SDP_GET8(type, values[n].value);
649 if (type == SDP_DATA_UINT32) {
650 SDP_GET32(value, values[n].value);
651 fprintf(stdout, "Record Handle: " \
654 fprintf(stderr, "Invalid type=%#x " \
656 "attribute!\n", type);
658 fprintf(stderr, "Invalid size=%d for Record " \
659 "Handle attribute\n",
663 case SDP_ATTR_SERVICE_CLASS_ID_LIST:
664 fprintf(stdout, "Service Class ID List:\n");
665 print_service_class_id_list(values[n].value,
666 values[n].value + values[n].vlen);
669 case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
670 fprintf(stdout, "Protocol Descriptor List:\n");
671 print_protocol_descriptor_list(values[n].value,
672 values[n].value + values[n].vlen);
675 case SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST:
676 fprintf(stdout, "Bluetooth Profile Descriptor List:\n");
677 print_bluetooth_profile_descriptor_list(values[n].value,
678 values[n].value + values[n].vlen);
682 fprintf(stderr, "Unexpected attribute ID=%#4.4x\n",
689 } /* do_sdp_search */
691 /* Perform SDP browse command */
693 do_sdp_browse(void *xs, int argc, char **argv)
698 #define STR(x) _STR(x)
700 static char const * const av[] = {
701 STR(SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP),
711 return (do_sdp_search(xs, argc, argv));
715 } /* do_sdp_browse */
717 /* List of SDP commands */
718 struct sdp_command sdp_commands[] = {
721 "Browse for services. The <Group> parameter is a 16-bit UUID of the group\n" \
722 "to browse. If omitted <Group> is set to Public Browse Group.\n\n" \
723 "\t<Group> - xxxx; 16-bit UUID of the group to browse\n",
728 "Search for the <Service>. The <Service> parameter is a 16-bit UUID of the\n" \
729 "service to search for. For some services it is possible to use service name\n"\
730 "instead of service UUID\n\n" \
731 "\t<Service> - xxxx; 16-bit UUID of the service to search for\n\n" \
732 "\tKnown service names\n" \
733 "\t===================\n" \
734 "\tCIP - Common ISDN Access\n" \
735 "\tCTP - Cordless Telephony\n" \
736 "\tDUN - DialUp Networking\n" \
738 "\tFTRN - OBEX File Transfer\n" \
740 "\tHID - Human Interface Device\n" \
741 "\tHSET - Headset\n" \
742 "\tLAN - LAN Access Using PPP\n" \
743 "\tNAP - Network Access Point\n" \
744 "\tOPUSH - OBEX Object Push\n" \
745 "\tSP - Serial Port\n",