File network-byte-order.patch of Package knetworkmanager-kde3
--- knetworkmanager/src/knetworkmanager-connection_setting_ipv4.cpp
+++ knetworkmanager/src/knetworkmanager-connection_setting_ipv4.cpp
@@ -22,6 +22,8 @@
*
**************************************************************************/
+#include <netinet/in.h>
+
/* qt headers */
#include <qhostaddress.h>
#include <qvariant.h>
@@ -41,19 +43,6 @@
using namespace ConnectionSettings;
-// reverse order the bytes
-Q_UINT32 swap32(Q_UINT32 x)
-{
- Q_UINT32 ret = 0;
-
- Q_UINT8* from = (Q_UINT8*) &x;
- Q_UINT8* to = (Q_UINT8*) &ret;
-
- for (int i = 0; i < 4; ++i)
- to[3-i] = from[i];
- return ret;
-}
-
/*
class IPv4
*/
@@ -204,7 +193,7 @@ IPv4::toMap() const
QValueList<QHostAddress>::ConstIterator it_dns = _dns.begin();
// the strange swap32 is needed as NM reads the address exactly the other way round as Qt
for(;it_dns != _dns.end(); ++it_dns)
- dns.append(QDBusData::fromUInt32(swap32((*it_dns).toIPv4Address())));
+ dns.append(QDBusData::fromUInt32(htonl((*it_dns).toIPv4Address())));
map.insert(NM_SETTING_IP4_CONFIG_DNS, QDBusData::fromQValueList(dns));
}
@@ -216,10 +205,10 @@ IPv4::toMap() const
for (QValueList<IPv4Address>::ConstIterator it = _addresses.begin(); it != _addresses.end(); ++it)
{
QValueList<QDBusData> cur_ip;
- cur_ip.append(QDBusData::fromUInt32(swap32((*it).address.toIPv4Address())));
+ cur_ip.append(QDBusData::fromUInt32(htonl((*it).address.toIPv4Address())));
cur_ip.append(QDBusData::fromUInt32(toCIDRSuffix((*it).netmask)));
if (!(*it).gateway.isNull())
- cur_ip.append(QDBusData::fromUInt32(swap32((*it).gateway.toIPv4Address())));
+ cur_ip.append(QDBusData::fromUInt32(htonl((*it).gateway.toIPv4Address())));
ips.append(QDBusData::fromQValueList(cur_ip));
}
map.insert(NM_SETTING_IP4_CONFIG_ADDRESSES, QDBusData::fromQValueList(ips));
@@ -263,7 +252,7 @@ IPv4::fromMap(const SettingsMap& map)
QValueList<QDBusData> dns = it.data().toQValueList();
for (QValueList<QDBusData>::Iterator it = dns.begin(); it != dns.end(); ++it)
{
- _dns.append( QHostAddress(swap32((*it).toUInt32())) );
+ _dns.append( QHostAddress(ntohl((*it).toUInt32())) );
}
}
@@ -276,15 +265,15 @@ IPv4::fromMap(const SettingsMap& map)
QValueList<QDBusData> cur_ip = (*it2).toQValueList();
IPv4Address address;
- address.address = swap32(cur_ip[0].toUInt32());
+ address.address = ntohl(cur_ip[0].toUInt32());
if (cur_ip[1].toUInt32() >= 0 && cur_ip[1].toUInt32() <= 32)
address.netmask = fromCIDRSuffix(cur_ip[1].toUInt32());
else
- address.netmask = swap32(cur_ip[1].toUInt32());
+ address.netmask = ntohl(cur_ip[1].toUInt32());
if (cur_ip.size() > 2)
- address.gateway = swap32(cur_ip[2].toUInt32());
+ address.gateway = ntohl(cur_ip[2].toUInt32());
_addresses.append(address);
}
@@ -312,7 +301,10 @@ Q_UINT32 IPv4::toCIDRSuffix(const QHostA
QHostAddress IPv4::fromCIDRSuffix(Q_UINT32 suffix)
{
Q_UINT32 netmask = 0xFFFFFFFF;
- netmask = netmask << (32 - suffix);
+ if (suffix > 0)
+ netmask = netmask << (32 - suffix);
+ else
+ netmask = 0;
return QHostAddress(netmask);
}