apportate/src/connect.c

56 lines
816 B
C
Raw Normal View History

2022-11-24 08:18:05 +00:00
#include "headers.h"
int dial(const char *fqdn, const char *proto, struct tls **tlsres)
2022-11-24 08:18:05 +00:00
{
int sd;
struct addrinfo *ainfo;
struct tls_config *tlshints;
2022-11-24 08:18:05 +00:00
if( !(sd = socket(AF_INET, SOCK_STREAM, 0)))
{
return(0);
}
if(getaddrinfo(fqdn, proto, 0, &ainfo))
2022-11-24 08:18:05 +00:00
{
return(0);
}
if(connect(sd, ainfo->ai_addr, sizeof(struct sockaddr)))
{
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;
}
}
2022-11-24 08:18:05 +00:00
return(sd);
err_ssl:
return(0);
2022-11-24 08:18:05 +00:00
}