1 /* 2 * 3 * BlueZ - Bluetooth protocol stack for Linux 4 * 5 * Copyright (C) 2001-2002 Nokia Corporation 6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk (at) qualcomm.com> 7 * Copyright (C) 2002-2010 Marcel Holtmann <marcel (at) holtmann.org> 8 * Copyright (C) 2002-2003 Stephen Crane <steve.crane (at) rococosoft.com> 9 * 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 * 25 */ 26 27 #ifdef HAVE_CONFIG_H 28 #include <config.h> 29 #endif 30 31 #include <stdio.h> 32 #include <errno.h> 33 #include <unistd.h> 34 #include <stdlib.h> 35 #include <sys/stat.h> 36 #include <sys/socket.h> 37 #include <cutils/sockets.h> 38 39 #include <bluetooth/bluetooth.h> 40 #include <bluetooth/l2cap.h> 41 #include <bluetooth/sdp.h> 42 #include <bluetooth/sdp_lib.h> 43 44 #include <sys/un.h> 45 #include <netinet/in.h> 46 47 #include <glib.h> 48 49 #include "log.h" 50 #include "sdpd.h" 51 52 static guint l2cap_id = 0, unix_id = 0; 53 54 static int l2cap_sock, unix_sock; 55 56 /* 57 * SDP server initialization on startup includes creating the 58 * l2cap and unix sockets over which discovery and registration clients 59 * access us respectively 60 */ 61 static int init_server(uint16_t mtu, int master, int compat) 62 { 63 struct l2cap_options opts; 64 struct sockaddr_l2 l2addr; 65 struct sockaddr_un unaddr; 66 socklen_t optlen; 67 68 /* Register the public browse group root */ 69 register_public_browse_group(); 70 71 /* Register the SDP server's service record */ 72 register_server_service(); 73 74 /* Create L2CAP socket */ 75 l2cap_sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); 76 if (l2cap_sock < 0) { 77 error("opening L2CAP socket: %s", strerror(errno)); 78 return -1; 79 } 80 81 memset(&l2addr, 0, sizeof(l2addr)); 82 l2addr.l2_family = AF_BLUETOOTH; 83 bacpy(&l2addr.l2_bdaddr, BDADDR_ANY); 84 l2addr.l2_psm = htobs(SDP_PSM); 85 86 if (bind(l2cap_sock, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) { 87 error("binding L2CAP socket: %s", strerror(errno)); 88 return -1; 89 } 90 91 if (master) { 92 int opt = L2CAP_LM_MASTER; 93 if (setsockopt(l2cap_sock, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0) { 94 error("setsockopt: %s", strerror(errno)); 95 return -1; 96 } 97 } 98 99 if (mtu > 0) { 100 memset(&opts, 0, sizeof(opts)); 101 optlen = sizeof(opts); 102 103 if (getsockopt(l2cap_sock, SOL_L2CAP, L2CAP_OPTIONS, &opts, &optlen) < 0) { 104 error("getsockopt: %s", strerror(errno)); 105 return -1; 106 } 107 108 opts.omtu = mtu; 109 opts.imtu = mtu; 110 111 if (setsockopt(l2cap_sock, SOL_L2CAP, L2CAP_OPTIONS, &opts, sizeof(opts)) < 0) { 112 error("setsockopt: %s", strerror(errno)); 113 return -1; 114 } 115 } 116 117 if (listen(l2cap_sock, 5) < 0) { 118 error("listen: %s", strerror(errno)); 119 return -1; 120 } 121 122 if (!compat) { 123 unix_sock = -1; 124 return 0; 125 } 126 #if 0 127 /* Create local Unix socket */ 128 unix_sock = socket(PF_UNIX, SOCK_STREAM, 0); 129 if (unix_sock < 0) { 130 error("opening UNIX socket: %s", strerror(errno)); 131 return -1; 132 } 133 134 memset(&unaddr, 0, sizeof(unaddr)); 135 unaddr.sun_family = AF_UNIX; 136 strcpy(unaddr.sun_path, SDP_UNIX_PATH); 137 138 unlink(unaddr.sun_path); 139 140 if (bind(unix_sock, (struct sockaddr *) &unaddr, sizeof(unaddr)) < 0) { 141 error("binding UNIX socket: %s", strerror(errno)); 142 return -1; 143 } 144 145 listen(unix_sock, 5); 146 147 chmod(SDP_UNIX_PATH, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); 148 #else 149 unix_sock = android_get_control_socket("bluetooth"); 150 if (unix_sock < 0) { 151 error("Unable to get the control socket for 'bluetooth'"); 152 return -1; 153 } 154 155 if (listen(unix_sock, 5)) { 156 error("Listening on local socket failed: %s", strerror(errno)); 157 return -1; 158 } 159 160 info("Got Unix socket fd '%d' from environment", unix_sock); 161 #endif 162 return 0; 163 } 164 165 static gboolean io_session_event(GIOChannel *chan, GIOCondition cond, gpointer data) 166 { 167 sdp_pdu_hdr_t hdr; 168 uint8_t *buf; 169 int sk, len, size; 170 171 if (cond & G_IO_NVAL) 172 return FALSE; 173 174 sk = g_io_channel_unix_get_fd(chan); 175 176 if (cond & (G_IO_HUP | G_IO_ERR)) { 177 sdp_svcdb_collect_all(sk); 178 return FALSE; 179 } 180 181 len = recv(sk, &hdr, sizeof(sdp_pdu_hdr_t), MSG_PEEK); 182 if (len <= 0) { 183 sdp_svcdb_collect_all(sk); 184 return FALSE; 185 } 186 187 size = sizeof(sdp_pdu_hdr_t) + ntohs(hdr.plen); 188 buf = malloc(size); 189 if (!buf) 190 return TRUE; 191 192 len = recv(sk, buf, size, 0); 193 if (len <= 0) { 194 sdp_svcdb_collect_all(sk); 195 free(buf); 196 return FALSE; 197 } 198 199 handle_request(sk, buf, len); 200 201 return TRUE; 202 } 203 204 static gboolean io_accept_event(GIOChannel *chan, GIOCondition cond, gpointer data) 205 { 206 GIOChannel *io; 207 int nsk; 208 209 if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) 210 return FALSE; 211 212 if (data == &l2cap_sock) { 213 struct sockaddr_l2 addr; 214 socklen_t len = sizeof(addr); 215 216 nsk = accept(l2cap_sock, (struct sockaddr *) &addr, &len); 217 } else if (data == &unix_sock) { 218 struct sockaddr_un addr; 219 socklen_t len = sizeof(addr); 220 221 nsk = accept(unix_sock, (struct sockaddr *) &addr, &len); 222 } else 223 return FALSE; 224 225 if (nsk < 0) { 226 error("Can't accept connection: %s", strerror(errno)); 227 return TRUE; 228 } 229 230 io = g_io_channel_unix_new(nsk); 231 g_io_channel_set_close_on_unref(io, TRUE); 232 233 g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, 234 io_session_event, data); 235 236 g_io_channel_unref(io); 237 238 return TRUE; 239 } 240 241 int start_sdp_server(uint16_t mtu, const char *did, uint32_t flags) 242 { 243 int compat = flags & SDP_SERVER_COMPAT; 244 int master = flags & SDP_SERVER_MASTER; 245 GIOChannel *io; 246 247 info("Starting SDP server"); 248 249 if (init_server(mtu, master, compat) < 0) { 250 error("Server initialization failed"); 251 return -1; 252 } 253 254 if (did && strlen(did) > 0) { 255 const char *ptr = did; 256 uint16_t vid = 0x0000, pid = 0x0000, ver = 0x0000; 257 258 vid = (uint16_t) strtol(ptr, NULL, 16); 259 ptr = strchr(ptr, ':'); 260 if (ptr) { 261 pid = (uint16_t) strtol(ptr + 1, NULL, 16); 262 ptr = strchr(ptr + 1, ':'); 263 if (ptr) 264 ver = (uint16_t) strtol(ptr + 1, NULL, 16); 265 register_device_id(vid, pid, ver); 266 } 267 } 268 269 io = g_io_channel_unix_new(l2cap_sock); 270 g_io_channel_set_close_on_unref(io, TRUE); 271 272 l2cap_id = g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, 273 io_accept_event, &l2cap_sock); 274 g_io_channel_unref(io); 275 276 if (compat && unix_sock > fileno(stderr)) { 277 io = g_io_channel_unix_new(unix_sock); 278 g_io_channel_set_close_on_unref(io, TRUE); 279 280 unix_id = g_io_add_watch(io, 281 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, 282 io_accept_event, &unix_sock); 283 g_io_channel_unref(io); 284 } 285 286 return 0; 287 } 288 289 void stop_sdp_server(void) 290 { 291 info("Stopping SDP server"); 292 293 sdp_svcdb_reset(); 294 295 if (unix_id > 0) 296 g_source_remove(unix_id); 297 298 if (l2cap_id > 0) 299 g_source_remove(l2cap_id); 300 301 l2cap_id = unix_id = 0; 302 l2cap_sock = unix_sock = -1; 303 } 304