#include <sys/wait.h>
#define L2CAP_SOCKET_CHECKED
#include <bluetooth.h>
#include <err.h>
#include <fcntl.h>
#include <paths.h>
#include <sdp.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "btpand.h"
const char * control_path;
const char * interface_name;
const char * service_name;
uint16_t service_class;
bdaddr_t local_bdaddr;
bdaddr_t remote_bdaddr;
uint16_t l2cap_psm;
int l2cap_mode;
int server_limit;
static const struct {
const char * name;
uint16_t class;
const char * desc;
} services[] = {
{ "PANU", SDP_SERVICE_CLASS_PANU, "Personal Area Networking User" },
{ "NAP", SDP_SERVICE_CLASS_NAP, "Network Access Point" },
{ "GN", SDP_SERVICE_CLASS_GN, "Group Network" },
};
static void main_exit(int) __dead2;
static void main_detach(void);
static void usage(void) __dead2;
int
main(int argc, char *argv[])
{
unsigned long ul;
char * ep;
int ch, status;
while ((ch = getopt(argc, argv, "a:c:d:i:l:m:p:S:s:")) != -1) {
switch (ch) {
case 'a':
if (!bt_aton(optarg, &remote_bdaddr)) {
struct hostent *he;
if ((he = bt_gethostbyname(optarg)) == NULL)
errx(EXIT_FAILURE, "%s: %s",
optarg, hstrerror(h_errno));
bdaddr_copy(&remote_bdaddr,
(bdaddr_t *)he->h_addr);
}
break;
case 'c':
control_path = optarg;
break;
case 'd':
if (!bt_devaddr(optarg, &local_bdaddr)) {
struct hostent *he;
if ((he = bt_gethostbyname(optarg)) == NULL)
errx(EXIT_FAILURE, "%s: %s",
optarg, hstrerror(h_errno));
bdaddr_copy(&local_bdaddr,
(bdaddr_t *)he->h_addr);
}
break;
case 'i':
if (strchr(optarg, '/') == NULL) {
asprintf(&ep, "/dev/%s", optarg);
interface_name = ep;
} else
interface_name = optarg;
break;
case 'l':
ul = strtoul(optarg, &ep, 10);
if (*optarg == '\0' || *ep != '\0' || ul == 0)
errx(EXIT_FAILURE, "%s: invalid session limit",
optarg);
server_limit = ul;
break;
case 'm':
warnx("Setting link mode is not yet supported");
break;
case 'p':
ul = strtoul(optarg, &ep, 0);
if (*optarg == '\0' || *ep != '\0'
|| ul > 0xffff || L2CAP_PSM_INVALID(ul))
errx(EXIT_FAILURE, "%s: invalid PSM", optarg);
l2cap_psm = ul;
break;
case 's':
case 'S':
for (ul = 0; ul < __arraycount(services); ul++) {
if (strcasecmp(optarg, services[ul].name) == 0)
break;
}
if (ul == __arraycount(services))
errx(EXIT_FAILURE, "%s: unknown service", optarg);
if (ch == 's')
service_name = services[ul].name;
service_class = services[ul].class;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (bdaddr_any(&local_bdaddr) || service_class == 0)
usage();
if (!bdaddr_any(&remote_bdaddr) && (server_limit != 0 ||
control_path != NULL || (service_name != NULL && l2cap_psm != 0)))
usage();
if (interface_name == NULL)
interface_name = "/dev/tap";
if (l2cap_psm == 0)
l2cap_psm = L2CAP_PSM_BNEP;
if (bdaddr_any(&remote_bdaddr) && server_limit == 0) {
if (service_class == SDP_SERVICE_CLASS_PANU)
server_limit = 1;
else
server_limit = 7;
}
#ifdef L2CAP_LM_MASTER
if (server_limit > 1 && service_class != SDP_SERVICE_CLASS_PANU)
l2cap_mode |= L2CAP_LM_MASTER;
#endif
switch(fork()) {
case -1:
err(EXIT_FAILURE, "fork() failed");
case 0:
signal(SIGPIPE, SIG_IGN);
openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
channel_init();
server_init();
event_init();
client_init();
tap_init();
main_detach();
event_dispatch();
break;
default:
signal(SIGUSR1, main_exit);
wait(&status);
if (WIFEXITED(status))
exit(WEXITSTATUS(status));
break;
}
err(EXIT_FAILURE, "exiting");
}
static void
main_exit(int s)
{
_exit(EXIT_SUCCESS);
}
static void
main_detach(void)
{
int fd;
if (kill(getppid(), SIGUSR1) == -1)
log_err("Could not signal main process: %m");
if (setsid() == -1)
log_err("setsid() failed");
fd = open(_PATH_DEVNULL, O_RDWR, 0);
if (fd == -1) {
log_err("Could not open %s", _PATH_DEVNULL);
} else {
(void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO);
(void)dup2(fd, STDERR_FILENO);
close(fd);
}
}
static void
usage(void)
{
const char *p = getprogname();
int n = strlen(p);
fprintf(stderr,
"usage: %s [-i ifname] [-m mode] -a address -d device\n"
" %*s {-s service | -S service [-p psm]}\n"
" %s [-c path] [-i ifname] [-l limit] [-m mode] [-p psm] -d device\n"
" %*s {-s service | -S service}\n"
"\n"
"Where:\n"
"\t-a address remote bluetooth device\n"
"\t-c path SDP server socket\n"
"\t-d device local bluetooth device\n"
"\t-i ifname tap interface\n"
"\t-l limit limit server sessions\n"
"\t-m mode L2CAP link mode (NOT YET SUPPORTED)\n"
"\t-p psm L2CAP PSM\n"
"\t-S service service name (no SDP)\n"
"\t-s service service name\n"
"\n"
"Known services:\n"
"", p, n, "", p, n, "");
for (n = 0; n < __arraycount(services); n++)
fprintf(stderr, "\t%s\t%s\n", services[n].name, services[n].desc);
exit(EXIT_FAILURE);
}