PTLib  Version 2.10.11
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: 27568 $
30  * $Author: ededu $
31  * $Date: 2012-05-03 07:57:49 -0500 (Thu, 03 May 2012) $
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 __NUCLEUS_PLUS__
44 #include <sys/socket.h>
45 #endif
46 
47 class PSocket;
48 
49 PLIST(PSocketList, PSocket);
50 
51 
58 class PSocket : public PChannel
59 {
60  PCLASSINFO(PSocket, PChannel);
61 
62  protected:
63  PSocket();
64 
65  public:
78  virtual PBoolean Connect(
79  const PString & address
80  );
81 
82 
84  enum Reusability {
87  };
88 
102  virtual PBoolean Listen(
103  unsigned queueSize = 5,
104  WORD port = 0,
106  );
107 
108 
130  virtual PBoolean Accept(
131  PSocket & socket
132  );
133 
139  virtual PBoolean Shutdown(
140  ShutdownValue option
141  );
143 
153  int option,
154  int value,
155  int level = SOL_SOCKET
156  );
157 
165  int option,
166  const void * valuePtr,
167  PINDEX valueSize,
168  int level = SOL_SOCKET
169  );
170 
178  int option,
179  int & value,
180  int level = SOL_SOCKET
181  );
182 
190  int option,
191  void * valuePtr,
192  PINDEX valueSize,
193  int level = SOL_SOCKET
194  );
196 
204  static WORD GetProtocolByName(
205  const PString & name
206  );
207 
213  static PString GetNameByProtocol(
214  WORD proto
215  );
216 
217 
219  virtual WORD GetPortByService(
220  const PString & service
221  ) const;
239  static WORD GetPortByService(
240  const char * protocol,
241  const PString & service
242  );
243 
245  virtual PString GetServiceByPort(
246  WORD port
247  ) const;
265  static PString GetServiceByPort(
266  const char * protocol,
267  WORD port
268  );
269 
270 
272  void SetPort(
273  WORD port
274  );
287  void SetPort(
288  const PString & service
289  );
290 
296  WORD GetPort() const;
297 
305  PString GetService() const;
307 
310  class SelectList : public PSocketList
312  {
313  PCLASSINFO(SelectList, PSocketList)
314  public:
316  { DisallowDeleteObjects(); }
318  void operator+=(PSocket & sock )
319  { Append(&sock); }
321  void operator-=(PSocket & sock )
322  { Remove(&sock); }
323  };
324 
326  static int Select(
327  PSocket & sock1,
328  PSocket & sock2
329  );
331  static int Select(
332  PSocket & sock1,
333  PSocket & sock2,
334  const PTimeInterval & timeout
335  );
337  static Errors Select(
338  SelectList & read
339  );
341  static Errors Select(
342  SelectList & read,
343  const PTimeInterval & timeout
344  );
346  static Errors Select(
347  SelectList & read,
348  SelectList & write
349  );
351  static Errors Select(
352  SelectList & read,
353  SelectList & write,
354  const PTimeInterval & timeout
355  );
357  static Errors Select(
358  SelectList & read,
359  SelectList & write,
360  SelectList & except
361  );
383  static Errors Select(
384  SelectList & read,
385  SelectList & write,
386  SelectList & except,
387  const PTimeInterval & timeout
388  );
390 
393  inline static WORD Host2Net(WORD v) { return htons(v); }
396  inline static DWORD Host2Net(DWORD v) { return htonl(v); }
397 
399  inline static WORD Net2Host(WORD v) { return ntohs(v); }
401  inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
403 
404  protected:
405  /*This function calls os_socket() with the correct parameters for the
406  socket protocol type.
407  */
408  virtual PBoolean OpenSocket() = 0;
409 
412  virtual const char * GetProtocolName() const = 0;
413 
414 
415  int os_close();
416  int os_socket(int af, int type, int proto);
418  struct sockaddr * sin,
419  PINDEX size
420  );
422  void * buf,
423  PINDEX len,
424  int flags,
425  struct sockaddr * from,
426  PINDEX * fromlen
427  );
429  const void * buf,
430  PINDEX len,
431  int flags,
432  struct sockaddr * to,
433  PINDEX tolen
434  );
436  PSocket & listener,
437  struct sockaddr * addr,
438  PINDEX * size
439  );
440 
441 
442  // Member variables
444  WORD port;
445 
446 #if P_HAS_RECVMSG
447  PBoolean catchReceiveToAddr;
448  virtual void SetLastReceiveAddr(void * /*addr*/, int /*addrLen*/)
449  { }
450 #endif
451 
452 // Include platform dependent part of class
453 #ifdef _WIN32
454 #include "msos/ptlib/socket.h"
455 #else
456 #include "unix/ptlib/socket.h"
457 #endif
458 };
459 
460 
461 // Utility classes
462 
463 class P_fd_set {
464  public:
465  P_fd_set();
466  P_fd_set(SOCKET fd);
468  {
469  free(set);
470  }
471 
472  P_fd_set & operator=(SOCKET fd);
473  P_fd_set & operator+=(SOCKET fd);
474  P_fd_set & operator-=(SOCKET fd);
475 
476  void Zero();
477 
478  PBoolean IsPresent(SOCKET fd) const
479  {
480  return FD_ISSET(fd, set);
481  }
482 
483  operator fd_set*() const
484  {
485  return set;
486  }
487 
488  protected:
489  void Construct();
490 
491  SOCKET max_fd;
492  fd_set * set;
493 
494  private:
495 #ifdef __MINGW32__
496  // avoid compile error when this declaration is private
497  // this could be a gcc bug
498  public:
499  P_fd_set(const P_fd_set &) {}
500  private:
501 #else
502  P_fd_set(const P_fd_set &) {}
503 #endif
504  void operator=(const P_fd_set &) {}
505 };
506 
507 
508 class P_timeval {
509  public:
510  P_timeval();
511  P_timeval(const PTimeInterval & time)
512  {
513  operator=(time);
514  }
515 
516  P_timeval & operator=(const PTimeInterval & time);
517 
518  operator timeval*()
519  {
520  return infinite ? NULL : &tval;
521  }
522 
523  timeval * operator->()
524  {
525  return &tval;
526  }
527 
528  timeval & operator*()
529  {
530  return tval;
531  }
532 
533  private:
534  struct timeval tval;
535  PBoolean infinite;
536 };
537 
538 #ifdef _WIN32
539 class PWinSock : public PSocket
540 {
541  PCLASSINFO(PWinSock, PSocket)
542 // Must be one and one only instance of this class, and it must be static!.
543  public:
544  PWinSock();
545  ~PWinSock();
546  private:
547  virtual PBoolean OpenSocket();
548  virtual const char * GetProtocolName() const;
549 };
550 #endif
551 
552 
553 #endif // PTLIB_SOCKET_H
554 
555 
556 // End Of File ///////////////////////////////////////////////////////////////
Definition: socket.h:86
P_timeval(const PTimeInterval &time)
Definition: socket.h:511
int os_close()
static DWORD Host2Net(DWORD v)
Convert from host to network byte order.
Definition: socket.h:396
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PString GetService() const
Get a service name for the port number the TCP socket channel object instance is using.
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PLIST(PSocketList, PSocket)
PChannel & operator=(const PChannel &)
PBoolean os_connect(struct sockaddr *sin, PINDEX size)
static WORD GetProtocolByName(const PString &name)
Get the number of the protocol associated with the specified name.
static int Select(PSocket &sock1, PSocket &sock2)
Select a socket with available data.
List of sockets used for Select() function.
Definition: socket.h:311
virtual PBoolean Shutdown(ShutdownValue option)
Close one or both of the data streams associated with a socket.
WORD port
Port to be used by the socket when opening the channel.
Definition: socket.h:444
ShutdownValue
Definition: channel.h:433
static PString GetNameByProtocol(WORD proto)
Get the name of the protocol number specified.
Errors
Normalised error codes.
Definition: channel.h:529
virtual PBoolean Connect(const PString &address)
Connect a socket to a remote host on the specified port number.
timeval * operator->()
Definition: socket.h:523
BOOL PBoolean
Definition: object.h:102
static DWORD Net2Host(DWORD v)
Convert from network to host byte order.
Definition: socket.h:401
PBoolean GetOption(int option, int &value, int level=SOL_SOCKET)
Get options on the socket.
Definition: socket.h:85
PBoolean os_accept(PSocket &listener, struct sockaddr *addr, PINDEX *size)
#define free(p)
Override of system call for memory check system.
Definition: object.h:864
virtual PBoolean OpenSocket()=0
virtual WORD GetPortByService(const PString &service) const
Get the port number for the specified service name.
static WORD Host2Net(WORD v)
Convert from host to network byte order.
Definition: socket.h:394
Abstract class defining I/O channel semantics.
Definition: channel.h:107
Definition: socket.h:463
The character string class.
Definition: pstring.h:108
SOCKET max_fd
Definition: socket.h:491
virtual PString GetServiceByPort(WORD port) const
Get the service name from the port number.
Definition: socket.h:508
PBoolean os_recvfrom(void *buf, PINDEX len, int flags, struct sockaddr *from, PINDEX *fromlen)
WORD GetPort() const
Get the port the TCP socket channel object instance is using.
void operator-=(PSocket &sock)
Remove a socket from list .
Definition: socket.h:321
void operator+=(PSocket &sock)
Add a socket to list .
Definition: socket.h:318
~P_fd_set()
Definition: socket.h:467
virtual const char * GetProtocolName() const =0
This function returns the protocol name for the socket type.
A network communications channel.
Definition: socket.h:58
static WORD Net2Host(WORD v)
Convert from network to host byte order.
Definition: socket.h:399
Reusability
Flags to reuse of port numbers in Listen() function.
Definition: socket.h:84
PBoolean SetOption(int option, int value, int level=SOL_SOCKET)
Set options on the socket.
PBoolean IsPresent(SOCKET fd) const
Definition: socket.h:478
virtual PBoolean Listen(unsigned queueSize=5, WORD port=0, Reusability reuse=AddressIsExclusive)
Listen on a socket for a remote host on the specified port number.
virtual PBoolean Accept(PSocket &socket)
Open a socket to a remote host on the specified port number.
void SetPort(WORD port)
Set the port number for the channel.
int os_socket(int af, int type, int proto)
timeval & operator*()
Definition: socket.h:528
PBoolean os_sendto(const void *buf, PINDEX len, int flags, struct sockaddr *to, PINDEX tolen)