Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libbluetooth / sdp_session.c
blobfd9170bc7ee13a7bcf9ef6b6e837dd517e2c49ff
1 /* $NetBSD: sdp_session.c,v 1.1 2009/05/12 10:05:06 plunky Exp $ */
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Iain Hibbert.
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.
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.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: sdp_session.c,v 1.1 2009/05/12 10:05:06 plunky Exp $");
35 #include <sys/socket.h>
36 #include <sys/un.h>
38 #include <errno.h>
39 #include <sdp.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "sdp-int.h"
47 * open session with remote Bluetooth SDP server
49 struct sdp_session *
50 _sdp_open(const bdaddr_t *laddr, const bdaddr_t *raddr)
52 struct sdp_session * ss;
53 struct sockaddr_bt sa;
54 struct linger li;
55 socklen_t len;
57 ss = calloc(1, sizeof(struct sdp_session));
58 if (ss == NULL)
59 goto fail;
61 ss->s = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
62 if (ss->s == -1)
63 goto fail;
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;
71 if (laddr == NULL)
72 laddr = BDADDR_ANY;
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;
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;
86 len = sizeof(ss->imtu);
87 if (getsockopt(ss->s, BTPROTO_L2CAP, SO_L2CAP_IMTU, &ss->imtu, &len) == -1)
88 goto fail;
90 ss->ibuf = malloc(ss->imtu);
91 if (ss->ibuf == NULL)
92 goto fail;
94 return ss;
96 fail:
97 _sdp_close(ss);
98 return NULL;
102 * open session with local SDP server
104 struct sdp_session *
105 _sdp_open_local(const char *control)
107 struct sdp_session * ss;
108 struct sockaddr_un sa;
110 ss = calloc(1, sizeof(struct sdp_session));
111 if (ss == NULL)
112 goto fail;
114 ss->s = socket(PF_LOCAL, SOCK_STREAM, 0);
115 if (ss->s == -1)
116 goto fail;
118 if (control == NULL)
119 control = SDP_LOCAL_PATH;
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;
128 ss->imtu = L2CAP_MTU_DEFAULT;
130 ss->ibuf = malloc(ss->imtu);
131 if (ss->ibuf == NULL)
132 goto fail;
134 return ss;
136 fail:
137 _sdp_close(ss);
138 return NULL;
142 * close session and release all resources
144 void
145 _sdp_close(struct sdp_session *ss)
148 if (ss == NULL)
149 return;
151 if (ss->s != -1)
152 close(ss->s);
154 if (ss->ibuf != NULL)
155 free(ss->ibuf);
157 if (ss->rbuf != NULL)
158 free(ss->rbuf);
160 free(ss);
164 * internal function; send a PDU on session
166 * caller provides an iovec array with an empty slot at the beginning for
167 * PDU header, num is total iovec count.
169 bool
170 _sdp_send_pdu(struct sdp_session *ss, uint8_t pid, struct iovec *iov, int num)
172 sdp_pdu_t pdu;
173 ssize_t len, nw;
174 int i;
176 for (len = 0, i = 1; i < num; i++)
177 len += iov[i].iov_len;
179 if (len > UINT16_MAX) {
180 errno = EMSGSIZE;
181 return false;
184 ss->tid += 1;
186 pdu.pid = pid;
187 pdu.tid = htobe16(ss->tid);
188 pdu.len = htobe16(len);
190 iov[0].iov_base = &pdu;
191 iov[0].iov_len = sizeof(pdu);
193 do {
194 nw = writev(ss->s, iov, num);
195 } while (nw == -1 && errno == EINTR);
197 if ((size_t)nw != sizeof(pdu) + len) {
198 errno = EIO;
199 return false;
202 return true;
206 * internal function; receive a PDU on session
208 * validate the PDU and transaction IDs and data length, stores
209 * received data in the session incoming buffer.
211 ssize_t
212 _sdp_recv_pdu(struct sdp_session *ss, uint8_t pid)
214 struct iovec iov[2];
215 sdp_pdu_t pdu;
216 ssize_t nr;
218 iov[0].iov_base = &pdu;
219 iov[0].iov_len = sizeof(pdu);
221 iov[1].iov_base = ss->ibuf;
222 iov[1].iov_len = ss->imtu;
224 do {
225 nr = readv(ss->s, iov, __arraycount(iov));
226 } while (nr == -1 && errno == EINTR);
228 if (nr == -1)
229 return -1;
231 if ((size_t)nr < sizeof(pdu)) {
232 errno = EIO;
233 return -1;
236 pdu.tid = be16toh(pdu.tid);
237 pdu.len = be16toh(pdu.len);
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;
248 return -1;
251 return pdu.len;
255 * translate ErrorCode to errno
258 _sdp_errno(uint16_t ec)
261 switch (ec) {
262 case SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE:
263 return ENOATTR;
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;