From 1604cea7467a76866dcf4efc067d910712a869a0 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 8 Jun 2014 18:41:58 +0000 Subject: merged most changes from rail branch to trunk --- src/Makefile | 4 +- src/auth_algo.c | 28 ++++----- src/auth_algo.h | 4 +- src/bsd/tun.c | 32 +++++----- src/cipher.c | 40 ++++++------ src/cipher.h | 4 +- src/configure | 6 +- src/daemon.h | 5 +- src/datatypes.h | 4 +- src/encrypted_packet.c | 6 +- src/encrypted_packet.h | 4 +- src/init_crypt.h | 8 +-- src/key_derivation.c | 18 +++--- src/key_derivation.h | 4 +- src/linux/tun.c | 40 ++++++------ src/log.c | 10 +-- src/log.h | 4 +- src/log_targets.h | 20 +++--- src/options.c | 19 +++--- src/options.h | 4 +- src/plain_packet.c | 8 +-- src/plain_packet.h | 6 +- src/seq_window.c | 22 ++++--- src/seq_window.h | 5 +- src/sig_handler.c | 6 +- src/sig_handler.h | 4 +- src/string_list.c | 8 +-- src/string_list.h | 4 +- src/sysexec.c | 8 +-- src/sysexec.h | 4 +- src/tun.h | 6 +- src/tun_helper.h | 6 +- src/uanytun.c | 97 +++++++++++++---------------- src/udp.c | 162 +++++++++++++++++++++++++++++++++---------------- src/udp.h | 19 +++--- 35 files changed, 339 insertions(+), 290 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 5ed36d9..6653b60 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,9 +13,9 @@ ## 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-2010 Christian Pointner +## +## Copyright (C) 2007-2014 Christian Pointner ## ## This file is part of uAnytun. ## diff --git a/src/auth_algo.c b/src/auth_algo.c index b148946..db87e7a 100644 --- a/src/auth_algo.c +++ b/src/auth_algo.c @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -50,7 +50,7 @@ auth_algo_type_t auth_algo_get_type(const char* type) return aa_null; else if(!strcmp(type, "sha1")) return aa_sha1; - + return aa_unknown; } @@ -65,7 +65,7 @@ u_int32_t auth_algo_get_max_length(const char* type) int auth_algo_init(auth_algo_t* aa, const char* type) { - if(!aa) + if(!aa) return -1; aa->type_ = auth_algo_get_type(type); @@ -103,7 +103,7 @@ void auth_algo_close(auth_algo_t* aa) void auth_algo_generate(auth_algo_t* aa, key_derivation_t* kd, key_derivation_dir_t dir, encrypted_packet_t* packet) { - if(!aa) + if(!aa) return; if(aa->type_ == aa_null) @@ -118,7 +118,7 @@ void auth_algo_generate(auth_algo_t* aa, key_derivation_t* kd, key_derivation_di int auth_algo_check_tag(auth_algo_t* aa, key_derivation_t* kd, key_derivation_dir_t dir, encrypted_packet_t* packet) { - if(!aa) + if(!aa) return 0; if(aa->type_ == aa_null) @@ -159,7 +159,7 @@ int auth_algo_sha1_init(auth_algo_t* aa) if(err) { log_printf(ERROR, "failed to open message digest algo: %s", gcry_strerror(err)); return -1; - } + } #else HMAC_CTX_init(¶ms->ctx_); HMAC_Init_ex(¶ms->ctx_, NULL, 0, EVP_sha1(), NULL); @@ -181,7 +181,7 @@ void auth_algo_sha1_close(auth_algo_t* aa) gcry_md_close(params->handle_); #else HMAC_CTX_cleanup(¶ms->ctx_); -#endif +#endif free(aa->params_); } @@ -212,8 +212,8 @@ void auth_algo_sha1_generate(auth_algo_t* aa, key_derivation_t* kd, key_derivati if(err) { log_printf(ERROR, "failed to set hmac key: %s", gcry_strerror(err)); return; - } - + } + gcry_md_reset(params->handle_); gcry_md_write(params->handle_, encrypted_packet_get_auth_portion(packet), encrypted_packet_get_auth_portion_length(packet)); gcry_md_final(params->handle_); @@ -260,7 +260,7 @@ int auth_algo_sha1_check_tag(auth_algo_t* aa, key_derivation_t* kd, key_derivati if(err) { log_printf(ERROR, "failed to set hmac key: %s", gcry_strerror(err)); return -1; - } + } gcry_md_reset(params->handle_); gcry_md_write(params->handle_, encrypted_packet_get_auth_portion(packet), encrypted_packet_get_auth_portion_length(packet)); @@ -280,11 +280,11 @@ int auth_algo_sha1_check_tag(auth_algo_t* aa, key_derivation_t* kd, key_derivati if(length > SHA1_LENGTH) { u_int32_t i; for(i=0; i < (encrypted_packet_get_auth_tag_length(packet) - SHA1_LENGTH); ++i) - if(tag[i]) return 0; + if(tag[i]) return 0; } - + int result = memcmp(&tag[encrypted_packet_get_auth_tag_length(packet) - length], &hmac[SHA1_LENGTH - length], length); - + if(result) return 0; diff --git a/src/auth_algo.h b/src/auth_algo.h index 1007955..d911ca8 100644 --- a/src/auth_algo.h +++ b/src/auth_algo.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * diff --git a/src/bsd/tun.c b/src/bsd/tun.c index a995c35..734c3d8 100644 --- a/src/bsd/tun.c +++ b/src/bsd/tun.c @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -58,9 +58,9 @@ int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix) { - if(!dev) + if(!dev) return -1; - + tun_conf(dev, dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400); dev->actual_name_ = NULL; @@ -109,7 +109,7 @@ int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, cons tun_close(dev); return -2; } - + dev->fd_ = open(device_file_tmp, O_RDWR); free(device_file_tmp); if(dev->fd_ >= 0) @@ -125,7 +125,7 @@ int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, cons log_printf(ERROR, "can't open device file dynamically: no unused node left"); else log_printf(ERROR, "can't open device file (%s): %s", device_file, strerror(errno)); - + tun_close(dev); return -1; } @@ -164,18 +164,18 @@ int tun_init_post(tun_device_t* dev) dev->with_pi_ = 1; if(dev->type_ == TYPE_TAP) dev->with_pi_ = 0; - - struct tuninfo ti; + + struct tuninfo ti; if(ioctl(dev->fd_, TUNGIFINFO, &ti) < 0) { log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno)); return -1; - } + } ti.flags |= IFF_MULTICAST; if(dev->type_ == TYPE_TUN) ti.flags &= ~IFF_POINTOPOINT; - + if(ioctl(dev->fd_, TUNSIFINFO, &ti) < 0) { log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno)); return -1; @@ -199,20 +199,20 @@ int tun_init_post(tun_device_t* dev) if(ioctl(dev->fd_, TUNSLMODE, &arg) < 0) { log_printf(ERROR, "can't disable link-layer mode for interface: %s", strerror(errno)); return -1; - } + } arg = 1; if(ioctl(dev->fd_, TUNSIFHEAD, &arg) < 0) { log_printf(ERROR, "can't enable multi-af mode for interface: %s", strerror(errno)); return -1; - } + } arg = IFF_BROADCAST; arg |= IFF_MULTICAST; if(ioctl(dev->fd_, TUNSIFMODE, &arg) < 0) { log_printf(ERROR, "can't enable multicast for interface: %s", strerror(errno)); return -1; - } + } } return 0; @@ -268,7 +268,7 @@ int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len) { struct iovec iov[2]; u_int32_t type; - + iov[0].iov_base = &type; iov[0].iov_len = sizeof(type); iov[1].iov_base = buf; @@ -292,13 +292,13 @@ int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len) struct iovec iov[2]; u_int32_t type; struct ip *hdr = (struct ip*)buf; - + type = 0; if(hdr->ip_v == 4) type = htonl(AF_INET); else type = htonl(AF_INET6); - + iov[0].iov_base = &type; iov[0].iov_len = sizeof(type); iov[1].iov_base = buf; diff --git a/src/cipher.c b/src/cipher.c index 427ce9a..d6eae57 100644 --- a/src/cipher.c +++ b/src/cipher.c @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -47,7 +47,7 @@ int cipher_init(cipher_t* c, const char* type) { - if(!c) + if(!c) return -1; c->key_length_ = 0; @@ -61,7 +61,7 @@ int cipher_init(cipher_t* c, const char* type) if(type[7] == 0) { c->key_length_ = C_AESCTR_DEFAULT_KEY_LENGTH; } - else if(type[7] != '-') + else if(type[7] != '-') return -1; else { const char* tmp = &type[8]; @@ -113,12 +113,12 @@ void cipher_close(cipher_t* c) int cipher_encrypt(cipher_t* c, key_derivation_t* kd, key_derivation_dir_t dir, plain_packet_t* in, encrypted_packet_t* out, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux) { - if(!c) + if(!c) return -1; - int32_t len; + int32_t len; if(c->type_ == c_null) - len = cipher_null_crypt(plain_packet_get_packet(in), plain_packet_get_length(in), + len = cipher_null_crypt(plain_packet_get_packet(in), plain_packet_get_length(in), encrypted_packet_get_payload(out), encrypted_packet_get_payload_length(out)); #ifndef NO_CRYPT else if(c->type_ == c_aes_ctr) @@ -134,7 +134,7 @@ int cipher_encrypt(cipher_t* c, key_derivation_t* kd, key_derivation_dir_t dir, if(len < 0) return 0; - encrypted_packet_set_sender_id(out, sender_id); + encrypted_packet_set_sender_id(out, sender_id); encrypted_packet_set_seq_nr(out, seq_nr); encrypted_packet_set_mux(out, mux); @@ -145,10 +145,10 @@ int cipher_encrypt(cipher_t* c, key_derivation_t* kd, key_derivation_dir_t dir, int cipher_decrypt(cipher_t* c, key_derivation_t* kd, key_derivation_dir_t dir, encrypted_packet_t* in, plain_packet_t* out) { - if(!c) + if(!c) return -1; - int32_t len; + int32_t len; if(c->type_ == c_null) len = cipher_null_crypt(encrypted_packet_get_payload(in), encrypted_packet_get_payload_length(in), plain_packet_get_packet(out), plain_packet_get_length(out)); @@ -163,11 +163,11 @@ int cipher_decrypt(cipher_t* c, key_derivation_t* kd, key_derivation_dir_t dir, log_printf(ERROR, "unknown cipher type"); return -1; } - + if(len < 0) return 0; - plain_packet_set_length(out, len); + plain_packet_set_length(out, len); return 0; } @@ -176,7 +176,7 @@ int cipher_decrypt(cipher_t* c, key_derivation_t* kd, key_derivation_dir_t dir, int32_t cipher_null_crypt(u_int8_t* in, u_int32_t ilen, u_int8_t* out, u_int32_t olen) { - memcpy(out, in, (ilen < olen) ? ilen : olen); + memcpy(out, in, (ilen < olen) ? ilen : olen); return (ilen < olen) ? ilen : olen; } @@ -227,7 +227,7 @@ int cipher_aesctr_init(cipher_t* c) if(err) { log_printf(ERROR, "failed to open cipher: %s", gcry_strerror(err)); return -1; - } + } #endif return 0; @@ -241,10 +241,8 @@ void cipher_aesctr_close(cipher_t* c) if(c->params_) { #ifndef USE_SSL_CRYPTO cipher_aesctr_param_t* params = c->params_; - if(params->handle_) - gcry_cipher_close(params->handle_); + gcry_cipher_close(params->handle_); #endif - free(c->params_); } } @@ -253,7 +251,7 @@ int cipher_aesctr_calc_ctr(cipher_t* c, key_derivation_t* kd, key_derivation_dir { if(!c || !c->params_) return -1; - + cipher_aesctr_param_t* params = c->params_; int ret = key_derivation_generate(kd, dir, LABEL_SALT, seq_nr, c->salt_.buf_, C_AESCTR_SALT_LENGTH); @@ -286,7 +284,7 @@ int32_t cipher_aesctr_crypt(cipher_t* c, key_derivation_t* kd, key_derivation_di int ret = key_derivation_generate(kd, dir, LABEL_ENC, seq_nr, c->key_.buf_, c->key_.length_); if(ret < 0) return ret; - + #ifdef USE_SSL_CRYPTO ret = AES_set_encrypt_key(c->key_.buf_, c->key_length_, ¶ms->aes_key_); if(ret) { @@ -306,7 +304,7 @@ int32_t cipher_aesctr_crypt(cipher_t* c, key_derivation_t* kd, key_derivation_di log_printf(ERROR, "failed to calculate cipher CTR"); return ret; } - + #ifndef USE_SSL_CRYPTO err = gcry_cipher_setctr(params->handle_, params->ctr_.buf_, C_AESCTR_CTR_LENGTH); if(err) { @@ -329,6 +327,6 @@ int32_t cipher_aesctr_crypt(cipher_t* c, key_derivation_t* kd, key_derivation_di AES_ctr128_encrypt(in, out, (ilen < olen) ? ilen : olen, ¶ms->aes_key_, params->ctr_.buf_, params->ecount_buf_, &num); #endif - return (ilen < olen) ? ilen : olen; + return (ilen < olen) ? ilen : olen; } #endif diff --git a/src/cipher.h b/src/cipher.h index e33d815..4582a46 100644 --- a/src/cipher.h +++ b/src/cipher.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * diff --git a/src/configure b/src/configure index d2f60a8..542b4b4 100755 --- a/src/configure +++ b/src/configure @@ -16,7 +16,7 @@ # tunneling and relaying of packets of any protocol. # # -# Copyright (C) 2007-2010 Christian Pointner +# Copyright (C) 2007-2014 Christian Pointner # # This file is part of uAnytun. # @@ -60,6 +60,7 @@ print_usage() { echo " --no-manpage dont't install manpage" echo " --examplesdir= the path to the examples files (default: $PREFIX/share/examples)" echo " --no-examples dont't install example files" + echo " --use-gcrypt use libgcrypt (this is the default)" echo " --use-ssl-crypto use ssl crypto library instead of libgcrypt" echo " --no-crypto disable crypto at all (only NULL cipher)" echo " --disable-passphrase disable master key and salt passphrase" @@ -97,6 +98,9 @@ do --no-examples) INSTALLEXAMPLES=0 ;; + --use-gcrypt) + CRYPTO_LIB='gcrypt' + ;; --use-ssl-crypto) CRYPTO_LIB='ssl' ;; diff --git a/src/daemon.h b/src/daemon.h index 9e6f1b2..05fa83e 100644 --- a/src/daemon.h +++ b/src/daemon.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -172,4 +172,3 @@ void daemonize() } #endif - diff --git a/src/datatypes.h b/src/datatypes.h index 0105062..a374022 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * diff --git a/src/encrypted_packet.c b/src/encrypted_packet.c index 12f04ef..801d8e6 100644 --- a/src/encrypted_packet.c +++ b/src/encrypted_packet.c @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -188,7 +188,7 @@ mux_t encrypted_packet_get_mux(encrypted_packet_t* packet) { if(!packet) return 0; - + return MUX_T_NTOH(packet->data_.header_.mux_); } diff --git a/src/encrypted_packet.h b/src/encrypted_packet.h index 4afe323..fcd16ab 100644 --- a/src/encrypted_packet.h +++ b/src/encrypted_packet.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * diff --git a/src/init_crypt.h b/src/init_crypt.h index 2461a54..848f9d7 100644 --- a/src/init_crypt.h +++ b/src/init_crypt.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -54,7 +54,7 @@ int init_crypt() #define MIN_GCRYPT_VERSION "1.2.0" -int init_crypt() +int init_crypt() { if(!gcry_check_version(MIN_GCRYPT_VERSION)) { log_printf(NOTICE, "invalid Version of libgcrypt, should be >= %s", MIN_GCRYPT_VERSION); @@ -81,7 +81,7 @@ int init_crypt() int init_crypt() { -// nothing here +// nothing here return 0; } diff --git a/src/key_derivation.c b/src/key_derivation.c index 8e6e95a..a9c4f6d 100644 --- a/src/key_derivation.c +++ b/src/key_derivation.c @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -48,7 +48,7 @@ int key_derivation_init(key_derivation_t* kd, const char* type, role_t role, const char* passphrase, u_int8_t* key, u_int32_t key_len, u_int8_t* salt, u_int32_t salt_len) { - if(!kd) + if(!kd) return -1; kd->role_ = role; @@ -62,7 +62,7 @@ int key_derivation_init(key_derivation_t* kd, const char* type, role_t role, con if(type[7] == 0) { kd->key_length_ = KD_AESCTR_DEFAULT_KEY_LENGTH; } - else if(type[7] != '-') + else if(type[7] != '-') return -1; else { const char* tmp = &type[8]; @@ -127,7 +127,7 @@ int key_derivation_generate_master_key(key_derivation_t* kd, const char* passphr if(kd->master_key_.buf_) { log_printf(WARNING, "master key and passphrase provided, ignoring passphrase"); return 0; - } + } log_printf(NOTICE, "using passphrase to generate master key"); if(!key_length || (key_length % 8)) { @@ -183,7 +183,7 @@ int key_derivation_generate_master_salt(key_derivation_t* kd, const char* passph if(kd->master_salt_.buf_) { log_printf(WARNING, "master salt and passphrase provided, ignoring passphrase"); return 0; - } + } log_printf(NOTICE, "using passphrase to generate master salt"); if(!salt_length || (salt_length % 8)) { @@ -247,7 +247,7 @@ void key_derivation_close(key_derivation_t* kd) int key_derivation_generate(key_derivation_t* kd, key_derivation_dir_t dir, satp_prf_label_t label, seq_nr_t seq_nr, u_int8_t* key, u_int32_t len) { - if(!kd || !key) + if(!kd || !key) return -1; if(label >= LABEL_NIL) { @@ -361,7 +361,7 @@ int key_derivation_aesctr_init(key_derivation_t* kd, const char* passphrase) if(err) { log_printf(ERROR, "failed to open key derivation cipher: %s", gcry_strerror(err)); return -1; - } + } err = gcry_cipher_setkey(params->handle_, kd->master_key_.buf_, kd->master_key_.length_); if(err) { @@ -457,6 +457,6 @@ int key_derivation_aesctr_generate(key_derivation_t* kd, key_derivation_dir_t di memset(key, 0, len); AES_ctr128_encrypt(key, key, len, ¶ms->aes_key_, params->ctr_.buf_, params->ecount_buf_, &num); #endif - + return 0; } diff --git a/src/key_derivation.h b/src/key_derivation.h index d037157..5268faf 100644 --- a/src/key_derivation.h +++ b/src/key_derivation.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * diff --git a/src/linux/tun.c b/src/linux/tun.c index 43370f0..acd823e 100644 --- a/src/linux/tun.c +++ b/src/linux/tun.c @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -59,44 +59,44 @@ #include "sysexec.h" int tun_init(tun_device_t* dev, const char* dev_name, const char* dev_type, const char* ifcfg_addr, u_int16_t ifcfg_prefix){ - if(!dev) + if(!dev) return -1; - + tun_conf(dev, dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 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(ERROR, "can't open device file (%s): %s", DEFAULT_DEVICE, strerror(errno)); tun_close(dev); return -1; } - struct ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); + struct ifreq ifr; + memset(&ifr, 0, sizeof(ifr)); if(dev->type_ == TYPE_TUN) { ifr.ifr_flags = IFF_TUN; dev->with_pi_ = 1; - } + } else if(dev->type_ == TYPE_TAP) { ifr.ifr_flags = IFF_TAP | IFF_NO_PI; dev->with_pi_ = 0; - } + } else { log_printf(ERROR, "unable to recognize type of device (tun or tap)"); tun_close(dev); return -1; } - if(dev_name) - strncpy(ifr.ifr_name, dev_name, IFNAMSIZ); + 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); - } else { + 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(ERROR, "tun/tap device ioctl failed: %s", strerror(errno)); tun_close(dev); return -1; @@ -147,7 +147,7 @@ int tun_read(tun_device_t* dev, u_int8_t* buf, u_int32_t len) { struct iovec iov[2]; struct tun_pi tpi; - + iov[0].iov_base = &tpi; iov[0].iov_len = sizeof(tpi); iov[1].iov_base = buf; @@ -171,13 +171,13 @@ int tun_write(tun_device_t* dev, u_int8_t* buf, u_int32_t len) struct iovec iov[2]; struct tun_pi tpi; struct iphdr *hdr = (struct iphdr *)buf; - + tpi.flags = 0; if(hdr->version == 4) tpi.proto = htons(ETH_P_IP); else tpi.proto = htons(ETH_P_IPV6); - + iov[0].iov_base = &tpi; iov[0].iov_len = sizeof(tpi); iov[1].iov_base = buf; diff --git a/src/log.c b/src/log.c index d789123..5659d3a 100644 --- a/src/log.c +++ b/src/log.c @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -85,7 +85,7 @@ int log_targets_target_exists(log_targets_t* targets, log_target_type_t type) if(tmp->type_ == type) return 1; tmp = tmp->next_; - } + } return 0; } @@ -148,7 +148,7 @@ int log_targets_add(log_targets_t* targets, const char* conf) log_target_t* tmp = targets->first_; while(tmp->next_) tmp = tmp->next_; - + tmp->next_ = new_target; } return 0; @@ -248,7 +248,7 @@ void log_print_hex_dump(log_prio_t prio, const u_int8_t* buf, u_int32_t len) if(offset < 0) return; char* ptr = &msg[offset]; - + for(i=0; i < len; i++) { if(((i+1)*3) >= (MSG_LENGTH_MAX - offset)) break; diff --git a/src/log.h b/src/log.h index 17de487..a87286d 100644 --- a/src/log.h +++ b/src/log.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * diff --git a/src/log_targets.h b/src/log_targets.h index 4a19d8e..babe1d1 100644 --- a/src/log_targets.h +++ b/src/log_targets.h @@ -13,9 +13,9 @@ * 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-2010 Christian Pointner + * + * Copyright (C) 2007-2014 Christian Pointner * * This file is part of uAnytun. * @@ -42,7 +42,7 @@ static char* get_time_formatted() { char* time_string; time_t t = time(NULL); - if(t < 0) + if(t < 0) time_string = "