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

Add MPLS PDU and hook it up with ICMP extensions

This commit is contained in:
Matias Fontanini
2015-12-31 05:57:18 -08:00
parent 4ba9085eeb
commit 7bffa7801d
14 changed files with 434 additions and 1 deletions

View File

@@ -78,6 +78,7 @@ namespace Tins {
SPRITE = 0x0500, /* Sprite */
IP = 0x0800, /* IP */
ARP = 0x0806, /* Address resolution */
MPLS = 0x8847, /* MPLS */
REVARP = 0x8035, /* Reverse ARP */
AT = 0x809B, /* AppleTalk protocol */
AARP = 0x80F3, /* AppleTalk ARP */

View File

@@ -10,6 +10,8 @@
namespace Tins {
class MPLS;
/**
* \brief Class that represents an ICMP extension object
*/
@@ -207,10 +209,20 @@ public:
/**
* \brief Adds an extension to this structure
*
* \param extension The extension to be added;
* \param extension The extension to be added
*/
void add_extension(const ICMPExtension& extension);
/**
* \brief Adds an MPLS extension to this structure
*
* This will construct an extension using the provided MPLS packet as
* its payload. The class and type fields will be set appropriately.
*
* \param extension The MPLS payload to be used for the new extension
*/
void add_extension(MPLS& mpls);
/**
* \brief Gets the size of this ICMP extensions structure
*

View File

@@ -4,6 +4,7 @@
#include <stdint.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include "exceptions.h"
#include "ip_address.h"
#include "ipv6_address.h"
@@ -36,6 +37,10 @@ public:
: buffer_(buffer), size_(total_sz) {
}
InputMemoryStream(const std::vector<uint8_t>& data)
: buffer_(&data[0]), size_(data.size()) {
}
void skip(uint32_t size) {
buffer_ += size;
size_ -= size;

154
include/tins/mpls.h Normal file
View File

@@ -0,0 +1,154 @@
/*
* Copyright (c) 2014, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef TINS_MPLS_H
#define TINS_MPLS_H
#include "pdu.h"
#include "endianness.h"
#include "macros.h"
#include "small_uint.h"
namespace Tins {
class ICMPExtension;
/**
* \brief Represents an MPLS PDU
*/
class MPLS : public PDU {
public:
/**
* \brief This PDU's flag.
*/
static const PDU::PDUType pdu_flag = PDU::MPLS;
/**
* \brief Default constructor
*/
MPLS();
/**
* \brief Construct an MPLS layer from an ICMP extension
*
* This will use the extension's payload to build this packet.
* The extension's class and type are not checked.
*
*/
MPLS(const ICMPExtension& extension);
/**
* \brief Constructor from buffer
*/
MPLS(const uint8_t* buffer, uint32_t total_sz);
/**
* \brief Getter for the label field.
*/
small_uint<20> label() const {
return (Endian::be_to_host(header_.label_high) << 4) |
((header_.label_low_and_bottom >> 4) & 0xf);
}
/**
* \brief Getter for the bottom of the stack field.
*/
small_uint<1> bottom_of_stack() const {
return header_.label_low_and_bottom & 0x1;
}
/**
* \brief Getter for the ttl field.
*/
uint8_t ttl() const {
return header_.ttl;
}
/**
* \brief Setter for the label field
*
* \param value The new label field value
*/
void label(small_uint<20> value);
/**
* \brief Setter for the bottom of the stack field
*
* Note that if this MPLS layer is somewhere between an Ethernet and IP
* layers, the bottom of the stack field will be overriden and set
* automatically. You should only set this field when constructing ICMP
* extensions.
*
* \param value The new bottom of the stack field value
*/
void bottom_of_stack(small_uint<1> value);
/**
* \brief Setter for the ttl field
*
* \param value The new ttl field value
*/
void ttl(uint8_t value);
/**
* \brief Returns the MPLS frame's header length.
*
* \return The header's size.
* \sa PDU::header_size()
*/
uint32_t header_size() const;
/**
* \brief Getter for the PDU's type.
* \sa PDU::pdu_type
*/
PDUType pdu_type() const { return pdu_flag; }
/**
* \sa PDU::clone
*/
MPLS *clone() const {
return new MPLS(*this);
}
private:
TINS_BEGIN_PACK
struct mpls_header {
uint16_t label_high;
uint8_t label_low_and_bottom;
uint8_t ttl;
} TINS_END_PACK;
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
mpls_header header_;
};
} // Tins
#endif // TINS_MPLS_H

View File

@@ -129,6 +129,7 @@ namespace Tins {
RAW,
ETHERNET_II,
IEEE802_3,
DOT3 = IEEE802_3,
RADIOTAP,
DOT11,
DOT11_ACK,
@@ -177,6 +178,7 @@ namespace Tins {
IPSEC_AH,
IPSEC_ESP,
PKTAP,
MPLS,
USER_DEFINED_PDU = 1000
};

View File

@@ -45,6 +45,7 @@
#include "dot3.h"
#include "ip.h"
#include "ipv6.h"
#include "mpls.h"
#include "packet_sender.h"
#include "packet_writer.h"
#include "pdu.h"