PTLib  Version 2.10.11
psockbun.h
Go to the documentation of this file.
1 /*
2  * psockbun.h
3  *
4  * Socket and interface bundle code
5  *
6  * Portable Windows Library
7  *
8  * Copyright (C) 2007 Post Increment
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 Post Increment
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 28152 $
27  * $Author: rjongbloed $
28  * $Date: 2012-08-06 22:03:48 -0500 (Mon, 06 Aug 2012) $
29  */
30 
31 #ifndef PTLIB_PSOCKBUN_H
32 #define PTLIB_PSOCKBUN_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 
39 #include <ptlib.h>
40 #include <ptlib/ipsock.h>
41 #include <ptlib/sockets.h>
42 #include <ptlib/safecoll.h>
43 #include <list>
44 
45 
46 class PNatMethod;
48 class PInterfaceFilter;
49 
50 
51 #define PINTERFACE_MONITOR_FACTORY_NAME "InterfaceMonitor"
52 
53 
55 
65 {
67  public:
68  enum {
70  };
71 
73  unsigned refreshInterval = DefaultRefreshInterval,
74  bool runMonitorThread = true
75  );
76  virtual ~PInterfaceMonitor();
77 
80 
82  void SetRefreshInterval (unsigned refresh);
83 
85  void SetRunMonitorThread (bool runMonitorThread);
86 
92  void Start();
93 
95  void Stop();
96 
98 
104  bool includeLoopBack = false,
105  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
107  );
108 
115  const PIPSocket::Address & binding,
116  const PIPSocket::Address & destination
117  );
118 
123  bool GetInterfaceInfo(
124  const PString & iface,
125  InterfaceEntry & info
126  ) const;
127 
132  static bool IsMatchingInterface(
133  const PString & iface,
134  const InterfaceEntry & entry
135  );
136 
140  void SetInterfaceFilter(PInterfaceFilter * filter);
141  bool HasInterfaceFilter() const { return m_interfaceFilter != NULL; }
142 
143  virtual void RefreshInterfaceList();
144 
145  void OnRemoveNatMethod(const PNatMethod * natMethod);
146 
147  protected:
148  virtual void OnShutdown();
149 
150  void UpdateThreadMain();
151 
154 
155  virtual void OnInterfacesChanged(const PIPSocket::InterfaceTable & addedInterfaces, const PIPSocket::InterfaceTable & removedInterfaces);
156 
158 
159  typedef std::list<PInterfaceMonitorClient *> ClientList_T;
160  ClientList_T m_clients;
162 
163  PIPSocket::InterfaceTable m_interfaces;
165 
170 
173 
175 };
176 
177 
179 
186 {
188  public:
189  enum {
190  DefaultPriority = 50,
191  };
192  PInterfaceMonitorClient(PINDEX priority = DefaultPriority);
194 
196 
203  virtual PStringArray GetInterfaces(
204  bool includeLoopBack = false,
205  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
207  );
208 
213  virtual PBoolean GetInterfaceInfo(
214  const PString & iface,
215  InterfaceEntry & info
216  ) const;
217 
222  PINDEX GetPriority() const { return priority; }
223 
224  protected:
226  virtual void OnAddInterface(const InterfaceEntry & entry) = 0;
227 
229  virtual void OnRemoveInterface(const InterfaceEntry & entry) = 0;
230 
232  virtual void OnRemoveNatMethod(const PNatMethod * /*natMethod*/) { }
233 
234  PINDEX priority;
235 
236  friend class PInterfaceMonitor;
237 };
238 
239 
241 
242 class PInterfaceFilter : public PObject {
244 
245  public:
246  virtual PIPSocket::InterfaceTable FilterInterfaces(const PIPSocket::Address & destination,
247  PIPSocket::InterfaceTable & interfaces) const = 0;
248 };
249 
250 
252 
259 {
261  protected:
263  bool reuseAddr,
264  PNatMethod * natMethod
265  );
266 
267  public:
274  virtual PBoolean Open(
275  WORD port
276  ) = 0;
277 
279  PBoolean IsOpen() const { return opened; }
280 
282  virtual PBoolean Close() = 0;
283 
285  WORD GetPort() const { return localPort; }
286 
288  virtual PBoolean GetAddress(
289  const PString & iface,
290  PIPSocket::Address & address,
291  WORD & port,
292  PBoolean usingNAT
293  ) const = 0;
294 
300  virtual PChannel::Errors WriteToBundle(
301  const void * buffer,
302  PINDEX length,
303  const PIPSocket::Address & addr,
304  WORD port,
305  const PString & iface,
306  PINDEX & lastWriteCount
307  ) = 0;
308 
315  virtual PChannel::Errors ReadFromBundle(
316  void * buffer,
317  PINDEX length,
318  PIPSocket::Address & addr,
319  WORD & port,
320  PString & iface,
321  PINDEX & lastReadCount,
322  const PTimeInterval & timeout
323  ) = 0;
324 
327  PNatMethod * method
328  ) { natMethod = method; }
329 
330 
331  // Get the current NAT method, eg STUN client pointer
332  PNatMethod * GetNatMethod() const { return natMethod; }
333 
338  static PMonitoredSockets * Create(
339  const PString & iface,
340  bool reuseAddr = false,
341  PNatMethod * natMethod = NULL
342  );
343 
344  protected:
345  virtual void OnRemoveNatMethod(
346  const PNatMethod * natMethod
347  );
348 
349  struct SocketInfo {
351  : socket(NULL)
352  , inUse(false)
353  { }
355  bool inUse;
356  };
357 
358  bool CreateSocket(
359  SocketInfo & info,
360  const PIPSocket::Address & binding
361  );
362  bool DestroySocket(SocketInfo & info);
363  bool GetSocketAddress(
364  const SocketInfo & info,
365  PIPSocket::Address & address,
366  WORD & port,
367  bool usingNAT
368  ) const;
369 
370  PChannel::Errors WriteToSocket(
371  const void * buf,
372  PINDEX len,
373  const PIPSocket::Address & addr,
374  WORD port,
375  const SocketInfo & info,
376  PINDEX & lastWriteCount
377  );
378  PChannel::Errors ReadFromSocket(
379  SocketInfo & info,
380  void * buf,
381  PINDEX len,
382  PIPSocket::Address & addr,
383  WORD & port,
384  PINDEX & lastReadCount,
385  const PTimeInterval & timeout
386  );
387  PChannel::Errors ReadFromSocket(
388  PSocket::SelectList & readers,
389  PUDPSocket * & socket,
390  void * buf,
391  PINDEX len,
392  PIPSocket::Address & addr,
393  WORD & port,
394  PINDEX & lastReadCount,
395  const PTimeInterval & timeout
396  );
397 
398  WORD localPort;
401 
402  bool opened;
404 };
405 
407 
408 
410 
415 {
417  public:
422  const PMonitoredSocketsPtr & sockets,
423  bool shared
424  );
426 
429  virtual PBoolean IsOpen() const;
430  virtual PBoolean Close();
431 
434  virtual PBoolean Read(
435  void * buffer,
436  PINDEX length
437  );
438 
441  virtual PBoolean Write(
442  const void * buffer,
443  PINDEX length
444  );
446 
452  void SetInterface(
453  const PString & iface
454  );
455 
457  PString GetInterface();
458 
461  bool GetLocal(
462  PIPSocket::Address & address,
463  WORD & port,
464  bool usingNAT
465  );
466 
468  void SetRemote(
469  const PIPSocket::Address & address,
470  WORD port
471  );
472 
474  void SetRemote(
475  const PString & hostAndPort
476  );
477 
479  void GetRemote(
480  PIPSocket::Address & addr,
481  WORD & port
482  ) const { addr = remoteAddress; port = remotePort; }
483 
489  bool flag
490  ) { promiscuousReads = flag; }
491 
493  bool GetPromiscuous() { return promiscuousReads; }
494 
497  PIPSocket::Address & addr,
498  WORD & port
499  ) const { addr = lastReceivedAddress; port = lastReceivedPort; }
500 
502  PString GetLastReceivedInterface() const { return lastReceivedInterface; }
503 
505  const PMonitoredSocketsPtr & GetMonitoredSockets() const { return socketBundle; }
507 
508  protected:
509  PMonitoredSocketsPtr socketBundle;
514  bool closing;
520 };
521 
522 
524 
529 {
531  public:
533  bool reuseAddr = false,
534  PNatMethod * natMethod = NULL
535  );
537 
544  virtual PStringArray GetInterfaces(
545  bool includeLoopBack = false,
546  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
548  );
549 
556  virtual PBoolean Open(
557  WORD port
558  );
559 
561  virtual PBoolean Close();
562 
564  virtual PBoolean GetAddress(
565  const PString & iface,
566  PIPSocket::Address & address,
567  WORD & port,
568  PBoolean usingNAT
569  ) const;
570 
576  virtual PChannel::Errors WriteToBundle(
577  const void * buf,
578  PINDEX len,
579  const PIPSocket::Address & addr,
580  WORD port,
581  const PString & iface,
582  PINDEX & lastWriteCount
583  );
584 
591  virtual PChannel::Errors ReadFromBundle(
592  void * buf,
593  PINDEX len,
594  PIPSocket::Address & addr,
595  WORD & port,
596  PString & iface,
597  PINDEX & lastReadCount,
598  const PTimeInterval & timeout
599  );
600 
601  protected:
603  virtual void OnAddInterface(const InterfaceEntry & entry);
604 
606  virtual void OnRemoveInterface(const InterfaceEntry & entry);
607 
608  typedef std::map<std::string, SocketInfo> SocketInfoMap_T;
609 
610  void OpenSocket(const PString & iface);
611  void CloseSocket(SocketInfoMap_T::iterator iterSocket);
612 
613  SocketInfoMap_T socketInfoMap;
614 };
615 
616 
618 
624 {
626  public:
628  const PString & theInterface,
629  bool reuseAddr = false,
630  PNatMethod * natMethod = NULL
631  );
633 
638  virtual PStringArray GetInterfaces(
639  bool includeLoopBack = false,
640  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
642  );
643 
650  virtual PBoolean Open(
651  WORD port
652  );
653 
655  virtual PBoolean Close();
656 
658  virtual PBoolean GetAddress(
659  const PString & iface,
660  PIPSocket::Address & address,
661  WORD & port,
662  PBoolean usingNAT
663  ) const;
664 
670  virtual PChannel::Errors WriteToBundle(
671  const void * buf,
672  PINDEX len,
673  const PIPSocket::Address & addr,
674  WORD port,
675  const PString & iface,
676  PINDEX & lastWriteCount
677  );
678 
685  virtual PChannel::Errors ReadFromBundle(
686  void * buf,
687  PINDEX len,
688  PIPSocket::Address & addr,
689  WORD & port,
690  PString & iface,
691  PINDEX & lastReadCount,
692  const PTimeInterval & timeout
693  );
694 
695 
696  protected:
698  virtual void OnAddInterface(const InterfaceEntry & entry);
699 
701  virtual void OnRemoveInterface(const InterfaceEntry & entry);
702 
703  bool IsInterface(const PString & iface) const;
704 
706  InterfaceEntry theEntry;
708 };
709 
710 
711 #endif // PTLIB_PSOCKBUN_H
712 
713 
714 // End Of File ///////////////////////////////////////////////////////////////
Class for detector of Route Table changes.
Definition: ipsock.h:620
bool inUse
Definition: psockbun.h:355
friend class PInterfaceMonitorClient
Definition: psockbun.h:174
PIPSocket::Address lastReceivedAddress
Definition: psockbun.h:516
SocketInfoMap_T socketInfoMap
Definition: psockbun.h:613
bool reuseAddress
Definition: psockbun.h:399
PINDEX priority
Definition: psockbun.h:234
std::list< PInterfaceMonitorClient * > ClientList_T
Definition: psockbun.h:159
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PInterfaceMonitor(unsigned refreshInterval=DefaultRefreshInterval, bool runMonitorThread=true)
bool promiscuousReads
Definition: psockbun.h:512
static bool IsMatchingInterface(const PString &iface, const InterfaceEntry &entry)
Returns whether the descriptor string equals the interface entry.
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
bool closing
Definition: psockbun.h:514
PIPSocket::InterfaceEntry InterfaceEntry
Definition: psockbun.h:195
This is a base class for UDP socket(s) that are monitored for interface changes.
Definition: psockbun.h:258
void GetLastReceived(PIPSocket::Address &addr, WORD &port) const
Get the IP address and port of the last received UDP data.
Definition: psockbun.h:496
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
Describe an interface table entry.
Definition: ipsock.h:642
virtual ~PInterfaceMonitor()
virtual void RefreshInterfaceList()
virtual void OnInterfacesChanged(const PIPSocket::InterfaceTable &addedInterfaces, const PIPSocket::InterfaceTable &removedInterfaces)
PMutex m_threadMutex
Definition: psockbun.h:168
void GetRemote(PIPSocket::Address &addr, WORD &port) const
Get the current remote address/port for all Write() functions.
Definition: psockbun.h:479
PINDEX GetPriority() const
Returns the priority of this client.
Definition: psockbun.h:222
virtual void OnRemoveNatMethod(const PNatMethod *)
Called when a NAT method is about to be destroyed.
Definition: psockbun.h:232
Definition: psockbun.h:349
List of sockets used for Select() function.
Definition: socket.h:311
bool IsValidBindingForDestination(const PIPSocket::Address &binding, const PIPSocket::Address &destination)
Returns whether destination is reachable through binding or not.
WORD remotePort
Definition: psockbun.h:515
PIPSocket::InterfaceEntry InterfaceEntry
Definition: psockbun.h:97
This is an array collection class of PString objects.
Definition: pstring.h:2024
Definition: psockbun.h:242
PString lastReceivedInterface
Definition: psockbun.h:518
ClientList_T m_clients
Definition: psockbun.h:160
PSafePtr< PMonitoredSockets > PMonitoredSocketsPtr
Definition: psockbun.h:406
PSmartPtr< PInterfaceMonitorClient > ClientPtr
Definition: psockbun.h:157
Errors
Normalised error codes.
Definition: channel.h:529
BOOL PBoolean
Definition: object.h:102
PNatMethod * GetNatMethod() const
Definition: psockbun.h:332
void SetRunMonitorThread(bool runMonitorThread)
Change whether the monitor thread should run.
void SetNatMethod(PNatMethod *method)
Set the NAT method, eg STUN client pointer.
Definition: psockbun.h:326
This class can be used to access the bundled/monitored UDP sockets using the PChannel API...
Definition: psockbun.h:414
PIPSocket::Address remoteAddress
Definition: psockbun.h:513
PUDPSocket interfaceAddedSignal
Definition: psockbun.h:403
PNatMethod Base Network Address Traversal Method class All NAT Traversal Methods are derived off this...
Definition: pnat.h:49
virtual void OnShutdown()
PBoolean IsOpen() const
Indicate if the socket(s) are open and ready for reads/writes.
Definition: psockbun.h:279
PString theInterface
Definition: psockbun.h:705
PMutex mutex
Definition: psockbun.h:519
This concrete class bundles a set of UDP sockets which are dynamically adjusted as interfaces are add...
Definition: psockbun.h:528
Abstract class defining I/O channel semantics.
Definition: channel.h:107
This class defines a thread-safe object in a collection.
Definition: safecoll.h:119
bool GetInterfaceInfo(const PString &iface, InterfaceEntry &info) const
Return information about an active interface given the descriptor string.
The character string class.
Definition: pstring.h:108
void OnRemoveNatMethod(const PNatMethod *natMethod)
This is a base class for clients of the PInterfaceMonitor singleton object.
Definition: psockbun.h:185
PMutex m_interfacesMutex
Definition: psockbun.h:164
This concrete class monitors a single scoket bound to a specific interface or address.
Definition: psockbun.h:623
This class is a singleton that will monitor the network interfaces on a machine and update a list aof...
Definition: psockbun.h:64
PString currentInterface
Definition: psockbun.h:511
PThread * m_updateThread
Definition: psockbun.h:169
WORD GetPort() const
Return the local port number being used by the socket(s)
Definition: psockbun.h:285
PString GetLastReceivedInterface() const
Get the interface the last received UDP data was recieved on.
Definition: psockbun.h:502
This template class creates a type safe version of PSmartPointer.
Definition: smartptr.h:174
This class defines a thread of execution in the system.
Definition: thread.h:66
PIPSocket::RouteTableDetector * m_changedDetector
Definition: psockbun.h:172
static PIPSocket::Address GetDefaultIpAny()
PMutex m_clientsMutex
Definition: psockbun.h:161
bool HasInterfaceFilter() const
Definition: psockbun.h:141
PTimeInterval m_refreshInterval
Definition: psockbun.h:167
void SetRefreshInterval(unsigned refresh)
Change the refresh interval.
Template class for generic factories of an abstract class.
Definition: pfactory.h:144
A class describing an IP address.
Definition: ipsock.h:75
bool sharedBundle
Definition: psockbun.h:510
PStringArray GetInterfaces(bool includeLoopBack=false, const PIPSocket::Address &destination=PIPSocket::GetDefaultIpAny())
Get an array of all current interface descriptors, possibly including the loopback (127...
PIPSocket::InterfaceTable m_interfaces
Definition: psockbun.h:163
Definition: pprocess.h:779
InterfaceEntry theEntry
Definition: psockbun.h:706
void Stop()
Stop monitoring network interfaces.
PFACTORY_GET_SINGLETON(PProcessStartupFactory, PInterfaceMonitor)
Return the singleton interface for the network monitor.
WORD lastReceivedPort
Definition: psockbun.h:517
void SetPromiscuous(bool flag)
Set flag for receiving UDP data from any remote address.
Definition: psockbun.h:488
void Start()
Start monitoring network interfaces.
Synonym for PTimedMutex.
bool opened
Definition: psockbun.h:402
void AddClient(PInterfaceMonitorClient *)
void RemoveClient(PInterfaceMonitorClient *)
void SetInterfaceFilter(PInterfaceFilter *filter)
Sets the monitor&#39;s interface filter.
std::map< std::string, SocketInfo > SocketInfoMap_T
Definition: psockbun.h:608
PNatMethod * natMethod
Definition: psockbun.h:400
const PMonitoredSocketsPtr & GetMonitoredSockets() const
Get the monitored socket bundle being used by this channel.
Definition: psockbun.h:505
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
bool GetPromiscuous()
Get flag for receiving UDP data from any remote address.
Definition: psockbun.h:493
SocketInfo theInfo
Definition: psockbun.h:707
SocketInfo()
Definition: psockbun.h:350
PInterfaceFilter * m_interfaceFilter
Definition: psockbun.h:171
PMonitoredSocketsPtr socketBundle
Definition: psockbun.h:509
WORD localPort
Definition: psockbun.h:398
bool m_runMonitorThread
Definition: psockbun.h:166
PUDPSocket * socket
Definition: psockbun.h:354