warp-vpn

point to point VPN implementation
git clone git://git.2f30.org/warp-vpn
Log | Files | Refs | README

commit f9ffd6a6462f24e68c9fa0ed90011cacd338faf0
parent dc9b14d9875bef6247ce8e125ca1bac67c206029
Author: sin <sin@2f30.org>
Date:   Wed,  6 Apr 2016 14:16:44 +0100

use a table

Diffstat:
Mstun.c | 47++++++++++++++++++++---------------------------
1 file changed, 20 insertions(+), 27 deletions(-)

diff --git a/stun.c b/stun.c @@ -74,6 +74,8 @@ #define explicit_bzero bzero #endif +#define LEN(x) (sizeof (x) / sizeof *(x)) + #define NOPRIVUSER "nobody" #define RCVTIMEO 250 /* in milliseconds */ #define RECONNECTTIMEO 60 /* in seconds */ @@ -650,35 +652,26 @@ err: void aead_from_name(const EVP_AEAD **aead, const char *name) { - *aead = NULL; + size_t i; + struct { + const char *name; + const EVP_AEAD *(*aeadfn)(void); + } ciphers[] = { + { "aes-128-gcm", EVP_aead_aes_128_gcm }, + { "aes-256-gcm", EVP_aead_aes_256_gcm }, + { "chacha20-poly1305", EVP_aead_chacha20_poly1305 }, + { "chacha20-poly1305-ietf", EVP_aead_chacha20_poly1305_ietf } + }; - if (!strcmp(name, "aes-128-gcm")) { -#ifndef OPENSSL_NO_AES - *aead = EVP_aead_aes_128_gcm(); -#else - logerr("No AES support"); -#endif - } else if (!strcmp(name, "aes-256-gcm")) { -#ifndef OPENSSL_NO_AES - *aead = EVP_aead_aes_256_gcm(); -#else - logerr("No AES support"); -#endif - } else if (!strcmp(name, "chacha20-poly1305")) { -#if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) - *aead = EVP_aead_chacha20_poly1305(); -#else - logerr("No chacha20-poly1305 support"); -#endif - } else if (!strcmp(name, "chacha20-poly1305-ietf")) { -#if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) - *aead = EVP_aead_chacha20_poly1305_ietf(); -#else - logerr("No chacha20-poly1305-ietf support"); -#endif - } else { - logerr("Unknown AEAD: %s", name); + *aead = NULL; + for (i = 0; i < LEN(ciphers); i++) { + if (!strcmp(ciphers[i].name, name)) { + *aead = (*ciphers[i].aeadfn)(); + break; + } } + if (i == LEN(ciphers)) + logerr("unknown cipher: %s", name); } void