perf_pkt6.h 4.86 KB
Newer Older
1
// Copyright (C) 2012-2015,2017 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6

7 8
#ifndef PERF_PKT6_H
#define PERF_PKT6_H
9 10

#include <time.h>
11
#include <boost/shared_ptr.hpp>
12 13
#include <dhcp/pkt6.h>

14
#include "localized_option.h"
15
#include "pkt_transform.h"
16

17 18 19 20 21
namespace isc {
namespace perfdhcp {

/// \brief PerfPkt6 (DHCPv6 packet)
///
22 23 24 25 26 27 28 29 30 31
/// This class extends the functionality of \ref isc::dhcp::Pkt6 by
/// adding the ability to specify an options offset in the DHCP message
/// and so override the options' contents. This is particularly useful when we
/// create a packet object using a template file (i.e. do not build it
/// dynamically). The client class should read the data from the template file
/// and pass it to this class as a buffer.
///
/// The contents of such packet can be later partially replaced: in particular,
/// selected options and the transaction ID can be altered. (The transaction
/// ID and its offset in the template file is passed via the constructor.)
32
///
33 34 35
/// In order to replace the contents of options, the client class has to
/// create a collection of \ref LocalizedOption by adding them using
/// \ref dhcp::Pkt6::addOption.
36
///
37 38 39
/// \note If you don't use template files, simply use constructors
/// inherited from parent class and the \ref isc::dhcp::Option type instead.

40 41
class PerfPkt6 : public dhcp::Pkt6 {
public:
42

43 44
    /// Localized option pointer type.
    typedef boost::shared_ptr<LocalizedOption> LocalizedOptionPtr;
45

46 47
    /// \brief Constructor, used to create messages from packet
    /// template files.
48
    ///
49 50 51
    /// Creates a new DHCPv6 message using the provided buffer.
    /// The transaction ID and its offset are specified via this
    /// constructor. The transaction ID is stored in outgoing message
52 53 54 55
    /// when client class calls \ref PerfPkt6::rawPack. Transaction id
    /// offset value is used for incoming and outgoing messages to
    /// identify transaction ID field's position in incoming and outgoing
    /// messages.
56
    ///
57
    /// \param buf buffer holding contents of the message (this can
58 59 60
    /// be directly read from template file).
    /// \param len length of the data in the buffer.
    /// \param transid_offset transaction id offset in a message.
61
    /// \param transid transaction id to be stored in outgoing message.
62
    PerfPkt6(const uint8_t* buf,
63
             size_t len,
64 65
             size_t transid_offset = 1,
             uint32_t transid = 0);
66 67 68

    /// \brief Returns transaction id offset in packet buffer
    ///
69
    /// \return Transaction ID offset in the packet buffer.
70
    size_t getTransidOffset() const { return transid_offset_; };
71

72
    /// \brief Prepares on-wire format from raw buffer
73
    ///
74 75
    /// The method copies the buffer provided in constructor to the
    /// output buffer and replaces the transaction ID and selected
76
    /// options with new data.
77
    ///
78
    /// \note Use this method to prepare an on-wire DHCPv6 message
79
    /// when you use template packets that require replacement
80
    /// of selected options' contents before sending.
81
    ///
82
    /// \return false ID pack operation failed.
83
    bool rawPack();
84 85 86 87

    /// \brief Handles limited binary packet parsing for packets with
    /// custom offsets of options and transaction id
    ///
88
    /// This method handles the parsing of packets that have custom offsets
89
    /// of options or transaction ID. Use
90
    /// \ref isc::dhcp::Pkt4::addOption to specify which options to parse.
91
    /// Options should be of the \ref isc::perfdhcp::LocalizedOption
92 93
    /// type with offset values provided. Each added option will
    /// be updated with actual data read from the binary packet buffer.
94
    ///
95
    /// \return false if unpack operation failed.
96 97
    bool rawUnpack();

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    /// \brief Replace contents of buffer with data.
    ///
    /// Function replaces part of the buffer with data from vector.
    ///
    /// \param dest_pos position in buffer where data is replaced.
    /// \param first beginning of data range in source vector.
    /// \param last end of data range in source vector.
    void writeAt(size_t dest_pos,
                 std::vector<uint8_t>::iterator first,
                 std::vector<uint8_t>::iterator last);

    /// \brief Replace contents of buffer with value.
    ///
    /// Function replaces part of buffer with value.
    ///
    /// \param dest_pos position in buffer where value is
    /// to be written.
    /// \param val value to be written.
    template<typename T>
    void writeValueAt(size_t dest_pos, T val) {
        PktTransform::writeValueAt<T>(data_, dest_pos, val);
    }

121
private:
122
    size_t transid_offset_;      ///< transaction id offset
123 124 125

};

126 127
typedef boost::shared_ptr<PerfPkt6> PerfPkt6Ptr;

128 129 130
} // namespace perfdhcp
} // namespace isc

131
#endif // PERF_PKT6_H