record.c revision 1.1 1 1.1 plunky /* $NetBSD: record.c,v 1.1 2009/05/12 10:05:07 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: record.c,v 1.1 2009/05/12 10:05:07 plunky Exp $");
34 1.1 plunky
35 1.1 plunky #include <bluetooth.h>
36 1.1 plunky #include <sdp.h>
37 1.1 plunky #include <string.h>
38 1.1 plunky
39 1.1 plunky #include "sdpd.h"
40 1.1 plunky
41 1.1 plunky static bool sdpd_valid_record(sdp_data_t *);
42 1.1 plunky
43 1.1 plunky /*
44 1.1 plunky * These record manipulation requests are not part of the SDP
45 1.1 plunky * specification, they are a private extension and valid only
46 1.1 plunky * for privileged clients on the control socket.
47 1.1 plunky */
48 1.1 plunky
49 1.1 plunky uint16_t
50 1.1 plunky record_insert_request(server_t *srv, int fd)
51 1.1 plunky {
52 1.1 plunky sdp_data_t seq;
53 1.1 plunky bdaddr_t bdaddr;
54 1.1 plunky
55 1.1 plunky seq.next = srv->ibuf;
56 1.1 plunky seq.end = srv->ibuf + srv->pdu.len;
57 1.1 plunky
58 1.1 plunky if (!srv->fdidx[fd].control
59 1.1 plunky || !srv->fdidx[fd].priv)
60 1.1 plunky return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
61 1.1 plunky
62 1.1 plunky srv->fdidx[fd].offset = 0;
63 1.1 plunky db_unselect(srv, fd);
64 1.1 plunky
65 1.1 plunky /*
66 1.1 plunky * extract BluetoothDeviceAddress
67 1.1 plunky */
68 1.1 plunky if (seq.next + sizeof(bdaddr_t) > seq.end)
69 1.1 plunky return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
70 1.1 plunky
71 1.1 plunky memcpy(&bdaddr, seq.next, sizeof(bdaddr_t));
72 1.1 plunky seq.next += sizeof(bdaddr_t);
73 1.1 plunky
74 1.1 plunky /*
75 1.1 plunky * extract ServiceRecord and add to database
76 1.1 plunky */
77 1.1 plunky if (!sdp_get_seq(&seq, &seq)
78 1.1 plunky || !sdpd_valid_record(&seq))
79 1.1 plunky return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
80 1.1 plunky
81 1.1 plunky /* (ignores any additional data) */
82 1.1 plunky
83 1.1 plunky if (!db_create(srv, fd, &bdaddr, srv->handle, &seq))
84 1.1 plunky return SDP_ERROR_CODE_INSUFFICIENT_RESOURCES;
85 1.1 plunky
86 1.1 plunky /*
87 1.1 plunky * encode 'success' ErrorCode and ServiceRecordHandle and
88 1.1 plunky * bump server handle
89 1.1 plunky */
90 1.1 plunky be16enc(srv->obuf, 0x0000);
91 1.1 plunky be32enc(srv->obuf + sizeof(uint16_t), srv->handle++);
92 1.1 plunky
93 1.1 plunky /*
94 1.1 plunky * fill in PDU header and we are done
95 1.1 plunky */
96 1.1 plunky srv->pdu.pid = SDP_PDU_ERROR_RESPONSE;
97 1.1 plunky srv->pdu.len = sizeof(uint16_t) + sizeof(uint32_t);
98 1.1 plunky return 0;
99 1.1 plunky }
100 1.1 plunky
101 1.1 plunky uint16_t
102 1.1 plunky record_update_request(server_t *srv, int fd)
103 1.1 plunky {
104 1.1 plunky record_t *rec;
105 1.1 plunky sdp_data_t seq;
106 1.1 plunky
107 1.1 plunky seq.next = srv->ibuf;
108 1.1 plunky seq.end = srv->ibuf + srv->pdu.len;
109 1.1 plunky
110 1.1 plunky if (!srv->fdidx[fd].control
111 1.1 plunky || !srv->fdidx[fd].priv)
112 1.1 plunky return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
113 1.1 plunky
114 1.1 plunky srv->fdidx[fd].offset = 0;
115 1.1 plunky db_unselect(srv, fd);
116 1.1 plunky
117 1.1 plunky /*
118 1.1 plunky * extract ServiceRecordHandle and select record
119 1.1 plunky */
120 1.1 plunky if (seq.next + sizeof(uint32_t) > seq.end)
121 1.1 plunky return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
122 1.1 plunky
123 1.1 plunky db_select_handle(srv, fd, be32dec(seq.next));
124 1.1 plunky seq.next += sizeof(uint32_t);
125 1.1 plunky
126 1.1 plunky rec = NULL;
127 1.1 plunky db_next(srv, fd, &rec);
128 1.1 plunky if (rec == NULL || rec->fd != fd)
129 1.1 plunky return SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE;
130 1.1 plunky
131 1.1 plunky db_unselect(srv, fd);
132 1.1 plunky
133 1.1 plunky /*
134 1.1 plunky * extract ServiceRecord and add to database
135 1.1 plunky */
136 1.1 plunky if (!sdp_get_seq(&seq, &seq)
137 1.1 plunky || !sdpd_valid_record(&seq))
138 1.1 plunky return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
139 1.1 plunky
140 1.1 plunky /* (ignores any additional data) */
141 1.1 plunky
142 1.1 plunky if (!db_create(srv, fd, &rec->bdaddr, rec->handle, &seq))
143 1.1 plunky return SDP_ERROR_CODE_INSUFFICIENT_RESOURCES;
144 1.1 plunky
145 1.1 plunky /*
146 1.1 plunky * encode 'success' ErrorCode
147 1.1 plunky */
148 1.1 plunky be16enc(srv->obuf, 0x0000);
149 1.1 plunky
150 1.1 plunky /*
151 1.1 plunky * fill in PDU header and we are done
152 1.1 plunky */
153 1.1 plunky srv->pdu.pid = SDP_PDU_ERROR_RESPONSE;
154 1.1 plunky srv->pdu.len = sizeof(uint16_t);
155 1.1 plunky return 0;
156 1.1 plunky }
157 1.1 plunky
158 1.1 plunky uint16_t
159 1.1 plunky record_remove_request(server_t *srv, int fd)
160 1.1 plunky {
161 1.1 plunky record_t *rec;
162 1.1 plunky
163 1.1 plunky if (!srv->fdidx[fd].control
164 1.1 plunky || !srv->fdidx[fd].priv)
165 1.1 plunky return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
166 1.1 plunky
167 1.1 plunky srv->fdidx[fd].offset = 0;
168 1.1 plunky db_unselect(srv, fd);
169 1.1 plunky
170 1.1 plunky /*
171 1.1 plunky * extract ServiceRecordHandle
172 1.1 plunky */
173 1.1 plunky if (srv->pdu.len != sizeof(uint32_t))
174 1.1 plunky return SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX;
175 1.1 plunky
176 1.1 plunky db_select_handle(srv, fd, be32dec(srv->ibuf));
177 1.1 plunky
178 1.1 plunky rec = NULL;
179 1.1 plunky db_next(srv, fd, &rec);
180 1.1 plunky if (rec == NULL || rec->fd != fd)
181 1.1 plunky return SDP_ERROR_CODE_INVALID_SERVICE_RECORD_HANDLE;
182 1.1 plunky
183 1.1 plunky /*
184 1.1 plunky * expire the record
185 1.1 plunky */
186 1.1 plunky rec->refcnt--;
187 1.1 plunky rec->valid = false;
188 1.1 plunky rec->fd = -1;
189 1.1 plunky db_unselect(srv, fd);
190 1.1 plunky
191 1.1 plunky /*
192 1.1 plunky * encode 'success' ErrorCode
193 1.1 plunky */
194 1.1 plunky be16enc(srv->obuf, 0x0000);
195 1.1 plunky
196 1.1 plunky /*
197 1.1 plunky * fill in PDU header and we are done
198 1.1 plunky */
199 1.1 plunky srv->pdu.pid = SDP_PDU_ERROR_RESPONSE;
200 1.1 plunky srv->pdu.len = sizeof(uint16_t);
201 1.1 plunky return 0;
202 1.1 plunky }
203 1.1 plunky
204 1.1 plunky /*
205 1.1 plunky * validate ServiceRecord data element list
206 1.1 plunky *
207 1.1 plunky * The record must contain a list of attribute/value pairs where the
208 1.1 plunky * attributes are unsigned 16-bit integer values in ascending order.
209 1.1 plunky */
210 1.1 plunky static bool
211 1.1 plunky sdpd_valid_record(sdp_data_t *data)
212 1.1 plunky {
213 1.1 plunky sdp_data_t d, s;
214 1.1 plunky uintmax_t a0, a;
215 1.1 plunky
216 1.1 plunky s = *data;
217 1.1 plunky if (!sdp_data_valid(&s))
218 1.1 plunky return false;
219 1.1 plunky
220 1.1 plunky /* The first attribute must be ServiceRecordHandle */
221 1.1 plunky if (!sdp_get_data(&s, &d)
222 1.1 plunky || sdp_data_type(&d) != SDP_DATA_UINT16
223 1.1 plunky || !sdp_get_uint(&d, &a0)
224 1.1 plunky || a0 != SDP_ATTR_SERVICE_RECORD_HANDLE
225 1.1 plunky || !sdp_get_data(&s, &d)
226 1.1 plunky || sdp_data_type(&d) != SDP_DATA_UINT32)
227 1.1 plunky return false;
228 1.1 plunky
229 1.1 plunky /* and remaining attribute IDs must be in ascending order */
230 1.1 plunky while (sdp_get_data(&s, &d)
231 1.1 plunky && sdp_data_type(&d) == SDP_DATA_UINT16
232 1.1 plunky && sdp_get_uint(&d, &a)
233 1.1 plunky && a > a0
234 1.1 plunky && sdp_get_data(&s, &d))
235 1.1 plunky a0 = a;
236 1.1 plunky
237 1.1 plunky if (s.next != s.end)
238 1.1 plunky return false;
239 1.1 plunky
240 1.1 plunky return true;
241 1.1 plunky }
242