4 * SPDX-License-Identifier: BSD-2-Clause
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 $
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #define L2CAP_SOCKET_CHECKED
37 #include <bluetooth.h>
45 static int32_t sdp_receive_error_pdu(sdp_session_p ss);
48 sdp_register_service(void *xss, uint16_t uuid, bdaddr_p const bdaddr,
49 uint8_t const *data, uint32_t datalen, uint32_t *handle)
51 sdp_session_p ss = (sdp_session_p) xss;
58 if (bdaddr == NULL || data == NULL ||
59 datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
63 if (sizeof(pdu)+sizeof(uuid)+sizeof(*bdaddr)+datalen > SDP_LOCAL_MTU) {
68 pdu.pid = SDP_PDU_SERVICE_REGISTER_REQUEST;
69 pdu.tid = htons(++ss->tid);
70 pdu.len = htons(sizeof(uuid) + sizeof(*bdaddr) + datalen);
74 iov[0].iov_base = (void *) &pdu;
75 iov[0].iov_len = sizeof(pdu);
77 iov[1].iov_base = (void *) &uuid;
78 iov[1].iov_len = sizeof(uuid);
80 iov[2].iov_base = (void *) bdaddr;
81 iov[2].iov_len = sizeof(*bdaddr);
83 iov[3].iov_base = (void *) data;
84 iov[3].iov_len = datalen;
87 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
88 } while (len < 0 && errno == EINTR);
95 len = sdp_receive_error_pdu(ss);
98 if (len != sizeof(pdu) + sizeof(uint16_t) + sizeof(uint32_t)) {
103 if (handle != NULL) {
104 *handle = (uint32_t) ss->rsp[--len];
105 *handle |= (uint32_t) ss->rsp[--len] << 8;
106 *handle |= (uint32_t) ss->rsp[--len] << 16;
107 *handle |= (uint32_t) ss->rsp[--len] << 24;
114 sdp_unregister_service(void *xss, uint32_t handle)
116 sdp_session_p ss = (sdp_session_p) xss;
123 if (!(ss->flags & SDP_SESSION_LOCAL)) {
127 if (sizeof(pdu) + sizeof(handle) > SDP_LOCAL_MTU) {
128 ss->error = EMSGSIZE;
132 pdu.pid = SDP_PDU_SERVICE_UNREGISTER_REQUEST;
133 pdu.tid = htons(++ss->tid);
134 pdu.len = htons(sizeof(handle));
136 handle = htonl(handle);
138 iov[0].iov_base = (void *) &pdu;
139 iov[0].iov_len = sizeof(pdu);
141 iov[1].iov_base = (void *) &handle;
142 iov[1].iov_len = sizeof(handle);
145 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
146 } while (len < 0 && errno == EINTR);
153 return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
157 sdp_change_service(void *xss, uint32_t handle,
158 uint8_t const *data, uint32_t datalen)
160 sdp_session_p ss = (sdp_session_p) xss;
167 if (data == NULL || datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
171 if (sizeof(pdu) + sizeof(handle) + datalen > SDP_LOCAL_MTU) {
172 ss->error = EMSGSIZE;
176 pdu.pid = SDP_PDU_SERVICE_CHANGE_REQUEST;
177 pdu.tid = htons(++ss->tid);
178 pdu.len = htons(sizeof(handle) + datalen);
180 handle = htons(handle);
182 iov[0].iov_base = (void *) &pdu;
183 iov[0].iov_len = sizeof(pdu);
185 iov[1].iov_base = (void *) &handle;
186 iov[1].iov_len = sizeof(handle);
188 iov[2].iov_base = (void *) data;
189 iov[2].iov_len = datalen;
192 len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
193 } while (len < 0 && errno == EINTR);
200 return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
204 sdp_receive_error_pdu(sdp_session_p ss)
211 len = read(ss->s, ss->rsp, ss->rsp_e - ss->rsp);
212 } while (len < 0 && errno == EINTR);
219 pdu = (sdp_pdu_p) ss->rsp;
220 pdu->tid = ntohs(pdu->tid);
221 pdu->len = ntohs(pdu->len);
223 if (pdu->pid != SDP_PDU_ERROR_RESPONSE || pdu->tid != ss->tid ||
224 pdu->len < 2 || pdu->len != len - sizeof(*pdu)) {
229 error = (uint16_t) ss->rsp[sizeof(pdu)] << 8;
230 error |= (uint16_t) ss->rsp[sizeof(pdu) + 1];