1 /* $NetBSD: ssar.c,v 1.2 2007/11/09 20:08:41 plunky Exp $ */
2 /* $DragonFly: src/usr.sbin/sdpd/ssar.c,v 1.2 2008/05/19 10:19:49 corecode Exp $ */
7 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $Id: ssar.c,v 1.2 2007/11/30 07:39:37 griffin Exp $
32 * $FreeBSD: src/usr.sbin/bluetooth/sdpd/ssar.c,v 1.2 2005/01/05 18:37:37 emax Exp $
35 #include <sys/queue.h>
36 #include <sys/select.h>
37 #include <bluetooth.h>
43 #include "uuid-private.h"
46 * Prepare SDP Service Search Attribute Response
50 server_prepare_service_search_attribute_response(server_p srv
, int32_t fd
)
52 uint8_t const *req
= srv
->req
+ sizeof(sdp_pdu_t
);
53 uint8_t const *req_end
= req
+ ((sdp_pdu_p
)(srv
->req
))->len
;
54 uint8_t *rsp
= srv
->fdidx
[fd
].rsp
;
55 uint8_t const *rsp_end
= rsp
+ L2CAP_MTU_MAXIMUM
;
57 uint8_t const *aidptr
= NULL
;
59 provider_t
*provider
= NULL
;
60 int32_t type
, rsp_limit
, ucount
, aidlen
, cslen
, cs
;
64 * Minimal Service Search Attribute Request request
67 * uuid16 value16 - 3 bytes ServiceSearchPattern
68 * value16 - 2 bytes MaximumAttributeByteCount
70 * uint16 value16 - 3 bytes AttributeIDList
71 * value8 - 1 byte ContinuationState
74 /* Get ServiceSearchPattern */
75 ucount
= server_get_service_search_pattern(&req
, req_end
, ulist
);
76 if (ucount
< 1 || ucount
> 12)
77 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
79 /* Get MaximumAttributeByteCount */
80 if (req
+ 2 > req_end
)
81 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
83 SDP_GET16(rsp_limit
, req
);
85 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
87 /* Get size of AttributeIDList */
88 if (req
+ 1 > req_end
)
89 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
95 if (req
+ 1 > req_end
)
96 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
98 SDP_GET8(aidlen
, req
);
102 if (req
+ 2 > req_end
)
103 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
105 SDP_GET16(aidlen
, req
);
109 if (req
+ 4 > req_end
)
110 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
112 SDP_GET32(aidlen
, req
);
116 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
121 /* Get ContinuationState */
122 if (req
+ 1 > req_end
)
123 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
125 SDP_GET8(cslen
, req
);
126 if (cslen
== 2 && req
+ 2 == req_end
)
128 else if (cslen
== 0 && req
== req_end
)
131 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
);
133 /* Process the request. First, check continuation state */
134 if (srv
->fdidx
[fd
].rsp_cs
!= cs
)
135 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE
);
136 if (srv
->fdidx
[fd
].rsp_size
> 0)
140 * Service Search Attribute Response format
142 * value16 - 2 bytes AttributeListByteCount (not incl.)
143 * seq8 len16 - 3 bytes
144 * attr list - 3+ bytes AttributeLists
148 rsp
+= 3; /* leave space for sequence header */
150 for (provider
= provider_get_first();
152 provider
= provider_get_next(provider
)) {
153 if (!provider_match_bdaddr(provider
, &srv
->req_sa
.bt_bdaddr
))
156 if (!provider_match_uuid(provider
, ulist
, ucount
))
159 cs
= server_prepare_attr_list(provider
,
160 aidptr
, aidptr
+ aidlen
, rsp
, rsp_end
);
162 return (SDP_ERROR_CODE_INSUFFICIENT_RESOURCES
);
167 /* Set reply size (not counting PDU header and continuation state) */
168 srv
->fdidx
[fd
].rsp_limit
= srv
->fdidx
[fd
].omtu
- sizeof(sdp_pdu_t
) - 2;
169 if (srv
->fdidx
[fd
].rsp_limit
> rsp_limit
)
170 srv
->fdidx
[fd
].rsp_limit
= rsp_limit
;
172 srv
->fdidx
[fd
].rsp_size
= rsp
- srv
->fdidx
[fd
].rsp
;
173 srv
->fdidx
[fd
].rsp_cs
= 0;
175 /* Fix AttributeLists sequence header */
176 rsp
= srv
->fdidx
[fd
].rsp
;
177 SDP_PUT8(SDP_DATA_SEQ16
, rsp
);
178 SDP_PUT16(srv
->fdidx
[fd
].rsp_size
- 3, rsp
);