Home | History | Annotate | Line # | Download | only in btpand
server.c revision 1.2
      1  1.2  plunky /*	$NetBSD: server.c,v 1.2 2009/01/24 17:29:28 plunky Exp $	*/
      2  1.1  plunky 
      3  1.1  plunky /*-
      4  1.1  plunky  * Copyright (c) 2008 Iain Hibbert
      5  1.1  plunky  * All rights reserved.
      6  1.1  plunky  *
      7  1.1  plunky  * Redistribution and use in source and binary forms, with or without
      8  1.1  plunky  * modification, are permitted provided that the following conditions
      9  1.1  plunky  * are met:
     10  1.1  plunky  * 1. Redistributions of source code must retain the above copyright
     11  1.1  plunky  *    notice, this list of conditions and the following disclaimer.
     12  1.1  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  plunky  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  plunky  *    documentation and/or other materials provided with the distribution.
     15  1.1  plunky  *
     16  1.1  plunky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  plunky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  plunky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  plunky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  plunky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  plunky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  plunky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  plunky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  plunky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  plunky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  plunky  */
     27  1.1  plunky 
     28  1.1  plunky #include <sys/cdefs.h>
     29  1.2  plunky __RCSID("$NetBSD: server.c,v 1.2 2009/01/24 17:29:28 plunky Exp $");
     30  1.1  plunky 
     31  1.1  plunky #include <sys/ioctl.h>
     32  1.1  plunky 
     33  1.1  plunky #include <bluetooth.h>
     34  1.1  plunky #include <errno.h>
     35  1.1  plunky #include <sdp.h>
     36  1.1  plunky #include <unistd.h>
     37  1.1  plunky 
     38  1.1  plunky #include "btpand.h"
     39  1.1  plunky #include "bnep.h"
     40  1.1  plunky 
     41  1.1  plunky static struct event	server_ev;
     42  1.1  plunky static int		server_fd;
     43  1.2  plunky static int		server_avail;
     44  1.1  plunky 
     45  1.1  plunky static void *		server_ss;
     46  1.1  plunky static uint32_t		server_handle;
     47  1.1  plunky 
     48  1.1  plunky static void server_open(void);
     49  1.1  plunky static void server_close(void);
     50  1.1  plunky static void server_read(int, short, void *);
     51  1.1  plunky static void server_register(void);
     52  1.1  plunky 
     53  1.1  plunky void
     54  1.1  plunky server_init(void)
     55  1.1  plunky {
     56  1.1  plunky 
     57  1.1  plunky 	server_fd = -1;
     58  1.1  plunky }
     59  1.1  plunky 
     60  1.1  plunky /*
     61  1.1  plunky  * The server_update() function is called whenever the channel count is
     62  1.1  plunky  * changed. We maintain the SDP record and open or close the server socket
     63  1.1  plunky  * as required.
     64  1.1  plunky  */
     65  1.1  plunky void
     66  1.1  plunky server_update(int count)
     67  1.1  plunky {
     68  1.1  plunky 
     69  1.1  plunky 	if (server_limit == 0)
     70  1.1  plunky 		return;
     71  1.1  plunky 
     72  1.1  plunky 	log_debug("count %d", count);
     73  1.1  plunky 
     74  1.2  plunky 	server_avail = UINT8_MAX - (count - 1) * UINT8_MAX / server_limit;
     75  1.2  plunky 	log_info("Service Availability: %d/%d", server_avail, UINT8_MAX);
     76  1.1  plunky 
     77  1.2  plunky 	if (server_avail == 0 && server_fd != -1)
     78  1.1  plunky 		server_close();
     79  1.1  plunky 
     80  1.2  plunky 	if (server_avail > 0 && server_fd == -1)
     81  1.1  plunky 		server_open();
     82  1.1  plunky 
     83  1.1  plunky 	if (service_name)
     84  1.1  plunky 		server_register();
     85  1.1  plunky }
     86  1.1  plunky 
     87  1.1  plunky static void
     88  1.1  plunky server_open(void)
     89  1.1  plunky {
     90  1.1  plunky 	struct sockaddr_bt sa;
     91  1.1  plunky 	uint16_t mru;
     92  1.1  plunky 
     93  1.1  plunky 	server_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
     94  1.1  plunky 	if (server_fd == -1) {
     95  1.1  plunky 		log_err("Could not open L2CAP socket: %m");
     96  1.1  plunky 		exit(EXIT_FAILURE);
     97  1.1  plunky 	}
     98  1.1  plunky 
     99  1.1  plunky 	memset(&sa, 0, sizeof(sa));
    100  1.1  plunky 	sa.bt_family = AF_BLUETOOTH;
    101  1.1  plunky 	sa.bt_len = sizeof(sa);
    102  1.1  plunky 	sa.bt_psm = l2cap_psm;
    103  1.1  plunky 	bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
    104  1.1  plunky 	if (bind(server_fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    105  1.1  plunky 		log_err("Could not bind server socket: %m");
    106  1.1  plunky 		exit(EXIT_FAILURE);
    107  1.1  plunky 	}
    108  1.1  plunky 
    109  1.1  plunky 	if (setsockopt(server_fd, BTPROTO_L2CAP,
    110  1.1  plunky 	    SO_L2CAP_LM, &l2cap_mode, sizeof(l2cap_mode)) == -1) {
    111  1.1  plunky 		log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
    112  1.1  plunky 		exit(EXIT_FAILURE);
    113  1.1  plunky 	}
    114  1.1  plunky 
    115  1.1  plunky 	mru = BNEP_MTU_MIN;
    116  1.1  plunky 	if (setsockopt(server_fd, BTPROTO_L2CAP,
    117  1.1  plunky 	    SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
    118  1.1  plunky 		log_err("Could not set L2CAP IMTU (%d): %m", mru);
    119  1.1  plunky 		exit(EXIT_FAILURE);
    120  1.1  plunky 	}
    121  1.1  plunky 
    122  1.1  plunky 	if (listen(server_fd, 0) == -1) {
    123  1.1  plunky 		log_err("Could not listen on server socket: %m");
    124  1.1  plunky 		exit(EXIT_FAILURE);
    125  1.1  plunky 	}
    126  1.1  plunky 
    127  1.1  plunky 	event_set(&server_ev, server_fd, EV_READ | EV_PERSIST, server_read, NULL);
    128  1.1  plunky 	if (event_add(&server_ev, NULL) == -1) {
    129  1.1  plunky 		log_err("Could not add server event: %m");
    130  1.1  plunky 		exit(EXIT_FAILURE);
    131  1.1  plunky 	}
    132  1.1  plunky 
    133  1.1  plunky 	log_info("server socket open");
    134  1.1  plunky }
    135  1.1  plunky 
    136  1.1  plunky static void
    137  1.1  plunky server_close(void)
    138  1.1  plunky {
    139  1.1  plunky 
    140  1.1  plunky 	event_del(&server_ev);
    141  1.1  plunky 	close(server_fd);
    142  1.1  plunky 	server_fd = -1;
    143  1.1  plunky 
    144  1.1  plunky 	log_info("server socket closed");
    145  1.1  plunky }
    146  1.1  plunky 
    147  1.1  plunky /*
    148  1.1  plunky  * handle connection request
    149  1.1  plunky  */
    150  1.1  plunky static void
    151  1.1  plunky server_read(int s, short ev, void *arg)
    152  1.1  plunky {
    153  1.1  plunky 	struct sockaddr_bt ra, la;
    154  1.1  plunky 	channel_t *chan;
    155  1.1  plunky 	socklen_t len;
    156  1.1  plunky 	int fd, n;
    157  1.1  plunky 	uint16_t mru, mtu;
    158  1.1  plunky 
    159  1.1  plunky 	len = sizeof(ra);
    160  1.1  plunky 	fd = accept(s, (struct sockaddr *)&ra, &len);
    161  1.1  plunky 	if (fd == -1)
    162  1.1  plunky 		return;
    163  1.1  plunky 
    164  1.1  plunky 	n = 1;
    165  1.1  plunky 	if (ioctl(fd, FIONBIO, &n) == -1) {
    166  1.1  plunky 		log_err("Could not set NonBlocking IO: %m");
    167  1.1  plunky 		close(fd);
    168  1.1  plunky 		return;
    169  1.1  plunky 	}
    170  1.1  plunky 
    171  1.1  plunky 	len = sizeof(mru);
    172  1.1  plunky 	if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) {
    173  1.1  plunky 		log_err("Could not get L2CAP IMTU: %m");
    174  1.1  plunky 		close(fd);
    175  1.1  plunky 		return;
    176  1.1  plunky 	}
    177  1.1  plunky 	if(mru < BNEP_MTU_MIN) {
    178  1.1  plunky 		log_err("L2CAP IMTU too small (%d)", mru);
    179  1.1  plunky 		close(fd);
    180  1.1  plunky 		return;
    181  1.1  plunky 	}
    182  1.1  plunky 
    183  1.1  plunky 	len = sizeof(mtu);
    184  1.1  plunky 	if (getsockopt(fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) {
    185  1.1  plunky 		log_err("Could not get L2CAP OMTU: %m");
    186  1.1  plunky 		close(fd);
    187  1.1  plunky 		return;
    188  1.1  plunky 	}
    189  1.1  plunky 	if (mtu < BNEP_MTU_MIN) {
    190  1.1  plunky 		log_err("L2CAP OMTU too small (%d)", mtu);
    191  1.1  plunky 		close(fd);
    192  1.1  plunky 		return;
    193  1.1  plunky 	}
    194  1.1  plunky 
    195  1.1  plunky 	len = sizeof(n);
    196  1.1  plunky 	if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, &len) == -1) {
    197  1.1  plunky 		log_err("Could not get socket send buffer size: %m");
    198  1.1  plunky 		close(fd);
    199  1.1  plunky 		return;
    200  1.1  plunky 	}
    201  1.1  plunky 
    202  1.1  plunky 	if (n < (mtu * 2)) {
    203  1.1  plunky 		n = mtu * 2;
    204  1.1  plunky 		if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) == -1) {
    205  1.1  plunky 			log_err("Could not set socket send buffer size (%d): %m", n);
    206  1.1  plunky 			close(fd);
    207  1.1  plunky 			return;
    208  1.1  plunky 		}
    209  1.1  plunky 	}
    210  1.1  plunky 
    211  1.1  plunky 	n = mtu;
    212  1.1  plunky 	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &n, sizeof(n)) == -1) {
    213  1.1  plunky 		log_err("Could not set socket low water mark (%d): %m", n);
    214  1.1  plunky 		close(fd);
    215  1.1  plunky 		return;
    216  1.1  plunky 	}
    217  1.1  plunky 
    218  1.1  plunky 	len = sizeof(la);
    219  1.1  plunky 	if (getsockname(fd, (struct sockaddr *)&la, &len) == -1) {
    220  1.1  plunky 		log_err("Could not get socket address: %m");
    221  1.1  plunky 		close(fd);
    222  1.1  plunky 		return;
    223  1.1  plunky 	}
    224  1.1  plunky 
    225  1.1  plunky 	log_info("Accepted connection from %s", bt_ntoa(&ra.bt_bdaddr, NULL));
    226  1.1  plunky 
    227  1.1  plunky 	chan = channel_alloc();
    228  1.1  plunky 	if (chan == NULL) {
    229  1.1  plunky 		close(fd);
    230  1.1  plunky 		return;
    231  1.1  plunky 	}
    232  1.1  plunky 
    233  1.1  plunky 	chan->send = bnep_send;
    234  1.1  plunky 	chan->recv = bnep_recv;
    235  1.1  plunky 	chan->mru = mru;
    236  1.1  plunky 	chan->mtu = mtu;
    237  1.1  plunky 	b2eaddr(chan->raddr, &ra.bt_bdaddr);
    238  1.1  plunky 	b2eaddr(chan->laddr, &la.bt_bdaddr);
    239  1.1  plunky 	chan->state = CHANNEL_WAIT_CONNECT_REQ;
    240  1.1  plunky 	channel_timeout(chan, 10);
    241  1.1  plunky 	if (!channel_open(chan, fd)) {
    242  1.1  plunky 		chan->state = CHANNEL_CLOSED;
    243  1.1  plunky 		channel_free(chan);
    244  1.1  plunky 		close(fd);
    245  1.1  plunky 		return;
    246  1.1  plunky 	}
    247  1.1  plunky }
    248  1.1  plunky 
    249  1.1  plunky static void
    250  1.1  plunky server_register(void)
    251  1.1  plunky {
    252  1.1  plunky 	sdp_nap_profile_t p;
    253  1.1  plunky 	int rv;
    254  1.1  plunky 
    255  1.1  plunky 	if (server_ss == NULL) {
    256  1.1  plunky 		server_ss = sdp_open_local(control_path);
    257  1.1  plunky 		if (server_ss == NULL || sdp_error(server_ss) != 0) {
    258  1.1  plunky 			log_err("failed to contact SDP server");
    259  1.1  plunky 			return;
    260  1.1  plunky 		}
    261  1.1  plunky 	}
    262  1.1  plunky 
    263  1.2  plunky 	memset(&p, 0, sizeof(p));
    264  1.2  plunky 	p.psm = l2cap_psm;
    265  1.2  plunky 	p.load_factor = server_avail;
    266  1.2  plunky 	p.security_description = (l2cap_mode == 0 ? 0x0000 : 0x0001);
    267  1.1  plunky 
    268  1.1  plunky 	if (server_handle)
    269  1.1  plunky 		rv = sdp_change_service(server_ss, server_handle,
    270  1.1  plunky 		    (uint8_t *)&p, sizeof(p));
    271  1.1  plunky 	else
    272  1.1  plunky 		rv = sdp_register_service(server_ss, service_class,
    273  1.1  plunky 		    &local_bdaddr, (uint8_t *)&p, sizeof(p), &server_handle);
    274  1.1  plunky 
    275  1.1  plunky 	if (rv != 0) {
    276  1.1  plunky 		errno = sdp_error(server_ss);
    277  1.1  plunky 		log_err("%s: %m", service_name);
    278  1.1  plunky 		exit(EXIT_FAILURE);
    279  1.1  plunky 	}
    280  1.1  plunky }
    281