[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: Endian bug in KNetwork::KIpAddress
From:       Nadeem Hasan <nhasan () nadmm ! com>
Date:       2006-07-22 14:42:28
Message-ID: 200607221042.32122.nhasan () nadmm ! com
[Download RAW message or body]

[Attachment #2 (multipart/mixed)]


On Saturday 22 July 2006 09:18, Thiago Macieira wrote:

> Please don't #include <arpa/inet.h> in the public header.
>
> Add a new function that returns the ntohl of the IPv4 address and call
> ntohl from it.

Try #2.

I had to use the underscore as ntohl() is defined as a macro.
-- 
Nadeem Hasan (nhasan-at-nadmm.com)
GPG Fingerprint: 7141 0B1C 9CAF 624D 307F F8EF 6C0C 753E DD3A 0F53

["ksocketaddress2.diff" (text/x-diff)]

Index: ksocketaddress.cpp
===================================================================
--- ksocketaddress.cpp	(revision 565100)
+++ ksocketaddress.cpp	(working copy)
@@ -206,6 +206,11 @@
   return QString::null;
 }
 
+uint32_t KIpAddress::_ntohl(uint32_t hostlong) const
+{
+  return ntohl( hostlong );
+}
+
 /*
  * An IPv6 socket address
  * This is taken from RFC 2553.
Index: ksocketaddress.h
===================================================================
--- ksocketaddress.h	(revision 565100)
+++ ksocketaddress.h	(working copy)
@@ -225,7 +225,7 @@
    * 32-bit integer. The result is only valid if @ref isIPv4Addr returns
    * true. Alternatively, if the contained IPv6 address is a v4-mapped one
    * and the @p convertMapped parameter is true, the result will also be
-   * valid.
+   * valid. The address returned is in network byte order.
    *
    * Note: you should not treat IP addresses as integers. Instead,
    * use types defined for that purpose, such as KIpAddress or the
@@ -238,6 +238,10 @@
     return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
   }
 
+protected:
+  Q_UINT32 _ntohl(Q_UINT32 hostlong) const;
+
+public:
   /*-- tests --*/
 
   /**
@@ -265,7 +269,7 @@
    * This function does not test for v4-mapped addresses.
    */
   inline bool isClassA() const
-  { return version() != 4 ? false : (IPv4Addr() & 0x80000000) == 0; }
+  { return version() != 4 ? false : (_ntohl(IPv4Addr()) & 0x80000000) == 0; }
 
   /**
    * Returns true if this is an IPv4 class B address, i.e., one from
@@ -274,7 +278,7 @@
    * This function does not test for v4-mapped addresses.
    */
   inline bool isClassB() const
-  { return version() != 4 ? false : (IPv4Addr() & 0xc0000000) == 0x80000000; }
+  { return version() != 4 ? false : (_ntohl(IPv4Addr()) & 0xc0000000) == 0x80000000; }
 
   /**
    * Returns true if this is an IPv4 class C address, i.e., one from
@@ -283,7 +287,7 @@
    * This function does not test for v4-mapped addresses.
    */
   inline bool isClassC() const
-  { return version() != 4 ? false : (IPv4Addr() & 0xe0000000) == 0xc0000000; }
+  { return version() != 4 ? false : (_ntohl(IPv4Addr()) & 0xe0000000) == 0xc0000000; }
 
   /**
    * Returns true if this is an IPv4 class D (a.k.a. multicast) address.
@@ -292,7 +296,7 @@
    * tests for IPv6 multicast addresses.
    */
   inline bool isClassD() const
-  { return version() != 4 ? false : (IPv4Addr() & 0xf0000000) == 0xe0000000; }
+  { return version() != 4 ? false : (_ntohl(IPv4Addr()) & 0xf0000000) == 0xe0000000; }
 
   /**
    * Returns true if this is a multicast address, be it IPv4 or IPv6.

[Attachment #6 (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic