libDwm-0.6.0
DwmHostPinger.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmHostPinger.hh 8499 $
3 // @(#) $Id: DwmHostPinger.hh 8499 2016-05-09 00:59:08Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2007, 2016
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMHOSTPINGER_HH_
43 #define _DWMHOSTPINGER_HH_
44 
45 #include <condition_variable>
46 #include <iostream>
47 #include <map>
48 #include <mutex>
49 #include <thread>
50 
51 #include "DwmIpv4Address.hh"
52 #include "DwmPcap.hh"
53 #include "DwmSocket.hh"
54 #include "DwmTimeValue.hh"
55 
57 
58 namespace Dwm {
59 
60  //--------------------------------------------------------------------------
63  //--------------------------------------------------------------------------
64  class HostPinger
65  {
66  public:
67  //------------------------------------------------------------------------
74  //------------------------------------------------------------------------
75  class Receiver
76  {
77  public:
78  //----------------------------------------------------------------------
80  //----------------------------------------------------------------------
81  virtual void operator () (const PingDestination & destination,
82  const TimeValue & recvTime,
83  const TimeValue & rtt) = 0;
84  };
85 
86  //------------------------------------------------------------------------
88  //------------------------------------------------------------------------
89  HostPinger(uint16_t identifier, const std::string & device = "lo0");
90 
91  //------------------------------------------------------------------------
93  //------------------------------------------------------------------------
94  static Receiver *GetReceiver();
95 
96  //------------------------------------------------------------------------
98  //------------------------------------------------------------------------
99  static Receiver *SetReceiver(Receiver *receiver);
100 
101  //------------------------------------------------------------------------
103  //------------------------------------------------------------------------
104  bool AddDestination(const PingDestination & destination);
105 
106  //------------------------------------------------------------------------
110  //------------------------------------------------------------------------
111  uint16_t PacketRate() const;
112 
113  //------------------------------------------------------------------------
117  //------------------------------------------------------------------------
118  uint16_t PacketRate(uint16_t pps);
119 
120  //------------------------------------------------------------------------
122  //------------------------------------------------------------------------
123  uint32_t PacketsSent(const PingDestination & destination) const;
124 
125  //------------------------------------------------------------------------
128  //------------------------------------------------------------------------
129  uint32_t PacketsReceived(const PingDestination & destination) const;
130 
131  //------------------------------------------------------------------------
133  //------------------------------------------------------------------------
134  float PacketLoss(const PingDestination & destination) const;
135 
136  //------------------------------------------------------------------------
138  //------------------------------------------------------------------------
139  bool Start(int count = 0);
140 
141  //------------------------------------------------------------------------
143  //------------------------------------------------------------------------
144  bool Stop();
145 
146  //------------------------------------------------------------------------
149  //------------------------------------------------------------------------
150  bool Outstanding() const;
151 
152  protected:
153  typedef std::map<PingDestination,PingDestinationEntry> DestinationMap;
154 
155  std::string _device;
156  Ipv4Address _myAddress;
157  std::thread _watcherThread;
158  std::thread _senderThread;
159  std::mutex _watcherReadyMutex;
160  std::unique_lock<std::mutex> _watcherReadyLock;
161  std::condition_variable _watcherReady;
162  bool _runSender;
163  bool _runWatcher;
164  uint16_t _identifier;
165  Socket _socket;
166  uint16_t _packetRate;
167  Pcap _pcap;
168  int _count;
169  bool _forever;
170  DestinationMap _destinations;
171  static Receiver *_receiver;
172 
173  bool StopWatcher();
174  bool StopSender();
175  void GetTcpPacket(const PingDestination & destination,
176  uint16_t seq, std::string & s);
177 
178  static void PcapCallback(uint8_t *user, const struct pcap_pkthdr *pchdr,
179  const uint8_t *pkt);
180  void RunWatcher();
181  void RunSender();
182  };
183 
184 } // namespace Dwm
185 
186 #endif // _DWMHOSTPINGER_HH_
This class abstracts a UNIX socket descriptor.
Definition: DwmSocket.hh:62
Dwm::PingDestinationEntry class definition.
This class encapsulates a time value beyond the UNIX epoch, with microsecond granularity.
Definition: DwmTimeValue.hh:72
float PacketLoss(const PingDestination &destination) const
Returns the percent packet loss for the given destination.
Encapsulates a &#39;pinger&#39;, which can &#39;ping&#39; one or more hosts using TCP or ICMP.
Definition: DwmHostPinger.hh:64
This class encapsulates an IPv4 address.
Definition: DwmIpv4Address.hh:62
bool Start(int count=0)
Starts the pinger. Returns true on success.
uint32_t PacketsSent(const PingDestination &destination) const
Returns the number of packets sent to the given destination.
Dwm::Socket class definition.
Dwm::Pcap class definition.
Common interface for an IPv4 destination we wish to ping with ICMP or TCP.
Definition: DwmPingDestination.hh:56
Dwm::TimeValue class definition.
virtual void operator()(const PingDestination &destination, const TimeValue &recvTime, const TimeValue &rtt)=0
Operator (). Must be implemented in a derived class.
Definition: DwmBZ2IO.hh:67
static Receiver * SetReceiver(Receiver *receiver)
Registers receiver to receive ping data.
uint32_t PacketsReceived(const PingDestination &destination) const
Returns the number of packets received from the given destination.
Dwm::Ipv4Address class definition.
static Receiver * GetReceiver()
Returns the Receiver registered to receive ping data.
bool Stop()
Stops the pinger. Returns true on success.
bool AddDestination(const PingDestination &destination)
Adds a destination. Returns true on success.
HostPinger(uint16_t identifier, const std::string &device="lo0")
Construct with the given identifier (normally getpid() & 0xFFFF).
This is a pcap wrapper/utility class.
Definition: DwmPcap.hh:57
bool Outstanding() const
Returns true if we have not yet received replies for some of the packets we sent. ...
uint16_t PacketRate() const
Returns the packet rate.
Data callback interface.
Definition: DwmHostPinger.hh:75