1 /* $NetBSD: client.c,v 1.2 2008/12/06 20:01:14 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: client.c,v 1.2 2008/12/06 20:01:14 plunky Exp $"); 32 33 #include <bluetooth.h> 34 #include <errno.h> 35 #include <sdp.h> 36 #include <unistd.h> 37 38 #include "btpand.h" 39 #include "bnep.h" 40 #include "sdp.h" 41 42 static void client_query(void); 43 44 void 45 client_init(void) 46 { 47 struct sockaddr_l2cap sa; 48 channel_t *chan; 49 socklen_t len; 50 int fd; 51 uint16_t mru, mtu; 52 53 if (bdaddr_any(&remote_bdaddr)) 54 return; 55 56 if (service_name) 57 client_query(); 58 59 fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); 60 if (fd == -1) { 61 log_err("Could not open L2CAP socket: %m"); 62 exit(EXIT_FAILURE); 63 } 64 65 memset(&sa, 0, sizeof(sa)); 66 sa.l2cap_family = AF_BLUETOOTH; 67 sa.l2cap_len = sizeof(sa); 68 bdaddr_copy(&sa.l2cap_bdaddr, &local_bdaddr); 69 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { 70 log_err("Could not bind client socket: %m"); 71 exit(EXIT_FAILURE); 72 } 73 74 mru = BNEP_MTU_MIN; 75 if (setsockopt(fd, SOL_L2CAP, SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) { 76 log_err("Could not set L2CAP IMTU (%d): %m", mru); 77 exit(EXIT_FAILURE); 78 } 79 80 log_info("Opening connection to service 0x%4.4x at %s", 81 service_class, bt_ntoa(&remote_bdaddr, NULL)); 82 83 sa.l2cap_psm = htole16(l2cap_psm); 84 bdaddr_copy(&sa.l2cap_bdaddr, &remote_bdaddr); 85 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { 86 log_err("Could not connect: %m"); 87 exit(EXIT_FAILURE); 88 } 89 90 len = sizeof(mru); 91 if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_IMTU, &mru, &len) == -1) { 92 log_err("Could not get IMTU: %m"); 93 exit(EXIT_FAILURE); 94 } 95 if (mru < BNEP_MTU_MIN) { 96 log_err("L2CAP IMTU too small (%d)", mru); 97 exit(EXIT_FAILURE); 98 } 99 100 len = sizeof(mtu); 101 if (getsockopt(fd, SOL_L2CAP, SO_L2CAP_OMTU, &mtu, &len) == -1) { 102 log_err("Could not get L2CAP OMTU: %m"); 103 exit(EXIT_FAILURE); 104 } 105 if (mtu < BNEP_MTU_MIN) { 106 log_err("L2CAP OMTU too small (%d)", mtu); 107 exit(EXIT_FAILURE); 108 } 109 110 chan = channel_alloc(); 111 if (chan == NULL) 112 exit(EXIT_FAILURE); 113 114 chan->send = bnep_send; 115 chan->recv = bnep_recv; 116 chan->mru = mru; 117 chan->mtu = mtu; 118 b2eaddr(chan->raddr, &remote_bdaddr); 119 b2eaddr(chan->laddr, &local_bdaddr); 120 chan->state = CHANNEL_WAIT_CONNECT_RSP; 121 channel_timeout(chan, 10); 122 if (!channel_open(chan, fd)) 123 exit(EXIT_FAILURE); 124 125 bnep_send_control(chan, BNEP_SETUP_CONNECTION_REQUEST, 126 2, service_class, SDP_SERVICE_CLASS_PANU); 127 } 128 129 static void 130 client_query(void) 131 { 132 uint8_t buffer[512]; 133 sdp_attr_t attr; 134 uint32_t range; 135 void *ss; 136 int rv; 137 uint8_t *seq0, *seq1; 138 139 attr.flags = SDP_ATTR_INVALID; 140 attr.attr = 0; 141 attr.vlen = sizeof(buffer); 142 attr.value = buffer; 143 144 range = SDP_ATTR_RANGE(SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST, 145 SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST); 146 147 ss = sdp_open(&local_bdaddr, &remote_bdaddr); 148 if (ss == NULL || (errno = sdp_error(ss)) != 0) { 149 log_err("%s: %m", service_name); 150 exit(EXIT_FAILURE); 151 } 152 153 log_info("Searching for %s service at %s", 154 service_name, bt_ntoa(&remote_bdaddr, NULL)); 155 156 rv = sdp_search(ss, 1, &service_class, 1, &range, 1, &attr); 157 if (rv != 0) { 158 log_err("%s: %s", service_name, strerror(sdp_error(ss))); 159 exit(EXIT_FAILURE); 160 } 161 162 sdp_close(ss); 163 164 if (attr.flags != SDP_ATTR_OK 165 || attr.attr != SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST) { 166 log_err("%s service not found", service_name); 167 exit(EXIT_FAILURE); 168 } 169 170 /* 171 * we expect the following protocol descriptor list 172 * 173 * seq len 174 * seq len 175 * uuid value == L2CAP 176 * uint16 value16 => PSM 177 * seq len 178 * uuid value == BNEP 179 */ 180 if (_sdp_get_seq(&attr.value, attr.value + attr.vlen, &seq0) 181 && _sdp_get_seq(&seq0, attr.value, &seq1) 182 && _sdp_match_uuid16(&seq1, seq0, SDP_UUID_PROTOCOL_L2CAP) 183 && _sdp_get_uint16(&seq1, seq0, &l2cap_psm) 184 && _sdp_get_seq(&seq0, attr.value, &seq1) 185 && _sdp_match_uuid16(&seq1, seq0, SDP_UUID_PROTOCOL_BNEP)) { 186 log_info("Found PSM %d for service %s", l2cap_psm, service_name); 187 return; 188 } 189 190 log_err("%s query failed", service_name); 191 exit(EXIT_FAILURE); 192 } 193