• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2003-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdio.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <syslog.h>
33 #include <limits.h>
34 #include <sys/stat.h>
35 #include <sys/socket.h>
36 
37 #include <bluetooth/bluetooth.h>
38 #include <bluetooth/l2cap.h>
39 #include <bluetooth/sdp.h>
40 #include <bluetooth/sdp_lib.h>
41 #include <bluetooth/hidp.h>
42 #include <bluetooth/bnep.h>
43 
44 #include "textfile.h"
45 #include "sdp.h"
46 
47 static sdp_record_t *record = NULL;
48 static sdp_session_t *session = NULL;
49 
add_lang_attr(sdp_record_t * r)50 static void add_lang_attr(sdp_record_t *r)
51 {
52 	sdp_lang_attr_t base_lang;
53 	sdp_list_t *langs = 0;
54 
55 	/* UTF-8 MIBenum (http://www.iana.org/assignments/character-sets) */
56 	base_lang.code_ISO639 = (0x65 << 8) | 0x6e;
57 	base_lang.encoding = 106;
58 	base_lang.base_offset = SDP_PRIMARY_LANG_BASE;
59 	langs = sdp_list_append(0, &base_lang);
60 	sdp_set_lang_attr(r, langs);
61 	sdp_list_free(langs, 0);
62 }
63 
epox_endian_quirk(unsigned char * data,int size)64 static void epox_endian_quirk(unsigned char *data, int size)
65 {
66 	/* USAGE_PAGE (Keyboard)	05 07
67 	 * USAGE_MINIMUM (0)		19 00
68 	 * USAGE_MAXIMUM (65280)	2A 00 FF   <= must be FF 00
69 	 * LOGICAL_MINIMUM (0)		15 00
70 	 * LOGICAL_MAXIMUM (65280)	26 00 FF   <= must be FF 00
71 	 */
72 	unsigned char pattern[] = { 0x05, 0x07, 0x19, 0x00, 0x2a, 0x00, 0xff,
73 						0x15, 0x00, 0x26, 0x00, 0xff };
74 	unsigned int i;
75 
76 	if (!data)
77 		return;
78 
79 	for (i = 0; i < size - sizeof(pattern); i++) {
80 		if (!memcmp(data + i, pattern, sizeof(pattern))) {
81 			data[i + 5] = 0xff;
82 			data[i + 6] = 0x00;
83 			data[i + 10] = 0xff;
84 			data[i + 11] = 0x00;
85 		}
86 	}
87 }
88 
store_device_info(const bdaddr_t * src,const bdaddr_t * dst,struct hidp_connadd_req * req)89 static int store_device_info(const bdaddr_t *src, const bdaddr_t *dst, struct hidp_connadd_req *req)
90 {
91 	char filename[PATH_MAX + 1], addr[18], *str, *desc;
92 	int i, err, size;
93 
94 	ba2str(src, addr);
95 	create_name(filename, PATH_MAX, STORAGEDIR, addr, "hidd");
96 
97 	size = 15 + 3 + 3 + 5 + (req->rd_size * 2) + 1 + 9 + strlen(req->name) + 2;
98 	str = malloc(size);
99 	if (!str)
100 		return -ENOMEM;
101 
102 	desc = malloc((req->rd_size * 2) + 1);
103 	if (!desc) {
104 		free(str);
105 		return -ENOMEM;
106 	}
107 
108 	memset(desc, 0, (req->rd_size * 2) + 1);
109 	for (i = 0; i < req->rd_size; i++)
110 		sprintf(desc + (i * 2), "%2.2X", req->rd_data[i]);
111 
112 	snprintf(str, size - 1, "%04X:%04X:%04X %02X %02X %04X %s %08X %s",
113 			req->vendor, req->product, req->version,
114 			req->subclass, req->country, req->parser, desc,
115 			req->flags, req->name);
116 
117 	free(desc);
118 
119 	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
120 
121 	ba2str(dst, addr);
122 	err = textfile_put(filename, addr, str);
123 
124 	free(str);
125 
126 	return err;
127 }
128 
get_stored_device_info(const bdaddr_t * src,const bdaddr_t * dst,struct hidp_connadd_req * req)129 int get_stored_device_info(const bdaddr_t *src, const bdaddr_t *dst, struct hidp_connadd_req *req)
130 {
131 	char filename[PATH_MAX + 1], addr[18], tmp[3], *str, *desc;
132 	unsigned int vendor, product, version, subclass, country, parser, pos;
133 	int i;
134 
135 	desc = malloc(4096);
136 	if (!desc)
137 		return -ENOMEM;
138 
139 	memset(desc, 0, 4096);
140 
141 	ba2str(src, addr);
142 	create_name(filename, PATH_MAX, STORAGEDIR, addr, "hidd");
143 
144 	ba2str(dst, addr);
145 	str = textfile_get(filename, addr);
146 	if (!str) {
147 		free(desc);
148 		return -EIO;
149 	}
150 
151 	sscanf(str, "%04X:%04X:%04X %02X %02X %04X %4095s %08X %n",
152 			&vendor, &product, &version, &subclass, &country,
153 			&parser, desc, &req->flags, &pos);
154 
155 
156 	req->vendor   = vendor;
157 	req->product  = product;
158 	req->version  = version;
159 	req->subclass = subclass;
160 	req->country  = country;
161 	req->parser   = parser;
162 
163 	snprintf(req->name, 128, "%s", str + pos);
164 
165 	free(str);
166 	req->rd_size = strlen(desc) / 2;
167 	req->rd_data = malloc(req->rd_size);
168 	if (!req->rd_data) {
169 		free(desc);
170 		return -ENOMEM;
171 	}
172 
173 	memset(tmp, 0, sizeof(tmp));
174 	for (i = 0; i < req->rd_size; i++) {
175 		memcpy(tmp, desc + (i * 2), 2);
176 		req->rd_data[i] = (uint8_t) strtol(tmp, NULL, 16);
177 	}
178 
179 	free(desc);
180 
181 	return 0;
182 }
183 
get_sdp_device_info(const bdaddr_t * src,const bdaddr_t * dst,struct hidp_connadd_req * req)184 int get_sdp_device_info(const bdaddr_t *src, const bdaddr_t *dst, struct hidp_connadd_req *req)
185 {
186 	struct sockaddr_l2 addr;
187 	socklen_t addrlen;
188 	bdaddr_t bdaddr;
189 	uint32_t range = 0x0000ffff;
190 	sdp_session_t *s;
191 	sdp_list_t *search, *attrid, *pnp_rsp, *hid_rsp;
192 	sdp_record_t *rec;
193 	sdp_data_t *pdlist, *pdlist2;
194 	uuid_t svclass;
195 	int err;
196 
197 	s = sdp_connect(src, dst, SDP_RETRY_IF_BUSY | SDP_WAIT_ON_CLOSE);
198 	if (!s)
199 		return -1;
200 
201 	sdp_uuid16_create(&svclass, PNP_INFO_SVCLASS_ID);
202 	search = sdp_list_append(NULL, &svclass);
203 	attrid = sdp_list_append(NULL, &range);
204 
205 	err = sdp_service_search_attr_req(s, search,
206 					SDP_ATTR_REQ_RANGE, attrid, &pnp_rsp);
207 
208 	sdp_list_free(search, NULL);
209 	sdp_list_free(attrid, NULL);
210 
211 	sdp_uuid16_create(&svclass, HID_SVCLASS_ID);
212 	search = sdp_list_append(NULL, &svclass);
213 	attrid = sdp_list_append(NULL, &range);
214 
215 	err = sdp_service_search_attr_req(s, search,
216 					SDP_ATTR_REQ_RANGE, attrid, &hid_rsp);
217 
218 	sdp_list_free(search, NULL);
219 	sdp_list_free(attrid, NULL);
220 
221 	memset(&addr, 0, sizeof(addr));
222 	addrlen = sizeof(addr);
223 
224 	if (getsockname(s->sock, (struct sockaddr *) &addr, &addrlen) < 0)
225 		bacpy(&bdaddr, src);
226 	else
227 		bacpy(&bdaddr, &addr.l2_bdaddr);
228 
229 	sdp_close(s);
230 
231 	if (err || !hid_rsp)
232 		return -1;
233 
234 	if (pnp_rsp) {
235 		rec = (sdp_record_t *) pnp_rsp->data;
236 
237 		pdlist = sdp_data_get(rec, 0x0201);
238 		req->vendor = pdlist ? pdlist->val.uint16 : 0x0000;
239 
240 		pdlist = sdp_data_get(rec, 0x0202);
241 		req->product = pdlist ? pdlist->val.uint16 : 0x0000;
242 
243 		pdlist = sdp_data_get(rec, 0x0203);
244 		req->version = pdlist ? pdlist->val.uint16 : 0x0000;
245 
246 		sdp_record_free(rec);
247 	}
248 
249 	rec = (sdp_record_t *) hid_rsp->data;
250 
251 	pdlist2 = sdp_data_get(rec, 0x0100);
252 	if (pdlist2)
253 		strncpy(req->name, pdlist2->val.str, sizeof(req->name) - 1);
254 	else {
255 		pdlist = sdp_data_get(rec, 0x0101);
256 		pdlist2 = sdp_data_get(rec, 0x0102);
257 		if (pdlist) {
258 			if (pdlist2) {
259 				if (strncmp(pdlist->val.str, pdlist2->val.str, 5)) {
260 					strncpy(req->name, pdlist2->val.str, sizeof(req->name) - 1);
261 					strcat(req->name, " ");
262 				}
263 				strncat(req->name, pdlist->val.str,
264 						sizeof(req->name) - strlen(req->name));
265 			} else
266 				strncpy(req->name, pdlist->val.str, sizeof(req->name) - 1);
267 		}
268 	}
269 
270 	pdlist = sdp_data_get(rec, 0x0201);
271 	req->parser = pdlist ? pdlist->val.uint16 : 0x0100;
272 
273 	pdlist = sdp_data_get(rec, 0x0202);
274 	req->subclass = pdlist ? pdlist->val.uint8 : 0;
275 
276 	pdlist = sdp_data_get(rec, 0x0203);
277 	req->country = pdlist ? pdlist->val.uint8 : 0;
278 
279 	pdlist = sdp_data_get(rec, 0x0206);
280 	if (pdlist) {
281 		pdlist = pdlist->val.dataseq;
282 		pdlist = pdlist->val.dataseq;
283 		pdlist = pdlist->next;
284 
285 		req->rd_data = malloc(pdlist->unitSize);
286 		if (req->rd_data) {
287 			memcpy(req->rd_data, (unsigned char *) pdlist->val.str, pdlist->unitSize);
288 			req->rd_size = pdlist->unitSize;
289 			epox_endian_quirk(req->rd_data, req->rd_size);
290 		}
291 	}
292 
293 	sdp_record_free(rec);
294 
295 	if (bacmp(&bdaddr, BDADDR_ANY))
296 		store_device_info(&bdaddr, dst, req);
297 
298 	return 0;
299 }
300 
get_alternate_device_info(const bdaddr_t * src,const bdaddr_t * dst,uint16_t * uuid,uint8_t * channel,char * name,size_t len)301 int get_alternate_device_info(const bdaddr_t *src, const bdaddr_t *dst, uint16_t *uuid, uint8_t *channel, char *name, size_t len)
302 {
303 	uint16_t attr1 = SDP_ATTR_PROTO_DESC_LIST;
304 	uint16_t attr2 = SDP_ATTR_SVCNAME_PRIMARY;
305 	sdp_session_t *s;
306 	sdp_list_t *search, *attrid, *rsp;
307 	uuid_t svclass;
308 	int err;
309 
310 	s = sdp_connect(src, dst, SDP_RETRY_IF_BUSY | SDP_WAIT_ON_CLOSE);
311 	if (!s)
312 		return -1;
313 
314 	sdp_uuid16_create(&svclass, HEADSET_SVCLASS_ID);
315 	search = sdp_list_append(NULL, &svclass);
316 	attrid = sdp_list_append(NULL, &attr1);
317 	attrid = sdp_list_append(attrid, &attr2);
318 
319 	err = sdp_service_search_attr_req(s, search,
320 					SDP_ATTR_REQ_INDIVIDUAL, attrid, &rsp);
321 
322 	sdp_list_free(search, NULL);
323 	sdp_list_free(attrid, NULL);
324 
325 	if (err <= 0) {
326 		sdp_uuid16_create(&svclass, SERIAL_PORT_SVCLASS_ID);
327 		search = sdp_list_append(NULL, &svclass);
328 		attrid = sdp_list_append(NULL, &attr1);
329 		attrid = sdp_list_append(attrid, &attr2);
330 
331 		err = sdp_service_search_attr_req(s, search,
332 					SDP_ATTR_REQ_INDIVIDUAL, attrid, &rsp);
333 
334 		sdp_list_free(search, NULL);
335 		sdp_list_free(attrid, NULL);
336 
337 		if (err < 0) {
338 			sdp_close(s);
339 			return err;
340 		}
341 
342 		if (uuid)
343 			*uuid = SERIAL_PORT_SVCLASS_ID;
344 	} else {
345 		if (uuid)
346 			*uuid = HEADSET_SVCLASS_ID;
347 	}
348 
349 	sdp_close(s);
350 
351 	for (; rsp; rsp = rsp->next) {
352 		sdp_record_t *rec = (sdp_record_t *) rsp->data;
353 		sdp_list_t *protos;
354 
355 		sdp_get_service_name(rec, name, len);
356 
357 		if (!sdp_get_access_protos(rec, &protos)) {
358 			uint8_t ch = sdp_get_proto_port(protos, RFCOMM_UUID);
359 			if (ch > 0) {
360 				if (channel)
361 					*channel = ch;
362 				return 0;
363 			}
364 		}
365 
366 		sdp_record_free(rec);
367 	}
368 
369 	return -EIO;
370 }
371 
bnep_sdp_unregister(void)372 void bnep_sdp_unregister(void)
373 {
374 	if (record && sdp_record_unregister(session, record))
375 		syslog(LOG_ERR, "Service record unregistration failed.");
376 
377 	sdp_close(session);
378 }
379 
bnep_sdp_register(bdaddr_t * device,uint16_t role)380 int bnep_sdp_register(bdaddr_t *device, uint16_t role)
381 {
382 	sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto;
383 	uuid_t root_uuid, pan, l2cap, bnep;
384 	sdp_profile_desc_t profile[1];
385 	sdp_list_t *proto[2];
386 	sdp_data_t *v, *p;
387 	uint16_t psm = 15, version = 0x0100;
388 	uint16_t security_desc = 0;
389 	uint16_t net_access_type = 0xfffe;
390 	uint32_t max_net_access_rate = 0;
391 	char *name = "BlueZ PAN";
392 	char *desc = "BlueZ PAN Service";
393 	int status;
394 
395 	session = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);
396 	if (!session) {
397 		syslog(LOG_ERR, "Failed to connect to the local SDP server. %s(%d)",
398 							strerror(errno), errno);
399 		return -1;
400 	}
401 
402 	record = sdp_record_alloc();
403 	if (!record) {
404 		syslog(LOG_ERR, "Failed to allocate service record %s(%d)",
405 							strerror(errno), errno);
406 		sdp_close(session);
407 		return -1;
408 	}
409 
410 	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
411 	root = sdp_list_append(NULL, &root_uuid);
412 	sdp_set_browse_groups(record, root);
413 	sdp_list_free(root, 0);
414 
415 	sdp_uuid16_create(&l2cap, L2CAP_UUID);
416 	proto[0] = sdp_list_append(NULL, &l2cap);
417 	p = sdp_data_alloc(SDP_UINT16, &psm);
418 	proto[0] = sdp_list_append(proto[0], p);
419 	apseq    = sdp_list_append(NULL, proto[0]);
420 
421 	sdp_uuid16_create(&bnep, BNEP_UUID);
422 	proto[1] = sdp_list_append(NULL, &bnep);
423 	v = sdp_data_alloc(SDP_UINT16, &version);
424 	proto[1] = sdp_list_append(proto[1], v);
425 
426 	/* Supported protocols */
427 	{
428 		uint16_t ptype[4] = {
429 			0x0800,  /* IPv4 */
430 			0x0806,  /* ARP */
431 		};
432 		sdp_data_t *head, *pseq;
433 		int p;
434 
435 		for (p = 0, head = NULL; p < 2; p++) {
436 			sdp_data_t *data = sdp_data_alloc(SDP_UINT16, &ptype[p]);
437 			if (head)
438 				sdp_seq_append(head, data);
439 			else
440 				head = data;
441 		}
442 		pseq = sdp_data_alloc(SDP_SEQ16, head);
443 		proto[1] = sdp_list_append(proto[1], pseq);
444 	}
445 
446 	apseq = sdp_list_append(apseq, proto[1]);
447 
448 	aproto = sdp_list_append(NULL, apseq);
449 	sdp_set_access_protos(record, aproto);
450 
451 	add_lang_attr(record);
452 
453 	sdp_list_free(proto[0], NULL);
454 	sdp_list_free(proto[1], NULL);
455 	sdp_list_free(apseq, NULL);
456 	sdp_list_free(aproto, NULL);
457 	sdp_data_free(p);
458 	sdp_data_free(v);
459 	sdp_attr_add_new(record, SDP_ATTR_SECURITY_DESC, SDP_UINT16, &security_desc);
460 
461 	switch (role) {
462 	case BNEP_SVC_NAP:
463 		sdp_uuid16_create(&pan, NAP_SVCLASS_ID);
464 		svclass = sdp_list_append(NULL, &pan);
465 		sdp_set_service_classes(record, svclass);
466 
467 		sdp_uuid16_create(&profile[0].uuid, NAP_PROFILE_ID);
468 		profile[0].version = 0x0100;
469 		pfseq = sdp_list_append(NULL, &profile[0]);
470 		sdp_set_profile_descs(record, pfseq);
471 
472 		sdp_set_info_attr(record, "Network Access Point", name, desc);
473 
474 		sdp_attr_add_new(record, SDP_ATTR_NET_ACCESS_TYPE, SDP_UINT16, &net_access_type);
475 		sdp_attr_add_new(record, SDP_ATTR_MAX_NET_ACCESSRATE, SDP_UINT32, &max_net_access_rate);
476 		break;
477 
478 	case BNEP_SVC_GN:
479 		sdp_uuid16_create(&pan, GN_SVCLASS_ID);
480 		svclass = sdp_list_append(NULL, &pan);
481 		sdp_set_service_classes(record, svclass);
482 
483 		sdp_uuid16_create(&profile[0].uuid, GN_PROFILE_ID);
484 		profile[0].version = 0x0100;
485 		pfseq = sdp_list_append(NULL, &profile[0]);
486 		sdp_set_profile_descs(record, pfseq);
487 
488 		sdp_set_info_attr(record, "Group Network Service", name, desc);
489 		break;
490 
491 	case BNEP_SVC_PANU:
492 		sdp_uuid16_create(&pan, PANU_SVCLASS_ID);
493 		svclass = sdp_list_append(NULL, &pan);
494 		sdp_set_service_classes(record, svclass);
495 		sdp_list_free(svclass, 0);
496 
497 		sdp_uuid16_create(&profile[0].uuid, PANU_PROFILE_ID);
498 		profile[0].version = 0x0100;
499 		pfseq = sdp_list_append(NULL, &profile[0]);
500 		sdp_set_profile_descs(record, pfseq);
501 		sdp_list_free(pfseq, 0);
502 
503 		sdp_set_info_attr(record, "PAN User", name, desc);
504 		break;
505 	}
506 
507 	status = sdp_device_record_register(session, device, record, 0);
508 	if (status) {
509 		syslog(LOG_ERR, "SDP registration failed.");
510 		sdp_record_free(record); record = NULL;
511 		sdp_close(session);
512 		return -1;
513 	}
514 
515 	return 0;
516 }
517 
518 /* Search for PAN service.
519  * Returns 1 if service is found and 0 otherwise. */
bnep_sdp_search(bdaddr_t * src,bdaddr_t * dst,uint16_t service)520 int bnep_sdp_search(bdaddr_t *src, bdaddr_t *dst, uint16_t service)
521 {
522 	sdp_list_t *srch, *rsp = NULL;
523 	sdp_session_t *s;
524 	uuid_t svclass;
525 	int err;
526 
527 	switch (service) {
528 	case BNEP_SVC_PANU:
529 		sdp_uuid16_create(&svclass, PANU_SVCLASS_ID);
530 		break;
531 	case BNEP_SVC_NAP:
532 		sdp_uuid16_create(&svclass, NAP_SVCLASS_ID);
533 		break;
534 	case BNEP_SVC_GN:
535 		sdp_uuid16_create(&svclass, GN_SVCLASS_ID);
536 		break;
537 	}
538 
539 	srch = sdp_list_append(NULL, &svclass);
540 
541 	s = sdp_connect(src, dst, 0);
542 	if (!s) {
543 		syslog(LOG_ERR, "Failed to connect to the SDP server. %s(%d)",
544 							strerror(errno), errno);
545 		return 0;
546 	}
547 
548 	err = sdp_service_search_req(s, srch, 1, &rsp);
549 	sdp_close(s);
550 
551 	/* Assume that search is successeful
552 	 * if at least one record is found */
553 	if (!err && sdp_list_len(rsp))
554 		return 1;
555 
556 	return 0;
557 }
558 
559 static unsigned char async_uuid[] = {	0x03, 0x50, 0x27, 0x8F, 0x3D, 0xCA, 0x4E, 0x62,
560 					0x83, 0x1D, 0xA4, 0x11, 0x65, 0xFF, 0x90, 0x6C };
561 
dun_sdp_unregister(void)562 void dun_sdp_unregister(void)
563 {
564 	if (record && sdp_record_unregister(session, record))
565 		syslog(LOG_ERR, "Service record unregistration failed.");
566 	sdp_close(session);
567 }
568 
dun_sdp_register(bdaddr_t * device,uint8_t channel,int type)569 int dun_sdp_register(bdaddr_t *device, uint8_t channel, int type)
570 {
571 	sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto;
572 	uuid_t root_uuid, l2cap, rfcomm, dun;
573 	sdp_profile_desc_t profile[1];
574 	sdp_list_t *proto[2];
575 	int status;
576 
577 	session = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0);
578 	if (!session) {
579 		syslog(LOG_ERR, "Failed to connect to the local SDP server. %s(%d)",
580 				strerror(errno), errno);
581 		return -1;
582 	}
583 
584 	record = sdp_record_alloc();
585 	if (!record) {
586 		syslog(LOG_ERR, "Failed to alloc service record");
587 		return -1;
588 	}
589 
590 	sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
591 	root = sdp_list_append(NULL, &root_uuid);
592 	sdp_set_browse_groups(record, root);
593 
594 	sdp_uuid16_create(&l2cap, L2CAP_UUID);
595 	proto[0] = sdp_list_append(NULL, &l2cap);
596 	apseq    = sdp_list_append(NULL, proto[0]);
597 
598 	sdp_uuid16_create(&rfcomm, RFCOMM_UUID);
599 	proto[1] = sdp_list_append(NULL, &rfcomm);
600 	proto[1] = sdp_list_append(proto[1], sdp_data_alloc(SDP_UINT8, &channel));
601 	apseq    = sdp_list_append(apseq, proto[1]);
602 
603 	aproto   = sdp_list_append(NULL, apseq);
604 	sdp_set_access_protos(record, aproto);
605 
606 	switch (type) {
607 	case MROUTER:
608 		sdp_uuid16_create(&dun, SERIAL_PORT_SVCLASS_ID);
609 		break;
610 	case ACTIVESYNC:
611 		sdp_uuid128_create(&dun, (void *) async_uuid);
612 		break;
613 	case DIALUP:
614 		sdp_uuid16_create(&dun, DIALUP_NET_SVCLASS_ID);
615 		break;
616 	default:
617 		sdp_uuid16_create(&dun, LAN_ACCESS_SVCLASS_ID);
618 		break;
619 	}
620 
621 	svclass = sdp_list_append(NULL, &dun);
622 	sdp_set_service_classes(record, svclass);
623 
624 	switch (type) {
625 	case LANACCESS:
626 		sdp_uuid16_create(&profile[0].uuid, LAN_ACCESS_PROFILE_ID);
627 		profile[0].version = 0x0100;
628 		pfseq = sdp_list_append(NULL, &profile[0]);
629 		sdp_set_profile_descs(record, pfseq);
630 		break;
631 	case DIALUP:
632 		sdp_uuid16_create(&profile[0].uuid, DIALUP_NET_PROFILE_ID);
633 		profile[0].version = 0x0100;
634 		pfseq = sdp_list_append(NULL, &profile[0]);
635 		sdp_set_profile_descs(record, pfseq);
636 		break;
637 	}
638 
639 	switch (type) {
640 	case MROUTER:
641 		sdp_set_info_attr(record, "mRouter", NULL, NULL);
642 		break;
643 	case ACTIVESYNC:
644 		sdp_set_info_attr(record, "ActiveSync", NULL, NULL);
645 		break;
646 	case DIALUP:
647 		sdp_set_info_attr(record, "Dialup Networking", NULL, NULL);
648 		break;
649 	default:
650 		sdp_set_info_attr(record, "LAN Access Point", NULL, NULL);
651 		break;
652 	}
653 
654 	status = sdp_device_record_register(session, device, record, 0);
655 	if (status) {
656 		syslog(LOG_ERR, "SDP registration failed.");
657 		sdp_record_free(record);
658 		record = NULL;
659 		return -1;
660 	}
661 	return 0;
662 }
663 
dun_sdp_search(bdaddr_t * src,bdaddr_t * dst,int * channel,int type)664 int dun_sdp_search(bdaddr_t *src, bdaddr_t *dst, int *channel, int type)
665 {
666 	sdp_session_t *s;
667 	sdp_list_t *srch, *attrs, *rsp;
668 	uuid_t svclass;
669 	uint16_t attr;
670 	int err;
671 
672 	s = sdp_connect(src, dst, 0);
673 	if (!s) {
674 		syslog(LOG_ERR, "Failed to connect to the SDP server. %s(%d)",
675 				strerror(errno), errno);
676 		return -1;
677 	}
678 
679 	switch (type) {
680 	case MROUTER:
681 		sdp_uuid16_create(&svclass, SERIAL_PORT_SVCLASS_ID);
682 		break;
683 	case ACTIVESYNC:
684 		sdp_uuid128_create(&svclass, (void *) async_uuid);
685 		break;
686 	case DIALUP:
687 		sdp_uuid16_create(&svclass, DIALUP_NET_SVCLASS_ID);
688 		break;
689 	default:
690 		sdp_uuid16_create(&svclass, LAN_ACCESS_SVCLASS_ID);
691 		break;
692 	}
693 
694 	srch  = sdp_list_append(NULL, &svclass);
695 
696 	attr  = SDP_ATTR_PROTO_DESC_LIST;
697 	attrs = sdp_list_append(NULL, &attr);
698 
699 	err = sdp_service_search_attr_req(s, srch, SDP_ATTR_REQ_INDIVIDUAL, attrs, &rsp);
700 
701 	sdp_close(s);
702 
703 	if (err)
704 		return 0;
705 
706 	for(; rsp; rsp = rsp->next) {
707 		sdp_record_t *rec = (sdp_record_t *) rsp->data;
708 		sdp_list_t *protos;
709 
710 		if (!sdp_get_access_protos(rec, &protos)) {
711 			int ch = sdp_get_proto_port(protos, RFCOMM_UUID);
712 			if (ch > 0) {
713 				*channel = ch;
714 				return 1;
715 			}
716 		}
717 	}
718 
719 	return 0;
720 }
721