/*-
* Copyright (c) 2008-2009 Iain Hibbert
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
len = sizeof(ra);
fd = accept(s, (struct sockaddr *)&ra, &len);
if (fd == -1)
return;
n = 1;
if (ioctl(fd, FIONBIO, &n) == -1) {
log_err("Could not set NonBlocking IO: %m");
close(fd);
return;
}
len = sizeof(mru);
if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
log_err("Could not get L2CAP IMTU: %m");
close(fd);
return;
}
if(mru < BNEP_MTU_MIN) {
log_err("L2CAP IMTU too small (%d)", mru);
close(fd);
return;
}
len = sizeof(n);
if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, &len) == -1) {
log_err("Could not read SO_RCVBUF");
close(fd);
return;
}
if (n < 10 * mru) {
n = 10 * mru;
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1)
log_info("Could not increase SO_RCVBUF (to %d)", n);
}
len = sizeof(mtu);
if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
log_err("Could not get L2CAP OMTU: %m");
close(fd);
return;
}
if (mtu < BNEP_MTU_MIN) {
log_err("L2CAP OMTU too small (%d)", mtu);
close(fd);
return;
}
len = sizeof(n);
if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
log_err("Could not get socket send buffer size: %m");
close(fd);
return;
}
if (n < (mtu * 2)) {
n = mtu * 2;
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
log_err("Could not set socket send buffer size (%d): %m", n);
close(fd);
return;
}
}
n = mtu;
if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
log_err("Could not set socket low water mark (%d): %m", n);
close(fd);
return;
}
len = sizeof(la);
if (getsockname(fd, (struct sockaddr *)&la, &len) == -1) {
log_err("Could not get socket address: %m");
close(fd);
return;
}
log_info("Accepted connection from %s", bt_ntoa(&ra.bt_bdaddr, NULL));
if (++server_count == server_limit) {
log_info("Server limit reached, closing server socket");
event_del(&server_ev);
close(s);
}
server_update();
}
/*
* Shut down a server channel, we need to update the service record and
* may want to restart accepting connections on the server socket
*/
static void
server_down(channel_t *chan)
{
assert(server_count > 0);
channel_close(chan);
if (server_count-- == server_limit)
server_open();
server_update();
}
static void
server_update(void)
{
bool rv;
if (service_type == NULL)
return;
if (server_ss == NULL) {
server_ss = sdp_open_local(control_path);
if (server_ss == NULL) {
log_err("failed to contact SDP server");
return;
}
}