1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Added some helper functions to set options to DHCP PDU. Added LGPL notice to dhcp.cpp

This commit is contained in:
Matias Fontanini
2011-08-17 16:20:02 -03:00
parent 4dfa4d70eb
commit 24e314283f
3 changed files with 68 additions and 12 deletions

View File

@@ -29,13 +29,15 @@
namespace Tins {
/** \brief Class that represents the DHCP PDU.
/**
* \brief Class that represents the DHCP PDU.
*
* The end option is added automatically at the end of the option list.
*/
class DHCP : public BootP {
public:
/** \brief DHCP flags.
/**
* \brief DHCP flags.
*/
enum Flags {
DHCPDISCOVER = 1,
@@ -48,7 +50,8 @@ namespace Tins {
DHCPINFORM = 8
};
/** \brief DHCP options enum.
/**
* \brief DHCP options enum.
*/
enum Options {
PAD,
@@ -123,7 +126,8 @@ namespace Tins {
END = 255
};
/** \brief DHCP options struct.
/**
* \brief DHCP options struct.
*/
struct DHCPOption {
uint8_t option, length;
@@ -132,14 +136,16 @@ namespace Tins {
DHCPOption(uint8_t opt, uint8_t len, uint8_t *val);
};
/** \brief Creates an instance of DHCP.
/**
* \brief Creates an instance of DHCP.
*
* This sets the hwtype and hlen fields to match the ethernet
* type and length.
*/
DHCP();
/** \brief Adds a new option to this DHCP PDU.
/**
* \brief Adds a new option to this DHCP PDU.
*
* This copies the value buffer. Adding options may fail if
* there's not enough size to hold a new option.
@@ -150,10 +156,26 @@ namespace Tins {
*/
bool add_option(Options opt, uint8_t len, uint8_t *val);
/** \brief Adds a type option the the option list.
/**
* \brief Adds a type option the the option list.
* \param type The type of this DHCP PDU.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
void add_type_option(Flags type);
bool add_type_option(Flags type);
/**
* \brief Adds a server identifier option.
* \param ip The ip of the server.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_server_identifier(uint32_t ip);
/**
* \brief Adds an IP address lease time option.
* \param time The lease time.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_lease_time(uint32_t time);
/** \brief Getter for the options list.
* \return The option list.
@@ -166,7 +188,8 @@ namespace Tins {
*/
PDUType pdu_type() const { return PDU::UDP; }
/** \brief Getter for the header size.
/**
* \brief Getter for the header size.
* \return Returns the BOOTP header size.
* \sa PDU::header_size
*/

View File

@@ -1,3 +1,24 @@
/*
* libtins is a net packet wrapper library for crafting and
* interpreting sniffed packets.
*
* Copyright (C) 2011 Nasel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <cstring>
#include <cassert>
#include <iostream> //borrame
@@ -28,8 +49,17 @@ bool Tins::DHCP::add_option(Options opt, uint8_t len, uint8_t *val) {
return true;
}
void Tins::DHCP::add_type_option(Flags type) {
add_option(DHCP_MESSAGE_TYPE, 1, (uint8_t*)&type);
bool Tins::DHCP::add_type_option(Flags type) {
return add_option(DHCP_MESSAGE_TYPE, 1, (uint8_t*)&type);
}
bool Tins::DHCP::add_server_identifier(uint32_t ip) {
return add_option(DHCP_SERVER_IDENTIFIER, 4, (uint8_t*)&ip);
}
bool Tins::DHCP::add_lease_time(uint32_t time) {
time = Utils::net_to_host_l(time);
return add_option(DHCP_LEASE_TIME, 4, (uint8_t*)&time);
}
uint32_t Tins::DHCP::header_size() const {
@@ -43,10 +73,12 @@ void Tins::DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
for(std::list<DHCPOption>::const_iterator it = _options.begin(); it != _options.end(); ++it) {
*(ptr++) = it->option;
*(ptr++) = it->length;
std::memcpy(ptr++, it->value, it->length);
std::memcpy(ptr, it->value, it->length);
ptr += it->length;
}
result[_size-1] = END;
vend(result, _size);
BootP::write_serialization(buffer, total_sz, parent);
delete[] result;
}

View File

@@ -58,6 +58,7 @@ void Tins::IP::init_ip_fields() {
_ip.version = 4;
_ip.ihl = sizeof(iphdr) / sizeof(uint32_t);
_ip.ttl = DEFAULT_TTL;
_ip.id = Utils::net_to_host_s(1);
_options_size = 0;
_padded_options_size = 0;
}