PTLib  Version 2.18.8
 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 
30 #ifndef PTLIB_SOCKET_H
31 #define PTLIB_SOCKET_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 #include <ptlib/channel.h>
38 
39 #ifdef P_OPENBSD
40 #include <sys/uio.h>
41 #endif
42 
43 #ifdef __NUCLEUS_PLUS__
44 #include <sys/socket.h>
45 #endif
46 
47 
48 class PSocket;
49 
50 PLIST(PSocketList, PSocket);
51 
52 
59 class PSocket : public PChannel
60 {
61  PCLASSINFO(PSocket, PChannel);
62 
63  protected:
64  PSocket();
65 
66  public:
79  virtual PBoolean Connect(
80  const PString & address
81  );
82 
83 
85  enum Reusability {
88  };
89 
103  virtual PBoolean Listen(
104  unsigned queueSize = 5,
105  WORD port = 0,
107  );
108 
109 
131  virtual PBoolean Accept(
132  PSocket & socket
133  );
134 
140  virtual PBoolean Shutdown(
141  ShutdownValue option
142  );
144 
154  int option,
155  int value,
156  int level = SOL_SOCKET
157  );
158 
166  int option,
167  const void * valuePtr,
168  PINDEX valueSize,
169  int level = SOL_SOCKET
170  );
171 
179  int option,
180  int & value,
181  int level = SOL_SOCKET
182  );
183 
191  int option,
192  void * valuePtr,
193  PINDEX valueSize,
194  int level = SOL_SOCKET
195  );
197 
205  static WORD GetProtocolByName(
206  const PString & name
207  );
208 
214  static PString GetNameByProtocol(
215  WORD proto
216  );
217 
218 
220  virtual WORD GetPortByService(
221  const PString & service
222  ) const;
240  static WORD GetPortByService(
241  const char * protocol,
242  const PString & service
243  );
244 
246  virtual PString GetServiceByPort(
247  WORD port
248  ) const;
266  static PString GetServiceByPort(
267  const char * protocol,
268  WORD port
269  );
270 
271 
273  void SetPort(
274  WORD port
275  );
288  void SetPort(
289  const PString & service
290  );
291 
297  WORD GetPort() const;
298 
306  PString GetService() const;
308 
311  class SelectList : public PSocketList
313  {
314  PCLASSINFO(SelectList, PSocketList)
315  public:
317  { DisallowDeleteObjects(); }
319  void operator+=(PSocket & sock )
320  { Append(&sock); }
322  void operator-=(PSocket & sock )
323  { Remove(&sock); }
324  };
325 
327  static int Select(
328  PSocket & sock1,
329  PSocket & sock2
330  );
332  static int Select(
333  PSocket & sock1,
334  PSocket & sock2,
335  const PTimeInterval & timeout
336  );
338  static Errors Select(
339  SelectList & read
340  );
342  static Errors Select(
343  SelectList & read,
344  const PTimeInterval & timeout
345  );
347  static Errors Select(
348  SelectList & read,
349  SelectList & write
350  );
352  static Errors Select(
353  SelectList & read,
354  SelectList & write,
355  const PTimeInterval & timeout
356  );
358  static Errors Select(
359  SelectList & read,
360  SelectList & write,
361  SelectList & except
362  );
384  static Errors Select(
385  SelectList & read,
386  SelectList & write,
387  SelectList & except,
388  const PTimeInterval & timeout
389  );
391 
394  inline static WORD Host2Net(WORD v) { return htons(v); }
397  inline static DWORD Host2Net(DWORD v) { return htonl(v); }
398 
400  inline static WORD Net2Host(WORD v) { return ntohs(v); }
402  inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
404 
405 
410 #if _WIN32
411  struct Slice : public WSABUF
412  {
413  Slice()
414  { buf = (char *)0; len = 0; }
415 
416  Slice(void * v, const size_t len)
417  { SetBase(v); SetLength(len); }
418 
419  Slice(const void * v, const size_t len)
420  { SetBase(v); SetLength(len); }
421 
422  void SetBase(const void * v) { buf = (char *)v; }
423  void SetBase(void * v) { buf = (char *)v; }
424  void * GetBase() const { return buf; }
425 
426  void SetLength(size_t v) { len = (ULONG)v; }
427  size_t GetLength() const { return len; }
428  };
429 #else
430 #if P_HAS_RECVMSG
431  struct Slice : public iovec
432  {
433 #else
434  struct Slice
435  {
436  protected:
437  void * iov_base;
438  size_t iov_len;
439  public:
440 #endif
442  { SetBase(NULL); SetLength(0); }
443 
444  Slice(void * v, size_t len)
445  { SetBase(v); SetLength(len); }
446 
447  void SetBase(void * v) { iov_base = v; }
448  void * GetBase() const { return iov_base; }
449  void SetLength(size_t v) { iov_len = v; }
450  size_t GetLength() const { return iov_len; }
451  };
452 #endif
453 
463  virtual bool Read(
464  Slice * slices, // slices to read to
465  size_t sliceCount
466  );
467 
477  virtual bool Write(
478  const Slice * slices, // slices to write from
479  size_t sliceCount
480  );
482 
483  protected:
484  virtual PBoolean ConvertOSError(P_INT_PTR libcReturnValue, ErrorGroup group = LastGeneralError);
485 
486  /*This function calls os_socket() with the correct parameters for the
487  socket protocol type.
488  */
489  virtual PBoolean OpenSocket() = 0;
490 
493  virtual const char * GetProtocolName() const = 0;
494 
495 
496  int os_close();
497  int os_socket(int af, int type, int proto);
499  struct sockaddr * sin,
500  socklen_t size
501  );
502 
504  Slice * slices,
505  size_t sliceCount,
506  int flags,
507  struct sockaddr * from,
508  socklen_t * fromlen
509  );
510 
512  const Slice * slices,
513  size_t sliceCount,
514  int flags,
515  struct sockaddr * to,
516  socklen_t tolen
517  );
518 
520  PSocket & listener,
521  struct sockaddr * addr,
522  socklen_t * size
523  );
524 
525  virtual int os_errno() const;
526 
527 
528  // Member variables
530  WORD m_port;
531 
532 // Include platform dependent part of class
533 #ifdef _WIN32
534 #include "msos/ptlib/socket.h"
535 #else
536 #include "unix/ptlib/socket.h"
537 #endif
538 };
539 
540 
541 #endif // PTLIB_SOCKET_H
542 
543 
544 // End Of File ///////////////////////////////////////////////////////////////
Definition: socket.h:87
PBoolean os_connect(struct sockaddr *sin, socklen_t size)
int os_close()
static DWORD Host2Net(DWORD v)
Convert from host to network byte order.
Definition: socket.h:397
Slice(void *v, size_t len)
Definition: socket.h:444
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
PLIST(PSocketList, PSocket)
Error during other operation, eg Open()
Definition: channel.h:254
Slice()
Definition: socket.h:441
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:312
virtual PBoolean Shutdown(ShutdownValue option)
Close one or both of the data streams associated with a socket.
ShutdownValue
Definition: channel.h:589
void SetLength(size_t v)
Definition: socket.h:449
static PString GetNameByProtocol(WORD proto)
Get the name of the protocol number specified.
Errors
Normalised error codes.
Definition: channel.h:227
virtual PBoolean Connect(const PString &address)
Connect a socket to a remote host on the specified port number.
PBoolean os_accept(PSocket &listener, struct sockaddr *addr, socklen_t *size)
static DWORD Net2Host(DWORD v)
Convert from network to host byte order.
Definition: socket.h:402
virtual PString GetServiceByPort(WORD port) const
Get the service name from the port number.
PBoolean GetOption(int option, int &value, int level=SOL_SOCKET)
Get options on the socket.
Definition: socket.h:86
virtual int os_errno() const
PBoolean os_vwrite(const Slice *slices, size_t sliceCount, int flags, struct sockaddr *to, socklen_t tolen)
virtual PBoolean OpenSocket()=0
static WORD Host2Net(WORD v)
Convert from host to network byte order.
Definition: socket.h:395
Abstract class defining I/O channel semantics.
Definition: channel.h:103
Structure that defines a &quot;slice&quot; of memory to be written to.
Definition: socket.h:434
bool PBoolean
Definition: object.h:174
virtual WORD GetPortByService(const PString &service) const
Get the port number for the specified service name.
WORD m_port
Port to be used by the socket when opening the channel.
Definition: socket.h:530
intptr_t P_INT_PTR
Definition: object.h:2646
The character string class.
Definition: pstring.h:108
void * iov_base
Definition: socket.h:437
ErrorGroup
Error groups.
Definition: channel.h:251
SelectList()
Definition: socket.h:316
void operator-=(PSocket &sock)
Remove a socket from list .
Definition: socket.h:322
PString GetService() const
Get a service name for the port number the TCP socket channel object instance is using.
void * GetBase() const
Definition: socket.h:448
virtual bool Read(Slice *slices, size_t sliceCount)
Low level scattered read from the channel.
PBoolean os_vread(Slice *slices, size_t sliceCount, int flags, struct sockaddr *from, socklen_t *fromlen)
void operator+=(PSocket &sock)
Add a socket to list .
Definition: socket.h:319
virtual bool Write(const Slice *slices, size_t sliceCount)
Low level scattered write to the channel.
size_t iov_len
Definition: socket.h:438
virtual const char * GetProtocolName() const =0
This function returns the protocol name for the socket type.
void SetBase(void *v)
Definition: socket.h:447
A network communications channel.
Definition: socket.h:59
static WORD Net2Host(WORD v)
Convert from network to host byte order.
Definition: socket.h:400
size_t GetLength() const
Definition: socket.h:450
virtual PBoolean ConvertOSError(P_INT_PTR libcReturnValue, ErrorGroup group=LastGeneralError)
Convert an operating system error into platform independent error.
Reusability
Flags to reuse of port numbers in Listen() function.
Definition: socket.h:85
PBoolean SetOption(int option, int value, int level=SOL_SOCKET)
Set options on the socket.
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)
WORD GetPort() const
Get the port the TCP socket channel object instance is using.