1
0
Fork 0
mirror of https://repo.or.cz/socat.git synced 2025-07-13 07:03:25 +00:00

Reworked domain name resolution, centralized IPv4/IPv6 sorting

This commit is contained in:
Gerhard Rieger 2024-08-21 20:53:15 +02:00
parent 127280088c
commit ec0e1ca20c
10 changed files with 258 additions and 183 deletions

View file

@ -55,8 +55,7 @@ static int xioopen_socks4_connect(
bool dofork = false;
union sockaddr_union us_sa, *us = &us_sa;
socklen_t uslen = sizeof(us_sa);
struct addrinfo *themlist, *themp;
struct addrinfo **ai_sorted;
struct addrinfo **themarr, *themp;
int i;
bool needbind = false;
bool lowport = false;
@ -103,22 +102,9 @@ static int xioopen_socks4_connect(
_xioopen_ipapp_prepare(opts, &opts0, sockdname, socksport,
&pf, ipproto,
sfd->para.socket.ip.ai_flags,
&themlist, us, &uslen,
&themarr, us, &uslen,
&needbind, &lowport, socktype);
/* Count addrinfo entries */
themp = themlist;
i = 0;
while (themp != NULL) {
++i;
themp = themp->ai_next;
}
ai_sorted = Calloc((i+1), sizeof(struct addrinfo *));
if (ai_sorted == NULL)
return STAT_RETRYLATER;
/* Generate a list of addresses sorted by preferred ip version */
_xio_sort_ip_addresses(themlist, ai_sorted);
/* we try to resolve the target address _before_ connecting to the socks
server: this avoids unnecessary socks connects and timeouts */
result =
@ -139,15 +125,15 @@ static int xioopen_socks4_connect(
return result;
}
/* loop over themlist */
/* loop over themarr */
i = 0;
themp = ai_sorted[i++];
themp = themarr[i++];
while (themp != NULL) {
Notice1("opening connection to %s",
sockaddr_info(themp->ai_addr, themp->ai_addrlen,
infobuff, sizeof(infobuff)));
#if WITH_RETRY
if (sfd->forever || sfd->retry || ai_sorted[i] != NULL) {
if (sfd->forever || sfd->retry || themarr[i] != NULL) {
level = E_INFO;
} else
#endif /* WITH_RETRY */
@ -161,7 +147,7 @@ static int xioopen_socks4_connect(
opts, pf?pf:themp->ai_family, socktype, IPPROTO_TCP, lowport, level);
if (result == STAT_OK)
break;
themp = ai_sorted[i++];
themp = themarr[i++];
if (themp == NULL)
result = STAT_RETRYLATER;
}
@ -178,11 +164,10 @@ static int xioopen_socks4_connect(
}
#endif /* WITH_RETRY */
default:
free(ai_sorted);
xiofreeaddrinfo(themlist);
xiofreeaddrinfo(themarr);
return result;
}
xiofreeaddrinfo(themlist);
xiofreeaddrinfo(themarr);
applyopts(sfd, -1, opts, PH_ALL);
if ((result = _xio_openlate(sfd, opts)) < 0)