1/*
2 * ssr.c
3 *
4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $Id: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
29 * $FreeBSD: head/usr.sbin/bluetooth/sdpd/ssr.c 124758 2004-01-20 20:48:26Z emax $
30 */
31
32#include <sys/queue.h>
33#include <sys/uio.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36#include <assert.h>
37#include <bluetooth.h>
38#include <errno.h>
39#include <sdp.h>
40#include <string.h>
41#include "profile.h"
42#include "provider.h"
43#include "server.h"
44
45/*
46 * Prepare SDP Service Search Response
47 */
48
49int32_t
50server_prepare_service_search_response(server_p srv, int32_t fd)
51{
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 + NG_L2CAP_MTU_MAXIMUM;
56
57 uint8_t *ptr = NULL;
58 provider_t *provider = NULL;
59 int32_t type, ssplen, rsp_limit, rcount, cslen, cs, uuid;
60
61 /*
62 * Minimal SDP Service Search Request
63 *
64 * seq8 len8 - 2 bytes
65 * uuid16 value16 - 3 bytes ServiceSearchPattern
66 * value16 - 2 bytes MaximumServiceRecordCount
67 * value8 - 1 byte ContinuationState
68 */
69
70 if (req_end - req < 8)
71 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
72
73 /* Get size of ServiceSearchPattern */
74 ssplen = 0;
75 SDP_GET8(type, req);
76 switch (type) {
77 case SDP_DATA_SEQ8:
78 SDP_GET8(ssplen, req);
79 break;
80
81 case SDP_DATA_SEQ16:
82 SDP_GET16(ssplen, req);
83 break;
84
85 case SDP_DATA_SEQ32:
86 SDP_GET32(ssplen, req);
87 break;
88 }
89 if (ssplen <= 0)
90 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
91
92 ptr = (uint8_t *) req + ssplen;
93
94 /* Get MaximumServiceRecordCount */
95 if (ptr + 2 > req_end)
96 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
97
98 SDP_GET16(rsp_limit, ptr);
99 if (rsp_limit <= 0)
100 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
101
102 /* Get ContinuationState */
103 if (ptr + 1 > req_end)
104 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
105
106 SDP_GET8(cslen, ptr);
107 if (cslen != 0) {
108 if (cslen != 2 || req_end - ptr != 2)
109 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
110
111 SDP_GET16(cs, ptr);
112 } else
113 cs = 0;
114
115 /* Process the request. First, check continuation state */
116 if (srv->fdidx[fd].rsp_cs != cs)
117 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
118 if (srv->fdidx[fd].rsp_size > 0)
119 return (0);
120
121 /*
122 * Service Search Response format
123 *
124 * value16 - 2 bytes TotalServiceRecordCount (not incl.)
125 * value16 - 2 bytes CurrentServiceRecordCount (not incl.)
126 * value32 - 4 bytes handle
127 * [ value32 ]
128 *
129 * Calculate how many record handles we can fit
130 * in our reply buffer and adjust rlimit.
131 */
132
133 ptr = rsp;
134 rcount = (rsp_end - ptr) / 4;
135 if (rcount < rsp_limit)
136 rsp_limit = rcount;
137
138 /* Look for the record handles */
139 for (rcount = 0; ssplen > 0 && rcount < rsp_limit; ) {
140 SDP_GET8(type, req);
141 ssplen --;
142
143 switch (type) {
144 case SDP_DATA_UUID16:
145 if (ssplen < 2)
146 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
147
148 SDP_GET16(uuid, req);
149 ssplen -= 2;
150 break;
151
152 case SDP_DATA_UUID32: /* XXX FIXME */
153 case SDP_DATA_UUID128: /* XXX FIXME */
154 default:
155 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
156 /* NOT REACHED */
157 }
158
159 for (provider = provider_get_first();
160 provider != NULL && rcount < rsp_limit;
161 provider = provider_get_next(provider)) {
162 if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
163 continue;
164
165 if (provider->profile->uuid == uuid ||
166 SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP == uuid) {
167 SDP_PUT32(provider->handle, ptr);
168 rcount ++;
169 }
170 }
171 }
172
173 /* Set reply size (not counting PDU header and continuation state) */
174 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 4;
175 srv->fdidx[fd].rsp_size = ptr - rsp;
176 srv->fdidx[fd].rsp_cs = 0;
177
178 return (0);
179}
180
181/*
182 * Send SDP Service Search Response
183 */
184
185int32_t
186server_send_service_search_response(server_p srv, int32_t fd)
187{
188 uint8_t *rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
189 uint8_t *rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
190
191 struct iovec iov[4];
192 sdp_pdu_t pdu;
193 uint16_t rcounts[2];
194 uint8_t cs[3];
195 int32_t size;
196
197 /* First update continuation state (assume we will send all data) */
198 size = rsp_end - rsp;
199 srv->fdidx[fd].rsp_cs += size;
200
201 if (size + 1 > srv->fdidx[fd].rsp_limit) {
202 /*
203 * We need to split out response. Add 3 more bytes for the
204 * continuation state and move rsp_end and rsp_cs backwards.
205 */
206
207 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
208 rsp_end -= 4;
209 srv->fdidx[fd].rsp_cs -= 4;
210 }
211
212 cs[0] = 2;
213 cs[1] = srv->fdidx[fd].rsp_cs >> 8;
214 cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
215 } else
216 cs[0] = 0;
217
218 assert(rsp_end >= rsp);
219
220 rcounts[0] = srv->fdidx[fd].rsp_size / 4; /* TotalServiceRecordCount */
221 rcounts[1] = (rsp_end - rsp) / 4; /* CurrentServiceRecordCount */
222
223 pdu.pid = SDP_PDU_SERVICE_SEARCH_RESPONSE;
224 pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
225 pdu.len = htons(sizeof(rcounts) + rcounts[1] * 4 + 1 + cs[0]);
226
227 iov[0].iov_base = &pdu;
228 iov[0].iov_len = sizeof(pdu);
229
230 iov[1].iov_base = rcounts;
231 iov[1].iov_len = sizeof(rcounts);
232
233 iov[2].iov_base = rsp;
234 iov[2].iov_len = rsp_end - rsp;
235
236 iov[3].iov_base = cs;
237 iov[3].iov_len = 1 + cs[0];
238
239 do {
240 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
241 } while (size < 0 && errno == EINTR);
242
243 /* Check if we have sent (or failed to sent) last response chunk */
244 if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
245 srv->fdidx[fd].rsp_cs = 0;
246 srv->fdidx[fd].rsp_size = 0;
247 srv->fdidx[fd].rsp_limit = 0;
248 }
249
250 return ((size < 0)? errno : 0);
251}
252
2 * ssr.c
3 *
4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $Id: ssr.c,v 1.5 2004/01/13 01:54:39 max Exp $
29 * $FreeBSD: head/usr.sbin/bluetooth/sdpd/ssr.c 124758 2004-01-20 20:48:26Z emax $
30 */
31
32#include <sys/queue.h>
33#include <sys/uio.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36#include <assert.h>
37#include <bluetooth.h>
38#include <errno.h>
39#include <sdp.h>
40#include <string.h>
41#include "profile.h"
42#include "provider.h"
43#include "server.h"
44
45/*
46 * Prepare SDP Service Search Response
47 */
48
49int32_t
50server_prepare_service_search_response(server_p srv, int32_t fd)
51{
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 + NG_L2CAP_MTU_MAXIMUM;
56
57 uint8_t *ptr = NULL;
58 provider_t *provider = NULL;
59 int32_t type, ssplen, rsp_limit, rcount, cslen, cs, uuid;
60
61 /*
62 * Minimal SDP Service Search Request
63 *
64 * seq8 len8 - 2 bytes
65 * uuid16 value16 - 3 bytes ServiceSearchPattern
66 * value16 - 2 bytes MaximumServiceRecordCount
67 * value8 - 1 byte ContinuationState
68 */
69
70 if (req_end - req < 8)
71 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
72
73 /* Get size of ServiceSearchPattern */
74 ssplen = 0;
75 SDP_GET8(type, req);
76 switch (type) {
77 case SDP_DATA_SEQ8:
78 SDP_GET8(ssplen, req);
79 break;
80
81 case SDP_DATA_SEQ16:
82 SDP_GET16(ssplen, req);
83 break;
84
85 case SDP_DATA_SEQ32:
86 SDP_GET32(ssplen, req);
87 break;
88 }
89 if (ssplen <= 0)
90 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
91
92 ptr = (uint8_t *) req + ssplen;
93
94 /* Get MaximumServiceRecordCount */
95 if (ptr + 2 > req_end)
96 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
97
98 SDP_GET16(rsp_limit, ptr);
99 if (rsp_limit <= 0)
100 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
101
102 /* Get ContinuationState */
103 if (ptr + 1 > req_end)
104 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
105
106 SDP_GET8(cslen, ptr);
107 if (cslen != 0) {
108 if (cslen != 2 || req_end - ptr != 2)
109 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
110
111 SDP_GET16(cs, ptr);
112 } else
113 cs = 0;
114
115 /* Process the request. First, check continuation state */
116 if (srv->fdidx[fd].rsp_cs != cs)
117 return (SDP_ERROR_CODE_INVALID_CONTINUATION_STATE);
118 if (srv->fdidx[fd].rsp_size > 0)
119 return (0);
120
121 /*
122 * Service Search Response format
123 *
124 * value16 - 2 bytes TotalServiceRecordCount (not incl.)
125 * value16 - 2 bytes CurrentServiceRecordCount (not incl.)
126 * value32 - 4 bytes handle
127 * [ value32 ]
128 *
129 * Calculate how many record handles we can fit
130 * in our reply buffer and adjust rlimit.
131 */
132
133 ptr = rsp;
134 rcount = (rsp_end - ptr) / 4;
135 if (rcount < rsp_limit)
136 rsp_limit = rcount;
137
138 /* Look for the record handles */
139 for (rcount = 0; ssplen > 0 && rcount < rsp_limit; ) {
140 SDP_GET8(type, req);
141 ssplen --;
142
143 switch (type) {
144 case SDP_DATA_UUID16:
145 if (ssplen < 2)
146 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
147
148 SDP_GET16(uuid, req);
149 ssplen -= 2;
150 break;
151
152 case SDP_DATA_UUID32: /* XXX FIXME */
153 case SDP_DATA_UUID128: /* XXX FIXME */
154 default:
155 return (SDP_ERROR_CODE_INVALID_REQUEST_SYNTAX);
156 /* NOT REACHED */
157 }
158
159 for (provider = provider_get_first();
160 provider != NULL && rcount < rsp_limit;
161 provider = provider_get_next(provider)) {
162 if (!provider_match_bdaddr(provider, &srv->req_sa.l2cap_bdaddr))
163 continue;
164
165 if (provider->profile->uuid == uuid ||
166 SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP == uuid) {
167 SDP_PUT32(provider->handle, ptr);
168 rcount ++;
169 }
170 }
171 }
172
173 /* Set reply size (not counting PDU header and continuation state) */
174 srv->fdidx[fd].rsp_limit = srv->fdidx[fd].omtu - sizeof(sdp_pdu_t) - 4;
175 srv->fdidx[fd].rsp_size = ptr - rsp;
176 srv->fdidx[fd].rsp_cs = 0;
177
178 return (0);
179}
180
181/*
182 * Send SDP Service Search Response
183 */
184
185int32_t
186server_send_service_search_response(server_p srv, int32_t fd)
187{
188 uint8_t *rsp = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_cs;
189 uint8_t *rsp_end = srv->fdidx[fd].rsp + srv->fdidx[fd].rsp_size;
190
191 struct iovec iov[4];
192 sdp_pdu_t pdu;
193 uint16_t rcounts[2];
194 uint8_t cs[3];
195 int32_t size;
196
197 /* First update continuation state (assume we will send all data) */
198 size = rsp_end - rsp;
199 srv->fdidx[fd].rsp_cs += size;
200
201 if (size + 1 > srv->fdidx[fd].rsp_limit) {
202 /*
203 * We need to split out response. Add 3 more bytes for the
204 * continuation state and move rsp_end and rsp_cs backwards.
205 */
206
207 while ((rsp_end - rsp) + 3 > srv->fdidx[fd].rsp_limit) {
208 rsp_end -= 4;
209 srv->fdidx[fd].rsp_cs -= 4;
210 }
211
212 cs[0] = 2;
213 cs[1] = srv->fdidx[fd].rsp_cs >> 8;
214 cs[2] = srv->fdidx[fd].rsp_cs & 0xff;
215 } else
216 cs[0] = 0;
217
218 assert(rsp_end >= rsp);
219
220 rcounts[0] = srv->fdidx[fd].rsp_size / 4; /* TotalServiceRecordCount */
221 rcounts[1] = (rsp_end - rsp) / 4; /* CurrentServiceRecordCount */
222
223 pdu.pid = SDP_PDU_SERVICE_SEARCH_RESPONSE;
224 pdu.tid = ((sdp_pdu_p)(srv->req))->tid;
225 pdu.len = htons(sizeof(rcounts) + rcounts[1] * 4 + 1 + cs[0]);
226
227 iov[0].iov_base = &pdu;
228 iov[0].iov_len = sizeof(pdu);
229
230 iov[1].iov_base = rcounts;
231 iov[1].iov_len = sizeof(rcounts);
232
233 iov[2].iov_base = rsp;
234 iov[2].iov_len = rsp_end - rsp;
235
236 iov[3].iov_base = cs;
237 iov[3].iov_len = 1 + cs[0];
238
239 do {
240 size = writev(fd, (struct iovec const *) &iov, sizeof(iov)/sizeof(iov[0]));
241 } while (size < 0 && errno == EINTR);
242
243 /* Check if we have sent (or failed to sent) last response chunk */
244 if (srv->fdidx[fd].rsp_cs == srv->fdidx[fd].rsp_size) {
245 srv->fdidx[fd].rsp_cs = 0;
246 srv->fdidx[fd].rsp_size = 0;
247 srv->fdidx[fd].rsp_limit = 0;
248 }
249
250 return ((size < 0)? errno : 0);
251}
252