libDwm-0.6.0
DwmIpv4TcpPayload.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4TcpPayload.hh 8401 $
3 // @(#) $Id: DwmIpv4TcpPayload.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 _DWMIPV4TCPPAYLOAD_HH_
43 #define _DWMIPV4TCPPAYLOAD_HH_
44 
45 extern "C" {
46  #include <inttypes.h>
47 }
48 
49 #include <cstdlib>
50 #include <cstring>
51 #include <sstream>
52 #include <string>
53 #include <utility>
54 
55 #include "DwmIO.hh"
56 #include "DwmIpv4TcpHeader.hh"
57 #include "DwmWritable.hh"
58 #include "DwmGZWritable.hh"
59 #include "DwmBZ2Writable.hh"
60 
61 namespace Dwm {
62 
63  //--------------------------------------------------------------------------
67  //--------------------------------------------------------------------------
69  : public Writable, public GZWritable, public BZ2Writable
70  {
71  public:
72  //------------------------------------------------------------------------
74  //------------------------------------------------------------------------
76 
77  //------------------------------------------------------------------------
83  //------------------------------------------------------------------------
84  Ipv4TcpPayload(const Ipv4PacketHeader & ipHeader,
85  const Ipv4TcpHeader & tcpHeader);
86 
87  //------------------------------------------------------------------------
92  //------------------------------------------------------------------------
93  Ipv4TcpPayload(uint8_t *buf, uint16_t len);
94 
95  //------------------------------------------------------------------------
97  //------------------------------------------------------------------------
98  Ipv4TcpPayload(const std::string & s);
99 
100  //------------------------------------------------------------------------
102  //------------------------------------------------------------------------
103  ~Ipv4TcpPayload();
104 
105  //------------------------------------------------------------------------
107  //------------------------------------------------------------------------
108  std::pair<uint16_t,const uint8_t *> Payload() const;
109 
110  //------------------------------------------------------------------------
112  //------------------------------------------------------------------------
113  std::string ToString() const;
114 
115  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
119  uint32_t Sum() const;
120 
121  //------------------------------------------------------------------------
125  //------------------------------------------------------------------------
126  template <typename T>
127  bool GetPayload(T & payload) const
128  {
129  bool rc = false;
130  if (_payload.first) {
131  std::istringstream is(std::string((const char *)_payload.second,
132  _payload.first));
133  rc = IO::Read(is, payload);
134  }
135  return(rc);
136  }
137 
138  //------------------------------------------------------------------------
142  //------------------------------------------------------------------------
143  template <typename T>
144  bool SetPayload(const T & payload)
145  {
146  bool rc = false;
147  Free();
148 
149  uint16_t maxPayloadLen = 65535 - (20 + 8);
150  uint32_t payloadLen = IO::StreamedLength(payload);
151  if (payloadLen <= maxPayloadLen) {
152  std::ostringstream os;
153  if (IO::Write(os, payload)) {
154  _payload.first = payloadLen;
155  _payload.second = (uint8_t *)calloc(1, _payload.first);
156  assert(_payload.second);
157  memcpy(_payload.second, os.str().c_str(), _payload.first);
158  rc = true;
159  }
160  }
161  return(rc);
162  }
163 
164  //------------------------------------------------------------------------
166  //------------------------------------------------------------------------
167  void Clear();
168 
169  //------------------------------------------------------------------------
171  //------------------------------------------------------------------------
172  std::istream & Read(std::istream & is, uint16_t len);
173 
174  //------------------------------------------------------------------------
176  //------------------------------------------------------------------------
177  std::ostream & Write(std::ostream & os) const;
178 
179  //------------------------------------------------------------------------
182  //------------------------------------------------------------------------
183  ssize_t Read(int fd, uint16_t len);
184 
185  //------------------------------------------------------------------------
188  //------------------------------------------------------------------------
189  ssize_t Write(int fd) const;
190 
191  //------------------------------------------------------------------------
194  //------------------------------------------------------------------------
195  size_t Read(FILE *f, uint16_t len);
196 
197  //------------------------------------------------------------------------
200  //------------------------------------------------------------------------
201  size_t Write(FILE *f) const;
202 
203  //------------------------------------------------------------------------
206  //------------------------------------------------------------------------
207  int Read(gzFile gzf, uint16_t len);
208 
209  //------------------------------------------------------------------------
212  //------------------------------------------------------------------------
213  int Write(gzFile gzf) const;
214 
215  //------------------------------------------------------------------------
218  //------------------------------------------------------------------------
219  int BZRead(BZFILE *bzf, uint16_t len);
220 
221  //------------------------------------------------------------------------
224  //------------------------------------------------------------------------
225  int BZWrite(BZFILE *bzf) const;
226 
227  //------------------------------------------------------------------------
230  //------------------------------------------------------------------------
231  uint32_t StreamedLength() const;
232 
233  private:
234  std::pair<uint16_t, uint8_t *> _payload;
235  bool _ownData;
236 
237  void Free();
238  void Allocate(uint16_t len);
239  };
240 
241 } // namespace Dwm
242 
243 #endif // _DWMIPV4TCPPAYLOAD_HH_
244 
245 //---------------------------- emacs settings -----------------------------
246 // Local Variables:
247 // mode: C++/la
248 // tab-width: 2
249 // indent-tabs-mode: nil
250 // c-basic-offset: 2
251 // End:
252 //-------------------------------------------------------------------------
Ipv4TcpPayload()
Constructor.
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmGZWritable.hh:57
~Ipv4TcpPayload()
Destructor.
Dwm::Writable pure virtual class definition.
std::istream & Read(std::istream &is, uint16_t len)
Reads len bytes of the payload from the given istream is.
bool SetPayload(const T &payload)
Sets the payload to payload.
Definition: DwmIpv4TcpPayload.hh:144
Dwm::Ipv4TcpHeader class definition.
int BZRead(BZFILE *bzf, uint16_t len)
Reads len bytes of the payload from the given BZFILE bzf.
uint32_t StreamedLength() const
Returns the number of bytes that would be written if we called one of the Write() members...
Encapsulates a TCP payload.
Definition: DwmIpv4TcpPayload.hh:68
std::ostream & Write(std::ostream &os) const
Writes the payload to the given ostream os.
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
std::pair< uint16_t, const uint8_t * > Payload() const
Returns a shallow copy of the payload.
Dwm::BZ2Writable pure virtual class definition.
std::string ToString() const
Returns the payload as a string.
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
Definition: DwmBZ2IO.hh:67
Dwm::IO class definition.
This class defines an interface for classes that can write their contents to an ostream, file descriptor or FILE pointer.
Definition: DwmWritable.hh:58
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
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmBZ2Writable.hh:57
Dwm::GZWritable pure virtual class definition.
uint32_t Sum() const
Returns the IP sum of the payload, which can be used as part of TCP checksum computation.
void Clear()
Clears the payload.
Encapsulates a TCP header.
Definition: DwmIpv4TcpHeader.hh:69
int BZWrite(BZFILE *bzf) const
Writes the payload to the given BZFILE bzf.
bool GetPayload(T &payload) const
Copies the payload into payload.
Definition: DwmIpv4TcpPayload.hh:127
Encapsulates an IPv4 packet header.
Definition: DwmIpv4PacketHeader.hh:65