Code cleanup

This commit is contained in:
angt
2015-11-17 07:15:59 +01:00
parent 53a55e83c4
commit a261f1a8b1
4 changed files with 42 additions and 19 deletions

View File

@@ -1,8 +1,27 @@
#include "common.h"
#include <stdio.h>
#include <stdarg.h>
void gt_not_available (const char *name)
void gt_log (const char *fmt, ...)
{
fprintf(stderr, "%s is not available on your platform!\n", name);
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void gt_fatal (const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void gt_na (const char *name)
{
gt_log("%s is not available on your platform!\n", name);
}

View File

@@ -14,6 +14,8 @@
#define PALIGN(x) ((void *)ALIGN((size_t)(x)))
#define PALIGN_DOWN(x) ((void *)ALIGN_DOWN((size_t)(x)))
#define _printf_(A,B) __attribute__((format(printf,A,B)))
#define _noreturn_ __attribute__((noreturn))
#define _unused_ __attribute__((unused))
typedef struct buffer buffer_t;
@@ -25,4 +27,6 @@ struct buffer {
uint8_t *end;
};
void gt_not_available (const char *);
void gt_log (const char *, ...) _printf_(1,2);
void gt_fatal (const char *, ...) _printf_(1,2) _noreturn_;
void gt_na (const char *);

View File

@@ -106,7 +106,7 @@ static void sk_set_congestion (int fd, const char *name)
#else
static void sk_set_congestion (_unused_ int fd, _unused_ const char *name)
{
gt_not_available("TCP_CONGESTION");
gt_na("TCP_CONGESTION");
}
#endif
@@ -221,8 +221,7 @@ static socklen_t sk_get_info (int fd, struct tcp_info *ti)
static void print_tcp_info (const char *name, struct tcp_info *ti)
{
fprintf(stderr,
"%s: tcpinfo"
gt_log("%s: tcpinfo"
" rto:%" PRIu32 " ato:%" PRIu32 " snd_mss:%" PRIu32
" rcv_mss:%" PRIu32 " unacked:%" PRIu32 " sacked:%" PRIu32
" lost:%" PRIu32 " retrans:%" PRIu32 " fackets:%" PRIu32
@@ -242,7 +241,7 @@ static void print_tcp_info (const char *name, struct tcp_info *ti)
static struct addrinfo *ai_create (const char *host, const char *port, int listener)
{
if (!port || !port[0]) {
fprintf(stderr, "port is not valid\n");
gt_log("port is not valid\n");
return NULL;
}
@@ -269,10 +268,10 @@ static struct addrinfo *ai_create (const char *host, const char *port, int liste
break;
case EAI_FAIL:
case EAI_AGAIN:
fprintf(stderr, "the name server returned a failure\n");
gt_log("the name server returned a failure\n");
break;
default:
fprintf(stderr, "%s.%s is not valid\n", host?:"", port);
gt_log("%s.%s is not valid\n", host?:"", port);
}
return NULL;
@@ -462,7 +461,7 @@ static void dump_ip_header (uint8_t *data, size_t size)
hex[40] = 0;
fprintf(stderr, "DUMP(%zu): %s\n", size, hex);
gt_log("DUMP(%zu): %s\n", size, hex);
}
static int gt_setup_secretkey (struct crypto_ctx *ctx, char *keyfile)
@@ -482,7 +481,7 @@ static int gt_setup_secretkey (struct crypto_ctx *ctx, char *keyfile)
}
if (fd_read_all(fd, ctx->skey, size)!=size) {
fprintf(stderr, "unable to read secret key in `%s'\n", keyfile);
gt_log("unable to read secret key in `%s'\n", keyfile);
close(fd);
return -1;
}
@@ -507,6 +506,8 @@ static int gt_setup_crypto (struct crypto_ctx *ctx, int fd, int listener)
uint8_t auth_r[hash_size], auth_w[hash_size];
uint8_t hash[hash_size];
crypto_generichash_state state;
randombytes_buf(data_w, nonce_size);
randombytes_buf(secret, sizeof(secret));
crypto_scalarmult_base(&data_w[nonce_size], secret);
@@ -544,10 +545,9 @@ static int gt_setup_crypto (struct crypto_ctx *ctx, int fd, int listener)
if (sodium_memcmp(auth_r, hash, hash_size))
return -2;
if (crypto_scalarmult(shared, secret, &data_r[nonce_size]) != 0)
if (crypto_scalarmult(shared, secret, &data_r[nonce_size]))
return -2;
crypto_generichash_state state;
crypto_generichash_init(&state, ctx->skey, sizeof(ctx->skey), sizeof(key));
crypto_generichash_update(&state, shared, sizeof(shared));
crypto_generichash_update(&state, data_r, size);
@@ -619,12 +619,12 @@ int main (int argc, char **argv)
}
if (sodium_init()==-1) {
fprintf(stderr, "libsodium initialization has failed!\n");
gt_log("libsodium initialization has failed!\n");
return 1;
}
if (!crypto_aead_aes256gcm_is_available()) {
gt_not_available("AES-256-GCM");
gt_na("AES-256-GCM");
return 1;
}
@@ -673,7 +673,7 @@ int main (int argc, char **argv)
if (!sockname)
goto restart;
fprintf(stderr, "%s: connected\n", sockname);
gt_log("%s: connected\n", sockname);
if (!delay)
sk_set_nodelay(sock.fd);
@@ -686,7 +686,7 @@ int main (int argc, char **argv)
sk_set_congestion(sock.fd, congestion);
switch (gt_setup_crypto(&ctx, sock.fd, listener)) {
case -2: fprintf(stderr, "%s: key exchange could not be verified!\n", sockname);
case -2: gt_log("%s: key exchange could not be verified!\n", sockname);
case -1: goto restart;
default: break;
}
@@ -800,7 +800,7 @@ int main (int argc, char **argv)
break;
if (decrypt_packet(&ctx, tunw.buf, ip_size, &sock.read.buf)) {
fprintf(stderr, "%s: message could not be verified!\n", sockname);
gt_log("%s: message could not be verified!\n", sockname);
goto restart;
}

View File

@@ -43,7 +43,7 @@ int tun_create (char *name, int multiqueue)
#ifdef IFF_MULTI_QUEUE
ifr.ifr_flags |= IFF_MULTI_QUEUE;
#else
gt_not_available("IFF_MULTI_QUEUE");
gt_na("IFF_MULTI_QUEUE");
#endif
}