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@qualcomm.com>
7 *  Copyright (C) 2002-2009  Marcel Holtmann <marcel@holtmann.org>
8 *  Copyright (C) 2002-2003  Stephen Crane <steve.crane@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 "logging.h"
50#include "sdpd.h"
51
52static GIOChannel *l2cap_io = NULL, *unix_io = NULL;
53
54static 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 */
61static 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	listen(l2cap_sock, 5);
118
119	if (!compat) {
120		unix_sock = -1;
121		return 0;
122	}
123#if 0
124        /* Create local Unix socket */
125        unix_sock = socket(PF_UNIX, SOCK_STREAM, 0);
126        if (unix_sock < 0) {
127                error("opening UNIX socket: %s", strerror(errno));
128                return -1;
129        }
130
131        memset(&unaddr, 0, sizeof(unaddr));
132        unaddr.sun_family = AF_UNIX;
133        strcpy(unaddr.sun_path, SDP_UNIX_PATH);
134
135        unlink(unaddr.sun_path);
136
137        if (bind(unix_sock, (struct sockaddr *) &unaddr, sizeof(unaddr)) < 0) {
138                error("binding UNIX socket: %s", strerror(errno));
139                return -1;
140        }
141
142        listen(unix_sock, 5);
143
144        chmod(SDP_UNIX_PATH, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
145#else
146        unix_sock = android_get_control_socket("bluetooth");
147        if (unix_sock < 0) {
148                error("Unable to get the control socket for 'bluetooth'");
149                return -1;
150        }
151
152        if (listen(unix_sock, 5)) {
153                error("Listening on local socket failed: %s", strerror(errno));
154                return -1;
155        }
156
157        info("Got Unix socket fd '%d' from environment", unix_sock);
158#endif
159
160        return 0;
161}
162
163static gboolean io_session_event(GIOChannel *chan, GIOCondition cond, gpointer data)
164{
165	sdp_pdu_hdr_t hdr;
166	uint8_t *buf;
167	int sk, len, size;
168
169	if (cond & G_IO_NVAL)
170		return FALSE;
171
172	sk = g_io_channel_unix_get_fd(chan);
173
174	if (cond & (G_IO_HUP | G_IO_ERR)) {
175		sdp_svcdb_collect_all(sk);
176		return FALSE;
177	}
178
179	len = recv(sk, &hdr, sizeof(sdp_pdu_hdr_t), MSG_PEEK);
180	if (len <= 0) {
181		sdp_svcdb_collect_all(sk);
182		return FALSE;
183	}
184
185	size = sizeof(sdp_pdu_hdr_t) + ntohs(hdr.plen);
186	buf = malloc(size);
187	if (!buf)
188		return TRUE;
189
190	len = recv(sk, buf, size, 0);
191	if (len <= 0) {
192		sdp_svcdb_collect_all(sk);
193		free(buf);
194		return FALSE;
195	}
196
197	handle_request(sk, buf, len);
198
199	return TRUE;
200}
201
202static gboolean io_accept_event(GIOChannel *chan, GIOCondition cond, gpointer data)
203{
204	GIOChannel *io;
205	int nsk;
206
207	if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
208		g_io_channel_unref(chan);
209		return FALSE;
210	}
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
241int 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
246	info("Starting SDP server");
247
248	if (init_server(mtu, master, compat) < 0) {
249		error("Server initialization failed");
250		return -1;
251	}
252
253	if (did && strlen(did) > 0) {
254		const char *ptr = did;
255		uint16_t vid = 0x0000, pid = 0x0000, ver = 0x0000;
256
257		vid = (uint16_t) strtol(ptr, NULL, 16);
258		ptr = strchr(ptr, ':');
259		if (ptr) {
260			pid = (uint16_t) strtol(ptr + 1, NULL, 16);
261			ptr = strchr(ptr + 1, ':');
262			if (ptr)
263				ver = (uint16_t) strtol(ptr + 1, NULL, 16);
264			register_device_id(vid, pid, ver);
265		}
266	}
267
268	l2cap_io = g_io_channel_unix_new(l2cap_sock);
269	g_io_channel_set_close_on_unref(l2cap_io, TRUE);
270
271	g_io_add_watch(l2cap_io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
272					io_accept_event, &l2cap_sock);
273
274	if (compat && unix_sock > fileno(stderr)) {
275		unix_io = g_io_channel_unix_new(unix_sock);
276		g_io_channel_set_close_on_unref(unix_io, TRUE);
277
278		g_io_add_watch(unix_io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
279					io_accept_event, &unix_sock);
280	}
281
282	return 0;
283}
284
285void stop_sdp_server(void)
286{
287	info("Stopping SDP server");
288
289	sdp_svcdb_reset();
290
291	if (unix_io)
292		g_io_channel_unref(unix_io);
293
294	if (l2cap_io)
295		g_io_channel_unref(l2cap_io);
296}
297