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