libDwm-0.6.0
DwmIpv4IcmpEchoReply.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4IcmpEchoReply.hh 8401 $
3 // @(#) $Id: DwmIpv4IcmpEchoReply.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 _DWMIPV4ICMPECHOREPLY_HH_
43 #define _DWMIPV4ICMPECHOREPLY_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  Ipv4IcmpEchoReply(uint16_t id, uint16_t seq, const std::string & data);
76 
77  Ipv4IcmpEchoReply(uint8_t *buf, uint16_t len);
78 
79  //------------------------------------------------------------------------
81  //------------------------------------------------------------------------
82  uint16_t Identifier() const;
83 
84  //------------------------------------------------------------------------
86  //------------------------------------------------------------------------
87  uint16_t Identifier(uint16_t id);
88 
89  //------------------------------------------------------------------------
91  //------------------------------------------------------------------------
92  uint16_t SequenceNumber() const;
93 
94  //------------------------------------------------------------------------
96  //------------------------------------------------------------------------
97  uint16_t SequenceNumber(uint16_t seq);
98 
99  //------------------------------------------------------------------------
101  //------------------------------------------------------------------------
102  std::string Data() const;
103 
104  //------------------------------------------------------------------------
106  //------------------------------------------------------------------------
107  std::string Data(const std::string & data);
108 
109  //------------------------------------------------------------------------
113  //------------------------------------------------------------------------
114  template <typename T>
115  bool SetData(const T & data)
116  {
117  bool rc = false;
118  uint32_t dataLen = IO::StreamedLength(data);
119  Reallocate(dataLen + 8);
120  std::ostringstream os;
121  if (IO::Write(os, data)) {
122  memcpy((char *)_data.second + 8, os.str().c_str(), dataLen);
123  rc = true;
124  }
125  return rc;
126  }
127 
128  //------------------------------------------------------------------------
133  //------------------------------------------------------------------------
134  template <typename T>
135  bool GetData(T & data) const
136  {
137  bool rc = false;
138  if (_data.first >= (8 + IO::StreamedLength(data))) {
139  std::istringstream is(std::string((char *)_data.second + 8,
140  _data.first - 8));
141  if (IO::Read(is, data)) {
142  rc = true;
143  }
144  }
145  return(rc);
146  }
147 
148  //------------------------------------------------------------------------
152  //------------------------------------------------------------------------
153  bool RecvFrom(Socket & s, Ipv4Address & source);
154 
155  };
156 
157 } // namespace Dwm
158 
159 #endif // _DWMIPV4ICMPECHOREPLY_HH_
160 
161 //---------------------------- emacs settings -----------------------------
162 // Local Variables:
163 // mode: C++/la
164 // tab-width: 2
165 // indent-tabs-mode: nil
166 // c-basic-offset: 2
167 // End:
168 //-------------------------------------------------------------------------
This class abstracts a UNIX socket descriptor.
Definition: DwmSocket.hh:62
Base class for ICMP messages.
Definition: DwmIpv4IcmpMessage.hh:60
uint16_t Identifier() const
Returns the identifier.
This class encapsulates an IPv4 address.
Definition: DwmIpv4Address.hh:62
Encapsulates an IPv4 ICMP echo reply.
Definition: DwmIpv4IcmpEchoReply.hh:62
Ipv4IcmpEchoReply()
Default constructor.
bool SetData(const T &data)
Sets the data to the given data.
Definition: DwmIpv4IcmpEchoReply.hh:115
Dwm::Socket class definition.
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
Definition: DwmBZ2IO.hh:67
std::string Data() const
Returns the data as a string.
Dwm::IO class definition.
Dwm::Ipv4IcmpMessage class definition.
Dwm::Ipv4Address class definition.
uint16_t SequenceNumber() const
Returns the sequence number.
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
bool GetData(T &data) const
Copies the encapsulated data into data.
Definition: DwmIpv4IcmpEchoReply.hh:135
bool RecvFrom(Socket &s, Ipv4Address &source)
Receives the echo reply from the given Socket s.