PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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: 32091 $
27  * $Author: rjongbloed $
28  * $Date: 2014-06-06 20:45:36 +1000 (Fri, 06 Jun 2014) $
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 <ptclib/pnat.h>
44 #include <map>
45 
46 
47 #define PINTERFACE_MONITOR_FACTORY_NAME "InterfaceMonitor"
48 
49 
51 
52 class PInterfaceFilter : public PObject {
53  PCLASSINFO(PInterfaceFilter, PObject);
54 
55  public:
56  virtual PIPSocket::InterfaceTable FilterInterfaces(const PIPSocket::Address & destination,
57  PIPSocket::InterfaceTable & interfaces) const = 0;
58 };
59 
60 
62 
72 {
74  public:
75  enum {
77  };
78 
80  unsigned refreshInterval = DefaultRefreshInterval,
81  bool runMonitorThread = true
82  );
83  virtual ~PInterfaceMonitor();
84 
87 
89  void SetRefreshInterval (unsigned refresh);
90 
92  void SetRunMonitorThread (bool runMonitorThread);
93 
99  void Start();
100 
102  void Stop();
103 
105 
111  bool includeLoopBack = false,
112  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
114  );
115 
122  const PIPSocket::Address & binding,
123  const PIPSocket::Address & destination
124  );
125 
130  bool GetInterfaceInfo(
131  const PString & iface,
132  InterfaceEntry & info
133  ) const;
134 
139  static bool IsMatchingInterface(
140  const PString & iface,
141  const InterfaceEntry & entry
142  );
143 
147  {
148  InterfaceChange(const InterfaceEntry & entry, bool added)
149  : InterfaceEntry(entry)
150  , m_added(added)
151  { }
152 
153  const bool m_added;
154  };
155 
158 
160  #define PDECLARE_InterfaceNotifier(cls, fn) PDECLARE_NOTIFIER2(PInterfaceMonitor, cls, fn, PInterfaceMonitor::InterfaceChange)
161 
163  #define PCREATE_InterfaceNotifier(fn) PCREATE_NOTIFIER2(fn, PInterfaceMonitor::InterfaceChange)
164 
165  enum {
167  };
168 
171  void AddNotifier(
172  const Notifier & notifier,
173  unsigned priority = DefaultPriority
174  );
175  void RemoveNotifier(
176  const Notifier & notifier
177  );
178 
182  void SetInterfaceFilter(PInterfaceFilter * filter);
183  bool HasInterfaceFilter() const { return m_interfaceFilter != NULL; }
184 
185  protected:
186  virtual void OnShutdown();
187 
188  void UpdateThreadMain();
189 
190  virtual void RefreshInterfaceList();
191  virtual void OnInterfacesChanged(const PIPSocket::InterfaceTable & addedInterfaces, const PIPSocket::InterfaceTable & removedInterfaces);
192 
193  typedef std::multimap<unsigned, Notifier> Notifiers;
196 
197  PIPSocket::InterfaceTable m_interfaces;
199 
204 
207 
209 };
210 
211 
213 
220 {
221  PCLASSINFO(PMonitoredSockets, PSafeObject);
222  protected:
224  bool reuseAddr
225  P_NAT_PARAM(PNatMethods * natMethods)
226  );
227 
228  public:
230 
237  virtual PBoolean Open(
238  WORD port
239  ) = 0;
240 
242  PBoolean IsOpen() const { return m_opened; }
243 
245  virtual PBoolean Close() = 0;
246 
248  WORD GetPort() const { return m_localPort; }
249 
251  virtual PBoolean GetAddress(
252  const PString & iface,
253  PIPSocket::Address & address,
254  WORD & port,
255  PBoolean usingNAT
256  ) const = 0;
257 
258  struct BundleParams {
260  : m_buffer(NULL)
261  , m_length(0)
262  , m_addr(0)
263  , m_port(0)
264  , m_lastCount(0)
265  , m_errorCode(PChannel::NoError)
266  , m_errorNumber(0)
267  { }
268 
269  void * m_buffer;
270  PINDEX m_length;
272  WORD m_port;
274  PINDEX m_lastCount;
278  };
279 
285  virtual void WriteToBundle(
286  BundleParams & param
287  ) = 0;
288 
295  virtual void ReadFromBundle(
296  BundleParams & param
297  ) = 0;
298 
305  virtual PStringArray GetInterfaces(
306  bool includeLoopBack = false,
307  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
309  );
310 
315  virtual bool GetInterfaceInfo(
316  const PString & iface,
317  InterfaceEntry & info
318  ) const;
319 
336  static PMonitoredSockets * Create(
337  const PString & iface,
338  bool reuseAddr = false
339  P_NAT_PARAM(PNatMethods * natMethods = NULL)
340  );
341 
342  protected:
343  struct SocketInfo {
345  : socket(NULL)
346  , inUse(false)
347  { }
348  void Read(PMonitoredSockets & bundle, BundleParams & param);
349  void Write(BundleParams & param);
350 
352  bool inUse;
353  };
354  friend struct SocketInfo;
355 
356  bool CreateSocket(
357  SocketInfo & info,
358  const PIPSocket::Address & binding
359  );
360  bool DestroySocket(SocketInfo & info);
361  bool GetSocketAddress(
362  const SocketInfo & info,
363  PIPSocket::Address & address,
364  WORD & port,
365  bool usingNAT
366  ) const;
367 
368  void ReadFromSocketList(
369  PSocket::SelectList & readers,
370  PUDPSocket * & socket,
371  BundleParams & param
372  );
373 
376 #if P_NAT
377  PNatMethods * m_natMethods;
378 #endif
379 
380  bool m_opened;
382 };
383 
385 
386 
388 
393 {
394  PCLASSINFO(PMonitoredSocketChannel, PChannel);
395  public:
398 
400  const PMonitoredSocketsPtr & sockets,
401  bool shared
402  );
404 
407  virtual PBoolean IsOpen() const;
408  virtual PBoolean Close();
409 
412  virtual PBoolean Read(
413  void * buffer,
414  PINDEX length
415  );
416 
419  virtual PBoolean Write(
420  const void * buffer,
421  PINDEX length
422  );
424 
430  void SetInterface(
431  const PString & iface
432  );
433 
436 
439  bool GetLocal(
440  PIPSocket::Address & address,
441  WORD & port,
442  bool usingNAT
443  );
444  bool GetLocal(
446  bool usingNAT
447  );
448 
450  void SetRemote(
451  const PIPSocket::Address & address,
452  WORD port
453  ) { m_remoteAP.SetAddress(address, port); }
454  void SetRemote(
455  const PIPSocket::AddressAndPort & ap
456  ) { m_remoteAP = ap; }
457 
459  void SetRemote(
460  const PString & hostAndPort
461  );
462 
464  void GetRemote(
465  PIPSocket::Address & addr,
466  WORD & port
467  ) const { addr = m_remoteAP.GetAddress(); port = m_remoteAP.GetPort(); }
468  void GetRemote(
470  ) const { ap = m_remoteAP; }
471 
477  bool flag
478  ) { m_promiscuousReads = flag; }
479 
482 
485  PIPSocket::Address & addr,
486  WORD & port
487  ) const { addr = m_lastReceivedAP.GetAddress(); port = m_lastReceivedAP.GetPort(); }
490  ) const { ap = m_lastReceivedAP; }
491 
494 
496  const PMonitoredSocketsPtr & GetMonitoredSockets() const { return m_socketBundle; }
498 
499  protected:
500  PMonitoredSocketsPtr m_socketBundle;
504  bool m_closing;
509 };
510 
511 
513 
518 {
520  public:
522  const PString & fixedInterface,
523  unsigned ipVersion,
524  bool reuseAddr
525  P_NAT_PARAM(PNatMethods * natMethods = NULL)
526  );
528 
535  virtual PStringArray GetInterfaces(
536  bool includeLoopBack = false,
537  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
539  );
540 
547  virtual PBoolean Open(
548  WORD port
549  );
550 
552  virtual PBoolean Close();
553 
555  virtual PBoolean GetAddress(
556  const PString & iface,
557  PIPSocket::Address & address,
558  WORD & port,
559  PBoolean usingNAT
560  ) const;
561 
567  virtual void WriteToBundle(
568  BundleParams & param
569  );
570 
577  virtual void ReadFromBundle(
578  BundleParams & param
579  );
580 
581  protected:
584 
585  typedef std::map<std::string, SocketInfo> SocketInfoMap_T;
586 
587  void OpenSocket(const PString & iface);
588  void CloseSocket(SocketInfoMap_T::iterator iterSocket);
589 
592  unsigned m_ipVersion;
593 };
594 
595 
597 
603 {
605  public:
607  const PString & theInterface,
608  bool reuseAddr
609  P_NAT_PARAM(PNatMethods * natMethods = NULL)
610  );
612 
617  virtual PStringArray GetInterfaces(
618  bool includeLoopBack = false,
619  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
621  );
622 
629  virtual PBoolean Open(
630  WORD port
631  );
632 
634  virtual PBoolean Close();
635 
637  virtual PBoolean GetAddress(
638  const PString & iface,
639  PIPSocket::Address & address,
640  WORD & port,
641  PBoolean usingNAT
642  ) const;
643 
649  virtual void WriteToBundle(
650  BundleParams & param
651  );
652 
659  virtual void ReadFromBundle(
660  BundleParams & param
661  );
662 
663 
664  protected:
667 
668  bool IsInterface(const PString & iface) const;
669 
673 };
674 
675 
676 #endif // PTLIB_PSOCKBUN_H
677 
678 
679 // End Of File ///////////////////////////////////////////////////////////////