1 /* $NetBSD: record.c$ */
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: record.c$");
35 #include <bluetooth.h>
41 static bool sdpd_valid_record(sdp_data_t
*);
44 * These record manipulation requests are not part of the SDP
45 * specification, they are a private extension and valid only
46 * for privileged clients on the control socket.
50 record_insert_request(server_t
*srv
, int fd
)
56 seq
.end
= srv
->ibuf
+ srv
->pdu
.len
;
58 if (!srv
->fdidx
[fd
].control
59 || !srv
->fdidx
[fd
].priv
)
60 return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
;
62 srv
->fdidx
[fd
].offset
= 0;
66 * extract BluetoothDeviceAddress
68 if (seq
.next
+ sizeof(bdaddr_t
) > seq
.end
)
69 return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
;
71 memcpy(&bdaddr
, seq
.next
, sizeof(bdaddr_t
));
72 seq
.next
+= sizeof(bdaddr_t
);
75 * extract ServiceRecord and add to database
77 if (!sdp_get_seq(&seq
, &seq
)
78 || !sdpd_valid_record(&seq
))
79 return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
;
81 /* (ignores any additional data) */
83 if (!db_create(srv
, fd
, &bdaddr
, srv
->handle
, &seq
))
84 return SDP_ERROR_CODE_INSUFFICIENT_RESOURCES
;
87 * encode 'success' ErrorCode and ServiceRecordHandle and
90 be16enc(srv
->obuf
, 0x0000);
91 be32enc(srv
->obuf
+ sizeof(uint16_t), srv
->handle
++);
94 * fill in PDU header and we are done
96 srv
->pdu
.pid
= SDP_PDU_ERROR_RESPONSE
;
97 srv
->pdu
.len
= sizeof(uint16_t) + sizeof(uint32_t);
102 record_update_request(server_t
*srv
, int fd
)
107 seq
.next
= srv
->ibuf
;
108 seq
.end
= srv
->ibuf
+ srv
->pdu
.len
;
110 if (!srv
->fdidx
[fd
].control
111 || !srv
->fdidx
[fd
].priv
)
112 return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
;
114 srv
->fdidx
[fd
].offset
= 0;
115 db_unselect(srv
, fd
);
118 * extract ServiceRecordHandle and select record
120 if (seq
.next
+ sizeof(uint32_t) > seq
.end
)
121 return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
;
123 db_select_handle(srv
, fd
, be32dec(seq
.next
));
124 seq
.next
+= sizeof(uint32_t);
127 db_next(srv
, fd
, &rec
);
128 if (rec
== NULL
|| rec
->fd
!= fd
)
129 return SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE
;
131 db_unselect(srv
, fd
);
134 * extract ServiceRecord and add to database
136 if (!sdp_get_seq(&seq
, &seq
)
137 || !sdpd_valid_record(&seq
))
138 return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
;
140 /* (ignores any additional data) */
142 if (!db_create(srv
, fd
, &rec
->bdaddr
, rec
->handle
, &seq
))
143 return SDP_ERROR_CODE_INSUFFICIENT_RESOURCES
;
146 * encode 'success' ErrorCode
148 be16enc(srv
->obuf
, 0x0000);
151 * fill in PDU header and we are done
153 srv
->pdu
.pid
= SDP_PDU_ERROR_RESPONSE
;
154 srv
->pdu
.len
= sizeof(uint16_t);
159 record_remove_request(server_t
*srv
, int fd
)
163 if (!srv
->fdidx
[fd
].control
164 || !srv
->fdidx
[fd
].priv
)
165 return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
;
167 srv
->fdidx
[fd
].offset
= 0;
168 db_unselect(srv
, fd
);
171 * extract ServiceRecordHandle
173 if (srv
->pdu
.len
!= sizeof(uint32_t))
174 return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX
;
176 db_select_handle(srv
, fd
, be32dec(srv
->ibuf
));
179 db_next(srv
, fd
, &rec
);
180 if (rec
== NULL
|| rec
->fd
!= fd
)
181 return SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE
;
189 db_unselect(srv
, fd
);
192 * encode 'success' ErrorCode
194 be16enc(srv
->obuf
, 0x0000);
197 * fill in PDU header and we are done
199 srv
->pdu
.pid
= SDP_PDU_ERROR_RESPONSE
;
200 srv
->pdu
.len
= sizeof(uint16_t);
205 * validate ServiceRecord data element list
207 * The record must contain a list of attribute/value pairs where the
208 * attributes are unsigned 16-bit integer values in ascending order.
211 sdpd_valid_record(sdp_data_t
*data
)
217 if (!sdp_data_valid(&s
))
220 /* The first attribute must be ServiceRecordHandle */
221 if (!sdp_get_data(&s
, &d
)
222 || sdp_data_type(&d
) != SDP_DATA_UINT16
223 || !sdp_get_uint(&d
, &a0
)
224 || a0
!= SDP_ATTR_SERVICE_RECORD_HANDLE
225 || !sdp_get_data(&s
, &d
)
226 || sdp_data_type(&d
) != SDP_DATA_UINT32
)
229 /* and remaining attribute IDs must be in ascending order */
230 while (sdp_get_data(&s
, &d
)
231 && sdp_data_type(&d
) == SDP_DATA_UINT16
232 && sdp_get_uint(&d
, &a
)
234 && sdp_get_data(&s
, &d
))