1*86d7f5d3SJohn Marino /* $NetBSD: sdpquery.c,v 1.3 2007/03/30 21:25:00 plunky Exp $ */
2*86d7f5d3SJohn Marino /* $DragonFly: src/usr.bin/sdpquery/sdpquery.c,v 1.1 2008/02/08 14:06:25 hasso Exp $ */
3*86d7f5d3SJohn Marino
4*86d7f5d3SJohn Marino /*-
5*86d7f5d3SJohn Marino * Copyright (c) 2006 Itronix Inc.
6*86d7f5d3SJohn Marino * All rights reserved.
7*86d7f5d3SJohn Marino *
8*86d7f5d3SJohn Marino * Written by Iain Hibbert for Itronix Inc.
9*86d7f5d3SJohn Marino *
10*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
11*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
12*86d7f5d3SJohn Marino * are met:
13*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
14*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
15*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
16*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
17*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
18*86d7f5d3SJohn Marino * 3. The name of Itronix Inc. may not be used to endorse
19*86d7f5d3SJohn Marino * or promote products derived from this software without specific
20*86d7f5d3SJohn Marino * prior written permission.
21*86d7f5d3SJohn Marino *
22*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23*86d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*86d7f5d3SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25*86d7f5d3SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26*86d7f5d3SJohn Marino * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27*86d7f5d3SJohn Marino * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*86d7f5d3SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*86d7f5d3SJohn Marino * ON ANY THEORY OF LIABILITY, WHETHER IN
30*86d7f5d3SJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31*86d7f5d3SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32*86d7f5d3SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
33*86d7f5d3SJohn Marino */
34*86d7f5d3SJohn Marino
35*86d7f5d3SJohn Marino #include <assert.h>
36*86d7f5d3SJohn Marino #include <bluetooth.h>
37*86d7f5d3SJohn Marino #include <err.h>
38*86d7f5d3SJohn Marino #include <errno.h>
39*86d7f5d3SJohn Marino #include <sdp.h>
40*86d7f5d3SJohn Marino #include <stdio.h>
41*86d7f5d3SJohn Marino #include <stdlib.h>
42*86d7f5d3SJohn Marino #include <string.h>
43*86d7f5d3SJohn Marino #include <unistd.h>
44*86d7f5d3SJohn Marino
45*86d7f5d3SJohn Marino #include "sdpquery.h"
46*86d7f5d3SJohn Marino
47*86d7f5d3SJohn Marino static void usage(void);
48*86d7f5d3SJohn Marino
49*86d7f5d3SJohn Marino const char *control_socket;
50*86d7f5d3SJohn Marino
51*86d7f5d3SJohn Marino static struct command {
52*86d7f5d3SJohn Marino const char *command;
53*86d7f5d3SJohn Marino int (*handler)(bdaddr_t *, bdaddr_t *, int, char const **);
54*86d7f5d3SJohn Marino const char *usage;
55*86d7f5d3SJohn Marino } commands[] = {
56*86d7f5d3SJohn Marino { "Browse", do_sdp_browse, "[UUID]" },
57*86d7f5d3SJohn Marino { "Search", do_sdp_search, "<service>" },
58*86d7f5d3SJohn Marino { NULL, NULL, NULL }
59*86d7f5d3SJohn Marino };
60*86d7f5d3SJohn Marino
61*86d7f5d3SJohn Marino int
main(int argc,char * argv[])62*86d7f5d3SJohn Marino main(int argc, char *argv[])
63*86d7f5d3SJohn Marino {
64*86d7f5d3SJohn Marino bdaddr_t laddr, raddr;
65*86d7f5d3SJohn Marino struct command *cmd;
66*86d7f5d3SJohn Marino int ch, local;
67*86d7f5d3SJohn Marino
68*86d7f5d3SJohn Marino bdaddr_copy(&laddr, BDADDR_ANY);
69*86d7f5d3SJohn Marino bdaddr_copy(&raddr, BDADDR_ANY);
70*86d7f5d3SJohn Marino control_socket = NULL;
71*86d7f5d3SJohn Marino local = 0;
72*86d7f5d3SJohn Marino
73*86d7f5d3SJohn Marino while ((ch = getopt(argc, argv, "a:c:d:hl")) != -1) {
74*86d7f5d3SJohn Marino switch (ch) {
75*86d7f5d3SJohn Marino case 'a': /* remote address */
76*86d7f5d3SJohn Marino if (!bt_aton(optarg, &raddr)) {
77*86d7f5d3SJohn Marino struct hostent *he = NULL;
78*86d7f5d3SJohn Marino
79*86d7f5d3SJohn Marino if ((he = bt_gethostbyname(optarg)) == NULL)
80*86d7f5d3SJohn Marino errx(EXIT_FAILURE, "%s: %s",
81*86d7f5d3SJohn Marino optarg, hstrerror(h_errno));
82*86d7f5d3SJohn Marino
83*86d7f5d3SJohn Marino bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
84*86d7f5d3SJohn Marino }
85*86d7f5d3SJohn Marino break;
86*86d7f5d3SJohn Marino
87*86d7f5d3SJohn Marino case 'c':
88*86d7f5d3SJohn Marino control_socket = optarg;
89*86d7f5d3SJohn Marino break;
90*86d7f5d3SJohn Marino
91*86d7f5d3SJohn Marino case 'd': /* local device address */
92*86d7f5d3SJohn Marino if (!bt_devaddr(optarg, &laddr))
93*86d7f5d3SJohn Marino err(EXIT_FAILURE, "%s", optarg);
94*86d7f5d3SJohn Marino
95*86d7f5d3SJohn Marino break;
96*86d7f5d3SJohn Marino
97*86d7f5d3SJohn Marino case 'l': /* local sdpd */
98*86d7f5d3SJohn Marino local = 1;
99*86d7f5d3SJohn Marino break;
100*86d7f5d3SJohn Marino
101*86d7f5d3SJohn Marino case 'h':
102*86d7f5d3SJohn Marino default:
103*86d7f5d3SJohn Marino usage();
104*86d7f5d3SJohn Marino /* NOT REACHED */
105*86d7f5d3SJohn Marino }
106*86d7f5d3SJohn Marino }
107*86d7f5d3SJohn Marino
108*86d7f5d3SJohn Marino argc -= optind;
109*86d7f5d3SJohn Marino argv += optind;
110*86d7f5d3SJohn Marino
111*86d7f5d3SJohn Marino optind = 0;
112*86d7f5d3SJohn Marino optreset = 1;
113*86d7f5d3SJohn Marino
114*86d7f5d3SJohn Marino if (argc < 1
115*86d7f5d3SJohn Marino || (bdaddr_any(&raddr) && !local)
116*86d7f5d3SJohn Marino || (!bdaddr_any(&raddr) && local))
117*86d7f5d3SJohn Marino usage();
118*86d7f5d3SJohn Marino
119*86d7f5d3SJohn Marino for (cmd = commands ; cmd->command != NULL; cmd++) {
120*86d7f5d3SJohn Marino if (strcasecmp(*argv, cmd->command) == 0)
121*86d7f5d3SJohn Marino return (*cmd->handler)(&laddr, &raddr, --argc, (char const **)++argv);
122*86d7f5d3SJohn Marino }
123*86d7f5d3SJohn Marino
124*86d7f5d3SJohn Marino errx(EXIT_FAILURE, "%s: Unknown Command", *argv);
125*86d7f5d3SJohn Marino }
126*86d7f5d3SJohn Marino
127*86d7f5d3SJohn Marino /* Usage */
128*86d7f5d3SJohn Marino static void
usage(void)129*86d7f5d3SJohn Marino usage(void)
130*86d7f5d3SJohn Marino {
131*86d7f5d3SJohn Marino struct command *cmd;
132*86d7f5d3SJohn Marino
133*86d7f5d3SJohn Marino fprintf(stderr,
134*86d7f5d3SJohn Marino "Usage: %s [-d device] -a bdaddr <command> [parameters..]\n"
135*86d7f5d3SJohn Marino " %s [-c path] -l <command> [parameters..]\n"
136*86d7f5d3SJohn Marino "\n", getprogname(), getprogname());
137*86d7f5d3SJohn Marino
138*86d7f5d3SJohn Marino fprintf(stderr,
139*86d7f5d3SJohn Marino "Where:\n"
140*86d7f5d3SJohn Marino "\t-a bdaddr remote address\n"
141*86d7f5d3SJohn Marino "\t-c path path to control socket\n"
142*86d7f5d3SJohn Marino "\t-d device local device address\n"
143*86d7f5d3SJohn Marino "\t-l connect to the local SDP server via control socket\n"
144*86d7f5d3SJohn Marino "\t-h display usage and quit\n"
145*86d7f5d3SJohn Marino "\n"
146*86d7f5d3SJohn Marino "Commands:\n");
147*86d7f5d3SJohn Marino
148*86d7f5d3SJohn Marino for (cmd = commands ; cmd->command != NULL ; cmd++)
149*86d7f5d3SJohn Marino fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->usage);
150*86d7f5d3SJohn Marino
151*86d7f5d3SJohn Marino exit(EXIT_FAILURE);
152*86d7f5d3SJohn Marino }
153