xref: /freebsd/lib/libsdp/service.c (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
1 /*
2  * service.c
3  *
4  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: service.c,v 1.1 2004/01/13 19:32:36 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <sys/uio.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <sdp-int.h>
42 #include <sdp.h>
43 
44 static int32_t sdp_receive_error_pdu(sdp_session_p ss);
45 
46 int32_t
47 sdp_register_service(void *xss, uint16_t uuid, bdaddr_p const bdaddr,
48 		uint8_t const *data, uint32_t datalen, uint32_t *handle)
49 {
50 	sdp_session_p	ss = (sdp_session_p) xss;
51 	struct iovec	iov[4];
52 	sdp_pdu_t	pdu;
53 	int32_t		len;
54 
55 	if (ss == NULL)
56 		return (-1);
57 	if (bdaddr == NULL || data == NULL ||
58 	    datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
59 		ss->error = EINVAL;
60 		return (-1);
61 	}
62 	if (sizeof(pdu)+sizeof(uuid)+sizeof(*bdaddr)+datalen > SDP_LOCAL_MTU) {
63 		ss->error = EMSGSIZE;
64 		return (-1);
65 	}
66 
67 	pdu.pid = SDP_PDU_SERVICE_REGISTER_REQUEST;
68 	pdu.tid = htons(++ss->tid);
69 	pdu.len = htons(sizeof(uuid) + sizeof(*bdaddr) + datalen);
70 
71 	uuid = htons(uuid);
72 
73 	iov[0].iov_base = (void *) &pdu;
74 	iov[0].iov_len = sizeof(pdu);
75 
76 	iov[1].iov_base = (void *) &uuid;
77 	iov[1].iov_len = sizeof(uuid);
78 
79 	iov[2].iov_base = (void *) bdaddr;
80 	iov[2].iov_len = sizeof(*bdaddr);
81 
82 	iov[3].iov_base = (void *) data;
83 	iov[3].iov_len = datalen;
84 
85 	do {
86 		len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
87 	} while (len < 0 && errno == EINTR);
88 
89 	if (len < 0) {
90 		ss->error = errno;
91 		return (-1);
92 	}
93 
94 	len = sdp_receive_error_pdu(ss);
95 	if (len < 0)
96 		return (-1);
97 	if (len != sizeof(pdu) + sizeof(uint16_t) + sizeof(uint32_t)) {
98 		ss->error = EIO;
99 		return (-1);
100 	}
101 
102 	if (handle != NULL) {
103 		*handle  = (uint32_t) ss->rsp[--len];
104 		*handle |= (uint32_t) ss->rsp[--len] << 8;
105 		*handle |= (uint32_t) ss->rsp[--len] << 16;
106 		*handle |= (uint32_t) ss->rsp[--len] << 24;
107 	}
108 
109 	return (0);
110 }
111 
112 int32_t
113 sdp_unregister_service(void *xss, uint32_t handle)
114 {
115 	sdp_session_p	ss = (sdp_session_p) xss;
116 	struct iovec	iov[2];
117 	sdp_pdu_t	pdu;
118 	int32_t		len;
119 
120 	if (ss == NULL)
121 		return (-1);
122 	if (!(ss->flags & SDP_SESSION_LOCAL)) {
123 		ss->error = EINVAL;
124 		return (-1);
125 	}
126 	if (sizeof(pdu) + sizeof(handle) > SDP_LOCAL_MTU) {
127 		ss->error = EMSGSIZE;
128 		return (-1);
129 	}
130 
131 	pdu.pid = SDP_PDU_SERVICE_UNREGISTER_REQUEST;
132 	pdu.tid = htons(++ss->tid);
133 	pdu.len = htons(sizeof(handle));
134 
135 	handle = htonl(handle);
136 
137 	iov[0].iov_base = (void *) &pdu;
138 	iov[0].iov_len = sizeof(pdu);
139 
140 	iov[1].iov_base = (void *) &handle;
141 	iov[1].iov_len = sizeof(handle);
142 
143 	do {
144 		len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
145 	} while (len < 0 && errno == EINTR);
146 
147 	if (len < 0) {
148 		ss->error = errno;
149 		return (-1);
150 	}
151 
152 	return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
153 }
154 
155 int32_t
156 sdp_change_service(void *xss, uint32_t handle,
157 		uint8_t const *data, uint32_t datalen)
158 {
159 	sdp_session_p	ss = (sdp_session_p) xss;
160 	struct iovec	iov[3];
161 	sdp_pdu_t	pdu;
162 	int32_t		len;
163 
164 	if (ss == NULL)
165 		return (-1);
166 	if (data == NULL || datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
167 		ss->error = EINVAL;
168 		return (-1);
169 	}
170 	if (sizeof(pdu) + sizeof(handle) + datalen > SDP_LOCAL_MTU) {
171 		ss->error = EMSGSIZE;
172 		return (-1);
173 	}
174 
175 	pdu.pid = SDP_PDU_SERVICE_CHANGE_REQUEST;
176 	pdu.tid = htons(++ss->tid);
177 	pdu.len = htons(sizeof(handle) + datalen);
178 
179 	handle = htons(handle);
180 
181 	iov[0].iov_base = (void *) &pdu;
182 	iov[0].iov_len = sizeof(pdu);
183 
184 	iov[1].iov_base = (void *) &handle;
185 	iov[1].iov_len = sizeof(handle);
186 
187 	iov[2].iov_base = (void *) data;
188 	iov[2].iov_len = datalen;
189 
190 	do {
191 		len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
192 	} while (len < 0 && errno == EINTR);
193 
194 	if (len < 0) {
195 		ss->error = errno;
196 		return (-1);
197 	}
198 
199 	return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
200 }
201 
202 static int32_t
203 sdp_receive_error_pdu(sdp_session_p ss)
204 {
205 	sdp_pdu_p	pdu;
206 	int32_t		len;
207 	uint16_t	error;
208 
209 	do {
210 		len = read(ss->s, ss->rsp, ss->rsp_e - ss->rsp);
211 	} while (len < 0 && errno == EINTR);
212 
213 	if (len < 0) {
214 		ss->error = errno;
215 		return (-1);
216 	}
217 
218 	pdu = (sdp_pdu_p) ss->rsp;
219 	pdu->tid = ntohs(pdu->tid);
220 	pdu->len = ntohs(pdu->len);
221 
222 	if (pdu->pid != SDP_PDU_ERROR_RESPONSE || pdu->tid != ss->tid ||
223 	    pdu->len < 2 || pdu->len != len - sizeof(*pdu)) {
224 		ss->error = EIO;
225 		return (-1);
226 	}
227 
228 	error  = (uint16_t) ss->rsp[sizeof(pdu)] << 8;
229 	error |= (uint16_t) ss->rsp[sizeof(pdu) + 1];
230 
231 	if (error != 0) {
232 		ss->error = EIO;
233 		return (-1);
234 	}
235 
236 	return (len);
237 }
238 
239