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)
241 d->status = newstatus;
246 setStatus(errorcode);
247 d->syserror = syserror;
261 if (d->status > nothing)
264 return d->flags = flags;
278 if (d->status > nothing)
281 d->resRemote.setNodeName(
host);
290 return d->resRemote.nodeName();
304 if (d->status > nothing)
307 d->resRemote.setServiceName(service);
316 return d->resRemote.serviceName();
342 if (d->status > nothing || d->flags & passiveSocket)
345 d->resLocal.setServiceName(
host);
363 return d->resLocal.serviceName();
377 if (d->status > nothing || d->flags & passiveSocket)
380 d->resLocal.setServiceName(service);
397 return d->resLocal.serviceName();
429 if (d->status >= connected)
432 d->timeout.tv_sec = secs;
433 d->timeout.tv_usec = usecs;
451 if (d->status < created)
457 int fdflags = fcntl(sockfd, F_GETFL, 0);
462 fdflags |= O_NONBLOCK;
464 fdflags &= ~O_NONBLOCK;
466 if (fcntl(sockfd, F_SETFL, fdflags) == -1)
468 setError(IO_UnspecifiedError, errno);
480 if (d->status < created)
486 int fdflags = fcntl(sockfd, F_GETFL, 0);
489 setError(IO_UnspecifiedError, errno);
492 return (fdflags & O_NONBLOCK) == 0;
501 d->addressReusable = enable;
502 if (d->status < created)
510 setError(IO_UnspecifiedError, errno);
523 if (setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR, (
char*)&on,
sizeof(on)) == -1)
534 if (d->status < created)
535 return d->addressReusable;
538 return d->addressReusable;
541 socklen_t onsiz =
sizeof(on);
542 if (getsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (
char*)&on, &onsiz) == -1)
544 setError(IO_UnspecifiedError, errno);
559 d->ipv6only = enable;
565 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
566 (
char *)&on,
sizeof(on)) == -1)
568 setError(IO_UnspecifiedError, errno);
576 d->ipv6only = enable;
578 setError(IO_UnspecifiedError, ENOSYS);
591 if (d->status < created || sockfd == -1)
595 socklen_t onsiz =
sizeof(on);
596 if (getsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
597 (
char *)&on, &onsiz) == -1)
599 setError(IO_UnspecifiedError, errno);
603 return d->ipv6only = on;
607 setError(IO_UnspecifiedError, ENOSYS);
619 if (d->status < created)
625 if (d->flags & passiveSocket)
639 if (d->qsnIn == NULL)
641 d->qsnIn =
new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
642 TQObject::connect(d->qsnIn, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(socketActivityRead()));
643 d->qsnIn->setEnabled(
true);
646 if (rsize == 0 && d->flags & inputBufferedSocket)
649 d->flags &= ~inputBufferedSocket;
654 else if (rsize != -2)
658 d->flags |= inputBufferedSocket;
659 d->inMaxSize = rsize;
667 if (wsize == 0 && d->flags & outputBufferedSocket)
670 d->flags &= ~outputBufferedSocket;
671 if (d->qsnOut && !d->emitWrite)
672 d->qsnOut->setEnabled(
false);
676 else if (wsize != -2)
680 d->flags |= outputBufferedSocket;
681 d->outMaxSize = wsize;
687 if (d->qsnOut == NULL)
689 d->qsnOut =
new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
690 TQObject::connect(d->qsnOut, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(socketActivityWrite()));
699 setFlags((mode() & ~IO_Raw) | ((d->flags & bufferedSocket) ? 0 : IO_Raw));
702 if (d->emitWrite && d->qsnOut == NULL)
704 d->qsnOut =
new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
705 TQObject::connect(d->qsnOut, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(socketActivityWrite()));
718 if (d->local != NULL)
720 if (d->status < bound)
735 if (d->flags & passiveSocket || d->status < connected)
749 if (!d->resRemote.wait() || !d->resLocal.wait())
755 d->status = lookupDone;
756 if (d->resRemote.error() != KResolver::NoError)
757 return d->resRemote.error();
758 if (d->resLocal.error() != KResolver::NoError)
759 return d->resLocal.error();
769 if (d->status > lookupInProgress)
771 if (d->status == lookupInProgress)
776 int socktype, familyMask, flags;
777 if (!process_flags(d->flags, socktype, familyMask, flags))
781 if (!d->resRemote.isRunning())
783 d->resRemote.setFlags(flags);
784 d->resRemote.setFamily(familyMask);
785 d->resRemote.setSocketType(socktype);
787 this, TQ_SLOT(dnsResultsReady()));
789 if (!d->resRemote.start())
791 setError(IO_LookupError, d->resRemote.error());
792 return d->resRemote.error();
796 if ((d->flags & passiveSocket) == 0 && !d->resLocal.isRunning())
799 flags |= KResolver::Passive;
800 d->resLocal.setFlags(flags);
801 d->resLocal.setFamily(familyMask);
802 d->resLocal.setSocketType(socktype);
804 this, TQ_SLOT(dnsResultsReady()));
806 if (!d->resLocal.start())
808 setError(IO_LookupError, d->resLocal.error());
809 return d->resLocal.error();
814 if (d->resRemote.isRunning() || d->resLocal.isRunning())
815 d->status = lookupInProgress;
818 d->status = lookupDone;
820 d->resLocal.results().count());
828 if (d->status != lookupInProgress)
832 d->resLocal.cancel(
false);
833 d->resRemote.cancel(
false);
839 if ((d->flags & passiveSocket) == 0 || d->status >= listening)
841 if (d->status < lookupDone)
844 if (d->resRemote.error())
848 KResolverResults::const_iterator it;
850 for (it = res.begin(); it != res.end(); ++it)
853 sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
861 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
863 if (d->addressReusable)
867 if (
KSocks::self()->bind(sockfd, (*it).address().address(), (*it).length()) == -1)
890 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite);
897 d->status = listening;
898 d->qsnIn =
new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
899 TQObject::connect(d->qsnIn, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(socketActivityRead()));
901 return retval == -1 ? -1 : 0;
908 if ((d->flags & passiveSocket) == 0 || d->status >= accepting)
910 if (d->status < listening)
919 ksocklen_t len =
sizeof(sa);
922 if (d->timeout.tv_sec > 0 || d->timeout.tv_usec > 0)
928 FD_SET(sockfd, &set);
936 setError(IO_UnspecifiedError, errno);
939 else if (retval == 0 || !FD_ISSET(sockfd, &set))
952 kdWarning(170) <<
"Error accepting on socket " << sockfd <<
":"
957 fcntl(newfd, F_SETFD, FD_CLOEXEC);
964 sock->d->status = connected;
965 sock->sockfd = newfd;
966 sock->setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
981 if (d->flags & passiveSocket || d->status >= connected)
983 if (d->status < lookupDone)
988 timeval timeout_copy = d->timeout;
996 local = d->resLocal.results();
997 KResolverResults::const_iterator it, it2;
1003 for (it = remote.begin(), it2 = local.begin(); it != remote.end(); ++it)
1005 bool doingtimeout = d->timeout.tv_sec > 0 || d->timeout.tv_usec > 0;
1008 gettimeofday(&end, NULL);
1009 end.tv_usec += d->timeout.tv_usec;
1010 end.tv_sec += d->timeout.tv_sec;
1011 if (end.tv_usec > 1000*1000)
1013 end.tv_usec -= 1000*1000;
1021 if (it2 != local.end())
1024 if ((*it).family() != (*it2).family())
1026 for (it2 = local.begin(); it2 != local.end(); ++it2)
1027 if ((*it).family() == (*it2).family())
1030 if ((*it).family() != (*it2).family())
1034 it2 = local.begin();
1040 sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
1044 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1045 if (d->addressReusable)
1049 if (
KSocks::self()->bind(sockfd, (*it2).address(), (*it2).length()))
1060 sockfd = ::socket((*it).family(), (*it).socketType(), (*it).protocol());
1066 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1067 if (d->addressReusable)
1074 d->status = created;
1077 if (doingtimeout &&
KSocks::self()->hasWorkingAsyncConnect())
1087 if (errno != EWOULDBLOCK && errno != EINPROGRESS)
1098 FD_SET(sockfd, &rd);
1099 FD_SET(sockfd, &wr);
1107 else if (retval == 0)
1116 d->timeout.tv_usec += timeout_copy.tv_usec;
1117 d->timeout.tv_sec += timeout_copy.tv_sec;
1118 if (d->timeout.tv_usec < 0)
1120 d->timeout.tv_usec += 1000*1000;
1121 d->timeout.tv_sec--;
1128 gettimeofday(&now, NULL);
1129 d->timeout.tv_sec = end.tv_sec - now.tv_sec;
1130 d->timeout.tv_usec = end.tv_usec - now.tv_usec;
1131 if (d->timeout.tv_usec < 0)
1133 d->timeout.tv_usec += 1000*1000;
1134 d->timeout.tv_sec--;
1141 socklen_t len =
sizeof(errcode);
1142 retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (
char*)&errcode,
1144 if (retval == -1 || errcode != 0)
1153 if (d->timeout.tv_sec == 0 && d->timeout.tv_usec == 0)
1155 d->status = lookupDone;
1160 setError(IO_ConnectError, errcode);
1168 d->status = connected;
1169 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
1171 d->flags & outputBufferedSocket ? -1 : 0);
1189 d->status = connected;
1190 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
1192 d->flags & outputBufferedSocket ? -1 : 0);
1209 if (d->status >= connected || d->flags & passiveSocket)
1212 if (d->status == connecting)
1219 if (d->status < lookupDone)
1221 TQObject::connect(
this, TQ_SIGNAL(
lookupFinished(
int)),
this, TQ_SLOT(startAsyncConnectSlot()));
1222 if (d->status < lookupInProgress)
1230 d->status = connecting;
1231 TQGuardedPtr<TQObject> p =
this;
1235 if (d->status < connecting)
1242 if (d->status != connecting)
1252 d->qsnIn = d->qsnOut = NULL;
1257 d->status = lookupDone;
1262 if (mode != IO_Raw | IO_ReadWrite)
1265 if (d->flags & passiveSocket)
1267 else if (d->status < connecting)
1275 if (sockfd == -1 || d->status >= closing)
1282 d->status = closing;
1297 d->qsnIn = d->qsnOut = NULL;
1309 if (d->status >= done)
1315 d->qsnIn = d->qsnOut = NULL;
1317 if (d->status > connecting && sockfd != -1)
1322 else if (d->status == connecting)
1324 else if (d->status == lookupInProgress)
1340 d->resRemote.cancel(
false);
1341 d->resLocal.cancel(
false);
1343 if (d->local != NULL)
1345 if (d->peer != NULL)
1348 d->peer = d->local = NULL;
1350 if (d->qsnIn != NULL)
1352 if (d->qsnOut != NULL)
1355 d->qsnIn = d->qsnOut = NULL;
1368 if (d->status < connected || d->status >= done || d->flags & passiveSocket)
1374 if ((d->flags & outputBufferedSocket) == 0)
1379 unsigned written = 0;
1388 TQByteArray buf(16384);
1389 TQByteArray *a =
outBuf.first();
1392 while (a && count + (a->size() - offset) <= buf.size())
1394 memcpy(buf.data() + count, a->data() + offset, a->size() - offset);
1395 count += a->size() - offset;
1401 if (a && count < buf.size())
1405 memcpy(buf.data() + count, a->data() + offset, buf.size() - count);
1406 offset += buf.size() - count;
1421 if ((
unsigned)wrote != count)
1437 if (d->status < connected || d->flags & passiveSocket)
1442 if ((d->flags & inputBufferedSocket) == 0)
1452 retval = skipData(sockfd, maxlen);
1469 setError(IO_ReadError, EWOULDBLOCK);
1482 if (d->status < connected || d->status >= closing || d->flags & passiveSocket)
1492 if ((d->flags & outputBufferedSocket) == 0)
1508 if (d->outMaxSize == (
int)wsize)
1511 setError(IO_WriteError, EWOULDBLOCK);
1516 if (d->outMaxSize != -1 && wsize + len > (
unsigned)d->outMaxSize)
1518 len = d->outMaxSize - wsize;
1522 if (wsize == 0 || d->emitWrite)
1524 d->qsnOut->setEnabled(
true);
1535 if (d->status < connected || d->flags & passiveSocket)
1542 if (d->flags & inputBufferedSocket)
1557 if (d->status < connected || d->flags & passiveSocket)
1562 if (d->flags & inputBufferedSocket)
1571 if (d->flags & passiveSocket || d->status < connected || d->status >= closing)
1578 FD_SET(sockfd, &rd);
1580 tv.tv_sec = msecs / 1000;
1581 tv.tv_usec = (msecs % 1000) * 1000;
1589 else if (retval != 0)
1590 socketActivityRead();
1599 retval =
readBlock((
char*)&c,
sizeof(c));
1608 unsigned char c = (char)ch;
1619 if (!enable && (d->flags & inputBufferedSocket) == 0 && d->qsnIn)
1620 d->qsnIn->setEnabled(
false);
1621 else if (enable && d->qsnIn)
1623 d->qsnIn->setEnabled(
true);
1624 d->emitRead = enable;
1631 if (!enable && (d->flags & outputBufferedSocket) == 0 && d->qsnOut)
1632 d->qsnOut->setEnabled(
false);
1633 else if (enable && d->qsnOut)
1635 d->qsnOut->setEnabled(
true);
1636 d->emitWrite = enable;
1641 void KExtendedSocket::socketActivityRead()
1643 if (d->flags & passiveSocket)
1648 if (d->status == connecting)
1653 if (d->status != connected)
1657 if (d->flags & inputBufferedSocket)
1662 int len, totalread = 0;
1668 if (d->inMaxSize == -1 || cursize < (
unsigned)d->inMaxSize)
1673 if (d->inMaxSize != -1 && d->inMaxSize - (cursize + totalread) <
sizeof(buf))
1676 len = d->inMaxSize - (cursize + totalread);
1684 a.resize(a.size() + len);
1685 memcpy(a.data() + totalread, buf, len);
1693 d->qsnIn->deleteLater();
1695 d->qsnIn = d->qsnOut = NULL;
1697 emit
closed(involuntary |
1710 while (len ==
sizeof(buf));
1730 d->qsnIn->setEnabled(
false);
1734 emit
closed(involuntary);
1743 void KExtendedSocket::socketActivityWrite()
1745 if (d->flags & passiveSocket)
1747 if (d->status == connecting)
1752 if (d->status != connected && d->status != closing)
1759 if (d->emitWrite && empty)
1761 else if (!d->emitWrite)
1764 d->qsnOut->setEnabled(!empty);
1766 if (d->status == closing && empty)
1783 void KExtendedSocket::connectionEvent()
1785 if (d->status != connecting)
1789 if (remote.count() == 0)
1792 kdError(170) <<
"KExtendedSocket::connectionEvent() called but no data available!\n";
1803 socklen_t len =
sizeof(errcode);
1804 retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (
char*)&errcode, &len);
1806 if (retval == -1 || errcode != 0)
1817 d->qsnIn = d->qsnOut = NULL;
1819 setError(IO_ConnectError, errcode);
1827 d->status = connected;
1829 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
1831 d->flags & outputBufferedSocket ? -1 : 0);
1840 unsigned localidx = 0;
1841 for ( ; d->current < remote.count(); d->current++)
1844 if (local.count() != 0)
1847 for (localidx = 0; localidx < local.count(); localidx++)
1848 if (remote[d->current].family() == local[localidx].family())
1851 if (remote[d->current].family() != local[localidx].family())
1858 sockfd = ::socket(remote[d->current].family(), remote[d->current].socketType(),
1859 remote[d->current].protocol());
1864 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1865 if (d->addressReusable)
1869 if (
KSocks::self()->bind(sockfd, local[localidx].address(),
1870 local[localidx].length()) == -1)
1880 sockfd = ::socket(remote[d->current].family(), remote[d->current].socketType(),
1881 remote[d->current].protocol());
1888 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
1889 if (d->addressReusable)
1898 remote[d->current].length()) == -1)
1900 if (errno != EWOULDBLOCK && errno != EINPROGRESS)
1911 d->qsnIn =
new TQSocketNotifier(sockfd, TQSocketNotifier::Read);
1912 TQObject::connect(d->qsnIn, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(socketActivityRead()));
1913 d->qsnOut =
new TQSocketNotifier(sockfd, TQSocketNotifier::Write);
1914 TQObject::connect(d->qsnOut, TQ_SIGNAL(activated(
int)),
this, TQ_SLOT(socketActivityWrite()));
1925 d->status = connected;
1927 setFlags(IO_Sequential | IO_Raw | IO_ReadWrite | IO_Open | IO_Async);
1929 d->flags & outputBufferedSocket ? -1 : 0);
1935 d->status = lookupDone;
1939 void KExtendedSocket::dnsResultsReady()
1942 if (d->status != lookupInProgress)
1946 if (d->resRemote.isRunning() || d->resLocal.isRunning())
1952 int n = d->resRemote.results().count() + d->resLocal.results().count();
1956 d->status = lookupDone;
1961 d->status = nothing;
1962 setError(IO_LookupError, KResolver::NoName);
1970 void KExtendedSocket::startAsyncConnectSlot()
1972 TQObject::disconnect(
this, TQ_SIGNAL(
lookupFinished(
int)),
this, TQ_SLOT(startAsyncConnectSlot()));
1974 if (d->status == lookupDone)
1979 TQString &port,
int flags)
1981 kdDebug(170) <<
"Deprecated function called:" << k_funcinfo <<
endl;
1984 char h[NI_MAXHOST], s[NI_MAXSERV];
1988 err = getnameinfo(sock, len, h,
sizeof(h) - 1, s,
sizeof(s) - 1, flags);
1989 host = TQString::fromUtf8(h);
1990 port = TQString::fromUtf8(s);
2002 int userflags,
int *error)
2004 kdDebug(170) <<
"Deprecated function called:" << k_funcinfo <<
endl;
2006 int socktype, familyMask, flags;
2008 TQPtrList<KAddressInfo> l;
2011 if (!process_flags(userflags, socktype, familyMask, flags))
2019 *error = res.
error();
2023 for (i = 0; i < res.count(); i++)
2029 ai->ai = (addrinfo *) malloc(
sizeof(addrinfo));
2030 memset(ai->ai, 0,
sizeof(addrinfo));
2032 ai->ai->ai_family = res[i].family();
2033 ai->ai->ai_socktype = res[i].socketType();
2034 ai->ai->ai_protocol = res[i].protocol();
2035 TQString canon = res[i].canonicalName();
2036 if (!canon.isEmpty())
2038 ai->ai->ai_canonname = (
char *) malloc(canon.length()+1);
2039 strcpy(ai->ai->ai_canonname, canon.ascii());
2041 if ((ai->ai->ai_addrlen = res[i].length()))
2043 ai->ai->ai_addr = (
struct sockaddr *) malloc(res[i].length());
2044 memcpy(ai->ai->ai_addr, res[i].address().address(), res[i].length());
2048 ai->ai->ai_addr = 0;
2065 struct sockaddr static_sa, *sa = &static_sa;
2066 ksocklen_t len =
sizeof(static_sa);
2076 if (len >
sizeof(static_sa)
2077 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2078 || sa->sa_len >
sizeof(static_sa)
2084 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2085 if (sa->sa_len != len)
2089 sa = (sockaddr*)malloc(len);
2113 struct sockaddr static_sa, *sa = &static_sa;
2114 ksocklen_t len =
sizeof(static_sa);
2124 if (len >
sizeof(static_sa)
2125 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2126 || sa->sa_len >
sizeof(static_sa)
2132 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2133 if (sa->sa_len != len)
2137 sa = (sockaddr*)malloc(len);
2159 if (code == IO_LookupError)
2160 msg = gai_strerror(syserr);
2162 msg = strerror(syserr);
2164 return TQString::fromLocal8Bit(msg);
2168 TQSocketNotifier *KExtendedSocket::readNotifier() {
return d->qsnIn; }
2169 TQSocketNotifier *KExtendedSocket::writeNotifier() {
return d->qsnOut; }
2176 KAddressInfo::KAddressInfo(addrinfo *p)
2178 ai = (addrinfo *) malloc(
sizeof(addrinfo));
2179 memcpy(ai, p,
sizeof(addrinfo));
2181 if (p->ai_canonname)
2183 ai->ai_canonname = (
char *) malloc(strlen(p->ai_canonname)+1);
2184 strcpy(ai->ai_canonname, p->ai_canonname);
2186 if (p->ai_addr && p->ai_addrlen)
2188 ai->ai_addr = (
struct sockaddr *) malloc(p->ai_addrlen);
2189 memcpy(ai->ai_addr, p->ai_addr, p->ai_addrlen);
2200 KAddressInfo::~KAddressInfo()
2202 if (ai && ai->ai_canonname)
2203 free(ai->ai_canonname);
2205 if (ai && ai->ai_addr)
2215 return ai->ai_flags;
2220 return ai->ai_family;
2225 return ai->ai_socktype;
2230 return ai->ai_protocol;
2235 return ai->ai_canonname;
2238 void KExtendedSocket::virtual_hook(
int id,
void* data )
2239 { TDEBufferedIO::virtual_hook(
id, data ); }
2241 #include "kextsock.moc"
int family() const TDE_DEPRECATED
Returns the family of the address info (see getaddrinfo(3)).
const char * canonname() const TDE_DEPRECATED
Returns the official name of the host (see getaddrinfo(3)).
int flags() const TDE_DEPRECATED
Returns the flags of the address info (see getaddrinfo(3)).
int protocol() const TDE_DEPRECATED
Returns the protocol of the address info (see getaddrinfo(3)).
int socktype() const TDE_DEPRECATED
Returns the socket type of the address info (see getaddrinfo(3)).
void readyRead()
This signal gets sent when the device is ready for reading.
void readyWrite()
This signal gets sent when the device is ready for writing.
The extended socket class.
int socketStatus() const
Returns the class status.
bool addressReusable()
Returns whether this socket's address can be reused.
virtual int bytesAvailable() const
Returns the number of available bytes yet to be read via readBlock and family of functions.
virtual ~KExtendedSocket()
Destroys the socket, disconnecting if still connected and freeing any related resources still being k...
virtual void enableRead(bool enable)
Toggles the emission of the readyRead signal.
void connectionSuccess()
This signal is emitted whenever we connected asynchronously to a host.
void reset()
Resets the socket, disconnecting if still connected and freeing any related resources still being kep...
virtual int peekBlock(char *data, uint maxlen)
Peeks at a block of data from the socket.
TQString bindPort() const
Returns the service to which the socket will be/is bound.
void readyAccept()
This signal is emitted whenever this socket is ready to accept another socket.
bool setAddressReusable(bool enable)
Sets/unsets address reusing flag for this socket.
virtual int waitForMore(int msec)
Waits msec milliseconds for more data to be available (use 0 to wait forever).
bool setBindAddress(const TQString &host, int port)
Sets both host and port to which we will bind the socket.
void connectionFailed(int error)
This signal is emitted whenever our asynchronous connection attempt failed to all hosts listed.
virtual void release()
Releases the socket and anything we have holding on it.
bool unsetBindHost()
Unsets the bind hostname.
const ::TDESocketAddress * localAddress()
Returns the local socket address.
bool setAddress(const TQString &host, int port)
Sets the address where we will connect to.
virtual TQ_LONG readBlock(char *data, TQ_ULONG maxlen)
Reads a block of data from the socket.
bool setBindPort(int port)
Sets the port/service to which we will bind before connecting.
bool setBlockingMode(bool enable)
Sets/unsets blocking mode for the socket.
virtual int connect()
Attempts to connect to the remote host.
virtual int lookup()
Performs lookup on the addresses we were given before.
bool blockingMode()
Returns the current blocking mode for this socket.
virtual int startAsyncConnect()
Starts an asynchronous connect.
TQString host() const
Returns the hostname.
virtual void cancelAsyncLookup()
Cancels any on-going asynchronous lookups.
static int resolve(sockaddr *sock, ksocklen_t len, TQString &host, TQString &port, int flags=0) TDE_DEPRECATED
Performs resolution on the given socket address.
bool setHost(const TQString &host)
Sets the hostname to the given value.
virtual void cancelAsyncConnect()
Cancels any on-going asynchronous connection attempt.
bool unsetBindPort()
Unsets the bind port/service.
int setSocketFlags(int flags)
Sets the given flags.
void lookupFinished(int count)
This signal is emitted whenever an asynchronous lookup process is done.
virtual void closeNow()
Closes the socket now, discarding the contents of the write buffer, if any.
virtual int unreadBlock(const char *data, uint len)
Reimplementation of unreadBlock() method.
bool setBindHost(const TQString &host)
Sets the hostname to which we will bind locally before connecting.
bool isIPv6Only()
Returns the status of the v6-only flag for IPv6 sockets.
virtual void flush()
Flushes the socket buffer.
KExtendedSocket()
Creates an empty KExtendedSocket.
int socketFlags() const
Returns the current flags.
TQString bindHost() const
Returns the hostname to which the socket will be/is bound.
bool setPort(int port)
Sets the port/service.
void setError(int errorkind, int error)
Sets the error code.
virtual bool open(int mode=(int)(IO_Raw|IO_ReadWrite))
Implementation of TQIODevice::open() pure virtual function.
int systemError() const
Returns the related system error code Except for IO_LookupError errors, these are codes found in errn...
int fd() const
Returns the file descriptor.
TQString port() const
Returns the port/service.
bool setIPv6Only(bool enable)
Sets/unsets the v6-only flag for IPv6 sockets.
virtual int listen(int N=5)
Place the socket in listen mode.
void setSocketStatus(int status)
Sets the socket status.
static TQString strError(int code, int syserr)
Returns the representing text of this error code.
virtual int getch()
Gets a single character (unsigned char) from the stream.
virtual bool setBufferSize(int rsize, int wsize=-2)
Sets the buffer sizes for this socket.
virtual void close()
Closes the socket.
virtual void enableWrite(bool enable)
Toggles the emission of the readyWrite signal.
bool unsetBindAddress()
Unsets the bind address for the socket.
virtual int startAsyncLookup()
Starts an asynchronous lookup for the addresses given.
const ::TDESocketAddress * peerAddress()
Returns the peer socket address.
virtual TQ_LONG writeBlock(const char *data, TQ_ULONG len)
Writes a block of data to the socket.
timeval timeout() const
Returns the timeout value for the connection.
virtual int putch(int ch)
Writes a single character (unsigned char) to the stream.
bool setTimeout(int secs, int usecs=0)
Sets the timeout value for the connection (if this is not passiveSocket) or acception (if it is).
virtual int accept(KExtendedSocket *&sock)
Accepts an incoming connection from the socket.
Name and service resolution results.
int error() const
Retrieves the error code associated with this resolution.
Name and service resolution class.
A generic socket address.
int getpeername(int s, sockaddr *name, ksocklen_t *namelen)
This is the re-implementation of libc's function of the same name.
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.
static KSocks * self()
Return an instance of class KSocks *.
int listen(int s, int backlog)
This is the re-implementation of libc's function of the same name.
int getsockname(int s, sockaddr *name, ksocklen_t *namelen)
This is the re-implementation of libc's function of the same name.
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.
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.
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.
int accept(int s, sockaddr *addr, ksocklen_t *addrlen)
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.
void closed(int state)
This signal gets sent when the stream is closed.
virtual unsigned consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard=true)
Consumes data from the input buffer.
virtual unsigned readBufferSize() const
Returns the number of bytes in the read buffer.
virtual unsigned feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning=false)
Feeds data into the input buffer.
void bytesWritten(int nbytes)
This signal gets sent whenever bytes are written from the buffer.
unsigned outBufIndex
Offset into first output buffer.
TQPtrList< TQByteArray > outBuf
For an explanation on how this buffer work, please refer to the comments at the top of kbufferedio....
virtual int bytesAvailable() const
Returns the number of bytes available for reading in the read buffer.
virtual void consumeWriteBuffer(unsigned nbytes)
Consumes data from the output buffer.
virtual unsigned writeBufferSize() const
Returns the number of bytes in the write buffer.
static TDESocketAddress * newAddress(const struct sockaddr *sa, ksocklen_t size)
Creates a new TDESocketAddress or descendant class from given raw socket address.
kndbgstream & endl(kndbgstream &s)
Does nothing.
A namespace to store all networking-related (socket) classes.