libDwm-0.6.0
DwmIpv4UdpPayload.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4UdpPayload.hh 8401 $
3 // @(#) $Id: DwmIpv4UdpPayload.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 _DWMIPV4UDPPAYLOAD_HH_
43 #define _DWMIPV4UDPPAYLOAD_HH_
44 
45 extern "C" {
46  #include <inttypes.h>
47 }
48 
49 #include <cassert>
50 #include <cstdlib>
51 #include <cstring>
52 #include <sstream>
53 #include <string>
54 #include <utility>
55 
56 #include "DwmIO.hh"
57 #include "DwmIpv4UdpHeader.hh"
58 
59 namespace Dwm {
60 
61  //--------------------------------------------------------------------------
63  //--------------------------------------------------------------------------
65  : public Writable, public GZWritable, public BZ2Writable
66  {
67  public:
68  //------------------------------------------------------------------------
70  //------------------------------------------------------------------------
72 
73  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  Ipv4UdpPayload(const Ipv4UdpHeader & udpHeader);
81 
82  //------------------------------------------------------------------------
87  //------------------------------------------------------------------------
88  Ipv4UdpPayload(uint8_t *buf, uint16_t len);
89 
90  //------------------------------------------------------------------------
92  //------------------------------------------------------------------------
93  Ipv4UdpPayload(const std::string & s);
94 
95  //------------------------------------------------------------------------
97  //------------------------------------------------------------------------
99 
100  std::pair<uint16_t,const uint8_t *> Payload() const;
101 
102  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
105  std::string ToString() const;
106 
107  //------------------------------------------------------------------------
110  //------------------------------------------------------------------------
111  uint32_t Sum() const;
112 
113  //------------------------------------------------------------------------
117  //------------------------------------------------------------------------
118  template <typename T>
119  bool GetPayload(T & payload) const
120  {
121  bool rc = false;
122  if (_payload.first) {
123  std::istringstream is(std::string((const char *)_payload.second,
124  _payload.first));
125  rc = IO::Read(is, payload);
126  }
127  return(rc);
128  }
129 
130  //------------------------------------------------------------------------
134  //------------------------------------------------------------------------
135  template <typename T>
136  bool SetPayload(const T & payload)
137  {
138  bool rc = false;
139  Free();
140 
141  uint16_t maxPayloadLen = 65535 - (20 + 8);
142  uint32_t payloadLen = IO::StreamedLength(payload);
143  if (payloadLen <= maxPayloadLen) {
144  std::ostringstream os;
145  if (IO::Write(os, payload)) {
146  _payload.first = payloadLen;
147  _payload.second = (uint8_t *)calloc(1, _payload.first);
148  assert(_payload.second);
149  memcpy(_payload.second, os.str().c_str(), _payload.first);
150  rc = true;
151  }
152  }
153  return(rc);
154  }
155 
156  //------------------------------------------------------------------------
158  //------------------------------------------------------------------------
159  void Clear();
160 
161  //------------------------------------------------------------------------
163  //------------------------------------------------------------------------
164  std::istream & Read(std::istream & is, uint16_t len);
165 
166  //------------------------------------------------------------------------
168  //------------------------------------------------------------------------
169  std::ostream & Write(std::ostream & os) const;
170 
171  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  ssize_t Read(int fd, uint16_t len);
176 
177  //------------------------------------------------------------------------
180  //------------------------------------------------------------------------
181  ssize_t Write(int fd) const;
182 
183  //------------------------------------------------------------------------
186  //------------------------------------------------------------------------
187  size_t Read(FILE *f, uint16_t len);
188 
189  //------------------------------------------------------------------------
192  //------------------------------------------------------------------------
193  size_t Write(FILE *f) const;
194 
195  //------------------------------------------------------------------------
198  //------------------------------------------------------------------------
199  int Read(gzFile gzf, uint16_t len);
200 
201  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
205  int Write(gzFile gzf) const;
206 
207  //------------------------------------------------------------------------
210  //------------------------------------------------------------------------
211  int BZRead(BZFILE *bzf, uint16_t len);
212 
213  //------------------------------------------------------------------------
216  //------------------------------------------------------------------------
217  int BZWrite(BZFILE *bzf) const;
218 
219  //------------------------------------------------------------------------
222  //------------------------------------------------------------------------
223  uint32_t StreamedLength() const;
224 
225  private:
226  std::pair<uint16_t, uint8_t *> _payload;
227  bool _ownData;
228 
229  void Free();
230  void Allocate(uint16_t len);
231  };
232 
233 } // namespace Dwm
234 
235 #endif // _DWMIPV4UDPPAYLOAD_HH_
236 
237 //---------------------------- emacs settings -----------------------------
238 // Local Variables:
239 // mode: C++/la
240 // tab-width: 2
241 // indent-tabs-mode: nil
242 // c-basic-offset: 2
243 // End:
244 //-------------------------------------------------------------------------
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmGZWritable.hh:57
std::string ToString() const
Returns the payload as a string.
uint32_t Sum() const
Returns the IP sum of the payload, which can be used as part of UDP checksum computation.
int BZWrite(BZFILE *bzf) const
Writes the payload to the given BZFILE bzf.
Encapsulates an IPv4 UDP header.
Definition: DwmIpv4UdpHeader.hh:71
std::ostream & Write(std::ostream &os) const
Writes the payload to the given ostream os.
void Clear()
Clears the payload.
std::istream & Read(std::istream &is, uint16_t len)
Reads len bytes of the payload from the given istream is.
~Ipv4UdpPayload()
Destructor.
bool GetPayload(T &payload) const
Copies the payload into payload.
Definition: DwmIpv4UdpPayload.hh:119
Encapsulates a UDP payload.
Definition: DwmIpv4UdpPayload.hh:64
static std::ostream & Write(std::ostream &os, char c)
Writes c to os. Returns os.
Ipv4UdpPayload()
Constructor.
static std::istream & Read(std::istream &is, char &c)
Reads c from is. Returns is.
Definition: DwmBZ2IO.hh:67
Dwm::IO class definition.
uint32_t StreamedLength() const
Returns the number of bytes that would be written if one of the Write() members were called...
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
int BZRead(BZFILE *bzf, uint16_t len)
Reads len bytes of the payload from the given BZFILE bzf.
Dwm::Ipv4UdpHeader class definition.
bool SetPayload(const T &payload)
Sets the payload to payload.
Definition: DwmIpv4UdpPayload.hh:136