socket: support binding to interface *AND* IP

Introduce new notation for CURLOPT_INTERFACE / --interface:
ifhost!<interface>!<host>

Binding to an interface doesn't set the address, and an interface can
have multiple addresses.

When binding to an address (without interface), the kernel is free to
choose the route, and it can route through any device that can access
the target address, not necessarily the one with the chosen address.

Moreover, it is possible for different interfaces to have the same IP
address, on which case we need to provide a way to be more specific.

Factor out the parsing part of interface option, and add unit tests:
1663.

Closes #13719
This commit is contained in:
Orgad Shaneh 2024-05-17 14:44:44 +03:00 committed by Daniel Stenberg
parent 23fe1a52dc
commit 3060557af7
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
10 changed files with 322 additions and 79 deletions

View file

@ -139,6 +139,42 @@ static CURLcode setstropt_userpwd(char *option, char **userp, char **passwdp)
return CURLE_OK;
}
static CURLcode setstropt_interface(
char *option, char **devp, char **ifacep, char **hostp)
{
char *dev = NULL;
char *iface = NULL;
char *host = NULL;
size_t len;
CURLcode result;
DEBUGASSERT(devp);
DEBUGASSERT(ifacep);
DEBUGASSERT(hostp);
/* Parse the interface details */
if(!option || !*option)
return CURLE_BAD_FUNCTION_ARGUMENT;
len = strlen(option);
if(len > 255)
return CURLE_BAD_FUNCTION_ARGUMENT;
result = Curl_parse_interface(option, len, &dev, &iface, &host);
if(result)
return result;
free(*devp);
*devp = dev;
free(*ifacep);
*ifacep = iface;
free(*hostp);
*hostp = host;
return CURLE_OK;
}
#define C_SSLVERSION_VALUE(x) (x & 0xffff)
#define C_SSLVERSION_MAX_VALUE(x) (x & 0xffff0000)
@ -1881,8 +1917,10 @@ CURLcode Curl_vsetopt(struct Curl_easy *data, CURLoption option, va_list param)
* Set what interface or address/hostname to bind the socket to when
* performing an operation and thus what from-IP your connection will use.
*/
result = Curl_setstropt(&data->set.str[STRING_DEVICE],
va_arg(param, char *));
result = setstropt_interface(va_arg(param, char *),
&data->set.str[STRING_DEVICE],
&data->set.str[STRING_INTERFACE],
&data->set.str[STRING_BINDHOST]);
break;
#ifndef CURL_DISABLE_BINDLOCAL
case CURLOPT_LOCALPORT: