session.c revision 121054
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 121054 2003-10-12 22:04:24Z 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 *
105121054Semaxsdp_open_local(void)
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
119121054Semax	sa.sun_len = sizeof(sa);
120121054Semax	sa.sun_family = AF_UNIX;
121121054Semax	strlcpy(sa.sun_path, SDP_UNSOCK_PATH, sizeof(sa.sun_path));
122121054Semax
123121054Semax	if (connect(ss->s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
124121054Semax		ss->error = errno;
125121054Semax		goto fail;
126121054Semax	}
127121054Semax
128121054Semax	ss->flags |= SDP_SESSION_LOCAL;
129121054Semax	ss->imtu = ss->omtu = SDP_UNSOCK_MTU;
130121054Semax
131121054Semax	if ((ss->req = malloc(ss->omtu)) == NULL) {
132121054Semax		ss->error = ENOMEM;
133121054Semax		goto fail;
134121054Semax	}
135121054Semax	ss->req_e = ss->req + ss->omtu;
136121054Semax
137121054Semax	if ((ss->rsp = malloc(ss->imtu)) == NULL) {
138121054Semax		ss->error = ENOMEM;
139121054Semax		goto fail;
140121054Semax	}
141121054Semax	ss->rsp_e = ss->rsp + ss->imtu;
142121054Semax	ss->error = 0;
143121054Semaxfail:
144121054Semax	return ((void *) ss);
145121054Semax}
146121054Semax
147121054Semaxint32_t
148121054Semaxsdp_close(void *xss)
149121054Semax{
150121054Semax	sdp_session_p	ss = (sdp_session_p) xss;
151121054Semax
152121054Semax	if (ss != NULL) {
153121054Semax		if (ss->s >= 0)
154121054Semax			close(ss->s);
155121054Semax
156121054Semax		if (ss->req != NULL)
157121054Semax			free(ss->req);
158121054Semax		if (ss->rsp != NULL)
159121054Semax			free(ss->rsp);
160121054Semax
161121054Semax		memset(ss, 0, sizeof(*ss));
162121054Semax		free(ss);
163121054Semax	}
164121054Semax
165121054Semax	return (0);
166121054Semax}
167121054Semax
168121054Semaxint32_t
169121054Semaxsdp_error(void *xss)
170121054Semax{
171121054Semax	sdp_session_p	ss = (sdp_session_p) xss;
172121054Semax
173121054Semax	return ((ss != NULL)? ss->error : EINVAL);
174121054Semax}
175