Blame src/sdpd-server.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2001-2002  Nokia Corporation
Packit 34410b
 *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
Packit 34410b
 *  Copyright (C) 2002-2010  Marcel Holtmann <marcel@holtmann.org>
Packit 34410b
 *  Copyright (C) 2002-2003  Stephen Crane <steve.crane@rococosoft.com>
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This program is free software; you can redistribute it and/or modify
Packit 34410b
 *  it under the terms of the GNU General Public License as published by
Packit 34410b
 *  the Free Software Foundation; either version 2 of the License, or
Packit 34410b
 *  (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This program is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 34410b
 *  GNU General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU General Public License
Packit 34410b
 *  along with this program; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#define _GNU_SOURCE
Packit 34410b
#include <errno.h>
Packit 34410b
#include <unistd.h>
Packit 34410b
#include <stdlib.h>
Packit 34410b
#include <stdbool.h>
Packit 34410b
#include <sys/stat.h>
Packit 34410b
#include <sys/un.h>
Packit 34410b
Packit 34410b
#include <glib.h>
Packit 34410b
Packit 34410b
#include "lib/bluetooth.h"
Packit 34410b
#include "lib/l2cap.h"
Packit 34410b
#include "lib/sdp.h"
Packit 34410b
#include "lib/sdp_lib.h"
Packit 34410b
Packit 34410b
#include "log.h"
Packit 34410b
#include "sdpd.h"
Packit 34410b
Packit 34410b
static guint l2cap_id = 0, unix_id = 0;
Packit 34410b
static int l2cap_sock = -1, unix_sock = -1;
Packit 34410b
Packit 34410b
/*
Packit 34410b
 * SDP server initialization on startup includes creating the
Packit 34410b
 * l2cap and unix sockets over which discovery and registration clients
Packit 34410b
 * access us respectively
Packit 34410b
 */
Packit 34410b
static int init_server(uint16_t mtu, int master, int compat)
Packit 34410b
{
Packit 34410b
	struct l2cap_options opts;
Packit 34410b
	struct sockaddr_l2 l2addr;
Packit 34410b
	struct sockaddr_un unaddr;
Packit 34410b
	socklen_t optlen;
Packit 34410b
Packit 34410b
	/* Register the public browse group root */
Packit 34410b
	register_public_browse_group();
Packit 34410b
Packit 34410b
	/* Register the SDP server's service record */
Packit 34410b
	register_server_service();
Packit 34410b
Packit 34410b
	/* Create L2CAP socket */
Packit 34410b
	l2cap_sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
Packit 34410b
	if (l2cap_sock < 0) {
Packit 34410b
		error("opening L2CAP socket: %s", strerror(errno));
Packit 34410b
		return -1;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	memset(&l2addr, 0, sizeof(l2addr));
Packit 34410b
	l2addr.l2_family = AF_BLUETOOTH;
Packit 34410b
	bacpy(&l2addr.l2_bdaddr, BDADDR_ANY);
Packit 34410b
	l2addr.l2_psm = htobs(SDP_PSM);
Packit 34410b
Packit 34410b
	if (bind(l2cap_sock, (struct sockaddr *) &l2addr, sizeof(l2addr)) < 0) {
Packit 34410b
		error("binding L2CAP socket: %s", strerror(errno));
Packit 34410b
		return -1;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (master) {
Packit 34410b
		int opt = L2CAP_LM_MASTER;
Packit 34410b
		if (setsockopt(l2cap_sock, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0) {
Packit 34410b
			error("setsockopt: %s", strerror(errno));
Packit 34410b
			return -1;
Packit 34410b
		}
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (mtu > 0) {
Packit 34410b
		memset(&opts, 0, sizeof(opts));
Packit 34410b
		optlen = sizeof(opts);
Packit 34410b
Packit 34410b
		if (getsockopt(l2cap_sock, SOL_L2CAP, L2CAP_OPTIONS, &opts, &optlen) < 0) {
Packit 34410b
			error("getsockopt: %s", strerror(errno));
Packit 34410b
			return -1;
Packit 34410b
		}
Packit 34410b
Packit 34410b
		opts.omtu = mtu;
Packit 34410b
		opts.imtu = mtu;
Packit 34410b
Packit 34410b
		if (setsockopt(l2cap_sock, SOL_L2CAP, L2CAP_OPTIONS, &opts, sizeof(opts)) < 0) {
Packit 34410b
			error("setsockopt: %s", strerror(errno));
Packit 34410b
			return -1;
Packit 34410b
		}
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (listen(l2cap_sock, 5) < 0) {
Packit 34410b
		error("listen: %s", strerror(errno));
Packit 34410b
		return -1;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (!compat) {
Packit 34410b
		unix_sock = -1;
Packit 34410b
		return 0;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	/* Create local Unix socket */
Packit 34410b
	unix_sock = socket(PF_UNIX, SOCK_STREAM, 0);
Packit 34410b
	if (unix_sock < 0) {
Packit 34410b
		error("opening UNIX socket: %s", strerror(errno));
Packit 34410b
		return -1;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	memset(&unaddr, 0, sizeof(unaddr));
Packit 34410b
	unaddr.sun_family = AF_UNIX;
Packit 34410b
	strcpy(unaddr.sun_path, SDP_UNIX_PATH);
Packit 34410b
Packit 34410b
	unlink(unaddr.sun_path);
Packit 34410b
Packit 34410b
	if (bind(unix_sock, (struct sockaddr *) &unaddr, sizeof(unaddr)) < 0) {
Packit 34410b
		error("binding UNIX socket: %s", strerror(errno));
Packit 34410b
		return -1;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	if (listen(unix_sock, 5) < 0) {
Packit 34410b
		error("listen UNIX socket: %s", strerror(errno));
Packit 34410b
		return -1;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	chmod(SDP_UNIX_PATH, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
Packit 34410b
Packit 34410b
	return 0;
Packit 34410b
}
Packit 34410b
Packit 34410b
static gboolean io_session_event(GIOChannel *chan, GIOCondition cond, gpointer data)
Packit 34410b
{
Packit 34410b
	sdp_pdu_hdr_t hdr;
Packit 34410b
	uint8_t *buf;
Packit 34410b
	int sk, len, size;
Packit 34410b
Packit 34410b
	if (cond & G_IO_NVAL)
Packit 34410b
		return FALSE;
Packit 34410b
Packit 34410b
	sk = g_io_channel_unix_get_fd(chan);
Packit 34410b
Packit 34410b
	if (cond & (G_IO_HUP | G_IO_ERR)) {
Packit 34410b
		sdp_svcdb_collect_all(sk);
Packit 34410b
		return FALSE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	len = recv(sk, &hdr, sizeof(sdp_pdu_hdr_t), MSG_PEEK);
Packit 34410b
	if (len < 0 || (unsigned int) len < sizeof(sdp_pdu_hdr_t)) {
Packit 34410b
		sdp_svcdb_collect_all(sk);
Packit 34410b
		return FALSE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	size = sizeof(sdp_pdu_hdr_t) + ntohs(hdr.plen);
Packit 34410b
	buf = malloc(size);
Packit 34410b
	if (!buf)
Packit 34410b
		return TRUE;
Packit 34410b
Packit 34410b
	len = recv(sk, buf, size, 0);
Packit 34410b
	/* Check here only that the received message is not empty.
Packit 34410b
	 * Incorrect length of message should be processed later
Packit 34410b
	 * inside handle_request() in order to produce ErrorResponse.
Packit 34410b
	 */
Packit 34410b
	if (len <= 0) {
Packit 34410b
		sdp_svcdb_collect_all(sk);
Packit 34410b
		free(buf);
Packit 34410b
		return FALSE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	handle_request(sk, buf, len);
Packit 34410b
Packit 34410b
	return TRUE;
Packit 34410b
}
Packit 34410b
Packit 34410b
static gboolean io_accept_event(GIOChannel *chan, GIOCondition cond, gpointer data)
Packit 34410b
{
Packit 34410b
	GIOChannel *io;
Packit 34410b
	int nsk;
Packit 34410b
Packit 34410b
	if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL))
Packit 34410b
		return FALSE;
Packit 34410b
Packit 34410b
	if (data == &l2cap_sock) {
Packit 34410b
		struct sockaddr_l2 addr;
Packit 34410b
		socklen_t len = sizeof(addr);
Packit 34410b
Packit 34410b
		nsk = accept(l2cap_sock, (struct sockaddr *) &addr, &len;;
Packit 34410b
	} else if (data == &unix_sock) {
Packit 34410b
		struct sockaddr_un addr;
Packit 34410b
		socklen_t len = sizeof(addr);
Packit 34410b
Packit 34410b
		nsk = accept(unix_sock, (struct sockaddr *) &addr, &len;;
Packit 34410b
	} else
Packit 34410b
		return FALSE;
Packit 34410b
Packit 34410b
	if (nsk < 0) {
Packit 34410b
		error("Can't accept connection: %s", strerror(errno));
Packit 34410b
		return TRUE;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	io = g_io_channel_unix_new(nsk);
Packit 34410b
	g_io_channel_set_close_on_unref(io, TRUE);
Packit 34410b
Packit 34410b
	g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
Packit 34410b
					io_session_event, data);
Packit 34410b
Packit 34410b
	g_io_channel_unref(io);
Packit 34410b
Packit 34410b
	return TRUE;
Packit 34410b
}
Packit 34410b
Packit 34410b
int start_sdp_server(uint16_t mtu, uint32_t flags)
Packit 34410b
{
Packit 34410b
	int compat = flags & SDP_SERVER_COMPAT;
Packit 34410b
	int master = flags & SDP_SERVER_MASTER;
Packit 34410b
	GIOChannel *io;
Packit 34410b
Packit 34410b
	info("Starting SDP server");
Packit 34410b
Packit 34410b
	if (init_server(mtu, master, compat) < 0) {
Packit 34410b
		error("Server initialization failed");
Packit 34410b
		return -1;
Packit 34410b
	}
Packit 34410b
Packit 34410b
	io = g_io_channel_unix_new(l2cap_sock);
Packit 34410b
	g_io_channel_set_close_on_unref(io, TRUE);
Packit 34410b
Packit 34410b
	l2cap_id = g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
Packit 34410b
					io_accept_event, &l2cap_sock);
Packit 34410b
	g_io_channel_unref(io);
Packit 34410b
Packit 34410b
	if (compat && unix_sock > fileno(stderr)) {
Packit 34410b
		io = g_io_channel_unix_new(unix_sock);
Packit 34410b
		g_io_channel_set_close_on_unref(io, TRUE);
Packit 34410b
Packit 34410b
		unix_id = g_io_add_watch(io,
Packit 34410b
					G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
Packit 34410b
					io_accept_event, &unix_sock);
Packit 34410b
		g_io_channel_unref(io);
Packit 34410b
	}
Packit 34410b
Packit 34410b
	return 0;
Packit 34410b
}
Packit 34410b
Packit 34410b
void stop_sdp_server(void)
Packit 34410b
{
Packit 34410b
	info("Stopping SDP server");
Packit 34410b
Packit 34410b
	sdp_svcdb_reset();
Packit 34410b
Packit 34410b
	if (unix_id > 0)
Packit 34410b
		g_source_remove(unix_id);
Packit 34410b
Packit 34410b
	if (l2cap_id > 0)
Packit 34410b
		g_source_remove(l2cap_id);
Packit 34410b
Packit 34410b
	l2cap_id = unix_id = 0;
Packit 34410b
	l2cap_sock = unix_sock = -1;
Packit 34410b
}