-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeetle.c
372 lines (320 loc) · 10.2 KB
/
beetle.c
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/********************************************************************************
*
* Copyright 2016-2017 Afero, Inc.
*
* Licensed under the MIT license (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License
* at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <fcntl.h>
#include <stdarg.h>
#include <getopt.h>
#include <sys/poll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <bluetooth/bluetooth.h>
#include "beetle.h"
#include "central.h"
#include "command.h"
#include "connlist.h"
#include "devicelist.h"
#include "hci_beetle.h"
#include "log.h"
#include "peripheral.h"
#include "utils.h"
#include "build_info.h"
#define CENTRAL_NAME "cen"
#define PERIPHERAL_NAME "per"
#define DEFAULT_PORT 6969
static session_type_t s_defaultSessionType = centralSession;
static session_type_t s_sessionType;
session_type_t get_session_type(void)
{
return s_sessionType;
}
static int s_clientFd = -1; /* client socket */
static int s_signal = 0; /* the signal that occurred */
// listening IP/port:
static struct sockaddr_in g_sockaddr;
#define SET_SCAN_ENABLE_TIMEOUT 5000
#define SET_ADVERTISE_ENABLE_TIMEOUT 5000
#define CONNECT_TIMEOUT 5000
#define NO_SOCKET_FD 0xffff
#define OPEN_ATTEMPT_DELAY 20
#define MAX_OPEN_ATTEMPTS 10
#define CMD_BUF_SIZE 1024
int send_cmd(char *fmt, ...)
{
char buf[CMD_BUF_SIZE];
va_list va;
int len;
if (s_clientFd < 0) {
errno = EBADF;
return -1;
}
va_start(va, fmt);
len = vsnprintf (buf, sizeof(buf), fmt, va);
va_end(va);
/* don't log advertisements */
if (buf[0] != 'a' || s_sessionType == peripheralSession) TRACE("<%s", buf);
int res = write (s_clientFd, buf, len);
if (res < 0) {
return res;
}
return write (s_clientFd, "\n", 1);
}
/* handle debug command */
int cmd_debug(void *param1, void *param2, void *param3, void *context)
{
g_debugging = *(int *)param1;
send_cmd("deb %04x", STATUS_OK);
return 0;
}
/* handle central command */
int cmd_mode(void *param1, void *param2, void *param3, void *context)
{
int retVal = 0;
char *mode = (char *)param1;
if (!strncasecmp(mode, PERIPHERAL_NAME, sizeof(PERIPHERAL_NAME) - 1)) {
if (s_sessionType != peripheralSession) {
s_sessionType = peripheralSession;
retVal = SESSION_SWITCH_SESSION; /* we're switching sessions */
send_cmd("mod %04x %04x", STATUS_OK, s_sessionType);
} else {
send_cmd("mod %04x", STATUS_ALREADY_IN_MODE);
}
} else if (!strncasecmp(mode, CENTRAL_NAME, sizeof(CENTRAL_NAME) - 1)) {
if (s_sessionType != centralSession) {
s_sessionType = centralSession;
retVal = SESSION_SWITCH_SESSION;
send_cmd("mod %04x %04x", STATUS_OK, s_sessionType);
} else {
send_cmd("mod %04x", STATUS_ALREADY_IN_MODE);
}
} else {
send_cmd("mod %04x", STATUS_BAD_PARAM);
}
return retVal;
}
static int set_up_listener(void) {
/* create the socket */
int listenFd = socket(AF_INET, SOCK_STREAM, 0);
if (listenFd < 0) {
log_failure("socket");
return -1;
}
int optval = 1;
if (setsockopt(listenFd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int)) < 0) {
log_failure("setsockopt");
close(listenFd);
return -1;
}
if (bind(listenFd, (struct sockaddr *)&g_sockaddr, sizeof(g_sockaddr)) < 0) {
log_failure("bind");
close(listenFd);
return -1;
}
if (listen(listenFd, 1) < 0) {
log_failure("listen");
close(listenFd);
return -1;
}
char address[32];
INFO("listening on %s:%d",
inet_ntop(AF_INET, &g_sockaddr.sin_addr, address, sizeof(address)),
ntohs(g_sockaddr.sin_port));
return listenFd;
}
static int accept_connection(int listenFd)
{
struct sockaddr_in clientaddr;
socklen_t len = sizeof(clientaddr);
int clientFd = accept(listenFd, (struct sockaddr *)&clientaddr, &len);
if (clientFd < 0) {
log_failure("accept");
return -1;
}
INFO("Connection from %s", inet_ntoa(clientaddr.sin_addr));
return clientFd;
}
int dying(void) {
return s_signal == SIGINT || s_signal == SIGTERM;
}
/* return & clear any non-fatal signal */
int signaled(void) {
if (dying()) return s_signal;
int rv = s_signal;
s_signal = 0;
return rv;
}
static void on_signal(int signal) {
s_signal = signal;
}
static void setup_signals(void) {
// Set signal handlers
sigset_t sigset;
sigemptyset(&sigset);
struct sigaction siginfo = {
.sa_handler = on_signal,
.sa_mask = sigset,
.sa_flags = 0,
};
sigaction(SIGINT, &siginfo, NULL);
sigaction(SIGTERM, &siginfo, NULL);
sigaction(SIGUSR1, &siginfo, NULL);
sigaction(SIGUSR2, &siginfo, NULL);
}
static void usage(void) {
fprintf(stderr, "usage -- beetle [options]\n");
fprintf(stderr, " -v show version info and exit\n");
fprintf(stderr, " -i <interface> set hci interface (example: hci0)\n");
fprintf(stderr, " -m {cen|per} set start up mode (central or peripheral)\n");
fprintf(stderr, " -p <port#> set listen port\n");
fprintf(stderr, " -A <IP> listen on a specific IP interface, instead of localhost (*security risk*)\n");
fprintf(stderr, " -d run in background (daemon)\n");
fprintf(stderr, " -D increase debug log level\n");
}
int main(int argc, char* const argv[])
{
int daemonize = 0;
int opt;
char *interface = NULL;
memset(&g_sockaddr, 0, sizeof(g_sockaddr));
g_sockaddr.sin_family = AF_INET;
g_sockaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
g_sockaddr.sin_port = htons(DEFAULT_PORT);
while ((opt = getopt(argc, argv, "A:Ddi:m:p:qv")) != -1) {
switch (opt) {
case 'A':
if (!inet_pton(AF_INET, optarg, &g_sockaddr.sin_addr)) {
fprintf(stderr, "unable to parse -A address\n");
exit(1);
}
break;
case 'D':
debug_level_increase();
break;
case 'd':
daemonize = 1;
break;
case 'i':
interface = optarg;
break;
case 'm':
if (!strncasecmp(optarg, CENTRAL_NAME, sizeof(CENTRAL_NAME) - 1)) {
s_defaultSessionType = centralSession;
} else if (!strncasecmp(optarg, PERIPHERAL_NAME, sizeof(PERIPHERAL_NAME) - 1)) {
s_defaultSessionType = peripheralSession;
} else {
usage();
exit(1);
}
break;
case 'p': {
int port = atoi(optarg);
if (port < 1024 || port > 65535) {
fprintf(stderr, "port must be between 1024 and 65535; falling back to default %d\n", DEFAULT_PORT);
port = DEFAULT_PORT;
}
g_sockaddr.sin_port = htons(port);
break;
}
case 'q':
debug_level_decrease();
break;
case 'v':
fprintf(stderr, "beetle-%s %s %s\n", BUILD_NUMBER, BUILD_DATE, REVISION);
exit(0);
break;
default:
usage();
exit(1);
break;
}
}
if (daemonize) {
pid_t pid = fork();
if (pid < 0) {
log_failure("fork failed");
exit(1);
} else if (pid != 0) {
exit(0);
}
}
openlog("beetle", LOG_PID | LOG_NDELAY, LOG_USER);
INFO("beetle-%s starting up: %s %s", BUILD_NUMBER, BUILD_DATE, REVISION);
setup_signals();
int listenFd = set_up_listener();
if (listenFd < 0) {
goto exit;
}
while (1) {
bhci_t bhci;
if (bhci_open(&bhci, interface) < 0) {
log_failure("bhci_open");
exit(1);
}
s_clientFd = accept_connection(listenFd);
if (s_clientFd >= 0) {
/* go to default session type */
s_sessionType = s_defaultSessionType;
while (1) {
int ret;
if (s_sessionType == centralSession) {
ret = central_session(s_clientFd, &bhci);
} else if (s_sessionType == peripheralSession) {
ret = peripheral_session(s_clientFd, &bhci);
} else {
ERROR("unknown session type %d", s_sessionType);
ret = SESSION_FAILED_FATAL;
break;
}
if (ret == SESSION_REBUILD_BLUETOOTH) {
DEBUG("bluetooth looks hoarked; kicking it");
bhci_close(&bhci);
if (bhci_open(&bhci, interface) < 0) {
log_failure("bhci_open");
exit(1);
}
continue;
}
if (ret != SESSION_SWITCH_SESSION) {
break;
}
}
INFO("Disconnecting");
close(s_clientFd);
}
/* we can safely exit without BlueZ issues here */
if (s_signal) {
close(listenFd);
closelog();
exit(128 + s_signal);
}
bhci_close(&bhci);
}
close(listenFd);
exit:
closelog();
return 0;
}