1#pragma once 2 3#include "Common.h" 4#include "logging/Logging.h" 5 6#include <tls.h> 7 8class TlsConfig : noncopyable 9{ 10 public: 11 TlsConfig() 12 : config_(CHECK_NOTNULL(tls_config_new())) 13 { 14 if (initialized <= 0) 15 { 16 LOG_FATAL; 17 } 18 } 19 20 ~TlsConfig() 21 { 22 tls_config_free(config_); 23 } 24 25 void setCaFile(StringArg caFile) 26 { 27 check(tls_config_set_ca_file(config_, caFile.c_str())); 28 } 29 30 void setCertFile(StringArg certFile) 31 { 32 check(tls_config_set_cert_file(config_, certFile.c_str())); 33 } 34 35 void setKeyFile(StringArg keyFile) 36 { 37 check(tls_config_set_key_file(config_, keyFile.c_str())); 38 } 39 40 struct tls_config* get() { return config_; } 41 42 private: 43 void check(int ret) 44 { 45 if (ret != 0) 46 { 47 LOG_FATAL << tls_config_error(config_); 48 } 49 } 50 51 struct tls_config* config_; 52 53 static int initialized; 54}; 55