mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 21:14:28 +01:00
Working ethernet II PDU. Fixed bugs in IP and added checksum calculation. Added layer2 sending to PacketSender
This commit is contained in:
@@ -22,42 +22,69 @@
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
#include <net/ethernet.h>
|
||||
#ifndef WIN32
|
||||
#include <net/ethernet.h>
|
||||
#include <netpacket/packet.h>
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
|
||||
#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) {
|
||||
Tins::Ethernet::Ethernet(const uint8_t* mac_dst, const uint8_t* mac_src, const std::string& iface, PDU* child) throw (std::runtime_error) : PDU(ETHERTYPE_IP, child) {
|
||||
|
||||
this->dst_mac(mac_dst);
|
||||
this->src_mac(mac_src);
|
||||
this->iface(iface);
|
||||
|
||||
}
|
||||
|
||||
void Tins::Ethernet::dst_mac(uint8_t new_dst_mac[6]) {
|
||||
Tins::Ethernet::Ethernet(const uint8_t* mac_dst, const uint8_t* mac_src, uint32_t iface_index, PDU* child) : PDU(ETHERTYPE_IP, child) {
|
||||
this->dst_mac(mac_dst);
|
||||
this->src_mac(mac_src);
|
||||
this->iface(iface_index);
|
||||
}
|
||||
|
||||
void Tins::Ethernet::dst_mac(const uint8_t* new_dst_mac) {
|
||||
memcpy(this->header.dst_mac, new_dst_mac, 6);
|
||||
}
|
||||
|
||||
void Tins::Ethernet::src_mac(uint8_t new_src_mac[6]) {
|
||||
void Tins::Ethernet::src_mac(const uint8_t* new_src_mac) {
|
||||
memcpy(this->header.src_mac, new_src_mac, 6);
|
||||
}
|
||||
|
||||
void Tins::Ethernet::crc(uint32_t new_crc) {
|
||||
this->_crc = new_crc;
|
||||
void Tins::Ethernet::iface(uint32_t new_iface_index) {
|
||||
this->_iface_index = new_iface_index;
|
||||
}
|
||||
|
||||
void Tins::Ethernet::iface(const std::string& new_iface) throw (std::runtime_error) {
|
||||
if (!Tins::Utils::interface_id(new_iface, this->_iface_index)) {
|
||||
throw std::runtime_error("Invalid interface name!");
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
struct sockaddr_ll addr;
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = this->_iface_index;
|
||||
memcpy(&(addr.sll_addr), this->header.dst_mac, 6);
|
||||
|
||||
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
void Tins::Ethernet::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
|
||||
uint32_t my_sz = header_size();
|
||||
assert(total_sz >= my_sz);
|
||||
/*
|
||||
if (this->inner_pdu()) {
|
||||
@@ -70,10 +97,8 @@ void Tins::Ethernet::write_serialization(uint8_t *buffer, uint32_t total_sz, PDU
|
||||
}
|
||||
*/
|
||||
/* 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));
|
||||
this->header.payload_type = Utils::net_to_host_s(ETHERTYPE_IP);
|
||||
|
||||
memcpy(buffer, &this->header, sizeof(ethernet_header));
|
||||
*((uint32_t*)&buffer[total_sz - sizeof(uint32_t)]) = this->_crc;
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
uint16_t Tins::ICMP::global_id = 0, Tins::ICMP::global_seq = 0;
|
||||
|
||||
|
||||
@@ -149,7 +148,7 @@ Tins::PDU *Tins::ICMP::clone_packet(uint8_t *ptr, uint32_t total_sz) {
|
||||
}
|
||||
else
|
||||
child = new RawPDU(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr));
|
||||
|
||||
|
||||
}
|
||||
cloned = new ICMP(icmp_ptr);
|
||||
cloned->inner_pdu(child);
|
||||
|
||||
12
src/ip.cpp
12
src/ip.cpp
@@ -185,7 +185,7 @@ Tins::PDU *Tins::IP::recv_response(PacketSender *sender) {
|
||||
return sender->recv_l3(this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
|
||||
}
|
||||
|
||||
void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
|
||||
void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU* parent) {
|
||||
uint32_t my_sz = header_size();
|
||||
uint32_t new_flag;
|
||||
assert(total_sz >= my_sz);
|
||||
@@ -198,8 +198,16 @@ void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU
|
||||
new_flag = IPPROTO_IP;
|
||||
flag(new_flag);
|
||||
_ip.protocol = new_flag;
|
||||
_ip.tot_len = total_sz;
|
||||
_ip.tot_len = Utils::net_to_host_s(total_sz);
|
||||
_ip.ihl = my_sz / sizeof(uint32_t);
|
||||
if (parent && (_ip.check == 0)) {
|
||||
uint32_t checksum = PDU::do_checksum((uint8_t*)&_ip, ((uint8_t*)&_ip) + sizeof(iphdr));
|
||||
checksum += PDU::do_checksum(buffer + sizeof(iphdr), buffer + total_sz);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
_ip.check = Utils::net_to_host_s(~checksum);
|
||||
}
|
||||
|
||||
memcpy(buffer, &_ip, sizeof(iphdr));
|
||||
|
||||
/* IP Options here... */
|
||||
|
||||
@@ -45,9 +45,16 @@ Tins::PacketSender::PacketSender() : _sockets(SOCKETS_END, INVALID_RAW_SOCKET) {
|
||||
|
||||
bool Tins::PacketSender::open_l2_socket() {
|
||||
|
||||
/* To be implemented */
|
||||
if (_sockets[ETHER_SOCKET] != INVALID_RAW_SOCKET)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
||||
if (sock == -1)
|
||||
return false;
|
||||
|
||||
_sockets[ETHER_SOCKET] = sock;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Tins::PacketSender::open_l3_socket(SocketType type) {
|
||||
@@ -86,21 +93,28 @@ Tins::PDU *Tins::PacketSender::send_recv(PDU *pdu) {
|
||||
return pdu->recv_response(this);
|
||||
}
|
||||
|
||||
bool Tins::PacketSender::send_l2(PDU *pdu) {
|
||||
bool Tins::PacketSender::send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr) {
|
||||
|
||||
/* To be implemented */
|
||||
if(!open_l2_socket())
|
||||
return false;
|
||||
|
||||
return false;
|
||||
uint32_t sz;
|
||||
int sock = _sockets[ETHER_SOCKET];
|
||||
uint8_t *buffer = pdu->serialize(sz);
|
||||
bool ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_link_addr) != -1);
|
||||
delete[] buffer;
|
||||
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::PacketSender::recv_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type) {
|
||||
Tins::PDU *Tins::PacketSender::recv_l3(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr, SocketType type) {
|
||||
if(!open_l3_socket(type))
|
||||
return 0;
|
||||
uint8_t buffer[2048];
|
||||
int sock = _sockets[type];
|
||||
bool done = false;
|
||||
socklen_t addrlen = len_link_addr;
|
||||
|
||||
|
||||
while(!done) {
|
||||
ssize_t size = recvfrom(sock, buffer, 2048, 0, link_addr, &addrlen);
|
||||
if(size == -1)
|
||||
|
||||
18
src/pdu.cpp
18
src/pdu.cpp
@@ -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
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
|
||||
Tins::PDU::PDU(uint32_t flag, PDU *next_pdu) : _flag(flag), _inner_pdu(next_pdu) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
Tins::PDU::~PDU() {
|
||||
@@ -85,10 +85,10 @@ uint32_t Tins::PDU::pseudoheader_checksum(uint32_t source_ip, uint32_t dest_ip,
|
||||
source_ip = Utils::net_to_host_l(source_ip);
|
||||
dest_ip = Utils::net_to_host_l(dest_ip);
|
||||
uint16_t *ptr = (uint16_t*)&source_ip;
|
||||
|
||||
checksum += *ptr + ptr[1];
|
||||
|
||||
checksum += (uint32_t)(*ptr) + (uint32_t)(*(ptr+1));
|
||||
ptr = (uint16_t*)&dest_ip;
|
||||
checksum += *ptr + ptr[1];
|
||||
checksum += (uint32_t)(*ptr) + (uint32_t)(*(ptr+1));
|
||||
checksum += flag + len;
|
||||
return checksum;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#ifndef WIN32
|
||||
#include <netdb.h>
|
||||
#include <linux/if_packet.h>
|
||||
#include <net/if.h>
|
||||
#endif
|
||||
#include "utils.h"
|
||||
|
||||
@@ -36,7 +37,7 @@ using namespace std;
|
||||
|
||||
struct InterfaceCollector {
|
||||
set<string> ifaces;
|
||||
|
||||
|
||||
void operator() (struct ifaddrs *addr) {
|
||||
ifaces.insert(addr->ifa_name);
|
||||
}
|
||||
@@ -46,9 +47,9 @@ struct IPv4Collector {
|
||||
uint32_t ip;
|
||||
bool found;
|
||||
const char *iface;
|
||||
|
||||
|
||||
IPv4Collector(const char *interface) : ip(0), found(false), iface(interface) { }
|
||||
|
||||
|
||||
void operator() (struct ifaddrs *addr) {
|
||||
if(!found && addr->ifa_addr->sa_family == AF_INET && !strcmp(addr->ifa_name, iface)) {
|
||||
ip = ((struct sockaddr_in *)addr->ifa_addr)->sin_addr.s_addr;
|
||||
@@ -57,28 +58,13 @@ struct IPv4Collector {
|
||||
}
|
||||
};
|
||||
|
||||
struct InterfaceIDCollector {
|
||||
uint32_t id;
|
||||
bool found;
|
||||
const char *iface;
|
||||
|
||||
InterfaceIDCollector(const char *interface) : id(0), found(false), iface(interface) { }
|
||||
|
||||
void operator() (struct ifaddrs *addr) {
|
||||
if(!found && !strcmp(addr->ifa_name, iface)) {
|
||||
id = addr->ifa_flags;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct HWAddressCollector {
|
||||
uint8_t *result;
|
||||
bool found;
|
||||
const char *iface;
|
||||
|
||||
|
||||
HWAddressCollector(uint8_t *res, const char *interface) : result(res), found(false), iface(interface) { }
|
||||
|
||||
|
||||
void operator() (struct ifaddrs *addr) {
|
||||
if(!found && addr->ifa_addr->sa_family == AF_PACKET && !strcmp(addr->ifa_name, iface)) {
|
||||
memcpy(result, ((struct sockaddr_ll*)addr->ifa_addr)->sll_addr, 6);
|
||||
@@ -189,10 +175,10 @@ bool Tins::Utils::interface_hwaddr(const string &iface, uint8_t *buffer) {
|
||||
}
|
||||
|
||||
bool Tins::Utils::interface_id(const string &iface, uint32_t &id) {
|
||||
InterfaceIDCollector collector(iface.c_str());
|
||||
generic_iface_loop(collector);
|
||||
id = collector.id;
|
||||
return collector.found;
|
||||
|
||||
id = if_nametoindex(iface.c_str());
|
||||
return (((int32_t)id) != -1);
|
||||
|
||||
}
|
||||
|
||||
uint32_t Tins::Utils::crc32(uint8_t* data, uint32_t data_size) {
|
||||
|
||||
Reference in New Issue
Block a user