xref: /freebsd/usr.sbin/bluetooth/sdpcontrol/sdpcontrol.c (revision 56e53cb8ef000c3ef72337a4095987a932cdedef)
1 /*-
2  * sdpcontrol.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $Id: sdpcontrol.c,v 1.1 2003/09/08 02:27:27 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <assert.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <sdp.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include "sdpcontrol.h"
45 
46 /* Prototypes */
47 static int                  do_sdp_command	(bdaddr_p, char const *, int,
48 						 int, char **);
49 static struct sdp_command * find_sdp_command	(char const *,
50 						 struct sdp_command *);
51 static void                 print_sdp_command	(struct sdp_command *);
52 static void                 usage		(void);
53 
54 /* Main */
55 int
56 main(int argc, char *argv[])
57 {
58 	char const	*control = SDP_LOCAL_PATH;
59 	int		 n, local;
60 	bdaddr_t	 bdaddr;
61 
62 	memset(&bdaddr, 0, sizeof(bdaddr));
63 	local = 0;
64 
65 	/* Process command line arguments */
66 	while ((n = getopt(argc, argv, "a:c:lh")) != -1) {
67 		switch (n) {
68 		case 'a': /* bdaddr */
69 			if (!bt_aton(optarg, &bdaddr)) {
70 				struct hostent  *he = NULL;
71 
72 				if ((he = bt_gethostbyname(optarg)) == NULL)
73 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
74 
75 				memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
76 			}
77 			break;
78 
79 		case 'c': /* control socket */
80 			control = optarg;
81 			break;
82 
83 		case 'l': /* local sdpd */
84 			local = 1;
85 			break;
86 
87 		case 'h':
88 		default:
89 			usage();
90 			/* NOT REACHED */
91 		}
92 	}
93 
94 	argc -= optind;
95 	argv += optind;
96 
97 	if (*argv == NULL)
98 		usage();
99 
100 	return (do_sdp_command(&bdaddr, control, local, argc, argv));
101 }
102 
103 /* Execute commands */
104 static int
105 do_sdp_command(bdaddr_p bdaddr, char const *control, int local,
106 		int argc, char **argv)
107 {
108 	char			*cmd = argv[0];
109 	struct sdp_command	*c = NULL;
110 	void			*xs = NULL;
111 	int			 e, help;
112 
113 	help = 0;
114 	if (strcasecmp(cmd, "help") == 0) {
115 		argc --;
116 		argv ++;
117 
118 		if (argc <= 0) {
119 			fprintf(stdout, "Supported commands:\n");
120 			print_sdp_command(sdp_commands);
121 			fprintf(stdout, "\nFor more information use " \
122 				"'help command'\n");
123 
124 			return (OK);
125 		}
126 
127 		help = 1;
128 		cmd = argv[0];
129 	}
130 
131 	c = find_sdp_command(cmd, sdp_commands);
132 	if (c == NULL) {
133 		fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
134 		return (ERROR);
135 	}
136 
137 	if (!help) {
138 		if (!local) {
139 			if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
140 				usage();
141 
142 			xs = sdp_open(NG_HCI_BDADDR_ANY, bdaddr);
143 		} else
144 			xs = sdp_open_local(control);
145 
146 		if (xs == NULL)
147 			errx(1, "Could not create SDP session object");
148 		if (sdp_error(xs) == 0)
149 			e = (c->handler)(xs, -- argc, ++ argv);
150 		else
151 			e = ERROR;
152 	} else
153 		e = USAGE;
154 
155 	switch (e) {
156 	case OK:
157 	case FAILED:
158 		break;
159 
160 	case ERROR:
161 		fprintf(stdout, "Could not execute command \"%s\". %s\n",
162 			cmd, strerror(sdp_error(xs)));
163 		break;
164 
165 	case USAGE:
166 		fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
167 		break;
168 
169 	default: assert(0); break;
170 	}
171 
172 	sdp_close(xs);
173 
174 	return (e);
175 } /* do_sdp_command */
176 
177 /* Try to find command in specified category */
178 static struct sdp_command *
179 find_sdp_command(char const *command, struct sdp_command *category)
180 {
181 	struct sdp_command	*c = NULL;
182 
183 	for (c = category; c->command != NULL; c++) {
184 		char	*c_end = strchr(c->command, ' ');
185 
186 		if (c_end != NULL) {
187 			int	len = c_end - c->command;
188 
189 			if (strncasecmp(command, c->command, len) == 0)
190 				return (c);
191 		} else if (strcasecmp(command, c->command) == 0)
192 				return (c);
193 	}
194 
195 	return (NULL);
196 } /* find_sdp_command */
197 
198 /* Print commands in specified category */
199 static void
200 print_sdp_command(struct sdp_command *category)
201 {
202 	struct sdp_command	*c = NULL;
203 
204 	for (c = category; c->command != NULL; c++)
205 		fprintf(stdout, "\t%s\n", c->command);
206 } /* print_sdp_command */
207 
208 /* Usage */
209 static void
210 usage(void)
211 {
212 	fprintf(stderr,
213 "Usage: sdpcontrol options command\n" \
214 "Where options are:\n"
215 "	-a address	address to connect to\n" \
216 "	-c path		path to the control socket (default is %s)\n" \
217 "	-h		display usage and quit\n" \
218 "	-l		connect to the local SDP server via control socket\n" \
219 "	command		one of the supported commands\n", SDP_LOCAL_PATH);
220 	exit(255);
221 } /* usage */
222 
223