PTLib  Version 2.10.10
 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: 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;
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 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 
577  const void * buf,
578  PINDEX len,
579  const PIPSocket::Address & addr,
580  WORD port,
581  const PString & iface,
582  PINDEX & lastWriteCount
583  );
584 
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 
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 
671  const void * buf,
672  PINDEX len,
673  const PIPSocket::Address & addr,
674  WORD port,
675  const PString & iface,
676  PINDEX & lastWriteCount
677  );
678 
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 
708 };
709 
710 
711 #endif // PTLIB_PSOCKBUN_H
712 
713 
714 // End Of File ///////////////////////////////////////////////////////////////