Skip to content

Commit

Permalink
tcp congestion: support compatible congestion_algorithm option
Browse files Browse the repository at this point in the history
- config: add HasKey methods
  • Loading branch information
Chilledheart committed Aug 27, 2024
1 parent 7ae4aa4 commit 5ac471a
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ bool ReadConfig() {
config_impl->Read("tcp_keep_alive_cnt", &FLAGS_tcp_keep_alive_cnt);
config_impl->Read("tcp_keep_alive_idle_timeout", &FLAGS_tcp_keep_alive_idle_timeout);
config_impl->Read("tcp_keep_alive_interval", &FLAGS_tcp_keep_alive_interval);
/* deprecated option: congestion_algorithm */
if (config_impl->HasKey<std::string>("congestion_algorithm")) {
config_impl->Read("congestion_algorithm", &FLAGS_tcp_congestion_algorithm);
}
config_impl->Read("tcp_congestion_algorithm", &FLAGS_tcp_congestion_algorithm);

/* optional tls fields */
Expand Down Expand Up @@ -130,6 +134,8 @@ bool SaveConfig() {
all_fields_written &= config_impl->Write("tcp_keep_alive_cnt", FLAGS_tcp_keep_alive_cnt);
all_fields_written &= config_impl->Write("tcp_keep_alive_idle_timeout", FLAGS_tcp_keep_alive_idle_timeout);
all_fields_written &= config_impl->Write("tcp_keep_alive_interval", FLAGS_tcp_keep_alive_interval);
/* remove deprecated option: congestion_algorithm */
static_cast<void>(config_impl->Delete("congestion_algorithm"));
all_fields_written &= config_impl->Write("tcp_congestion_algorithm", FLAGS_tcp_congestion_algorithm);

all_fields_written &= config_impl->Write("cacert", FLAGS_cacert);
Expand Down
30 changes: 30 additions & 0 deletions src/config/config_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,36 @@ bool ConfigImpl::Close() {
return ret;
}

template <>
bool ConfigImpl::HasKey<std::string>(const std::string& key) {
return HasKeyStringImpl(key);
}

template <>
bool ConfigImpl::HasKey<bool>(const std::string& key) {
return HasKeyBoolImpl(key);
}

template <>
bool ConfigImpl::HasKey<uint32_t>(const std::string& key) {
return HasKeyUint32Impl(key);
}

template <>
bool ConfigImpl::HasKey<uint64_t>(const std::string& key) {
return HasKeyUint64Impl(key);
}

template <>
bool ConfigImpl::HasKey<int32_t>(const std::string& key) {
return HasKeyInt32Impl(key);
}

template <>
bool ConfigImpl::HasKey<int64_t>(const std::string& key) {
return HasKeyInt64Impl(key);
}

template <typename T>
bool ConfigImpl::Read(const std::string& key, absl::Flag<T>* value, bool is_masked) {
// Use an int instead of a bool to guarantee that a non-zero value has
Expand Down
36 changes: 36 additions & 0 deletions src/config/config_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class ConfigImpl {
/// Close the ConfigTree, useful to flush the ConfigTree into persistent media
bool Close();

/// Test the key from ConfigTree
///
/// \param key the key value
template <typename T>
bool HasKey(const std::string& key);

/// Read the key from ConfigTree
///
/// \param key the key value
Expand Down Expand Up @@ -70,6 +76,36 @@ class ConfigImpl {
/// Close the ConfigTree, useful to flush the ConfigTree into persistent media
virtual bool CloseImpl() = 0;

/// Test the key from ConfigTree
///
/// \param key the key value
virtual bool HasKeyStringImpl(const std::string& key) = 0;

/// Test the key from ConfigTree
///
/// \param key the key value
virtual bool HasKeyBoolImpl(const std::string& key) = 0;

/// Test the key from ConfigTree
///
/// \param key the key value
virtual bool HasKeyUint32Impl(const std::string& key) = 0;

/// Test the key from ConfigTree
///
/// \param key the key value
virtual bool HasKeyUint64Impl(const std::string& key) = 0;

/// Test the key from ConfigTree
///
/// \param key the key value
virtual bool HasKeyInt32Impl(const std::string& key) = 0;

/// Test the key from ConfigTree
///
/// \param key the key value
virtual bool HasKeyInt64Impl(const std::string& key) = 0;

/// Read the key from ConfigTree
///
/// \param key the key value
Expand Down
7 changes: 7 additions & 0 deletions src/config/config_impl_apple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class ConfigImplApple : public ConfigImpl {

bool CloseImpl() override;

bool HasKeyStringImpl(const std::string& key) override;
bool HasKeyBoolImpl(const std::string& key) override;
bool HasKeyUint32Impl(const std::string& key) override;
bool HasKeyUint64Impl(const std::string& key) override;
bool HasKeyInt32Impl(const std::string& key) override;
bool HasKeyInt64Impl(const std::string& key) override;

bool ReadImpl(const std::string& key, std::string* value) override;
bool ReadImpl(const std::string& key, bool* value) override;
bool ReadImpl(const std::string& key, uint32_t* value) override;
Expand Down
50 changes: 49 additions & 1 deletion src/config/config_impl_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,54 @@
return true;
}

bool ConfigImplApple::HasKeyStringImpl(const std::string& key) {
CFStringRef obj;
if (CFDictionaryGetValueIfPresent(root_, SysUTF8ToCFStringRef(key).get(), (const void**)&obj) &&
CFGetTypeID(obj) == CFStringGetTypeID()) {
return true;
}
return false;
}

bool ConfigImplApple::HasKeyBoolImpl(const std::string& key) {
CFBooleanRef obj;
if (CFDictionaryGetValueIfPresent(root_, SysUTF8ToCFStringRef(key).get(), (const void**)&obj) &&
CFGetTypeID(obj) == CFBooleanGetTypeID()) {
return true;
}
return false;
}

bool ConfigImplApple::HasKeyUint32Impl(const std::string& key) {
return HasKeyInt32Impl(key);
}

bool ConfigImplApple::HasKeyUint64Impl(const std::string& key) {
return HasKeyInt64Impl(key);
}

bool ConfigImplApple::HasKeyInt32Impl(const std::string& key) {
CFNumberRef obj;
if (CFDictionaryGetValueIfPresent(root_, SysUTF8ToCFStringRef(key).get(), (const void**)&obj) &&
CFGetTypeID(obj) == CFNumberGetTypeID() &&
(CFNumberGetType(obj) == kCFNumberSInt32Type || CFNumberGetType(obj) == kCFNumberSInt16Type ||
CFNumberGetType(obj) == kCFNumberSInt8Type)) {
return true;
}
return false;
}

bool ConfigImplApple::HasKeyInt64Impl(const std::string& key) {
CFNumberRef obj;
if (CFDictionaryGetValueIfPresent(root_, SysUTF8ToCFStringRef(key).get(), (const void**)&obj) &&
CFGetTypeID(obj) == CFNumberGetTypeID() &&
(CFNumberGetType(obj) == kCFNumberSInt64Type || CFNumberGetType(obj) == kCFNumberSInt32Type ||
CFNumberGetType(obj) == kCFNumberSInt16Type || CFNumberGetType(obj) == kCFNumberSInt8Type)) {
return true;
}
return false;
}

bool ConfigImplApple::ReadImpl(const std::string& key, std::string* value) {
CFStringRef obj;
if (CFDictionaryGetValueIfPresent(root_, SysUTF8ToCFStringRef(key).get(), (const void**)&obj) &&
Expand Down Expand Up @@ -169,7 +217,7 @@
}

bool ConfigImplApple::DeleteImpl(const std::string& key) {
if (CFDictionaryGetValueIfPresent(write_root_, SysUTF8ToCFStringRef(key).get(), nullptr)) {
if (CFDictionaryContainsKey(write_root_, SysUTF8ToCFStringRef(key).get())) {
CFDictionaryRemoveValue(write_root_, SysUTF8ToCFStringRef(key).get());
return true;
}
Expand Down
20 changes: 20 additions & 0 deletions src/config/config_impl_local.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ class ConfigImplLocal : public ConfigImpl {
return true;
}

bool HasKeyStringImpl(const std::string& key) override { return root_.contains(key) && root_[key].is_string(); }

bool HasKeyBoolImpl(const std::string& key) override { return root_.contains(key) && root_[key].is_boolean(); }

bool HasKeyUint32Impl(const std::string& key) override {
return root_.contains(key) && root_[key].is_number_unsigned() && root_[key].is_number_integer();
}

bool HasKeyUint64Impl(const std::string& key) override {
return root_.contains(key) && root_[key].is_number_unsigned() && root_[key].is_number_integer();
}

bool HasKeyInt32Impl(const std::string& key) override {
return root_.contains(key) && root_[key].is_number_integer();
}

bool HasKeyInt64Impl(const std::string& key) override {
return root_.contains(key) && root_[key].is_number_integer();
}

bool ReadImpl(const std::string& key, std::string* value) override {
if (root_.contains(key) && root_[key].is_string()) {
*value = root_[key].get<std::string>();
Expand Down
43 changes: 43 additions & 0 deletions src/config/config_impl_windows.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ typedef unsigned __int64 QWORD;
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dtyp/ac050bbf-a821-4fab-bccf-d95d892f428f
static_assert(sizeof(QWORD) == sizeof(uint64_t), "A QWORD is a 64-bit unsigned integer.");

bool HasValue(HKEY hkey, const std::string& value, DWORD* type, DWORD* size) {
std::wstring wvalue = SysUTF8ToWide(value);

if (::RegQueryValueExW(hkey /* HKEY */, wvalue.c_str() /* lpValueName */, nullptr /* lpReserved */, type /* lpType */,
nullptr /* lpData */, size /* lpcbData */) != ERROR_SUCCESS) {
return false;
}
return true;
}

bool ReadValue(HKEY hkey, const std::string& value, DWORD* type, std::vector<BYTE>* output) {
DWORD BufferSize;
std::wstring wvalue = SysUTF8ToWide(value);
Expand Down Expand Up @@ -126,6 +136,39 @@ class ConfigImplWindows : public ConfigImpl {
return ret;
}

bool HasKeyStringImpl(const std::string& key) override {
DWORD type, size;

return HasValue(hkey_, key, &type, &size) && (type == REG_SZ || type == REG_EXPAND_SZ) &&
size % sizeof(wchar_t) == 0;
}

bool HasKeyBoolImpl(const std::string& key) override { return HasKeyUint32Impl(key); }

bool HasKeyUint32Impl(const std::string& key) override {
DWORD type, size;

return HasValue(hkey_, key, &type, &size) && (type == REG_DWORD || type == REG_BINARY) && size == sizeof(uint32_t);
}

bool HasKeyUint64Impl(const std::string& key) override {
DWORD type, size;

if (HasValue(hkey_, key, &type, &size) && (type == REG_QWORD || type == REG_BINARY) && size == sizeof(uint64_t)) {
return true;
}

if (HasValue(hkey_, key, &type, &size) && (type == REG_DWORD || type == REG_BINARY) && size == sizeof(uint32_t)) {
return true;
}

return false;
}

bool HasKeyInt32Impl(const std::string& key) override { return HasKeyUint32Impl(key); }

bool HasKeyInt64Impl(const std::string& key) override { return HasKeyUint64Impl(key); }

bool ReadImpl(const std::string& key, std::string* value) override {
DWORD type;
std::vector<BYTE> output;
Expand Down
Loading

0 comments on commit 5ac471a

Please sign in to comment.