code cleanup
This commit is contained in:
parent
b880ae0cd3
commit
abf9dc565c
8 changed files with 70 additions and 130 deletions
|
@ -6,7 +6,7 @@ add_subdirectory(libraries)
|
|||
|
||||
set(LIBRARIES_DIR libraries)
|
||||
|
||||
set(RIDDLE_SRCS ${LIBRARIES_DIR}/libRiddle.cpp ${LIBRARIES_DIR}/libRiddle.h Riddle.cpp)
|
||||
set(RIDDLE_SRCS Riddle.cpp)
|
||||
set(CIGARETTE_SRCS ${LIBRARIES_DIR}/libCigarette.cpp ${LIBRARIES_DIR}/libCigarette.h Cigarette.cpp)
|
||||
set(RANGING_SRCS ${LIBRARIES_DIR}/libRanging.h ${LIBRARIES_DIR}/libRanging.cpp Ranging.cpp)
|
||||
set(PURSUER_SRCS ${LIBRARIES_DIR}/libPursuer.h ${LIBRARIES_DIR}/libPursuer.cpp Pursuer.cpp)
|
||||
|
|
|
@ -82,7 +82,7 @@ int main ( int argc, char **argv ) {
|
|||
packet* pkg = packet::factory ( r_packet );
|
||||
|
||||
|
||||
cout << "[" << std::dec << pkg->getEpoch() << " " << setfill ( '0' ) << std::setw ( 6 ) << pkg->getMillis() << "] Size: " << pkg->getPacketLength() << " byte" << endl;
|
||||
cout << "[" << std::dec << pkg->getEpoch() << " " << setfill ( '0' ) << setw ( 6 ) << pkg->getMillis() << "] Size: " << pkg->getPacketLength() << " byte" << endl;
|
||||
cout << " From " << pkg->getSenderMac().to_string() << " to "<< pkg->getTargetMac().to_string() << endl;
|
||||
cout << " EtherType: 0x" << std::hex << pkg->getEtherType() << " ("<< ether_type_decode ( pkg->getEtherType() ) << ")" << endl;
|
||||
cout << endl;
|
||||
|
|
3
Project-Riddle.kdev4
Normal file
3
Project-Riddle.kdev4
Normal file
|
@ -0,0 +1,3 @@
|
|||
[Project]
|
||||
Manager=KDevCMakeManager
|
||||
Name=Project-Riddle
|
30
Riddle.cpp
30
Riddle.cpp
|
@ -33,7 +33,7 @@
|
|||
#include <limits>
|
||||
#include <pcap.h>
|
||||
#include <boost/program_options.hpp>
|
||||
#include "./libraries/libRiddle.h"
|
||||
#include "./commons/libDump.h"
|
||||
|
||||
#ifdef __linux__
|
||||
#include <unistd.h>
|
||||
|
@ -109,16 +109,16 @@ int main ( int argc, char **argv ) {
|
|||
pcap_t *pcap_handle;
|
||||
|
||||
if ( vm.count ( "input" ) ) {
|
||||
|
||||
|
||||
pcap_handle = pcap_open_offline ( vm["input"].as<string>().c_str(), error_buffer );
|
||||
|
||||
|
||||
if ( pcap_handle == NULL ) {
|
||||
cerr << "ERROR >> pcap_open_offline: " << error_buffer << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
cerr << ">> Reading packets from " << vm["input"].as<string>() << endl;
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
string pcap_device;
|
||||
|
@ -126,7 +126,7 @@ int main ( int argc, char **argv ) {
|
|||
if ( vm.count ( "iface" ) ) {
|
||||
pcap_device=vm["iface"].as<string>();
|
||||
} else {
|
||||
|
||||
|
||||
// Cerca e restituisce interfaccia
|
||||
char *dev=pcap_lookupdev ( error_buffer );
|
||||
if ( dev!=NULL ) {
|
||||
|
@ -135,17 +135,17 @@ int main ( int argc, char **argv ) {
|
|||
cerr << "ERROR >> pcap_lookupdev: " << error_buffer << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Apre il device in mod promiscua
|
||||
pcap_handle = pcap_open_live ( pcap_device.c_str(), 4096, 1, 0, error_buffer );
|
||||
|
||||
|
||||
if ( pcap_handle == NULL ) {
|
||||
cerr << "ERROR >> pcap_open_live: " << error_buffer << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
cerr << ">> Sniffing on device " << pcap_device << endl;
|
||||
}
|
||||
|
||||
|
@ -189,9 +189,12 @@ int main ( int argc, char **argv ) {
|
|||
maxpacket=vm["limit"].as<int>();
|
||||
}
|
||||
|
||||
void ( *dumper ) ( const unsigned char*,struct pcap_pkthdr );
|
||||
if ( vm.count ( "dump" ) ) dumper=hexDump;
|
||||
else dumper=rawDump;
|
||||
string ( *dumper ) ( string, uint64_t, uint32_t );
|
||||
if ( vm.count ( "dump" ) ) {
|
||||
dumper=libDump::classicDump;
|
||||
} else {
|
||||
dumper=libDump::riddleDump;
|
||||
}
|
||||
|
||||
const u_char *packet;
|
||||
pcap_pkthdr header;
|
||||
|
@ -202,7 +205,8 @@ int main ( int argc, char **argv ) {
|
|||
cerr << ">> Flow terminated" << endl;
|
||||
break;
|
||||
}
|
||||
dumper ( packet, header );
|
||||
|
||||
cout << dumper ( libDump::encodeHexText( packet, header.len ), header.ts.tv_sec, header.ts.tv_usec );
|
||||
if ( maxpacket!=numeric_limits<int>::max() ) maxpacket--;
|
||||
}
|
||||
|
||||
|
|
|
@ -33,9 +33,21 @@
|
|||
#include <string>
|
||||
#include <sstream>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "libDump.h"
|
||||
|
||||
|
||||
std::string libDump::classicDump ( std::string input, uint64_t timeEpoch, uint32_t timeMillis ) {
|
||||
|
||||
std::stringstream out;
|
||||
|
||||
out << "[ epoch: " << timeEpoch << " ] -> " << timeMillis << "ms" << std::endl;
|
||||
out << classicDump( input );
|
||||
|
||||
return out.str();
|
||||
|
||||
}
|
||||
|
||||
std::string libDump::classicDump ( std::string input ) {
|
||||
|
||||
std::stringstream out;
|
||||
|
@ -105,6 +117,18 @@ std::string libDump::classicDump ( std::string input ) {
|
|||
return out.str();
|
||||
}
|
||||
|
||||
std::string libDump::riddleDump( std::string input, uint64_t timeEpoch, uint32_t timeMillis ) {
|
||||
|
||||
std::stringstream out;
|
||||
|
||||
out << timeEpoch << "!";
|
||||
out << timeMillis << "!";
|
||||
out << input << std::endl;
|
||||
|
||||
return out.str();
|
||||
|
||||
}
|
||||
|
||||
std::string libDump::decodeHexText ( std::string raw ) {
|
||||
|
||||
std::string text;
|
||||
|
@ -122,3 +146,17 @@ std::string libDump::decodeHexText ( std::string raw ) {
|
|||
return text;
|
||||
|
||||
}
|
||||
|
||||
std::string libDump::encodeHexText ( const unsigned char* text, uint32_t size ) {
|
||||
|
||||
std::stringstream out;
|
||||
|
||||
for ( int i = 0; i < size; i++ ) {
|
||||
|
||||
out << std::setfill ( '0' ) << std::setw ( 2 ) << std::hex << (int) text[i];
|
||||
|
||||
}
|
||||
|
||||
return out.str();
|
||||
|
||||
}
|
||||
|
|
|
@ -32,15 +32,25 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace libDump {
|
||||
|
||||
/** print hex string in a format style "memory dump". */
|
||||
std::string classicDump ( std::string input, uint64_t timeEpoch, uint32_t timeMillis );
|
||||
|
||||
/** print hex string in a format style "memory dump". overload */
|
||||
std::string classicDump ( std::string input );
|
||||
|
||||
/** print data in riddle protocol */
|
||||
std::string riddleDump ( std::string input, uint64_t timeEpoch, uint32_t timeMillis );
|
||||
|
||||
/** decode hex string using the ASCII table. */
|
||||
std::string decodeHexText ( std::string raw );
|
||||
|
||||
/** enccoding to hex format */
|
||||
std::string encodeHexText ( const unsigned char *text, uint32_t size);
|
||||
|
||||
}
|
||||
|
||||
#endif //LIBDUMP_H
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
/**
|
||||
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
|
||||
*
|
||||
* Name : Project Riddle
|
||||
* Author : Andrea Bontempi
|
||||
* Version : 0.1 aplha
|
||||
* Description : Modular Network Sniffer
|
||||
*
|
||||
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
|
||||
*
|
||||
* This file is part of the project Riddle.
|
||||
*
|
||||
* The project Riddle is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The project Riddle 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this project. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include "libRiddle.h"
|
||||
#include "../commons/libDump.h"
|
||||
|
||||
static void memPrint ( const unsigned char *start, char len, int index ) {
|
||||
printf ( "0x%08x | ",index );
|
||||
int i;
|
||||
for ( i=0; i<len; i++ ) printf ( "%02x ",start[i] );
|
||||
for ( i=0; i< ( 16-len ); i++ ) printf ( " " );
|
||||
printf ( "| " );
|
||||
for ( i=0; i<len; i++ ) {
|
||||
if ( ( start[i]>32 ) && ( start[i]<128 ) ) printf ( "%c",start[i] );
|
||||
else printf ( "." );
|
||||
}
|
||||
printf ( "\n" );
|
||||
}
|
||||
|
||||
void hexDump ( const unsigned char *start, struct pcap_pkthdr header ) {
|
||||
std::cout<<std::endl<<"[TS: "<<header.ts.tv_sec;
|
||||
std::cout<<" uS: "<<header.ts.tv_usec;
|
||||
std::cout<<"] Received "<<header.len<<" byte:"<<std::endl;
|
||||
int index=0;
|
||||
while ( header.len>16 ) {
|
||||
memPrint ( start,16,index );
|
||||
header.len-=16;
|
||||
start+=16;
|
||||
index+=16;
|
||||
}
|
||||
if ( header.len>0 ) memPrint ( start,header.len,index );
|
||||
|
||||
}
|
||||
|
||||
void rawDump ( const unsigned char *start, struct pcap_pkthdr header ) {
|
||||
std::cout<<header.ts.tv_sec<<"!";
|
||||
std::cout<<header.ts.tv_usec<<"!";
|
||||
for ( int i=0; i<header.len; i++ ) printf ( "%02x",start[i] );
|
||||
std::cout<<std::endl;
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
/**
|
||||
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
|
||||
*
|
||||
* Name : Project Riddle
|
||||
* Author : Andrea Bontempi
|
||||
* Version : 0.1 aplha
|
||||
* Description : Modular Network Sniffer
|
||||
*
|
||||
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
|
||||
*
|
||||
* This file is part of the project Riddle.
|
||||
*
|
||||
* The project Riddle is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The project Riddle 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this project. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
|
||||
*/
|
||||
|
||||
#ifndef LIBRIDDLE_H
|
||||
#define LIBRIDDLE_H
|
||||
|
||||
#include <pcap.h>
|
||||
|
||||
/* struct pcap_pkthdr {
|
||||
* struct timeval ts; time stamp
|
||||
* bpf_u_int32 caplen; length of portion present
|
||||
* bpf_u_int32; lebgth this packet (off wire)
|
||||
} *
|
||||
*/
|
||||
|
||||
/* Funzioni per la stampa su schermo dei dati in formato esadecimale */
|
||||
void hexDump ( const unsigned char *start, struct pcap_pkthdr header );
|
||||
void rawDump ( const unsigned char *start, struct pcap_pkthdr header );
|
||||
|
||||
#endif //LIBRIDDLE_H
|
Loading…
Reference in a new issue