Skip to content

Commit a14dc6f

Browse files
committed
[DXFC-395] Add the ability to enumerate all resolved IP addresses by DNS names
1 parent 68ec885 commit a14dc6f

File tree

4 files changed

+143
-16
lines changed

4 files changed

+143
-16
lines changed

src/AddressesManager.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ int dx_am_next_socket_address(dxf_connection_t connection) {
2929
return dx::AddressesManager::getInstance()->nextSocketAddress(connection);
3030
}
3131

32+
void dx_am_clear_addresses(dxf_connection_t connection) {
33+
return dx::AddressesManager::getInstance()->clearAddresses(connection);
34+
}
35+
3236
int dx_am_is_current_address_tls_enabled(dxf_connection_t connection) {
3337
return dx::AddressesManager::getInstance()->isCurrentAddressTlsEnabled(connection);
3438
}
@@ -72,7 +76,7 @@ const char* dx_am_get_current_address_port(dxf_connection_t connection) {
7276
currentAddressPort = dx::AddressesManager::getInstance()->getCurrentAddressPort(connection);
7377

7478
return currentAddressPort.c_str();
75-
}
79+
};
7680

7781
int dx_ma_get_current_socket_address_info(dxf_connection_t connection, int* address_family, int* address_socket_type,
7882
int* address_protocol, struct sockaddr* native_socket_address,
@@ -85,34 +89,54 @@ int dx_ma_get_current_socket_address_info(dxf_connection_t connection, int* addr
8589
const char* dx_am_get_current_address_tls_trust_store(dxf_connection_t connection) {
8690
thread_local std::string currentAddressTlsTrustStore{};
8791

92+
currentAddressTlsTrustStore = dx::AddressesManager::getInstance()->getCurrentAddressTlsTrustStore(connection);
93+
8894
return currentAddressTlsTrustStore.c_str();
8995
}
9096

9197
const char* dx_am_get_current_address_tls_trust_store_password(dxf_connection_t connection) {
9298
thread_local std::string currentAddressTlsTrustStorePassword{};
9399

100+
currentAddressTlsTrustStorePassword =
101+
dx::AddressesManager::getInstance()->getCurrentAddressTlsTrustStorePassword(connection);
102+
94103
return currentAddressTlsTrustStorePassword.c_str();
95104
}
96105

97106
const char* dx_am_get_current_address_tls_key_store(dxf_connection_t connection) {
98107
thread_local std::string currentAddressTlsKeyStore{};
99108

109+
currentAddressTlsKeyStore = dx::AddressesManager::getInstance()->getCurrentAddressTlsKeyStore(connection);
110+
100111
return currentAddressTlsKeyStore.c_str();
101112
}
102113

103114
const char* dx_am_get_current_address_tls_key_store_password(dxf_connection_t connection) {
104115
thread_local std::string currentAddressTlsKeyStorePassword{};
105116

117+
currentAddressTlsKeyStorePassword =
118+
dx::AddressesManager::getInstance()->getCurrentAddressTlsKeyStorePassword(connection);
119+
106120
return currentAddressTlsKeyStorePassword.c_str();
107121
}
108122

109-
void dx_am_set_current_address_tls_trust_store_mem(dxf_connection_t connection, uint8_t* trust_store_mem) {}
123+
#ifdef DXFEED_CODEC_TLS_ENABLED
124+
void dx_am_set_current_address_tls_trust_store_mem(dxf_connection_t connection, uint8_t* trust_store_mem) {
125+
dx::AddressesManager::getInstance()->setCurrentAddressTlsTrustStoreMem(connection, trust_store_mem);
126+
}
110127

111-
void dx_am_set_current_address_tls_trust_store_len(dxf_connection_t connection, size_t trust_store_len) {}
128+
void dx_am_set_current_address_tls_trust_store_len(dxf_connection_t connection, size_t trust_store_len) {
129+
dx::AddressesManager::getInstance()->setCurrentAddressTlsTrustStoreLen(connection, trust_store_len);
130+
}
112131

113-
void dx_am_set_current_address_tls_key_store_mem(dxf_connection_t connection, uint8_t* key_store_mem) {}
132+
void dx_am_set_current_address_tls_key_store_mem(dxf_connection_t connection, uint8_t* key_store_mem) {
133+
dx::AddressesManager::getInstance()->setCurrentAddressTlsKeyStoreMem(connection, key_store_mem);
134+
}
114135

115-
void dx_am_set_current_address_tls_key_store_len(dxf_connection_t connection, size_t key_store_len) {}
136+
void dx_am_set_current_address_tls_key_store_len(dxf_connection_t connection, size_t key_store_len) {
137+
dx::AddressesManager::getInstance()->setCurrentAddressTlsKeyStoreLen(connection, key_store_len);
138+
}
139+
#endif
116140

117141
const char* dx_am_get_current_connected_address(dxf_connection_t connection) {
118142
thread_local std::string currentConnectedAddress{};

src/AddressesManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern "C" {
3232
#endif
3333

3434
int dx_am_next_socket_address(dxf_connection_t connection);
35+
void dx_am_clear_addresses(dxf_connection_t connection);
3536
int dx_am_is_current_address_tls_enabled(dxf_connection_t connection);
3637
int dx_am_is_current_socket_address_connection_failed(dxf_connection_t connection);
3738
void dx_am_set_current_socket_address_connection_failed(dxf_connection_t connection, int is_connection_failed);
@@ -46,10 +47,14 @@ const char* dx_am_get_current_address_tls_trust_store(dxf_connection_t connectio
4647
const char* dx_am_get_current_address_tls_trust_store_password(dxf_connection_t connection);
4748
const char* dx_am_get_current_address_tls_key_store(dxf_connection_t connection);
4849
const char* dx_am_get_current_address_tls_key_store_password(dxf_connection_t connection);
50+
51+
#ifdef DXFEED_CODEC_TLS_ENABLED
4952
void dx_am_set_current_address_tls_trust_store_mem(dxf_connection_t connection, uint8_t* trust_store_mem);
5053
void dx_am_set_current_address_tls_trust_store_len(dxf_connection_t connection, size_t trust_store_len);
5154
void dx_am_set_current_address_tls_key_store_mem(dxf_connection_t connection, uint8_t* key_store_mem);
5255
void dx_am_set_current_address_tls_key_store_len(dxf_connection_t connection, size_t key_store_len);
56+
#endif
57+
5358
const char* dx_am_get_current_connected_address(dxf_connection_t connection);
5459
const char* dx_am_get_current_connected_socket_address(dxf_connection_t connection);
5560

src/AddressesManager.hpp

Lines changed: 107 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ class ReconnectHelper {
9999
std::int64_t startTime_{};
100100

101101
public:
102-
// ReconnectHelper(std::int64_t delay = 100) : delay_{delay} {}
103-
ReconnectHelper(std::int64_t delay = 10000) : delay_{delay} {}
104-
105-
void setDelay(std::int64_t delay) { delay_ = delay; }
102+
explicit ReconnectHelper(std::int64_t delay = 10000) : delay_{delay} {}
106103

107104
void sleepBeforeConnection() {
108105
std::int64_t worked = System::currentTimeMillis() - startTime_;
@@ -222,9 +219,10 @@ class AddressesManager {
222219

223220
template <typename It>
224221
static void shuffle(It begin, It end) {
225-
static std::default_random_engine randomEngine{};
222+
static std::random_device randomDevice{};
223+
static std::mt19937 engine{randomDevice()};
226224

227-
std::shuffle(begin, end, randomEngine);
225+
std::shuffle(begin, end, engine);
228226
}
229227

230228
static ResolvedAddresses resolveAddresses(dxf_connection_t connection) {
@@ -388,7 +386,11 @@ class AddressesManager {
388386
return !SocketAddress::isInvalid(getNextSocketAddress(connection));
389387
}
390388

391-
void clearAddresses(dxf_connection_t connection) {}
389+
void clearAddresses(dxf_connection_t connection) {
390+
std::lock_guard<std::mutex> guard(mutex_);
391+
392+
resolvedAddressesByConnection_.erase(connection);
393+
}
392394

393395
bool isCurrentSocketAddressConnectionFailed(dxf_connection_t connection) {
394396
std::lock_guard<std::mutex> guard(mutex_);
@@ -498,11 +500,105 @@ class AddressesManager {
498500
return true;
499501
}
500502

501-
Address* getCurrentAddress(dxf_connection_t connection) {
502-
std::lock_guard<std::mutex> guard(mutex_);
503+
std::string getCurrentAddressTlsTrustStore(dxf_connection_t connection) {
504+
std::lock_guard<std::mutex> guard(mutex_);
505+
506+
const auto* a = getCurrentAddressImpl(connection);
507+
508+
if (!a) {
509+
return "";
510+
}
511+
512+
return a->tlsData.trustStore;
513+
}
514+
515+
std::string getCurrentAddressTlsTrustStorePassword(dxf_connection_t connection) {
516+
std::lock_guard<std::mutex> guard(mutex_);
517+
518+
const auto* a = getCurrentAddressImpl(connection);
519+
520+
if (!a) {
521+
return "";
522+
}
523+
524+
return a->tlsData.trustStorePassword;
525+
}
526+
527+
std::string getCurrentAddressTlsKeyStore(dxf_connection_t connection) {
528+
std::lock_guard<std::mutex> guard(mutex_);
529+
530+
const auto* a = getCurrentAddressImpl(connection);
531+
532+
if (!a) {
533+
return "";
534+
}
535+
536+
return a->tlsData.keyStore;
537+
}
538+
539+
std::string getCurrentAddressTlsKeyStorePassword(dxf_connection_t connection) {
540+
std::lock_guard<std::mutex> guard(mutex_);
541+
542+
const auto* a = getCurrentAddressImpl(connection);
543+
544+
if (!a) {
545+
return "";
546+
}
547+
548+
return a->tlsData.keyStorePassword;
549+
}
550+
551+
Address* getCurrentAddress(dxf_connection_t connection) {
552+
std::lock_guard<std::mutex> guard(mutex_);
553+
554+
return getCurrentAddressImpl(connection);
555+
}
556+
557+
#ifdef DXFEED_CODEC_TLS_ENABLED
558+
void setCurrentAddressTlsTrustStoreMem(dxf_connection_t connection, uint8_t* trustStoreMem) {
559+
std::lock_guard<std::mutex> guard(mutex_);
560+
auto* a = getCurrentAddressImpl(connection);
561+
562+
if (!a) {
563+
return;
564+
}
565+
566+
a->trustStoreMem = trustStoreMem;
567+
}
568+
569+
void setCurrentAddressTlsTrustStoreLen(dxf_connection_t connection, size_t trustStoreLen) {
570+
std::lock_guard<std::mutex> guard(mutex_);
571+
auto* a = getCurrentAddressImpl(connection);
572+
573+
if (!a) {
574+
return;
575+
}
576+
577+
a->trustStoreLen = trustStoreLen;
578+
}
579+
580+
void setCurrentAddressTlsKeyStoreMem(dxf_connection_t connection, uint8_t* keyStoreMem) {
581+
std::lock_guard<std::mutex> guard(mutex_);
582+
auto* a = getCurrentAddressImpl(connection);
583+
584+
if (!a) {
585+
return;
586+
}
587+
588+
a->keyStoreMem = keyStoreMem;
589+
}
590+
591+
void setCurrentAddressTlsKeyStoreLen(dxf_connection_t connection, size_t keyStoreLen) {
592+
std::lock_guard<std::mutex> guard(mutex_);
593+
auto* a = getCurrentAddressImpl(connection);
594+
595+
if (!a) {
596+
return;
597+
}
503598

504-
return getCurrentAddressImpl(connection);
505-
}
599+
a->keyStoreLen = keyStoreLen;
600+
}
601+
#endif
506602
};
507603

508604
} // namespace dx

src/DXNetwork.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ int dx_clear_connection_data(dx_network_connection_context_t* context) {
327327

328328
dx_free(context);
329329

330+
dx_am_clear_addresses(context->connection);
331+
330332
return success;
331333
}
332334

0 commit comments

Comments
 (0)