tpws: connect-bind-addr link local ifname delayed resolution

This commit is contained in:
bol-van 2024-08-23 16:57:44 +03:00
parent e86fc2e7b5
commit 700de59241
3 changed files with 19 additions and 1 deletions

View File

@ -74,6 +74,7 @@ struct params_s
struct sockaddr_in connect_bind4; struct sockaddr_in connect_bind4;
struct sockaddr_in6 connect_bind6; struct sockaddr_in6 connect_bind6;
char connect_bind6_ifname[IF_NAMESIZE];
int debug; int debug;

View File

@ -797,7 +797,13 @@ void parse_params(int argc, char *argv[])
else if (inet_pton(AF_INET6, optarg, &params.connect_bind6.sin6_addr)) else if (inet_pton(AF_INET6, optarg, &params.connect_bind6.sin6_addr))
{ {
params.connect_bind6.sin6_family = AF_INET6; params.connect_bind6.sin6_family = AF_INET6;
if (p && *p) params.connect_bind6.sin6_scope_id=if_nametoindex(p); if (p && *p)
{
// copy interface name for delayed resolution
strncpy(params.connect_bind6_ifname,p,sizeof(params.connect_bind6_ifname));
params.connect_bind6_ifname[sizeof(params.connect_bind6_ifname)-1]=0;
}
} }
else else
{ {

View File

@ -403,6 +403,17 @@ static int connect_remote(const struct sockaddr *remote_addr, bool bApplyConnect
} }
else if(remote_addr->sa_family == params.connect_bind6.sin6_family) else if(remote_addr->sa_family == params.connect_bind6.sin6_family)
{ {
if (*params.connect_bind6_ifname && !params.connect_bind6.sin6_scope_id)
{
params.connect_bind6.sin6_scope_id=if_nametoindex(params.connect_bind6_ifname);
if (!params.connect_bind6.sin6_scope_id)
{
fprintf(stderr, "interface name not found : %s\n", params.connect_bind6_ifname);
close(remote_fd);
return -1;
}
}
if (bind(remote_fd, (struct sockaddr *)&params.connect_bind6, sizeof(struct sockaddr_in6)) == -1) if (bind(remote_fd, (struct sockaddr *)&params.connect_bind6, sizeof(struct sockaddr_in6)) == -1)
{ {
perror("bind on connect"); perror("bind on connect");