summaryrefslogtreecommitdiff
path: root/usr.sbin/btd/sdp.c
blob: bf307cc9fec321e3b6a4e4c75d194e5613606557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*	$OpenBSD: sdp.c,v 1.5 2008/11/27 00:51:17 uwe Exp $	*/

/*
 * Copyright (c) 2008 Uwe Stuehler <uwe@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <netbt/l2cap.h>

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "btd.h"
#include "sdp.h"

struct sdp_state {
	TAILQ_HEAD(, sdp_session) sessions;
};

struct sdp_session *sdp_new_session(struct sdp_state *, const bdaddr_t *);
struct sdp_session *sdp_find_session(struct sdp_state *, const bdaddr_t *);
int sdp_open_session(struct sdp_session *, const bdaddr_t *);
void sdp_close_session(struct sdp_session *);
void sdp_delete_session(struct sdp_session *);
void sdp_eventcb(int, short, void *);

void
sdp_init(struct btd *env)
{
	struct sdp_state *sdp;

	sdp = env->sdp = calloc(1, sizeof(*env->sdp));

	TAILQ_INIT(&sdp->sessions);
}

/* return 0 on success, 2 if the SDP query is still in progress, 1 if
 * the device was not found, -1 on error */
int
sdp_get_devinfo(struct bt_interface *iface, struct bt_device *btdev)
{
	struct sdp_state *sdp = iface->env->sdp;
	struct sdp_session *sess;
	const char *service;

	switch (btdev->type) {
	case BTDEV_NONE:
		return 1;
	case BTDEV_HID:
		service = "HID";
		break;
	case BTDEV_HSET:
		service = "HSET";
		break;
	case BTDEV_HF:
		service = "HF";
		break;
	default:
		log_warnx("sdp_get_devinfo: invalid device type %#x",
		    btdev->type);
		return -1;
	}

	if ((sess = sdp_find_session(sdp, &btdev->addr)) == NULL &&
	    (sess = sdp_new_session(sdp, &btdev->addr)) == NULL)
		return -1;

	if (sdp_open_session(sess, &iface->addr) < 0)
		return -1;

	if (sdp_query(sess, &btdev->info.baa, &iface->addr, &btdev->addr,
	    service) < 0) {
		sdp_close_session(sess);
		return -1;
	}

	sdp_close_session(sess);
	return 0;
}

struct sdp_session *
sdp_new_session(struct sdp_state *sdp, const bdaddr_t *raddr)
{
	struct sdp_session *sess;

	if ((sess = calloc(1, sizeof(*sess))) == NULL) {
		log_warn("sdp_new_session");
		return NULL;
	}

	sess->sdp = sdp;
	sess->state = SDP_SESSION_CLOSED;
	bdaddr_copy(&sess->raddr, raddr);

	TAILQ_INSERT_TAIL(&sdp->sessions, sess, entry);

	return sess;
}

struct sdp_session *
sdp_find_session(struct sdp_state *sdp, const bdaddr_t *raddr)
{
	struct sdp_session *sess;

	TAILQ_FOREACH(sess, &sdp->sessions, entry)
		if (bdaddr_same(&sess->raddr, raddr))
			return sess;

	return NULL;
}

int
sdp_open_session(struct sdp_session *sess, const bdaddr_t *laddr)
{
	struct sockaddr_bt sa;
	socklen_t size;

	if (sess->state != SDP_SESSION_CLOSED) {
		if (bdaddr_same(&sess->laddr, laddr))
			return 0;

		sdp_close_session(sess);
	}

	bdaddr_copy(&sess->laddr, laddr);

	sess->fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
	if (sess->fd == -1) {
		log_warn("sdp_open_session: socket");
		goto fail;
	}

	memset(&sa, 0, sizeof(sa));
	sa.bt_len = sizeof(sa);
	sa.bt_family = AF_BLUETOOTH;
	bdaddr_copy(&sa.bt_bdaddr, &sess->laddr);

	if (bind(sess->fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
		log_warn("sdp_open_session: bind");
		goto fail;
	}

	sa.bt_psm = L2CAP_PSM_SDP;
	bdaddr_copy(&sa.bt_bdaddr, &sess->raddr);

	if (connect(sess->fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
		log_warn("sdp_open_session: connect");
		goto fail;
	}

	size = sizeof(sess->omtu);
	if (getsockopt(sess->fd, BTPROTO_L2CAP, SO_L2CAP_OMTU, &sess->omtu,
	    &size) < 0) {
		log_warn("sdp_open_session: getsockopt");
		goto fail;
	}

	size = sizeof(sess->imtu);
	if (getsockopt(sess->fd, BTPROTO_L2CAP, SO_L2CAP_IMTU, &sess->imtu,
	    &size) < 0) {
		log_warn("sdp_open_session: getsockopt");
		goto fail;
	}

	if ((sess->req = malloc((size_t)sess->omtu)) == NULL) {
		log_warn("sdp_open_session: malloc req");
		goto fail;
	}
	sess->req_e = sess->req + sess->omtu;

	if ((sess->rsp = malloc((size_t)sess->imtu)) == NULL) {
		log_warn("sdp_open_session: malloc rsp");
		goto fail;
	}
	sess->rsp_e = sess->rsp + sess->imtu;

	event_set(&sess->ev, sess->fd, EV_READ | EV_PERSIST,
	    sdp_eventcb, sess);
	if (event_add(&sess->ev, NULL)) {
		log_warnx("event_add failed");
		goto fail;
	}

	sess->state = SDP_SESSION_OPEN;
	return 0;

fail:
	if (sess->fd != -1) {
		close(sess->fd);
		sess->fd = -1;
	}

	if (sess->req != NULL) {
		free(sess->req);
		sess->req = NULL;
	}

	if (sess->rsp != NULL) {
		free(sess->rsp);
		sess->rsp = NULL;
	}

	return -1;
}

void
sdp_close_session(struct sdp_session *sess)
{
	if (sess->state == SDP_SESSION_CLOSED)
		return;

	if (sess->req != NULL) {
		free(sess->req);
		sess->req = NULL;
	}

	if (sess->rsp != NULL) {
		free(sess->rsp);
		sess->rsp = NULL;
	}

	close(sess->fd);
	sess->fd = -1;
	sess->state = SDP_SESSION_CLOSED;
}

void
sdp_delete_session(struct sdp_session *sess)
{
	struct sdp_state *sdp = sess->sdp;

	sdp_close_session(sess);

	TAILQ_REMOVE(&sdp->sessions, sess, entry);

	free(sess);
}

void
sdp_eventcb(int fd, short evflags, void *arg)
{
}