23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/times.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
41 #include <tqiodevice.h>
42 #include <tqsocketnotifier.h>
43 #include <tqguardedptr.h>
45 #include "kresolver.h"
49 #include "ksockaddr.h"
62 class KExtendedSocketPrivate
78 TQSocketNotifier *qsnIn, *qsnOut;
79 int inMaxSize, outMaxSize;
80 bool emitRead : 1, emitWrite : 1;
81 mutable bool addressReusable : 1, ipv6only : 1;
83 KExtendedSocketPrivate() :
84 flags(0), status(0), syserror(0),
85 current(0), local(0), peer(0),
86 qsnIn(0), qsnOut(0), inMaxSize(-1), outMaxSize(-1), emitRead(false), emitWrite(false),
87 addressReusable(false), ipv6only(false)
89 timeout.tv_sec = timeout.tv_usec = 0;
94 static bool process_flags(
int flags,
int& socktype,
int& familyMask,
int& outflags)
96 switch (flags & (KExtendedSocket::streamSocket | KExtendedSocket::datagramSocket | KExtendedSocket::rawSocket))
101 case KExtendedSocket::streamSocket:
103 socktype = SOCK_STREAM;
106 case KExtendedSocket::datagramSocket:
108 socktype = SOCK_DGRAM;
111 case KExtendedSocket::rawSocket:
121 if (flags & KExtendedSocket::knownSocket)
124 if ((flags & KExtendedSocket::unixSocket) == KExtendedSocket::unixSocket)
125 familyMask |= KResolver::UnixFamily;
127 switch ((flags & (KExtendedSocket::ipv6Socket|KExtendedSocket::ipv4Socket)))
129 case KExtendedSocket::ipv4Socket:
130 familyMask |= KResolver::IPv4Family;
132 case KExtendedSocket::ipv6Socket:
133 familyMask |= KResolver::IPv6Family;
135 case KExtendedSocket::inetSocket:
136 familyMask |= KResolver::InternetFamily;
143 familyMask = KResolver::KnownFamily;
146 outflags = (flags & KExtendedSocket::passiveSocket ? KResolver::Passive : 0) |
147 (flags & KExtendedSocket::canonName ? KResolver::CanonName : 0) |
148 (flags & KExtendedSocket::noResolve ? KResolver::NoResolve : 0);
150 if (getenv(
"TDE_NO_IPV6"))
151 familyMask &= ~KResolver::IPv6Family;
160 static int skipData(
int fd,
unsigned len)
163 unsigned skipped = 0;
166 int count =
sizeof(buf);
167 if ((
unsigned)count > len)
187 sockfd(-1), d(new KExtendedSocketPrivate)
193 sockfd(-1), d(new KExtendedSocketPrivate)
201 sockfd(-1), d(new KExtendedSocketPrivate)
212 if (d->local != NULL)
217 if (d->qsnIn != NULL)
219 if (d->qsnOut != NULL)
226 void KExtendedSocket::reset()
229 bool KExtendedSocket::reset()
246 d->status = newstatus;
251 setStatus(errorcode);
252 d->syserror = syserror;
266 if (d->status > nothing)
269 return d->flags = flags;
283 if (d->status > nothing)
286 d->resRemote.setNodeName(host);
295 return d->resRemote.nodeName();
304 return setPort(TQString::number(port));
309 if (d->status > nothing)
312 d->resRemote.setServiceName(service);
321 return d->resRemote.serviceName();
347 if (d->status > nothing || d->flags & passiveSocket)
350 d->resLocal.setServiceName(host);
368 return d->resLocal.serviceName();
382 if (d->status > nothing || d->flags & passiveSocket)
385 d->resLocal.setServiceName(service);
402 return d->resLocal.serviceName();
434 if (d->status >= connected)
437 d->timeout.tv_sec = secs;
438 d->timeout.tv_usec = usecs;
456 if (d->status < created)
462 int fdflags = fcntl(sockfd, F_GETFL, 0);
467 fdflags |= O_NONBLOCK;
469 fdflags &= ~O_NONBLOCK;
471 if (fcntl(sockfd, F_SETFL, fdflags) == -1)
473 setError(IO_UnspecifiedError, errno);
485 if (d->status < created)
491 int fdflags = fcntl(sockfd, F_GETFL, 0);
494 setError(IO_UnspecifiedError, errno);
497 return (fdflags & O_NONBLOCK) == 0;
506 d->addressReusable = enable;
507 if (d->status < created)
515 setError(IO_UnspecifiedError, errno);
528 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (
char*)&on,
sizeof(on)) == -1)
539 if (d->status < created)
540 return d->addressReusable;
543 return d->addressReusable;
546 socklen_t onsiz =
sizeof(on);
547 if (getsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (
char*)&on, &onsiz) == -1)
549 setError(IO_UnspecifiedError, errno);
564 d->ipv6only = enable;
570 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
571 (
char *)&on,
sizeof(on)) == -1)
573 setError(IO_UnspecifiedError, errno);
581 d->ipv6only = enable;
583 setError(IO_UnspecifiedError, ENOSYS);
596 if (d->status < created || sockfd == -1)
600 socklen_t onsiz =
sizeof(on);
601 if (getsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
602 (
char *)&on, &onsiz) == -1)
604 setError(IO_UnspecifiedError, errno);
608 return d->ipv6only = on;
612 setError(IO_UnspecifiedError, ENOSYS);
624 if (d->status < created)
630 if (d->flags & passiveSocket)
644 if (d->qsnIn == NULL)
646 d->qsnIn =
new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
647 TQObject::connect(d->qsnIn, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(socketActivityRead()));
648 d->qsnIn->setEnabled(
true);
651 if (rsize == 0 && d->flags & inputBufferedSocket)
654 d->flags &= ~inputBufferedSocket;
659 else if (rsize != -2)
663 d->flags |= inputBufferedSocket;
664 d->inMaxSize = rsize;
672 if (wsize == 0 && d->flags & outputBufferedSocket)
675 d->flags &= ~outputBufferedSocket;
676 if (d->qsnOut && !d->emitWrite)
677 d->qsnOut->setEnabled(
false);
681 else if (wsize != -2)
685 d->flags |= outputBufferedSocket;
686 d->outMaxSize = wsize;
692 if (d->qsnOut == NULL)
694 d->qsnOut =
new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
695 TQObject::connect(d->qsnOut, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(socketActivityWrite()));
704 setFlags((mode() & ~IO_Raw) | ((d->flags & bufferedSocket) ? 0 : IO_Raw));
707 if (d->emitWrite && d->qsnOut == NULL)
709 d->qsnOut =
new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
710 TQObject::connect(d->qsnOut, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(socketActivityWrite()));
723 if (d->local != NULL)
725 if (d->status < bound)
740 if (d->flags & passiveSocket || d->status < connected)
754 if (!d->resRemote.wait() || !d->resLocal.wait())
760 d->status = lookupDone;
761 if (d->resRemote.error() != KResolver::NoError)
762 return d->resRemote.error();
763 if (d->resLocal.error() != KResolver::NoError)
764 return d->resLocal.error();
774 if (d->status > lookupInProgress)
776 if (d->status == lookupInProgress)
781 int socktype, familyMask, flags;
782 if (!process_flags(d->flags, socktype, familyMask, flags))
786 if (!d->resRemote.isRunning())
788 d->resRemote.setFlags(flags);
789 d->resRemote.setFamily(familyMask);
790 d->resRemote.setSocketType(socktype);
792 this, TQT_SLOT(dnsResultsReady()));
794 if (!d->resRemote.start())
796 setError(IO_LookupError, d->resRemote.error());
797 return d->resRemote.error();
801 if ((d->flags & passiveSocket) == 0 && !d->resLocal.isRunning())
804 flags |= KResolver::Passive;
805 d->resLocal.setFlags(flags);
806 d->resLocal.setFamily(familyMask);
807 d->resLocal.setSocketType(socktype);
809 this, TQT_SLOT(dnsResultsReady()));
811 if (!d->resLocal.start())
813 setError(IO_LookupError, d->resLocal.error());
814 return d->resLocal.error();
819 if (d->resRemote.isRunning() || d->resLocal.isRunning())
820 d->status = lookupInProgress;
823 d->status = lookupDone;
825 d->resLocal.results().count());
833 if (d->status != lookupInProgress)
837 d->resLocal.cancel(
false);
838 d->resRemote.cancel(
false);
844 if ((d->flags & passiveSocket) == 0 || d->status >= listening)
846 if (d->status < lookupDone)
849 if (d->resRemote.error())
853 KResolverResults::const_iterator it;
855 for (it = res.begin(); it != res.end(); ++it)
858 sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
866 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
868 if (d->addressReusable)
872 if (
KSocks::self()->bind(sockfd, (*it).address().address(), (*it).length()) == -1)
895 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);
902 d->status = listening;
903 d->qsnIn =
new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
904 TQObject::connect(d->qsnIn, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(socketActivityRead()));
906 return retval == -1 ? -1 : 0;
913 if ((d->flags & passiveSocket) == 0 || d->status >= accepting)
915 if (d->status < listening)
924 ksocklen_t len =
sizeof(sa);
927 if (d->timeout.tv_sec > 0 || d->timeout.tv_usec > 0)
933 FD_SET(sockfd, &set);
941 setError(IO_UnspecifiedError, errno);
944 else if (retval == 0 || !FD_ISSET(sockfd, &set))
957 kdWarning(170) <<
"Error accepting on socket " << sockfd <<
":"
962 fcntl(newfd, F_SETFD, FD_CLOEXEC);
969 sock->d->status = connected;
970 sock->sockfd = newfd;
971 sock->setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
986 if (d->flags & passiveSocket || d->status >= connected)
988 if (d->status < lookupDone)
993 timeval timeout_copy = d->timeout;
1001 local = d->resLocal.results();
1002 KResolverResults::const_iterator it, it2;
1008 for (it = remote.begin(), it2 = local.begin(); it != remote.end(); ++it)
1010 bool doingtimeout = d->timeout.tv_sec > 0 || d->timeout.tv_usec > 0;
1013 gettimeofday(&end, NULL);
1014 end.tv_usec += d->timeout.tv_usec;
1015 end.tv_sec += d->timeout.tv_sec;
1016 if (end.tv_usec > 1000*1000)
1018 end.tv_usec -= 1000*1000;
1026 if (it2 != local.end())
1029 if ((*it).family() != (*it2).family())
1031 for (it2 = local.begin(); it2 != local.end(); ++it2)
1032 if ((*it).family() == (*it2).family())
1035 if ((*it).family() != (*it2).family())
1039 it2 = local.begin();
1045 sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
1049 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1050 if (d->addressReusable)
1054 if (
KSocks::self()->bind(sockfd, (*it2).address(), (*it2).length()))
1065 sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
1071 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1072 if (d->addressReusable)
1079 d->status = created;
1082 if (doingtimeout &&
KSocks::self()->hasWorkingAsyncConnect())
1092 if (errno != EWOULDBLOCK && errno != EINPROGRESS)
1103 FD_SET(sockfd, &rd);
1104 FD_SET(sockfd, &wr);
1112 else if (retval == 0)
1121 d->timeout.tv_usec += timeout_copy.tv_usec;
1122 d->timeout.tv_sec += timeout_copy.tv_sec;
1123 if (d->timeout.tv_usec < 0)
1125 d->timeout.tv_usec += 1000*1000;
1126 d->timeout.tv_sec--;
1133 gettimeofday(&now, NULL);
1134 d->timeout.tv_sec = end.tv_sec - now.tv_sec;
1135 d->timeout.tv_usec = end.tv_usec - now.tv_usec;
1136 if (d->timeout.tv_usec < 0)
1138 d->timeout.tv_usec += 1000*1000;
1139 d->timeout.tv_sec--;
1146 socklen_t len =
sizeof(errcode);
1147 retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (
char*)&errcode,
1149 if (retval == -1 || errcode != 0)
1158 if (d->timeout.tv_sec == 0 && d->timeout.tv_usec == 0)
1160 d->status = lookupDone;
1165 setError(IO_ConnectError, errcode);
1173 d->status = connected;
1174 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
1176 d->flags & outputBufferedSocket ? -1 : 0);
1194 d->status = connected;
1195 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
1197 d->flags & outputBufferedSocket ? -1 : 0);
1214 if (d->status >= connected || d->flags & passiveSocket)
1217 if (d->status == connecting)
1224 if (d->status < lookupDone)
1226 TQObject::connect(
this, TQT_SIGNAL(
lookupFinished(
int)),
this, TQT_SLOT(startAsyncConnectSlot()));
1227 if (d->status < lookupInProgress)
1235 d->status = connecting;
1236 TQGuardedPtr<TQObject> p = TQT_TQOBJECT(
this);
1240 if (d->status < connecting)
1247 if (d->status != connecting)
1257 d->qsnIn = d->qsnOut = NULL;
1262 d->status = lookupDone;
1267 if (mode != IO_Raw | IO_ReadWrite)
1270 if (d->flags & passiveSocket)
1272 else if (d->status < connecting)
1280 if (sockfd == -1 || d->status >= closing)
1287 d->status = closing;
1302 d->qsnIn = d->qsnOut = NULL;
1314 if (d->status >= done)
1320 d->qsnIn = d->qsnOut = NULL;
1322 if (d->status > connecting && sockfd != -1)
1327 else if (d->status == connecting)
1329 else if (d->status == lookupInProgress)
1345 d->resRemote.cancel(
false);
1346 d->resLocal.cancel(
false);
1348 if (d->local != NULL)
1350 if (d->peer != NULL)
1353 d->peer = d->local = NULL;
1355 if (d->qsnIn != NULL)
1357 if (d->qsnOut != NULL)
1360 d->qsnIn = d->qsnOut = NULL;
1373 if (d->status < connected || d->status >= done || d->flags & passiveSocket)
1379 if ((d->flags & outputBufferedSocket) == 0)
1384 unsigned written = 0;
1393 TQByteArray buf(16384);
1394 TQByteArray *a =
outBuf.first();
1397 while (a && count + (a->size() - offset) <= buf.size())
1399 memcpy(buf.data() + count, a->data() + offset, a->size() - offset);
1400 count += a->size() - offset;
1406 if (a && count < buf.size())
1410 memcpy(buf.data() + count, a->data() + offset, buf.size() - count);
1411 offset += buf.size() - count;
1426 if ((
unsigned)wrote != count)
1442 if (d->status < connected || d->flags & passiveSocket)
1447 if ((d->flags & inputBufferedSocket) == 0)
1457 retval = skipData(sockfd, maxlen);
1474 setError(IO_ReadError, EWOULDBLOCK);
1487 if (d->status < connected || d->status >= closing || d->flags & passiveSocket)
1497 if ((d->flags & outputBufferedSocket) == 0)
1513 if (d->outMaxSize == (
int)wsize)
1516 setError(IO_WriteError, EWOULDBLOCK);
1521 if (d->outMaxSize != -1 && wsize + len > (
unsigned)d->outMaxSize)
1523 len = d->outMaxSize - wsize;
1527 if (wsize == 0 || d->emitWrite)
1529 d->qsnOut->setEnabled(
true);
1540 if (d->status < connected || d->flags & passiveSocket)
1547 if (d->flags & inputBufferedSocket)
1561 int KExtendedSocket::bytesAvailable() const
1564 qint64 KExtendedSocket::bytesAvailable() const
1567 if (d->status < connected || d->flags & passiveSocket)
1572 if (d->flags & inputBufferedSocket)
1573 return TDEBufferedIO::bytesAvailable();
1581 if (d->flags & passiveSocket || d->status < connected || d->status >= closing)
1588 FD_SET(sockfd, &rd);
1590 tv.tv_sec = msecs / 1000;
1591 tv.tv_usec = (msecs % 1000) * 1000;
1599 else if (retval != 0)
1600 socketActivityRead();
1602 return bytesAvailable();
1618 unsigned char c = (char)ch;
1629 if (!enable && (d->flags & inputBufferedSocket) == 0 && d->qsnIn)
1630 d->qsnIn->setEnabled(
false);
1631 else if (enable && d->qsnIn)
1633 d->qsnIn->setEnabled(
true);
1634 d->emitRead = enable;
1641 if (!enable && (d->flags & outputBufferedSocket) == 0 && d->qsnOut)
1642 d->qsnOut->setEnabled(
false);
1643 else if (enable && d->qsnOut)
1645 d->qsnOut->setEnabled(
true);
1646 d->emitWrite = enable;
1651 void KExtendedSocket::socketActivityRead()
1653 if (d->flags & passiveSocket)
1658 if (d->status == connecting)
1663 if (d->status != connected)
1667 if (d->flags & inputBufferedSocket)
1672 int len, totalread = 0;
1678 if (d->inMaxSize == -1 || cursize < (
unsigned)d->inMaxSize)
1683 if (d->inMaxSize != -1 && d->inMaxSize - (cursize + totalread) <
sizeof(buf))
1686 len = d->inMaxSize - (cursize + totalread);
1694 a.resize(a.size() + len);
1695 memcpy(a.data() + totalread, buf, len);
1703 d->qsnIn->deleteLater();
1705 d->qsnIn = d->qsnOut = NULL;
1707 emit
closed(involuntary |
1720 while (len ==
sizeof(buf));
1740 d->qsnIn->setEnabled(
false);
1744 emit
closed(involuntary);
1753 void KExtendedSocket::socketActivityWrite()
1755 if (d->flags & passiveSocket)
1757 if (d->status == connecting)
1762 if (d->status != connected && d->status != closing)
1769 if (d->emitWrite && empty)
1771 else if (!d->emitWrite)
1774 d->qsnOut->setEnabled(!empty);
1776 if (d->status == closing && empty)
1793 void KExtendedSocket::connectionEvent()
1795 if (d->status != connecting)
1799 if (remote.count() == 0)
1802 kdError(170) <<
"KExtendedSocket::connectionEvent() called but no data available!\n";
1813 socklen_t len =
sizeof(errcode);
1814 retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (
char*)&errcode, &len);
1816 if (retval == -1 || errcode != 0)
1827 d->qsnIn = d->qsnOut = NULL;
1829 setError(IO_ConnectError, errcode);
1837 d->status = connected;
1839 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
1841 d->flags & outputBufferedSocket ? -1 : 0);
1850 unsigned localidx = 0;
1851 for ( ; d->current < remote.count(); d->current++)
1854 if (local.count() != 0)
1857 for (localidx = 0; localidx < local.count(); localidx++)
1858 if (remote[d->current].family() == local[localidx].family())
1861 if (remote[d->current].family() != local[localidx].family())
1868 sockfd = ::socket(remote[d->current].family(), remote[d->current].socketType(),
1869 remote[d->current].protocol());
1874 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1875 if (d->addressReusable)
1879 if (
KSocks::self()->bind(sockfd, local[localidx].address(),
1880 local[localidx].length()) == -1)
1890 sockfd = ::socket(remote[d->current].family(), remote[d->current].socketType(),
1891 remote[d->current].protocol());
1898 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1899 if (d->addressReusable)
1908 remote[d->current].length()) == -1)
1910 if (errno != EWOULDBLOCK && errno != EINPROGRESS)
1921 d->qsnIn =
new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
1922 TQObject::connect(d->qsnIn, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(socketActivityRead()));
1923 d->qsnOut =
new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
1924 TQObject::connect(d->qsnOut, TQT_SIGNAL(activated(
int)),
this, TQT_SLOT(socketActivityWrite()));
1935 d->status = connected;
1937 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
1939 d->flags & outputBufferedSocket ? -1 : 0);
1945 d->status = lookupDone;
1949 void KExtendedSocket::dnsResultsReady()
1952 if (d->status != lookupInProgress)
1956 if (d->resRemote.isRunning() || d->resLocal.isRunning())
1962 int n = d->resRemote.results().count() + d->resLocal.results().count();
1966 d->status = lookupDone;
1971 d->status = nothing;
1972 setError(IO_LookupError, KResolver::NoName);
1980 void KExtendedSocket::startAsyncConnectSlot()
1982 TQObject::disconnect(
this, TQT_SIGNAL(
lookupFinished(
int)),
this, TQT_SLOT(startAsyncConnectSlot()));
1984 if (d->status == lookupDone)
1989 TQString &port,
int flags)
1991 kdDebug(170) <<
"Deprecated function called:" << k_funcinfo <<
endl;
1994 char h[NI_MAXHOST], s[NI_MAXSERV];
1998 err = getnameinfo(sock, len, h,
sizeof(h) - 1, s,
sizeof(s) - 1, flags);
1999 host = TQString::fromUtf8(h);
2000 port = TQString::fromUtf8(s);
2008 return resolve(sock->data, sock->datasize, host, port, flags);
2012 int userflags,
int *error)
2014 kdDebug(170) <<
"Deprecated function called:" << k_funcinfo <<
endl;
2016 int socktype, familyMask, flags;
2018 TQPtrList<KAddressInfo> l;
2021 if (!process_flags(userflags, socktype, familyMask, flags))
2029 *error = res.
error();
2033 for (i = 0; i < res.count(); i++)
2039 ai->ai = (addrinfo *) malloc(
sizeof(addrinfo));
2040 memset(ai->ai, 0,
sizeof(addrinfo));
2042 ai->ai->ai_family = res[i].family();
2043 ai->ai->ai_socktype = res[i].socketType();
2044 ai->ai->ai_protocol = res[i].protocol();
2045 TQString canon = res[i].canonicalName();
2046 if (!canon.isEmpty())
2048 ai->ai->ai_canonname = (
char *) malloc(canon.length()+1);
2049 strcpy(ai->ai->ai_canonname, canon.ascii());
2051 if ((ai->ai->ai_addrlen = res[i].length()))
2053 ai->ai->ai_addr = (
struct sockaddr *) malloc(res[i].length());
2054 memcpy(ai->ai->ai_addr, res[i].address().address(), res[i].length());
2058 ai->ai->ai_addr = 0;
2075 struct sockaddr static_sa, *sa = &static_sa;
2076 ksocklen_t len =
sizeof(static_sa);
2086 if (len >
sizeof(static_sa)
2087 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2088 || sa->sa_len >
sizeof(static_sa)
2094 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2095 if (sa->sa_len != len)
2099 sa = (sockaddr*)malloc(len);
2123 struct sockaddr static_sa, *sa = &static_sa;
2124 ksocklen_t len =
sizeof(static_sa);
2134 if (len >
sizeof(static_sa)
2135 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2136 || sa->sa_len >
sizeof(static_sa)
2142 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2143 if (sa->sa_len != len)
2147 sa = (sockaddr*)malloc(len);
2169 if (code == IO_LookupError)
2170 msg = gai_strerror(syserr);
2172 msg = strerror(syserr);
2174 return TQString::fromLocal8Bit(msg);
2178 TQSocketNotifier *KExtendedSocket::readNotifier() {
return d->qsnIn; }
2179 TQSocketNotifier *KExtendedSocket::writeNotifier() {
return d->qsnOut; }
2186 KAddressInfo::KAddressInfo(addrinfo *p)
2188 ai = (addrinfo *) malloc(
sizeof(addrinfo));
2189 memcpy(ai, p,
sizeof(addrinfo));
2191 if (p->ai_canonname)
2193 ai->ai_canonname = (
char *) malloc(strlen(p->ai_canonname)+1);
2194 strcpy(ai->ai_canonname, p->ai_canonname);
2196 if (p->ai_addr && p->ai_addrlen)
2198 ai->ai_addr = (
struct sockaddr *) malloc(p->ai_addrlen);
2199 memcpy(ai->ai_addr, p->ai_addr, p->ai_addrlen);
2210 KAddressInfo::~KAddressInfo()
2212 if (ai && ai->ai_canonname)
2213 free(ai->ai_canonname);
2215 if (ai && ai->ai_addr)
2225 return ai->ai_flags;
2230 return ai->ai_family;
2235 return ai->ai_socktype;
2240 return ai->ai_protocol;
2245 return ai->ai_canonname;
2248 void KExtendedSocket::virtual_hook(
int id,
void* data )
2249 { TDEBufferedIO::virtual_hook(
id, data ); }
2251 #include "kextsock.moc"
bool unsetBindAddress()
Unsets the bind address for the socket.
int setSocketFlags(int flags)
Sets the given flags.
int listen(int s, int backlog)
This is the re-implementation of libc's function of the same name.
A generic socket address.
void readyWrite()
This signal gets sent when the device is ready for writing.
static TDESocketAddress * newAddress(const struct sockaddr *sa, ksocklen_t size)
Creates a new TDESocketAddress or descendant class from given raw socket address. ...
virtual void enableRead(bool enable)
Toggles the emission of the readyRead signal.
virtual void release()
Releases the socket and anything we have holding on it.
virtual unsigned readBufferSize() const
Returns the number of bytes in the read buffer.
static TQString strError(int code, int syserr)
Returns the representing text of this error code.
virtual int unreadBlock(const char *data, uint len)
Reimplementation of unreadBlock() method.
virtual void closeNow()
Closes the socket now, discarding the contents of the write buffer, if any.
Name and service resolution class.
void lookupFinished(int count)
This signal is emitted whenever an asynchronous lookup process is done.
int socktype() const KDE_DEPRECATED
Returns the socket type of the address info (see getaddrinfo(3)).
virtual bool open(TQ_OpenMode mode=(TQ_OpenMode)(IO_Raw|IO_ReadWrite))
Implementation of TQIODevice::open() pure virtual function.
virtual void flush()
Flushes the socket buffer.
bool unsetBindPort()
Unsets the bind port/service.
Name and service resolution results.
bool setBlockingMode(bool enable)
Sets/unsets blocking mode for the socket.
TQPtrList< TQByteArray > outBuf
For an explanation on how this buffer work, please refer to the comments at the top of kbufferedio...
virtual unsigned feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning=false)
Feeds data into the input buffer.
static KSocks * self()
Return an instance of class KSocks *.
void connectionFailed(int error)
This signal is emitted whenever our asynchronous connection attempt failed to all hosts listed...
A namespace to store all networking-related (socket) classes.
bool setBindPort(int port)
Sets the port/service to which we will bind before connecting.
bool setIPv6Only(bool enable)
Sets/unsets the v6-only flag for IPv6 sockets.
virtual bool setBufferSize(int rsize, int wsize=-2)
Sets the buffer sizes for this socket.
bool unsetBindHost()
Unsets the bind hostname.
timeval timeout() const
Returns the timeout value for the connection.
virtual int accept(KExtendedSocket *&sock)
Accepts an incoming connection from the socket.
int protocol() const KDE_DEPRECATED
Returns the protocol of the address info (see getaddrinfo(3)).
bool setPort(int port)
Sets the port/service.
virtual int waitForMore(int msec)
Returns the number of available bytes yet to be read via readBlock and family of functions.
virtual TQT_TQIO_LONG tqreadBlock(char *data, TQT_TQIO_ULONG maxlen)
Reads a block of data from the socket.
virtual int connect()
Attempts to connect to the remote host.
virtual void cancelAsyncConnect()
Cancels any on-going asynchronous connection attempt.
int systemError() const
Returns the related system error code Except for IO_LookupError errors, these are codes found in errn...
virtual int peekBlock(char *data, uint maxlen)
Peeks at a block of data from the socket.
KExtendedSocket()
Creates an empty KExtendedSocket.
static int resolve(sockaddr *sock, ksocklen_t len, TQString &host, TQString &port, int flags=0) KDE_DEPRECATED
Performs resolution on the given socket address.
virtual unsigned writeBufferSize() const
Returns the number of bytes in the write buffer.
virtual int putch(int ch)
Writes a single character (unsigned char) to the stream.
int accept(int s, sockaddr *addr, ksocklen_t *addrlen)
This is the re-implementation of libc's function of the same name.
int family() const KDE_DEPRECATED
Returns the family of the address info (see getaddrinfo(3)).
int getsockname(int s, sockaddr *name, ksocklen_t *namelen)
This is the re-implementation of libc's function of the same name.
virtual int lookup()
Performs lookup on the addresses we were given before.
TQString bindHost() const
Returns the hostname to which the socket will be/is bound.
bool isIPv6Only()
Returns the status of the v6-only flag for IPv6 sockets.
virtual TQT_TQIO_LONG tqwriteBlock(const char *data, TQT_TQIO_ULONG len)
Writes a block of data to the socket.
void bytesWritten(int nbytes)
This signal gets sent whenever bytes are written from the buffer.
bool setHost(const TQString &host)
Sets the hostname to the given value.
TQString port() const
Returns the port/service.
bool setBindHost(const TQString &host)
Sets the hostname to which we will bind locally before connecting.
void closed(int state)
This signal gets sent when the stream is closed.
virtual void enableWrite(bool enable)
Toggles the emission of the readyWrite signal.
const ::TDESocketAddress * peerAddress()
Returns the peer socket address.
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
This is the re-implementation of libc's function of the same name.
bool setTimeout(int secs, int usecs=0)
Sets the timeout value for the connection (if this is not passiveSocket) or acception (if it is)...
TQString bindPort() const
Returns the service to which the socket will be/is bound.
bool addressReusable()
Returns whether this socket's address can be reused.
int socketFlags() const
Returns the current flags.
virtual void close()
Closes the socket.
void readyRead()
This signal gets sent when the device is ready for reading.
virtual int startAsyncConnect()
Starts an asynchronous connect.
const ::TDESocketAddress * localAddress()
Returns the local socket address.
unsigned outBufIndex
Offset into first output buffer.
bool setAddress(const TQString &host, int port)
Sets the address where we will connect to.
int getpeername(int s, sockaddr *name, ksocklen_t *namelen)
This is the re-implementation of libc's function of the same name.
bool blockingMode()
Returns the current blocking mode for this socket.
virtual void cancelAsyncLookup()
Cancels any on-going asynchronous lookups.
virtual void consumeWriteBuffer(unsigned nbytes)
Consumes data from the output buffer.
The extended socket class.
int socketStatus() const
Resets the socket, disconnecting if still connected and freeing any related resources still being kep...
void setError(int errorkind, int error)
Sets the error code.
signed long int read(int fd, void *buf, unsigned long int count)
This is the re-implementation of libc's function of the same name.
virtual unsigned consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard=true)
Consumes data from the input buffer.
bool setAddressReusable(bool enable)
Sets/unsets address reusing flag for this socket.
kndbgstream & endl(kndbgstream &s)
Does nothing.
int flags() const KDE_DEPRECATED
Returns the flags of the address info (see getaddrinfo(3)).
virtual int listen(int N=5)
Place the socket in listen mode.
virtual int getch()
Gets a single character (unsigned char) from the stream.
void connectionSuccess()
This signal is emitted whenever we connected asynchronously to a host.
void setSocketStatus(int status)
Sets the socket status.
int error() const
Retrieves the error code associated with this resolution.
const char * canonname() const KDE_DEPRECATED
Returns the official name of the host (see getaddrinfo(3)).
TQString host() const
Returns the hostname.
signed long int write(int fd, const void *buf, unsigned long int count)
This is the re-implementation of libc's function of the same name.
int recv(int s, void *buf, unsigned long int len, int flags)
This is the re-implementation of libc's function of the same name.
virtual unsigned feedWriteBuffer(unsigned nbytes, const char *buffer)
Feeds data into the output buffer.
bool setBindAddress(const TQString &host, int port)
Sets both host and port to which we will bind the socket.
void readyAccept()
This signal is emitted whenever this socket is ready to accept another socket.
virtual ~KExtendedSocket()
Destroys the socket, disconnecting if still connected and freeing any related resources still being k...
virtual int startAsyncLookup()
Starts an asynchronous lookup for the addresses given.