PTLib  Version 2.10.4
 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: 26520 $
27  * $Author: rjongbloed $
28  * $Date: 2011-10-04 23:54:38 -0500 (Tue, 04 Oct 2011) $
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;
162 
163  PIPSocket::InterfaceTable m_interfaces;
165 
170 
173 
175 };
176 
177 
179 
186 {
188  public:
189  enum {
191  };
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 {
243  PCLASSINFO(PInterfaceFilter, 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,
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 
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 
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 
371  const void * buf,
372  PINDEX len,
373  const PIPSocket::Address & addr,
374  WORD port,
375  const SocketInfo & info,
376  PINDEX & lastWriteCount
377  );
379  SocketInfo & info,
380  void * buf,
381  PINDEX len,
382  PIPSocket::Address & addr,
383  WORD & port,
384  PINDEX & lastReadCount,
385  const PTimeInterval & timeout
386  );
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 {
416  PCLASSINFO(PMonitoredSocketChannel, PChannel);
417  public:
420 
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 
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 
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 PBoolean Open(
545  WORD port
546  );
547 
549  virtual PBoolean Close();
550 
552  virtual PBoolean GetAddress(
553  const PString & iface,
554  PIPSocket::Address & address,
555  WORD & port,
556  PBoolean usingNAT
557  ) const;
558 
565  const void * buf,
566  PINDEX len,
567  const PIPSocket::Address & addr,
568  WORD port,
569  const PString & iface,
570  PINDEX & lastWriteCount
571  );
572 
580  void * buf,
581  PINDEX len,
582  PIPSocket::Address & addr,
583  WORD & port,
584  PString & iface,
585  PINDEX & lastReadCount,
586  const PTimeInterval & timeout
587  );
588 
589  protected:
591  virtual void OnAddInterface(const InterfaceEntry & entry);
592 
594  virtual void OnRemoveInterface(const InterfaceEntry & entry);
595 
596  typedef std::map<std::string, SocketInfo> SocketInfoMap_T;
597 
598  void OpenSocket(const PString & iface);
599  void CloseSocket(SocketInfoMap_T::iterator iterSocket);
600 
602 };
603 
604 
606 
612 {
614  public:
616  const PString & theInterface,
617  bool reuseAddr = false,
618  PNatMethod * natMethod = NULL
619  );
621 
626  virtual PStringArray GetInterfaces(
627  bool includeLoopBack = false,
628  const PIPSocket::Address & destination = PIPSocket::GetDefaultIpAny()
630  );
631 
638  virtual PBoolean Open(
639  WORD port
640  );
641 
643  virtual PBoolean Close();
644 
646  virtual PBoolean GetAddress(
647  const PString & iface,
648  PIPSocket::Address & address,
649  WORD & port,
650  PBoolean usingNAT
651  ) const;
652 
659  const void * buf,
660  PINDEX len,
661  const PIPSocket::Address & addr,
662  WORD port,
663  const PString & iface,
664  PINDEX & lastWriteCount
665  );
666 
674  void * buf,
675  PINDEX len,
676  PIPSocket::Address & addr,
677  WORD & port,
678  PString & iface,
679  PINDEX & lastReadCount,
680  const PTimeInterval & timeout
681  );
682 
683 
684  protected:
686  virtual void OnAddInterface(const InterfaceEntry & entry);
687 
689  virtual void OnRemoveInterface(const InterfaceEntry & entry);
690 
691  bool IsInterface(const PString & iface) const;
692 
696 };
697 
698 
699 #endif // PTLIB_PSOCKBUN_H
700 
701 
702 // End Of File ///////////////////////////////////////////////////////////////