1331722Seadler/* 2121054Semax * session.c 3121054Semax * 4121054Semax * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5121054Semax * All rights reserved. 6121054Semax * 7121054Semax * Redistribution and use in source and binary forms, with or without 8121054Semax * modification, are permitted provided that the following conditions 9121054Semax * are met: 10121054Semax * 1. Redistributions of source code must retain the above copyright 11121054Semax * notice, this list of conditions and the following disclaimer. 12121054Semax * 2. Redistributions in binary form must reproduce the above copyright 13121054Semax * notice, this list of conditions and the following disclaimer in the 14121054Semax * documentation and/or other materials provided with the distribution. 15121054Semax * 16121054Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17121054Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18121054Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19121054Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20121054Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21121054Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22121054Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23121054Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24121054Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25121054Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26121054Semax * SUCH DAMAGE. 27121054Semax * 28121054Semax * $Id: session.c,v 1.2 2003/09/04 22:12:13 max Exp $ 29121054Semax * $FreeBSD$ 30121054Semax */ 31281210Stakawata#define L2CAP_SOCKET_CHECKED 32121054Semax#include <bluetooth.h> 33121054Semax#include <errno.h> 34121054Semax#include <stdlib.h> 35121054Semax#include <string.h> 36121054Semax#include <unistd.h> 37121054Semax 38121054Semax#include <sdp-int.h> 39121054Semax#include <sdp.h> 40121054Semax 41121054Semaxvoid * 42121054Semaxsdp_open(bdaddr_t const *l, bdaddr_t const *r) 43121054Semax{ 44121054Semax sdp_session_p ss = NULL; 45121054Semax struct sockaddr_l2cap sa; 46121054Semax socklen_t size; 47121054Semax 48121054Semax if ((ss = calloc(1, sizeof(*ss))) == NULL) 49121054Semax goto fail; 50121054Semax 51121054Semax if (l == NULL || r == NULL) { 52121054Semax ss->error = EINVAL; 53121054Semax goto fail; 54121054Semax } 55121054Semax 56121054Semax ss->s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP); 57121054Semax if (ss->s < 0) { 58121054Semax ss->error = errno; 59121054Semax goto fail; 60121054Semax } 61121054Semax 62121054Semax sa.l2cap_len = sizeof(sa); 63121054Semax sa.l2cap_family = AF_BLUETOOTH; 64121054Semax sa.l2cap_psm = 0; 65281210Stakawata sa.l2cap_cid = 0; 66281210Stakawata sa.l2cap_bdaddr_type = BDADDR_BREDR; 67281210Stakawata 68121054Semax memcpy(&sa.l2cap_bdaddr, l, sizeof(sa.l2cap_bdaddr)); 69121054Semax if (bind(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) { 70121054Semax ss->error = errno; 71121054Semax goto fail; 72121054Semax } 73121054Semax 74121054Semax sa.l2cap_psm = htole16(NG_L2CAP_PSM_SDP); 75121054Semax memcpy(&sa.l2cap_bdaddr, r, sizeof(sa.l2cap_bdaddr)); 76121054Semax if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) { 77121054Semax ss->error = errno; 78121054Semax goto fail; 79121054Semax } 80121054Semax 81121054Semax size = sizeof(ss->omtu); 82121054Semax if (getsockopt(ss->s, SOL_L2CAP, SO_L2CAP_OMTU, &ss->omtu, &size) < 0) { 83121054Semax ss->error = errno; 84121054Semax goto fail; 85121054Semax } 86121054Semax if ((ss->req = malloc(ss->omtu)) == NULL) { 87121054Semax ss->error = ENOMEM; 88121054Semax goto fail; 89121054Semax } 90121054Semax ss->req_e = ss->req + ss->omtu; 91121054Semax 92121054Semax size = sizeof(ss->imtu); 93121054Semax if (getsockopt(ss->s, SOL_L2CAP, SO_L2CAP_IMTU, &ss->imtu, &size) < 0) { 94121054Semax ss->error = errno; 95121054Semax goto fail; 96121054Semax } 97121054Semax if ((ss->rsp = malloc(ss->imtu)) == NULL) { 98121054Semax ss->error = ENOMEM; 99121054Semax goto fail; 100121054Semax } 101121054Semax ss->rsp_e = ss->rsp + ss->imtu; 102121054Semax ss->error = 0; 103121054Semaxfail: 104121054Semax return ((void *) ss); 105121054Semax} 106121054Semax 107121054Semaxvoid * 108124317Semaxsdp_open_local(char const *control) 109121054Semax{ 110121054Semax sdp_session_p ss = NULL; 111121054Semax struct sockaddr_un sa; 112121054Semax 113121054Semax if ((ss = calloc(1, sizeof(*ss))) == NULL) 114121054Semax goto fail; 115121054Semax 116121054Semax ss->s = socket(PF_UNIX, SOCK_STREAM, 0); 117121054Semax if (ss->s < 0) { 118121054Semax ss->error = errno; 119121054Semax goto fail; 120121054Semax } 121121054Semax 122124317Semax if (control == NULL) 123124317Semax control = SDP_LOCAL_PATH; 124124317Semax 125121054Semax sa.sun_len = sizeof(sa); 126121054Semax sa.sun_family = AF_UNIX; 127124317Semax strlcpy(sa.sun_path, control, sizeof(sa.sun_path)); 128121054Semax 129121054Semax if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) { 130121054Semax ss->error = errno; 131121054Semax goto fail; 132121054Semax } 133121054Semax 134121054Semax ss->flags |= SDP_SESSION_LOCAL; 135124305Semax ss->imtu = ss->omtu = SDP_LOCAL_MTU; 136121054Semax 137121054Semax if ((ss->req = malloc(ss->omtu)) == NULL) { 138121054Semax ss->error = ENOMEM; 139121054Semax goto fail; 140121054Semax } 141121054Semax ss->req_e = ss->req + ss->omtu; 142121054Semax 143121054Semax if ((ss->rsp = malloc(ss->imtu)) == NULL) { 144121054Semax ss->error = ENOMEM; 145121054Semax goto fail; 146121054Semax } 147121054Semax ss->rsp_e = ss->rsp + ss->imtu; 148121054Semax ss->error = 0; 149121054Semaxfail: 150121054Semax return ((void *) ss); 151121054Semax} 152121054Semax 153121054Semaxint32_t 154121054Semaxsdp_close(void *xss) 155121054Semax{ 156121054Semax sdp_session_p ss = (sdp_session_p) xss; 157121054Semax 158121054Semax if (ss != NULL) { 159121054Semax if (ss->s >= 0) 160121054Semax close(ss->s); 161121054Semax 162121054Semax if (ss->req != NULL) 163121054Semax free(ss->req); 164121054Semax if (ss->rsp != NULL) 165121054Semax free(ss->rsp); 166121054Semax 167121054Semax memset(ss, 0, sizeof(*ss)); 168121054Semax free(ss); 169121054Semax } 170121054Semax 171121054Semax return (0); 172121054Semax} 173121054Semax 174121054Semaxint32_t 175121054Semaxsdp_error(void *xss) 176121054Semax{ 177121054Semax sdp_session_p ss = (sdp_session_p) xss; 178121054Semax 179121054Semax return ((ss != NULL)? ss->error : EINVAL); 180121054Semax} 181