libDwm-0.6.0
DwmIpv4TcpPacket.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4TcpPacket.hh 8401 $
3 // @(#) $Id: DwmIpv4TcpPacket.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 _DWMIPV4TCPPACKET_HH_
43 #define _DWMIPV4TCPPACKET_HH_
44 
45 #include <sstream>
46 #include <utility>
47 
48 #include "DwmBZ2Readable.hh"
49 #include "DwmBZ2Writable.hh"
50 #include "DwmGZReadable.hh"
51 #include "DwmGZWritable.hh"
52 #include "DwmIO.hh"
53 #include "DwmIpv4PacketHeader.hh"
54 #include "DwmIpv4TcpHeader.hh"
55 #include "DwmIpv4TcpPayload.hh"
56 #include "DwmReadable.hh"
57 #include "DwmSocket.hh"
58 #include "DwmWritable.hh"
59 
60 namespace Dwm {
61 
62  //--------------------------------------------------------------------------
64  //--------------------------------------------------------------------------
66  : public Readable, public Writable,
67  public GZReadable, public GZWritable,
68  public BZ2Readable, public BZ2Writable
69  {
70  public:
71  //------------------------------------------------------------------------
73  //------------------------------------------------------------------------
74  Ipv4TcpPacket();
75 
76  //------------------------------------------------------------------------
79  //------------------------------------------------------------------------
80  Ipv4TcpPacket(uint8_t *ptr);
81 
82  //------------------------------------------------------------------------
84  //------------------------------------------------------------------------
86 
87  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  const Ipv4PacketHeader & IpHeader() const;
91 
92  //------------------------------------------------------------------------
94  //------------------------------------------------------------------------
96 
97  //------------------------------------------------------------------------
99  //------------------------------------------------------------------------
100  const Ipv4TcpHeader & TcpHeader() const;
101 
102  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
106 
107  //------------------------------------------------------------------------
109  //------------------------------------------------------------------------
110  std::string Payload() const
111  {
112  return(_tcpPayload.ToString());
113  }
114 
115  //------------------------------------------------------------------------
119  //------------------------------------------------------------------------
120  template <typename T>
121  bool GetPayload(T & payload) const
122  {
123  return(_tcpPayload.GetPayload<T>(payload));
124  }
125 
126  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  template <typename T>
132  bool SetPayload(const T & payload)
133  {
134  bool rc = _tcpPayload.SetPayload<T>(payload);
135 
136  if (rc) {
137  _ipHeader.TotalLength(_ipHeader.HeaderLength()
138  + _tcpHeader.DataOffset() +
139  _tcpPayload.StreamedLength());
140  }
141  return(rc);
142  }
143 
144  //------------------------------------------------------------------------
146  //------------------------------------------------------------------------
147  std::istream & Read(std::istream & is);
148 
149  //------------------------------------------------------------------------
151  //------------------------------------------------------------------------
152  std::ostream & Write(std::ostream & os) const;
153 
154  //------------------------------------------------------------------------
157  //------------------------------------------------------------------------
158  ssize_t Read(int fd);
159 
160  //------------------------------------------------------------------------
163  //------------------------------------------------------------------------
164  ssize_t Write(int fd) const;
165 
166  //------------------------------------------------------------------------
169  //------------------------------------------------------------------------
170  size_t Read(FILE *f);
171 
172  //------------------------------------------------------------------------
175  //------------------------------------------------------------------------
176  size_t Write(FILE *f) const;
177 
178  //------------------------------------------------------------------------
181  //------------------------------------------------------------------------
182  int Read(gzFile gzf);
183 
184  //------------------------------------------------------------------------
187  //------------------------------------------------------------------------
188  int Write(gzFile gzf) const;
189 
190  //------------------------------------------------------------------------
193  //------------------------------------------------------------------------
194  int BZRead(BZFILE *bzf);
195 
196  //------------------------------------------------------------------------
199  //------------------------------------------------------------------------
200  int BZWrite(BZFILE *bzf) const;
201 
202  //------------------------------------------------------------------------
205  //------------------------------------------------------------------------
206  uint32_t StreamedLength() const;
207 
208  //------------------------------------------------------------------------
211  //------------------------------------------------------------------------
212  bool SendTo(int fd) const;
213 
214  //------------------------------------------------------------------------
217  //------------------------------------------------------------------------
218  bool SendTo(Socket & s) const;
219 
220  private:
221  Ipv4PacketHeader _ipHeader;
222  Ipv4TcpHeader _tcpHeader;
223  Ipv4TcpPayload _tcpPayload;
224  bool _ownData;
225 
226  void FreePayload();
227  };
228 
229 } // namespace Dwm
230 
231 #endif // _DWMIPV4TCPPACKET_HH_
232 
233 //---------------------------- emacs settings -----------------------------
234 // Local Variables:
235 // mode: C++/la
236 // tab-width: 2
237 // indent-tabs-mode: nil
238 // c-basic-offset: 2
239 // End:
240 //-------------------------------------------------------------------------
This class abstracts a UNIX socket descriptor.
Definition: DwmSocket.hh:62
const Ipv4TcpHeader & TcpHeader() const
Returns a const refernece to the TCP header.
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmGZWritable.hh:57
Dwm::Writable pure virtual class definition.
Dwm::BZ2Readable pure virtual class definition.
bool SetPayload(const T &payload)
Sets the packet payload to payload.
Definition: DwmIpv4TcpPacket.hh:132
uint16_t TotalLength() const
Returns the total length field in host byte order.
bool SetPayload(const T &payload)
Sets the payload to payload.
Definition: DwmIpv4TcpPayload.hh:144
Dwm::GZReadable pure virtual class definition.
Dwm::Readable pure virtual class definition.
bool SendTo(int fd) const
Sends the packet via the given descriptor fd.
Dwm::Ipv4TcpHeader class definition.
const Ipv4PacketHeader & IpHeader() const
Returns a const reference to the IP header.
int BZWrite(BZFILE *bzf) const
Writes the packet to the given BZFILE bzf.
bool GetPayload(T &payload) const
Copies the packet payload into payload.
Definition: DwmIpv4TcpPacket.hh:121
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
Dwm::Ipv4TcpPayload class definition.
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: DwmBZ2Readable.hh:57
Dwm::Ipv4PacketHeader class definition.
Dwm::Socket class definition.
uint8_t DataOffset() const
The number of 32 bit words in the TCP header.
~Ipv4TcpPacket()
Destructor. If we own the underlying data, we free it.
Dwm::BZ2Writable pure virtual class definition.
Encapsulates a TCP packet, including the IP header.
Definition: DwmIpv4TcpPacket.hh:65
Ipv4TcpPacket()
Constructor.
std::string Payload() const
Returns the payload as a string.
Definition: DwmIpv4TcpPacket.hh:110
std::string ToString() const
Returns the payload as a string.
uint32_t StreamedLength() const
Returns the numbers of bytes that should be written if one of the Write() members is called...
Definition: DwmBZ2IO.hh:67
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmGZReadable.hh:57
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
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::istream & Read(std::istream &is)
Reads from an istream. Returns the istream.
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 BZRead(BZFILE *bzf)
Reads the packet from the given BZFILE bzf.
Encapsulates a TCP header.
Definition: DwmIpv4TcpHeader.hh:69
bool GetPayload(T &payload) const
Copies the payload into payload.
Definition: DwmIpv4TcpPayload.hh:127
Encapsulates an IPv4 packet header.
Definition: DwmIpv4PacketHeader.hh:65