PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
socket.h
Go to the documentation of this file.
1 /*
2  * socket.h
3  *
4  * Berkley Socket channel ancestor class.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 32474 $
30  * $Author: rjongbloed $
31  * $Date: 2014-08-09 14:23:47 +1000 (Sat, 09 Aug 2014) $
32  */
33 
34 #ifndef PTLIB_SOCKET_H
35 #define PTLIB_SOCKET_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 #include <ptlib/channel.h>
42 
43 #ifdef P_OPENBSD
44 #include <sys/uio.h>
45 #endif
46 
47 #ifdef __NUCLEUS_PLUS__
48 #include <sys/socket.h>
49 #endif
50 
51 
52 class PSocket;
53 
54 PLIST(PSocketList, PSocket);
55 
56 
63 class PSocket : public PChannel
64 {
65  PCLASSINFO(PSocket, PChannel);
66 
67  protected:
68  PSocket();
69 
70  public:
83  virtual PBoolean Connect(
84  const PString & address
85  );
86 
87 
89  enum Reusability {
92  };
93 
107  virtual PBoolean Listen(
108  unsigned queueSize = 5,
109  WORD port = 0,
111  );
112 
113 
135  virtual PBoolean Accept(
136  PSocket & socket
137  );
138 
144  virtual PBoolean Shutdown(
145  ShutdownValue option
146  );
148 
158  int option,
159  int value,
160  int level = SOL_SOCKET
161  );
162 
170  int option,
171  const void * valuePtr,
172  PINDEX valueSize,
173  int level = SOL_SOCKET
174  );
175 
183  int option,
184  int & value,
185  int level = SOL_SOCKET
186  );
187 
195  int option,
196  void * valuePtr,
197  PINDEX valueSize,
198  int level = SOL_SOCKET
199  );
201 
209  static WORD GetProtocolByName(
210  const PString & name
211  );
212 
218  static PString GetNameByProtocol(
219  WORD proto
220  );
221 
222 
224  virtual WORD GetPortByService(
225  const PString & service
226  ) const;
244  static WORD GetPortByService(
245  const char * protocol,
246  const PString & service
247  );
248 
250  virtual PString GetServiceByPort(
251  WORD port
252  ) const;
270  static PString GetServiceByPort(
271  const char * protocol,
272  WORD port
273  );
274 
275 
277  void SetPort(
278  WORD port
279  );
292  void SetPort(
293  const PString & service
294  );
295 
301  WORD GetPort() const;
302 
310  PString GetService() const;
312 
315 
316  class SelectList : public PSocketList
317  {
318  PCLASSINFO(SelectList, PSocketList)
319  public:
321  { DisallowDeleteObjects(); }
323  void operator+=(PSocket & sock )
324  { Append(&sock); }
326  void operator-=(PSocket & sock )
327  { Remove(&sock); }
328  };
329 
331  static int Select(
332  PSocket & sock1,
333  PSocket & sock2
334  );
336  static int Select(
337  PSocket & sock1,
338  PSocket & sock2,
339  const PTimeInterval & timeout
340  );
342  static Errors Select(
343  SelectList & read
344  );
346  static Errors Select(
347  SelectList & read,
348  const PTimeInterval & timeout
349  );
351  static Errors Select(
352  SelectList & read,
353  SelectList & write
354  );
356  static Errors Select(
357  SelectList & read,
358  SelectList & write,
359  const PTimeInterval & timeout
360  );
362  static Errors Select(
363  SelectList & read,
364  SelectList & write,
365  SelectList & except
366  );
388  static Errors Select(
389  SelectList & read,
390  SelectList & write,
391  SelectList & except,
392  const PTimeInterval & timeout
393  );
395 
398 
399  inline static WORD Host2Net(WORD v) { return htons(v); }
401  inline static DWORD Host2Net(DWORD v) { return htonl(v); }
402 
404  inline static WORD Net2Host(WORD v) { return ntohs(v); }
406  inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
408 
409 
414 #if _WIN32
415  struct Slice : public WSABUF
416  {
417  Slice()
418  { buf = (char *)0; len = 0; }
419 
420  Slice(void * v, const size_t len)
421  { SetBase(v); SetLength(len); }
422 
423  Slice(const void * v, const size_t len)
424  { SetBase(v); SetLength(len); }
425 
426  void SetBase(const void * v) { buf = (char *)v; }
427  void SetBase(void * v) { buf = (char *)v; }
428  void * GetBase() const { return buf; }
429 
430  void SetLength(size_t v) { len = (ULONG)v; }
431  size_t GetLength() const { return len; }
432  };
433 #else
434 #if P_HAS_RECVMSG
435  struct Slice : public iovec
436  {
437 #else
438  struct Slice
439  {
440  protected:
441  void * iov_base;
442  size_t iov_len;
443  public:
444 #endif
446  { SetBase(NULL); SetLength(0); }
447 
448  Slice(void * v, size_t len)
449  { SetBase(v); SetLength(len); }
450 
451  void SetBase(void * v) { iov_base = v; }
452  void * GetBase() const { return iov_base; }
453  void SetLength(size_t v) { iov_len = v; }
454  size_t GetLength() const { return iov_len; }
455  };
456 #endif
457 
467  virtual bool Read(
468  Slice * slices, // slices to read to
469  size_t sliceCount
470  );
471 
481  virtual bool Write(
482  const Slice * slices, // slices to write from
483  size_t sliceCount
484  );
486 
487  protected:
488  virtual PBoolean ConvertOSError(P_INT_PTR libcReturnValue, ErrorGroup group = LastGeneralError);
489 
490  /*This function calls os_socket() with the correct parameters for the
491  socket protocol type.
492  */
493  virtual PBoolean OpenSocket() = 0;
494 
497  virtual const char * GetProtocolName() const = 0;
498 
499 
500  int os_close();
501  int os_socket(int af, int type, int proto);
503  struct sockaddr * sin,
504  socklen_t size
505  );
506 
508  Slice * slices,
509  size_t sliceCount,
510  int flags,
511  struct sockaddr * from,
512  socklen_t * fromlen
513  );
514 
516  const Slice * slices,
517  size_t sliceCount,
518  int flags,
519  struct sockaddr * to,
520  socklen_t tolen
521  );
522 
524  PSocket & listener,
525  struct sockaddr * addr,
526  socklen_t * size
527  );
528 
529  virtual int os_errno() const;
530 
531 
532  // Member variables
534  WORD port;
535 
536 // Include platform dependent part of class
537 #ifdef _WIN32
538 #include "msos/ptlib/socket.h"
539 #else
540 #include "unix/ptlib/socket.h"
541 #endif
542 };
543 
544 
545 // Utility classes
546 
547 class P_fd_set {
548  public:
549  P_fd_set();
550  P_fd_set(SOCKET fd);
552  {
553  free(set);
554  }
555 
556  P_fd_set & operator=(SOCKET fd);
557  P_fd_set & operator+=(SOCKET fd);
558  P_fd_set & operator-=(SOCKET fd);
559 
560  void Zero();
561 
562  PBoolean IsPresent(SOCKET fd) const;
563 
564  operator fd_set*() const
565  {
566  return set;
567  }
568 
569  protected:
570  void Construct();
571 
572  SOCKET max_fd;
573  fd_set * set;
574 
575  private:
576 #ifdef __MINGW32__
577  // avoid compile error when this declaration is private
578  // this could be a gcc bug
579  public:
580  P_fd_set(const P_fd_set &) {}
581  private:
582 #else
583  P_fd_set(const P_fd_set &) {}
584 #endif
585  void operator=(const P_fd_set &) {}
586 };
587 
588 
589 class P_timeval {
590  public:
591  P_timeval();
592  P_timeval(const PTimeInterval & time)
593  {
594  operator=(time);
595  }
596 
597  P_timeval & operator=(const PTimeInterval & time);
598 
599  operator timeval*()
600  {
601  return infinite ? NULL : &tval;
602  }
603 
604  timeval * operator->()
605  {
606  return &tval;
607  }
608 
609  timeval & operator*()
610  {
611  return tval;
612  }
613 
614  private:
615  struct timeval tval;
616  PBoolean infinite;
617 };
618 
619 
620 #endif // PTLIB_SOCKET_H
621 
622 
623 // End Of File ///////////////////////////////////////////////////////////////