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