xref: /freebsd/lib/libsdp/service.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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 #include <bluetooth.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <sdp-int.h>
41 #include <sdp.h>
42 
43 static int32_t sdp_receive_error_pdu(sdp_session_p ss);
44 
45 int32_t
46 sdp_register_service(void *xss, uint16_t uuid, bdaddr_p const bdaddr,
47 		uint8_t const *data, uint32_t datalen, uint32_t *handle)
48 {
49 	sdp_session_p	ss = (sdp_session_p) xss;
50 	struct iovec	iov[4];
51 	sdp_pdu_t	pdu;
52 	int32_t		len;
53 
54 	if (ss == NULL)
55 		return (-1);
56 	if (bdaddr == NULL || data == NULL ||
57 	    datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
58 		ss->error = EINVAL;
59 		return (-1);
60 	}
61 	if (sizeof(pdu)+sizeof(uuid)+sizeof(*bdaddr)+datalen > SDP_LOCAL_MTU) {
62 		ss->error = EMSGSIZE;
63 		return (-1);
64 	}
65 
66 	pdu.pid = SDP_PDU_SERVICE_REGISTER_REQUEST;
67 	pdu.tid = htons(++ss->tid);
68 	pdu.len = htons(sizeof(uuid) + sizeof(*bdaddr) + datalen);
69 
70 	uuid = htons(uuid);
71 
72 	iov[0].iov_base = (void *) &pdu;
73 	iov[0].iov_len = sizeof(pdu);
74 
75 	iov[1].iov_base = (void *) &uuid;
76 	iov[1].iov_len = sizeof(uuid);
77 
78 	iov[2].iov_base = (void *) bdaddr;
79 	iov[2].iov_len = sizeof(*bdaddr);
80 
81 	iov[3].iov_base = (void *) data;
82 	iov[3].iov_len = datalen;
83 
84 	do {
85 		len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
86 	} while (len < 0 && errno == EINTR);
87 
88 	if (len < 0) {
89 		ss->error = errno;
90 		return (-1);
91 	}
92 
93 	len = sdp_receive_error_pdu(ss);
94 	if (len < 0)
95 		return (-1);
96 	if (len != sizeof(pdu) + sizeof(uint16_t) + sizeof(uint32_t)) {
97 		ss->error = EIO;
98 		return (-1);
99 	}
100 
101 	if (handle != NULL) {
102 		*handle  = (uint32_t) ss->rsp[--len];
103 		*handle |= (uint32_t) ss->rsp[--len] << 8;
104 		*handle |= (uint32_t) ss->rsp[--len] << 16;
105 		*handle |= (uint32_t) ss->rsp[--len] << 24;
106 	}
107 
108 	return (0);
109 }
110 
111 int32_t
112 sdp_unregister_service(void *xss, uint32_t handle)
113 {
114 	sdp_session_p	ss = (sdp_session_p) xss;
115 	struct iovec	iov[2];
116 	sdp_pdu_t	pdu;
117 	int32_t		len;
118 
119 	if (ss == NULL)
120 		return (-1);
121 	if (!(ss->flags & SDP_SESSION_LOCAL)) {
122 		ss->error = EINVAL;
123 		return (-1);
124 	}
125 	if (sizeof(pdu) + sizeof(handle) > SDP_LOCAL_MTU) {
126 		ss->error = EMSGSIZE;
127 		return (-1);
128 	}
129 
130 	pdu.pid = SDP_PDU_SERVICE_UNREGISTER_REQUEST;
131 	pdu.tid = htons(++ss->tid);
132 	pdu.len = htons(sizeof(handle));
133 
134 	handle = htonl(handle);
135 
136 	iov[0].iov_base = (void *) &pdu;
137 	iov[0].iov_len = sizeof(pdu);
138 
139 	iov[1].iov_base = (void *) &handle;
140 	iov[1].iov_len = sizeof(handle);
141 
142 	do {
143 		len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
144 	} while (len < 0 && errno == EINTR);
145 
146 	if (len < 0) {
147 		ss->error = errno;
148 		return (-1);
149 	}
150 
151 	return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
152 }
153 
154 int32_t
155 sdp_change_service(void *xss, uint32_t handle,
156 		uint8_t const *data, uint32_t datalen)
157 {
158 	sdp_session_p	ss = (sdp_session_p) xss;
159 	struct iovec	iov[3];
160 	sdp_pdu_t	pdu;
161 	int32_t		len;
162 
163 	if (ss == NULL)
164 		return (-1);
165 	if (data == NULL || datalen == 0 || !(ss->flags & SDP_SESSION_LOCAL)) {
166 		ss->error = EINVAL;
167 		return (-1);
168 	}
169 	if (sizeof(pdu) + sizeof(handle) + datalen > SDP_LOCAL_MTU) {
170 		ss->error = EMSGSIZE;
171 		return (-1);
172 	}
173 
174 	pdu.pid = SDP_PDU_SERVICE_CHANGE_REQUEST;
175 	pdu.tid = htons(++ss->tid);
176 	pdu.len = htons(sizeof(handle) + datalen);
177 
178 	handle = htons(handle);
179 
180 	iov[0].iov_base = (void *) &pdu;
181 	iov[0].iov_len = sizeof(pdu);
182 
183 	iov[1].iov_base = (void *) &handle;
184 	iov[1].iov_len = sizeof(handle);
185 
186 	iov[2].iov_base = (void *) data;
187 	iov[2].iov_len = datalen;
188 
189 	do {
190 		len = writev(ss->s, iov, sizeof(iov)/sizeof(iov[0]));
191 	} while (len < 0 && errno == EINTR);
192 
193 	if (len < 0) {
194 		ss->error = errno;
195 		return (-1);
196 	}
197 
198 	return ((sdp_receive_error_pdu(ss) < 0)? -1 : 0);
199 }
200 
201 static int32_t
202 sdp_receive_error_pdu(sdp_session_p ss)
203 {
204 	sdp_pdu_p	pdu;
205 	int32_t		len;
206 	uint16_t	error;
207 
208 	do {
209 		len = read(ss->s, ss->rsp, ss->rsp_e - ss->rsp);
210 	} while (len < 0 && errno == EINTR);
211 
212 	if (len < 0) {
213 		ss->error = errno;
214 		return (-1);
215 	}
216 
217 	pdu = (sdp_pdu_p) ss->rsp;
218 	pdu->tid = ntohs(pdu->tid);
219 	pdu->len = ntohs(pdu->len);
220 
221 	if (pdu->pid != SDP_PDU_ERROR_RESPONSE || pdu->tid != ss->tid ||
222 	    pdu->len < 2 || pdu->len != len - sizeof(*pdu)) {
223 		ss->error = EIO;
224 		return (-1);
225 	}
226 
227 	error  = (uint16_t) ss->rsp[sizeof(pdu)] << 8;
228 	error |= (uint16_t) ss->rsp[sizeof(pdu) + 1];
229 
230 	if (error != 0) {
231 		ss->error = EIO;
232 		return (-1);
233 	}
234 
235 	return (len);
236 }
237 
238