From 965c347ab154fd36ca98cd1fd29b048068d70dab Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 25 Apr 2004 21:54:11 +0000 Subject: Add the pand utility --- pand/Makefile.am | 15 ++ pand/bnep.c | 305 ++++++++++++++++++++++++++++ pand/main.c | 604 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ pand/pand.1 | 59 ++++++ pand/pand.h | 44 ++++ pand/sdp.c | 201 ++++++++++++++++++ 6 files changed, 1228 insertions(+) create mode 100644 pand/Makefile.am create mode 100644 pand/bnep.c create mode 100644 pand/main.c create mode 100644 pand/pand.1 create mode 100644 pand/pand.h create mode 100644 pand/sdp.c (limited to 'pand') diff --git a/pand/Makefile.am b/pand/Makefile.am new file mode 100644 index 00000000..20b99270 --- /dev/null +++ b/pand/Makefile.am @@ -0,0 +1,15 @@ +# +# $Id$ +# + +bin_PROGRAMS = pand + +pand_SOURCES = main.c pand.h bnep.c sdp.c + +LDFLAGS = @BLUEZ_LIBS@ + +INCLUDES = @BLUEZ_INCLUDES@ + +man_MANS = pand.1 + +EXTRA_DIST = $(man_MANS) diff --git a/pand/bnep.c b/pand/bnep.c new file mode 100644 index 00000000..6833f941 --- /dev/null +++ b/pand/bnep.c @@ -0,0 +1,305 @@ +/* + pand - Bluetooth PAN daemon for BlueZ + Copyright (C) 2002 Maxim Krasnyansky + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* + * $Id$ + */ + +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include + +#include "pand.h" + +static int ctl; + +/* Compatibility with old ioctls */ +#define OLD_BNEPCONADD 1 +#define OLD_BNEPCONDEL 2 +#define OLD_BNEPGETCONLIST 3 +#define OLD_BNEPGETCONINFO 4 + +static unsigned long bnepconnadd; +static unsigned long bnepconndel; +static unsigned long bnepgetconnlist; +static unsigned long bnepgetconninfo; + +static struct { + char *str; + uint16_t uuid; +} __svc[] = { + { "PANU", BNEP_SVC_PANU }, + { "NAP", BNEP_SVC_NAP }, + { "GN", BNEP_SVC_GN }, + { NULL } +}; + +int bnep_str2svc(char *svc, uint16_t *uuid) +{ + int i; + for (i=0; __svc[i].str; i++) + if (!strcasecmp(svc, __svc[i].str)) { + *uuid = __svc[i].uuid; + return 0; + } + return -1; +} + +char *bnep_svc2str(uint16_t uuid) +{ + int i; + for (i=0; __svc[i].str; i++) + if (__svc[i].uuid == uuid) + return __svc[i].str; + return NULL; +} + +int bnep_init(void) +{ + ctl = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_BNEP); + if (ctl < 0) { + perror("Failed to open control socket"); + return 1; + } + + /* Temporary ioctl compatibility hack */ + { + struct bnep_connlist_req req; + struct bnep_conninfo ci[1]; + + req.cnum = 1; + req.ci = ci; + + if (!ioctl(ctl, BNEPGETCONNLIST, &req)) { + /* New ioctls */ + bnepconnadd = BNEPCONNADD; + bnepconndel = BNEPCONNDEL; + bnepgetconnlist = BNEPGETCONNLIST; + bnepgetconninfo = BNEPGETCONNINFO; + } else { + /* Old ioctls */ + bnepconnadd = OLD_BNEPCONADD; + bnepconndel = OLD_BNEPCONDEL; + bnepgetconnlist = OLD_BNEPGETCONLIST; + bnepgetconninfo = OLD_BNEPGETCONINFO; + } + } + + return 0; +} + +int bnep_cleanup(void) +{ + close(ctl); + return 0; +} + +int bnep_show_connections(void) +{ + struct bnep_connlist_req req; + struct bnep_conninfo ci[48]; + int i; + + req.cnum = 48; + req.ci = ci; + if (ioctl(ctl, bnepgetconnlist, &req)) { + perror("Failed to get connection list"); + return -1; + } + + for (i=0; i < req.cnum; i++) { + printf("%s %s %s\n", ci[i].device, + batostr((bdaddr_t *) ci[i].dst), + bnep_svc2str(ci[i].role)); + } + return 0; +} + +int bnep_kill_connection(uint8_t *dst) +{ + struct bnep_conndel_req req; + + memcpy(req.dst, dst, ETH_ALEN); + req.flags = 0; + if (ioctl(ctl, bnepconndel, &req)) { + perror("Failed to kill connection"); + return -1; + } + return 0; +} + +int bnep_kill_all_connections(void) +{ + struct bnep_connlist_req req; + struct bnep_conninfo ci[48]; + int i; + + req.cnum = 48; + req.ci = ci; + if (ioctl(ctl, bnepgetconnlist, &req)) { + perror("Failed to get connection list"); + return -1; + } + + for (i=0; i < req.cnum; i++) { + struct bnep_conndel_req req; + memcpy(req.dst, ci[i].dst, ETH_ALEN); + req.flags = 0; + ioctl(ctl, bnepconndel, &req); + } + return 0; +} + +static int bnep_connadd(int sk, uint16_t role, char *dev) +{ + struct bnep_connadd_req req; + + strcpy(req.device, dev); + req.sock = sk; + req.role = role; + if (ioctl(ctl, bnepconnadd, &req)) + return -1; + strcpy(dev, req.device); + return 0; +} + +struct __service_16 { + uint16_t dst; + uint16_t src; +} __attribute__ ((packed)); + +struct __service_32 { + uint16_t unused1; + uint16_t dst; + uint16_t unused2; + uint16_t src; +} __attribute__ ((packed)); + +struct __service_128 { + uint16_t unused1; + uint16_t dst; + uint16_t unused2[8]; + uint16_t src; + uint16_t unused3[7]; +} __attribute__ ((packed)); + +int bnep_accept_connection(int sk, uint16_t role, char *dev) +{ + struct bnep_setup_conn_req *req; + struct bnep_control_rsp *rsp; + unsigned char pkt[BNEP_MTU]; + int r; + + r = recv(sk, pkt, BNEP_MTU, 0); + if (r <= 0) + return -1; + + errno = EPROTO; + + if (r < sizeof(*req)) + return -1; + + req = (void *) pkt; + if (req->type != BNEP_CONTROL || req->ctrl != BNEP_SETUP_CONN_REQ) + return -1; + + /* FIXME: Check role UUIDs */ + + rsp = (void *) pkt; + rsp->type = BNEP_CONTROL; + rsp->ctrl = BNEP_SETUP_CONN_RSP; + rsp->resp = htons(BNEP_SUCCESS); + if (send(sk, rsp, sizeof(*rsp), 0) < 0) + return -1; + + return bnep_connadd(sk, role, dev); +} + +/* Create BNEP connection + * sk - Connect L2CAP socket + * role - Local role + * service - Remote service + * dev - Network device (contains actual dev name on return) + */ +int bnep_create_connection(int sk, uint16_t role, uint16_t svc, char *dev) +{ + struct bnep_setup_conn_req *req; + struct bnep_control_rsp *rsp; + struct __service_16 *s; + unsigned char pkt[BNEP_MTU]; + int r; + + /* Send request */ + req = (void *) pkt; + req->type = BNEP_CONTROL; + req->ctrl = BNEP_SETUP_CONN_REQ; + req->uuid_size = 2; //16bit UUID + s = (void *) req->service; + s->dst = htons(svc); + s->src = htons(role); + + if (send(sk, pkt, sizeof(*req) + sizeof(*s), 0) < 0) + return -1; + +receive: + /* Get response */ + r = recv(sk, pkt, BNEP_MTU, 0); + if (r <= 0) + return -1; + + errno = EPROTO; + + if (r < sizeof(*rsp)) + return -1; + + rsp = (void *) pkt; + if (rsp->type != BNEP_CONTROL) + return -1; + + if (rsp->ctrl != BNEP_SETUP_CONN_RSP) + goto receive; + + r = ntohs(rsp->resp); + + switch (r) { + case BNEP_SUCCESS: + break; + + case BNEP_CONN_INVALID_DST: + case BNEP_CONN_INVALID_SRC: + case BNEP_CONN_INVALID_SVC: + errno = EPROTO; + return -1; + + case BNEP_CONN_NOT_ALLOWED: + errno = EACCES; + return -1; + } + + return bnep_connadd(sk, role, dev); +} diff --git a/pand/main.c b/pand/main.c new file mode 100644 index 00000000..91a9a92c --- /dev/null +++ b/pand/main.c @@ -0,0 +1,604 @@ +/* + pand - Bluetooth PAN daemon for BlueZ + Copyright (C) 2002 Maxim Krasnyansky + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include "pand.h" + +static uint16_t role = BNEP_SVC_PANU; // Local role (ie service) +static uint16_t service = BNEP_SVC_NAP; // Remote service + +static int detach = 1; +static int persist; +static int use_sdp = 1; +static int use_cache; +static int encrypt; +static int master; +static int search_duration = 10; + +static struct { + int valid; + char dst[40]; + bdaddr_t bdaddr; +} cache; + +static char netdev[16] = "bnep%d"; + +static bdaddr_t src_addr = *BDADDR_ANY; +static int src_dev = -1; + +volatile int terminate; + +enum { + NONE, + SHOW, + LISTEN, + CONNECT, + KILL +} modes; + +static void run_devup(char *dev, char *dst) +{ + char *argv[4], prog[40]; + + sprintf(prog, "%s/%s", PAND_CONFIG_DIR, PAND_DEVUP_CMD); + + if (access(prog, R_OK | X_OK)) + return; + + if (fork()) + return; + + argv[0] = prog; + argv[1] = dev; + argv[2] = dst; + argv[3] = NULL; + execv(prog, argv); + exit(1); +} + +static int do_listen(void) +{ + struct l2cap_options l2o; + struct sockaddr_l2 l2a; + int sk, olen, lm; + + if (use_sdp) + bnep_sdp_register(role); + + // Create L2CAP socket and bind it to PSM BNEP + sk = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + if (sk < 0) { + syslog(LOG_ERR, "Cannot create L2CAP socket. %s(%d)", + strerror(errno), errno); + return -1; + } + + l2a.l2_family = AF_BLUETOOTH; + l2a.l2_psm = htobs(BNEP_PSM); + l2a.l2_bdaddr = src_addr; + + if (bind(sk, (struct sockaddr *) &l2a, sizeof(l2a))) { + syslog(LOG_ERR, "Bind failed. %s(%d)", strerror(errno), errno); + return -1; + } + + /* Setup L2CAP options according to BNEP spec */ + if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &olen) < 0) { + syslog(LOG_ERR, "Failed to get L2CAP options. %s(%d)", + strerror(errno), errno); + return -1; + } + + l2o.imtu = l2o.omtu = BNEP_MTU; + if (setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)) < 0) { + syslog(LOG_ERR, "Failed to set L2CAP options. %s(%d)", + strerror(errno), errno); + return -1; + } + + /* Set link mode */ + lm = 0; + if (master) + lm |= L2CAP_LM_MASTER; + + if (encrypt) + lm |= L2CAP_LM_ENCRYPT; + + if (lm && setsockopt(sk, SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm)) < 0) { + syslog(LOG_ERR, "Failed to set link mode. %s(%d)", strerror(errno), errno); + return -1; + } + + listen(sk, 10); + + while (!terminate) { + int alen = sizeof(l2a); + int nsk; + nsk = accept(sk, (struct sockaddr *) &l2a, &alen); + if (nsk < 0) { + syslog(LOG_ERR, "Accept failed. %s(%d)", strerror(errno), errno); + continue; + } + + switch (fork()) { + case 0: + break; + case -1: + syslog(LOG_ERR, "Fork failed. %s(%d)", strerror(errno), errno); + default: + close(nsk); + continue; + } + + if (!bnep_accept_connection(nsk, role, netdev)) { + char str[40]; + ba2str(&l2a.l2_bdaddr, str); + + syslog(LOG_INFO, "New connection from %s %s", str, netdev); + + run_devup(netdev, str); + } else { + syslog(LOG_ERR, "Connection failed. %s(%d)", + strerror(errno), errno); + } + + close(nsk); + exit(0); + } + + if (use_sdp) + bnep_sdp_unregister(); + return 0; +} + +/* Wait for disconnect or error condition on the socket */ +static int w4_hup(int sk) +{ + struct pollfd pf; + int n; + + while (!terminate) { + pf.fd = sk; + pf.events = POLLERR | POLLHUP; + n = poll(&pf, 1, -1); + if (n < 0) { + if (errno == EINTR || errno == EAGAIN) + continue; + syslog(LOG_ERR, "Poll failed. %s(%d)", + strerror(errno), errno); + return 1; + } + + if (n) { + int err = 0, olen = sizeof(err); + getsockopt(sk, SOL_SOCKET, SO_ERROR, &err, &olen); + syslog(LOG_INFO, "%s disconnected%s%s", netdev, + err ? " : " : "", err ? strerror(err) : ""); + + close(sk); + return 0; + } + } + return 0; +} + +/* Connect and initiate BNEP session + * Returns: + * -1 - critical error (exit persist mode) + * 1 - non critical error + * 0 - success + */ +static int create_connection(char *dst, bdaddr_t *bdaddr) +{ + struct l2cap_options l2o; + struct sockaddr_l2 l2a; + int sk, olen, r = 0; + + syslog(LOG_INFO, "Connecting to %s", dst); + + sk = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + if (sk < 0) { + syslog(LOG_ERR, "Cannot create L2CAP socket. %s(%d)", + strerror(errno), errno); + return -1; + } + + /* Setup L2CAP options according to BNEP spec */ + getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &olen); + l2o.imtu = l2o.omtu = BNEP_MTU; + setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)); + + l2a.l2_family = AF_BLUETOOTH; + + /* Set local address */ + l2a.l2_psm = 0; + l2a.l2_bdaddr = src_addr; + + if (bind(sk, (struct sockaddr *) &l2a, sizeof(l2a))) + syslog(LOG_ERR, "Bind failed. %s(%d)", + strerror(errno), errno); + + l2a.l2_psm = htobs(BNEP_PSM); + l2a.l2_bdaddr = *bdaddr; + + if (!connect(sk, (struct sockaddr *) &l2a, sizeof(l2a)) && + !bnep_create_connection(sk, role, service, netdev)) { + + syslog(LOG_INFO, "%s connected", netdev); + + run_devup(netdev, dst); + + if (persist) + w4_hup(sk); + + r = 0; + } else { + syslog(LOG_ERR, "Connect to %s failed. %s(%d)", + dst, strerror(errno), errno); + r = 1; + } + + close(sk); + + if (use_cache) { + if (!r) { + /* Succesesful connection, validate cache */ + strcpy(cache.dst, dst); + bacpy(&cache.bdaddr, bdaddr); + cache.valid = use_cache; + } else + cache.valid--; + } + + return r; +} + +/* Search and connect + * Returns: + * -1 - critical error (exit persist mode) + * 1 - non critical error + * 0 - success + */ +static int do_connect(void) +{ + inquiry_info *ii; + int reconnect = 0; + int i, n, r = 0; + + do { + if (reconnect) + sleep(persist); + reconnect = 1; + + if (cache.valid > 0) { + /* Use cached bdaddr */ + r = create_connection(cache.dst, &cache.bdaddr); + if (r < 0) { + terminate = 1; + break; + } + continue; + } + + syslog(LOG_INFO, "Inquiring"); + + /* FIXME: Should we use non general LAP here ? */ + + ii = NULL; + n = hci_inquiry(src_dev, search_duration, 10, NULL, &ii, 0); + if (n < 0) { + syslog(LOG_ERR, "Inquiry failed. %s(%d)", strerror(errno), errno); + continue; + } + + for (i = 0; i < n; i++) { + char dst[40]; + ba2str(&ii[i].bdaddr, dst); + + if (use_sdp) { + syslog(LOG_INFO, "Searching for %s on %s", + bnep_svc2str(service), dst); + + if (bnep_sdp_search(&src_addr, &ii[i].bdaddr, service) <= 0) + continue; + } + + r = create_connection(dst, &ii[i].bdaddr); + if (r < 0) { + terminate = 1; + break; + } + } + free(ii); + } while (!terminate && persist); + + return r; +} + +static void do_show(void) +{ + bnep_show_connections(); +} + +static void do_kill(char *dst) +{ + if (dst) + bnep_kill_connection((void *) strtoba(dst)); + else + bnep_kill_all_connections(); +} + +void sig_hup(int sig) +{ + return; +} + +void sig_term(int sig) +{ + terminate = 1; +} + +static struct option main_lopts[] = { + { "help", 0, 0, 'h' }, + { "listen", 0, 0, 's' }, + { "connect", 1, 0, 'c' }, + { "search", 2, 0, 'Q' }, + { "kill", 1, 0, 'k' }, + { "killall", 0, 0, 'K' }, + { "role", 1, 0, 'r' }, + { "service", 1, 0, 'd' }, + { "device", 1, 0, 'i' }, + { "source", 1, 0, 'S' }, + { "nosdp", 0, 0, 'D' }, + { "list", 0, 0, 'l' }, + { "show", 0, 0, 'l' }, + { "nodetach", 0, 0, 'n' }, + { "persist", 2, 0, 'p' }, + { "encrypt", 0, 0, 'E' }, + { "master", 0, 0, 'M' }, + { "cache", 0, 0, 'C' }, + { 0, 0, 0, 0 } +}; + +static char main_sopts[] = "hsc:k:Kr:i:S:lnp::DQ::EMC::"; + +static char main_help[] = + "PAN daemon version " VERSION " \n" + "Usage:\n" + "\tpand \n" + "Options:\n" + "\t--show --list -l Show active PAN connections\n" + "\t--listen -s Listen for PAN connections\n" + "\t--connect -c Create PAN connection\n" + "\t--search -Q[duration] Search and connect\n" + "\t--kill -k Kill PAN connection\n" + "\t--killall -K Kill all PAN connections\n" + "\t--role -r Local PAN role (PANU, NAP, GN)\n" + "\t--service -d Remote PAN service (PANU, NAP, GN)\n" + "\t--device -i Network interface name\n" + "\t--source -S Source bdaddr\n" + "\t--nosdp -D Disable SDP\n" + "\t--encrypt -E Enable encryption\n" + "\t--master -M Become the master of a piconet\n" + "\t--nodetach -n Do not become a daemon\n" + "\t--persist -p[interval] Persist mode\n" + "\t--cache -C[valid] Cache addresses\n"; + +int main(int argc, char **argv) +{ + char *dst = NULL, *src = NULL; + struct sigaction sa; + int mode = NONE; + int opt; + + while ((opt=getopt_long(argc, argv, main_sopts, main_lopts, NULL)) != -1) { + switch(opt) { + case 'l': + mode = SHOW; + detach = 0; + break; + + case 's': + mode = LISTEN; + break; + + case 'c': + mode = CONNECT; + dst = strdup(optarg); + break; + + case 'Q': + mode = CONNECT; + dst = NULL; + if (optarg) + search_duration = atoi(optarg); + break; + + case 'k': + mode = KILL; + detach = 0; + dst = strdup(optarg); + break; + + case 'K': + mode = KILL; + detach = 0; + dst = NULL; + break; + + case 'S': + src = strdup(optarg); + break; + + case 'r': + bnep_str2svc(optarg, &role); + break; + + case 'd': + bnep_str2svc(optarg, &service); + break; + + case 'D': + use_sdp = 0; + break; + + case 'E': + encrypt = 1; + break; + + case 'M': + master = 1; + break; + + case 'i': + strcpy(netdev, optarg); + break; + + case 'n': + detach = 0; + break; + + case 'p': + if (optarg) + persist = atoi(optarg); + else + persist = 5; + break; + + case 'C': + if (optarg) + use_cache = atoi(optarg); + else + use_cache = 2; + break; + + case 'h': + default: + printf(main_help); + exit(0); + } + } + + argc -= optind; + argv += optind; + optind = 0; + + if (bnep_init()) + return -1; + + /* Check non daemon modes first */ + switch (mode) { + case SHOW: + do_show(); + return 0; + + case KILL: + do_kill(dst); + return 0; + + case NONE: + printf(main_help); + return 0; + } + + /* Initialize signals */ + memset(&sa, 0, sizeof(sa)); + sa.sa_flags = SA_NOCLDSTOP; + sa.sa_handler = SIG_IGN; + sigaction(SIGCHLD, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); + + sa.sa_handler = sig_hup; + sigaction(SIGHUP, &sa, NULL); + + sa.sa_handler = sig_term; + sigaction(SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + + if (detach) { + if (fork()) exit(0); + + /* Direct stdin,stdout,stderr to '/dev/null' */ + { + int fd = open("/dev/null", O_RDWR); + dup2(fd, 0); dup2(fd, 1); dup2(fd, 2); + close(fd); + } + + setsid(); + chdir("/"); + } + + openlog("pand", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON); + syslog(LOG_INFO, "PAN daemon ver %s", VERSION); + + if (src) { + src_dev = hci_devid(src); + if (src_dev < 0 || hci_devba(src_dev, &src_addr) < 0) { + syslog(LOG_ERR, "Invalid source. %s(%d)", strerror(errno), errno); + return -1; + } + } + + if (dst) { + /* Disable cache invalidation */ + use_cache = 0; + + strncpy(cache.dst, dst, sizeof(cache.dst) - 1); + str2ba(dst, &cache.bdaddr); + cache.valid = 1; + } + + switch (mode) { + case CONNECT: + do_connect(); + break; + + case LISTEN: + do_listen(); + break; + } + + return 0; +} diff --git a/pand/pand.1 b/pand/pand.1 new file mode 100644 index 00000000..a11a33c0 --- /dev/null +++ b/pand/pand.1 @@ -0,0 +1,59 @@ +.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.29. +.TH BlueZ "1" "February 2003" "PAN daemon" "User Commands" +.SH NAME +pand \- BlueZ Bluetooth PAN daemon +.SH DESCRIPTION +The pand PAN daemon allows your computer to connect to ethernet +networks using Bluetooth. +.SH SYNPOSIS +pand +.SH OPTIONS +.TP +\fB\-\-show\fR \fB\-\-list\fR \fB\-l\fR +Show active PAN connections +.TP +\fB\-\-listen\fR \fB\-s\fR +Listen for PAN connections +.TP +\fB\-\-connect\fR \fB\-c\fR +Create PAN connection +.TP +\fB\-\-search\fR \fB\-Q[duration]\fR +Search and connect +.TP +\fB\-\-kill\fR \fB\-k\fR +Kill PAN connection +.TP +\fB\-\-killall\fR \fB\-K\fR +Kill all PAN connections +.TP +\fB\-\-role\fR \fB\-r\fR +Local PAN role (PANU, NAP, GN) +.TP +\fB\-\-service\fR \fB\-d\fR +Remote PAN service (PANU, NAP, GN) +.TP +\fB\-\-device\fR \fB\-i\fR +Network interface name +.TP +\fB\-\-source\fR \fB\-S\fR +Source bdaddr +.TP +\fB\-\-nosdp\fR \fB\-D\fR +Disable SDP +.TP +\fB\-\-encrypt\fR \fB\-E\fR +Enable encryption +.TP +\fB\-\-master\fR \fB\-M\fR +Become the master of a piconet +.TP +\fB\-\-nodetach\fR \fB\-n\fR +Do not become a daemon +.TP +\fB\-\-persist\fR \fB\-p[interval]\fR +Persist mode +.TP +\fB\-\-cache\fR \fB\-C[valid]\fR +Cache addresses + diff --git a/pand/pand.h b/pand/pand.h new file mode 100644 index 00000000..268316de --- /dev/null +++ b/pand/pand.h @@ -0,0 +1,44 @@ +/* + pand - Bluetooth PAN daemon for BlueZ + Copyright (C) 2002 Maxim Krasnyansky + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* + * $Id$ + */ + +/* PAN scripts & commands */ +#define PAND_CONFIG_DIR "/etc/bluetooth/pan" +#define PAND_DEVUP_CMD "dev-up" + +/* BNEP functions */ +int bnep_init(void); +int bnep_cleanup(void); + +int bnep_str2svc(char *svc, uint16_t *uuid); +char *bnep_svc2str(uint16_t uuid); + +int bnep_show_connections(void); +int bnep_kill_connection(uint8_t *dst); +int bnep_kill_all_connections(void); + +int bnep_accept_connection(int sk, uint16_t role, char *dev); +int bnep_create_connection(int sk, uint16_t role, uint16_t svc, char *dev); + +/* SDP functions */ +int bnep_sdp_register(uint16_t role); +void bnep_sdp_unregister(void); +int bnep_sdp_search(bdaddr_t *src, bdaddr_t *dst, uint16_t service); diff --git a/pand/sdp.c b/pand/sdp.c new file mode 100644 index 00000000..16f092d9 --- /dev/null +++ b/pand/sdp.c @@ -0,0 +1,201 @@ +/* + pand - Bluetooth PAN daemon for BlueZ + Copyright (C) 2002 Maxim Krasnyansky + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "pand.h" + +static sdp_record_t *record; +static sdp_session_t *session; + +void bnep_sdp_unregister(void) +{ + if (record && sdp_record_unregister(session, record)) + syslog(LOG_ERR, "Service record unregistration failed."); + + sdp_close(session); +} + +int bnep_sdp_register(uint16_t role) +{ + sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto; + uuid_t root_uuid, pan, l2cap, bnep; + sdp_profile_desc_t profile[1]; + sdp_list_t *proto[2]; + uint16_t psm = 15, version = 0x0100; + int status; + + session = sdp_connect(BDADDR_ANY, BDADDR_LOCAL, 0); + if (!session) { + syslog(LOG_ERR, "Failed to connect to the local SDP server. %s(%d)", + strerror(errno), errno); + return -1; + } + + record = sdp_record_alloc(); + if (!record) { + syslog(LOG_ERR, "Failed to allocate service record %s(%d)", + strerror(errno), errno); + sdp_close(session); + return -1; + } + + sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP); + root = sdp_list_append(NULL, &root_uuid); + sdp_set_browse_groups(record, root); + + sdp_uuid16_create(&l2cap, L2CAP_UUID); + proto[0] = sdp_list_append(NULL, &l2cap); + proto[0] = sdp_list_append(proto[0], sdp_data_alloc(SDP_UINT16, &psm)); + apseq = sdp_list_append(NULL, proto[0]); + + sdp_uuid16_create(&bnep, BNEP_UUID); + proto[1] = sdp_list_append(NULL, &bnep); + proto[1] = sdp_list_append(proto[1], sdp_data_alloc(SDP_UINT16, &version)); + + /* Supported protocols */ + { + uint16_t ptype[4] = { + 0x0800, /* IPv4 */ + 0x0806, /* ARP */ + }; + sdp_data_t *head, *pseq; + int p; + + for (p = 0, head = NULL; p < 2; p++) { + sdp_data_t *data = sdp_data_alloc(SDP_UINT16, &ptype[p]); + if (head) + sdp_seq_append(head, data); + else + head = data; + } + pseq = sdp_data_alloc(SDP_SEQ16, head); + proto[1] = sdp_list_append(proto[1], pseq); + } + + apseq = sdp_list_append(apseq, proto[1]); + + aproto = sdp_list_append(NULL, apseq); + sdp_set_access_protos(record, aproto); + + switch (role) { + case BNEP_SVC_NAP: + sdp_uuid16_create(&pan, NAP_SVCLASS_ID); + svclass = sdp_list_append(NULL, &pan); + sdp_set_service_classes(record, svclass); + + sdp_uuid16_create(&profile[0].uuid, NAP_PROFILE_ID); + profile[0].version = 0x0100; + pfseq = sdp_list_append(NULL, &profile[0]); + sdp_set_profile_descs(record, pfseq); + + sdp_set_info_attr(record, "Network Access Point", NULL, NULL); + break; + + case BNEP_SVC_GN: + sdp_uuid16_create(&pan, GN_SVCLASS_ID); + svclass = sdp_list_append(NULL, &pan); + sdp_set_service_classes(record, svclass); + + sdp_uuid16_create(&profile[0].uuid, GN_PROFILE_ID); + profile[0].version = 0x0100; + pfseq = sdp_list_append(NULL, &profile[0]); + sdp_set_profile_descs(record, pfseq); + + sdp_set_info_attr(record, "Group Network Service", NULL, NULL); + break; + + case BNEP_SVC_PANU: + sdp_uuid16_create(&pan, PANU_SVCLASS_ID); + svclass = sdp_list_append(NULL, &pan); + sdp_set_service_classes(record, svclass); + + sdp_uuid16_create(&profile[0].uuid, PANU_PROFILE_ID); + profile[0].version = 0x0100; + pfseq = sdp_list_append(NULL, &profile[0]); + sdp_set_profile_descs(record, pfseq); + + sdp_set_info_attr(record, "PAN User", NULL, NULL); + break; + } + + status = sdp_record_register(session, record, 0); + if (status) { + syslog(LOG_ERR, "SDP registration failed."); + sdp_record_free(record); record = NULL; + sdp_close(session); + return -1; + } + return 0; +} + +// Search for PAN service. +// Returns 1 if service is found and 0 otherwise. +int bnep_sdp_search(bdaddr_t *src, bdaddr_t *dst, uint16_t service) +{ + sdp_list_t *srch, *rsp = NULL; + sdp_session_t *s; + uuid_t svclass; + int err; + + switch (service) { + case BNEP_SVC_PANU: + sdp_uuid16_create(&svclass, PANU_SVCLASS_ID); + break; + case BNEP_SVC_NAP: + sdp_uuid16_create(&svclass, NAP_SVCLASS_ID); + break; + case BNEP_SVC_GN: + sdp_uuid16_create(&svclass, GN_SVCLASS_ID); + break; + } + + srch = sdp_list_append(NULL, &svclass); + + s = sdp_connect(src, dst, 0); + if (!s) { + syslog(LOG_ERR, "Failed to connect to the SDP server. %s(%d)", + strerror(errno), errno); + return 0; + } + + err = sdp_service_search_req(s, srch, 1, &rsp); + sdp_close(s); + + /* Assume that search is successeful + * if at least one record is found */ + if (!err && sdp_list_len(rsp)) + return 1; + + return 0; +} -- cgit From 1c78dbe58cb701c7a0463b3bafadadb3d08461f1 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 28 Apr 2004 12:30:24 +0000 Subject: Unify copyright and license information --- pand/bnep.c | 55 ++++++++++++++++++++++++++++++--------------------- pand/main.c | 65 ++++++++++++++++++++++++++++++++++--------------------------- pand/pand.h | 45 ++++++++++++++++++++++++------------------ pand/sdp.c | 53 +++++++++++++++++++++++++++++-------------------- 4 files changed, 127 insertions(+), 91 deletions(-) (limited to 'pand') diff --git a/pand/bnep.c b/pand/bnep.c index 6833f941..be0d492a 100644 --- a/pand/bnep.c +++ b/pand/bnep.c @@ -1,25 +1,36 @@ /* - pand - Bluetooth PAN daemon for BlueZ - Copyright (C) 2002 Maxim Krasnyansky - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License, version 2, as - published by the Free Software Foundation. - - 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -/* - * $Id$ + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2002-2003 Maxim Krasnyansky + * Copyright (C) 2002-2004 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY + * CLAIM, OR ANY SPECIAL 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. + * + * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, + * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS + * SOFTWARE IS DISCLAIMED. + * + * + * $Id$ */ +#ifdef HAVE_CONFIG_H +#include +#endif + #include #include #include @@ -62,7 +73,7 @@ static struct { int bnep_str2svc(char *svc, uint16_t *uuid) { int i; - for (i=0; __svc[i].str; i++) + for (i = 0; __svc[i].str; i++) if (!strcasecmp(svc, __svc[i].str)) { *uuid = __svc[i].uuid; return 0; @@ -73,7 +84,7 @@ int bnep_str2svc(char *svc, uint16_t *uuid) char *bnep_svc2str(uint16_t uuid) { int i; - for (i=0; __svc[i].str; i++) + for (i = 0; __svc[i].str; i++) if (__svc[i].uuid == uuid) return __svc[i].str; return NULL; @@ -258,7 +269,7 @@ int bnep_create_connection(int sk, uint16_t role, uint16_t svc, char *dev) req = (void *) pkt; req->type = BNEP_CONTROL; req->ctrl = BNEP_SETUP_CONN_REQ; - req->uuid_size = 2; //16bit UUID + req->uuid_size = 2; /* 16bit UUID */ s = (void *) req->service; s->dst = htons(svc); s->src = htons(role); diff --git a/pand/main.c b/pand/main.c index 91a9a92c..88383331 100644 --- a/pand/main.c +++ b/pand/main.c @@ -1,23 +1,30 @@ /* - pand - Bluetooth PAN daemon for BlueZ - Copyright (C) 2002 Maxim Krasnyansky - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License, version 2, as - published by the Free Software Foundation. - - 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -/* - * $Id$ + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2002-2003 Maxim Krasnyansky + * Copyright (C) 2002-2004 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY + * CLAIM, OR ANY SPECIAL 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. + * + * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, + * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS + * SOFTWARE IS DISCLAIMED. + * + * + * $Id$ */ #ifdef HAVE_CONFIG_H @@ -45,8 +52,8 @@ #include "pand.h" -static uint16_t role = BNEP_SVC_PANU; // Local role (ie service) -static uint16_t service = BNEP_SVC_NAP; // Remote service +static uint16_t role = BNEP_SVC_PANU; /* Local role (ie service) */ +static uint16_t service = BNEP_SVC_NAP; /* Remote service */ static int detach = 1; static int persist; @@ -106,7 +113,7 @@ static int do_listen(void) if (use_sdp) bnep_sdp_register(role); - // Create L2CAP socket and bind it to PSM BNEP + /* Create L2CAP socket and bind it to PSM BNEP */ sk = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); if (sk < 0) { syslog(LOG_ERR, "Cannot create L2CAP socket. %s(%d)", @@ -290,7 +297,7 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) } else cache.valid--; } - + return r; } @@ -460,17 +467,17 @@ int main(int argc, char **argv) detach = 0; dst = strdup(optarg); break; - + case 'K': mode = KILL; detach = 0; dst = NULL; break; - + case 'S': src = strdup(optarg); break; - + case 'r': bnep_str2svc(optarg, &role); break; @@ -494,7 +501,7 @@ int main(int argc, char **argv) case 'i': strcpy(netdev, optarg); break; - + case 'n': detach = 0; break; @@ -536,7 +543,7 @@ int main(int argc, char **argv) case KILL: do_kill(dst); return 0; - + case NONE: printf(main_help); return 0; @@ -589,7 +596,7 @@ int main(int argc, char **argv) str2ba(dst, &cache.bdaddr); cache.valid = 1; } - + switch (mode) { case CONNECT: do_connect(); diff --git a/pand/pand.h b/pand/pand.h index 268316de..b0052676 100644 --- a/pand/pand.h +++ b/pand/pand.h @@ -1,23 +1,30 @@ /* - pand - Bluetooth PAN daemon for BlueZ - Copyright (C) 2002 Maxim Krasnyansky - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License, version 2, as - published by the Free Software Foundation. - - 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -/* - * $Id$ + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2002-2003 Maxim Krasnyansky + * Copyright (C) 2002-2004 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY + * CLAIM, OR ANY SPECIAL 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. + * + * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, + * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS + * SOFTWARE IS DISCLAIMED. + * + * + * $Id$ */ /* PAN scripts & commands */ diff --git a/pand/sdp.c b/pand/sdp.c index 16f092d9..dfa4c0c7 100644 --- a/pand/sdp.c +++ b/pand/sdp.c @@ -1,25 +1,36 @@ /* - pand - Bluetooth PAN daemon for BlueZ - Copyright (C) 2002 Maxim Krasnyansky - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License, version 2, as - published by the Free Software Foundation. - - 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - -/* - * $Id$ + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2002-2003 Maxim Krasnyansky + * Copyright (C) 2002-2004 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY + * CLAIM, OR ANY SPECIAL 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. + * + * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, + * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS + * SOFTWARE IS DISCLAIMED. + * + * + * $Id$ */ +#ifdef HAVE_CONFIG_H +#include +#endif + #include #include #include @@ -159,8 +170,8 @@ int bnep_sdp_register(uint16_t role) return 0; } -// Search for PAN service. -// Returns 1 if service is found and 0 otherwise. +/* Search for PAN service. + * Returns 1 if service is found and 0 otherwise. */ int bnep_sdp_search(bdaddr_t *src, bdaddr_t *dst, uint16_t service) { sdp_list_t *srch, *rsp = NULL; -- cgit From 7b767cdc03c44f2626eb80129fc0b78460f882fc Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 7 May 2004 23:11:23 +0000 Subject: Fix server information --- pand/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 88383331..1bc1d9e0 100644 --- a/pand/main.c +++ b/pand/main.c @@ -578,7 +578,7 @@ int main(int argc, char **argv) } openlog("pand", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON); - syslog(LOG_INFO, "PAN daemon ver %s", VERSION); + syslog(LOG_INFO, "Bluetooth PAN daemon"); if (src) { src_dev = hci_devid(src); -- cgit From 7ac6ede99ac309c7e28f3c2993fbc9bbbe28a68b Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 23 May 2004 10:39:50 +0000 Subject: Use LIBS instead of LDFLAGS --- pand/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pand') diff --git a/pand/Makefile.am b/pand/Makefile.am index 20b99270..0520dc3d 100644 --- a/pand/Makefile.am +++ b/pand/Makefile.am @@ -6,7 +6,7 @@ bin_PROGRAMS = pand pand_SOURCES = main.c pand.h bnep.c sdp.c -LDFLAGS = @BLUEZ_LIBS@ +LIBS = @BLUEZ_LIBS@ INCLUDES = @BLUEZ_INCLUDES@ -- cgit From 603fb9b1df0e95cbd5af23615f96f2aa16ba7407 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 31 May 2004 22:21:42 +0000 Subject: Add support for writing a pidfile and cleaning up the connection on exit --- pand/main.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- pand/pand.1 | 6 ++++ 2 files changed, 105 insertions(+), 5 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 1bc1d9e0..d4a29867 100644 --- a/pand/main.c +++ b/pand/main.c @@ -43,6 +43,8 @@ #include #include +#include +#include #include #include @@ -61,6 +63,7 @@ static int use_sdp = 1; static int use_cache; static int encrypt; static int master; +static int cleanup; static int search_duration = 10; static struct { @@ -70,12 +73,14 @@ static struct { } cache; static char netdev[16] = "bnep%d"; - +static char *pidfile = NULL; static bdaddr_t src_addr = *BDADDR_ANY; static int src_dev = -1; volatile int terminate; +static void do_kill(char *dst); + enum { NONE, SHOW, @@ -276,9 +281,15 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) run_devup(netdev, dst); - if (persist) + if (persist) { w4_hup(sk); + if (terminate && cleanup) { + syslog(LOG_INFO, "Disconnecting from %s.", dst); + do_kill(dst); + } + } + r = 0; } else { syslog(LOG_ERR, "Connect to %s failed. %s(%d)", @@ -386,6 +397,71 @@ void sig_term(int sig) terminate = 1; } +int write_pidfile(void) +{ + int fd; + FILE *f; + pid_t pid; + + do { + fd = open(pidfile, O_WRONLY|O_TRUNC|O_CREAT|O_EXCL, 0644); + if (fd == -1) { + /* Try to open the file for read. */ + fd = open(pidfile, O_RDONLY); + if(fd == -1) { + syslog(LOG_ERR, "Could not read old pidfile: %s(%d)", strerror(errno), errno); + return -1; + } + + /* We're already running; send a SIGHUP (we presume that they + * are calling ifup for a reason, so they probably want to + * rescan) and then exit cleanly and let things go on in the + * background. Muck with the filename so that we don't go + * deleting the pid file for the already-running instance. + */ + f = fdopen(fd, "r"); + if (!f) { + syslog(LOG_ERR, "Could not fdopen old pidfile: %s(%d)", strerror(errno), errno); + close(fd); + return -1; + } + + pid = 0; + fscanf(f, "%d", &pid); + fclose(f); + + if (pid) { + /* Try to kill it. */ + if (kill(pid, SIGHUP) == -1) { + /* No such pid; remove the bogus pid file. */ + syslog(LOG_INFO, "Removing stale pidfile"); + unlink(pidfile); + fd = -1; + } else { + /* Got it. Don't mess with the pid file on + * our way out. */ + syslog(LOG_INFO, "Signalling existing process %d and exiting\n", pid); + pidfile = NULL; + return -1; + } + } + } + } while(fd == -1); + + f = fdopen(fd, "w"); + if (!f) { + syslog(LOG_ERR, "Could not fdopen new pidfile: %s(%d)", strerror(errno), errno); + close(fd); + unlink(pidfile); + return -1; + } + fprintf(f, "%d\n", getpid()); + fclose(f); + return 0; +} + + + static struct option main_lopts[] = { { "help", 0, 0, 'h' }, { "listen", 0, 0, 's' }, @@ -405,10 +481,12 @@ static struct option main_lopts[] = { { "encrypt", 0, 0, 'E' }, { "master", 0, 0, 'M' }, { "cache", 0, 0, 'C' }, + { "pidfile", 1, 0, 'P' }, + { "autozap", 0, 0, 'z' }, { 0, 0, 0, 0 } }; -static char main_sopts[] = "hsc:k:Kr:i:S:lnp::DQ::EMC::"; +static char main_sopts[] = "hsc:k:Kr:i:S:lnp::DQ::EMC::P:z"; static char main_help[] = "PAN daemon version " VERSION " \n" @@ -418,6 +496,7 @@ static char main_help[] = "\t--show --list -l Show active PAN connections\n" "\t--listen -s Listen for PAN connections\n" "\t--connect -c Create PAN connection\n" + "\t--autozap -z Disconnect automatically on exit\n" "\t--search -Q[duration] Search and connect\n" "\t--kill -k Kill PAN connection\n" "\t--killall -K Kill all PAN connections\n" @@ -430,7 +509,8 @@ static char main_help[] = "\t--master -M Become the master of a piconet\n" "\t--nodetach -n Do not become a daemon\n" "\t--persist -p[interval] Persist mode\n" - "\t--cache -C[valid] Cache addresses\n"; + "\t--cache -C[valid] Cache addresses\n" + "\t--pidfile -P Create PID file\n"; int main(int argc, char **argv) { @@ -519,7 +599,15 @@ int main(int argc, char **argv) else use_cache = 2; break; - + + case 'P': + pidfile = strdup(optarg); + break; + + case 'z': + cleanup = 1; + break; + case 'h': default: printf(main_help); @@ -588,6 +676,9 @@ int main(int argc, char **argv) } } + if (pidfile && write_pidfile()) + return -1; + if (dst) { /* Disable cache invalidation */ use_cache = 0; @@ -607,5 +698,8 @@ int main(int argc, char **argv) break; } + if (pidfile) + unlink(pidfile); + return 0; } diff --git a/pand/pand.1 b/pand/pand.1 index a11a33c0..fbcc1ddd 100644 --- a/pand/pand.1 +++ b/pand/pand.1 @@ -56,4 +56,10 @@ Persist mode .TP \fB\-\-cache\fR \fB\-C[valid]\fR Cache addresses +.TP +\fB\-\-autozap\fR \fB\-z\fR +Disconnect automatically on exit +.TP +\fB\-\-pidfile\fR \fB\-P \fR +Create PID file -- cgit From 4b2d32e1fe19c4b84d925bc1d6bb2bd0e39cb156 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 28 Jun 2004 10:57:18 +0000 Subject: Set olen before calling getsockopt() --- pand/main.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index d4a29867..9dfa18a1 100644 --- a/pand/main.c +++ b/pand/main.c @@ -136,6 +136,7 @@ static int do_listen(void) } /* Setup L2CAP options according to BNEP spec */ + olen = sizeof(l2o); if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &olen) < 0) { syslog(LOG_ERR, "Failed to get L2CAP options. %s(%d)", strerror(errno), errno); @@ -257,6 +258,7 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) } /* Setup L2CAP options according to BNEP spec */ + olen = sizeof(l2o); getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &olen); l2o.imtu = l2o.omtu = BNEP_MTU; setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)); -- cgit From f21d9765a64fc97cf269727b7762fd1d277fedec Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 29 Jun 2004 08:53:29 +0000 Subject: Restore signals for dev-up script --- pand/main.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 9dfa18a1..4afc095f 100644 --- a/pand/main.c +++ b/pand/main.c @@ -89,9 +89,10 @@ enum { KILL } modes; -static void run_devup(char *dev, char *dst) +static void run_devup(char *dev, char *dst, int sk, int nsk) { char *argv[4], prog[40]; + struct sigaction sa; sprintf(prog, "%s/%s", PAND_CONFIG_DIR, PAND_DEVUP_CMD); @@ -101,6 +102,17 @@ static void run_devup(char *dev, char *dst) if (fork()) return; + if (sk >= 0) + close(sk); + + if (nsk >= 0) + close(nsk); + + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = SIG_DFL; + sigaction(SIGCHLD, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); + argv[0] = prog; argv[1] = dev; argv[2] = dst; @@ -190,7 +202,7 @@ static int do_listen(void) syslog(LOG_INFO, "New connection from %s %s", str, netdev); - run_devup(netdev, str); + run_devup(netdev, str, sk, nsk); } else { syslog(LOG_ERR, "Connection failed. %s(%d)", strerror(errno), errno); @@ -281,7 +293,7 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) syslog(LOG_INFO, "%s connected", netdev); - run_devup(netdev, dst); + run_devup(netdev, dst, sk, -1); if (persist) { w4_hup(sk); -- cgit From 43f017c9527dd4f64670831114f3c85f5defebbd Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 21 Jul 2004 15:05:27 +0000 Subject: Use AM_CFLAGS instead of INCLUDES --- pand/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pand') diff --git a/pand/Makefile.am b/pand/Makefile.am index 0520dc3d..c18c24e0 100644 --- a/pand/Makefile.am +++ b/pand/Makefile.am @@ -8,7 +8,7 @@ pand_SOURCES = main.c pand.h bnep.c sdp.c LIBS = @BLUEZ_LIBS@ -INCLUDES = @BLUEZ_INCLUDES@ +AM_CFLAGS = @BLUEZ_CFLAGS@ man_MANS = pand.1 -- cgit From 15c1cf1ca0d2fc45dff471d71339db49deecd1cd Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 21 Jul 2004 15:20:28 +0000 Subject: Make use of MAINTAINERCLEANFILES --- pand/Makefile.am | 2 ++ 1 file changed, 2 insertions(+) (limited to 'pand') diff --git a/pand/Makefile.am b/pand/Makefile.am index c18c24e0..3ed77891 100644 --- a/pand/Makefile.am +++ b/pand/Makefile.am @@ -13,3 +13,5 @@ AM_CFLAGS = @BLUEZ_CFLAGS@ man_MANS = pand.1 EXTRA_DIST = $(man_MANS) + +MAINTAINERCLEANFILES = Makefile.in -- cgit From 49c565df53bf26483c097b511547c203c81f4d41 Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Fri, 27 Aug 2004 14:37:54 +0000 Subject: fix memory leaks --- pand/main.c | 3 +-- pand/sdp.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 4afc095f..e57c80be 100644 --- a/pand/main.c +++ b/pand/main.c @@ -551,7 +551,6 @@ int main(int argc, char **argv) case 'Q': mode = CONNECT; - dst = NULL; if (optarg) search_duration = atoi(optarg); break; @@ -565,7 +564,6 @@ int main(int argc, char **argv) case 'K': mode = KILL; detach = 0; - dst = NULL; break; case 'S': @@ -700,6 +698,7 @@ int main(int argc, char **argv) strncpy(cache.dst, dst, sizeof(cache.dst) - 1); str2ba(dst, &cache.bdaddr); cache.valid = 1; + free(dst); } switch (mode) { diff --git a/pand/sdp.c b/pand/sdp.c index dfa4c0c7..686b25b5 100644 --- a/pand/sdp.c +++ b/pand/sdp.c @@ -63,6 +63,7 @@ int bnep_sdp_register(uint16_t role) uuid_t root_uuid, pan, l2cap, bnep; sdp_profile_desc_t profile[1]; sdp_list_t *proto[2]; + sdp_data_t *v, *p; uint16_t psm = 15, version = 0x0100; int status; @@ -84,15 +85,18 @@ int bnep_sdp_register(uint16_t role) sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP); root = sdp_list_append(NULL, &root_uuid); sdp_set_browse_groups(record, root); + sdp_list_free(root, 0); sdp_uuid16_create(&l2cap, L2CAP_UUID); proto[0] = sdp_list_append(NULL, &l2cap); - proto[0] = sdp_list_append(proto[0], sdp_data_alloc(SDP_UINT16, &psm)); + p = sdp_data_alloc(SDP_UINT16, &psm); + proto[0] = sdp_list_append(proto[0], p); apseq = sdp_list_append(NULL, proto[0]); sdp_uuid16_create(&bnep, BNEP_UUID); proto[1] = sdp_list_append(NULL, &bnep); - proto[1] = sdp_list_append(proto[1], sdp_data_alloc(SDP_UINT16, &version)); + v = sdp_data_alloc(SDP_UINT16, &version); + proto[1] = sdp_list_append(proto[1], v); /* Supported protocols */ { @@ -118,6 +122,12 @@ int bnep_sdp_register(uint16_t role) aproto = sdp_list_append(NULL, apseq); sdp_set_access_protos(record, aproto); + sdp_list_free(proto[0], NULL); + sdp_list_free(proto[1], NULL); + sdp_list_free(apseq, NULL); + sdp_list_free(aproto, NULL); + sdp_data_free(p); + sdp_data_free(v); switch (role) { case BNEP_SVC_NAP: @@ -150,11 +160,13 @@ int bnep_sdp_register(uint16_t role) sdp_uuid16_create(&pan, PANU_SVCLASS_ID); svclass = sdp_list_append(NULL, &pan); sdp_set_service_classes(record, svclass); + sdp_list_free(svclass, 0); sdp_uuid16_create(&profile[0].uuid, PANU_PROFILE_ID); profile[0].version = 0x0100; pfseq = sdp_list_append(NULL, &profile[0]); sdp_set_profile_descs(record, pfseq); + sdp_list_free(pfseq, 0); sdp_set_info_attr(record, "PAN User", NULL, NULL); break; -- cgit From 8e6ac465d6f3504f60d6e81e0c6f5dc6a653717c Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 25 Oct 2004 08:19:07 +0000 Subject: Display version number on start --- pand/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index e57c80be..9165bf92 100644 --- a/pand/main.c +++ b/pand/main.c @@ -503,7 +503,7 @@ static struct option main_lopts[] = { static char main_sopts[] = "hsc:k:Kr:i:S:lnp::DQ::EMC::P:z"; static char main_help[] = - "PAN daemon version " VERSION " \n" + "Bluetooth PAN daemon version " VERSION " \n" "Usage:\n" "\tpand \n" "Options:\n" @@ -678,7 +678,7 @@ int main(int argc, char **argv) } openlog("pand", LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON); - syslog(LOG_INFO, "Bluetooth PAN daemon"); + syslog(LOG_INFO, "Bluetooth PAN daemon version %s", VERSION); if (src) { src_dev = hci_devid(src); -- cgit From e3c755f8a89c8dab5f902d1c6f332c2c03348291 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 18 Nov 2004 08:17:50 +0000 Subject: Use --device for selecting the source device --- pand/main.c | 14 +++++++------- pand/pand.1 | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 9165bf92..725ee553 100644 --- a/pand/main.c +++ b/pand/main.c @@ -47,9 +47,9 @@ #include #include -#include #include #include +#include #include #include "pand.h" @@ -485,8 +485,8 @@ static struct option main_lopts[] = { { "killall", 0, 0, 'K' }, { "role", 1, 0, 'r' }, { "service", 1, 0, 'd' }, + { "ethernet", 1, 0, 'e' }, { "device", 1, 0, 'i' }, - { "source", 1, 0, 'S' }, { "nosdp", 0, 0, 'D' }, { "list", 0, 0, 'l' }, { "show", 0, 0, 'l' }, @@ -500,7 +500,7 @@ static struct option main_lopts[] = { { 0, 0, 0, 0 } }; -static char main_sopts[] = "hsc:k:Kr:i:S:lnp::DQ::EMC::P:z"; +static char main_sopts[] = "hsc:k:Kr:e:i:lnp::DQ::EMC::P:z"; static char main_help[] = "Bluetooth PAN daemon version " VERSION " \n" @@ -516,8 +516,8 @@ static char main_help[] = "\t--killall -K Kill all PAN connections\n" "\t--role -r Local PAN role (PANU, NAP, GN)\n" "\t--service -d Remote PAN service (PANU, NAP, GN)\n" - "\t--device -i Network interface name\n" - "\t--source -S Source bdaddr\n" + "\t--ethernet -e Network interface name\n" + "\t--device -i Source bdaddr\n" "\t--nosdp -D Disable SDP\n" "\t--encrypt -E Enable encryption\n" "\t--master -M Become the master of a piconet\n" @@ -566,7 +566,7 @@ int main(int argc, char **argv) detach = 0; break; - case 'S': + case 'i': src = strdup(optarg); break; @@ -590,7 +590,7 @@ int main(int argc, char **argv) master = 1; break; - case 'i': + case 'e': strcpy(netdev, optarg); break; diff --git a/pand/pand.1 b/pand/pand.1 index fbcc1ddd..c6c32dfc 100644 --- a/pand/pand.1 +++ b/pand/pand.1 @@ -33,10 +33,10 @@ Local PAN role (PANU, NAP, GN) \fB\-\-service\fR \fB\-d\fR Remote PAN service (PANU, NAP, GN) .TP -\fB\-\-device\fR \fB\-i\fR +\fB\-\-ethernet\fR \fB\-e\fR Network interface name .TP -\fB\-\-source\fR \fB\-S\fR +\fB\-\-device\fR \fB\-i\fR Source bdaddr .TP \fB\-\-nosdp\fR \fB\-D\fR -- cgit From 6f6c722d1fc37d94f45c438f1b611c2d885e62f3 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 18 Nov 2004 08:23:23 +0000 Subject: Add support for secure mode --- pand/main.c | 12 ++++++++++-- pand/pand.1 | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 725ee553..e7fcb875 100644 --- a/pand/main.c +++ b/pand/main.c @@ -62,6 +62,7 @@ static int persist; static int use_sdp = 1; static int use_cache; static int encrypt; +static int secure; static int master; static int cleanup; static int search_duration = 10; @@ -166,9 +167,10 @@ static int do_listen(void) lm = 0; if (master) lm |= L2CAP_LM_MASTER; - if (encrypt) lm |= L2CAP_LM_ENCRYPT; + if (secure) + lm |= L2CAP_LM_SECURE; if (lm && setsockopt(sk, SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm)) < 0) { syslog(LOG_ERR, "Failed to set link mode. %s(%d)", strerror(errno), errno); @@ -493,6 +495,7 @@ static struct option main_lopts[] = { { "nodetach", 0, 0, 'n' }, { "persist", 2, 0, 'p' }, { "encrypt", 0, 0, 'E' }, + { "secure", 0, 0, 'S' }, { "master", 0, 0, 'M' }, { "cache", 0, 0, 'C' }, { "pidfile", 1, 0, 'P' }, @@ -500,7 +503,7 @@ static struct option main_lopts[] = { { 0, 0, 0, 0 } }; -static char main_sopts[] = "hsc:k:Kr:e:i:lnp::DQ::EMC::P:z"; +static char main_sopts[] = "hsc:k:Kr:e:i:lnp::DQ::ESMC::P:z"; static char main_help[] = "Bluetooth PAN daemon version " VERSION " \n" @@ -520,6 +523,7 @@ static char main_help[] = "\t--device -i Source bdaddr\n" "\t--nosdp -D Disable SDP\n" "\t--encrypt -E Enable encryption\n" + "\t--secure -S Secure connection\n" "\t--master -M Become the master of a piconet\n" "\t--nodetach -n Do not become a daemon\n" "\t--persist -p[interval] Persist mode\n" @@ -586,6 +590,10 @@ int main(int argc, char **argv) encrypt = 1; break; + case 'S': + secure = 1; + break; + case 'M': master = 1; break; diff --git a/pand/pand.1 b/pand/pand.1 index c6c32dfc..4e18ad4a 100644 --- a/pand/pand.1 +++ b/pand/pand.1 @@ -45,6 +45,9 @@ Disable SDP \fB\-\-encrypt\fR \fB\-E\fR Enable encryption .TP +\fB\-\-secure\fR \fB\-S\fR +Secure connection +.TP \fB\-\-master\fR \fB\-M\fR Become the master of a piconet .TP -- cgit From bbda499067067aefc8e642a2784d247ac0331eae Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 25 Dec 2004 17:43:16 +0000 Subject: Add memset() to different places to initialize the structures --- pand/main.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index e7fcb875..2090f9f8 100644 --- a/pand/main.c +++ b/pand/main.c @@ -139,9 +139,10 @@ static int do_listen(void) return -1; } + memset(&l2a, 0, sizeof(l2a)); l2a.l2_family = AF_BLUETOOTH; - l2a.l2_psm = htobs(BNEP_PSM); - l2a.l2_bdaddr = src_addr; + bacpy(&l2a.l2_bdaddr, &src_addr); + l2a.l2_psm = htobs(BNEP_PSM); if (bind(sk, (struct sockaddr *) &l2a, sizeof(l2a))) { syslog(LOG_ERR, "Bind failed. %s(%d)", strerror(errno), errno); @@ -149,6 +150,7 @@ static int do_listen(void) } /* Setup L2CAP options according to BNEP spec */ + memset(&l2o, 0, sizeof(l2o)); olen = sizeof(l2o); if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &olen) < 0) { syslog(LOG_ERR, "Failed to get L2CAP options. %s(%d)", @@ -272,23 +274,24 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) } /* Setup L2CAP options according to BNEP spec */ + memset(&l2o, 0, sizeof(l2o)); olen = sizeof(l2o); getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &olen); l2o.imtu = l2o.omtu = BNEP_MTU; setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)); + memset(&l2a, 0, sizeof(l2a)); l2a.l2_family = AF_BLUETOOTH; - - /* Set local address */ - l2a.l2_psm = 0; - l2a.l2_bdaddr = src_addr; + bacpy(&l2a.l2_bdaddr, &src_addr); if (bind(sk, (struct sockaddr *) &l2a, sizeof(l2a))) syslog(LOG_ERR, "Bind failed. %s(%d)", strerror(errno), errno); + memset(&l2a, 0, sizeof(l2a)); + l2a.l2_family = AF_BLUETOOTH; + bacpy(&l2a.l2_bdaddr, bdaddr); l2a.l2_psm = htobs(BNEP_PSM); - l2a.l2_bdaddr = *bdaddr; if (!connect(sk, (struct sockaddr *) &l2a, sizeof(l2a)) && !bnep_create_connection(sk, role, service, netdev)) { -- cgit From fa8b3a2161c997be308438040641dc0445f9d494 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 22 Feb 2005 15:21:59 +0000 Subject: Always include the stdio.h header file --- pand/sdp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'pand') diff --git a/pand/sdp.c b/pand/sdp.c index 686b25b5..85edc681 100644 --- a/pand/sdp.c +++ b/pand/sdp.c @@ -31,11 +31,12 @@ #include #endif +#include +#include #include #include #include #include -#include #include #include -- cgit From f6a34bc5fd6f54256080d63734b414c037f0603c Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Mon, 18 Apr 2005 22:27:22 +0000 Subject: Use unlimited inquiry responses --- pand/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 2090f9f8..959d6057 100644 --- a/pand/main.c +++ b/pand/main.c @@ -363,7 +363,7 @@ static int do_connect(void) /* FIXME: Should we use non general LAP here ? */ ii = NULL; - n = hci_inquiry(src_dev, search_duration, 10, NULL, &ii, 0); + n = hci_inquiry(src_dev, search_duration, 0, NULL, &ii, 0); if (n < 0) { syslog(LOG_ERR, "Inquiry failed. %s(%d)", strerror(errno), errno); continue; -- cgit From adbb22a47c9356e0c191a75daec0439e8d941bd1 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 21 Apr 2005 21:34:08 +0000 Subject: Use LDADD instead of LIBS --- pand/Makefile.am | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'pand') diff --git a/pand/Makefile.am b/pand/Makefile.am index 3ed77891..9e7c3648 100644 --- a/pand/Makefile.am +++ b/pand/Makefile.am @@ -5,8 +5,7 @@ bin_PROGRAMS = pand pand_SOURCES = main.c pand.h bnep.c sdp.c - -LIBS = @BLUEZ_LIBS@ +pand_LDADD = @BLUEZ_LIBS@ AM_CFLAGS = @BLUEZ_CFLAGS@ -- cgit From 258887a0f6a798cc021837c3f686d02ff317d271 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 7 May 2005 20:40:01 +0000 Subject: Add authentication support --- pand/main.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 959d6057..c128d332 100644 --- a/pand/main.c +++ b/pand/main.c @@ -61,6 +61,7 @@ static int detach = 1; static int persist; static int use_sdp = 1; static int use_cache; +static int auth; static int encrypt; static int secure; static int master; @@ -169,6 +170,8 @@ static int do_listen(void) lm = 0; if (master) lm |= L2CAP_LM_MASTER; + if (auth) + lm |= L2CAP_LM_AUTH; if (encrypt) lm |= L2CAP_LM_ENCRYPT; if (secure) @@ -497,6 +500,7 @@ static struct option main_lopts[] = { { "show", 0, 0, 'l' }, { "nodetach", 0, 0, 'n' }, { "persist", 2, 0, 'p' }, + { "auth", 0, 0, 'A' }, { "encrypt", 0, 0, 'E' }, { "secure", 0, 0, 'S' }, { "master", 0, 0, 'M' }, @@ -506,7 +510,7 @@ static struct option main_lopts[] = { { 0, 0, 0, 0 } }; -static char main_sopts[] = "hsc:k:Kr:e:i:lnp::DQ::ESMC::P:z"; +static char main_sopts[] = "hsc:k:Kr:e:i:lnp::DQ::AESMC::P:z"; static char main_help[] = "Bluetooth PAN daemon version " VERSION " \n" @@ -525,6 +529,7 @@ static char main_help[] = "\t--ethernet -e Network interface name\n" "\t--device -i Source bdaddr\n" "\t--nosdp -D Disable SDP\n" + "\t--auth -A Enable authentication\n" "\t--encrypt -E Enable encryption\n" "\t--secure -S Secure connection\n" "\t--master -M Become the master of a piconet\n" @@ -589,6 +594,10 @@ int main(int argc, char **argv) use_sdp = 0; break; + case 'A': + auth = 1; + break; + case 'E': encrypt = 1; break; -- cgit From 9ea3fe29624242544794b25154dc1b246e5e10cf Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 9 Jun 2005 01:47:57 +0000 Subject: Support -d parameter --- pand/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index c128d332..98b1720d 100644 --- a/pand/main.c +++ b/pand/main.c @@ -510,7 +510,7 @@ static struct option main_lopts[] = { { 0, 0, 0, 0 } }; -static char main_sopts[] = "hsc:k:Kr:e:i:lnp::DQ::AESMC::P:z"; +static char main_sopts[] = "hsc:k:Kr:d:e:i:lnp::DQ::AESMC::P:z"; static char main_help[] = "Bluetooth PAN daemon version " VERSION " \n" -- cgit From 1f422e5f2b343d35a8c77ce4be16f74b2819b2bf Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 5 Jul 2005 21:15:41 +0000 Subject: Fix some GCC 4.0 warnings --- pand/main.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 98b1720d..20fbec19 100644 --- a/pand/main.c +++ b/pand/main.c @@ -127,7 +127,8 @@ static int do_listen(void) { struct l2cap_options l2o; struct sockaddr_l2 l2a; - int sk, olen, lm; + socklen_t olen; + int sk, lm; if (use_sdp) bnep_sdp_register(role); @@ -185,7 +186,7 @@ static int do_listen(void) listen(sk, 10); while (!terminate) { - int alen = sizeof(l2a); + socklen_t alen = sizeof(l2a); int nsk; nsk = accept(sk, (struct sockaddr *) &l2a, &alen); if (nsk < 0) { @@ -243,7 +244,8 @@ static int w4_hup(int sk) } if (n) { - int err = 0, olen = sizeof(err); + int err = 0; + socklen_t olen = sizeof(err); getsockopt(sk, SOL_SOCKET, SO_ERROR, &err, &olen); syslog(LOG_INFO, "%s disconnected%s%s", netdev, err ? " : " : "", err ? strerror(err) : ""); @@ -265,7 +267,8 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) { struct l2cap_options l2o; struct sockaddr_l2 l2a; - int sk, olen, r = 0; + socklen_t olen; + int sk, r = 0; syslog(LOG_INFO, "Connecting to %s", dst); -- cgit From 7ba9b7fda144865222a5d254b36bf484d5af99e7 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 6 Aug 2005 06:42:20 +0000 Subject: Use bt_free() instead of free() for inquiry results --- pand/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 20fbec19..72e607a9 100644 --- a/pand/main.c +++ b/pand/main.c @@ -393,7 +393,7 @@ static int do_connect(void) break; } } - free(ii); + bt_free(ii); } while (!terminate && persist); return r; -- cgit From 76823d4777869743af04038d82705cd7249657b8 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 7 Aug 2005 06:22:38 +0000 Subject: Use device specific service record register function --- pand/main.c | 2 +- pand/pand.h | 2 +- pand/sdp.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 72e607a9..b5a4ee95 100644 --- a/pand/main.c +++ b/pand/main.c @@ -131,7 +131,7 @@ static int do_listen(void) int sk, lm; if (use_sdp) - bnep_sdp_register(role); + bnep_sdp_register(&src_addr, role); /* Create L2CAP socket and bind it to PSM BNEP */ sk = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); diff --git a/pand/pand.h b/pand/pand.h index b0052676..2981168b 100644 --- a/pand/pand.h +++ b/pand/pand.h @@ -46,6 +46,6 @@ int bnep_accept_connection(int sk, uint16_t role, char *dev); int bnep_create_connection(int sk, uint16_t role, uint16_t svc, char *dev); /* SDP functions */ -int bnep_sdp_register(uint16_t role); +int bnep_sdp_register(bdaddr_t *device, uint16_t role); void bnep_sdp_unregister(void); int bnep_sdp_search(bdaddr_t *src, bdaddr_t *dst, uint16_t service); diff --git a/pand/sdp.c b/pand/sdp.c index 85edc681..802887b1 100644 --- a/pand/sdp.c +++ b/pand/sdp.c @@ -58,7 +58,7 @@ void bnep_sdp_unregister(void) sdp_close(session); } -int bnep_sdp_register(uint16_t role) +int bnep_sdp_register(bdaddr_t *device, uint16_t role) { sdp_list_t *svclass, *pfseq, *apseq, *root, *aproto; uuid_t root_uuid, pan, l2cap, bnep; @@ -173,7 +173,7 @@ int bnep_sdp_register(uint16_t role) break; } - status = sdp_record_register(session, record, 0); + status = sdp_device_record_register(session, device, record, 0); if (status) { syslog(LOG_ERR, "SDP registration failed."); sdp_record_free(record); record = NULL; -- cgit From 632a9432774ff3a0c6e556e8f32a565b38890767 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 29 Oct 2005 22:36:31 +0000 Subject: Big cleanup of CVS relics --- pand/Makefile.am | 3 --- pand/bnep.c | 36 +++++++++++++++--------------------- pand/main.c | 38 ++++++++++++++++---------------------- pand/pand.h | 27 +++++++++++---------------- pand/sdp.c | 29 ++++++++++++----------------- 5 files changed, 54 insertions(+), 79 deletions(-) (limited to 'pand') diff --git a/pand/Makefile.am b/pand/Makefile.am index 9e7c3648..3d92aa85 100644 --- a/pand/Makefile.am +++ b/pand/Makefile.am @@ -1,6 +1,3 @@ -# -# $Id$ -# bin_PROGRAMS = pand diff --git a/pand/bnep.c b/pand/bnep.c index be0d492a..6c1d205b 100644 --- a/pand/bnep.c +++ b/pand/bnep.c @@ -3,48 +3,42 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2004 Marcel Holtmann + * Copyright (C) 2002-2005 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; + * 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. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY - * CLAIM, OR ANY SPECIAL 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. + * 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. * - * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, - * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS - * SOFTWARE IS DISCLAIMED. + * 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 * - * - * $Id$ */ #ifdef HAVE_CONFIG_H #include #endif -#include -#include #include #include - +#include +#include #include #include -#include - #include #include #include +#include + #include "pand.h" static int ctl; diff --git a/pand/main.c b/pand/main.c index b5a4ee95..576472b7 100644 --- a/pand/main.c +++ b/pand/main.c @@ -3,28 +3,23 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2004 Marcel Holtmann + * Copyright (C) 2002-2005 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; + * 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. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY - * CLAIM, OR ANY SPECIAL 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. + * 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. * - * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, - * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS - * SOFTWARE IS DISCLAIMED. + * 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 * - * - * $Id$ */ #ifdef HAVE_CONFIG_H @@ -32,19 +27,18 @@ #endif #include +#include +#include +#include #include #include -#include -#include #include #include -#include #include - -#include #include -#include #include +#include +#include #include #include diff --git a/pand/pand.h b/pand/pand.h index 2981168b..1e4509f9 100644 --- a/pand/pand.h +++ b/pand/pand.h @@ -3,28 +3,23 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2004 Marcel Holtmann + * Copyright (C) 2002-2005 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; + * 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. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY - * CLAIM, OR ANY SPECIAL 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. + * 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. * - * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, - * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS - * SOFTWARE IS DISCLAIMED. + * 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 * - * - * $Id$ */ /* PAN scripts & commands */ diff --git a/pand/sdp.c b/pand/sdp.c index 802887b1..0edaf8f3 100644 --- a/pand/sdp.c +++ b/pand/sdp.c @@ -3,28 +3,23 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2004 Marcel Holtmann + * Copyright (C) 2002-2005 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation; + * 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. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY - * CLAIM, OR ANY SPECIAL 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. + * 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. * - * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, - * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS - * SOFTWARE IS DISCLAIMED. + * 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 * - * - * $Id$ */ #ifdef HAVE_CONFIG_H @@ -33,9 +28,9 @@ #include #include +#include #include #include -#include #include #include #include -- cgit From f2e48c44a7e4c9ee31b8ce2e302186f6047cfeab Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 3 Jan 2006 13:28:56 +0000 Subject: Update copyright information --- pand/bnep.c | 2 +- pand/main.c | 2 +- pand/pand.h | 2 +- pand/sdp.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'pand') diff --git a/pand/bnep.c b/pand/bnep.c index 6c1d205b..3e7283f3 100644 --- a/pand/bnep.c +++ b/pand/bnep.c @@ -3,7 +3,7 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2005 Marcel Holtmann + * Copyright (C) 2002-2006 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify diff --git a/pand/main.c b/pand/main.c index 576472b7..485a120d 100644 --- a/pand/main.c +++ b/pand/main.c @@ -3,7 +3,7 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2005 Marcel Holtmann + * Copyright (C) 2002-2006 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify diff --git a/pand/pand.h b/pand/pand.h index 1e4509f9..d4a39fa4 100644 --- a/pand/pand.h +++ b/pand/pand.h @@ -3,7 +3,7 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2005 Marcel Holtmann + * Copyright (C) 2002-2006 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify diff --git a/pand/sdp.c b/pand/sdp.c index 0edaf8f3..1b1343ae 100644 --- a/pand/sdp.c +++ b/pand/sdp.c @@ -3,7 +3,7 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2005 Marcel Holtmann + * Copyright (C) 2002-2006 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify -- cgit From c0ac4b3ddff4543901155eb2d7c28952a694f686 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Fri, 20 Jan 2006 01:42:21 +0000 Subject: Include a devup script option --- pand/main.c | 118 +++++++++++++++++++++++++++++++++++------------------------- pand/pand.1 | 10 +++--- pand/pand.h | 4 --- 3 files changed, 76 insertions(+), 56 deletions(-) (limited to 'pand') diff --git a/pand/main.c b/pand/main.c index 485a120d..c01bb6b6 100644 --- a/pand/main.c +++ b/pand/main.c @@ -48,19 +48,19 @@ #include "pand.h" -static uint16_t role = BNEP_SVC_PANU; /* Local role (ie service) */ -static uint16_t service = BNEP_SVC_NAP; /* Remote service */ - -static int detach = 1; -static int persist; -static int use_sdp = 1; -static int use_cache; -static int auth; -static int encrypt; -static int secure; -static int master; -static int cleanup; -static int search_duration = 10; +static uint16_t role = BNEP_SVC_PANU; /* Local role (ie service) */ +static uint16_t service = BNEP_SVC_NAP; /* Remote service */ + +static int detach = 1; +static int persist; +static int use_sdp = 1; +static int use_cache; +static int auth; +static int encrypt; +static int secure; +static int master; +static int cleanup; +static int search_duration = 10; static struct { int valid; @@ -70,10 +70,12 @@ static struct { static char netdev[16] = "bnep%d"; static char *pidfile = NULL; +static char *devupcmd = NULL; + static bdaddr_t src_addr = *BDADDR_ANY; static int src_dev = -1; -volatile int terminate; +static volatile int terminate; static void do_kill(char *dst); @@ -87,12 +89,13 @@ enum { static void run_devup(char *dev, char *dst, int sk, int nsk) { - char *argv[4], prog[40]; + char *argv[4]; struct sigaction sa; - sprintf(prog, "%s/%s", PAND_CONFIG_DIR, PAND_DEVUP_CMD); + if (!devupcmd) + return; - if (access(prog, R_OK | X_OK)) + if (access(devupcmd, R_OK | X_OK)) return; if (fork()) @@ -103,17 +106,19 @@ static void run_devup(char *dev, char *dst, int sk, int nsk) if (nsk >= 0) close(nsk); - + memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_DFL; sigaction(SIGCHLD, &sa, NULL); sigaction(SIGPIPE, &sa, NULL); - argv[0] = prog; + argv[0] = devupcmd; argv[1] = dev; argv[2] = dst; argv[3] = NULL; - execv(prog, argv); + + execv(devupcmd, argv); + exit(1); } @@ -131,7 +136,7 @@ static int do_listen(void) sk = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); if (sk < 0) { syslog(LOG_ERR, "Cannot create L2CAP socket. %s(%d)", - strerror(errno), errno); + strerror(errno), errno); return -1; } @@ -141,7 +146,8 @@ static int do_listen(void) l2a.l2_psm = htobs(BNEP_PSM); if (bind(sk, (struct sockaddr *) &l2a, sizeof(l2a))) { - syslog(LOG_ERR, "Bind failed. %s(%d)", strerror(errno), errno); + syslog(LOG_ERR, "Bind failed. %s(%d)", + strerror(errno), errno); return -1; } @@ -150,14 +156,14 @@ static int do_listen(void) olen = sizeof(l2o); if (getsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &olen) < 0) { syslog(LOG_ERR, "Failed to get L2CAP options. %s(%d)", - strerror(errno), errno); + strerror(errno), errno); return -1; } l2o.imtu = l2o.omtu = BNEP_MTU; if (setsockopt(sk, SOL_L2CAP, L2CAP_OPTIONS, &l2o, sizeof(l2o)) < 0) { syslog(LOG_ERR, "Failed to set L2CAP options. %s(%d)", - strerror(errno), errno); + strerror(errno), errno); return -1; } @@ -173,7 +179,8 @@ static int do_listen(void) lm |= L2CAP_LM_SECURE; if (lm && setsockopt(sk, SOL_L2CAP, L2CAP_LM, &lm, sizeof(lm)) < 0) { - syslog(LOG_ERR, "Failed to set link mode. %s(%d)", strerror(errno), errno); + syslog(LOG_ERR, "Failed to set link mode. %s(%d)", + strerror(errno), errno); return -1; } @@ -184,7 +191,8 @@ static int do_listen(void) int nsk; nsk = accept(sk, (struct sockaddr *) &l2a, &alen); if (nsk < 0) { - syslog(LOG_ERR, "Accept failed. %s(%d)", strerror(errno), errno); + syslog(LOG_ERR, "Accept failed. %s(%d)", + strerror(errno), errno); continue; } @@ -192,7 +200,8 @@ static int do_listen(void) case 0: break; case -1: - syslog(LOG_ERR, "Fork failed. %s(%d)", strerror(errno), errno); + syslog(LOG_ERR, "Fork failed. %s(%d)", + strerror(errno), errno); default: close(nsk); continue; @@ -202,12 +211,13 @@ static int do_listen(void) char str[40]; ba2str(&l2a.l2_bdaddr, str); - syslog(LOG_INFO, "New connection from %s %s", str, netdev); + syslog(LOG_INFO, "New connection from %s %s", + str, netdev); run_devup(netdev, str, sk, nsk); } else { syslog(LOG_ERR, "Connection failed. %s(%d)", - strerror(errno), errno); + strerror(errno), errno); } close(nsk); @@ -233,7 +243,7 @@ static int w4_hup(int sk) if (errno == EINTR || errno == EAGAIN) continue; syslog(LOG_ERR, "Poll failed. %s(%d)", - strerror(errno), errno); + strerror(errno), errno); return 1; } @@ -269,7 +279,7 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) sk = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); if (sk < 0) { syslog(LOG_ERR, "Cannot create L2CAP socket. %s(%d)", - strerror(errno), errno); + strerror(errno), errno); return -1; } @@ -285,8 +295,8 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) bacpy(&l2a.l2_bdaddr, &src_addr); if (bind(sk, (struct sockaddr *) &l2a, sizeof(l2a))) - syslog(LOG_ERR, "Bind failed. %s(%d)", - strerror(errno), errno); + syslog(LOG_ERR, "Bind failed. %s(%d)", + strerror(errno), errno); memset(&l2a, 0, sizeof(l2a)); l2a.l2_family = AF_BLUETOOTH; @@ -312,7 +322,7 @@ static int create_connection(char *dst, bdaddr_t *bdaddr) r = 0; } else { syslog(LOG_ERR, "Connect to %s failed. %s(%d)", - dst, strerror(errno), errno); + dst, strerror(errno), errno); r = 1; } @@ -365,7 +375,8 @@ static int do_connect(void) ii = NULL; n = hci_inquiry(src_dev, search_duration, 0, NULL, &ii, 0); if (n < 0) { - syslog(LOG_ERR, "Inquiry failed. %s(%d)", strerror(errno), errno); + syslog(LOG_ERR, "Inquiry failed. %s(%d)", + strerror(errno), errno); continue; } @@ -406,29 +417,30 @@ static void do_kill(char *dst) bnep_kill_all_connections(); } -void sig_hup(int sig) +static void sig_hup(int sig) { return; } -void sig_term(int sig) +static void sig_term(int sig) { terminate = 1; } -int write_pidfile(void) +static int write_pidfile(void) { int fd; FILE *f; pid_t pid; - do { + do { fd = open(pidfile, O_WRONLY|O_TRUNC|O_CREAT|O_EXCL, 0644); if (fd == -1) { /* Try to open the file for read. */ fd = open(pidfile, O_RDONLY); if(fd == -1) { - syslog(LOG_ERR, "Could not read old pidfile: %s(%d)", strerror(errno), errno); + syslog(LOG_ERR, "Could not read old pidfile: %s(%d)", + strerror(errno), errno); return -1; } @@ -440,7 +452,8 @@ int write_pidfile(void) */ f = fdopen(fd, "r"); if (!f) { - syslog(LOG_ERR, "Could not fdopen old pidfile: %s(%d)", strerror(errno), errno); + syslog(LOG_ERR, "Could not fdopen old pidfile: %s(%d)", + strerror(errno), errno); close(fd); return -1; } @@ -469,17 +482,18 @@ int write_pidfile(void) f = fdopen(fd, "w"); if (!f) { - syslog(LOG_ERR, "Could not fdopen new pidfile: %s(%d)", strerror(errno), errno); + syslog(LOG_ERR, "Could not fdopen new pidfile: %s(%d)", + strerror(errno), errno); close(fd); unlink(pidfile); return -1; } + fprintf(f, "%d\n", getpid()); fclose(f); + return 0; } - - static struct option main_lopts[] = { { "help", 0, 0, 'h' }, @@ -503,11 +517,12 @@ static struct option main_lopts[] = { { "master", 0, 0, 'M' }, { "cache", 0, 0, 'C' }, { "pidfile", 1, 0, 'P' }, + { "devup", 1, 0, 'u' }, { "autozap", 0, 0, 'z' }, { 0, 0, 0, 0 } }; -static char main_sopts[] = "hsc:k:Kr:d:e:i:lnp::DQ::AESMC::P:z"; +static char main_sopts[] = "hsc:k:Kr:d:e:i:lnp::DQ::AESMC::P:u:z"; static char main_help[] = "Bluetooth PAN daemon version " VERSION " \n" @@ -533,7 +548,8 @@ static char main_help[] = "\t--nodetach -n Do not become a daemon\n" "\t--persist -p[interval] Persist mode\n" "\t--cache -C[valid] Cache addresses\n" - "\t--pidfile -P Create PID file\n"; + "\t--pidfile -P Create PID file\n" + "\t--devup -u