perf_pkt4.h 5.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#ifndef __PERF_PKT4_H
#define __PERF_PKT4_H

#include <time.h>
#include <boost/shared_ptr.hpp>
#include <dhcp/pkt4.h>

#include "localized_option.h"

namespace isc {
namespace perfdhcp {

/// \brief PerfPkt4 (DHCPv4 packet)
///
/// This class extends functionality of \ref isc::dhcp::Pkt4 by
/// adding ability to specify options offset in DHCP message
31 32 33 34 35 36 37 38 39 40 41 42
/// and override options' contents.
/// This is in particular useful when we create packet object using
/// template file (do not build it dynamically). Client class
/// should read data from template file and pass data to this class
/// as buffer.
/// Contents of such packet can be later partially replaced: preciselly
/// selected options and transaction id can be replaced.
/// Transaction id and its offset in template file is passed via
/// constructor.
/// In order to replace contents of options client class has to
/// create collection of \ref LocalizedOption by adding them using
/// \ref dhcp::Pkt4::addOption.
43 44 45 46 47 48 49 50 51 52
///
/// \note: if you don't use template files simply use constructors
/// inherited from parent class and isc::dhcp::Option type instead
///
class PerfPkt4 : public dhcp::Pkt4 {
public:

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

53 54 55 56 57 58 59 60 61 62
    /// \brief Constructor, used for outgoing and incoming messages
    ///
    /// This constructor initializes transaction id and
    /// transaction id offset of the packet with default
    /// values.
    ///
    /// \param buf buffer holding contents of the message.
    /// \param len length of the data in the buffer.
    PerfPkt4(const uint8_t* buf, size_t len);

63 64 65
    /// \brief Constructor, used for outgoing DHCP messages.
    ///
    /// Creates new DHCPv4 message using provided buffer.
66 67
    /// Transaction id and its offset are specified via this
    /// constructor. Transaction id is stored in outgoing message
68 69 70
    /// when client class calls \ref PerfPkt4::rawPack.
    ///
    /// \note This constructor should be used only for outgoing
71
    /// messages that are created from template files.
72
    ///
73
    /// \param buf buffer holding contents of the message (this can
74 75 76 77 78
    /// be directly read from template file).
    /// \param len length of the data in the buffer.
    /// \param transid_offset transaction id offset in outgoing message.
    /// \param transid transaction id to be stored in outgoing message.
    PerfPkt4(const uint8_t* buf,
79
             size_t len,
80 81 82 83 84
             size_t transid_offset,
             uint32_t transid);

    /// Constructor, used for incoming DHCP messages.
    ///
85 86 87 88 89
    /// Creates new DHCPv4 message using provided buffer.
    /// Client class may indicate which options are to be read
    /// from the buffer. Such options should be added to
    /// options collection using dhcp::Pkt4::addOption. Such
    /// options should be of \ref LocalizedOption class.
90
    ///
91
    /// Transaction id offset points to location where
92 93 94 95 96
    /// transaction id field is stored. The transaction id will
    /// be read from this location when PerfPkt4::rawUnpack is
    /// called. The transid_ class member will be updated accordingly.
    ///
    /// \note use this constructor only in case you want to create
97
    /// incoming DHCPv4 object from the raw buffer
98 99 100 101 102 103 104
    /// and you know options offsets. Options offsets are
    /// specified from perfdhcp command line by the user.
    ///
    /// \param buf pointer to a buffer of received packet content.
    /// \param len size of buffer of packet content.
    /// \param transid_offset transaction id offset in a message.
    PerfPkt4(const uint8_t* buf,
105
             size_t len,
106 107 108 109 110 111 112
             size_t transid_offset);

    /// \brief Returns transaction id offset in packet buffer
    ///
    /// return transaction id offset in packet buffer
    size_t getTransIdOffset() const { return transid_offset_; };

113
    /// \brief Prepares on-wire format from raw buffer.
114
    ///
115 116 117
    /// The method copies buffer provided in constructor to
    /// output buffer and replaces transaction id and selected
    /// options with new data.
118
    ///
119
    /// \note: Use this method to prepare on-wire DHCPv4 message
120 121 122
    /// when you use template packets that require replacement
    /// of selected options' contents before sending.
    ///
123
    /// \return false, id pack operation failed.
124 125 126 127 128
    bool rawPack();

    /// \brief Handles limited binary packet parsing for packets with
    /// custom offsets of options and transaction id
    ///
129 130
    /// Function handles parsing of packets that have custom offsets
    /// of options or transaction id. Use
131
    /// \ref isc::dhcp::Pkt4::addOption to specify which options to parse.
132 133 134
    /// Options should be of the: isc::perfdhcp::LocalizedOption
    /// type with offset values provided. Each added option will
    /// be updated with actual data read from the binary packet buffer.
135 136 137 138 139 140 141 142 143 144 145 146 147
    ///
    /// \return false, if unpack operation failed.
    bool rawUnpack();

private:
    size_t transid_offset_;      ///< transaction id offset

};

} // namespace perfdhcp
} // namespace isc

#endif // __PERF_PKT4_H