diff --git a/tools/btproxy.c b/tools/btproxy.c
index 8779af5..139788d 100644
--- a/tools/btproxy.c
+++ b/tools/btproxy.c
#include <sys/socket.h>
#include <sys/un.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
#include "monitor/mainloop.h"
#include "monitor/bt.h"
len = read(client_fd, client_buffer + client_length,
sizeof(client_buffer) - client_length);
if (len < 1) {
- fprintf(stderr, "Failed to read unix packet\n");
+ fprintf(stderr, "Failed to read client packet\n");
return;
}
fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd < 0) {
- perror("Failed to open server socket");
+ perror("Failed to open Unix server socket");
return -1;
}
strcpy(addr.sun_path, path);
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
- perror("Failed to bind server socket");
+ perror("Failed to bind Unix server socket");
close(fd);
return -1;
}
if (listen(fd, 1) < 0) {
- perror("Failed to listen server socket");
+ perror("Failed to listen Unix server socket");
close(fd);
return -1;
}
return fd;
}
+static int open_tcp(unsigned int port)
+{
+ struct sockaddr_in addr;
+ int fd, opt = 1;
+
+ fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (fd < 0) {
+ perror("Failed to open TCP server socket");
+ return -1;
+ }
+
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = INADDR_ANY;
+ addr.sin_port = htons(port);
+
+ if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ perror("Failed to bind TCP server socket");
+ close(fd);
+ return -1;
+ }
+
+ if (listen(fd, 1) < 0) {
+ perror("Failed to listen TCP server socket");
+ close(fd);
+ return -1;
+ }
+
+ return fd;
+}
+
static void signal_callback(int signum, void *user_data)
{
switch (signum) {
printf("\tbtproxy [options]\n");
printf("options:\n"
"\t-u, --unix [unixpath] Use unix server\n"
+ "\t-p, --port [port] Use TCP server\n"
"\t-i, --index <num> Use specified controller\n"
"\t-h, --help Show help options\n");
}
static const struct option main_options[] = {
- { "unix", optional_argument, NULL, 'U' },
+ { "unix", optional_argument, NULL, 'u' },
+ { "port", optional_argument, NULL, 'p' },
{ "index", required_argument, NULL, 'i' },
{ "version", no_argument, NULL, 'v' },
{ "help", no_argument, NULL, 'h' },
int main(int argc, char *argv[])
{
const char *unixpath = NULL;
+ unsigned short tcpport = 0;
const char *str;
sigset_t mask;
for (;;) {
int opt;
- opt = getopt_long(argc, argv, "u::i:vh",
+ opt = getopt_long(argc, argv, "u::p::i:vh",
main_options, NULL);
if (opt < 0)
break;
else
unixpath = "/tmp/bt-server-bredr";
break;
+ case 'p':
+ if (optarg)
+ tcpport = atoi(optarg);
+ else
+ tcpport = 0xb1ee;
+ break;
case 'i':
if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
str = optarg + 3;
mainloop_set_signal(&mask, signal_callback, NULL, NULL);
- if (unixpath) {
+ if (unixpath)
server_fd = open_unix(unixpath);
- if (server_fd < 0)
- return EXIT_FAILURE;
-
- mainloop_add_fd(server_fd, EPOLLIN, server_callback,
- NULL, NULL);
- } else {
+ else if (tcpport > 0)
+ server_fd = open_tcp(tcpport);
+ else {
fprintf(stderr, "Missing emulator device\n");
return EXIT_FAILURE;
}
+ if (server_fd < 0)
+ return EXIT_FAILURE;
+
+ mainloop_add_fd(server_fd, EPOLLIN, server_callback, NULL, NULL);
+
return mainloop_run();
}