summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2008-12-27 17:27:45 +0000
committerChristian Pointner <equinox@anytun.org>2008-12-27 17:27:45 +0000
commitc930c97326d576f13a52dc3a4595d99fac1ed2e7 (patch)
tree2ce2f896827631a1657b1c89945ce0ddd1d9027e /src
parentfixed some memory errors at tun device (diff)
some more memory erros @ tun device fixed
added exec script for post up scripts
Diffstat (limited to 'src')
-rw-r--r--src/linux/tun.c14
-rw-r--r--src/sysexec.h66
-rw-r--r--src/uanytun.c16
3 files changed, 86 insertions, 10 deletions
diff --git a/src/linux/tun.c b/src/linux/tun.c
index c886335..7f3b60c 100644
--- a/src/linux/tun.c
+++ b/src/linux/tun.c
@@ -62,6 +62,10 @@ void tun_init(tun_device_t** dev, const char* dev_name, const char* dev_type, co
(*dev)->fd_ = open(DEFAULT_DEVICE, O_RDWR);
if((*dev)->fd_ < 0) {
log_printf(ERR, "can't open device file (%s): %m", DEFAULT_DEVICE);
+ if((*dev)->local_)
+ free((*dev)->local_);
+ if((*dev)->remote_netmask_)
+ free((*dev)->remote_netmask_);
free(*dev);
*dev = NULL;
return;
@@ -80,6 +84,10 @@ void tun_init(tun_device_t** dev, const char* dev_name, const char* dev_type, co
}
else {
log_printf(ERR, "unable to recognize type of device (tun or tap)");
+ if((*dev)->local_)
+ free((*dev)->local_);
+ if((*dev)->remote_netmask_)
+ free((*dev)->remote_netmask_);
free(*dev);
*dev = NULL;
return;
@@ -178,14 +186,14 @@ int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
void tun_do_ifconfig(tun_device_t* dev)
{
- if(!dev)
+ if(!dev || !dev->actual_name_ || !dev->local_ || !dev->remote_netmask_)
return;
char* command = NULL;
if(dev->type_ == TYPE_TUN)
- asprintf(&command, "/sbin/ifconfig %s %s pointopoint %s mtu %d" , dev->actual_name_, dev->local_, dev->remote_netmask_, dev->mtu_);
+ asprintf(&command, "/sbin/ifconfig %s %s pointopoint %s mtu %d", dev->actual_name_, dev->local_, dev->remote_netmask_, dev->mtu_);
else
- asprintf(&command, "/sbin/ifconfig %s %s netmask %s mtu %d" , dev->actual_name_, dev->local_, dev->remote_netmask_, dev->mtu_);
+ asprintf(&command, "/sbin/ifconfig %s %s netmask %s mtu %d", dev->actual_name_, dev->local_, dev->remote_netmask_, dev->mtu_);
if(!command) {
log_printf(ERR, "Execution of ifconfig failed");
diff --git a/src/sysexec.h b/src/sysexec.h
new file mode 100644
index 0000000..2b01bc8
--- /dev/null
+++ b/src/sysexec.h
@@ -0,0 +1,66 @@
+/*
+ * ľAnytun
+ *
+ * ľAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
+ * featured implementation ľAnytun has no support for multiple connections
+ * or synchronisation. It is a small single threaded implementation intended
+ * to act as a client on small platforms.
+ * The secure anycast tunneling protocol (satp) defines a protocol used
+ * for communication between any combination of unicast and anycast
+ * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
+ * mode and allows tunneling of every ETHER TYPE protocol (e.g.
+ * ethernet, ip, arp ...). satp directly includes cryptography and
+ * message authentication based on the methodes used by SRTP. It is
+ * intended to deliver a generic, scaleable and secure solution for
+ * tunneling and relaying of packets of any protocol.
+ *
+ *
+ * Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
+ *
+ * This file is part of ľAnytun.
+ *
+ * ľAnytun is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * ľAnytun 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 ľAnytun. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _SYSEXEC_H_
+#define _SYSEXEC_H_
+
+int exec_script(const char* script, const char* ifname)
+{
+ if(!script || !ifname)
+ return;
+
+ pid_t pid;
+ pid = fork();
+ if(!pid) {
+ int fd;
+ for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
+ close(fd);
+
+ fd = open("/dev/null",O_RDWR); // stdin
+ if(fd == -1)
+ log_printf(WARNING, "can't open stdin");
+ else {
+ if(dup(fd) == -1) // stdout
+ log_printf(WARNING, "can't open stdout");
+ if(dup(fd) == -1) // stderr
+ log_printf(WARNING, "can't open stderr");
+ }
+ return execl("/bin/sh", "/bin/sh", script, ifname, NULL);
+ }
+ int status = 0;
+ waitpid(pid, &status, 0);
+ return status;
+}
+
+#endif
diff --git a/src/uanytun.c b/src/uanytun.c
index fca5e40..ffdfdf3 100644
--- a/src/uanytun.c
+++ b/src/uanytun.c
@@ -36,11 +36,12 @@
#include <stdio.h>
#include "log.h"
-#include "daemon.h"
#include "signal.h"
-
#include "tun.h"
+#include "daemon.h"
+#include "sysexec.h"
+
int main(int argc, char* argv[])
{
log_init("uanytun", DAEMON);
@@ -51,25 +52,26 @@ int main(int argc, char* argv[])
// log_printf(INFO, "running in background now");
tun_device_t* dev;
- tun_init(&dev, "tun0", "tun", "192.168.23.1", "192.168.23.2");
+ tun_init(&dev, NULL, "tun", "192.168.23.1", "192.168.23.2");
if(!dev) {
log_printf(ERR, "error on tun_init");
- exit -1;
+ exit(-1);
}
+/* int ret = exec_script("post-up.sh", dev->actual_name_); */
+/* log_printf(NOTICE, "post-up script returned %d", ret); */
log_printf(INFO, "entering main loop");
u_int8_t buf[1600];
int len = 0;
unsigned int cnt = 0;
- while(cnt < 10) {
+ while(cnt < 5) {
len = tun_read(dev, buf, 1600);
printf("read %d bytes from device\n", len);
+// tun_write(dev, buf, len);
cnt++;
}
tun_close(&dev);
return 0;
}
-
-