bootp_callouts.cc 2.88 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
// Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the End User License
// Agreement. See COPYING file in the premium/ directory.

#include <config.h>

#include <bootp_log.h>
#include <hooks/hooks.h>
#include <dhcp/pkt4.h>

using namespace isc;
using namespace isc::bootp;
using namespace isc::dhcp;
using namespace isc::hooks;
using namespace isc::log;

// Functions accessed by the hooks framework use C linkage to avoid the name
// mangling that accompanies use of the C++ compiler as well as to avoid
// issues related to namespaces.
extern "C" {

23
/// @brief This callout is called at the "buffer4_receive" hook.
24
25
///
/// Ignore DHCP and BOOTREPLY messages.
26
/// Remaining packets should be BOOTP requests so add the BOOTP client class,
27
28
29
30
///
/// @param handle CalloutHandle.
///
/// @return 0 upon success, non-zero otherwise.
31
int buffer4_receive(CalloutHandle& handle) {
32
33
34
35
36
    // Get the received unpacked message.
    Pkt4Ptr query;
    handle.getArgument("query4", query);

    try {
37
38
39
40
41
        // Take a copy and unpack it.
        Pkt4Ptr copy(new Pkt4(&query->data_[0], query->data_.size()));
        copy->unpack();

        if (copy->getType() != DHCP_NOTYPE) {
42
43
44
            // DHCP query.
            return (0);
        }
45
        if (copy->getOp() == BOOTREPLY) {
46
47
48
49
50
            // BOOTP response.
            return (0);
        }
        // BOOTP query.
        query->addClass("BOOTP");
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        LOG_DEBUG(bootp_logger, DBGLVL_TRACE_BASIC, BOOTP_ADDED_CLASS)
            .arg(query->getLabel());
    } catch (const std::exception&) {
        // Got an error. The query shall very likely be dropped later.
        // Note this covers malformed DHCP packets where the message
        // type option can't be unpacked, and RFC 951 BOOTP.
    }

    return (0);
}

/// @brief This callout is called at the "pkt4_receive" hook.
///
/// If in the BOOTP class add a DHCPREQUEST dhcp-message-type.
///
/// @param handle CalloutHandle.
///
/// @return 0 upon success, non-zero otherwise.
int pkt4_receive(CalloutHandle& handle) {
    // Get the received unpacked message.
    Pkt4Ptr query;
    handle.getArgument("query4", query);

    try {
        if (!query->inClass("BOOTP")) {
            return (0);
        }
78
        query->setType(DHCPREQUEST);
79
        LOG_DEBUG(bootp_logger, DBGLVL_TRACE_BASIC, BOOTP_ADDED_MSGTYPE)
80
            .arg(query->getLabel());
81
82
    } catch (const std::exception&) {
        // Got an error (should not happen).
83
84
85
86
87
88
89
90
91
    }

    return (0);
}

/// @brief This function is called when the library is loaded.
///
/// @param handle library handle (unused).
/// @return always 0.
92
int load(LibraryHandle& /* handle */) {
93
94
95
96
97
98
99
100
101
102
103
104
105
    LOG_INFO(bootp_logger, BOOTP_LOAD);
    return (0);
}

/// @brief This function is called when the library is unloaded.
///
/// @return always 0.
int unload() {
    LOG_INFO(bootp_logger, BOOTP_UNLOAD);
    return (0);
}

} // end extern "C"