Don't assume IPv4 in dial().

This commit is contained in:
Mid Favila 2022-12-13 17:21:35 -04:00
parent 8bba6e9f60
commit b5e1a30092

View File

@ -5,52 +5,52 @@ int dial(const char *fqdn, const char *proto, struct tls **tlsres)
{ {
int sd; int sd;
struct addrinfo *ainfo; struct addrinfo *ainfo;
struct tls_config *tlshints; struct tls_config *tlshints;
if( !(sd = socket(AF_INET, SOCK_STREAM, 0))) if(getaddrinfo(fqdn, proto, 0, &ainfo))
{ {
return(0); return(0);
} }
if(getaddrinfo(fqdn, proto, 0, &ainfo)) if( !(sd = socket(ainfo->ai_family, SOCK_STREAM, 0)))
{ {
return(0); return(0);
} }
if(connect(sd, ainfo->ai_addr, ainfo->ai_addrlen)) if(connect(sd, ainfo->ai_addr, ainfo->ai_addrlen))
{ {
return(0);
}
if(tlsres != 0)
{
close(sd);
if( 0 == (*tlsres = tls_client()))
{
goto err_ssl;
}
if( 0 == (tlshints = tls_config_new()))
{
goto err_ssl;
}
if(tls_configure(*tlsres, tlshints))
{
goto err_ssl;
}
if( (tls_connect(*tlsres, fqdn, proto)))
{
goto err_ssl;
}
}
return(sd);
err_ssl:
return(0); return(0);
} }
if(tlsres != 0)
{
close(sd);
if( 0 == (*tlsres = tls_client()))
{
goto err_ssl;
}
if( 0 == (tlshints = tls_config_new()))
{
goto err_ssl;
}
if(tls_configure(*tlsres, tlshints))
{
goto err_ssl;
}
if( (tls_connect(*tlsres, fqdn, proto)))
{
goto err_ssl;
}
}
return(sd);
err_ssl:
return(0);
}