4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: service.c,v 1.1 2004/01/13 19:32:36 max Exp $
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #define L2CAP_SOCKET_CHECKED
38 #include <bluetooth.h>
46 static int32_t sdp_receive_error_pdu(sdp_session_p ss);
49 sdp_register_service(void *xss, uint16_t uuid, bdaddr_p const bdaddr,
50 uint8_t const *data, uint32_t datalen, uint32_t *handle)
52 sdp_session_p ss = (sdp_session_p) xss;
59 if (bdaddr == NULL || data == NULL ||
60 datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
64 if (sizeof(pdu)+sizeof(uuid)+sizeof(*bdaddr)+datalen > SDP_LOCAL_MTU) {
69 pdu.pid = SDP_PDU_SERVICE_REGISTER_REQUEST;
70 pdu.tid = htons(++ss->tid);
71 pdu.len = htons(sizeof(uuid) + sizeof(*bdaddr) + datalen);
75 iov[0].iov_base = (void *) &pdu;
76 iov[0].iov_len = sizeof(pdu);
78 iov[1].iov_base = (void *) &uuid;
79 iov[1].iov_len = sizeof(uuid);
81 iov[2].iov_base = (void *) bdaddr;
82 iov[2].iov_len = sizeof(*bdaddr);
84 iov[3].iov_base = (void *) data;
85 iov[3].iov_len = datalen;
88 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
89 } while (len < 0 && errno == EINTR);
96 len = sdp_receive_error_pdu(ss);
99 if (len != sizeof(pdu) + sizeof(uint16_t) + sizeof(uint32_t)) {
104 if (handle != NULL) {
105 *handle = (uint32_t) ss->rsp[--len];
106 *handle |= (uint32_t) ss->rsp[--len] << 8;
107 *handle |= (uint32_t) ss->rsp[--len] << 16;
108 *handle |= (uint32_t) ss->rsp[--len] << 24;
115 sdp_unregister_service(void *xss, uint32_t handle)
117 sdp_session_p ss = (sdp_session_p) xss;
124 if (!(ss->flags & SDP_SESSION_LOCAL)) {
128 if (sizeof(pdu) + sizeof(handle) > SDP_LOCAL_MTU) {
129 ss->error = EMSGSIZE;
133 pdu.pid = SDP_PDU_SERVICE_UNREGISTER_REQUEST;
134 pdu.tid = htons(++ss->tid);
135 pdu.len = htons(sizeof(handle));
137 handle = htonl(handle);
139 iov[0].iov_base = (void *) &pdu;
140 iov[0].iov_len = sizeof(pdu);
142 iov[1].iov_base = (void *) &handle;
143 iov[1].iov_len = sizeof(handle);
146 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
147 } while (len < 0 && errno == EINTR);
154 return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
158 sdp_change_service(void *xss, uint32_t handle,
159 uint8_t const *data, uint32_t datalen)
161 sdp_session_p ss = (sdp_session_p) xss;
168 if (data == NULL || datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
172 if (sizeof(pdu) + sizeof(handle) + datalen > SDP_LOCAL_MTU) {
173 ss->error = EMSGSIZE;
177 pdu.pid = SDP_PDU_SERVICE_CHANGE_REQUEST;
178 pdu.tid = htons(++ss->tid);
179 pdu.len = htons(sizeof(handle) + datalen);
181 handle = htons(handle);
183 iov[0].iov_base = (void *) &pdu;
184 iov[0].iov_len = sizeof(pdu);
186 iov[1].iov_base = (void *) &handle;
187 iov[1].iov_len = sizeof(handle);
189 iov[2].iov_base = (void *) data;
190 iov[2].iov_len = datalen;
193 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
194 } while (len < 0 && errno == EINTR);
201 return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
205 sdp_receive_error_pdu(sdp_session_p ss)
212 len = read(ss->s, ss->rsp, ss->rsp_e - ss->rsp);
213 } while (len < 0 && errno == EINTR);
220 pdu = (sdp_pdu_p) ss->rsp;
221 pdu->tid = ntohs(pdu->tid);
222 pdu->len = ntohs(pdu->len);
224 if (pdu->pid != SDP_PDU_ERROR_RESPONSE || pdu->tid != ss->tid ||
225 pdu->len < 2 || pdu->len != len - sizeof(*pdu)) {
230 error = (uint16_t) ss->rsp[sizeof(pdu)] << 8;
231 error |= (uint16_t) ss->rsp[sizeof(pdu) + 1];