1*9dc6fb5cSplunky /* $NetBSD: sdp_session.c,v 1.3 2010/11/13 19:43:56 plunky Exp $ */
2dfbf818aSplunky
3dfbf818aSplunky /*-
4dfbf818aSplunky * Copyright (c) 2009 The NetBSD Foundation, Inc.
5dfbf818aSplunky * All rights reserved.
6dfbf818aSplunky *
7dfbf818aSplunky * This code is derived from software contributed to The NetBSD Foundation
8dfbf818aSplunky * by Iain Hibbert.
9dfbf818aSplunky *
10dfbf818aSplunky * Redistribution and use in source and binary forms, with or without
11dfbf818aSplunky * modification, are permitted provided that the following conditions
12dfbf818aSplunky * are met:
13dfbf818aSplunky * 1. Redistributions of source code must retain the above copyright
14dfbf818aSplunky * notice, this list of conditions and the following disclaimer.
15dfbf818aSplunky * 2. Redistributions in binary form must reproduce the above copyright
16dfbf818aSplunky * notice, this list of conditions and the following disclaimer in the
17dfbf818aSplunky * documentation and/or other materials provided with the distribution.
18dfbf818aSplunky *
19dfbf818aSplunky * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20dfbf818aSplunky * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21dfbf818aSplunky * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22dfbf818aSplunky * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23dfbf818aSplunky * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24dfbf818aSplunky * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25dfbf818aSplunky * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26dfbf818aSplunky * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27dfbf818aSplunky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28dfbf818aSplunky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29dfbf818aSplunky * POSSIBILITY OF SUCH DAMAGE.
30dfbf818aSplunky */
31dfbf818aSplunky
32dfbf818aSplunky #include <sys/cdefs.h>
33*9dc6fb5cSplunky __RCSID("$NetBSD: sdp_session.c,v 1.3 2010/11/13 19:43:56 plunky Exp $");
34dfbf818aSplunky
35dfbf818aSplunky #include <sys/socket.h>
36dfbf818aSplunky #include <sys/un.h>
37dfbf818aSplunky
38dfbf818aSplunky #include <errno.h>
39dfbf818aSplunky #include <sdp.h>
40dfbf818aSplunky #include <stdlib.h>
41dfbf818aSplunky #include <string.h>
42dfbf818aSplunky #include <unistd.h>
43dfbf818aSplunky
44dfbf818aSplunky #include "sdp-int.h"
45dfbf818aSplunky
46dfbf818aSplunky /*
47dfbf818aSplunky * open session with remote Bluetooth SDP server
48dfbf818aSplunky */
49dfbf818aSplunky struct sdp_session *
_sdp_open(const bdaddr_t * laddr,const bdaddr_t * raddr)50dfbf818aSplunky _sdp_open(const bdaddr_t *laddr, const bdaddr_t *raddr)
51dfbf818aSplunky {
52dfbf818aSplunky struct sdp_session * ss;
53dfbf818aSplunky struct sockaddr_bt sa;
54dfbf818aSplunky struct linger li;
55dfbf818aSplunky socklen_t len;
56dfbf818aSplunky
57dfbf818aSplunky ss = calloc(1, sizeof(struct sdp_session));
58dfbf818aSplunky if (ss == NULL)
59dfbf818aSplunky goto fail;
60dfbf818aSplunky
61dfbf818aSplunky ss->s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
62dfbf818aSplunky if (ss->s == -1)
63dfbf818aSplunky goto fail;
64dfbf818aSplunky
65dfbf818aSplunky memset(&li, 0, sizeof(li));
66dfbf818aSplunky li.l_onoff = 1;
67dfbf818aSplunky li.l_linger = 5;
68dfbf818aSplunky if (setsockopt(ss->s, SOL_SOCKET, SO_LINGER, &li, sizeof(li)) == -1)
69dfbf818aSplunky goto fail;
70dfbf818aSplunky
71dfbf818aSplunky if (laddr == NULL)
72dfbf818aSplunky laddr = BDADDR_ANY;
73dfbf818aSplunky
74dfbf818aSplunky memset(&sa, 0, sizeof(sa));
75dfbf818aSplunky sa.bt_len = sizeof(sa);
76dfbf818aSplunky sa.bt_family = AF_BLUETOOTH;
77dfbf818aSplunky bdaddr_copy(&sa.bt_bdaddr, laddr);
78dfbf818aSplunky if (bind(ss->s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
79dfbf818aSplunky goto fail;
80dfbf818aSplunky
81dfbf818aSplunky sa.bt_psm = L2CAP_PSM_SDP;
82dfbf818aSplunky bdaddr_copy(&sa.bt_bdaddr, raddr);
83dfbf818aSplunky if (connect(ss->s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
84dfbf818aSplunky goto fail;
85dfbf818aSplunky
86dfbf818aSplunky len = sizeof(ss->imtu);
87dfbf818aSplunky if (getsockopt(ss->s, BTPROTO_L2CAP, SO_L2CAP_IMTU, &ss->imtu, &len) == -1)
88dfbf818aSplunky goto fail;
89dfbf818aSplunky
90*9dc6fb5cSplunky ss->ibuf = malloc((size_t)(ss->imtu));
91dfbf818aSplunky if (ss->ibuf == NULL)
92dfbf818aSplunky goto fail;
93dfbf818aSplunky
94dfbf818aSplunky return ss;
95dfbf818aSplunky
96dfbf818aSplunky fail:
97dfbf818aSplunky _sdp_close(ss);
98dfbf818aSplunky return NULL;
99dfbf818aSplunky }
100dfbf818aSplunky
101dfbf818aSplunky /*
102dfbf818aSplunky * open session with local SDP server
103dfbf818aSplunky */
104dfbf818aSplunky struct sdp_session *
_sdp_open_local(const char * control)105dfbf818aSplunky _sdp_open_local(const char *control)
106dfbf818aSplunky {
107dfbf818aSplunky struct sdp_session * ss;
108dfbf818aSplunky struct sockaddr_un sa;
109dfbf818aSplunky
110dfbf818aSplunky ss = calloc(1, sizeof(struct sdp_session));
111dfbf818aSplunky if (ss == NULL)
112dfbf818aSplunky goto fail;
113dfbf818aSplunky
114dfbf818aSplunky ss->s = socket(PF_LOCAL, SOCK_STREAM, 0);
115dfbf818aSplunky if (ss->s == -1)
116dfbf818aSplunky goto fail;
117dfbf818aSplunky
118dfbf818aSplunky if (control == NULL)
119dfbf818aSplunky control = SDP_LOCAL_PATH;
120dfbf818aSplunky
121dfbf818aSplunky memset(&sa, 0, sizeof(sa));
122dfbf818aSplunky sa.sun_len = sizeof(sa);
123dfbf818aSplunky sa.sun_family = AF_LOCAL;
124dfbf818aSplunky strlcpy(sa.sun_path, control, sizeof(sa.sun_path));
125dfbf818aSplunky if (connect(ss->s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
126dfbf818aSplunky goto fail;
127dfbf818aSplunky
128dfbf818aSplunky ss->imtu = L2CAP_MTU_DEFAULT;
129dfbf818aSplunky
130*9dc6fb5cSplunky ss->ibuf = malloc((size_t)(ss->imtu));
131dfbf818aSplunky if (ss->ibuf == NULL)
132dfbf818aSplunky goto fail;
133dfbf818aSplunky
134dfbf818aSplunky return ss;
135dfbf818aSplunky
136dfbf818aSplunky fail:
137dfbf818aSplunky _sdp_close(ss);
138dfbf818aSplunky return NULL;
139dfbf818aSplunky }
140dfbf818aSplunky
141dfbf818aSplunky /*
142dfbf818aSplunky * close session and release all resources
143dfbf818aSplunky */
144dfbf818aSplunky void
_sdp_close(struct sdp_session * ss)145dfbf818aSplunky _sdp_close(struct sdp_session *ss)
146dfbf818aSplunky {
147dfbf818aSplunky
148dfbf818aSplunky if (ss == NULL)
149dfbf818aSplunky return;
150dfbf818aSplunky
151dfbf818aSplunky if (ss->s != -1)
152dfbf818aSplunky close(ss->s);
153dfbf818aSplunky
154dfbf818aSplunky if (ss->ibuf != NULL)
155dfbf818aSplunky free(ss->ibuf);
156dfbf818aSplunky
157dfbf818aSplunky if (ss->rbuf != NULL)
158dfbf818aSplunky free(ss->rbuf);
159dfbf818aSplunky
160dfbf818aSplunky free(ss);
161dfbf818aSplunky }
162dfbf818aSplunky
163dfbf818aSplunky /*
164dfbf818aSplunky * internal function; send a PDU on session
165dfbf818aSplunky *
166dfbf818aSplunky * caller provides an iovec array with an empty slot at the beginning for
167dfbf818aSplunky * PDU header, num is total iovec count.
168dfbf818aSplunky */
169dfbf818aSplunky bool
_sdp_send_pdu(struct sdp_session * ss,uint8_t pid,struct iovec * iov,int num)170dfbf818aSplunky _sdp_send_pdu(struct sdp_session *ss, uint8_t pid, struct iovec *iov, int num)
171dfbf818aSplunky {
172dfbf818aSplunky sdp_pdu_t pdu;
173dfbf818aSplunky ssize_t len, nw;
174dfbf818aSplunky int i;
175dfbf818aSplunky
176dfbf818aSplunky for (len = 0, i = 1; i < num; i++)
177dfbf818aSplunky len += iov[i].iov_len;
178dfbf818aSplunky
179dfbf818aSplunky if (len > UINT16_MAX) {
180dfbf818aSplunky errno = EMSGSIZE;
181dfbf818aSplunky return false;
182dfbf818aSplunky }
183dfbf818aSplunky
184dfbf818aSplunky ss->tid += 1;
185dfbf818aSplunky
186dfbf818aSplunky pdu.pid = pid;
187dfbf818aSplunky pdu.tid = htobe16(ss->tid);
188dfbf818aSplunky pdu.len = htobe16(len);
189dfbf818aSplunky
190dfbf818aSplunky iov[0].iov_base = &pdu;
191dfbf818aSplunky iov[0].iov_len = sizeof(pdu);
192dfbf818aSplunky
193dfbf818aSplunky do {
194dfbf818aSplunky nw = writev(ss->s, iov, num);
195dfbf818aSplunky } while (nw == -1 && errno == EINTR);
196dfbf818aSplunky
1978ebedbbbSplunky if ((size_t)nw != sizeof(pdu) + len) {
198dfbf818aSplunky errno = EIO;
199dfbf818aSplunky return false;
200dfbf818aSplunky }
201dfbf818aSplunky
202dfbf818aSplunky return true;
203dfbf818aSplunky }
204dfbf818aSplunky
205dfbf818aSplunky /*
206dfbf818aSplunky * internal function; receive a PDU on session
207dfbf818aSplunky *
208dfbf818aSplunky * validate the PDU and transaction IDs and data length, stores
209dfbf818aSplunky * received data in the session incoming buffer.
210dfbf818aSplunky */
211dfbf818aSplunky ssize_t
_sdp_recv_pdu(struct sdp_session * ss,uint8_t pid)212dfbf818aSplunky _sdp_recv_pdu(struct sdp_session *ss, uint8_t pid)
213dfbf818aSplunky {
214dfbf818aSplunky struct iovec iov[2];
215dfbf818aSplunky sdp_pdu_t pdu;
216dfbf818aSplunky ssize_t nr;
217dfbf818aSplunky
218dfbf818aSplunky iov[0].iov_base = &pdu;
219dfbf818aSplunky iov[0].iov_len = sizeof(pdu);
220dfbf818aSplunky
221dfbf818aSplunky iov[1].iov_base = ss->ibuf;
222dfbf818aSplunky iov[1].iov_len = ss->imtu;
223dfbf818aSplunky
224dfbf818aSplunky do {
225dfbf818aSplunky nr = readv(ss->s, iov, __arraycount(iov));
226dfbf818aSplunky } while (nr == -1 && errno == EINTR);
227dfbf818aSplunky
228dfbf818aSplunky if (nr == -1)
229dfbf818aSplunky return -1;
230dfbf818aSplunky
2318ebedbbbSplunky if ((size_t)nr < sizeof(pdu)) {
232dfbf818aSplunky errno = EIO;
233dfbf818aSplunky return -1;
234dfbf818aSplunky }
235dfbf818aSplunky
236dfbf818aSplunky pdu.tid = be16toh(pdu.tid);
237dfbf818aSplunky pdu.len = be16toh(pdu.len);
238dfbf818aSplunky
239dfbf818aSplunky if (pid != pdu.pid
240dfbf818aSplunky || ss->tid != pdu.tid
2418ebedbbbSplunky || (size_t)nr != sizeof(pdu) + pdu.len) {
242dfbf818aSplunky if (pdu.pid == SDP_PDU_ERROR_RESPONSE
243dfbf818aSplunky && pdu.len == sizeof(uint16_t))
244dfbf818aSplunky errno = _sdp_errno(be16dec(ss->ibuf));
245dfbf818aSplunky else
246dfbf818aSplunky errno = EIO;
247dfbf818aSplunky
248dfbf818aSplunky return -1;
249dfbf818aSplunky }
250dfbf818aSplunky
251dfbf818aSplunky return pdu.len;
252dfbf818aSplunky }
253dfbf818aSplunky
254dfbf818aSplunky /*
255dfbf818aSplunky * translate ErrorCode to errno
256dfbf818aSplunky */
257dfbf818aSplunky int
_sdp_errno(uint16_t ec)258dfbf818aSplunky _sdp_errno(uint16_t ec)
259dfbf818aSplunky {
260dfbf818aSplunky
261dfbf818aSplunky switch (ec) {
262dfbf818aSplunky case SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE:
263dfbf818aSplunky return ENOATTR;
264dfbf818aSplunky
265dfbf818aSplunky case SDP_ERROR_CODE_INVALID_SDP_VERSION:
266dfbf818aSplunky case SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX:
267dfbf818aSplunky case SDP_ERROR_CODE_INVALID_PDU_SIZE:
268dfbf818aSplunky case SDP_ERROR_CODE_INVALID_CONTINUATION_STATE:
269dfbf818aSplunky case SDP_ERROR_CODE_INSUFFICIENT_RESOURCES:
270dfbf818aSplunky default:
271dfbf818aSplunky return EIO;
272dfbf818aSplunky }
273dfbf818aSplunky }
274