From bc4cb70ddb8fe59d8b097a62029b0f24849163a3 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 25 Apr 2004 22:03:05 +0000 Subject: Add the dund utility --- dund/dun.c | 308 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 308 insertions(+) create mode 100644 dund/dun.c (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c new file mode 100644 index 00000000..8ef72ce6 --- /dev/null +++ b/dund/dun.c @@ -0,0 +1,308 @@ +/* + dund - Bluetooth LAN/DUN 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 +#include +#include + +#include + +#include +#include + +#include "dund.h" +#include "lib.h" + +#define PROC_BASE "/proc" + +static int for_each_port(int (*func)(struct rfcomm_dev_info *, unsigned long), unsigned long arg) +{ + struct rfcomm_dev_list_req *dl; + struct rfcomm_dev_info *di; + long r = 0; + int sk, i; + + sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM); + if (sk < 0 ) { + perror("Can't open RFCOMM control socket"); + exit(1); + } + + dl = malloc(sizeof(*dl) + RFCOMM_MAX_DEV * sizeof(*di)); + if (!dl) { + perror("Can't allocate request memory"); + close(sk); + exit(1); + } + + dl->dev_num = RFCOMM_MAX_DEV; + di = dl->dev_info; + + if (ioctl(sk, RFCOMMGETDEVLIST, (void *) dl) < 0) { + perror("Can't get device list"); + exit(1); + } + + for (i = 0; i < dl->dev_num; i++) { + r = func(di + i, arg); + if (r) break; + } + + close(sk); + return r; +} + +static int uses_rfcomm(char *path, char *dev) +{ + struct dirent *de; + DIR *dir; + + dir = opendir(path); + if (!dir) + return 0; + chdir(path); + + while ((de = readdir(dir)) != NULL) { + char link[PATH_MAX + 1]; + int len = readlink(de->d_name, link, sizeof(link)); + if (len > 0) { + link[len] = 0; + if (strstr(link, dev)) + return 1; + } + } + closedir(dir); + return 0; +} + +static int find_pppd(int id, pid_t *pid) +{ + struct dirent *de; + char path[PATH_MAX + 1]; + char dev[10]; + int empty = 1; + DIR *dir; + + dir = opendir(PROC_BASE); + if (!dir) { + perror(PROC_BASE); + return -1; + } + + sprintf(dev, "rfcomm%d", id); + + *pid = 0; + while ((de = readdir(dir)) != NULL) { + empty = 0; + if (isdigit(de->d_name[0])) { + sprintf(path, "%s/%s/fd", PROC_BASE, de->d_name); + if (uses_rfcomm(path, dev)) { + *pid = atoi(de->d_name); + break; + } + } + } + closedir(dir); + + if (empty) + fprintf(stderr, "%s is empty (not mounted ?)\n", PROC_BASE); + + return *pid != 0; +} + +static int dun_exec(char *tty, char *prog, char **args) +{ + int pid = fork(); + int fd; + + switch (pid) { + case -1: + return -1; + + case 0: + break; + + default: + return pid; + } + + setsid(); + + /* Close all FDs */ + for (fd=3; fd < 20; fd++) + close(fd); + + execvp(prog, args); + exit(1); +} + +static int dun_create_tty(int sk, char *tty, int size) +{ + struct sockaddr_rc sa; + struct stat st; + int id, alen; + + struct rfcomm_dev_req req = { + flags: (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP), + dev_id: -1 + }; + + alen = sizeof(sa); + if (getpeername(sk, (struct sockaddr *) &sa, &alen) < 0) + return -1; + bacpy(&req.dst, &sa.rc_bdaddr); + + alen = sizeof(sa); + if (getsockname(sk, (struct sockaddr *) &sa, &alen) < 0) + return -1; + bacpy(&req.src, &sa.rc_bdaddr); + req.channel = sa.rc_channel; + + id = ioctl(sk, RFCOMMCREATEDEV, &req); + if (id < 0) + return id; + + snprintf(tty, size, "/dev/rfcomm%d", id); + if (stat(tty, &st) < 0) { + snprintf(tty, size, "/dev/bluetooth/rfcomm/%d", id); + if (stat(tty, &st) < 0) { + snprintf(tty, size, "/dev/rfcomm%d", id); + return -1; + } + } + + return id; +} + +int dun_init(void) +{ + return 0; +} + +int dun_cleanup(void) +{ + return 0; +} + +static int show_conn(struct rfcomm_dev_info *di, unsigned long arg) +{ + pid_t pid; + + if (di->state == BT_CONNECTED && + (di->flags & (1<flags & (1<flags & (1<id, &pid)) { + char dst[18]; + ba2str(&di->dst, dst); + + printf("rfcomm%d: %s channel %d pppd pid %d\n", + di->id, dst, di->channel, pid); + } + } + return 0; +} + +static int kill_conn(struct rfcomm_dev_info *di, unsigned long arg) +{ + bdaddr_t *dst = (bdaddr_t *) arg; + pid_t pid; + + if (di->state == BT_CONNECTED && + (di->flags & (1<flags & (1<flags & (1<dst, dst)) + return 0; + + if (find_pppd(di->id, &pid)) { + if (kill(pid, SIGINT) < 0) + perror("Kill"); + + if (!dst) + return 0; + return 1; + } + } + return 0; +} + +int dun_show_connections(void) +{ + for_each_port(show_conn, 0); + return 0; +} + +int dun_kill_connection(uint8_t *dst) +{ + for_each_port(kill_conn, (unsigned long) dst); + return 0; +} + +int dun_kill_all_connections(void) +{ + for_each_port(kill_conn, 0); + return 0; +} + +int dun_open_connection(int sk, char *pppd, char **args, int wait) +{ + char tty[100]; + int pid; + + if (dun_create_tty(sk, tty, sizeof(tty) - 1) < 0) { + syslog(LOG_ERR, "RFCOMM TTY creation failed. %s(%d)", strerror(errno), errno); + return -1; + } + + args[0] = "pppd"; + args[1] = tty; + args[2] = "nodetach"; + + pid = dun_exec(tty, pppd, args); + if (pid < 0) { + syslog(LOG_ERR, "Exec failed. %s(%d)", strerror(errno), errno); + return -1; + } + + if (wait) { + int status; + waitpid(pid, &status, 0); + /* FIXME: Check for waitpid errors */ + } + + return 0; +} -- 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 --- dund/dun.c | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 8ef72ce6..1486d515 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -1,25 +1,36 @@ /* - dund - Bluetooth LAN/DUN 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 @@ -239,7 +250,7 @@ static int kill_conn(struct rfcomm_dev_info *di, unsigned long arg) { bdaddr_t *dst = (bdaddr_t *) arg; pid_t pid; - + if (di->state == BT_CONNECTED && (di->flags & (1<flags & (1< Date: Wed, 28 Apr 2004 13:40:58 +0000 Subject: Give udev some time to create the RFCOMM device nodes --- dund/dun.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 1486d515..6611ad86 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -182,7 +182,7 @@ static int dun_create_tty(int sk, char *tty, int size) { struct sockaddr_rc sa; struct stat st; - int id, alen; + int id, alen, try = 3; struct rfcomm_dev_req req = { flags: (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP), @@ -205,10 +205,19 @@ static int dun_create_tty(int sk, char *tty, int size) return id; snprintf(tty, size, "/dev/rfcomm%d", id); - if (stat(tty, &st) < 0) { + while (stat(tty, &st) < 0) { snprintf(tty, size, "/dev/bluetooth/rfcomm/%d", id); if (stat(tty, &st) < 0) { snprintf(tty, size, "/dev/rfcomm%d", id); + if (try--) { + sleep(1); + continue; + } + + memset(&req, 0, sizeof(req)); + req.dev_id = id; + ioctl(sk, RFCOMMRELEASEDEV, &req); + return -1; } } -- 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 --- dund/dun.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 6611ad86..0593cb85 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -182,7 +182,8 @@ static int dun_create_tty(int sk, char *tty, int size) { struct sockaddr_rc sa; struct stat st; - int id, alen, try = 3; + socklen_t alen; + int id, try = 3; struct rfcomm_dev_req req = { flags: (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP), -- cgit From 7d463a7ae8a9775fd091131659a849baca993e53 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 21 Aug 2005 08:56:10 +0000 Subject: Add ActiveSync networking support --- dund/dun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 0593cb85..d5724cfb 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -3,7 +3,7 @@ * 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 -- 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 --- dund/dun.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index d5724cfb..3de858d9 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -7,45 +7,40 @@ * * * 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 #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 --- dund/dun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 3de858d9..2a4a5cc6 100644 --- a/dund/dun.c +++ b/dund/dun.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 a388c640dd22bb07bd899cd32883bc42b16c430a Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 7 Feb 2006 11:17:31 +0000 Subject: Write error is execution of pppd fails --- dund/dun.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 2a4a5cc6..f8bc6ee4 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -166,10 +166,13 @@ static int dun_exec(char *tty, char *prog, char **args) setsid(); /* Close all FDs */ - for (fd=3; fd < 20; fd++) + for (fd = 3; fd < 20; fd++) close(fd); execvp(prog, args); + + syslog(LOG_ERR, "Error while executing %s", prog); + exit(1); } -- cgit From 943b02e163e169795a010c36a3b3343cc5092a96 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 27 Jun 2006 07:33:36 +0000 Subject: Include sys/param.h for PATH_MAX when cross-compiling --- dund/dun.c | 1 + 1 file changed, 1 insertion(+) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index f8bc6ee4..b1ba8380 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include -- cgit From b0f139af155d04ef8c399e77e89707af98441693 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 6 Jul 2006 10:58:43 +0000 Subject: Sleep only 100 msecs for device detection --- dund/dun.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index b1ba8380..4fbbad69 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -182,7 +182,7 @@ static int dun_create_tty(int sk, char *tty, int size) struct sockaddr_rc sa; struct stat st; socklen_t alen; - int id, try = 3; + int id, try = 30; struct rfcomm_dev_req req = { flags: (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP), @@ -210,7 +210,7 @@ static int dun_create_tty(int sk, char *tty, int size) if (stat(tty, &st) < 0) { snprintf(tty, size, "/dev/rfcomm%d", id); if (try--) { - sleep(1); + usleep(100); continue; } -- cgit From b102348e988e4abc5d579ce13c067ce2c885aaf7 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 26 Jul 2006 13:32:44 +0000 Subject: Fix declared with attribute warn_unused_result errors --- dund/dun.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 4fbbad69..718da7ec 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -98,7 +98,9 @@ static int uses_rfcomm(char *path, char *dev) dir = opendir(path); if (!dir) return 0; - chdir(path); + + if (chdir(path) < 0) + return 0; while ((de = readdir(dir)) != NULL) { char link[PATH_MAX + 1]; @@ -109,7 +111,9 @@ static int uses_rfcomm(char *path, char *dev) return 1; } } + closedir(dir); + return 0; } -- cgit From a1bc48d15a5d6e78efe744eb7b27b6421cb7222f Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Wed, 16 Aug 2006 10:54:06 +0000 Subject: Convert to using ppoll() and pselect() --- dund/dun.c | 1 - 1 file changed, 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 718da7ec..fdc1e287 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -37,7 +37,6 @@ #include #include -#include #include #include #include -- cgit From 607695ed109340f4b7a5628420e0a8e8aee34f4e Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 13 Jan 2007 17:48:12 +0000 Subject: Update copyright information --- dund/dun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index fdc1e287..0f801932 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -3,7 +3,7 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2006 Marcel Holtmann + * Copyright (C) 2002-2007 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify -- cgit From 4539df01941b46f707bd8ccf9d54ff6f288cae70 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Tue, 13 Feb 2007 20:41:55 +0000 Subject: Wait 30 * 100ms for udev to create the device node --- dund/dun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 0f801932..67e60213 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -213,7 +213,7 @@ static int dun_create_tty(int sk, char *tty, int size) if (stat(tty, &st) < 0) { snprintf(tty, size, "/dev/rfcomm%d", id); if (try--) { - usleep(100); + usleep(100 * 1000); continue; } -- cgit From e823c15e43a6f924779e466d434c51157002d9ee Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sat, 2 Feb 2008 03:37:05 +0000 Subject: Update copyright information --- dund/dun.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'dund/dun.c') diff --git a/dund/dun.c b/dund/dun.c index 67e60213..de437cf4 100644 --- a/dund/dun.c +++ b/dund/dun.c @@ -3,7 +3,7 @@ * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2002-2003 Maxim Krasnyansky - * Copyright (C) 2002-2007 Marcel Holtmann + * Copyright (C) 2002-2008 Marcel Holtmann * * * This program is free software; you can redistribute it and/or modify -- cgit