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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
* Copyright (C) 2002-2009 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include "dund.h"
#include "lib.h"
#define PROC_BASE "/proc"
static int for_each_port(int (*func)(struct rfcomm_dev_info *, unsigned long), unsigned long arg)
{
struct rfcomm_dev_list_req *dl;
struct rfcomm_dev_info *di;
long r = 0;
int sk, i;
sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM);
if (sk < 0 ) {
perror("Can't open RFCOMM control socket");
exit(1);
}
dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di));
if (!dl) {
perror("Can't allocate request memory");
close(sk);
exit(1);
}
dl->dev_num = RFCOMM_MAX_DEV;
di = dl->dev_info;
if (ioctl(sk, RFCOMMGETDEVLIST, (void *) dl) < 0) {
perror("Can't get device list");
exit(1);
}
for (i = 0; i < dl->dev_num; i++) {
r = func(di + i, arg);
if (r) break;
}
close(sk);
free(dl);
return r;
}
static int uses_rfcomm(char *path, char *dev)
{
struct dirent *de;
DIR *dir;
dir = opendir(path);
if (!dir)
return 0;
if (chdir(path) < 0)
return 0;
while ((de = readdir(dir)) != NULL) {
char link[PATH_MAX + 1];
int len = readlink(de->d_name, link, sizeof(link));
if (len > 0) {
link[len] = 0;
if (strstr(link, dev)) {
closedir(dir);
return 1;
}
}
}
closedir(dir);
return 0;
}
static int find_pppd(int id, pid_t *pid)
{
struct dirent *de;
char path[PATH_MAX + 1];
char dev[10];
int empty = 1;
DIR *dir;
dir = opendir(PROC_BASE);
if (!dir) {
perror(PROC_BASE);
return -1;
}
sprintf(dev, "rfcomm%d", id);
*pid = 0;
while ((de = readdir(dir)) != NULL) {
empty = 0;
if (isdigit(de->d_name[0])) {
sprintf(path, "%s/%s/fd", PROC_BASE, de->d_name);
if (uses_rfcomm(path, dev)) {
*pid = atoi(de->d_name);
break;
}
}
}
closedir(dir);
if (empty)
fprintf(stderr, "%s is empty (not mounted ?)\n", PROC_BASE);
return *pid != 0;
}
static int dun_exec(char *tty, char *prog, char **args)
{
int pid = fork();
int fd;
switch (pid) {
case -1:
return -1;
case 0:
break;
default:
return pid;
}
setsid();
/* Close all FDs */
for (fd = 3; fd < 20; fd++)
close(fd);
execvp(prog, args);
syslog(LOG_ERR, "Error while executing %s", prog);
exit(1);
}
static int dun_create_tty(int sk, char *tty, int size)
{
struct sockaddr_rc sa;
struct stat st;
socklen_t alen;
int id, try = 30;
struct rfcomm_dev_req req = {
flags: (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP),
dev_id: -1
};
alen = sizeof(sa);
if (getpeername(sk, (struct sockaddr *) &sa, &alen) < 0)
return -1;
bacpy(&req.dst, &sa.rc_bdaddr);
alen = sizeof(sa);
if (getsockname(sk, (struct sockaddr *) &sa, &alen) < 0)
return -1;
bacpy(&req.src, &sa.rc_bdaddr);
req.channel = sa.rc_channel;
id = ioctl(sk, RFCOMMCREATEDEV, &req);
if (id < 0)
return id;
snprintf(tty, size, "/dev/rfcomm%d", id);
while (stat(tty, &st) < 0) {
snprintf(tty, size, "/dev/bluetooth/rfcomm/%d", id);
if (stat(tty, &st) < 0) {
snprintf(tty, size, "/dev/rfcomm%d", id);
if (try--) {
usleep(100 * 1000);
continue;
}
memset(&req, 0, sizeof(req));
req.dev_id = id;
ioctl(sk, RFCOMMRELEASEDEV, &req);
return -1;
}
}
return id;
}
int dun_init(void)
{
return 0;
}
int dun_cleanup(void)
{
return 0;
}
static int show_conn(struct rfcomm_dev_info *di, unsigned long arg)
{
pid_t pid;
if (di->state == BT_CONNECTED &&
(di->flags & (1<<RFCOMM_REUSE_DLC)) &&
(di->flags & (1<<RFCOMM_TTY_ATTACHED)) &&
(di->flags & (1<<RFCOMM_RELEASE_ONHUP))) {
if (find_pppd(di->id, &pid)) {
char dst[18];
ba2str(&di->dst, dst);
printf("rfcomm%d: %s channel %d pppd pid %d\n",
di->id, dst, di->channel, pid);
}
}
return 0;
}
static int kill_conn(struct rfcomm_dev_info *di, unsigned long arg)
{
bdaddr_t *dst = (bdaddr_t *) arg;
pid_t pid;
if (di->state == BT_CONNECTED &&
(di->flags & (1<<RFCOMM_REUSE_DLC)) &&
(di->flags & (1<<RFCOMM_TTY_ATTACHED)) &&
(di->flags & (1<<RFCOMM_RELEASE_ONHUP))) {
if (dst && bacmp(&di->dst, dst))
return 0;
if (find_pppd(di->id, &pid)) {
if (kill(pid, SIGINT) < 0)
perror("Kill");
if (!dst)
return 0;
return 1;
}
}
return 0;
}
int dun_show_connections(void)
{
for_each_port(show_conn, 0);
return 0;
}
int dun_kill_connection(uint8_t *dst)
{
for_each_port(kill_conn, (unsigned long) dst);
return 0;
}
int dun_kill_all_connections(void)
{
for_each_port(kill_conn, 0);
return 0;
}
int dun_open_connection(int sk, char *pppd, char **args, int wait)
{
char tty[100];
int pid;
if (dun_create_tty(sk, tty, sizeof(tty) - 1) < 0) {
syslog(LOG_ERR, "RFCOMM TTY creation failed. %s(%d)", strerror(errno), errno);
return -1;
}
args[0] = "pppd";
args[1] = tty;
args[2] = "nodetach";
pid = dun_exec(tty, pppd, args);
if (pid < 0) {
syslog(LOG_ERR, "Exec failed. %s(%d)", strerror(errno), errno);
return -1;
}
if (wait) {
int status;
waitpid(pid, &status, 0);
/* FIXME: Check for waitpid errors */
}
return 0;
}
|