mirror of
https://github.com/mfontanini/libtins
synced 2026-01-27 20:24:26 +01:00
Added Sniffer class. Added a constructor to eery PDU subclass which creates an instance of the PDU from a byte array.
This commit is contained in:
45
src/tcp.cpp
45
src/tcp.cpp
@@ -19,6 +19,7 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#ifndef WIN32
|
||||
@@ -41,6 +42,18 @@ Tins::TCP::TCP(uint16_t dport, uint16_t sport) : PDU(IPPROTO_TCP), _options_size
|
||||
this->check(0);
|
||||
}
|
||||
|
||||
Tins::TCP::TCP(const uint8_t *buffer, uint32_t total_sz) : PDU(IPPROTO_TCP) {
|
||||
if(total_sz < sizeof(tcphdr))
|
||||
throw std::runtime_error("Not enought size for an TCP header in the buffer.");
|
||||
std::memcpy(&_tcp, buffer, sizeof(tcphdr));
|
||||
|
||||
/* Options... */
|
||||
|
||||
total_sz -= sizeof(tcphdr);
|
||||
if(total_sz)
|
||||
inner_pdu(new RawPDU(buffer + sizeof(tcphdr), total_sz));
|
||||
}
|
||||
|
||||
Tins::TCP::~TCP() {
|
||||
for(unsigned i(0); i < _options.size(); ++i)
|
||||
delete[] _options[i].data;
|
||||
@@ -92,6 +105,38 @@ void Tins::TCP::set_timestamp(uint32_t value, uint32_t reply) {
|
||||
add_option(TSOPT, 8, (uint8_t*)&buffer);
|
||||
}
|
||||
|
||||
uint8_t Tins::TCP::get_flag(Flags tcp_flag) {
|
||||
switch(tcp_flag) {
|
||||
case FIN:
|
||||
return _tcp.fin;
|
||||
break;
|
||||
case SYN:
|
||||
return _tcp.syn;
|
||||
break;
|
||||
case RST:
|
||||
return _tcp.rst;
|
||||
break;
|
||||
case PSH:
|
||||
return _tcp.psh;
|
||||
break;
|
||||
case ACK:
|
||||
return _tcp.ack;
|
||||
break;
|
||||
case URG:
|
||||
return _tcp.urg;
|
||||
break;
|
||||
case ECE:
|
||||
return _tcp.ece;
|
||||
break;
|
||||
case CWR:
|
||||
return _tcp.cwr;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
void Tins::TCP::set_flag(Flags tcp_flag, uint8_t value) {
|
||||
switch(tcp_flag) {
|
||||
case FIN:
|
||||
|
||||
Reference in New Issue
Block a user