[Interest] Convert IPv6 to IPv4 ?

Thiago Macieira thiago.macieira at intel.com
Thu Apr 21 19:02:05 CEST 2016


On quinta-feira, 21 de abril de 2016 14:05:52 PDT Alexander CarĂ´t wrote:
> Hello,
> 
> I noticed that retrieving a sender's IP address of a QWebSocket via
> ->peerAddress() returns an IPv6 socket address.
> 
> For certain reasons I need an IPv4 address so I wonder how it is possible to
> either let ->peerAddress() return an IPv4 address or convert the IPv6
> address to an IPv4 address.

Hello Alex

You probably mean you've got a v4-mapped IPv6 address, as
	::ffff:192.0.2.1

You should code as:

	bool ok;
	quint32 ipv4 = hostaddr.toIPv4Address(&ok);
	if (ok) {
		// check my IPv4 ACL
	} else if (hostaddr.protocol() == QAbstractSocket::IPv6Protocol) {
		// check my IPv6 ACL
	} else {
		// uh... what?
	}


-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list