Allow generating pcap files of Daynaport traffic

If rasctl is given a file to "attach" to a daynaport interface, rascsi
will generate a pcap file at that path containing all sent/received
packets.
This commit is contained in:
Quentin Smith 2021-04-03 20:02:20 +01:00
parent 5ef97b99db
commit 2579437b57
7 changed files with 66 additions and 4 deletions

View File

@ -41,7 +41,7 @@ function initialChecks() {
}
function installPackages() {
sudo apt-get update && sudo apt install git libspdlog-dev genisoimage python3 python3-venv nginx -y
sudo apt-get update && sudo apt install git libspdlog-dev genisoimage python3 python3-venv nginx libpcap-dev -y
}
# install all dependency packages for RaSCSI Service

View File

@ -152,7 +152,7 @@ ALL: all
docs: $(DOC_DIR)/rascsi_man_page.txt $(DOC_DIR)/rasctl_man_page.txt $(DOC_DIR)/scsimon_man_page.txt
$(BINDIR)/$(RASCSI): $(OBJ_RASCSI) | $(BINDIR)
$(CXX) $(CXXFLAGS) -o $@ $(OBJ_RASCSI) -lpthread -lz
$(CXX) $(CXXFLAGS) -o $@ $(OBJ_RASCSI) -lpthread -lz -lpcap
$(BINDIR)/$(RASCTL): $(OBJ_RASCTL) | $(BINDIR)
$(CXX) $(CXXFLAGS) -o $@ $(OBJ_RASCTL)

View File

@ -37,6 +37,8 @@ CTapDriver::CTapDriver()
// Initialization
m_hTAP = -1;
memset(&m_MacAddr, 0, sizeof(m_MacAddr));
m_pcap = NULL;
m_pcap_dumper = NULL;
}
//---------------------------------------------------------------------------
@ -248,6 +250,22 @@ BOOL FASTCALL CTapDriver::Init()
}
#endif // __NetBSD__
BOOL FASTCALL CTapDriver::OpenDump(const Filepath& path) {
if (m_pcap == NULL) {
m_pcap = pcap_open_dead(DLT_EN10MB, 65535);
}
if (m_pcap_dumper != NULL) {
pcap_dump_close(m_pcap_dumper);
}
m_pcap_dumper = pcap_dump_open(m_pcap, path.GetPath());
if (m_pcap_dumper == NULL) {
LOGERROR("Error: can't open pcap file: %s", pcap_geterr(m_pcap));
return FALSE;
}
LOGTRACE("%s Opened %s for dumping", __PRETTY_FUNCTION__, path.GetPath())
return TRUE;
}
//---------------------------------------------------------------------------
//
// Cleanup
@ -274,6 +292,14 @@ void FASTCALL CTapDriver::Cleanup()
close(m_hTAP);
m_hTAP = -1;
}
if (m_pcap_dumper != NULL) {
pcap_dump_close(m_pcap_dumper);
m_pcap_dumper = NULL;
}
if (m_pcap != NULL) {
pcap_close(m_pcap);
}
}
//---------------------------------------------------------------------------
@ -400,6 +426,16 @@ int FASTCALL CTapDriver::Rx(BYTE *buf)
dwReceived += 4;
}
if (m_pcap_dumper != NULL) {
struct pcap_pkthdr h = {
.caplen = dwReceived,
.len = dwReceived,
};
gettimeofday(&h.ts, NULL);
pcap_dump((u_char*)m_pcap_dumper, &h, buf);
LOGTRACE("%s Dumped %d byte packet (first byte: %02x last byte: %02x)", __PRETTY_FUNCTION__, (unsigned int)dwReceived, buf[0], buf[dwReceived-1]);
}
// Return the number of bytes
return dwReceived;
}
@ -414,6 +450,16 @@ int FASTCALL CTapDriver::Tx(const BYTE *buf, int len)
ASSERT(this);
ASSERT(m_hTAP != -1);
if (m_pcap_dumper != NULL) {
struct pcap_pkthdr h = {
.caplen = (bpf_u_int32)len,
.len = (bpf_u_int32)len,
};
gettimeofday(&h.ts, NULL);
pcap_dump((u_char*)m_pcap_dumper, &h, buf);
LOGTRACE("%s Dumped %d byte packet (first byte: %02x last byte: %02x)", __PRETTY_FUNCTION__, (unsigned int)h.len, buf[0], buf[h.len-1]);
}
// Start sending
return write(m_hTAP, buf, len);
}

View File

@ -16,6 +16,9 @@
#if !defined(ctapdriver_h)
#define ctapdriver_h
#include <pcap/pcap.h>
#include "filepath.h"
#ifndef ETH_FRAME_LEN
#define ETH_FRAME_LEN 1514
#endif
@ -32,7 +35,9 @@ public:
CTapDriver();
// Constructor
BOOL FASTCALL Init();
// Initilization
// Initialization
BOOL FASTCALL OpenDump(const Filepath& path);
// Capture packets
void FASTCALL Cleanup();
// Cleanup
void FASTCALL GetMacAddr(BYTE *mac);
@ -58,6 +63,9 @@ private:
int m_hTAP;
// File handle
BYTE m_garbage_buffer[ETH_FRAME_LEN];
pcap_t *m_pcap;
pcap_dumper_t *m_pcap_dumper;
};
#endif // ctapdriver_h

View File

@ -106,6 +106,12 @@ SCSIDaynaPort::~SCSIDaynaPort()
}
}
BOOL FASTCALL SCSIDaynaPort::Open(const Filepath& path, BOOL attn)
{
LOGTRACE("SCSIDaynaPort Open");
return m_tap->OpenDump(path);
}
//---------------------------------------------------------------------------
//
// INQUIRY

View File

@ -46,6 +46,8 @@ public:
// Constructor
virtual ~SCSIDaynaPort();
// Destructor
BOOL FASTCALL Open(const Filepath& path, BOOL attn = TRUE);
// Capture packets
// commands
int FASTCALL Inquiry(const DWORD *cdb, BYTE *buffer, DWORD major, DWORD minor);

View File

@ -557,7 +557,7 @@ BOOL ProcessCmd(FILE *fp, int id, int un, int cmd, int type, char *file)
}
// drive checks files
if (type <= rasctl_dev_scsi_hd || (type <= rasctl_dev_cd && xstrcasecmp(file, "-") != 0)) {
if (type <= rasctl_dev_scsi_hd || ((type <= rasctl_dev_cd || type == rasctl_dev_daynaport) && xstrcasecmp(file, "-") != 0)) {
// Set the Path
filepath.SetPath(file);