• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • network
k3socketdevice.cpp
Go to the documentation of this file.
1 /* -*- C++ -*-
2  * Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org>
3  *
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 // syssocket.h needs to come before any header that includes k3socketbase.h
26 #include "syssocket.h"
27 
28 #include "k3socketdevice.h" //krazy:exclude=includes (KDE3 compat: not worth fixing)
29 
30 #include <config.h>
31 #include <config-network.h>
32 
33 #include <QMap>
34 
35 #ifdef HAVE_SYS_FILIO_H
36 # include <sys/filio.h>
37 #endif
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/time.h>
41 #include <sys/ioctl.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h> // WARNING: verify if this is portable
46 #include <unistd.h>
47 
48 #ifdef HAVE_POLL
49 # include <sys/poll.h>
50 #else
51 # ifdef HAVE_SYS_SELECT_H
52 # include <sys/select.h>
53 # endif
54 #endif
55 
56 #ifdef Q_WS_WIN
57 #include <windows.h>
58 #endif
59 
60 #include <QMutex>
61 #include <QSocketNotifier>
62 
63 #include "k3resolver.h"
64 #include "k3socketaddress.h"
65 #include "k3socketbase.h"
66 #ifndef KDE_USE_FINAL
67 #include "k3socks.h"
68 #endif
69 using namespace KNetwork;
70 
71 class KNetwork::KSocketDevicePrivate
72 {
73 public:
74  mutable QSocketNotifier *input, *output, *exception;
75  KSocketAddress local, peer;
76  int af;
77  int proto;
78 
79  inline KSocketDevicePrivate()
80  {
81  input = output = exception = 0L;
82  af = proto = 0;
83  }
84 };
85 
86 
87 KSocketDevice::KSocketDevice(const KSocketBase* parent, QObject* objparent)
88  : KActiveSocketBase(objparent), m_sockfd(-1),
89  d(new KSocketDevicePrivate)
90 {
91  setSocketDevice(this);
92  if (parent)
93  setSocketOptions(parent->socketOptions());
94 }
95 
96 KSocketDevice::KSocketDevice(int fd, OpenMode mode)
97  : KActiveSocketBase(0L), m_sockfd(fd), d(new KSocketDevicePrivate)
98 {
99  if (mode)
100  mode |= Unbuffered;
101  KActiveSocketBase::open(mode);
102  setSocketDevice(this);
103  d->af = localAddress().family();
104 }
105 
106 KSocketDevice::KSocketDevice(QObject* parent)
107  : KActiveSocketBase(parent), m_sockfd(-1), d(new KSocketDevicePrivate)
108 {
109  setSocketDevice(this);
110 }
111 
112 KSocketDevice::KSocketDevice(bool, const KSocketBase* parent)
113  : KActiveSocketBase(0L), m_sockfd(-1), d(new KSocketDevicePrivate)
114 {
115  // do not set parent
116  if (parent)
117  setSocketOptions(parent->socketOptions());
118 }
119 
120 KSocketDevice::~KSocketDevice()
121 {
122  close(); // deletes the notifiers
123  unsetSocketDevice(); // prevent double deletion
124  delete d;
125 }
126 
127 int KSocketDevice::socket() const
128 {
129  return m_sockfd;
130 }
131 
132 int KSocketDevice::capabilities() const
133 {
134  return 0;
135 }
136 
137 bool KSocketDevice::setSocketOptions(int opts)
138 {
139  // must call parent
140  QMutexLocker locker(mutex());
141  KSocketBase::setSocketOptions(opts);
142 
143  if (m_sockfd == -1)
144  return true; // flags are stored
145 
146 #ifdef Q_WS_WIN
147  u_long iMode = ((opts & Blocking) == Blocking) ? 0 : 1;
148  // disable non blocking
149  if (ioctlsocket(m_sockfd, FIONBIO, &iMode) == SOCKET_ERROR)
150  {
151  // socket can't made blocking because WSAAsyncSelect/WSAEventSelect (==QSocketNotifier)
152  // is activated for them
153  if(WSAGetLastError() == WSAEINVAL)
154  return true;
155  qDebug("socket set %s failed %d", iMode ? "nonblocking" : "blocking", GetLastError());
156  setError(UnknownError);
157  return false; // error
158  }
159 
160 #else
161  {
162  int fdflags = fcntl(m_sockfd, F_GETFL, 0);
163  if (fdflags == -1)
164  {
165  setError(UnknownError);
166  return false; // error
167  }
168 
169  if (opts & Blocking)
170  fdflags &= ~O_NONBLOCK;
171  else
172  fdflags |= O_NONBLOCK;
173 
174  if (fcntl(m_sockfd, F_SETFL, fdflags) == -1)
175  {
176  setError(UnknownError);
177  return false; // error
178  }
179  }
180 #endif
181 
182  {
183  int on = opts & AddressReuseable ? 1 : 0;
184  if (setsockopt(m_sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) == -1)
185  {
186  setError(UnknownError);
187  return false; // error
188  }
189  }
190 
191 #if defined(IPV6_V6ONLY) && defined(AF_INET6)
192  if (d->af == AF_INET6)
193  {
194  // don't try this on non-IPv6 sockets, or we'll get an error
195 
196  int on = opts & IPv6Only ? 1 : 0;
197  if (setsockopt(m_sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&on, sizeof(on)) == -1)
198  {
199  setError(UnknownError);
200  return false; // error
201  }
202  }
203 #endif
204 
205  {
206  int on = opts & Broadcast ? 1 : 0;
207  if (setsockopt(m_sockfd, SOL_SOCKET, SO_BROADCAST, (char*)&on, sizeof(on)) == -1)
208  {
209  setError(UnknownError);
210  return false; // error
211  }
212  }
213 
214  if ((d->proto == IPPROTO_TCP || d->proto == 0) &&
215  (d->af == AF_INET
216 #if defined(AF_INET6)
217  || d->af == AF_INET6
218 #endif
219  ))
220  {
221  int on = opts & NoDelay ? 1 : 0;
222  if (setsockopt(m_sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)) == -1)
223  {
224  setError(UnknownError);
225  return false; // error
226  }
227  }
228 
229  return true; // all went well
230 }
231 
232 void KSocketDevice::close()
233 {
234  resetError();
235  if (m_sockfd != -1)
236  {
237  delete d->input;
238  delete d->output;
239  delete d->exception;
240 
241  d->input = d->output = d->exception = 0L;
242 #ifdef Q_WS_WIN
243  ::closesocket(m_sockfd);
244 #else
245  d->local.setFamily(AF_UNSPEC);
246  d->peer.setFamily(AF_UNSPEC);
247 
248  ::close(m_sockfd);
249 #endif
250  }
251  setOpenMode(0); // closed
252 
253  m_sockfd = -1;
254 }
255 
256 bool KSocketDevice::flush()
257 {
258  return false;
259 }
260 
261 bool KSocketDevice::create(int family, int type, int protocol)
262 {
263  resetError();
264 
265  if (m_sockfd != -1)
266  {
267  // it's already created!
268  setError(AlreadyCreated);
269  return false;
270  }
271 
272  // no socket yet; we have to create it
273  m_sockfd = kde_socket(family, type, protocol);
274 
275  if (m_sockfd == -1)
276  {
277  setError(NotSupported);
278  return false;
279  }
280 
281  d->af = family;
282  d->proto = protocol;
283  setSocketOptions(socketOptions());
284  setOpenMode(Unbuffered); // there's no "Open" flag
285  return true; // successfully created
286 }
287 
288 bool KSocketDevice::create(const KResolverEntry& address)
289 {
290  return create(address.family(), address.socketType(), address.protocol());
291 }
292 
293 bool KSocketDevice::bind(const KResolverEntry& address)
294 {
295  resetError();
296 
297  if (m_sockfd == -1 && !create(address))
298  return false; // failed creating
299 
300  // we have a socket, so try and bind
301  if (kde_bind(m_sockfd, address.address(), address.length()) == -1)
302  {
303  if (errno == EADDRINUSE)
304  {
305  setError(AddressInUse);
306  return false;
307  }
308  else if (errno == EINVAL)
309  setError(AlreadyBound);
310  else
311  {
312 #ifdef Q_WS_WIN
313  qDebug(" bind failed: %s ",address.address().toString().toLatin1().constData());
314 #endif
315  // assume the address is the cause
316  setError(NotSupported);
317  return false;
318  }
319  }
320 
321  return true;
322 }
323 
324 bool KSocketDevice::listen(int backlog)
325 {
326  if (m_sockfd != -1)
327  {
328  if (kde_listen(m_sockfd, backlog) == -1)
329  {
330  setError(NotSupported);
331  return false;
332  }
333 
334  resetError();
335  setOpenMode(QIODevice::Unbuffered | QIODevice::ReadWrite);
336  return true;
337  }
338 
339  // we don't have a socket
340  // can't listen
341  setError(NotCreated);
342  return false;
343 }
344 
345 bool KSocketDevice::connect(const KResolverEntry& address, OpenMode mode)
346 {
347  resetError();
348 
349  if (m_sockfd == -1 && !create(address))
350  return false; // failed creating!
351 
352  if (kde_connect(m_sockfd, address.address(), address.length()) == -1)
353  {
354  if (errno == EISCONN)
355  {
356  KActiveSocketBase::open(Unbuffered | mode);
357  return true; // we're already connected
358  }
359  else if (errno == EALREADY || errno == EINPROGRESS)
360  {
361  KActiveSocketBase::open(Unbuffered | mode);
362  setError(InProgress);
363  return true;
364  }
365  else if (errno == ECONNREFUSED)
366  setError(ConnectionRefused);
367  else if (errno == ENETDOWN || errno == ENETUNREACH ||
368  errno == ENETRESET || errno == ECONNABORTED ||
369  errno == ECONNRESET || errno == EHOSTDOWN ||
370  errno == EHOSTUNREACH)
371  setError(NetFailure);
372  else
373  setError(NotSupported);
374 
375  return false;
376  }
377 
378  KActiveSocketBase::open(Unbuffered | mode);
379  return true; // all is well
380 }
381 
382 KSocketDevice* KSocketDevice::accept()
383 {
384  if (m_sockfd == -1)
385  {
386  // can't accept without a socket
387  setError(NotCreated);
388  return 0L;
389  }
390 
391  struct sockaddr sa;
392  socklen_t len = sizeof(sa);
393  int newfd = kde_accept(m_sockfd, &sa, &len);
394  if (newfd == -1)
395  {
396  if (errno == EAGAIN || errno == EWOULDBLOCK)
397  setError(WouldBlock);
398  else
399  setError(UnknownError);
400  return NULL;
401  }
402 
403  return new KSocketDevice(newfd);
404 }
405 
406 bool KSocketDevice::disconnect()
407 {
408  resetError();
409 
410  if (m_sockfd == -1)
411  return false; // can't create
412 
413  KSocketAddress address;
414  address.setFamily(AF_UNSPEC);
415  if (kde_connect(m_sockfd, address.address(), address.length()) == -1)
416  {
417  if (errno == EALREADY || errno == EINPROGRESS)
418  {
419  setError(InProgress);
420  return false;
421  }
422  else if (errno == ECONNREFUSED)
423  setError(ConnectionRefused);
424  else if (errno == ENETDOWN || errno == ENETUNREACH ||
425  errno == ENETRESET || errno == ECONNABORTED ||
426  errno == ECONNRESET || errno == EHOSTDOWN ||
427  errno == EHOSTUNREACH)
428  setError(NetFailure);
429  else
430  setError(NotSupported);
431 
432  return false;
433  }
434 
435  setOpenMode(QIODevice::Unbuffered | QIODevice::ReadWrite);
436  return true; // all is well
437 }
438 
439 qint64 KSocketDevice::bytesAvailable() const
440 {
441  if (m_sockfd == -1)
442  return -1; // there's nothing to read in a closed socket
443 
444  int nchars;
445  if (kde_ioctl(m_sockfd, FIONREAD, &nchars) == -1)
446  return -1; // error!
447 
448  return nchars;
449 }
450 
451 qint64 KSocketDevice::waitForMore(int msecs, bool *timeout)
452 {
453  if (m_sockfd == -1)
454  return -1; // there won't ever be anything to read...
455 
456  bool input;
457  if (!poll(&input, 0, 0, msecs, timeout))
458  return -1; // failed polling
459 
460  return bytesAvailable();
461 }
462 
463 static int do_read_common(int sockfd, char *data, qint64 maxlen, KSocketAddress* from, ssize_t &retval, bool peek = false)
464 {
465  socklen_t len;
466  if (from)
467  {
468  from->setLength(len = 128); // arbitrary length
469  retval = ::recvfrom(sockfd, data, maxlen, peek ? MSG_PEEK : 0, from->address(), &len);
470  }
471  else
472  retval = ::recvfrom(sockfd, data, maxlen, peek ? MSG_PEEK : 0, NULL, NULL);
473 
474  if (retval == -1)
475  {
476 #ifdef Q_WS_WIN
477  if (WSAGetLastError() == WSAEWOULDBLOCK )
478  return KSocketDevice::WouldBlock;
479  else
480 #endif
481  if (errno == EAGAIN || errno == EWOULDBLOCK )
482  return KSocketDevice::WouldBlock;
483  else
484  return KSocketDevice::UnknownError;
485  }
486  if (retval == 0)
487  return KSocketDevice::RemotelyDisconnected;
488 
489  if (from)
490  from->setLength(len);
491  return 0;
492 }
493 
494 qint64 KSocketDevice::readData(char *data, qint64 maxlen, KSocketAddress *from)
495 {
496  resetError();
497  if (m_sockfd == -1)
498  return -1; // nothing to do here
499 
500  if (data == 0L || maxlen == 0)
501  return 0; // user doesn't want to read
502 
503  ssize_t retval;
504  int err = do_read_common(m_sockfd, data, maxlen, from, retval);
505 
506  if (err)
507  {
508  setError(static_cast<SocketError>(err));
509  return -1;
510  }
511 
512  return retval;
513 }
514 
515 qint64 KSocketDevice::peekData(char *data, qint64 maxlen, KSocketAddress* from)
516 {
517  resetError();
518  if (m_sockfd == -1)
519  return -1; // nothing to do here
520 
521  if (data == 0L || maxlen == 0)
522  return 0; // user doesn't want to read
523 
524  ssize_t retval;
525  int err = do_read_common(m_sockfd, data, maxlen, from, retval, true);
526 
527  if (err)
528  {
529  setError(static_cast<SocketError>(err));
530  return -1;
531  }
532 
533  return retval;
534 }
535 
536 qint64 KSocketDevice::writeData(const char *data, qint64 len, const KSocketAddress* to)
537 {
538  resetError();
539  if (m_sockfd == -1)
540  return -1; // can't write to unopen socket
541 
542  if (data == 0L || len == 0)
543  return 0; // nothing to be written
544 
545  ssize_t retval;
546  if (to != 0L)
547  retval = ::sendto(m_sockfd, data, len, 0, to->address(), to->length());
548  else
549 #ifdef Q_WS_WIN
550  retval = ::send(m_sockfd, data, len, 0);
551 #else
552  retval = ::write(m_sockfd, data, len);
553 #endif
554  if (retval == -1)
555  {
556  if (errno == EAGAIN || errno == EWOULDBLOCK)
557  setError(WouldBlock);
558  else
559  setError(UnknownError);
560  return -1; // nothing written
561  }
562  else if (retval == 0)
563  setError(RemotelyDisconnected);
564 
565  return retval;
566 }
567 
568 KSocketAddress KSocketDevice::localAddress() const
569 {
570  if (m_sockfd == -1)
571  return KSocketAddress(); // not open, empty value
572 
573  if (d->local.family() != AF_UNSPEC)
574  return d->local;
575 
576  socklen_t len;
577  KSocketAddress localAddress;
578  localAddress.setLength(len = 32); // arbitrary value
579  if (kde_getsockname(m_sockfd, localAddress.address(), &len) == -1)
580  // error!
581  return d->local = KSocketAddress();
582 
583 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
584  len = localAddress.address()->sa_len;
585 #endif
586 
587  if (len <= localAddress.length())
588  {
589  // it has fit already
590  localAddress.setLength(len);
591  return d->local = localAddress;
592  }
593 
594  // no, the socket address is actually larger than we had anticipated
595  // call again
596  localAddress.setLength(len);
597  if (kde_getsockname(m_sockfd, localAddress.address(), &len) == -1)
598  // error!
599  return d->local = KSocketAddress();
600 
601  return d->local = localAddress;
602 }
603 
604 KSocketAddress KSocketDevice::peerAddress() const
605 {
606  if (m_sockfd == -1)
607  return KSocketAddress(); // not open, empty value
608 
609  if (d->peer.family() != AF_UNSPEC)
610  return d->peer;
611 
612  socklen_t len;
613  KSocketAddress peerAddress;
614  peerAddress.setLength(len = 32); // arbitrary value
615  if (kde_getpeername(m_sockfd, peerAddress.address(), &len) == -1)
616  // error!
617  return d->peer = KSocketAddress();
618 
619 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
620  len = peerAddress.address()->sa_len;
621 #endif
622 
623  if (len <= peerAddress.length())
624  {
625  // it has fit already
626  peerAddress.setLength(len);
627  return d->peer = peerAddress;
628  }
629 
630  // no, the socket address is actually larger than we had anticipated
631  // call again
632  peerAddress.setLength(len);
633  if (kde_getpeername(m_sockfd, peerAddress.address(), &len) == -1)
634  // error!
635  return d->peer = KSocketAddress();
636 
637  return d->peer = peerAddress;
638 }
639 
640 KSocketAddress KSocketDevice::externalAddress() const
641 {
642  // for normal sockets, the externally visible address is the same
643  // as the local address
644  return localAddress();
645 }
646 
647 QSocketNotifier* KSocketDevice::readNotifier() const
648 {
649  if (d->input)
650  return d->input;
651 
652  QMutexLocker locker(mutex());
653  if (d->input)
654  return d->input;
655 
656  if (m_sockfd == -1)
657  {
658  // socket doesn't exist; can't create notifier
659  return 0L;
660  }
661 
662  return d->input = createNotifier(QSocketNotifier::Read);
663 }
664 
665 QSocketNotifier* KSocketDevice::writeNotifier() const
666 {
667  if (d->output)
668  return d->output;
669 
670  QMutexLocker locker(mutex());
671  if (d->output)
672  return d->output;
673 
674  if (m_sockfd == -1)
675  {
676  // socket doesn't exist; can't create notifier
677  return 0L;
678  }
679 
680  return d->output = createNotifier(QSocketNotifier::Write);
681 }
682 
683 QSocketNotifier* KSocketDevice::exceptionNotifier() const
684 {
685  if (d->exception)
686  return d->exception;
687 
688  QMutexLocker locker(mutex());
689  if (d->exception)
690  return d->exception;
691 
692  if (m_sockfd == -1)
693  {
694  // socket doesn't exist; can't create notifier
695  return 0L;
696  }
697 
698  return d->exception = createNotifier(QSocketNotifier::Exception);
699 }
700 
701 bool KSocketDevice::poll(bool *input, bool *output, bool *exception,
702  int timeout, bool* timedout)
703 {
704  if (m_sockfd == -1)
705  {
706  setError(NotCreated);
707  return false;
708  }
709 
710  resetError();
711 #ifdef HAVE_POLL
712  struct pollfd fds;
713  fds.fd = m_sockfd;
714  fds.events = 0;
715 
716  if (input)
717  {
718  fds.events |= POLLIN;
719  *input = false;
720  }
721  if (output)
722  {
723  fds.events |= POLLOUT;
724  *output = false;
725  }
726  if (exception)
727  {
728  fds.events |= POLLPRI;
729  *exception = false;
730  }
731 
732  int retval = ::poll(&fds, 1, timeout);
733  if (retval == -1)
734  {
735  setError(UnknownError);
736  return false;
737  }
738  if (retval == 0)
739  {
740  // timeout
741  if (timedout)
742  *timedout = true;
743  return true;
744  }
745 
746  if (input && fds.revents & POLLIN)
747  *input = true;
748  if (output && fds.revents & POLLOUT)
749  *output = true;
750  if (exception && fds.revents & POLLPRI)
751  *exception = true;
752  if (timedout)
753  *timedout = false;
754 
755  return true;
756 #else
757  /*
758  * We don't have poll(2). We'll have to make do with select(2).
759  */
760 
761  fd_set readfds, writefds, exceptfds;
762  fd_set *preadfds = 0L, *pwritefds = 0L, *pexceptfds = 0L;
763 
764  if (input)
765  {
766  preadfds = &readfds;
767  FD_ZERO(preadfds);
768  FD_SET(m_sockfd, preadfds);
769  *input = false;
770  }
771  if (output)
772  {
773  pwritefds = &writefds;
774  FD_ZERO(pwritefds);
775  FD_SET(m_sockfd, pwritefds);
776  *output = false;
777  }
778  if (exception)
779  {
780  pexceptfds = &exceptfds;
781  FD_ZERO(pexceptfds);
782  FD_SET(m_sockfd, pexceptfds);
783  *exception = false;
784  }
785 
786  int retval;
787  if (timeout < 0)
788  retval = select(m_sockfd + 1, preadfds, pwritefds, pexceptfds, 0L);
789  else
790  {
791  // convert the milliseconds to timeval
792  struct timeval tv;
793  tv.tv_sec = timeout / 1000;
794  tv.tv_usec = timeout % 1000 * 1000;
795 
796  retval = select(m_sockfd + 1, preadfds, pwritefds, pexceptfds, &tv);
797  }
798 
799  if (retval == -1)
800  {
801  setError(UnknownError);
802  return false;
803  }
804  if (retval == 0)
805  {
806  // timeout
807  if (timedout)
808  *timedout = true;
809  return true;
810  }
811 
812  if (input && FD_ISSET(m_sockfd, preadfds))
813  *input = true;
814  if (output && FD_ISSET(m_sockfd, pwritefds))
815  *output = true;
816  if (exception && FD_ISSET(m_sockfd, pexceptfds))
817  *exception = true;
818 
819  return true;
820 #endif
821 }
822 
823 bool KSocketDevice::poll(int timeout, bool *timedout)
824 {
825  bool input, output, exception;
826  return poll(&input, &output, &exception, timeout, timedout);
827 }
828 
829 QSocketNotifier* KSocketDevice::createNotifier(QSocketNotifier::Type type) const
830 {
831  if (m_sockfd == -1)
832  return 0L;
833 
834  return new QSocketNotifier(m_sockfd, type);
835 }
836 
837 namespace
838 {
839  // simple class to avoid pointer stuff
840  template<class T> class ptr
841  {
842  typedef T type;
843  type* obj;
844  public:
845  ptr() : obj(0)
846  { }
847 
848  ptr(const ptr<T>& other) : obj(other.obj)
849  { }
850 
851  ptr(type* _obj) : obj(_obj)
852  { }
853 
854  ~ptr()
855  { }
856 
857  ptr<T>& operator=(const ptr<T>& other)
858  { obj = other.obj; return *this; }
859 
860  ptr<T>& operator=(T* _obj)
861  { obj = _obj; return *this; }
862 
863  type* operator->() const { return obj; }
864 
865  operator T*() const { return obj; }
866 
867  bool isNull() const
868  { return obj == 0; }
869  };
870 }
871 
872 static KSocketDeviceFactoryBase* defaultImplFactory;
873 static QMutex defaultImplFactoryMutex;
874 typedef QMap<int, KSocketDeviceFactoryBase* > factoryMap;
875 static factoryMap factories;
876 
877 KSocketDevice* KSocketDevice::createDefault(KSocketBase* parent)
878 {
879  KSocketDevice* device = dynamic_cast<KSocketDevice*>(parent);
880  if (device != 0L)
881  return device;
882 
883  if (defaultImplFactory)
884  return defaultImplFactory->create(parent);
885 
886  // the really default
887  return new KSocketDevice(parent);
888 }
889 
890 KSocketDevice* KSocketDevice::createDefault(KSocketBase* parent, int capabilities)
891 {
892  KSocketDevice* device = dynamic_cast<KSocketDevice*>(parent);
893  if (device != 0L)
894  return device;
895 
896  QMutexLocker locker(&defaultImplFactoryMutex);
897  factoryMap::ConstIterator it = factories.constBegin();
898  for ( ; it != factories.constEnd(); ++it)
899  if ((it.key() & capabilities) == capabilities)
900  // found a match
901  return it.value()->create(parent);
902 
903  return 0L; // no default
904 }
905 
906 KSocketDeviceFactoryBase*
907 KSocketDevice::setDefaultImpl(KSocketDeviceFactoryBase* factory)
908 {
909  QMutexLocker locker(&defaultImplFactoryMutex);
910  KSocketDeviceFactoryBase* old = defaultImplFactory;
911  defaultImplFactory = factory;
912  return old;
913 }
914 
915 void KSocketDevice::addNewImpl(KSocketDeviceFactoryBase* factory, int capabilities)
916 {
917  QMutexLocker locker(&defaultImplFactoryMutex);
918  if (factories.contains(capabilities))
919  delete factories[capabilities];
920  factories.insert(capabilities, factory);
921 }
922 
k3socketaddress.h
KNetwork::KSocketBase::AddressInUse
Definition: k3socketbase.h:147
QMutex
qint64
KNetwork::KSocketBase::AlreadyCreated
Definition: k3socketbase.h:148
KNetwork::KSocketDevice::listen
virtual bool listen(int backlog=5)
Puts this socket into listening mode.
Definition: k3socketdevice.cpp:324
KNetwork::KSocketDevice::writeData
virtual qint64 writeData(const char *data, qint64 len, const KSocketAddress *to=0L)
Writes the given data to the given destination address.
Definition: k3socketdevice.cpp:536
k3socketdevice.h
KFileSystemType::Type
Type
Definition: kfilesystemtype_p.h:28
QMap::contains
bool contains(const Key &key) const
defaultImplFactoryMutex
static QMutex defaultImplFactoryMutex
Definition: k3socketdevice.cpp:873
QSocketNotifier
T
#define T
KNetwork::KSocketDevice::accept
virtual KSocketDevice * accept()
Accepts a new incoming connection.
Definition: k3socketdevice.cpp:382
KNetwork::KActiveSocketBase
Abstract class for active sockets.
Definition: k3socketbase.h:461
KNetwork::KSocketAddress::toString
virtual QString toString() const
Returns this socket address as a string suitable for printing.
Definition: k3socketaddress.cpp:606
KNetwork::KSocketBase::NotCreated
Definition: k3socketbase.h:153
timeout
int timeout
Definition: kkernel_mac.cpp:46
KNetwork::KResolverEntry
One resolution entry.
Definition: k3resolver.h:68
KNetwork::KSocketAddress::setLength
KSocketAddress & setLength(quint16 len)
Sets the length of this socket structure.
Definition: k3socketaddress.cpp:480
KNetwork::KSocketDevice::flush
virtual bool flush()
This call is not supported on sockets.
Definition: k3socketdevice.cpp:256
KNetwork::KSocketBase::AddressReuseable
Definition: k3socketbase.h:111
KNetwork::KActiveSocketBase::resetError
void resetError()
Resets the socket error code and the I/O Device's status.
Definition: k3socketbase.cpp:441
KNetwork::KSocketDevice::writeNotifier
QSocketNotifier * writeNotifier() const
Returns a socket notifier for output on this socket.
Definition: k3socketdevice.cpp:665
KNetwork::KSocketDevice::connect
virtual bool connect(const KResolverEntry &address, OpenMode mode=ReadWrite)
Connect to a remote host.
Definition: k3socketdevice.cpp:345
QMap::constBegin
const_iterator constBegin() const
QMap
KNetwork::KSocketBase::UnknownError
Definition: k3socketbase.h:161
KNetwork::KSocketDevice::capabilities
virtual int capabilities() const
Returns the set of capabilities this socket class implements.
Definition: k3socketdevice.cpp:132
KNetwork::KSocketBase::InProgress
Definition: k3socketbase.h:157
KNetwork::KSocketDevice::socket
int socket() const
Returns the file descriptor for this socket.
Definition: k3socketdevice.cpp:127
syssocket.h
KNetwork::KSocketAddress
A generic socket address.
Definition: k3socketaddress.h:414
KNetwork::KSocketAddress::address
const sockaddr * address() const
Returns the socket address structure, to be passed down to low level functions.
Definition: k3socketaddress.cpp:449
KNetwork::KSocketDevice::setSocketOptions
virtual bool setSocketOptions(int opts)
This implementation sets the options on the socket.
Definition: k3socketdevice.cpp:137
KNetwork::KSocketDevice::exceptionNotifier
QSocketNotifier * exceptionNotifier() const
Returns a socket notifier for exceptional events on this socket.
Definition: k3socketdevice.cpp:683
KNetwork::KSocketDevice::createNotifier
virtual QSocketNotifier * createNotifier(QSocketNotifier::Type type) const
Creates a socket notifier of the given type.
Definition: k3socketdevice.cpp:829
KNetwork::KSocketDevice::~KSocketDevice
virtual ~KSocketDevice()
Destructor.
Definition: k3socketdevice.cpp:120
KNetwork::KResolverEntry::protocol
int protocol() const
Retrieves the protocol associated with this entry.
Definition: k3resolver.cpp:171
KNetwork::KSocketAddress::setFamily
virtual KSocketAddress & setFamily(int family)
Sets the family of this object.
Definition: k3socketaddress.cpp:494
KNetwork::KActiveSocketBase::open
virtual bool open(OpenMode mode)
Reimplemented from QIODevice.
Definition: k3socketbase.cpp:339
factories
static factoryMap factories
Definition: k3socketdevice.cpp:875
k3socks.h
KNetwork::KSocketBase::WouldBlock
Definition: k3socketbase.h:154
KNetwork::KSocketAddress::family
int family() const
Returns the family of this address.
Definition: k3socketaddress.cpp:487
KNetwork::KSocketDevice::readNotifier
QSocketNotifier * readNotifier() const
Returns a socket notifier for input on this socket.
Definition: k3socketdevice.cpp:647
factoryMap
QMap< int, KSocketDeviceFactoryBase * > factoryMap
Definition: k3socketdevice.cpp:874
KNetwork::KSocketBase::RemotelyDisconnected
Definition: k3socketbase.h:162
KNetwork::KSocketDevice::addNewImpl
static void addNewImpl(KSocketDeviceFactoryBase *factory, int capabilities)
Adds a factory of KSocketDevice objects to the list, along with its capabilities flag.
Definition: k3socketdevice.cpp:915
KNetwork::KSocketDevice::bind
virtual bool bind(const KResolverEntry &address)
Binds this socket to the given address.
Definition: k3socketdevice.cpp:293
KNetwork::KSocketDevice::create
virtual bool create(int family, int type, int protocol)
Creates a socket but don't connect or bind anywhere.
Definition: k3socketdevice.cpp:261
QObject
KNetwork::KSocketBase::NoDelay
Definition: k3socketbase.h:115
KNetwork::KSocketBase::mutex
QMutex * mutex() const
Returns the internal mutex for this class.
Definition: k3socketbase.cpp:320
QMap::constEnd
const_iterator constEnd() const
QByteArray::constData
const char * constData() const
KNetwork::KSocketDevice::readData
virtual qint64 readData(char *data, qint64 maxlen, KSocketAddress *from=0L)
Reads data and the source address from this socket.
Definition: k3socketdevice.cpp:494
KNetwork::KSocketDevice::peekData
virtual qint64 peekData(char *data, qint64 maxlen, KSocketAddress *from=0L)
Peeks the data in the socket and the source address.
Definition: k3socketdevice.cpp:515
KNetwork::KSocketDeviceFactoryBase
Definition: k3socketdevice.h:387
KNetwork::KSocketBase::setSocketOptions
virtual bool setSocketOptions(int opts)
Set the given socket options.
Definition: k3socketbase.cpp:86
do_read_common
static int do_read_common(int sockfd, char *data, qint64 maxlen, KSocketAddress *from, ssize_t &retval, bool peek=false)
Definition: k3socketdevice.cpp:463
KNetwork::KActiveSocketBase::setError
void setError(SocketError error)
Sets the socket's error code.
Definition: k3socketbase.cpp:435
KNetwork::KSocketBase::NotSupported
Definition: k3socketbase.h:159
KNetwork::KActiveSocketBase::write
qint64 write(const char *data, qint64 len)
Writes the given data to the socket.
Definition: k3socketbase.cpp:404
k3resolver.h
KNetwork::KResolverEntry::address
KSocketAddress address() const
Retrieves the socket address associated with this entry.
Definition: k3resolver.cpp:135
KNetwork::KSocketDevice::setDefaultImpl
static KSocketDeviceFactoryBase * setDefaultImpl(KSocketDeviceFactoryBase *factory)
Sets the default KSocketDevice implementation to use and return the old factory.
Definition: k3socketdevice.cpp:907
KNetwork::KSocketDevice::disconnect
virtual bool disconnect()
Disconnects this socket.
Definition: k3socketdevice.cpp:406
defaultImplFactory
static KSocketDeviceFactoryBase * defaultImplFactory
Definition: k3socketdevice.cpp:872
KNetwork::KActiveSocketBase::setSocketDevice
virtual void setSocketDevice(KSocketDevice *device)
Definition: k3socketbase.cpp:347
KNetwork::KSocketDeviceFactoryBase::create
virtual KSocketDevice * create(KSocketBase *) const =0
KNetwork::KSocketDevice::KSocketDevice
KSocketDevice(const KSocketBase *=0L, QObject *objparent=0L)
Default constructor.
Definition: k3socketdevice.cpp:87
KNetwork::KSocketBase::Broadcast
Definition: k3socketbase.h:114
k3socketbase.h
KNetwork::KResolverEntry::length
quint16 length() const
Retrieves the length of the socket address structure.
Definition: k3resolver.cpp:141
KNetwork::KSocketDevice::m_sockfd
int m_sockfd
The socket file descriptor.
Definition: k3socketdevice.h:96
KNetwork::KSocketBase::IPv6Only
Definition: k3socketbase.h:112
QString::toLatin1
QByteArray toLatin1() const
KNetwork::KResolverEntry::socketType
int socketType() const
Retrieves the socket type associated with this entry.
Definition: k3resolver.cpp:165
KNetwork::KSocketDevice::close
virtual void close()
Closes the socket.
Definition: k3socketdevice.cpp:232
output
void output(QList< Action > actions, QHash< QString, QString > domain)
Definition: fake/kauth-policy-gen-polkit.cpp:41
KNetwork::KSocketDevice::waitForMore
virtual qint64 waitForMore(int msecs, bool *timeout=0L)
Waits up to msecs for more data to be available on this socket.
Definition: k3socketdevice.cpp:451
QMutexLocker
KNetwork::KSocketDevice::poll
virtual bool poll(bool *input, bool *output, bool *exception=0L, int timeout=-1, bool *timedout=0L)
Executes a poll in the socket, via select(2) or poll(2).
Definition: k3socketdevice.cpp:701
KNetwork::KSocketAddress::length
quint16 length() const
Returns the length of this socket address structure.
Definition: k3socketaddress.cpp:473
KNetwork::KSocketDevice::localAddress
virtual KSocketAddress localAddress() const
Returns this socket's local address.
Definition: k3socketdevice.cpp:568
KNetwork::KSocketDevice::peerAddress
virtual KSocketAddress peerAddress() const
Returns this socket's peer address.
Definition: k3socketdevice.cpp:604
KNetwork::KSocketBase::AlreadyBound
Definition: k3socketbase.h:149
KNetwork::KResolverEntry::family
int family() const
Retrieves the family associated with this socket address.
Definition: k3resolver.cpp:147
KNetwork::KSocketDevice
Low-level socket functionality.
Definition: k3socketdevice.h:51
QMap::insert
iterator insert(const Key &key, const T &value)
KNetwork::KSocketBase::Blocking
Definition: k3socketbase.h:110
KNetwork::KSocketBase::ConnectionRefused
Definition: k3socketbase.h:155
KNetwork::KSocketBase::NetFailure
Definition: k3socketbase.h:158
KNetwork::KSocketBase::socketOptions
virtual int socketOptions() const
Retrieves the socket options that have been set.
Definition: k3socketbase.cpp:92
KNetwork::KSocketBase
Basic socket functionality.
Definition: k3socketbase.h:85
QMap::ConstIterator
typedef ConstIterator
QIODevice::setOpenMode
void setOpenMode(QFlags< QIODevice::OpenModeFlag > openMode)
QObject::parent
QObject * parent() const
KNetwork::KSocketDevice::externalAddress
virtual KSocketAddress externalAddress() const
Returns this socket's externally visible local address.
Definition: k3socketdevice.cpp:640
KNetwork::KSocketDevice::createDefault
static KSocketDevice * createDefault(KSocketBase *parent)
Creates a new default KSocketDevice object given the parent object.
Definition: k3socketdevice.cpp:877
KNetwork::KSocketDevice::bytesAvailable
virtual qint64 bytesAvailable() const
Returns the number of bytes available for reading without blocking.
Definition: k3socketdevice.cpp:439
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal