summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2014-08-02 00:53:47 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2014-08-02 00:53:47 +0200
commit546ac7936340312cf272969ff83317ae4d50d2b4 (patch)
tree7ecadca11430c8624d9f80aae7c348fa4d65b969
parentb22364f4af3564f0dd9a5f4e150bb09747bd5c4e (diff)
downloadfastd-546ac7936340312cf272969ff83317ae4d50d2b4.tar
fastd-546ac7936340312cf272969ff83317ae4d50d2b4.zip
Introduce and use alloc helpers
These new helpers will terminate fastd on allocation failures and add some additional convenience (allow strdup with NULL; typesafe new(type) macros).
-rw-r--r--src/alloc.h122
-rw-r--r--src/config.c22
-rw-r--r--src/config.y28
-rw-r--r--src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c3
-rw-r--r--src/crypto/cipher/salsa20/nacl/salsa20_nacl.c3
-rw-r--r--src/crypto/cipher/salsa20/xmm/salsa20_xmm.c3
-rw-r--r--src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c3
-rw-r--r--src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c3
-rw-r--r--src/fastd.c2
-rw-r--r--src/fastd.h8
-rw-r--r--src/handshake.c4
-rw-r--r--src/lex.c8
-rw-r--r--src/log.h1
-rw-r--r--src/methods/cipher_test/cipher_test.c4
-rw-r--r--src/methods/composed_gmac/composed_gmac.c4
-rw-r--r--src/methods/generic_gmac/generic_gmac.c4
-rw-r--r--src/methods/generic_poly1305/generic_poly1305.c4
-rw-r--r--src/methods/null/null.c2
-rw-r--r--src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c2
-rw-r--r--src/options.c16
-rw-r--r--src/peer.c8
-rw-r--r--src/peer_hashtable.c2
-rw-r--r--src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c4
-rw-r--r--src/protocols/ec25519_fhmqvc/handshake.c2
-rw-r--r--src/protocols/ec25519_fhmqvc/state.c4
-rw-r--r--src/resolve.c4
-rw-r--r--src/shell.c4
-rw-r--r--src/shell.h4
-rw-r--r--src/socket.c4
-rw-r--r--src/tuntap.c8
-rw-r--r--src/vector.c5
-rw-r--r--src/verify.c2
32 files changed, 213 insertions, 84 deletions
diff --git a/src/alloc.h b/src/alloc.h
new file mode 100644
index 0000000..7b3e93a
--- /dev/null
+++ b/src/alloc.h
@@ -0,0 +1,122 @@
+/*
+ Copyright (c) 2012-2014, Matthias Schiffer <mschiffer@universe-factory.net>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+ \file
+
+ \em memory allocation functions
+ */
+
+
+#pragma once
+
+#include "log.h"
+
+
+/**
+ Allocates a block of uninitialized memory on the heap
+
+ Terminates the process on failure.
+*/
+static inline void * fastd_alloc(size_t size) {
+ void *ret = malloc(size);
+ if (!ret)
+ exit_errno("malloc");
+
+ return ret;
+}
+
+/**
+ Allocates a block of memory set to zero for an array on the heap
+
+ Terminates the process on failure.
+*/
+static inline void * fastd_alloc0_array(size_t members, size_t size) {
+ void *ret = calloc(members, size);
+ if (!ret)
+ exit_errno("calloc");
+
+ return ret;
+}
+
+/**
+ Allocates a block of memory set to zero on the heap
+
+ Terminates the process on failure.
+*/
+static inline void * fastd_alloc0(size_t size) {
+ return fastd_alloc0_array(1, size);
+}
+
+/**
+ Reallocates a block of memory on the heap
+
+ Terminates the process on failure.
+*/
+static inline void * fastd_realloc(void *ptr, size_t size) {
+ void *ret = realloc(ptr, size);
+ if (!ret)
+ exit_errno("realloc");
+
+ return ret;
+}
+
+
+/** Allocates a block of uninitialized memory in the size of a given type */
+#define fastd_new(type) ((type *)fastd_alloc(sizeof(type)))
+
+/** Allocates a block of memory set to zero in the size of a given type */
+#define fastd_new0(type) ((type *)fastd_alloc0(sizeof(type)))
+
+/** Allocates a block of undefined memory for an array of elements of a given type */
+#define fastd_new_array(members, type) ((type *)fastd_alloc(members * sizeof(type)))
+
+/** Allocates a block of memory set to zero for an array of elements of a given type */
+#define fastd_new0_array(members, type) ((type *)fastd_alloc0_array(members, sizeof(type)))
+
+
+/** Duplicates a string (string may be NULL) */
+static inline char * fastd_strdup(const char *s) {
+ if (!s)
+ return NULL;
+
+ char *ret = strdup(s);
+ if (!ret)
+ exit_errno("strdup");
+
+ return ret;
+}
+
+/** Duplicates a string up to a maximum length (string may be NULL) */
+static inline char * fastd_strndup(const char *s, size_t n) {
+ if (!s)
+ return NULL;
+
+ char *ret = strndup(s, n);
+ if (!ret)
+ exit_errno("strndup");
+
+ return ret;
+}
diff --git a/src/config.c b/src/config.c
index 343823b..f2a6dab 100644
--- a/src/config.c
+++ b/src/config.c
@@ -58,7 +58,7 @@ extern const fastd_protocol_t fastd_protocol_ec25519_fhmqvc;
/** Initializes the global configuration with default values */
static void default_config(void) {
- conf.log_syslog_ident = strdup("fastd");
+ conf.log_syslog_ident = fastd_strdup("fastd");
conf.mtu = 1500;
conf.mode = MODE_TAP;
@@ -68,8 +68,8 @@ static void default_config(void) {
conf.protocol = &fastd_protocol_ec25519_fhmqvc;
- conf.peer_group = calloc(1, sizeof(fastd_peer_group_t));
- conf.peer_group->name = strdup("default");
+ conf.peer_group = fastd_new0(fastd_peer_group_t);
+ conf.peer_group->name = fastd_strdup("default");
conf.peer_group->max_connections = -1;
}
@@ -125,13 +125,13 @@ void fastd_config_bind_address(const fastd_peer_address_t *address, const char *
}
#endif
- fastd_bind_address_t *addr = malloc(sizeof(fastd_bind_address_t));
+ fastd_bind_address_t *addr = fastd_new(fastd_bind_address_t);
addr->next = conf.bind_addrs;
conf.bind_addrs = addr;
conf.n_bind_addrs++;
addr->addr = *address;
- addr->bindtodev = bindtodev ? strdup(bindtodev) : NULL;
+ addr->bindtodev = fastd_strdup(bindtodev);
fastd_peer_address_simplify(&addr->addr);
@@ -144,8 +144,8 @@ void fastd_config_bind_address(const fastd_peer_address_t *address, const char *
/** Handles the start of a peer group configuration */
void fastd_config_peer_group_push(fastd_parser_state_t *state, const char *name) {
- fastd_peer_group_t *group = calloc(1, sizeof(fastd_peer_group_t));
- group->name = strdup(name);
+ fastd_peer_group_t *group = fastd_new0(fastd_peer_group_t);
+ group->name = fastd_strdup(name);
group->max_connections = -1;
group->parent = state->peer_group;
@@ -222,7 +222,7 @@ static void read_peer_dir(fastd_peer_config_t **peers, fastd_peer_group_t *group
}
fastd_peer_config_t *peer = fastd_peer_config_new(group);
- peer->name = strdup(result->d_name);
+ peer->name = fastd_strdup(result->d_name);
peer->config_source_dir = dir;
if (fastd_config_read(result->d_name, group, peer, 0)) {
@@ -312,7 +312,7 @@ bool fastd_config_read(const char *filename, fastd_peer_group_t *peer_group, fas
lex = fastd_lex_init(file);
if (filename) {
- filename2 = strdup(filename);
+ filename2 = fastd_strdup(filename);
dir = dirname(filename2);
if (chdir(dir)) {
@@ -439,7 +439,7 @@ static void configure_user(void) {
if (getgrouplist(conf.user, conf.gid, NULL, &ngroups) < 0) {
/* the user has supplementary groups */
- conf.groups = calloc(ngroups, sizeof(gid_t));
+ conf.groups = fastd_new0_array(ngroups, gid_t);
if (getgrouplist(conf.user, conf.gid, conf.groups, &ngroups) < 0)
exit_errno("getgrouplist");
@@ -480,7 +480,7 @@ static void configure_methods(void) {
for (method_name = conf.method_list; method_name; method_name = method_name->next)
n_methods++;
- conf.methods = calloc(n_methods+1, sizeof(fastd_method_info_t));
+ conf.methods = fastd_new0_array(n_methods+1, fastd_method_info_t);
for (i = 0, method_name = conf.method_list; method_name; i++, method_name = method_name->next) {
conf.methods[i].name = method_name->str;
diff --git a/src/config.y b/src/config.y
index a2e14e3..3d4e96c 100644
--- a/src/config.y
+++ b/src/config.y
@@ -211,12 +211,12 @@ peer_group_statement:
user: TOK_STRING {
free(conf.user);
- conf.user = strdup($1->str);
+ conf.user = fastd_strdup($1->str);
}
group: TOK_STRING {
free(conf.group);
- conf.group = strdup($1->str);
+ conf.group = fastd_strdup($1->str);
}
drop_capabilities:
@@ -260,7 +260,7 @@ log: TOK_LEVEL log_level {
}
| TOK_TO TOK_SYSLOG TOK_AS TOK_STRING maybe_log_level {
free(conf.log_syslog_ident);
- conf.log_syslog_ident = strdup($4->str);
+ conf.log_syslog_ident = fastd_strdup($4->str);
conf.log_syslog_level = $5;
}
@@ -288,7 +288,7 @@ log_level: TOK_FATAL { $$ = LL_FATAL; }
| TOK_DEBUG2 { $$ = LL_DEBUG2; }
;
-interface: TOK_STRING { free(conf.ifname); conf.ifname = strdup($1->str); }
+interface: TOK_STRING { free(conf.ifname); conf.ifname = fastd_strdup($1->str); }
;
bind: bind_address maybe_bind_interface maybe_bind_default {
@@ -373,7 +373,7 @@ method: TOK_STRING {
}
;
-secret: TOK_STRING { free(conf.secret); conf.secret = strdup($1->str); }
+secret: TOK_STRING { free(conf.secret); conf.secret = fastd_strdup($1->str); }
;
on_pre_up: sync_def_sync TOK_STRING {
@@ -423,7 +423,7 @@ on_verify: sync_def_async TOK_STRING {
peer: TOK_STRING {
fastd_peer_config_t *peer = fastd_peer_config_new(state->peer_group);
- peer->name = strdup($1->str);
+ peer->name = fastd_strdup($1->str);
peer->next = conf.peers;
conf.peers = peer;
@@ -446,7 +446,7 @@ peer_remote: TOK_ADDR4 port {
while (*remote)
remote = &(*remote)->next;
- *remote = calloc(1, sizeof(fastd_remote_config_t));
+ *remote = fastd_new0(fastd_remote_config_t);
(*remote)->address.in.sin_family = AF_INET;
(*remote)->address.in.sin_addr = $1;
@@ -458,7 +458,7 @@ peer_remote: TOK_ADDR4 port {
while (*remote)
remote = &(*remote)->next;
- *remote = calloc(1, sizeof(fastd_remote_config_t));
+ *remote = fastd_new0(fastd_remote_config_t);
(*remote)->address.in6.sin6_family = AF_INET6;
(*remote)->address.in6.sin6_addr = $1;
@@ -475,9 +475,9 @@ peer_remote: TOK_ADDR4 port {
inet_ntop(AF_INET6, &$1.addr, addrbuf, sizeof(addrbuf));
addrlen = strlen(addrbuf);
- *remote = calloc(1, sizeof(fastd_remote_config_t));
+ *remote = fastd_new0(fastd_remote_config_t);
- (*remote)->hostname = malloc(addrlen + strlen($1.ifname) + 2);
+ (*remote)->hostname = fastd_alloc(addrlen + strlen($1.ifname) + 2);
memcpy((*remote)->hostname, addrbuf, addrlen);
(*remote)->hostname[addrlen] = '%';
strcpy((*remote)->hostname+addrlen+1, $1.ifname);
@@ -490,9 +490,9 @@ peer_remote: TOK_ADDR4 port {
while (*remote)
remote = &(*remote)->next;
- *remote = calloc(1, sizeof(fastd_remote_config_t));
+ *remote = fastd_new0(fastd_remote_config_t);
- (*remote)->hostname = strdup($2->str);
+ (*remote)->hostname = fastd_strdup($2->str);
(*remote)->address.sa.sa_family = $1;
(*remote)->address.in.sin_port = htons($3);
}
@@ -504,7 +504,7 @@ peer_float: boolean {
;
peer_key: TOK_STRING {
- free(state->peer->key); state->peer->key = strdup($1->str);
+ free(state->peer->key); state->peer->key = fastd_strdup($1->str);
}
;
@@ -543,7 +543,7 @@ forward: boolean { conf.forward = $1; }
include: TOK_PEER TOK_STRING maybe_as {
fastd_peer_config_t *peer = fastd_peer_config_new(state->peer_group);
if ($3)
- peer->name = strdup($3->str);
+ peer->name = fastd_strdup($3->str);
if (!fastd_config_read($2->str, state->peer_group, peer, state->depth))
YYERROR;
diff --git a/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c b/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c
index 0bf4bbc..cbf1ed5 100644
--- a/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c
+++ b/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c
@@ -30,6 +30,7 @@
*/
+#include "../../../../alloc.h"
#include "../../../../crypto.h"
#include <openssl/evp.h>
@@ -43,7 +44,7 @@ struct fastd_cipher_state {
/** Initializes the cipher state */
static fastd_cipher_state_t* aes128_ctr_init(const uint8_t *key) {
- fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t));
+ fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t);
state->aes = EVP_CIPHER_CTX_new();
EVP_EncryptInit(state->aes, EVP_aes_128_ctr(), (const unsigned char*)key, NULL);
diff --git a/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c b/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c
index 6179bc2..b9c2175 100644
--- a/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c
+++ b/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c
@@ -30,6 +30,7 @@
*/
+#include "../../../../alloc.h"
#include "../../../../crypto.h"
#include <crypto_stream_salsa20.h>
@@ -43,7 +44,7 @@ struct fastd_cipher_state {
/** Initializes the cipher state */
static fastd_cipher_state_t* salsa20_init(const uint8_t *key) {
- fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t));
+ fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t);
memcpy(state->key, key, crypto_stream_salsa20_KEYBYTES);
return state;
diff --git a/src/crypto/cipher/salsa20/xmm/salsa20_xmm.c b/src/crypto/cipher/salsa20/xmm/salsa20_xmm.c
index a85ed72..abf717f 100644
--- a/src/crypto/cipher/salsa20/xmm/salsa20_xmm.c
+++ b/src/crypto/cipher/salsa20/xmm/salsa20_xmm.c
@@ -33,6 +33,7 @@
*/
+#include "../../../../alloc.h"
#include "../../../../crypto.h"
#include "../../../../cpuid.h"
@@ -67,7 +68,7 @@ static bool salsa20_available(void) {
/** Initializes the cipher state */
static fastd_cipher_state_t* salsa20_init(const uint8_t *key) {
- fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t));
+ fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t);
memcpy(state->key, key, KEYBYTES);
return state;
diff --git a/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c b/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c
index 18ec502..24450dc 100644
--- a/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c
+++ b/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c
@@ -30,6 +30,7 @@
*/
+#include "../../../../alloc.h"
#include "../../../../crypto.h"
#include <crypto_stream_salsa2012.h>
@@ -43,7 +44,7 @@ struct fastd_cipher_state {
/** Initializes the cipher state */
static fastd_cipher_state_t* salsa2012_init(const uint8_t *key) {
- fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t));
+ fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t);
memcpy(state->key, key, crypto_stream_salsa2012_KEYBYTES);
return state;
diff --git a/src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c b/src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c
index 7e6fe80..6180702 100644
--- a/src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c
+++ b/src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c
@@ -33,6 +33,7 @@
*/
+#include "../../../../alloc.h"
#include "../../../../crypto.h"
#include "../../../../cpuid.h"
@@ -67,7 +68,7 @@ static bool salsa2012_available(void) {
/** Initializes the cipher state */
static fastd_cipher_state_t* salsa2012_init(const uint8_t *key) {
- fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t));
+ fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t);
memcpy(state->key, key, KEYBYTES);
return state;
diff --git a/src/fastd.c b/src/fastd.c
index e0d4bb2..b6e7484 100644
--- a/src/fastd.c
+++ b/src/fastd.c
@@ -151,7 +151,7 @@ static inline void close_log(void) {
/** Initializes the configured sockets */
static void init_sockets(void) {
- ctx.socks = malloc(conf.n_bind_addrs * sizeof(fastd_socket_t));
+ ctx.socks = fastd_new_array(conf.n_bind_addrs, fastd_socket_t);
size_t i;
fastd_bind_address_t *addr = conf.bind_addrs;
diff --git a/src/fastd.h b/src/fastd.h
index 8700be2..5619e66 100644
--- a/src/fastd.h
+++ b/src/fastd.h
@@ -32,7 +32,7 @@
#pragma once
-#include "types.h"
+#include "alloc.h"
#include "dlist.h"
#include "buffer.h"
#include "log.h"
@@ -395,7 +395,7 @@ static inline bool fastd_peer_address_is_v6_ll(const fastd_peer_address_t *addr)
/** Duplicates a string, creating a one-element string stack */
static inline fastd_string_stack_t* fastd_string_stack_dup(const char *str) {
- fastd_string_stack_t *ret = malloc(alignto(sizeof(fastd_string_stack_t) + strlen(str) + 1, 8));
+ fastd_string_stack_t *ret = fastd_alloc(alignto(sizeof(fastd_string_stack_t) + strlen(str) + 1, 8));
ret->next = NULL;
strcpy(ret->str, str);
@@ -405,7 +405,7 @@ static inline fastd_string_stack_t* fastd_string_stack_dup(const char *str) {
/** Duplicates a string of a given maximum length, creating a one-element string stack */
static inline fastd_string_stack_t* fastd_string_stack_dupn(const char *str, size_t len) {
size_t str_len = strnlen(str, len);
- fastd_string_stack_t *ret = malloc(alignto(sizeof(fastd_string_stack_t) + str_len + 1, 8));
+ fastd_string_stack_t *ret = fastd_alloc(alignto(sizeof(fastd_string_stack_t) + str_len + 1, 8));
ret->next = NULL;
strncpy(ret->str, str, str_len);
ret->str[str_len] = 0;
@@ -415,7 +415,7 @@ static inline fastd_string_stack_t* fastd_string_stack_dupn(const char *str, siz
/** Pushes the copy of a string onto the top of a string stack */
static inline fastd_string_stack_t* fastd_string_stack_push(fastd_string_stack_t *stack, const char *str) {
- fastd_string_stack_t *ret = malloc(alignto(sizeof(fastd_string_stack_t) + strlen(str) + 1, 8));
+ fastd_string_stack_t *ret = fastd_alloc(alignto(sizeof(fastd_string_stack_t) + strlen(str) + 1, 8));
ret->next = stack;
strcpy(ret->str, str);
diff --git a/src/handshake.c b/src/handshake.c
index e8b2197..23c9f4e 100644
--- a/src/handshake.c
+++ b/src/handshake.c
@@ -72,7 +72,7 @@ static uint8_t* create_method_list(size_t *len) {
for (i = 0; conf.methods[i].name; i++)
*len += strlen(conf.methods[i].name) + 1;
- uint8_t *ret = malloc(*len);
+ uint8_t *ret = fastd_alloc(*len);
(*len)--;
char *ptr = (char*)ret;
@@ -364,7 +364,7 @@ void fastd_handshake_handle(fastd_socket_t *sock, const fastd_peer_address_t *lo
method = get_method(&handshake);
if (handshake.records[RECORD_VERSION_NAME].data)
- handshake.peer_version = peer_version = strndup((const char*)handshake.records[RECORD_VERSION_NAME].data, handshake.records[RECORD_VERSION_NAME].length);
+ handshake.peer_version = peer_version = fastd_strndup((const char*)handshake.records[RECORD_VERSION_NAME].data, handshake.records[RECORD_VERSION_NAME].length);
}
if (handshake.type > 1 && !method) {
diff --git a/src/lex.c b/src/lex.c
index 47f6847..1586538 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -159,7 +159,7 @@ static inline char current(fastd_lex_t *lex) {
/** Returns the current token as a newly allocated string */
static char* get_token(fastd_lex_t *lex) {
- return strndup(lex->buffer+lex->start, lex->tok_len);
+ return fastd_strndup(lex->buffer+lex->start, lex->tok_len);
}
/** Tries to add the next character to the current token */
@@ -249,7 +249,7 @@ static int parse_string(YYSTYPE *yylval, YYLTYPE *yylloc, fastd_lex_t *lex) {
if (lex->needspace)
return syntax_error(yylval, lex);
- buf = malloc(len);
+ buf = fastd_alloc(len);
while (true) {
if (!next(yylloc, lex, true)) {
@@ -276,7 +276,7 @@ static int parse_string(YYSTYPE *yylval, YYLTYPE *yylloc, fastd_lex_t *lex) {
if (pos >= len) {
len *= 2;
- buf = realloc(buf, len);
+ buf = fastd_realloc(buf, len);
}
buf[pos++] = cur;
@@ -444,7 +444,7 @@ static int parse_keyword(YYSTYPE *yylval, YYLTYPE *yylloc, fastd_lex_t *lex) {
/** Initializes a new scanner for the given file */
fastd_lex_t* fastd_lex_init(FILE *file) {
- fastd_lex_t *lex = calloc(1, sizeof(fastd_lex_t));
+ fastd_lex_t *lex = fastd_new0(fastd_lex_t);
lex->file = file;
advance(lex);
diff --git a/src/log.h b/src/log.h
index 7d4ea41..6cf1487 100644
--- a/src/log.h
+++ b/src/log.h
@@ -34,6 +34,7 @@
#include "types.h"
+#include <errno.h>
#include <stdlib.h>
#include <string.h>
diff --git a/src/methods/cipher_test/cipher_test.c b/src/methods/cipher_test/cipher_test.c
index 7e064ba..277671a 100644
--- a/src/methods/cipher_test/cipher_test.c
+++ b/src/methods/cipher_test/cipher_test.c
@@ -72,7 +72,7 @@ static bool method_create_by_name(const char *name, fastd_method_t **method) {
if (!m.cipher_info)
return false;
- *method = malloc(sizeof(fastd_method_t));
+ *method = fastd_new(fastd_method_t);
**method = m;
return true;
@@ -90,7 +90,7 @@ static size_t method_key_length(const fastd_method_t *method) {
/** Initializes a session */
static fastd_method_session_state_t* method_session_init(const fastd_method_t *method, const uint8_t *secret, bool initiator) {
- fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t));
+ fastd_method_session_state_t *session = fastd_new(fastd_method_session_state_t);
fastd_method_common_init(&session->common, initiator);
session->method = method;
diff --git a/src/methods/composed_gmac/composed_gmac.c b/src/methods/composed_gmac/composed_gmac.c
index 6636e49..ead3cb0 100644
--- a/src/methods/composed_gmac/composed_gmac.c
+++ b/src/methods/composed_gmac/composed_gmac.c
@@ -115,7 +115,7 @@ static bool method_create_by_name(const char *name, fastd_method_t **method) {
if (m.gmac_cipher_info->iv_length <= COMMON_NONCEBYTES)
return false;
- *method = malloc(sizeof(fastd_method_t));
+ *method = fastd_new(fastd_method_t);
**method = m;
return true;
@@ -133,7 +133,7 @@ static size_t method_key_length(const fastd_method_t *method) {
/** Initializes a session */
static fastd_method_session_state_t* method_session_init(const fastd_method_t *method, const uint8_t *secret, bool initiator) {
- fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t));
+ fastd_method_session_state_t *session = fastd_new(fastd_method_session_state_t);
fastd_method_common_init(&session->common, initiator);
session->method = method;
diff --git a/src/methods/generic_gmac/generic_gmac.c b/src/methods/generic_gmac/generic_gmac.c
index 546cd50..930e52d 100644
--- a/src/methods/generic_gmac/generic_gmac.c
+++ b/src/methods/generic_gmac/generic_gmac.c
@@ -90,7 +90,7 @@ static bool method_create_by_name(const char *name, fastd_method_t **method) {
if (m.cipher_info->iv_length <= COMMON_NONCEBYTES)
return false;
- *method = malloc(sizeof(fastd_method_t));
+ *method = fastd_new(fastd_method_t);
**method = m;
return true;
@@ -108,7 +108,7 @@ static size_t method_key_length(const fastd_method_t *method) {
/** Initializes a session */
static fastd_method_session_state_t* method_session_init(const fastd_method_t *method, const uint8_t *secret, bool initiator) {
- fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t));
+ fastd_method_session_state_t *session = fastd_new(fastd_method_session_state_t);
fastd_method_common_init(&session->common, initiator);
session->method = method;
diff --git a/src/methods/generic_poly1305/generic_poly1305.c b/src/methods/generic_poly1305/generic_poly1305.c
index 142b50e..f804ba3 100644
--- a/src/methods/generic_poly1305/generic_poly1305.c
+++ b/src/methods/generic_poly1305/generic_poly1305.c
@@ -84,7 +84,7 @@ static bool method_create_by_name(const char *name, fastd_method_t **method) {
if (m.cipher_info->iv_length <= COMMON_NONCEBYTES)
return false;
- *method = malloc(sizeof(fastd_method_t));
+ *method = fastd_new(fastd_method_t);
**method = m;
return true;
@@ -102,7 +102,7 @@ static size_t method_key_length(const fastd_method_t *method) {
/** Initializes a session */
static fastd_method_session_state_t* method_session_init(const fastd_method_t *method, const uint8_t *secret, bool initiator) {
- fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t));
+ fastd_method_session_state_t *session = fastd_new(fastd_method_session_state_t);
fastd_method_common_init(&session->common, initiator);
session->method = method;
diff --git a/src/methods/null/null.c b/src/methods/null/null.c
index 3062fcf..71191de 100644
--- a/src/methods/null/null.c
+++ b/src/methods/null/null.c
@@ -55,7 +55,7 @@ static size_t method_key_length(const fastd_method_t *method UNUSED) {
/** Initiates a new null session */
static fastd_method_session_state_t* method_session_init(const fastd_method_t *method UNUSED, const uint8_t *secret UNUSED, bool initiator) {
- fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t));
+ fastd_method_session_state_t *session = fastd_new(fastd_method_session_state_t);
session->valid = true;
session->initiator = initiator;
diff --git a/src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c b/src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c
index 9f2d61c..5a4966e 100644
--- a/src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c
+++ b/src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c
@@ -65,7 +65,7 @@ static size_t method_key_length(const fastd_method_t *method UNUSED) {
/** Initializes the session state */
static fastd_method_session_state_t* method_session_init(const fastd_method_t *method UNUSED, const uint8_t *secret, bool initiator) {
- fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t));
+ fastd_method_session_state_t *session = fastd_new(fastd_method_session_state_t);
fastd_method_common_init(&session->common, initiator);
diff --git a/src/options.c b/src/options.c
index 63115ef..c3c9844 100644
--- a/src/options.c
+++ b/src/options.c
@@ -102,7 +102,7 @@ static void option_daemon(void) {
/** Handles the --pid-file option */
static void option_pid_file(const char *arg) {
free(conf.pid_file);
- conf.pid_file = strdup(arg);
+ conf.pid_file = fastd_strdup(arg);
}
@@ -138,13 +138,13 @@ static void option_config_peer_dir(const char *arg) {
/** Handles the --config-user option */
static void option_user(const char *arg) {
free(conf.user);
- conf.user = strdup(arg);
+ conf.user = fastd_strdup(arg);
}
/** Handles the --config-group option */
static void option_group(const char *arg) {
free(conf.group);
- conf.group = strdup(arg);
+ conf.group = fastd_strdup(arg);
}
#endif
@@ -184,7 +184,7 @@ static void option_syslog_level(const char *arg) {
/** Handles the --syslog-ident option */
static void option_syslog_ident(const char *arg) {
free(conf.log_syslog_ident);
- conf.log_syslog_ident = strdup(arg);
+ conf.log_syslog_ident = fastd_strdup(arg);
}
/** Handles the --hide-ip-addresses option */
@@ -214,7 +214,7 @@ static void option_mode(const char *arg) {
/** Handles the --interface option */
static void option_interface(const char *arg) {
free(conf.ifname);
- conf.ifname = strdup(arg);
+ conf.ifname = fastd_strdup(arg);
}
/** Handles the --mtu option */
@@ -241,7 +241,7 @@ static void option_bind(const char *arg) {
if (!charptr || (charptr[1] != ':' && charptr[1] != '\0'))
exit_error("invalid bind address `%s'", arg);
- addrstr = strndup(arg+1, charptr-arg-1);
+ addrstr = fastd_strndup(arg+1, charptr-arg-1);
if (charptr[1] == ':')
charptr++;
@@ -251,10 +251,10 @@ static void option_bind(const char *arg) {
else {
charptr = strrchr(arg, ':');
if (charptr) {
- addrstr = strndup(arg, charptr-arg);
+ addrstr = fastd_strndup(arg, charptr-arg);
}
else {
- addrstr = strdup(arg);
+ addrstr = fastd_strdup(arg);
}
}
diff --git a/src/peer.c b/src/peer.c
index 9bec0f0..8ff40dc 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -350,7 +350,7 @@ static void init_handshake(fastd_peer_t *peer) {
/** Handles an asynchronous DNS resolve response */
void fastd_peer_handle_resolve(fastd_peer_t *peer, fastd_remote_t *remote, size_t n_addresses, const fastd_peer_address_t *addresses) {
free(remote->addresses);
- remote->addresses = malloc(n_addresses*sizeof(fastd_peer_address_t));
+ remote->addresses = fastd_new_array(n_addresses, fastd_peer_address_t);
memcpy(remote->addresses, addresses, n_addresses*sizeof(fastd_peer_address_t));
remote->n_addresses = n_addresses;
@@ -432,7 +432,7 @@ static void delete_peer(fastd_peer_t *peer) {
/** Allocates a new peer config */
fastd_peer_config_t* fastd_peer_config_new(fastd_peer_group_t *group) {
- fastd_peer_config_t *peer = calloc(1, sizeof(fastd_peer_config_t));
+ fastd_peer_config_t *peer = fastd_new0(fastd_peer_config_t);
peer->group = group;
return peer;
@@ -707,7 +707,7 @@ bool fastd_peer_may_connect(fastd_peer_t *peer) {
/** Create a new peer */
fastd_peer_t* fastd_peer_add(fastd_peer_config_t *peer_conf) {
- fastd_peer_t *peer = calloc(1, sizeof(fastd_peer_t));
+ fastd_peer_t *peer = fastd_new0(fastd_peer_t);
peer->id = ctx.next_peer_id++;
@@ -723,7 +723,7 @@ fastd_peer_t* fastd_peer_add(fastd_peer_config_t *peer_conf) {
if (!remote_config->hostname) {
remote.n_addresses = 1;
- remote.addresses = malloc(sizeof(fastd_peer_address_t));
+ remote.addresses = fastd_new(fastd_peer_address_t);
remote.addresses[0] = remote_config->address;
}
diff --git a/src/peer_hashtable.c b/src/peer_hashtable.c
index 100e8a3..b835ea9 100644
--- a/src/peer_hashtable.c
+++ b/src/peer_hashtable.c
@@ -44,7 +44,7 @@
void fastd_peer_hashtable_init(void) {
fastd_random_bytes(&ctx.peer_addr_ht_seed, sizeof(ctx.peer_addr_ht_seed), false);
- ctx.peer_addr_ht = malloc(sizeof(*ctx.peer_addr_ht) * PEER_ADDR_HT_SIZE);
+ ctx.peer_addr_ht = fastd_new_array(PEER_ADDR_HT_SIZE, __typeof__(*ctx.peer_addr_ht));
size_t i;
for (i = 0; i < PEER_ADDR_HT_SIZE; i++)
diff --git a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c
index 86acbec..f9bf1f7 100644
--- a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c
+++ b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c
@@ -59,7 +59,7 @@ static inline void check_session_refresh(fastd_peer_t *peer) {
/** Initializes the protocol-specific configuration */
static fastd_protocol_config_t* protocol_init(void) {
- fastd_protocol_config_t *protocol_config = malloc(sizeof(fastd_protocol_config_t));
+ fastd_protocol_config_t *protocol_config = fastd_new(fastd_protocol_config_t);
if (!conf.secret)
exit_error("no secret key configured");
@@ -100,7 +100,7 @@ static void protocol_peer_configure(fastd_peer_config_t *peer_conf) {
return;
}
- peer_conf->protocol_config = malloc(sizeof(fastd_protocol_peer_config_t));
+ peer_conf->protocol_config = fastd_new(fastd_protocol_peer_config_t);
peer_conf->protocol_config->public_key = key;
if (memcmp(&peer_conf->protocol_config->public_key, &conf.protocol_config->key.public, PUBLICKEYBYTES) == 0)
diff --git a/src/protocols/ec25519_fhmqvc/handshake.c b/src/protocols/ec25519_fhmqvc/handshake.c
index 99f1829..9a1c8de 100644
--- a/src/protocols/ec25519_fhmqvc/handshake.c
+++ b/src/protocols/ec25519_fhmqvc/handshake.c
@@ -580,7 +580,7 @@ static fastd_peer_t * add_temporary(fastd_socket_t *sock, const fastd_peer_addre
fastd_peer_t *peer = fastd_peer_add(NULL);
- peer->protocol_config = malloc(sizeof(fastd_protocol_peer_config_t));
+ peer->protocol_config = fastd_new(fastd_protocol_peer_config_t);
memcpy(&peer->protocol_config->public_key, key, PUBLICKEYBYTES);
/* Ugly hack */
diff --git a/src/protocols/ec25519_fhmqvc/state.c b/src/protocols/ec25519_fhmqvc/state.c
index 451e31d..d4a2a0e 100644
--- a/src/protocols/ec25519_fhmqvc/state.c
+++ b/src/protocols/ec25519_fhmqvc/state.c
@@ -37,7 +37,7 @@
/** Allocates the protocol-specific state */
static void init_protocol_state(void) {
if (!ctx.protocol_state) {
- ctx.protocol_state = calloc(1, sizeof(fastd_protocol_state_t));
+ ctx.protocol_state = fastd_new0(fastd_protocol_state_t);
ctx.protocol_state->prev_handshake_key.preferred_till = ctx.now;
ctx.protocol_state->handshake_key.preferred_till = ctx.now;
@@ -84,7 +84,7 @@ void fastd_protocol_ec25519_fhmqvc_init_peer_state(fastd_peer_t *peer) {
if (peer->protocol_state)
exit_bug("tried to reinit peer state");
- peer->protocol_state = calloc(1, sizeof(fastd_protocol_peer_state_t));
+ peer->protocol_state = fastd_new0(fastd_protocol_peer_state_t);
peer->protocol_state->last_serial = ctx.protocol_state->handshake_key.serial;
}
diff --git a/src/resolve.c b/src/resolve.c
index 1020253..4ed2faf 100644
--- a/src/resolve.c
+++ b/src/resolve.c
@@ -125,11 +125,11 @@ void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote) {
remote->last_resolve_timeout = fastd_in_seconds(MIN_RESOLVE_INTERVAL);
- resolv_arg_t *arg = malloc(sizeof(resolv_arg_t));
+ resolv_arg_t *arg = fastd_new(resolv_arg_t);
arg->peer_id = peer->id;
arg->remote = remote - VECTOR_DATA(peer->remotes);
- arg->hostname = strdup(remote->config->hostname);
+ arg->hostname = fastd_strdup(remote->config->hostname);
arg->constraints = remote->config->address;
pthread_t thread;
diff --git a/src/shell.c b/src/shell.c
index 6fba92a..87aa320 100644
--- a/src/shell.c
+++ b/src/shell.c
@@ -54,7 +54,7 @@ struct fastd_shell_env {
/** Allocated a new shell environment */
fastd_shell_env_t * fastd_shell_env_alloc(void) {
- fastd_shell_env_t *env = malloc(sizeof(fastd_shell_env_t));
+ fastd_shell_env_t *env = fastd_new(fastd_shell_env_t);
VECTOR_ALLOC(env->entries, 0);
return env;
@@ -62,7 +62,7 @@ fastd_shell_env_t * fastd_shell_env_alloc(void) {
/** Sets a variable in a shell environment */
void fastd_shell_env_set(fastd_shell_env_t *env, const char *key, const char *value) {
- shell_env_entry_t entry = {.key = key, .value = value ? strdup(value) : NULL};
+ shell_env_entry_t entry = {.key = key, .value = fastd_strdup(value)};
VECTOR_ADD(env->entries, entry);
}
diff --git a/src/shell.h b/src/shell.h
index 141126b..bc469c8 100644
--- a/src/shell.h
+++ b/src/shell.h
@@ -32,7 +32,7 @@
#pragma once
-#include "types.h"
+#include "alloc.h"
#include <stdlib.h>
#include <string.h>
@@ -59,7 +59,7 @@ static inline void fastd_shell_command_unset(fastd_shell_command_t *command) {
static inline void fastd_shell_command_set(fastd_shell_command_t *command, const char *val, bool sync) {
fastd_shell_command_unset(command);
- command->command = strdup(val);
+ command->command = fastd_strdup(val);
command->dir = get_current_dir_name();
command->sync = sync;
}
diff --git a/src/socket.c b/src/socket.c
index 5ab9fc1..c01eec8 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -185,7 +185,7 @@ static bool set_bound_address(fastd_socket_t *sock) {
return false;
}
- sock->bound_addr = calloc(1, sizeof(addr));
+ sock->bound_addr = fastd_new0(fastd_peer_address_t);
*sock->bound_addr = addr;
return true;
@@ -234,7 +234,7 @@ fastd_socket_t* fastd_socket_open(fastd_peer_t *peer, int af) {
if (fd < 0)
return NULL;
- fastd_socket_t *sock = malloc(sizeof(fastd_socket_t));
+ fastd_socket_t *sock = fastd_new(fastd_socket_t);
sock->fd = fd;
sock->addr = NULL;
diff --git a/src/tuntap.c b/src/tuntap.c
index 7f60caf..fce0619 100644
--- a/src/tuntap.c
+++ b/src/tuntap.c
@@ -90,7 +90,7 @@ void fastd_tuntap_open(void) {
if (ioctl(ctx.tunfd, TUNSETIFF, &ifr) < 0)
exit_errno("TUNSETIFF ioctl failed");
- ctx.ifname = strndup(ifr.ifr_name, IFNAMSIZ-1);
+ ctx.ifname = fastd_strndup(ifr.ifr_name, IFNAMSIZ-1);
int ctl_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (ctl_sock < 0)
@@ -161,7 +161,7 @@ static void setup_tap(void) {
exit_errno("TAPGIFNAME ioctl failed");
free(ctx.ifname);
- ctx.ifname = strndup(ifr.ifr_name, IFNAMSIZ-1);
+ ctx.ifname = fastd_strndup(ifr.ifr_name, IFNAMSIZ-1);
set_tap_mtu();
}
@@ -199,7 +199,7 @@ void fastd_tuntap_open(void) {
if ((ctx.tunfd = open(ifname, O_RDWR|O_NONBLOCK)) < 0)
exit_errno("could not open tun/tap device file");
- if (!(ctx.ifname = fdevname_r(ctx.tunfd, malloc(IFNAMSIZ), IFNAMSIZ)))
+ if (!(ctx.ifname = fdevname_r(ctx.tunfd, fastd_alloc(IFNAMSIZ), IFNAMSIZ)))
exit_errno("could not get tun/tap interface name");
switch (conf.mode) {
@@ -272,7 +272,7 @@ void fastd_tuntap_open(void) {
if ((ctx.tunfd = open(ifname, O_RDWR|O_NONBLOCK)) < 0)
exit_errno("could not open tun device file");
- ctx.ifname = strndup(conf.ifname, IFNAMSIZ-1);
+ ctx.ifname = fastd_strndup(conf.ifname, IFNAMSIZ-1);
switch (conf.mode) {
case MODE_TAP:
diff --git a/src/vector.c b/src/vector.c
index 6e8c18d..04dd934 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -31,6 +31,7 @@
#include "vector.h"
+#include "alloc.h"
#include <string.h>
@@ -52,7 +53,7 @@ void _fastd_vector_alloc(fastd_vector_desc_t *desc, void **data, size_t n, size_
desc->length = n;
- *data = malloc(desc->allocated * elemsize);
+ *data = fastd_alloc(desc->allocated * elemsize);
}
/**
@@ -74,7 +75,7 @@ void _fastd_vector_resize(fastd_vector_desc_t *desc, void **data, size_t n, size
if (alloc != desc->allocated) {
desc->allocated = alloc;
- *data = realloc(*data, alloc * elemsize);
+ *data = fastd_realloc(*data, alloc * elemsize);
}
}
diff --git a/src/verify.c b/src/verify.c
index 2890725..2031079 100644
--- a/src/verify.c
+++ b/src/verify.c
@@ -115,7 +115,7 @@ fastd_tristate_t fastd_verify_peer(fastd_peer_t *peer, fastd_socket_t *sock, con
return fastd_tristate_false;
}
- verify_arg_t *arg = calloc(1, sizeof(verify_arg_t) + data_len);
+ verify_arg_t *arg = fastd_alloc0(sizeof(verify_arg_t) + data_len);
arg->env = env;
arg->ret_len = sizeof(fastd_async_verify_return_t) + data_len;