mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Merge branch 'master' of ssh://git.code.sf.net/p/libtins/code
This commit is contained in:
133
include/ethernet.h
Normal file
133
include/ethernet.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#ifndef __ETHERNET_H
|
||||
#define __ETHERNET_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "pdu.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
/**
|
||||
* \brief Class representing an ethernet IEEE 802.3 packet
|
||||
*/
|
||||
class Ethernet : public PDU {
|
||||
|
||||
public:
|
||||
/**
|
||||
* \brief Constructor for creating an ethernet PDU
|
||||
*
|
||||
* Constructor that builds an ethernet PDU taking the destination's
|
||||
* and source's MAC.
|
||||
*
|
||||
* \param mac_dst uint8_t array of 6 bytes containing the destination's MAC.
|
||||
* \param mac_src uint8_t array of 6 bytes containing the source's MAC.
|
||||
*/
|
||||
Ethernet(const uint8_t mac_dst[6], const uint8_t mac_src[6], PDU* child = 0);
|
||||
|
||||
/* Getters */
|
||||
/**
|
||||
* \brief Getter for the destination's mac address.
|
||||
*
|
||||
* \return Returns the destination's mac address as a constant uint8_t pointer.
|
||||
*/
|
||||
inline const uint8_t* dst_mac() const { return this->header.dst_mac; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the source's mac address.
|
||||
*
|
||||
* \return Returns the source's mac address as a constant uint8_t pointer.
|
||||
*/
|
||||
inline const uint8_t* src_mac() const { return this->header.src_mac; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the CRC value.
|
||||
*
|
||||
* \return Returns the CRC.
|
||||
*/
|
||||
inline uint32_t crc() const { return this->_crc; }
|
||||
|
||||
/* Setters */
|
||||
/**
|
||||
* \brief Setter for the destination's MAC.
|
||||
*
|
||||
* \param new_dst_mac uint8_t array of 6 bytes containing the new destination's MAC.
|
||||
*/
|
||||
void dst_mac(uint8_t new_dst_mac[6]);
|
||||
|
||||
/**
|
||||
* \brief Setter for the source's MAC.
|
||||
*
|
||||
* \param new_src_mac uint8_t array of 6 bytes containing the new source's MAC.
|
||||
*/
|
||||
void src_mac(uint8_t new_src_mac[6]);
|
||||
|
||||
/**
|
||||
* \brief Setter for the CRC value.
|
||||
*
|
||||
* \param new_crc uint32_t containing the new CRC value.
|
||||
*/
|
||||
void crc(uint32_t new_crc);
|
||||
|
||||
/* Virtual methods */
|
||||
/**
|
||||
* \brief Returns the ethernet frame's header length.
|
||||
*
|
||||
* \return An uint32_t with the header's size.
|
||||
* \sa PDU::header_size()
|
||||
*/
|
||||
uint32_t header_size() const;
|
||||
|
||||
/**
|
||||
* \brief Returns the ethernet frame's trailer length.
|
||||
*
|
||||
* \return An uint32_t with the trailer's size.
|
||||
* \sa PDU::trailer_size()
|
||||
*/
|
||||
uint32_t trailer_size() const;
|
||||
|
||||
/**
|
||||
* \sa PDU::send()
|
||||
*/
|
||||
bool send(PacketSender* sender);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Struct that represents the IEEE 802.3 header
|
||||
*/
|
||||
struct ethernet_header {
|
||||
uint8_t dst_mac[6];
|
||||
uint8_t src_mac[6];
|
||||
uint16_t payload_type;
|
||||
};
|
||||
|
||||
ethernet_header header;
|
||||
uint32_t _crc;
|
||||
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, PDU *parent);
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,19 +1,19 @@
|
||||
/*
|
||||
* libtins is a net packet wrapper library for crafting and
|
||||
* 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
|
||||
@@ -30,19 +30,30 @@
|
||||
namespace Tins {
|
||||
class PDU;
|
||||
|
||||
/**
|
||||
* \brief Class that enables sending the created PDUs
|
||||
*
|
||||
* PacketSender class is responsible for sending the packets using the
|
||||
* correct PDU layer. It is responsible for opening the raw sockets.
|
||||
*/
|
||||
class PacketSender {
|
||||
public:
|
||||
/**
|
||||
* \brief Constructor for PacketSender objects.
|
||||
*/
|
||||
PacketSender();
|
||||
|
||||
/* Opens a socket, using flag as protocol family.
|
||||
* Return true if it was possible to open it(or it was already open),
|
||||
* false otherwise. */
|
||||
|
||||
|
||||
bool open_l2_socket();
|
||||
|
||||
bool open_l3_socket();
|
||||
|
||||
bool close_socket(uint32_t flag);
|
||||
|
||||
bool send(PDU* pdu);
|
||||
|
||||
bool send_l2(PDU *pdu);
|
||||
|
||||
bool send_l3(PDU *pdu, const struct sockaddr* link_addr, uint32_t len_link_addr);
|
||||
private:
|
||||
enum SocketType {
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
/*
|
||||
* libtins is a net packet wrapper library for crafting and
|
||||
* 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
|
||||
@@ -42,6 +42,8 @@ namespace Tins {
|
||||
inline uint32_t net_to_host_s(uint16_t address) {
|
||||
return ((address & 0xff00) >> 8) | ((address & 0x00ff) << 8);
|
||||
}
|
||||
|
||||
uint32_t crc32(uint8_t* data, uint32_t data_size);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
79
src/ethernet.cpp
Normal file
79
src/ethernet.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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 <cassert>
|
||||
#include <cstring>
|
||||
|
||||
#include <net/ethernet.h>
|
||||
|
||||
#include "ethernet.h"
|
||||
#include "utils.h"
|
||||
|
||||
Tins::Ethernet::Ethernet(const uint8_t mac_dst[6], const uint8_t mac_src[6], PDU* child) : PDU(ETHERTYPE_IP, child) {
|
||||
|
||||
}
|
||||
|
||||
void Tins::Ethernet::dst_mac(uint8_t new_dst_mac[6]) {
|
||||
memcpy(this->header.dst_mac, new_dst_mac, 6);
|
||||
}
|
||||
|
||||
void Tins::Ethernet::src_mac(uint8_t new_src_mac[6]) {
|
||||
memcpy(this->header.src_mac, new_src_mac, 6);
|
||||
}
|
||||
|
||||
void Tins::Ethernet::crc(uint32_t new_crc) {
|
||||
this->_crc = new_crc;
|
||||
}
|
||||
|
||||
uint32_t Tins::Ethernet::header_size() const {
|
||||
return sizeof(ethernet_header);
|
||||
}
|
||||
|
||||
uint32_t Tins::Ethernet::trailer_size() const {
|
||||
return sizeof(uint32_t);
|
||||
}
|
||||
|
||||
bool Tins::Ethernet::send(PacketSender* sender) {
|
||||
return false; //return sender->send_l2(this);
|
||||
}
|
||||
|
||||
void Tins::Ethernet::write_serialization(uint8_t *buffer, uint32_t total_sz, PDU *parent) {
|
||||
uint32_t my_sz = header_size() + trailer_size();
|
||||
uint32_t new_flag;
|
||||
assert(total_sz >= my_sz);
|
||||
/*
|
||||
if (this->inner_pdu()) {
|
||||
new_flag = this->inner_pdu()->flag();
|
||||
|
||||
switch (new_flag) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
/* This should be replaced by a switch statement */
|
||||
this->header.payload_type = ETHERTYPE_IP;
|
||||
this->_crc = Tins::Utils::crc32(buffer, total_sz - sizeof(uint32_t));
|
||||
|
||||
memcpy(buffer, &this->header, sizeof(ethernet_header));
|
||||
*((uint32_t*)&buffer[total_sz - sizeof(uint32_t)]) = this->_crc;
|
||||
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
/*
|
||||
* libtins is a net packet wrapper library for crafting and
|
||||
* 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
|
||||
@@ -41,6 +41,13 @@ Tins::PacketSender::PacketSender() : _sockets(SOCKETS_END, INVALID_RAW_SOCKET) {
|
||||
|
||||
}
|
||||
|
||||
bool Tins::PacketSender::open_l2_socket() {
|
||||
|
||||
/* To be implemented */
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Tins::PacketSender::open_l3_socket() {
|
||||
if(_sockets[IP_SOCKET] != INVALID_RAW_SOCKET)
|
||||
return true;
|
||||
@@ -68,6 +75,13 @@ bool Tins::PacketSender::send(PDU *pdu) {
|
||||
return pdu->send(this);
|
||||
}
|
||||
|
||||
bool Tins::PacketSender::send_l2(PDU *pdu) {
|
||||
|
||||
/* To be implemented */
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Tins::PacketSender::send_l3(PDU *pdu, const struct sockaddr* link_addr, uint32_t len_link_addr) {
|
||||
bool ret_val = true;
|
||||
if(!open_l3_socket())
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
/*
|
||||
* libtins is a net packet wrapper library for crafting and
|
||||
* 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
|
||||
@@ -69,3 +69,20 @@ uint32_t Tins::Utils::resolve_ip(const string &to_resolve) {
|
||||
return 0;
|
||||
return ((struct in_addr**)data->h_addr_list)[0]->s_addr;
|
||||
}
|
||||
|
||||
uint32_t Tins::Utils::crc32(uint8_t* data, uint32_t data_size) {
|
||||
uint32_t i, crc = 0;
|
||||
static uint32_t crc_table[] = {
|
||||
0x4DBDF21C, 0x500AE278, 0x76D3D2D4, 0x6B64C2B0,
|
||||
0x3B61B38C, 0x26D6A3E8, 0x000F9344, 0x1DB88320,
|
||||
0xA005713C, 0xBDB26158, 0x9B6B51F4, 0x86DC4190,
|
||||
0xD6D930AC, 0xCB6E20C8, 0xEDB71064, 0xF0000000
|
||||
};
|
||||
|
||||
for (i = 0; i < data_size; ++i) {
|
||||
crc = (crc >> 4) ^ crc_table[(crc ^ data[i]) & 0x0F];
|
||||
crc = (crc >> 4) ^ crc_table[(crc ^ (data[i] >> 4)) & 0x0F];
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user