From 90eb2a6d56a4287410fd5d2717e70d01e80c9a3d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 31 Dec 2008 18:21:29 +0000 Subject: removed useless malloc for cipher_t --- src/cipher.c | 39 +++++++++++++++++---------------------- src/cipher.h | 4 ++-- src/uanytun.c | 12 ++++++------ 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/cipher.c b/src/cipher.c index b3a8480..c9ea45b 100644 --- a/src/cipher.c +++ b/src/cipher.c @@ -44,29 +44,27 @@ #include #include -void cipher_init(cipher_t** c, const char* type) +int cipher_init(cipher_t* c, const char* type) { if(!c) - return; - - *c = malloc(sizeof(cipher_t)); - if(!*c) - return; + return -1; - (*c)->type_ = unknown; + c->type_ = unknown; if(!strcmp(type, "null")) - (*c)->type_ = null; + c->type_ = null; /* else if(!strcmp(type, "aes-ctr")) */ -/* (*c)->type_ = aes_ctr; */ +/* c->type_ = aes_ctr; */ else { log_printf(ERR, "unknown cipher type"); } - (*c)->key_.buf_ = NULL; - (*c)->key_.length_ = 0; + c->key_.buf_ = NULL; + c->key_.length_ = 0; - (*c)->salt_.buf_ = NULL; - (*c)->salt_.length_ = 0; + c->salt_.buf_ = NULL; + c->salt_.length_ = 0; + + return 0; } void cipher_set_key(cipher_t* c, u_int8_t* key, u_int32_t len) @@ -105,18 +103,15 @@ void cipher_set_salt(cipher_t* c, u_int8_t* salt, u_int32_t len) c->salt_.length_ = len; } -void cipher_close(cipher_t** c) +void cipher_close(cipher_t* c) { - if(!c || !(*c)) + if(!c) return; - if((*c)->key_.buf_) - free((*c)->key_.buf_); - if((*c)->salt_.buf_) - free((*c)->salt_.buf_); - - free(*c); - *c = NULL; + if(c->key_.buf_) + free(c->key_.buf_); + if(c->salt_.buf_) + free(c->salt_.buf_); } void cipher_encrypt(cipher_t* c, plain_packet_t* in, encrypted_packet_t* out, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux) diff --git a/src/cipher.h b/src/cipher.h index 0bb3f5f..344b7a0 100644 --- a/src/cipher.h +++ b/src/cipher.h @@ -45,10 +45,10 @@ struct cipher_struct { }; typedef struct cipher_struct cipher_t; -void cipher_init(cipher_t** c, const char* type); +int cipher_init(cipher_t* c, const char* type); void cipher_set_key(cipher_t* c, u_int8_t* key, u_int32_t len); void cipher_set_salt(cipher_t* c, u_int8_t* salt, u_int32_t len); -void cipher_close(cipher_t** c); +void cipher_close(cipher_t* c); void cipher_encrypt(cipher_t* c, plain_packet_t* in, encrypted_packet_t* out, seq_nr_t seq_nr, sender_id_t sender_id, mux_t mux); void cipher_decrypt(cipher_t* c, encrypted_packet_t* in, plain_packet_t* out); diff --git a/src/uanytun.c b/src/uanytun.c index 1225690..c2f5585 100644 --- a/src/uanytun.c +++ b/src/uanytun.c @@ -69,11 +69,11 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt) seq_nr_t seq_nr = 0; fd_set readfds; - cipher_t* c; - cipher_init(&c, opt->cipher_); - if(!c) { + cipher_t c; + int ret = cipher_init(&c, opt->cipher_); + if(ret) { log_printf(ERR, "could not initialize cipher of type %s", opt->cipher_); - return_value -1; + return_value = ret; } seq_win_t* seq_win; @@ -122,7 +122,7 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt) else plain_packet_set_type(&plain_packet, PAYLOAD_TYPE_UNKNOWN); - cipher_encrypt(c, &plain_packet, &encrypted_packet, seq_nr, opt->sender_id_, opt->mux_); + cipher_encrypt(&c, &plain_packet, &encrypted_packet, seq_nr, opt->sender_id_, opt->mux_); seq_nr++; // TODO: add auth-tag @@ -165,7 +165,7 @@ int main_loop(tun_device_t* dev, udp_socket_t* sock, options_t* opt) free(addrstring); } - cipher_decrypt(c, &encrypted_packet, &plain_packet); + cipher_decrypt(&c, &encrypted_packet, &plain_packet); len = tun_write(dev, plain_packet_get_payload(&plain_packet), plain_packet_get_payload_length(&plain_packet)); if(len == -1) -- cgit v1.2.3