21 #include "ksockaddr.h" 24 #include <sys/types.h> 27 #include <arpa/inet.h> 29 #include <netinet/in.h> 44 #ifndef HAVE_STRUCT_SOCKADDR_IN6 52 #define V6_CAN_CONVERT_TO_V4(addr) (KDE_IN6_IS_ADDR_V4MAPPED(addr) || KDE_IN6_IS_ADDR_V4COMPAT(addr)) 54 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 55 # define MY_MAX(a, b) ((a) > (b) ? (a) : (b)) 56 # define MIN_SOCKADDR_LEN MY_MAX(offsetof(sockaddr, sa_family) + sizeof(((sockaddr*)0)->sa_family), \ 57 offsetof(sockaddr, sa_len) + sizeof(((sockaddr*)0)->sa_len)) 59 # define MIN_SOCKADDR_LEN (offsetof(sockaddr, sa_family) + sizeof(((sockaddr*)0)->sa_family)) 65 #define MIN_SOCKADDR_IN6_LEN (offsetof(sockaddr_in6, sin6_addr) + sizeof(((sockaddr_in6*)0)->sin6_addr)) 70 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 74 #ifndef INET6_ADDRSTRLEN 75 #define INET6_ADDRSTRLEN 46 88 data = (sockaddr*)malloc(size);
91 memcpy(data, sa, size);
97 void KSocketAddress::init()
106 if (owndata && data != NULL)
112 return i18n(
"<unknown socket>");
118 return data->sa_family;
127 kdWarning() <<
"KSocketAddress::newAddress called with size = 0!\n";
132 if (size < MIN_SOCKADDR_LEN)
134 kdWarning() <<
"KSocketAddress::newAddress called with invalid size\n";
138 switch (sa->sa_family)
141 if (size >=
sizeof(sockaddr_in))
147 if (size >= MIN_SOCKADDR_IN6_LEN)
174 if (other.datasize != datasize)
176 return memcmp(data, other.data, datasize) == 0;
198 return TQString::null;
203 return TQString::null;
239 class KInetSocketAddressPrivate
248 KInetSocketAddressPrivate() :
249 sockfamily(AF_UNSPEC)
251 sin.sin_family = AF_INET;
253 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 254 sin.sin_len =
sizeof(sin);
257 sin6.sin6_family = AF_INET6;
259 sin6.sin6_flowinfo = 0;
260 # ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID 261 sin6.sin6_scope_id = 0;
263 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 264 sin6.sin6_len =
sizeof(sin6);
272 d(new KInetSocketAddressPrivate)
283 d(new KInetSocketAddressPrivate)
289 d(new KInetSocketAddressPrivate)
295 d(new KInetSocketAddressPrivate)
301 d(new KInetSocketAddressPrivate)
307 d(new KInetSocketAddressPrivate)
321 if (other.
family() == AF_INET)
324 else if (other.
family() == AF_INET6)
333 if ((len <
sizeof(sockaddr_in)) || (sin->sin_family != AF_INET))
335 kdWarning() <<
"KInetSocketAddress::setAddress(sockaddr_in*) called with invalid sockaddr_in\n";
346 if ((len < MIN_SOCKADDR_IN6_LEN) || (sin6->sin6_family != AF_INET6))
348 kdWarning() <<
"KInetSocketAddress::setAddress(sockaddr_in6*) called with invalid sockaddr_in6\n";
352 memset(&d->sin6, 0,
sizeof(d->sin6));
353 if (len >
sizeof(d->sin6))
354 len =
sizeof(d->sin6);
355 memcpy(&d->sin6, sin6, len);
358 d->sockfamily = d->sin6.sin6_family = AF_INET6;
359 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 360 d->sin6.sin6_len =
sizeof(d->sin6);
387 d->sockfamily = AF_INET;
388 d->sin.sin_addr = addr;
396 d->sockfamily = AF_INET6;
397 d->sin6.sin6_addr = addr;
408 if ((family != -1) && (family != AF_INET)
410 && (family != AF_INET6)
414 kdWarning() <<
"KInetSocketAddress::setHost(TQString, int) called with unknown family address\n";
424 if (addr.find(
':') != -1)
438 if (family == AF_INET)
440 inet_pton(family, addr.latin1(), (
void*)&(d->sin.sin_addr));
446 inet_pton(family, addr.latin1(), (
void*)&(d->sin6.sin6_addr));
457 d->sin.sin_port = htons(port);
459 d->sin6.sin6_port = htons(port);
467 if (_family != AF_INET
469 && _family != AF_INET6
473 kdWarning() <<
"KInetSocketAddress::setFamily(int) called with unknown family\n";
477 d->sockfamily = _family;
478 if (_family == AF_INET)
481 else if (_family == AF_INET6)
491 if (d->sockfamily == AF_INET6)
502 #if defined(AF_INET6) && defined(HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID) 503 if (d->sockfamily == AF_INET6)
505 d->sin6.sin6_scope_id = scopeid;
515 if (d->sockfamily == AF_INET)
518 else if (d->sockfamily == AF_INET6)
521 if (V6_CAN_CONVERT_TO_V4(&d->sin6.sin6_addr))
528 kdWarning() <<
"KInetSocketAddress::addressV4() called on uninitialized socket\n";
544 return d->sin.sin_addr;
555 return d->sin6.sin6_addr;
561 if (d->sockfamily != AF_INET
563 && d->sockfamily != AF_INET6
567 kdWarning() <<
"KInetSocketAddress::pretty() called on uninitialized class\n";
568 return i18n(
"<empty>");
571 return i18n(
"1: hostname, 2: port number",
"%1 port %2").arg(
nodeName()).arg(
serviceName());
576 char buf[INET6_ADDRSTRLEN];
578 if (d->sockfamily == AF_INET)
579 inet_ntop(d->sockfamily, (
void*)&d->sin.sin_addr, buf,
sizeof(buf));
581 else if (d->sockfamily == AF_INET6)
582 inet_ntop(d->sockfamily, (
void*)&d->sin6.sin6_addr, buf,
sizeof(buf));
586 kdWarning() <<
"KInetSocketAddress::nodeName() called on uninitialized class\n";
587 return i18n(
"<empty>");
590 return TQString::fromLatin1(buf);
595 return TQString::number(
port());
602 return ntohs(d->sin6.sin6_port);
604 return ntohs(d->sin.sin_port);
611 if (d->sockfamily == AF_INET6)
612 return (TQ_UINT32)d->sin6.sin6_flowinfo;
619 if (d->sockfamily == AF_INET)
620 return sizeof(d->sin);
622 else if (d->sockfamily == AF_INET6)
623 return sizeof(d->sin6);
633 if ((s1.
size() <
sizeof(sockaddr_in)) || (s2.
size() <
sizeof(sockaddr_in)))
636 struct sockaddr_in *sin1 = (sockaddr_in *) s1.
address();
637 struct sockaddr_in *sin2 = (sockaddr_in *) s2.
address();
640 return (memcmp(&sin1->sin_addr, &sin2->sin_addr,
sizeof(
struct in_addr)) == 0);
642 return (sin1->sin_port == sin2->sin_port) &&
643 (memcmp(&sin1->sin_addr, &sin2->sin_addr,
sizeof(
struct in_addr)) == 0);
652 if ((s1.
size() <
sizeof(sockaddr_in6)) || (s2.
size() <
sizeof(sockaddr_in6)))
655 struct sockaddr_in6 *sin1 = (sockaddr_in6 *) s1.
address();
656 struct sockaddr_in6 *sin2 = (sockaddr_in6 *) s2.
address();
659 return (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
sizeof(
struct in6_addr)) == 0);
661 return (sin1->sin6_port == sin2->sin6_port) &&
662 (sin1->sin6_flowinfo == sin2->sin6_flowinfo) &&
663 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
664 (sin1->sin6_scope_id == sin2->sin6_scope_id) &&
666 (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
sizeof(
struct in6_addr)) == 0);
672 void KInetSocketAddress::fromV4()
677 d->sin6.sin6_port = d->sin.sin_port;
680 ((TQ_UINT32*)&d->sin6.sin6_addr)[0] = ((TQ_UINT32*)&d->sin6.sin6_addr)[1] = 0;
681 ((TQ_UINT32*)&d->sin6.sin6_addr)[2] = htonl(0xffff);
682 ((TQ_UINT32*)&d->sin6.sin6_addr)[3] = *(TQ_UINT32*)&d->sin.sin_addr;
685 d->sin6.sin6_flowinfo = 0;
686 # ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
687 d->sin6.sin6_scope_id = 0;
692 data = (sockaddr*)&d->sin;
693 datasize =
sizeof( sockaddr_in );
696 void KInetSocketAddress::fromV6()
700 if (V6_CAN_CONVERT_TO_V4(&d->sin6.sin6_addr))
702 d->sin.sin_port = d->sin6.sin6_port;
703 *(TQ_UINT32*)&d->sin.sin_addr = ((TQ_UINT32*)&d->sin6.sin6_addr)[3];
708 memset(&d->sin.sin_addr, 0,
sizeof(d->sin.sin_addr));
711 data = (sockaddr*)&d->sin6;
712 datasize =
sizeof( d->sin6 );
718 char buf[INET6_ADDRSTRLEN+1];
720 return TQString::fromLatin1(inet_ntop(family, addr, buf, INET6_ADDRSTRLEN));
725 return inet_pton(family, text, dest) != 0;
732 class KUnixSocketAddressPrivate
737 KUnixSocketAddressPrivate() : m_sun(NULL)
742 d(new KUnixSocketAddressPrivate)
747 d(new KUnixSocketAddressPrivate)
753 d(new KUnixSocketAddressPrivate)
765 if (_sun->sun_family != AF_UNIX)
767 kdWarning() <<
"KUnixSocketAddress::setAddress called with invalid socket\n";
771 if (owndata && (d->m_sun != NULL) && (datasize >= _size))
774 memcpy(d->m_sun, _sun, _size);
778 if (owndata && (d->m_sun != NULL))
781 d->m_sun = (sockaddr_un*)malloc(_size);
783 if (d->m_sun == NULL)
790 memcpy(d->m_sun, _sun, _size);
794 data = (sockaddr*)d->m_sun;
796 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
797 data->sa_len = _size;
805 ksocklen_t newsize = offsetof(sockaddr_un, sun_path) + path.length() + 1;
807 if (owndata && (d->m_sun != NULL) && (datasize >= newsize))
810 strcpy(d->m_sun->sun_path, path);
811 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 812 data->sa_len = newsize;
818 if (owndata && (d->m_sun != NULL))
821 d->m_sun = (sockaddr_un*) malloc(newsize);
822 if (d->m_sun == NULL)
828 d->m_sun->sun_family = AF_UNIX;
829 strcpy(d->m_sun->sun_path, path);
830 data = (sockaddr*)d->m_sun;
832 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
833 data->sa_len = newsize;
840 if (d->m_sun != NULL)
842 if (datasize > offsetof(sockaddr_un, sun_path))
843 return d->m_sun->sun_path;
853 return i18n(
"<empty UNIX socket>");
854 return TQFile::decodeName(
pathname());
859 return TQString::fromUtf8(
pathname());
872 if ((s1.
size() < MIN_SOCKADDR_LEN) || (s2.
size() < MIN_SOCKADDR_LEN))
875 struct sockaddr_un *sun1 = (sockaddr_un *) s1.
address();
876 struct sockaddr_un *sun2 = (sockaddr_un *) s2.
address();
878 if (s1.
size() == MIN_SOCKADDR_LEN && s2.
size() == MIN_SOCKADDR_LEN)
881 return (strcmp(sun1->sun_path, sun2->sun_path) == 0);
884 void KSocketAddress::virtual_hook(
int,
void* )
887 void KInetSocketAddress::virtual_hook(
int id,
void* data )
888 { KSocketAddress::virtual_hook(
id, data ); }
890 void KUnixSocketAddress::virtual_hook(
int id,
void* data )
891 { KSocketAddress::virtual_hook(
id, data ); }
894 #include "ksockaddr.moc" static bool areEqualUnix(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly)
Compares two unix socket addresses.
static TQString addrToString(int family, const void *addr)
Convert s the given raw address into text form.
bool setFlowinfo(TQ_UINT32 flowinfo)
Sets flowinfo information for this socket address if this is IPv6.
virtual TQString pretty() const
Returns a pretty representation of this address.
virtual bool isEqual(const KSocketAddress &other) const
Returns true if this equals the other socket.
static KSocketAddress * newAddress(const struct sockaddr *sa, ksocklen_t size)
Creates a new KSocketAddress or descendant class from given raw socket address.
bool setScopeId(int scopeid)
Sets the scope id for this socket if this is IPv6.
unsigned short port() const
Returns the port number.
An Inet (IPv4 or IPv6) socket address.
const sockaddr * address() const
Returns a sockaddr structure, for passing down to library functions.
int family() const
Returns the family of this address.
virtual TQString serviceName() const
Returns the text representation of the port number.
in_addr hostV4() const
Returns the host address.
bool isCoreEqual(const KSocketAddress &other) const
Some sockets may differ in such things as services or port numbers, like Internet sockets...
static bool areEqualInet6(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly)
Compares two IPv6 addresses.
KInetSocketAddress()
Default constructor.
TQ_UINT32 flowinfo() const
Returns flowinfo for IPv6 socket.
bool setPort(unsigned short port)
Sets this socket's port number to given port number.
virtual ~KUnixSocketAddress()
Destructor.
virtual ~KInetSocketAddress()
Destructor.
static bool areEqualInet(const KSocketAddress &s1, const KSocketAddress &s2, bool coreOnly)
Compares two IPv4 addresses.
virtual TQString pretty() const
Returns a string representation of this socket.
bool setFamily(int family)
Turns this into an IPv4 or IPv6 address.
virtual TQString serviceName() const
Returns the service name for this socket, as KExtendedSocket::lookup expects as the service argument...
const sockaddr_in * addressV4() const
Returns the socket address.
TQCString pathname() const
Returns the pathname.
virtual TQString nodeName() const
Returns the node name of this socket, as KExtendedSocket::lookup expects as the first argument...
virtual ksocklen_t size() const
Returns sockaddr structure size.
virtual ksocklen_t size() const
Returns the socket length.
virtual TQString pretty() const
Returns pretty representation of this socket.
virtual ~KSocketAddress()
Destructor.
KSocketAddress()
Creates an empty class.
virtual TQString nodeName() const
Returns the text representation of the host address.
bool setAddress(const sockaddr_un *socket_address, ksocklen_t size)
Sets this to given sockaddr_un.
const sockaddr_un * address() const
Returns raw socket address.
static bool stringToAddr(int family, const char *text, void *dest)
Converts the address given in text form into raw form.
in6_addr hostV6() const
Returns the host address.
bool setHost(const in_addr &addr)
Sets this socket's host address to given raw address.
int ianaFamily() const
Returns the IANA family number of this address.
static int fromIanaFamily(int iana)
Returns the address family of the given IANA family number.
virtual TQString serviceName() const
Returns the service name for this socket, as KExtendedSocket::lookup expects as the service argument...
const sockaddr_in6 * addressV6() const
Returns the socket address in IPv6.
bool setAddress(const KInetSocketAddress &ksa)
Sets this socket to given socket.
KUnixSocketAddress()
Default constructor.