session.c revision 124317
1121054Semax/*
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: head/lib/libsdp/session.c 124317 2004-01-09 22:44:28Z emax $
30121054Semax */
31121054Semax
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;
65121054Semax	memcpy(&sa.l2cap_bdaddr, l, sizeof(sa.l2cap_bdaddr));
66121054Semax	if (bind(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
67121054Semax		ss->error = errno;
68121054Semax		goto fail;
69121054Semax	}
70121054Semax
71121054Semax	sa.l2cap_psm = htole16(NG_L2CAP_PSM_SDP);
72121054Semax	memcpy(&sa.l2cap_bdaddr, r, sizeof(sa.l2cap_bdaddr));
73121054Semax	if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
74121054Semax		ss->error = errno;
75121054Semax		goto fail;
76121054Semax	}
77121054Semax
78121054Semax	size = sizeof(ss->omtu);
79121054Semax	if (getsockopt(ss->s, SOL_L2CAP, SO_L2CAP_OMTU, &ss->omtu, &size) < 0) {
80121054Semax		ss->error = errno;
81121054Semax		goto fail;
82121054Semax	}
83121054Semax	if ((ss->req = malloc(ss->omtu)) == NULL) {
84121054Semax		ss->error = ENOMEM;
85121054Semax		goto fail;
86121054Semax	}
87121054Semax	ss->req_e = ss->req + ss->omtu;
88121054Semax
89121054Semax	size = sizeof(ss->imtu);
90121054Semax	if (getsockopt(ss->s, SOL_L2CAP, SO_L2CAP_IMTU, &ss->imtu, &size) < 0) {
91121054Semax		ss->error = errno;
92121054Semax		goto fail;
93121054Semax	}
94121054Semax	if ((ss->rsp = malloc(ss->imtu)) == NULL) {
95121054Semax		ss->error = ENOMEM;
96121054Semax		goto fail;
97121054Semax	}
98121054Semax	ss->rsp_e = ss->rsp + ss->imtu;
99121054Semax	ss->error = 0;
100121054Semaxfail:
101121054Semax	return ((void *) ss);
102121054Semax}
103121054Semax
104121054Semaxvoid *
105124317Semaxsdp_open_local(char const *control)
106121054Semax{
107121054Semax	sdp_session_p		ss = NULL;
108121054Semax	struct sockaddr_un	sa;
109121054Semax
110121054Semax	if ((ss = calloc(1, sizeof(*ss))) == NULL)
111121054Semax		goto fail;
112121054Semax
113121054Semax	ss->s = socket(PF_UNIX, SOCK_STREAM, 0);
114121054Semax	if (ss->s < 0) {
115121054Semax		ss->error = errno;
116121054Semax		goto fail;
117121054Semax	}
118121054Semax
119124317Semax	if (control == NULL)
120124317Semax		control = SDP_LOCAL_PATH;
121124317Semax
122121054Semax	sa.sun_len = sizeof(sa);
123121054Semax	sa.sun_family = AF_UNIX;
124124317Semax	strlcpy(sa.sun_path, control, sizeof(sa.sun_path));
125121054Semax
126121054Semax	if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
127121054Semax		ss->error = errno;
128121054Semax		goto fail;
129121054Semax	}
130121054Semax
131121054Semax	ss->flags |= SDP_SESSION_LOCAL;
132124305Semax	ss->imtu = ss->omtu = SDP_LOCAL_MTU;
133121054Semax
134121054Semax	if ((ss->req = malloc(ss->omtu)) == NULL) {
135121054Semax		ss->error = ENOMEM;
136121054Semax		goto fail;
137121054Semax	}
138121054Semax	ss->req_e = ss->req + ss->omtu;
139121054Semax
140121054Semax	if ((ss->rsp = malloc(ss->imtu)) == NULL) {
141121054Semax		ss->error = ENOMEM;
142121054Semax		goto fail;
143121054Semax	}
144121054Semax	ss->rsp_e = ss->rsp + ss->imtu;
145121054Semax	ss->error = 0;
146121054Semaxfail:
147121054Semax	return ((void *) ss);
148121054Semax}
149121054Semax
150121054Semaxint32_t
151121054Semaxsdp_close(void *xss)
152121054Semax{
153121054Semax	sdp_session_p	ss = (sdp_session_p) xss;
154121054Semax
155121054Semax	if (ss != NULL) {
156121054Semax		if (ss->s >= 0)
157121054Semax			close(ss->s);
158121054Semax
159121054Semax		if (ss->req != NULL)
160121054Semax			free(ss->req);
161121054Semax		if (ss->rsp != NULL)
162121054Semax			free(ss->rsp);
163121054Semax
164121054Semax		memset(ss, 0, sizeof(*ss));
165121054Semax		free(ss);
166121054Semax	}
167121054Semax
168121054Semax	return (0);
169121054Semax}
170121054Semax
171121054Semaxint32_t
172121054Semaxsdp_error(void *xss)
173121054Semax{
174121054Semax	sdp_session_p	ss = (sdp_session_p) xss;
175121054Semax
176121054Semax	return ((ss != NULL)? ss->error : EINVAL);
177121054Semax}
178