Skip to content

Commit 68ec885

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

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

src/AddressesManager.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,13 @@ const char* dx_am_get_current_connected_address(dxf_connection_t connection) {
122122

123123
return currentConnectedAddress.c_str();
124124
}
125+
126+
const char* dx_am_get_current_connected_socket_address(dxf_connection_t connection) {
127+
thread_local std::string currentConnectedSocketAddress{};
128+
129+
currentConnectedSocketAddress = dx::AddressesManager::getInstance()->getCurrentSocketIpAddress(connection) + ":" +
130+
dx::AddressesManager::getInstance()->getCurrentAddressPort(connection);
131+
132+
return currentConnectedSocketAddress.c_str();
133+
}
125134
}

src/AddressesManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void dx_am_set_current_address_tls_trust_store_len(dxf_connection_t connection,
5151
void dx_am_set_current_address_tls_key_store_mem(dxf_connection_t connection, uint8_t* key_store_mem);
5252
void dx_am_set_current_address_tls_key_store_len(dxf_connection_t connection, size_t key_store_len);
5353
const char* dx_am_get_current_connected_address(dxf_connection_t connection);
54+
const char* dx_am_get_current_connected_socket_address(dxf_connection_t connection);
5455

5556
#ifdef __cplusplus
5657
}

src/AddressesManager.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class AddressesManager {
300300
}
301301

302302
// Non thread-safe. Checks the connection
303-
SocketAddress* getCurrentSocketAddress(dxf_connection_t connection) {
303+
SocketAddress* getCurrentSocketAddressImpl(dxf_connection_t connection) {
304304
if (connection == nullptr) {
305305
return nullptr;
306306
}
@@ -392,7 +392,7 @@ class AddressesManager {
392392

393393
bool isCurrentSocketAddressConnectionFailed(dxf_connection_t connection) {
394394
std::lock_guard<std::mutex> guard(mutex_);
395-
const auto* sa = getCurrentSocketAddress(connection);
395+
const auto* sa = getCurrentSocketAddressImpl(connection);
396396

397397
if (!sa) {
398398
return true;
@@ -403,7 +403,7 @@ class AddressesManager {
403403

404404
void setCurrentSocketAddressConnectionFailed(dxf_connection_t connection, bool connectionFailed) {
405405
std::lock_guard<std::mutex> guard(mutex_);
406-
auto* sa = getCurrentSocketAddress(connection);
406+
auto* sa = getCurrentSocketAddressImpl(connection);
407407

408408
if (!sa) {
409409
return;
@@ -434,6 +434,17 @@ class AddressesManager {
434434
return a->password;
435435
}
436436

437+
std::string getCurrentSocketIpAddress(dxf_connection_t connection) {
438+
std::lock_guard<std::mutex> guard(mutex_);
439+
const auto* sa = getCurrentSocketAddressImpl(connection);
440+
441+
if (!sa) {
442+
return "";
443+
}
444+
445+
return sa->resolvedIp;
446+
}
447+
437448
std::string getCurrentAddressHost(dxf_connection_t connection) {
438449
std::lock_guard<std::mutex> guard(mutex_);
439450
const auto* a = getCurrentAddressImpl(connection);
@@ -471,7 +482,7 @@ class AddressesManager {
471482
int* addressProtocol, sockaddr* nativeSocketAddress,
472483
std::size_t* nativeSocketAddressLength) {
473484
std::lock_guard<std::mutex> guard(mutex_);
474-
const auto* sa = getCurrentSocketAddress(connection);
485+
const auto* sa = getCurrentSocketAddressImpl(connection);
475486

476487
if (!sa || !addressFamily || !addressSocketType || !addressProtocol || !nativeSocketAddress ||
477488
!nativeSocketAddressLength) {

src/DXNetwork.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,14 @@ static int dx_connect_via_socket(dx_network_connection_context_t* context) {
691691
return false;
692692
}
693693

694+
dx_logging_info(L"Connecting to %hs (%hs)", dx_am_get_current_connected_address(context->connection),
695+
dx_am_get_current_connected_socket_address(context->connection));
694696
context->s = dx_socket(address_family, address_socket_type, address_protocol);
695697

696698
if (context->s == INVALID_SOCKET) {
699+
dx_logging_info(L"Failed to connect to %hs (%hs): Invalid socket",
700+
dx_am_get_current_connected_address(context->connection),
701+
dx_am_get_current_connected_socket_address(context->connection));
697702
/* failed to create a new socket */
698703

699704
return false;
@@ -702,6 +707,8 @@ static int dx_connect_via_socket(dx_network_connection_context_t* context) {
702707
if (!dx_connect(context->s, &native_socket_address, (socklen_t)native_socket_address_length)) {
703708
/* failed to connect */
704709

710+
dx_logging_info(L"Failed to connect to %hs (%hs)", dx_am_get_current_connected_address(context->connection),
711+
dx_am_get_current_connected_socket_address(context->connection));
705712
dx_close(context->s);
706713

707714
return false;
@@ -940,7 +947,8 @@ int dx_bind_to_address(dxf_connection_t connection, const char* address, const d
940947

941948
context->set_fields_flags |= MUTEX_FIELD_FLAG;
942949

943-
if ((context->address = dx_ansi_create_string_src(address)) == NULL || !dx_check_if_address_is_file_and_set_raw_dump_file_flag(context) ||
950+
if ((context->address = dx_ansi_create_string_src(address)) == NULL ||
951+
!dx_check_if_address_is_file_and_set_raw_dump_file_flag(context) ||
944952
!dx_connect_to_file_or_open_socket(context)) {
945953
return false;
946954
}

src/DXSockets.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@
3333
# include <WS2tcpip.h>
3434
# pragma warning(pop)
3535

36-
typedef SOCKET dx_socket_t;
36+
typedef SOCKET dx_socket_t;
3737
#else
3838
# include <unistd.h>
3939
# include <sys/types.h>
4040
# include <sys/socket.h>
4141
# include <netinet/in.h>
42+
# include <arpa/inet.h>
4243
# include <netdb.h>
4344
# include <errno.h>
4445

4546
# define INVALID_SOCKET -1
46-
# define SOCKET_ERROR -1
47+
# define SOCKET_ERROR -1
4748

4849
typedef int dx_socket_t;
4950
#endif /* _WIN32 */
@@ -58,22 +59,21 @@ typedef int dx_socket_t;
5859
*/
5960
/* -------------------------------------------------------------------------- */
6061

61-
int dx_on_connection_created (void);
62-
int dx_on_connection_destroyed (void);
62+
int dx_on_connection_created(void);
63+
int dx_on_connection_destroyed(void);
6364

6465
/* -------------------------------------------------------------------------- */
6566
/*
6667
* Socket function wrappers
6768
*/
6869
/* -------------------------------------------------------------------------- */
6970

70-
dx_socket_t dx_socket (int family, int type, int protocol);
71-
int dx_connect (dx_socket_t s, const struct sockaddr* addr, socklen_t addrlen);
72-
int dx_send (dx_socket_t s, const void* buffer, int buflen);
73-
int dx_recv (dx_socket_t s, void* buffer, int buflen);
74-
int dx_close (dx_socket_t s);
75-
int dx_getaddrinfo (const char* nodename, const char* servname,
76-
const struct addrinfo* hints, struct addrinfo** res);
77-
void dx_freeaddrinfo (struct addrinfo* res);
71+
dx_socket_t dx_socket(int family, int type, int protocol);
72+
int dx_connect(dx_socket_t s, const struct sockaddr* addr, socklen_t addrlen);
73+
int dx_send(dx_socket_t s, const void* buffer, int buflen);
74+
int dx_recv(dx_socket_t s, void* buffer, int buflen);
75+
int dx_close(dx_socket_t s);
76+
int dx_getaddrinfo(const char* nodename, const char* servname, const struct addrinfo* hints, struct addrinfo** res);
77+
void dx_freeaddrinfo(struct addrinfo* res);
7878

7979
#endif /* DX_SOCKETS_H_INCLUDED */

0 commit comments

Comments
 (0)