31 #include <config-network.h>
35 #ifdef HAVE_SYS_FILIO_H
36 # include <sys/filio.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
41 #include <sys/ioctl.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
49 # include <sys/poll.h>
51 # ifdef HAVE_SYS_SELECT_H
52 # include <sys/select.h>
61 #include <QSocketNotifier>
69 using namespace KNetwork;
71 class KNetwork::KSocketDevicePrivate
74 mutable QSocketNotifier *input, *
output, *exception;
79 inline KSocketDevicePrivate()
81 input =
output = exception = 0L;
89 d(new KSocketDevicePrivate)
140 QMutexLocker locker(
mutex());
149 if (ioctlsocket(
m_sockfd, FIONBIO, &iMode) == SOCKET_ERROR)
153 if(WSAGetLastError() == WSAEINVAL)
155 qDebug(
"socket set %s failed %d", iMode ?
"nonblocking" :
"blocking", GetLastError());
162 int fdflags = fcntl(
m_sockfd, F_GETFL, 0);
170 fdflags &= ~O_NONBLOCK;
172 fdflags |= O_NONBLOCK;
174 if (fcntl(
m_sockfd, F_SETFL, fdflags) == -1)
184 if (setsockopt(
m_sockfd, SOL_SOCKET, SO_REUSEADDR, (
char*)&on,
sizeof(on)) == -1)
191 #if defined(IPV6_V6ONLY) && defined(AF_INET6)
192 if (d->af == AF_INET6)
197 if (setsockopt(
m_sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (
char*)&on,
sizeof(on)) == -1)
207 if (setsockopt(
m_sockfd, SOL_SOCKET, SO_BROADCAST, (
char*)&on,
sizeof(on)) == -1)
214 if ((d->proto == IPPROTO_TCP || d->proto == 0) &&
216 #
if defined(AF_INET6)
221 int on = opts &
NoDelay ? 1 : 0;
222 if (setsockopt(
m_sockfd, IPPROTO_TCP, TCP_NODELAY, (
char*)&on,
sizeof(on)) == -1)
241 d->input = d->output = d->exception = 0L;
245 d->local.setFamily(AF_UNSPEC);
246 d->peer.setFamily(AF_UNSPEC);
273 m_sockfd = kde_socket(family, type, protocol);
284 setOpenMode(Unbuffered);
303 if (errno == EADDRINUSE)
308 else if (errno == EINVAL)
313 qDebug(
" bind failed: %s ",address.
address().
toString().toLatin1().constData());
328 if (kde_listen(
m_sockfd, backlog) == -1)
335 setOpenMode(QIODevice::Unbuffered | QIODevice::ReadWrite);
354 if (errno == EISCONN)
359 else if (errno == EALREADY || errno == EINPROGRESS)
365 else if (errno == ECONNREFUSED)
367 else if (errno == ENETDOWN || errno == ENETUNREACH ||
368 errno == ENETRESET || errno == ECONNABORTED ||
369 errno == ECONNRESET || errno == EHOSTDOWN ||
370 errno == EHOSTUNREACH)
392 socklen_t len =
sizeof(sa);
393 int newfd = kde_accept(
m_sockfd, &sa, &len);
396 if (errno == EAGAIN || errno == EWOULDBLOCK)
417 if (errno == EALREADY || errno == EINPROGRESS)
422 else if (errno == ECONNREFUSED)
424 else if (errno == ENETDOWN || errno == ENETUNREACH ||
425 errno == ENETRESET || errno == ECONNABORTED ||
426 errno == ECONNRESET || errno == EHOSTDOWN ||
427 errno == EHOSTUNREACH)
435 setOpenMode(QIODevice::Unbuffered | QIODevice::ReadWrite);
445 if (kde_ioctl(
m_sockfd, FIONREAD, &nchars) == -1)
457 if (!
poll(&input, 0, 0, msecs, timeout))
469 retval = ::recvfrom(sockfd, data, maxlen, peek ? MSG_PEEK : 0, from->
address(), &len);
472 retval = ::recvfrom(sockfd, data, maxlen, peek ? MSG_PEEK : 0, NULL, NULL);
477 if (WSAGetLastError() == WSAEWOULDBLOCK )
481 if (errno == EAGAIN || errno == EWOULDBLOCK )
500 if (data == 0L || maxlen == 0)
508 setError(static_cast<SocketError>(err));
521 if (data == 0L || maxlen == 0)
529 setError(static_cast<SocketError>(err));
542 if (data == 0L || len == 0)
550 retval = ::send(
m_sockfd, data, len, 0);
556 if (errno == EAGAIN || errno == EWOULDBLOCK)
562 else if (retval == 0)
573 if (d->local.family() != AF_UNSPEC)
583 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
609 if (d->peer.family() != AF_UNSPEC)
619 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
652 QMutexLocker locker(
mutex());
670 QMutexLocker locker(
mutex());
688 QMutexLocker locker(
mutex());
718 fds.events |= POLLIN;
723 fds.events |= POLLOUT;
728 fds.events |= POLLPRI;
732 int retval =
::poll(&fds, 1, timeout);
746 if (input && fds.revents & POLLIN)
748 if (output && fds.revents & POLLOUT)
750 if (exception && fds.revents & POLLPRI)
761 fd_set readfds, writefds, exceptfds;
762 fd_set *preadfds = 0L, *pwritefds = 0L, *pexceptfds = 0L;
773 pwritefds = &writefds;
780 pexceptfds = &exceptfds;
788 retval = select(
m_sockfd + 1, preadfds, pwritefds, pexceptfds, 0L);
793 tv.tv_sec = timeout / 1000;
794 tv.tv_usec = timeout % 1000 * 1000;
796 retval = select(
m_sockfd + 1, preadfds, pwritefds, pexceptfds, &tv);
812 if (input && FD_ISSET(
m_sockfd, preadfds))
814 if (output && FD_ISSET(
m_sockfd, pwritefds))
816 if (exception && FD_ISSET(
m_sockfd, pexceptfds))
825 bool input,
output, exception;
826 return poll(&input, &output, &exception, timeout, timedout);
834 return new QSocketNotifier(
m_sockfd, type);
840 template<
class T>
class ptr
848 ptr(
const ptr<T>& other) : obj(other.obj)
851 ptr(type* _obj) : obj(_obj)
857 ptr<T>& operator=(
const ptr<T>& other)
858 { obj = other.obj;
return *
this; }
860 ptr<T>& operator=(T* _obj)
861 { obj = _obj;
return *
this; }
863 type* operator->()
const {
return obj; }
865 operator T*()
const {
return obj; }
897 factoryMap::ConstIterator it =
factories.constBegin();
898 for ( ; it !=
factories.constEnd(); ++it)
901 return it.value()->create(parent);