libDwm-0.6.0
DwmIpv4IcmpEchoRequest.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4IcmpEchoRequest.hh 8401 $
3 // @(#) $Id: DwmIpv4IcmpEchoRequest.hh 8401 2016-04-17 06:44:31Z 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 _DWMIPV4ICMPECHOREQUEST_HH_
43 #define _DWMIPV4ICMPECHOREQUEST_HH_
44 
45 extern "C" {
46 #include <inttypes.h>
47 }
48 
49 #include <sstream>
50 #include <string>
51 
52 #include "DwmIO.hh"
53 #include "DwmIpv4Address.hh"
54 #include "DwmIpv4IcmpMessage.hh"
55 #include "DwmSocket.hh"
56 
57 namespace Dwm {
58 
59  //--------------------------------------------------------------------------
61  //--------------------------------------------------------------------------
63  : public Ipv4IcmpMessage
64  {
65  public:
66  //------------------------------------------------------------------------
68  //------------------------------------------------------------------------
70 
71  //------------------------------------------------------------------------
74  //------------------------------------------------------------------------
75  Ipv4IcmpEchoRequest(uint16_t id, uint16_t seq, const std::string & data);
76 
77  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  Ipv4IcmpEchoRequest(uint8_t *buf, uint16_t len);
81 
82  //------------------------------------------------------------------------
84  //------------------------------------------------------------------------
85  uint16_t Identifier() const;
86 
87  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  uint16_t Identifier(uint16_t id);
91 
92  //------------------------------------------------------------------------
94  //------------------------------------------------------------------------
95  uint16_t SequenceNumber() const;
96 
97  //------------------------------------------------------------------------
99  //------------------------------------------------------------------------
100  uint16_t SequenceNumber(uint16_t seq);
101 
102  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
105  std::string Data() const;
106 
107  //------------------------------------------------------------------------
109  //------------------------------------------------------------------------
110  std::string Data(const std::string & data);
111 
112  //------------------------------------------------------------------------
117  //------------------------------------------------------------------------
118  template <typename T>
119  bool SetData(const T & data)
120  {
121  bool rc = false;
122  uint32_t dataLen = IO::StreamedLength(data);
123  Reallocate(dataLen + 8);
124  std::ostringstream os;
125  if (IO::Write(os, data)) {
126  memcpy((char *)_data.second + 8, os.str().c_str(), dataLen);
127  rc = true;
128  }
129  return rc;
130  }
131 
132  //------------------------------------------------------------------------
137  //------------------------------------------------------------------------
138  template <typename T>
139  bool GetData(T & data) const
140  {
141  bool rc = false;
142  if (_data.first >= (8 + IO::StreamedLength(data))) {
143  std::istringstream is(std::string((char *)_data.second + 8,
144  _data.first - 8));
145  rc = (IO::Read(is, data) ? true : false);
146  }
147  return(rc);
148  }
149 
150  //------------------------------------------------------------------------
153  //------------------------------------------------------------------------
154  bool SendTo(Socket & s, const Ipv4Address & destination);
155 
156  };
157 
158 } // namespace Dwm
159 
160 #endif // _DWMIPV4ICMPECHOREQUEST_HH_
161 
162 //---------------------------- emacs settings -----------------------------
163 // Local Variables:
164 // mode: C++/la
165 // tab-width: 2
166 // indent-tabs-mode: nil
167 // c-basic-offset: 2
168 // End:
169 //-------------------------------------------------------------------------
This class abstracts a UNIX socket descriptor.
Definition: DwmSocket.hh:62
Base class for ICMP messages.
Definition: DwmIpv4IcmpMessage.hh:60
std::string Data() const
Returns the data as a string.
This class encapsulates an IPv4 address.
Definition: DwmIpv4Address.hh:62
uint16_t Identifier() const
Returns the identifier.
bool GetData(T &data) const
Copies the enacapsulated data into data.
Definition: DwmIpv4IcmpEchoRequest.hh:139
Ipv4IcmpEchoRequest()
Default constructor.
Dwm::Socket class definition.
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
bool SetData(const T &data)
Sets the data to the given data.
Definition: DwmIpv4IcmpEchoRequest.hh:119
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
Definition: DwmBZ2IO.hh:67
Dwm::IO class definition.
Dwm::Ipv4IcmpMessage class definition.
Dwm::Ipv4Address class definition.
static uint32_t StreamedLength(char c)
Returns the number of bytes that would be written if we called Write() for a char.
Definition: DwmIO.hh:92
Encapsulates an ICMP echo request.
Definition: DwmIpv4IcmpEchoRequest.hh:62
uint16_t SequenceNumber() const
Returns the sequence number.
bool SendTo(Socket &s, const Ipv4Address &destination)
Sends the echo request to the given destination via the given Socket s.