Make OpenSSL network implementation compatible with IPv6
@@ -203,7 +203,8 @@ static int _dnsLookup( const IotNetworkServerInfoOpenssl_t * pServerInfo )
int tcpSocket = -1;
const uint16_t netPort = htons( pServerInfo->port );
struct addrinfo * pListHead = NULL, * pAddressInfo = NULL;
- struct sockaddr_in * pServer = NULL;
+ struct sockaddr * pServer = NULL;
+ socklen_t serverLength = 0;
/* Perform a DNS lookup of host name. */
IotLogDebug( "Performing DNS lookup of %s", pServerInfo->pHostName );
@@ -234,15 +235,28 @@ static int _dnsLookup( const IotNetworkServerInfoOpenssl_t * pServerInfo )
continue;
}
- /* Set the port for the connection. */
IotLogDebug( "Socket creation successful. Attempting connection." );
- pServer = ( struct sockaddr_in * ) ( pAddressInfo->ai_addr );
- pServer->sin_port = netPort;
+
+ /* Set the port for the connection based on protocol. */
+ pServer = pAddressInfo->ai_addr;
+
+ if( pServer->sa_family == AF_INET )
+ {
+ /* IPv4. */
+ ( ( struct sockaddr_in * ) pServer )->sin_port = netPort;
+ serverLength = sizeof( struct sockaddr_in );
+ }
+ else
+ {
+ /* IPv6. */
+ ( ( struct sockaddr_in6 * ) pServer )->sin6_port = netPort;
+ serverLength = sizeof( struct sockaddr_in6 );
+ }
/* Attempt to connect. */
if( connect( tcpSocket,
- ( struct sockaddr * ) pServer,
- sizeof( struct sockaddr ) ) == -1 )
+ pServer,
+ serverLength ) == -1 )
{
/* Connect failed; close socket and try next record. */
if( close( tcpSocket ) != 0 )
