ddad6a5cc542a84c953d535717856f726c0bcce7
[pingcheck] / src / icmp / icmppacketfactory.h
1 /*
2  The software in this package is distributed under the GNU General
3  Public License version 2 (with a special exception described below).
4
5  A copy of GNU General Public License (GPL) is included in this distribution,
6  in the file COPYING.GPL.
7
8  As a special exception, if other files instantiate templates or use macros
9  or inline functions from this file, or you compile this file and link it
10  with other works to produce a work based on this file, this file
11  does not by itself cause the resulting work to be covered
12  by the GNU General Public License.
13
14  However the source code for this file must still be made available
15  in accordance with section (3) of the GNU General Public License.
16
17  This exception does not invalidate any other reasons why a work based
18  on this file might be covered by the GNU General Public License.
19
20  Christian Herdtweck, Intra2net AG 2015
21  Based on an example in Boost Documentation (by Christopher M. Kohlhoff)
22  and adaptation by Guilherme M. Ferreira
23  */
24
25 #ifndef ICMP_PACKET_FACTORY_H
26 #define ICMP_PACKET_FACTORY_H
27
28 #include <stdint.h>
29
30 #include <iostream>
31
32 #include <boost/asio.hpp>
33
34 #include "icmp/icmppacket.h"
35
36
37 enum DumpMode {
38     DUMP_NEVER = 0,
39     DUMP_IF_ERROR = 1,
40     DUMP_ALWAYS = 2
41 };
42
43 //-----------------------------------------------------------------------------
44 // IcmpPacketFactory
45 //-----------------------------------------------------------------------------
46
47 /**
48  * @brief Class which constructs ICMP Packets, hiding from the class users the
49  * underlying details on how to build each packet.
50  *
51  * @param dump_mode: 0 to never dump, 1 to dump in case of parsing trouble,
52  *                   2 to always dump
53  */
54 class IcmpPacketFactory
55 {
56 public:
57     /// directory and file name start used to dump packets;
58     /// will be concatenated with time and pattern that ensures unique file name
59     static std::string DumpFilePrefix;
60     static DumpMode PacketDumpMode;
61
62     static void dump_packet(const std::string &data);
63     static void dump_packet(const IcmpPacket &packet);
64
65     static IcmpPacketItem create_icmp_packet(
66             const boost::asio::ip::icmp::socket::protocol_type &protocol,
67             std::istream &is
68     );
69     static IcmpPacketItem create_icmp_packet_echo_request(
70             const boost::asio::ip::icmp::socket::protocol_type &protocol,
71             const uint16_t identifier,
72             const uint16_t sequence_number
73     );
74
75 private:
76     static int create_dump_file(const time_t &capture_time);
77 };
78
79 #endif // ICMP_PACKET_FACTORY_H