libDwm-0.6.0
DwmIpv4UdpHeader.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4UdpHeader.hh 8401 $
3 // @(#) $Id: DwmIpv4UdpHeader.hh 8401 2016-04-17 06:44:31Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2006, 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 _DWMIPV4UDPHEADER_HH_
43 #define _DWMIPV4UDPHEADER_HH_
44 
45 extern "C" {
46  #include <sys/types.h>
47  #ifdef __linux__
48  #define __FAVOR_BSD 1
49  #endif
50  #include <netinet/udp.h>
51  #include <inttypes.h>
52 }
53 
54 #include <string>
55 
56 #include "DwmBZ2Readable.hh"
57 #include "DwmBZ2Writable.hh"
58 #include "DwmGZReadable.hh"
59 #include "DwmGZWritable.hh"
60 #include "DwmReadable.hh"
61 #include "DwmWritable.hh"
62 #include "DwmIpv4PacketHeader.hh"
63 
64 namespace Dwm {
65 
66  class Ipv4UdpPayload;
67 
68  //--------------------------------------------------------------------------
70  //--------------------------------------------------------------------------
72  : public Readable, public Writable,
73  public GZReadable, public GZWritable,
74  public BZ2Readable, public BZ2Writable
75  {
76  public:
77  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  Ipv4UdpHeader();
81 
82  //------------------------------------------------------------------------
84  //------------------------------------------------------------------------
85  Ipv4UdpHeader(const Ipv4PacketHeader & ipHeader);
86 
87  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  Ipv4UdpHeader(const Ipv4UdpHeader & udpHeader);
91 
92  //------------------------------------------------------------------------
97  //------------------------------------------------------------------------
98  Ipv4UdpHeader(struct udphdr *udpHdr);
99 
100  //------------------------------------------------------------------------
102  // own the underlying memory.
103  //------------------------------------------------------------------------
104  Ipv4UdpHeader(const struct udphdr & udpHdr);
105 
106  //------------------------------------------------------------------------
108  //------------------------------------------------------------------------
109  Ipv4UdpHeader & operator = (const Ipv4UdpHeader & udpHeader);
110 
111  //------------------------------------------------------------------------
114  //------------------------------------------------------------------------
115  ~Ipv4UdpHeader();
116 
117  //------------------------------------------------------------------------
119  //------------------------------------------------------------------------
120  uint16_t SourcePort() const;
121 
122  //------------------------------------------------------------------------
125  //------------------------------------------------------------------------
126  uint16_t SourcePort(uint16_t port);
127 
128  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  uint16_t DestinationPort() const;
132 
133  //------------------------------------------------------------------------
136  //------------------------------------------------------------------------
137  uint16_t DestinationPort(uint16_t port);
138 
139  //------------------------------------------------------------------------
142  //------------------------------------------------------------------------
143  uint16_t UdpLength() const;
144 
145  //------------------------------------------------------------------------
148  //------------------------------------------------------------------------
149  uint16_t UdpLength(uint16_t udpLength);
150 
151  //------------------------------------------------------------------------
153  //------------------------------------------------------------------------
154  uint16_t UdpChecksum() const;
155 
156  //------------------------------------------------------------------------
159  //------------------------------------------------------------------------
160  void SetUdpChecksum(const Ipv4PacketHeader & ipHeader,
161  const std::string & data);
162 
163  //------------------------------------------------------------------------
166  //------------------------------------------------------------------------
167  void SetUdpChecksum(const Ipv4PacketHeader & ipHeader,
168  const Ipv4UdpPayload & data);
169 
170  uint8_t *End() const;
171 
172  //------------------------------------------------------------------------
174  //------------------------------------------------------------------------
175  std::istream & Read(std::istream & is);
176 
177  //------------------------------------------------------------------------
179  //------------------------------------------------------------------------
180  std::ostream & Write(std::ostream & os) const;
181 
182  //------------------------------------------------------------------------
186  //------------------------------------------------------------------------
187  ssize_t Read(int fd);
188 
189  //------------------------------------------------------------------------
193  //------------------------------------------------------------------------
194  ssize_t Write(int fd) const;
195 
196  //------------------------------------------------------------------------
199  //------------------------------------------------------------------------
200  size_t Read(FILE * f);
201 
202  //------------------------------------------------------------------------
205  //------------------------------------------------------------------------
206  size_t Write(FILE *f) const;
207 
208  //------------------------------------------------------------------------
211  //------------------------------------------------------------------------
212  uint32_t StreamedLength() const;
213 
214  //------------------------------------------------------------------------
218  //------------------------------------------------------------------------
219  int Read(gzFile gzf);
220 
221  //------------------------------------------------------------------------
225  //------------------------------------------------------------------------
226  int Write(gzFile gzf) const;
227 
228  //------------------------------------------------------------------------
232  //------------------------------------------------------------------------
233  int BZRead(BZFILE *bzf);
234 
235  //------------------------------------------------------------------------
239  //------------------------------------------------------------------------
240  int BZWrite(BZFILE *bzf) const;
241 
242  //------------------------------------------------------------------------
244  //------------------------------------------------------------------------
245  bool operator == (const Ipv4UdpHeader & udpHeader) const;
246 
247  //------------------------------------------------------------------------
250  //------------------------------------------------------------------------
251  std::ostream & PrintXML(std::ostream & os,
252  const std::string & pre = "") const;
253 
254  private:
255  typedef struct {
256  uint16_t sourcePort;
257  uint16_t destinationPort;
258  uint16_t udpLength;
259  uint16_t checksum;
260  } Data;
261 
262  Data *_data;
263  bool _ownData;
264  };
265 
266 } // namespace Dwm
267 
268 #endif // _DWMIPV4UDPHEADER_HH_
269 
270 //---------------------------- emacs settings -----------------------------
271 // Local Variables:
272 // mode: C++/la
273 // tab-width: 2
274 // indent-tabs-mode: nil
275 // c-basic-offset: 2
276 // End:
277 //-------------------------------------------------------------------------
Ipv4UdpHeader()
Constructor. The new object will own the underlying memory.
~Ipv4UdpHeader()
Destructor.
uint16_t UdpLength() const
Returns the UDP length in host byte order.
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmGZWritable.hh:57
int BZRead(BZFILE *bzf)
Reads the UDP header from a BZFILE.
Dwm::Writable pure virtual class definition.
Dwm::BZ2Readable pure virtual class definition.
bool operator==(const Ipv4UdpHeader &udpHeader) const
operator ==
Dwm::GZReadable pure virtual class definition.
uint16_t DestinationPort() const
Returns the UDP destination port in host byte order.
Dwm::Readable pure virtual class definition.
std::ostream & PrintXML(std::ostream &os, const std::string &pre="") const
Print the UDP header to an ostream in pseudo-XML, with each line prefixed by pre. ...
Encapsulates an IPv4 UDP header.
Definition: DwmIpv4UdpHeader.hh:71
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmBZ2Readable.hh:57
Dwm::Ipv4PacketHeader class definition.
uint32_t StreamedLength() const
Returns the number of bytes that would be written if we called one of the Write() members...
Ipv4UdpHeader & operator=(const Ipv4UdpHeader &udpHeader)
operator =
Encapsulates a UDP payload.
Definition: DwmIpv4UdpPayload.hh:64
Dwm::BZ2Writable pure virtual class definition.
Definition: DwmBZ2IO.hh:67
void SetUdpChecksum(const Ipv4PacketHeader &ipHeader, const std::string &data)
Calculates and sets the UDP checksum field, which requires the UDP payload data and some fields from ...
std::istream & Read(std::istream &is)
Reads the UDP header from an istream. Returns the istream.
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmGZReadable.hh:57
uint16_t UdpChecksum() const
Returns the UDP checksum field.
This class defines an interface for classes that can write their contents to an ostream, file descriptor or FILE pointer.
Definition: DwmWritable.hh:58
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.
std::ostream & Write(std::ostream &os) const
Writes the UDP header to an ostream. Returns the ostream.
This class defines an interface for classes that can read their contents from an istream, file descriptor or FILE pointer.
Definition: DwmReadable.hh:58
int BZWrite(BZFILE *bzf) const
Writes the UDP header to a BZFILE.
uint16_t SourcePort() const
Returns the UDP source port in host byte order.
Encapsulates an IPv4 packet header.
Definition: DwmIpv4PacketHeader.hh:65