1 /* $NetBSD: session.c,v 1.2 2007/04/07 21:08:46 plunky Exp $ */
2 /* $DragonFly: src/lib/libsdp/session.c,v 1.1 2008/01/03 11:47:53 hasso Exp $ */
5 * Copyright (c) 2006 Itronix Inc.
8 * Written by Iain Hibbert for Itronix Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Itronix Inc. may not be used to endorse
19 * or promote products derived from this software without specific
20 * prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
37 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * $Id: session.c,v 1.2 2007/04/07 21:08:46 plunky Exp $
62 * $FreeBSD: src/lib/libsdp/session.c,v 1.3 2004/01/09 22:44:28 emax Exp $
65 #include <sys/types.h>
67 #include <bluetooth.h>
77 sdp_open(bdaddr_t const *l, bdaddr_t const *r)
79 sdp_session_p ss = NULL;
80 struct sockaddr_bt sa;
84 if ((ss = calloc(1, sizeof(*ss))) == NULL)
87 if (l == NULL || r == NULL) {
92 ss->s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
98 memset(&li, 0, sizeof(li));
101 if (setsockopt(ss->s, SOL_SOCKET, SO_LINGER, &li, sizeof(li)) < 0) {
106 memset(&sa, 0, sizeof(sa));
107 sa.bt_len = sizeof(sa);
108 sa.bt_family = AF_BLUETOOTH;
109 bdaddr_copy(&sa.bt_bdaddr, l);
110 if (bind(ss->s, (void *) &sa, sizeof(sa)) < 0) {
115 sa.bt_psm = L2CAP_PSM_SDP;
116 bdaddr_copy(&sa.bt_bdaddr, r);
117 if (connect(ss->s, (void *) &sa, sizeof(sa)) < 0) {
122 size = sizeof(ss->omtu);
123 if (getsockopt(ss->s, BTPROTO_L2CAP, SO_L2CAP_OMTU, &ss->omtu, &size) < 0) {
127 if ((ss->req = malloc((size_t)ss->omtu)) == NULL) {
131 ss->req_e = ss->req + ss->omtu;
133 size = sizeof(ss->imtu);
134 if (getsockopt(ss->s, BTPROTO_L2CAP, SO_L2CAP_IMTU, &ss->imtu, &size) < 0) {
138 if ((ss->rsp = malloc((size_t)ss->imtu)) == NULL) {
142 ss->rsp_e = ss->rsp + ss->imtu;
145 return ((void *) ss);
149 sdp_open_local(char const *control)
151 sdp_session_p ss = NULL;
152 struct sockaddr_un sa;
154 if ((ss = calloc(1, sizeof(*ss))) == NULL)
157 ss->s = socket(PF_LOCAL, SOCK_STREAM, 0);
164 control = SDP_LOCAL_PATH;
166 sa.sun_len = sizeof(sa);
167 sa.sun_family = AF_LOCAL;
168 strlcpy(sa.sun_path, control, sizeof(sa.sun_path));
170 if (connect(ss->s, (void *) &sa, sizeof(sa)) < 0) {
175 ss->flags |= SDP_SESSION_LOCAL;
176 ss->imtu = ss->omtu = SDP_LOCAL_MTU;
178 if ((ss->req = malloc((size_t)ss->omtu)) == NULL) {
182 ss->req_e = ss->req + ss->omtu;
184 if ((ss->rsp = malloc((size_t)ss->imtu)) == NULL) {
188 ss->rsp_e = ss->rsp + ss->imtu;
191 return ((void *) ss);
197 sdp_session_p ss = (sdp_session_p) xss;
208 memset(ss, 0, sizeof(*ss));
218 sdp_session_p ss = (sdp_session_p) xss;
220 return ((ss != NULL)? ss->error : EINVAL);