Skip to content

Commit

Permalink
Add define to use local ip hints
Browse files Browse the repository at this point in the history
When USE_LOCAL_IP_HINTS is defined, prioritize local interface hints and
use them when deciding the remote address.

Useful when remote is a FQDN which can resolve to both IPv4 nd IPv6.
  • Loading branch information
smititelu committed Dec 6, 2022
1 parent db0cb33 commit 866b095
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ option(USE_SSL "Build with SIPS support" OFF)
option(USE_SCTP "Build with SCTP support" OFF)
option(USE_PCAP "Build with PCAP playback support" OFF)
option(USE_GSL "Build with improved statistical support" ON)
option(USE_LOCAL_IP_HINTS "Build with local ip hints priority" OFF)

file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/src/*.cpp"
Expand Down Expand Up @@ -120,6 +121,10 @@ if(USE_PCAP)
add_definitions("-DPCAPPLAY")
endif(USE_PCAP)

if(USE_LOCAL_IP_HINTS)
add_definitions("-DUSE_LOCAL_IP_HINTS")
endif(USE_LOCAL_IP_HINTS)

if(USE_GSL)
find_library(GSL_LIBRARY gsl)
if(GSL_LIBRARY)
Expand Down
26 changes: 25 additions & 1 deletion src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2336,10 +2336,34 @@ int open_connections()
/* Resolving the remote IP */
{
fprintf(stderr, "Resolving remote host '%s'... ", remote_host);
struct addrinfo hints;

memset((char*)&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_UNSPEC;

#ifdef USE_LOCAL_IP_HINTS
struct addrinfo * local_addr;
int ret;
if (strlen(local_ip)) {
if ((ret = getaddrinfo(local_ip, NULL, &hints, &local_addr)) != 0) {
ERROR("Can't get local IP address in getaddrinfo, "
"local_ip='%s', ret=%d", local_ip, ret);
}

/* Use local address hints when getting the remote */
if (local_addr->ai_addr->sa_family == AF_INET6) {
local_ip_is_ipv6 = true;
hints.ai_family = AF_INET6;
} else {
hints.ai_family = AF_INET;
}
}
#endif

/* FIXME: add DNS SRV support using liburli? */
if (gai_getsockaddr(&remote_sockaddr, remote_host, remote_port,
AI_PASSIVE, AF_UNSPEC) != 0) {
hints.ai_flags, hints.ai_family) != 0) {
ERROR("Unknown remote host '%s'.\n"
"Use 'sipp -h' for details", remote_host);
}
Expand Down

0 comments on commit 866b095

Please sign in to comment.