sdp_service.c revision 1.1 1 1.1 plunky /* $NetBSD: sdp_service.c,v 1.1 2009/05/12 10:05:06 plunky Exp $ */
2 1.1 plunky
3 1.1 plunky /*-
4 1.1 plunky * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.1 plunky * All rights reserved.
6 1.1 plunky *
7 1.1 plunky * This code is derived from software contributed to The NetBSD Foundation
8 1.1 plunky * by Iain Hibbert.
9 1.1 plunky *
10 1.1 plunky * Redistribution and use in source and binary forms, with or without
11 1.1 plunky * modification, are permitted provided that the following conditions
12 1.1 plunky * are met:
13 1.1 plunky * 1. Redistributions of source code must retain the above copyright
14 1.1 plunky * notice, this list of conditions and the following disclaimer.
15 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 plunky * notice, this list of conditions and the following disclaimer in the
17 1.1 plunky * documentation and/or other materials provided with the distribution.
18 1.1 plunky *
19 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 plunky * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 plunky * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 plunky * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 plunky * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 plunky * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 plunky * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 plunky * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 plunky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 plunky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 plunky * POSSIBILITY OF SUCH DAMAGE.
30 1.1 plunky */
31 1.1 plunky
32 1.1 plunky #include <sys/cdefs.h>
33 1.1 plunky __RCSID("$NetBSD: sdp_service.c,v 1.1 2009/05/12 10:05:06 plunky Exp $");
34 1.1 plunky
35 1.1 plunky #include <errno.h>
36 1.1 plunky #include <limits.h>
37 1.1 plunky #include <sdp.h>
38 1.1 plunky #include <stdlib.h>
39 1.1 plunky #include <string.h>
40 1.1 plunky #include <unistd.h>
41 1.1 plunky
42 1.1 plunky #include "sdp-int.h"
43 1.1 plunky
44 1.1 plunky /*
45 1.1 plunky * If AttributeIDList is given as NULL, request all attributes.
46 1.1 plunky */
47 1.1 plunky static uint8_t ail_default[] = { 0x0a, 0x00, 0x00, 0xff, 0xff };
48 1.1 plunky
49 1.1 plunky /*
50 1.1 plunky * This provides the maximum size that the response buffer will be
51 1.1 plunky * allowed to grow to.
52 1.1 plunky *
53 1.1 plunky * Default is UINT16_MAX but it can be overridden at runtime.
54 1.1 plunky */
55 1.1 plunky static size_t
56 1.1 plunky sdp_response_max(void)
57 1.1 plunky {
58 1.1 plunky static size_t max = UINT16_MAX;
59 1.1 plunky static bool check = true;
60 1.1 plunky char *env, *ep;
61 1.1 plunky unsigned long v;
62 1.1 plunky
63 1.1 plunky while (check) {
64 1.1 plunky check = false; /* only check env once */
65 1.1 plunky
66 1.1 plunky env = getenv("SDP_RESPONSE_MAX");
67 1.1 plunky if (env == NULL)
68 1.1 plunky break;
69 1.1 plunky
70 1.1 plunky errno = 0;
71 1.1 plunky v = strtoul(env, &ep, 0);
72 1.1 plunky if (env[0] == '\0' || *ep != '\0')
73 1.1 plunky break;
74 1.1 plunky
75 1.1 plunky if (errno == ERANGE && v == ULONG_MAX)
76 1.1 plunky break;
77 1.1 plunky
78 1.1 plunky /* lower limit is arbitrary */
79 1.1 plunky if (v < UINT8_MAX || v > UINT32_MAX)
80 1.1 plunky break;
81 1.1 plunky
82 1.1 plunky max = v;
83 1.1 plunky }
84 1.1 plunky
85 1.1 plunky return max;
86 1.1 plunky }
87 1.1 plunky
88 1.1 plunky bool
89 1.1 plunky sdp_service_search(struct sdp_session *ss, const sdp_data_t *ssp,
90 1.1 plunky uint32_t *id, int *num)
91 1.1 plunky {
92 1.1 plunky struct iovec req[5];
93 1.1 plunky sdp_data_t hdr;
94 1.1 plunky uint8_t sdata[5], max[2];
95 1.1 plunky uint8_t *ptr, *end;
96 1.1 plunky ssize_t len;
97 1.1 plunky uint16_t total, count, got;
98 1.1 plunky
99 1.1 plunky /*
100 1.1 plunky * setup ServiceSearchPattern
101 1.1 plunky */
102 1.1 plunky len = ssp->end - ssp->next;
103 1.1 plunky if (len < 0 || len > UINT16_MAX) {
104 1.1 plunky errno = EINVAL;
105 1.1 plunky return false;
106 1.1 plunky }
107 1.1 plunky
108 1.1 plunky hdr.next = sdata;
109 1.1 plunky hdr.end = sdata + sizeof(sdata) + len;
110 1.1 plunky sdp_put_seq(&hdr, len);
111 1.1 plunky req[1].iov_base = sdata;
112 1.1 plunky req[1].iov_len = hdr.next - sdata;
113 1.1 plunky
114 1.1 plunky req[2].iov_base = ssp->next;
115 1.1 plunky req[2].iov_len = len;
116 1.1 plunky
117 1.1 plunky /*
118 1.1 plunky * setup MaximumServiceRecordCount
119 1.1 plunky */
120 1.1 plunky if (*num < 0 || *num > UINT16_MAX) {
121 1.1 plunky errno = EINVAL;
122 1.1 plunky return false;
123 1.1 plunky }
124 1.1 plunky be16enc(max, *num);
125 1.1 plunky req[3].iov_base = max;
126 1.1 plunky req[3].iov_len = sizeof(uint16_t);
127 1.1 plunky
128 1.1 plunky /*
129 1.1 plunky * clear ContinuationState
130 1.1 plunky */
131 1.1 plunky ss->cs[0] = 0;
132 1.1 plunky
133 1.1 plunky /*
134 1.1 plunky * ServiceSearch Transaction
135 1.1 plunky */
136 1.1 plunky got = 0;
137 1.1 plunky for (;;) {
138 1.1 plunky /*
139 1.1 plunky * setup ContinuationState
140 1.1 plunky */
141 1.1 plunky req[4].iov_base = ss->cs;
142 1.1 plunky req[4].iov_len = ss->cs[0] + 1;
143 1.1 plunky
144 1.1 plunky if (!_sdp_send_pdu(ss, SDP_PDU_SERVICE_SEARCH_REQUEST,
145 1.1 plunky req, __arraycount(req)))
146 1.1 plunky return false;
147 1.1 plunky
148 1.1 plunky len = _sdp_recv_pdu(ss, SDP_PDU_SERVICE_SEARCH_RESPONSE);
149 1.1 plunky if (len == -1)
150 1.1 plunky return false;
151 1.1 plunky
152 1.1 plunky ptr = ss->ibuf;
153 1.1 plunky end = ss->ibuf + len;
154 1.1 plunky
155 1.1 plunky /*
156 1.1 plunky * extract TotalServiceRecordCount
157 1.1 plunky */
158 1.1 plunky if (ptr + sizeof(uint16_t) > end)
159 1.1 plunky break;
160 1.1 plunky
161 1.1 plunky total = be16dec(ptr);
162 1.1 plunky ptr += sizeof(uint16_t);
163 1.1 plunky if (total > *num)
164 1.1 plunky break;
165 1.1 plunky
166 1.1 plunky /*
167 1.1 plunky * extract CurrentServiceRecordCount
168 1.1 plunky */
169 1.1 plunky if (ptr + sizeof(uint16_t) > end)
170 1.1 plunky break;
171 1.1 plunky
172 1.1 plunky count = be16dec(ptr);
173 1.1 plunky ptr += sizeof(uint16_t);
174 1.1 plunky if (got + count > total)
175 1.1 plunky break;
176 1.1 plunky
177 1.1 plunky /*
178 1.1 plunky * extract ServiceRecordHandleList
179 1.1 plunky */
180 1.1 plunky if (ptr + count * sizeof(uint32_t) > end)
181 1.1 plunky break;
182 1.1 plunky
183 1.1 plunky while (count-- > 0) {
184 1.1 plunky id[got++] = be32dec(ptr);
185 1.1 plunky ptr += sizeof(uint32_t);
186 1.1 plunky }
187 1.1 plunky
188 1.1 plunky /*
189 1.1 plunky * extract ContinuationState
190 1.1 plunky */
191 1.1 plunky if (ptr + 1 > end
192 1.1 plunky || ptr[0] > 16
193 1.1 plunky || ptr + ptr[0] + 1 != end)
194 1.1 plunky break;
195 1.1 plunky
196 1.1 plunky memcpy(ss->cs, ptr, ptr[0] + 1);
197 1.1 plunky
198 1.1 plunky /*
199 1.1 plunky * Complete?
200 1.1 plunky */
201 1.1 plunky if (ss->cs[0] == 0) {
202 1.1 plunky *num = got;
203 1.1 plunky return true;
204 1.1 plunky }
205 1.1 plunky }
206 1.1 plunky
207 1.1 plunky errno = EIO;
208 1.1 plunky return false;
209 1.1 plunky }
210 1.1 plunky
211 1.1 plunky bool
212 1.1 plunky sdp_service_attribute(struct sdp_session *ss, uint32_t id,
213 1.1 plunky const sdp_data_t *ail, sdp_data_t *rsp)
214 1.1 plunky {
215 1.1 plunky struct iovec req[6];
216 1.1 plunky sdp_data_t hdr;
217 1.1 plunky uint8_t adata[5], handle[4], max[2];
218 1.1 plunky uint8_t *ptr, *end, *rbuf;
219 1.1 plunky ssize_t len;
220 1.1 plunky size_t rlen, count;
221 1.1 plunky
222 1.1 plunky /*
223 1.1 plunky * setup ServiceRecordHandle
224 1.1 plunky */
225 1.1 plunky be32enc(handle, id);
226 1.1 plunky req[1].iov_base = handle;
227 1.1 plunky req[1].iov_len = sizeof(uint32_t);
228 1.1 plunky
229 1.1 plunky /*
230 1.1 plunky * setup MaximumAttributeByteCount
231 1.1 plunky */
232 1.1 plunky be16enc(max, ss->imtu - sizeof(uint16_t) - sizeof(ss->cs));
233 1.1 plunky req[2].iov_base = max;
234 1.1 plunky req[2].iov_len = sizeof(uint16_t);
235 1.1 plunky
236 1.1 plunky /*
237 1.1 plunky * setup AttributeIDList
238 1.1 plunky */
239 1.1 plunky len = (ail == NULL ? sizeof(ail_default) : (ail->end - ail->next));
240 1.1 plunky if (len < 0 || len > UINT16_MAX) {
241 1.1 plunky errno = EINVAL;
242 1.1 plunky return false;
243 1.1 plunky }
244 1.1 plunky
245 1.1 plunky hdr.next = adata;
246 1.1 plunky hdr.end = adata + sizeof(adata) + len;
247 1.1 plunky sdp_put_seq(&hdr, len);
248 1.1 plunky req[3].iov_base = adata;
249 1.1 plunky req[3].iov_len = hdr.next - adata;
250 1.1 plunky
251 1.1 plunky req[4].iov_base = (ail == NULL ? ail_default : ail->next);
252 1.1 plunky req[4].iov_len = len;
253 1.1 plunky
254 1.1 plunky /*
255 1.1 plunky * clear ContinuationState
256 1.1 plunky */
257 1.1 plunky ss->cs[0] = 0;
258 1.1 plunky
259 1.1 plunky /*
260 1.1 plunky * ServiceAttribute Transaction
261 1.1 plunky */
262 1.1 plunky rlen = 0;
263 1.1 plunky for (;;) {
264 1.1 plunky /*
265 1.1 plunky * setup ContinuationState
266 1.1 plunky */
267 1.1 plunky req[5].iov_base = ss->cs;
268 1.1 plunky req[5].iov_len = ss->cs[0] + 1;
269 1.1 plunky
270 1.1 plunky if (!_sdp_send_pdu(ss, SDP_PDU_SERVICE_ATTRIBUTE_REQUEST,
271 1.1 plunky req, __arraycount(req)))
272 1.1 plunky return false;
273 1.1 plunky
274 1.1 plunky len = _sdp_recv_pdu(ss, SDP_PDU_SERVICE_ATTRIBUTE_RESPONSE);
275 1.1 plunky if (len == -1)
276 1.1 plunky return false;
277 1.1 plunky
278 1.1 plunky ptr = ss->ibuf;
279 1.1 plunky end = ss->ibuf + len;
280 1.1 plunky
281 1.1 plunky /*
282 1.1 plunky * extract AttributeListByteCount
283 1.1 plunky */
284 1.1 plunky if (ptr + sizeof(uint16_t) > end)
285 1.1 plunky break;
286 1.1 plunky
287 1.1 plunky count = be16dec(ptr);
288 1.1 plunky ptr += sizeof(uint16_t);
289 1.1 plunky if (count == 0 || ptr + count > end)
290 1.1 plunky break;
291 1.1 plunky
292 1.1 plunky /*
293 1.1 plunky * extract AttributeList
294 1.1 plunky */
295 1.1 plunky if (rlen + count > sdp_response_max())
296 1.1 plunky break;
297 1.1 plunky
298 1.1 plunky rbuf = realloc(ss->rbuf, rlen + count);
299 1.1 plunky if (rbuf == NULL)
300 1.1 plunky return false;
301 1.1 plunky
302 1.1 plunky ss->rbuf = rbuf;
303 1.1 plunky memcpy(rbuf + rlen, ptr, count);
304 1.1 plunky rlen += count;
305 1.1 plunky ptr += count;
306 1.1 plunky
307 1.1 plunky /*
308 1.1 plunky * extract ContinuationState
309 1.1 plunky */
310 1.1 plunky if (ptr + 1 > end
311 1.1 plunky || ptr[0] > 16
312 1.1 plunky || ptr + ptr[0] + 1 != end)
313 1.1 plunky break;
314 1.1 plunky
315 1.1 plunky memcpy(ss->cs, ptr, ptr[0] + 1);
316 1.1 plunky
317 1.1 plunky /*
318 1.1 plunky * Complete?
319 1.1 plunky */
320 1.1 plunky if (ss->cs[0] == 0) {
321 1.1 plunky rsp->next = rbuf;
322 1.1 plunky rsp->end = rbuf + rlen;
323 1.1 plunky if (sdp_data_size(rsp) != rlen
324 1.1 plunky || !sdp_data_valid(rsp)
325 1.1 plunky || !sdp_get_seq(rsp, rsp))
326 1.1 plunky break;
327 1.1 plunky
328 1.1 plunky return true;
329 1.1 plunky }
330 1.1 plunky }
331 1.1 plunky
332 1.1 plunky errno = EIO;
333 1.1 plunky return false;
334 1.1 plunky }
335 1.1 plunky
336 1.1 plunky bool
337 1.1 plunky sdp_service_search_attribute(struct sdp_session *ss, const sdp_data_t *ssp,
338 1.1 plunky const sdp_data_t *ail, sdp_data_t *rsp)
339 1.1 plunky {
340 1.1 plunky struct iovec req[7];
341 1.1 plunky sdp_data_t hdr;
342 1.1 plunky uint8_t sdata[5], adata[5], max[2];
343 1.1 plunky uint8_t *ptr, *end, *rbuf;
344 1.1 plunky ssize_t len;
345 1.1 plunky size_t rlen, count;
346 1.1 plunky
347 1.1 plunky /*
348 1.1 plunky * setup ServiceSearchPattern
349 1.1 plunky */
350 1.1 plunky len = ssp->end - ssp->next;
351 1.1 plunky if (len < 0 || len > UINT16_MAX) {
352 1.1 plunky errno = EINVAL;
353 1.1 plunky return false;
354 1.1 plunky }
355 1.1 plunky
356 1.1 plunky hdr.next = sdata;
357 1.1 plunky hdr.end = sdata + sizeof(sdata) + len;
358 1.1 plunky sdp_put_seq(&hdr, len);
359 1.1 plunky req[1].iov_base = sdata;
360 1.1 plunky req[1].iov_len = hdr.next - sdata;
361 1.1 plunky
362 1.1 plunky req[2].iov_base = ssp->next;
363 1.1 plunky req[2].iov_len = len;
364 1.1 plunky
365 1.1 plunky /*
366 1.1 plunky * setup MaximumAttributeByteCount
367 1.1 plunky */
368 1.1 plunky be16enc(max, ss->imtu - sizeof(uint16_t) - sizeof(ss->cs));
369 1.1 plunky req[3].iov_base = max;
370 1.1 plunky req[3].iov_len = sizeof(uint16_t);
371 1.1 plunky
372 1.1 plunky /*
373 1.1 plunky * setup AttributeIDList
374 1.1 plunky */
375 1.1 plunky len = (ail == NULL ? sizeof(ail_default) : (ail->end - ail->next));
376 1.1 plunky if (len < 0 || len > UINT16_MAX) {
377 1.1 plunky errno = EINVAL;
378 1.1 plunky return false;
379 1.1 plunky }
380 1.1 plunky
381 1.1 plunky hdr.next = adata;
382 1.1 plunky hdr.end = adata + sizeof(adata) + len;
383 1.1 plunky sdp_put_seq(&hdr, len);
384 1.1 plunky req[4].iov_base = adata;
385 1.1 plunky req[4].iov_len = hdr.next - adata;
386 1.1 plunky
387 1.1 plunky req[5].iov_base = (ail == NULL ? ail_default : ail->next);
388 1.1 plunky req[5].iov_len = len;
389 1.1 plunky
390 1.1 plunky /*
391 1.1 plunky * clear ContinuationState
392 1.1 plunky */
393 1.1 plunky ss->cs[0] = 0;
394 1.1 plunky
395 1.1 plunky /*
396 1.1 plunky * ServiceSearchAttribute Transaction
397 1.1 plunky */
398 1.1 plunky rlen = 0;
399 1.1 plunky for (;;) {
400 1.1 plunky /*
401 1.1 plunky * setup ContinuationState
402 1.1 plunky */
403 1.1 plunky req[6].iov_base = ss->cs;
404 1.1 plunky req[6].iov_len = ss->cs[0] + 1;
405 1.1 plunky
406 1.1 plunky if (!_sdp_send_pdu(ss, SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_REQUEST,
407 1.1 plunky req, __arraycount(req)))
408 1.1 plunky return false;
409 1.1 plunky
410 1.1 plunky len = _sdp_recv_pdu(ss, SDP_PDU_SERVICE_SEARCH_ATTRIBUTE_RESPONSE);
411 1.1 plunky if (len == -1)
412 1.1 plunky return false;
413 1.1 plunky
414 1.1 plunky ptr = ss->ibuf;
415 1.1 plunky end = ss->ibuf + len;
416 1.1 plunky
417 1.1 plunky /*
418 1.1 plunky * extract AttributeListsByteCount
419 1.1 plunky */
420 1.1 plunky if (ptr + sizeof(uint16_t) > end)
421 1.1 plunky break;
422 1.1 plunky
423 1.1 plunky count = be16dec(ptr);
424 1.1 plunky ptr += sizeof(uint16_t);
425 1.1 plunky if (count == 0 || ptr + count > end)
426 1.1 plunky break;
427 1.1 plunky
428 1.1 plunky /*
429 1.1 plunky * extract AttributeLists
430 1.1 plunky */
431 1.1 plunky if (rlen + count > sdp_response_max())
432 1.1 plunky break;
433 1.1 plunky
434 1.1 plunky rbuf = realloc(ss->rbuf, rlen + count);
435 1.1 plunky if (rbuf == NULL)
436 1.1 plunky return false;
437 1.1 plunky
438 1.1 plunky ss->rbuf = rbuf;
439 1.1 plunky memcpy(rbuf + rlen, ptr, count);
440 1.1 plunky rlen += count;
441 1.1 plunky ptr += count;
442 1.1 plunky
443 1.1 plunky /*
444 1.1 plunky * extract ContinuationState
445 1.1 plunky */
446 1.1 plunky if (ptr + 1 > end
447 1.1 plunky || ptr[0] > 16
448 1.1 plunky || ptr + ptr[0] + 1 != end)
449 1.1 plunky break;
450 1.1 plunky
451 1.1 plunky memcpy(ss->cs, ptr, ptr[0] + 1);
452 1.1 plunky
453 1.1 plunky /*
454 1.1 plunky * Complete?
455 1.1 plunky */
456 1.1 plunky if (ss->cs[0] == 0) {
457 1.1 plunky rsp->next = rbuf;
458 1.1 plunky rsp->end = rbuf + rlen;
459 1.1 plunky if (sdp_data_size(rsp) != rlen
460 1.1 plunky || !sdp_data_valid(rsp)
461 1.1 plunky || !sdp_get_seq(rsp, rsp))
462 1.1 plunky break;
463 1.1 plunky
464 1.1 plunky return true;
465 1.1 plunky }
466 1.1 plunky }
467 1.1 plunky
468 1.1 plunky errno = EIO;
469 1.1 plunky return false;
470 1.1 plunky }
471