libDwm-0.6.0
DwmIpv4UdpPacket.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4UdpPacket.hh 8401 $
3 // @(#) $Id: DwmIpv4UdpPacket.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 _DWMIPV4UDPPACKET_HH_
43 #define _DWMIPV4UDPPACKET_HH_
44 
45 #include <sstream>
46 #include <utility>
47 
48 #include "DwmIO.hh"
49 #include "DwmIpv4PacketHeader.hh"
50 #include "DwmIpv4UdpHeader.hh"
51 #include "DwmIpv4UdpPayload.hh"
52 #include "DwmReadable.hh"
53 #include "DwmSocket.hh"
54 #include "DwmWritable.hh"
55 
56 namespace Dwm {
57 
58  //--------------------------------------------------------------------------
60  //--------------------------------------------------------------------------
62  : public Readable, public Writable,
63  public GZReadable, public GZWritable,
64  public BZ2Readable, public BZ2Writable
65  {
66  public:
67  //------------------------------------------------------------------------
69  //------------------------------------------------------------------------
70  Ipv4UdpPacket();
71 
72  //------------------------------------------------------------------------
75  //------------------------------------------------------------------------
76  Ipv4UdpPacket(uint8_t *ptr);
77 
78  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
82 
83  //------------------------------------------------------------------------
85  //------------------------------------------------------------------------
86  const Ipv4PacketHeader & IpHeader() const;
87 
88  //------------------------------------------------------------------------
90  //------------------------------------------------------------------------
92 
93  //------------------------------------------------------------------------
95  //------------------------------------------------------------------------
96  const Ipv4UdpHeader & UdpHeader() const;
97 
98  //------------------------------------------------------------------------
100  //------------------------------------------------------------------------
102 
103  //------------------------------------------------------------------------
105  //------------------------------------------------------------------------
106  std::string Payload() const
107  {
108  return(_udpPayload.ToString());
109  }
110 
111  //------------------------------------------------------------------------
115  //------------------------------------------------------------------------
116  template <typename T>
117  bool GetPayload(T & payload) const
118  {
119  return(_udpPayload.GetPayload<T>(payload));
120  }
121 
122  //------------------------------------------------------------------------
126  //------------------------------------------------------------------------
127  template <typename T>
128  bool SetPayload(const T & payload)
129  {
130  bool rc = _udpPayload.SetPayload<T>(payload);
131 
132  if (rc) {
133  _udpHeader.UdpLength(_udpPayload.StreamedLength() + 8);
134  _ipHeader.TotalLength(_ipHeader.HeaderLength()
135  + _udpHeader.UdpLength());
136  }
137  return(rc);
138  }
139 
140  //------------------------------------------------------------------------
142  //------------------------------------------------------------------------
143  std::istream & Read(std::istream & is);
144 
145  //------------------------------------------------------------------------
147  //------------------------------------------------------------------------
148  std::ostream & Write(std::ostream & os) const;
149 
150  //------------------------------------------------------------------------
152  //------------------------------------------------------------------------
153  ssize_t Read(int fd);
154 
155  //------------------------------------------------------------------------
158  //------------------------------------------------------------------------
159  ssize_t Write(int fd) const;
160 
161  //------------------------------------------------------------------------
164  //------------------------------------------------------------------------
165  size_t Read(FILE * f);
166 
167  //------------------------------------------------------------------------
170  //------------------------------------------------------------------------
171  size_t Write(FILE *f) const;
172 
173  //------------------------------------------------------------------------
176  //------------------------------------------------------------------------
177  int Read(gzFile gzf);
178 
179  //------------------------------------------------------------------------
182  //------------------------------------------------------------------------
183  int Write(gzFile gzf) const;
184 
185  //------------------------------------------------------------------------
188  //------------------------------------------------------------------------
189  int BZRead(BZFILE *bzf);
190 
191  //------------------------------------------------------------------------
194  //------------------------------------------------------------------------
195  int BZWrite(BZFILE *bzf) const;
196 
197  //------------------------------------------------------------------------
200  //------------------------------------------------------------------------
201  uint32_t StreamedLength() const;
202 
203  //------------------------------------------------------------------------
206  //------------------------------------------------------------------------
207  bool SendTo(int fd) const;
208 
209  //------------------------------------------------------------------------
212  //------------------------------------------------------------------------
213  bool SendTo(Socket & s) const;
214 
215  private:
216  Ipv4PacketHeader _ipHeader;
217  Ipv4UdpHeader _udpHeader;
218  Ipv4UdpPayload _udpPayload;
219  bool _ownData;
220 
221  void FreePayload();
222  };
223 
224 } // namespace Dwm
225 
226 #endif // _DWMIPV4UDPPACKET_HH_
227 
228 //---------------------------- emacs settings -----------------------------
229 // Local Variables:
230 // mode: C++/la
231 // tab-width: 2
232 // indent-tabs-mode: nil
233 // c-basic-offset: 2
234 // End:
235 //-------------------------------------------------------------------------
This class abstracts a UNIX socket descriptor.
Definition: DwmSocket.hh:62
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
bool SendTo(int fd) const
Sends the packet via the given descriptor fd.
Dwm::Writable pure virtual class definition.
std::string ToString() const
Returns the payload as a string.
std::istream & Read(std::istream &is)
Reads from an istream. Returns the istream.
uint16_t TotalLength() const
Returns the total length field in host byte order.
int BZRead(BZFILE *bzf)
Reads the packet from the given BZFILE bzf.
Dwm::Readable pure virtual class definition.
bool SetPayload(const T &payload)
Sets the contained payload to payload.
Definition: DwmIpv4UdpPacket.hh:128
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.
Dwm::Socket class definition.
uint32_t StreamedLength() const
Returns the numbers of bytes that should be written if one of the Write() members is called...
bool GetPayload(T &payload) const
Copies the payload into payload.
Definition: DwmIpv4UdpPayload.hh:119
Encapsulates a UDP payload.
Definition: DwmIpv4UdpPayload.hh:64
Ipv4UdpPacket()
Constructor.
Dwm::Ipv4UdpPayload class definition.
const Ipv4PacketHeader & IpHeader() const
Returns a const reference to the IP header.
Definition: DwmBZ2IO.hh:67
std::ostream & Write(std::ostream &os) const
Writes to an ostream. Returns the ostream.
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmGZReadable.hh:57
bool GetPayload(T &payload) const
Copies the contained payload into payload.
Definition: DwmIpv4UdpPacket.hh:117
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
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmBZ2Writable.hh:57
~Ipv4UdpPacket()
Destructor. If we own the underlying data, we free it.
This class defines an interface for classes that can read their contents from an istream, file descriptor or FILE pointer.
Definition: DwmReadable.hh:58
uint8_t HeaderLength() const
Returns the IP header length.
int BZWrite(BZFILE *bzf) const
Writes the packet to the given BZFILE bzf.
std::string Payload() const
Returns the payload as a string.
Definition: DwmIpv4UdpPacket.hh:106
const Ipv4UdpHeader & UdpHeader() const
Returns a const refernece to the UDP header.
Dwm::Ipv4UdpHeader class definition.
Encapsulates an IPv4 packet header.
Definition: DwmIpv4PacketHeader.hh:65
Encapsulates a UDP packet, including the IP header.
Definition: DwmIpv4UdpPacket.hh:61
bool SetPayload(const T &payload)
Sets the payload to payload.
Definition: DwmIpv4UdpPayload.hh:136