4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: session.c,v 1.2 2003/09/04 22:12:13 max Exp $
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
44 sdp_open(bdaddr_t const *l, bdaddr_t const *r)
46 sdp_session_p ss = NULL;
47 struct sockaddr_l2cap sa;
50 if ((ss = calloc(1, sizeof(*ss))) == NULL)
53 if (l == NULL || r == NULL) {
58 ss->s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BLUETOOTH_PROTO_L2CAP);
64 sa.l2cap_len = sizeof(sa);
65 sa.l2cap_family = AF_BLUETOOTH;
68 sa.l2cap_bdaddr_type = BDADDR_BREDR;
70 memcpy(&sa.l2cap_bdaddr, l, sizeof(sa.l2cap_bdaddr));
71 if (bind(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
76 sa.l2cap_psm = htole16(NG_L2CAP_PSM_SDP);
77 memcpy(&sa.l2cap_bdaddr, r, sizeof(sa.l2cap_bdaddr));
78 if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
83 size = sizeof(ss->omtu);
84 if (getsockopt(ss->s, SOL_L2CAP, SO_L2CAP_OMTU, &ss->omtu, &size) < 0) {
88 if ((ss->req = malloc(ss->omtu)) == NULL) {
92 ss->req_e = ss->req + ss->omtu;
94 size = sizeof(ss->imtu);
95 if (getsockopt(ss->s, SOL_L2CAP, SO_L2CAP_IMTU, &ss->imtu, &size) < 0) {
99 if ((ss->rsp = malloc(ss->imtu)) == NULL) {
103 ss->rsp_e = ss->rsp + ss->imtu;
106 return ((void *) ss);
110 sdp_open_local(char const *control)
112 sdp_session_p ss = NULL;
113 struct sockaddr_un sa;
115 if ((ss = calloc(1, sizeof(*ss))) == NULL)
118 ss->s = socket(PF_UNIX, SOCK_STREAM, 0);
125 control = SDP_LOCAL_PATH;
127 sa.sun_len = sizeof(sa);
128 sa.sun_family = AF_UNIX;
129 strlcpy(sa.sun_path, control, sizeof(sa.sun_path));
131 if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
136 ss->flags |= SDP_SESSION_LOCAL;
137 ss->imtu = ss->omtu = SDP_LOCAL_MTU;
139 if ((ss->req = malloc(ss->omtu)) == NULL) {
143 ss->req_e = ss->req + ss->omtu;
145 if ((ss->rsp = malloc(ss->imtu)) == NULL) {
149 ss->rsp_e = ss->rsp + ss->imtu;
152 return ((void *) ss);
158 sdp_session_p ss = (sdp_session_p) xss;
169 memset(ss, 0, sizeof(*ss));
179 sdp_session_p ss = (sdp_session_p) xss;
181 return ((ss != NULL)? ss->error : EINVAL);
185 sdp_get_lcaddr(void *xss, bdaddr_t *l)
187 sdp_session_p ss = (sdp_session_p) xss;
188 struct sockaddr_l2cap sa;
191 if (l == NULL || ss == NULL || ss->flags & SDP_SESSION_LOCAL) {
197 if (getsockname(ss->s, (struct sockaddr *)&sa, &size) == 0) {
198 bdaddr_copy(l, &sa.l2cap_bdaddr);
203 return ((ss->error == 0) ? 0 : -1);