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