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