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 GIOChannel *l2cap_io = NULL, *unix_io = NULL; 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 g_io_channel_unref(chan); 211 return FALSE; 212 } 213 214 if (data == &l2cap_sock) { 215 struct sockaddr_l2 addr; 216 socklen_t len = sizeof(addr); 217 218 nsk = accept(l2cap_sock, (struct sockaddr *) &addr, &len); 219 } else if (data == &unix_sock) { 220 struct sockaddr_un addr; 221 socklen_t len = sizeof(addr); 222 223 nsk = accept(unix_sock, (struct sockaddr *) &addr, &len); 224 } else 225 return FALSE; 226 227 if (nsk < 0) { 228 error("Can't accept connection: %s", strerror(errno)); 229 return TRUE; 230 } 231 232 io = g_io_channel_unix_new(nsk); 233 g_io_channel_set_close_on_unref(io, TRUE); 234 235 g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, 236 io_session_event, data); 237 238 g_io_channel_unref(io); 239 240 return TRUE; 241 } 242 243 int start_sdp_server(uint16_t mtu, const char *did, uint32_t flags) 244 { 245 int compat = flags & SDP_SERVER_COMPAT; 246 int master = flags & SDP_SERVER_MASTER; 247 248 info("Starting SDP server"); 249 250 if (init_server(mtu, master, compat) < 0) { 251 error("Server initialization failed"); 252 return -1; 253 } 254 255 if (did && strlen(did) > 0) { 256 const char *ptr = did; 257 uint16_t vid = 0x0000, pid = 0x0000, ver = 0x0000; 258 259 vid = (uint16_t) strtol(ptr, NULL, 16); 260 ptr = strchr(ptr, ':'); 261 if (ptr) { 262 pid = (uint16_t) strtol(ptr + 1, NULL, 16); 263 ptr = strchr(ptr + 1, ':'); 264 if (ptr) 265 ver = (uint16_t) strtol(ptr + 1, NULL, 16); 266 register_device_id(vid, pid, ver); 267 } 268 } 269 270 l2cap_io = g_io_channel_unix_new(l2cap_sock); 271 g_io_channel_set_close_on_unref(l2cap_io, TRUE); 272 273 g_io_add_watch(l2cap_io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, 274 io_accept_event, &l2cap_sock); 275 276 if (compat && unix_sock > fileno(stderr)) { 277 unix_io = g_io_channel_unix_new(unix_sock); 278 g_io_channel_set_close_on_unref(unix_io, TRUE); 279 280 g_io_add_watch(unix_io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, 281 io_accept_event, &unix_sock); 282 } 283 284 return 0; 285 } 286 287 void stop_sdp_server(void) 288 { 289 info("Stopping SDP server"); 290 291 sdp_svcdb_reset(); 292 293 if (unix_io) 294 g_io_channel_unref(unix_io); 295 296 if (l2cap_io) 297 g_io_channel_unref(l2cap_io); 298 } 299