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