25 #ifndef KSOCKETADDRESS_H
26 #define KSOCKETADDRESS_H
29 #include <tqcstring.h>
31 #include <kdelibs_export.h>
101 { setAddress(addr); }
110 { setAddress(addr, version); }
123 { setAddress(&ip4addr, 4); }
149 {
return compare(other,
true); }
164 bool compare(
const KIpAddress& other,
bool checkMapped =
true)
const;
171 inline int version()
const
172 {
return m_version; }
177 inline bool isIPv4Addr()
const
178 {
return version() == 4; }
183 inline bool isIPv6Addr()
const
184 {
return version() == 6; }
192 bool setAddress(
const TQString& address);
200 bool setAddress(
const char* address);
210 bool setAddress(
const void* raw,
int version = 4);
215 TQString toString()
const;
220 inline const void *addr()
const
235 inline TQ_UINT32 IPv4Addr(
bool convertMapped =
true)
const
237 return (convertMapped && isV4Mapped()) ? m_data[3] : m_data[0];
248 TQ_UINT32 hostIPv4Addr(
bool convertMapped =
true)
const;
256 inline bool isUnspecified()
const
257 {
return version() == 0 ?
true : (*
this == anyhostV4 || *
this == anyhostV6); }
262 inline bool isLocalhost()
const
263 {
return version() == 0 ?
false : (*
this == localhostV4 || *
this == localhostV6); }
268 inline bool isLoopback()
const
269 {
return isLocalhost(); }
277 inline bool isClassA()
const
278 {
return version() != 4 ?
false : (hostIPv4Addr() & 0x80000000) == 0; }
286 inline bool isClassB()
const
287 {
return version() != 4 ?
false : (hostIPv4Addr() & 0xc0000000) == 0x80000000; }
295 inline bool isClassC()
const
296 {
return version() != 4 ?
false : (hostIPv4Addr() & 0xe0000000) == 0xc0000000; }
304 inline bool isClassD()
const
305 {
return version() != 4 ?
false : (hostIPv4Addr() & 0xf0000000) == 0xe0000000; }
310 inline bool isMulticast()
const
312 if (version() == 4)
return isClassD();
313 if (version() == 6)
return ((TQ_UINT8*)addr())[0] == 0xff;
320 inline bool isLinkLocal()
const
322 if (version() != 6)
return false;
323 TQ_UINT8* addr = (TQ_UINT8*)this->addr();
324 return (addr[0] & 0xff) == 0xfe &&
325 (addr[1] & 0xc0) == 0x80;
331 inline bool isSiteLocal()
const
333 if (version() != 6)
return false;
334 TQ_UINT8* addr = (TQ_UINT8*)this->addr();
335 return (addr[0] & 0xff) == 0xfe &&
336 (addr[1] & 0xc0) == 0xc0;
342 inline bool isGlobal()
const
343 {
return version() != 6 ?
false : !(isMulticast() || isLinkLocal() || isSiteLocal()); }
348 inline bool isV4Mapped()
const
350 if (version() != 6)
return false;
351 TQ_UINT32* addr = (TQ_UINT32*)this->addr();
352 return addr[0] == 0 && addr[1] == 0 &&
353 ((TQ_UINT16*)&addr[2])[0] == 0 &&
354 ((TQ_UINT16*)&addr[2])[1] == 0xffff;
360 inline bool isV4Compat()
const
362 if (version() != 6 || isLocalhost())
return false;
363 TQ_UINT32* addr = (TQ_UINT32*)this->addr();
364 return addr[0] == 0 && addr[1] == 0 && addr[2] == 0 && addr[3] != 0;
370 inline bool isMulticastNodeLocal()
const
371 {
return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0x1; }
376 inline bool isMulticastLinkLocal()
const
377 {
return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0x2; }
382 inline bool isMulticastSiteLocal()
const
383 {
return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0x5; }
388 inline bool isMulticastOrgLocal()
const
389 {
return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0x8; }
394 inline bool isMulticastGlobal()
const
395 {
return version() == 6 && isMulticast() && (((TQ_UINT32*)addr())[0] & 0xf) == 0xe; }
415 class KSocketAddressData;
472 const sockaddr* address()
const;
499 inline operator const sockaddr*()
const
500 {
return address(); }
505 TQ_UINT16 length()
const;
550 inline int ianaFamily()
const
572 virtual TQString nodeName()
const;
583 virtual TQString serviceName()
const;
591 virtual TQString toString()
const;
618 KSocketAddressData *d;
632 static int ianaFamily(
int af);
638 static int fromIanaFamily(
int iana);
715 inline operator const sockaddr_in*()
const
716 {
return (
const sockaddr_in*)
address(); }
721 inline operator const sockaddr_in6*()
const
722 {
return (
const sockaddr_in6*)
address(); }
729 int ipVersion()
const;
753 TQ_UINT16 port()
const;
790 TQ_UINT32 flowinfo()
const;
888 inline operator const sockaddr_un*()
const
889 {
return (
const sockaddr_un*)
address(); }
895 TQString pathname()
const;