summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2008-12-31 18:03:10 +0000
committerChristian Pointner <equinox@anytun.org>2008-12-31 18:03:10 +0000
commit11bc606ed6685daa45a2dfe49d08ea2c36ceaf1f (patch)
tree2dae90bc1aa9f1ffded1e84406058a38ee731a45
parentchanged useless malloc for options_t (diff)
fixed useless malloc for dev_t
-rw-r--r--src/bsd/tun.c81
-rw-r--r--src/linux/tun.c67
-rw-r--r--src/tun.h4
-rw-r--r--src/uanytun.c14
4 files changed, 81 insertions, 85 deletions
diff --git a/src/bsd/tun.c b/src/bsd/tun.c
index 20ccb68..6bec02a 100644
--- a/src/bsd/tun.c
+++ b/src/bsd/tun.c
@@ -55,17 +55,13 @@
#include <netinet/ip.h>
#define DEVICE_FILE_MAX 255
-void tun_init(tun_device_t** dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp)
+int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp)
{
if(!dev)
return;
- *dev = malloc(sizeof(tun_device_t));
- if(!*dev)
- return;
-
- tun_conf(*dev, dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400);
- (*dev)->actual_name_ = NULL;
+ tun_conf(dev, dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400);
+ dev->actual_name_ = NULL;
char* device_file = NULL;
char* actual_name_start = NULL;
@@ -74,23 +70,23 @@ void tun_init(tun_device_t** dev, const char* dev_name, const char* dev_type, co
asprintf(&device_file, "/dev/%s", dev_name);
dynamic = 0;
}
- else if((*dev)->type_ == TYPE_TUN) {
+ else if(dev->type_ == TYPE_TUN) {
asprintf(&device_file, "/dev/tun");
actual_name_start = "tun";
}
- else if((*dev)->type_ == TYPE_TAP) {
+ else if(dev->type_ == TYPE_TAP) {
asprintf(&device_file, "/dev/tap");
actual_name_start = "tap";
}
else {
log_printf(ERR, "unable to recognize type of device (tun or tap)");
tun_close(dev);
- return;
+ return -1;
}
if(!device_file) {
log_printf(ERR, "can't open device file: memory error");
tun_close(dev);
- return;
+ return -2;
}
u_int32_t dev_id=0;
@@ -103,44 +99,48 @@ void tun_init(tun_device_t** dev, const char* dev_name, const char* dev_type, co
log_printf(ERR, "can't open device file: memory error");
free(device_file);
tun_close(dev);
- return;
+ return -2;
}
- (*dev)->fd_ = open(device_file_tmp, O_RDWR);
+ dev->fd_ = open(device_file_tmp, O_RDWR);
free(device_file_tmp);
- if((*dev)->fd_ >= 0)
- break;
+ if(dev->fd_ >= 0)
+ break -1;
}
}
else
- (*dev)->fd_ = open(device_file, O_RDWR);
+ dev->fd_ = open(device_file, O_RDWR);
free(device_file);
- if((*dev)->fd_ < 0) {
+ if(dev->fd_ < 0) {
if(dynamic)
log_printf(ERR, "can't open device file dynamically: no unused node left");
else
log_printf(ERR, "can't open device file (%s): %m", device_file);
tun_close(dev);
- return;
+ return -1;
}
if(dynamic)
- asprintf(&((*dev)->actual_name_), "%s%d", actual_name_start, dev_id);
+ asprintf(&(dev->actual_name_), "%s%d", actual_name_start, dev_id);
else
- (*dev)->actual_name_ = strdup(dev_name);
+ dev->actual_name_ = strdup(dev_name);
- if(!(*dev)->actual_name_) {
+ if(!dev->actual_name_) {
log_printf(ERR, "can't open device file: memory error");
tun_close(dev);
- return;
+ return -2;
}
- tun_init_post(*dev);
+ int ret = tun_init_post(dev);
+ if(ret)
+ return ret;
if(ifcfg_lp && ifcfg_rnmp)
- tun_do_ifconfig(*dev);
+ tun_do_ifconfig(dev);
+
+ return 0;
}
@@ -149,7 +149,7 @@ void tun_init(tun_device_t** dev, const char* dev_name, const char* dev_type, co
int tun_init_post(tun_device_t* dev)
{
if(!dev)
- return;
+ return -1;
dev->with_pi_ = 1;
if(dev->type_ == TYPE_TAP)
@@ -176,7 +176,7 @@ int tun_init_post(tun_device_t* dev)
int tun_init_post(tun_device_t* dev)
{
if(!dev)
- return;
+ return -1;
dev->with_pi_ = 1;
if(dev->type_ == TYPE_TAP)
@@ -186,6 +186,8 @@ int tun_init_post(tun_device_t* dev)
ioctl(dev->fd_, TUNSLMODE, &arg);
arg = 1;
ioctl(dev->fd_, TUNSIFHEAD, &arg);
+
+ return 0;
}
#elif defined(__GNUC__) && defined(__NetBSD__)
@@ -193,7 +195,7 @@ int tun_init_post(tun_device_t* dev)
int tun_init_post(tun_device_t* dev)
{
if(!dev)
- return;
+ return -1;
dev->with_pi_ = 0;
@@ -201,6 +203,8 @@ int tun_init_post(tun_device_t* dev)
ioctl(dev->fd_, TUNSIFMODE, &arg);
arg = 0;
ioctl(dev->fd_, TUNSLMODE, &arg);
+
+ return 0;
}
#else
@@ -209,25 +213,22 @@ int tun_init_post(tun_device_t* dev)
-void tun_close(tun_device_t** dev)
+void tun_close(tun_device_t* dev)
{
- if(!dev || !(*dev))
+ if(!dev)
return;
- if((*dev)->fd_ > 0)
- close((*dev)->fd_);
-
- if((*dev)->actual_name_)
- free((*dev)->actual_name_);
+ if(dev->fd_ > 0)
+ close(dev->fd_);
- if((*dev)->local_)
- free((*dev)->local_);
+ if(dev->actual_name_)
+ free(dev->actual_name_);
- if((*dev)->remote_netmask_)
- free((*dev)->remote_netmask_);
+ if(dev->local_)
+ free(dev->local_);
- free(*dev);
- *dev = NULL;
+ if(dev->remote_netmask_)
+ free(dev->remote_netmask_);
}
int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
diff --git a/src/linux/tun.c b/src/linux/tun.c
index a6d0d4b..bcf1885 100644
--- a/src/linux/tun.c
+++ b/src/linux/tun.c
@@ -52,63 +52,61 @@
#include "log.h"
-void tun_init(tun_device_t** dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp)
+int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp)
{
if(!dev)
return;
- *dev = malloc(sizeof(tun_device_t));
- if(!*dev)
- return;
-
- tun_conf(*dev, dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400);
- (*dev)->actual_name_ = NULL;
+ tun_conf(dev, dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400);
+ dev->actual_name_ = NULL;
- (*dev)->fd_ = open(DEFAULT_DEVICE, O_RDWR);
- if((*dev)->fd_ < 0) {
+ dev->fd_ = open(DEFAULT_DEVICE, O_RDWR);
+ if(dev->fd_ < 0) {
log_printf(ERR, "can't open device file (%s): %m", DEFAULT_DEVICE);
tun_close(dev);
- return;
+ return -1;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
- if((*dev)->type_ == TYPE_TUN) {
+ if(dev->type_ == TYPE_TUN) {
ifr.ifr_flags = IFF_TUN;
- (*dev)->with_pi_ = 1;
+ dev->with_pi_ = 1;
}
- else if((*dev)->type_ == TYPE_TAP) {
+ else if(dev->type_ == TYPE_TAP) {
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
- (*dev)->with_pi_ = 0;
+ dev->with_pi_ = 0;
}
else {
log_printf(ERR, "unable to recognize type of device (tun or tap)");
tun_close(dev);
- return;
+ return -1;
}
if(dev_name)
strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
- if(!ioctl((*dev)->fd_, TUNSETIFF, &ifr)) {
- (*dev)->actual_name_ = strdup(ifr.ifr_name);
- } else if(!ioctl((*dev)->fd_, (('T' << 8) | 202), &ifr)) {
- (*dev)->actual_name_ = strdup(ifr.ifr_name);
+ if(!ioctl(dev->fd_, TUNSETIFF, &ifr)) {
+ dev->actual_name_ = strdup(ifr.ifr_name);
+ } else if(!ioctl(dev->fd_, (('T' << 8) | 202), &ifr)) {
+ dev->actual_name_ = strdup(ifr.ifr_name);
} else {
log_printf(ERR, "tun/tap device ioctl failed: %m");
tun_close(dev);
- return;
+ return -1;
}
- if(!(*dev)->actual_name_) {
+ if(!dev->actual_name_) {
log_printf(ERR, "can't open device file: memory error");
tun_close(dev);
- return;
+ return -2;
}
if(ifcfg_lp && ifcfg_rnmp)
- tun_do_ifconfig(*dev);
+ tun_do_ifconfig(dev);
+
+ return 0;
}
int tun_init_post(tun_device_t* dev)
@@ -116,25 +114,22 @@ int tun_init_post(tun_device_t* dev)
// nothing yet
}
-void tun_close(tun_device_t** dev)
+void tun_close(tun_device_t* dev)
{
- if(!dev || !(*dev))
+ if(!dev)
return;
- if((*dev)->fd_ > 0)
- close((*dev)->fd_);
-
- if((*dev)->actual_name_)
- free((*dev)->actual_name_);
+ if(dev->fd_ > 0)
+ close(dev->fd_);
- if((*dev)->local_)
- free((*dev)->local_);
+ if(dev->actual_name_)
+ free(dev->actual_name_);
- if((*dev)->remote_netmask_)
- free((*dev)->remote_netmask_);
+ if(dev->local_)
+ free(dev->local_);
- free(*dev);
- *dev = NULL;
+ if(dev->remote_netmask_)
+ free(dev->remote_netmask_);
}
int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len)
diff --git a/src/tun.h b/src/tun.h
index 5f30aab..519c62b 100644
--- a/src/tun.h
+++ b/src/tun.h
@@ -51,10 +51,10 @@ struct tun_device_struct {
};
typedef struct tun_device_struct tun_device_t;
-void tun_init(tun_device_t** dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp);
+int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_lp, const char* ifcfg_rnmp);
int tun_init_post(tun_device_t* dev);
void tun_do_ifconfig(tun_device_t* dev);
-void tun_close(tun_device_t** dev);
+void tun_close(tun_device_t* dev);
int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len);
int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len);
diff --git a/src/uanytun.c b/src/uanytun.c
index db42d5c..1d6e646 100644
--- a/src/uanytun.c
+++ b/src/uanytun.c
@@ -216,17 +216,17 @@ int main(int argc, char* argv[])
log_printf(NOTICE, "just started...");
- tun_device_t* dev;
- tun_init(&dev, opt.dev_name_, opt.dev_type_, opt.ifconfig_param_local_, opt.ifconfig_param_remote_netmask_);
- if(!dev) {
+ tun_device_t dev;
+ ret = tun_init(&dev, opt.dev_name_, opt.dev_type_, opt.ifconfig_param_local_, opt.ifconfig_param_remote_netmask_);
+ if(ret) {
log_printf(ERR, "error on tun_init, exitting");
options_clear(&opt);
- exit(-1);
+ exit(ret);
}
- log_printf(NOTICE, "dev of type '%s' opened, actual name is '%s'", tun_get_type_string(dev), dev->actual_name_);
+ log_printf(NOTICE, "dev of type '%s' opened, actual name is '%s'", tun_get_type_string(&dev), dev.actual_name_);
if(opt.post_up_script_) {
- int ret = exec_script(opt.post_up_script_, dev->actual_name_);
+ int ret = exec_script(opt.post_up_script_, dev.actual_name_);
log_printf(NOTICE, "post-up script returned %d", ret);
}
@@ -273,7 +273,7 @@ int main(int argc, char* argv[])
fclose(pid_file);
}
- ret = main_loop(dev, sock, &opt);
+ ret = main_loop(&dev, sock, &opt);
tun_close(&dev);
udp_close(&sock);