diff options
-rw-r--r-- | src/bsd/tun.c | 5 | ||||
-rw-r--r-- | src/linux/tun.c | 35 | ||||
-rw-r--r-- | src/tun.h | 1 | ||||
-rw-r--r-- | src/tun_helper.h | 14 |
4 files changed, 37 insertions, 18 deletions
diff --git a/src/bsd/tun.c b/src/bsd/tun.c index 3eac360..07eae2b 100644 --- a/src/bsd/tun.c +++ b/src/bsd/tun.c @@ -226,6 +226,9 @@ void tun_close(tun_device_t* dev) if(dev->net_addr_) free(dev->net_addr_); + + if(dev->net_mask_) + free(dev->net_mask_); } int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len) @@ -280,7 +283,7 @@ 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 || !dev->actual_name_ || !dev->net_addr_) + if(!dev || !dev->actual_name_ || !dev->net_addr_ || !dev->net_mask_) return; diff --git a/src/linux/tun.c b/src/linux/tun.c index d8044b5..1d21eda 100644 --- a/src/linux/tun.c +++ b/src/linux/tun.c @@ -32,6 +32,8 @@ * along with ľAnytun. If not, see <http://www.gnu.org/licenses/>. */ +#include <stdio.h> + #include "datatypes.h" #include "tun.h" @@ -126,6 +128,9 @@ void tun_close(tun_device_t* dev) if(dev->net_addr_) free(dev->net_addr_); + + if(dev->net_mask_) + free(dev->net_mask_); } int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len) @@ -180,25 +185,21 @@ 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 || !dev->actual_name_ || !dev->net_addr_) + if(!dev || !dev->actual_name_ || !dev->net_addr_ || !dev->net_mask_) 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_); */ -/* else */ -/* 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"); */ -/* return; */ -/* } */ + char* command = NULL; + asprintf(&command, "/sbin/ifconfig %s %s netmask %s mtu %d", dev->actual_name_, dev->net_addr_, dev->net_mask_, dev->mtu_); + if(!command) { + log_printf(ERR, "Execution of ifconfig failed"); + return; + } -/* int result = system(command); */ -/* if(result == -1) */ -/* log_printf(ERR, "Execution of ifconfig failed"); */ -/* else */ -/* log_printf(NOTICE, "ifconfig returned %d", WEXITSTATUS(result)); */ + int result = system(command); + if(result == -1) + log_printf(ERR, "Execution of ifconfig failed"); + else + log_printf(NOTICE, "ifconfig returned %d", WEXITSTATUS(result)); -/* free(command); */ + free(command); } @@ -47,6 +47,7 @@ struct tun_device_struct { device_type_t type_; u_int16_t mtu_; char* net_addr_; + char* net_mask_; u_int16_t prefix_length_; }; typedef struct tun_device_struct tun_device_t; diff --git a/src/tun_helper.h b/src/tun_helper.h index 47c8033..db5fd6a 100644 --- a/src/tun_helper.h +++ b/src/tun_helper.h @@ -36,6 +36,9 @@ #define _TUN_HELPER_H_ #include <string.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix, u_int16_t mtu) { @@ -57,10 +60,21 @@ void tun_conf(tun_device_t* dev, const char* dev_name, const char* dev_type, con } dev->net_addr_ = NULL; + dev->net_mask_ = NULL; dev->prefix_length_ = 0; if(ifcfg_addr) { dev->net_addr_ = strdup(ifcfg_addr); dev->prefix_length_ = ifcfg_prefix; + + u_int32_t mask = 0; + u_int16_t i = 0; + for(i = 0; i < ifcfg_prefix; ++i) { + mask = mask >> 1; + mask |= 0x80000000L; + } + struct in_addr addr; + addr.s_addr = ntohl(mask); + dev->net_mask_ = strdup(inet_ntoa(addr)); } } |