From 36fbca9d0e89daa77efe3ba436b354ed6cd8f88d Mon Sep 17 00:00:00 2001 From: Jacob <> Date: Sat, 26 Sep 2020 15:27:53 -0400 Subject: [PATCH] Added exception handling to the stream class. - Removed all ui functionality from the repo --- app/context.cpp | 124 ------------ app/context.h | 44 ----- app/header_field.h | 86 --------- app/main.cpp | 17 -- app/main.h | 13 -- app/main_window.cpp | 102 ---------- app/main_window.h | 51 ----- app/ui/byte_viewer.cpp | 274 --------------------------- app/ui/byte_viewer.h | 107 ----------- app/ui/details_pane.cpp | 50 ----- app/ui/details_pane.h | 33 ---- app/ui/packet_tree.cpp | 189 ------------------ app/ui/packet_tree.h | 69 ------- app/ui/received_pane.cpp | 72 ------- app/ui/received_pane.h | 38 ---- app/ui/stream_pane.cpp | 82 -------- app/ui/stream_pane.h | 48 ----- include/packethacker/packet_stream.h | 48 +++-- sandbox/sandbox.cpp | 9 +- src/packet_stream.cpp | 30 +-- 20 files changed, 52 insertions(+), 1434 deletions(-) delete mode 100644 app/context.cpp delete mode 100644 app/context.h delete mode 100644 app/header_field.h delete mode 100644 app/main.cpp delete mode 100644 app/main.h delete mode 100644 app/main_window.cpp delete mode 100644 app/main_window.h delete mode 100644 app/ui/byte_viewer.cpp delete mode 100644 app/ui/byte_viewer.h delete mode 100644 app/ui/details_pane.cpp delete mode 100644 app/ui/details_pane.h delete mode 100644 app/ui/packet_tree.cpp delete mode 100644 app/ui/packet_tree.h delete mode 100644 app/ui/received_pane.cpp delete mode 100644 app/ui/received_pane.h delete mode 100644 app/ui/stream_pane.cpp delete mode 100644 app/ui/stream_pane.h diff --git a/app/context.cpp b/app/context.cpp deleted file mode 100644 index 4d0988c..0000000 --- a/app/context.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "context.h" -#include "packet/adapter.h" -#include "packet/utils/packet_utils.h" - -#include - -#ifndef WX_PRECOMP -#include -#endif - - -namespace PacketHacker { - -Context::Context(MainWindow *window) - : m_CurrentAdapter(), - m_AdapterSet(false), - m_pBasePacket(), - m_MainWindow(window) -{ -} - -Context::~Context() -{ - if (m_CurrentAdapter) m_CurrentAdapter->ClosePacketStream(); - delete m_CurrentAdapter; - delete m_pBasePacket; -} - -void Context::SetAdapter(const AdapterInfo &info) -{ - m_AdapterSet = true; - if (m_CurrentAdapter) { - m_CurrentAdapter->ClosePacketStream(); - delete m_CurrentAdapter; - } - m_CurrentAdapter = new Adapter(info); - - char errbuf[256]; - if (!m_CurrentAdapter->OpenPacketStream(errbuf)) { - wxLogMessage("%s", errbuf); - m_AdapterSet = false; - } -} - -void Context::SetBasePacket(int packetId) -{ - delete m_pBasePacket; - m_pBasePacket = Utils::PacketFromId(packetId); - if (!m_pBasePacket) wxLogMessage("Illegal packetId: %d", packetId); -} - -void Context::AddPacket(int packetId) -{ - if (!m_pBasePacket) { - SetBasePacket(packetId); - } else { - Packet *packet = Utils::PacketFromId(packetId); - if (!packet) wxLogMessage("Illegal packetId: %d", packetId); - - Packet *currentPacket = m_pBasePacket; - while (currentPacket) { - if (packet->GetName() == currentPacket->GetName()) { - wxLogMessage("Cannot have multiple packets of same type: %s", packet->GetName()); - return; - } - if (!currentPacket->GetInnerPacket()) break; - currentPacket = currentPacket->GetInnerPacket(); - } - - currentPacket->SetInnerPacket(packet); - } -} - -void Context::RemovePacket(std::string name) -{ - if (!m_pBasePacket) return; - if (m_pBasePacket->GetName() == name) { - delete m_pBasePacket; - m_pBasePacket = nullptr; - } - - Packet *currentPacket = m_pBasePacket; - while (currentPacket) { - if (currentPacket->GetInnerPacket()->GetName() == name) { - currentPacket->RemoveInnerPacket(); - } - currentPacket = currentPacket->GetInnerPacket(); - } -} - -bool Context::SendPacket() -{ - if (!IsAdapterSet()) { - wxLogMessage("Adapter not set!"); - return false; - } - - char errbuf[256]; - if (!m_CurrentAdapter->SendPacket(m_pBasePacket, errbuf)) { - wxLogMessage("%s", errbuf); - return false; - } - - return true; -} - -const uint8_t *Context::ReadNextPacket(uint32_t *size) -{ - if (!IsAdapterSet()) { - wxLogMessage("Adapter not set!"); - return nullptr; - } - - char errbuf[256]; - const uint8_t *data; - if ((data = m_CurrentAdapter->GetNextPacket(size, errbuf)) == nullptr) { - wxLogMessage("%s", errbuf); - return nullptr; - } - return data; -} - - -}// namespace PacketHacker \ No newline at end of file diff --git a/app/context.h b/app/context.h deleted file mode 100644 index fb29c30..0000000 --- a/app/context.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include -#include - -#include "packet/packet.h" -#include "packet/packets.h" -#include "packet/adapter.h" - -namespace PacketHacker { - -// class Adapter; -class Context -{ - friend class MainWindow; - -public: - Context(MainWindow *window); - ~Context(); - - void SetAdapter(const AdapterInfo &info); - Adapter *GetAdapter() { return m_CurrentAdapter; } - bool IsAdapterSet() const { return m_AdapterSet; } - - void SetBasePacket(int packetId); - void AddPacket(int packetId); - void RemovePacket(std::string name); - Packet *GetBasePacket() const { return m_pBasePacket; } - - bool SendPacket(); - const uint8_t *ReadNextPacket(uint32_t *size); - - MainWindow *GetMainWindow() const { return m_MainWindow; } - -private: - MainWindow *m_MainWindow; - - Adapter *m_CurrentAdapter; - bool m_AdapterSet; - - Packet *m_pBasePacket; -}; - -}// namespace PacketHacker \ No newline at end of file diff --git a/app/header_field.h b/app/header_field.h deleted file mode 100644 index af80d2a..0000000 --- a/app/header_field.h +++ /dev/null @@ -1,86 +0,0 @@ -#pragma once - -enum FieldType { - FIELD_HARDWARE, - FIELD_IP, - FIELD_INT8, - FIELD_INT16, - FIELD_INT32, - FIELD_INT64 -}; - -using FieldData = std::variant; - -class HeaderField -{ -public: - HeaderField(Packet *packet, - const std::string name, - const FieldData &defaultVal, - const bool editable, - const FieldType type) - : m_packet(packet), m_name(std::move(name)), m_currentVal(defaultVal), m_editable(editable), m_type(type) - { - } - - virtual ~HeaderField() {} - - virtual void HandleData(const FieldData &data) = 0; - - Packet *GetPacket() const { return m_packet; } - const std::string &GetName() const { return m_name; } - const FieldData &GetCurrentVal() const { return m_currentVal; } - const bool IsEditable() const { return m_editable; } - const FieldType GetType() const { return m_type; } - - void SetValue(const FieldData &value) - { - m_currentVal = value; - } - -protected: - Packet *m_packet; - const std::string m_name; - FieldData m_currentVal; - const bool m_editable; - const FieldType m_type; -}; - -template -class HeaderFieldImpl : public HeaderField -{ -public: - typedef void (T::*HandlerFunctionPtr)(const FieldData &); - - HeaderFieldImpl(T *packet, - const std::string name, - const FieldData &defaultVal, - FieldType type, - bool editable, - HandlerFunctionPtr function) - : HeaderField(packet, name, defaultVal, editable, type), m_function(function) - { - } - - virtual void HandleData(const FieldData &data) - { - T *packet = static_cast(m_packet); - (packet->*m_function)(data); - } - -private: - HandlerFunctionPtr m_function; -}; - -#define HEADER_FIELD_HARDWARE(classname, name, defaultVal, editable, function) \ - (new PacketHacker::HeaderFieldImpl(this, name, defaultVal, FieldType::FIELD_HARDWARE, editable, &classname::function)) -#define HEADER_FIELD_IPv4(classname, name, defaultVal, editable, function) \ - (new PacketHacker::HeaderFieldImpl(this, name, defaultVal, FieldType::FIELD_IP, editable, &classname::function)) -#define HEADER_FIELD_INT8(classname, name, defaultVal, editable, function) \ - (new PacketHacker::HeaderFieldImpl(this, name, (uint8_t)defaultVal, FieldType::FIELD_INT8, editable, &classname::function)) -#define HEADER_FIELD_INT16(classname, name, defaultVal, editable, function) \ - (new PacketHacker::HeaderFieldImpl(this, name, (uint16_t)defaultVal, FieldType::FIELD_INT16, editable, &classname::function)) -#define HEADER_FIELD_INT32(classname, name, defaultVal, editable, function) \ - (new PacketHacker::HeaderFieldImpl(this, name, (uint32_t)defaultVal, FieldType::FIELD_INT32, editable, &classname::function)) -#define HEADER_FIELD_INT64(classname, name, defaultVal, editable, function) \ - (new PacketHacker::HeaderFieldImpl(this, name, (uint64_t)defaultVal, FieldType::FIELD_INT64, editable, &classname::function)) \ No newline at end of file diff --git a/app/main.cpp b/app/main.cpp deleted file mode 100644 index bb01bcd..0000000 --- a/app/main.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "main.h" -#include "app/main_window.h" - -DECLARE_APP(PacketHackerApp); -IMPLEMENT_APP(PacketHackerApp); - -bool PacketHackerApp::OnInit() -{ - using namespace PacketHacker; - - MainWindow *window = new MainWindow(); - SetTopWindow(window); - window->Center(); - window->Show(); - - return true; -} diff --git a/app/main.h b/app/main.h deleted file mode 100644 index b107aed..0000000 --- a/app/main.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - -#ifndef WX_PRECOMP -#include -#endif - -class PacketHackerApp : public wxApp -{ -public: - virtual bool OnInit(); -}; diff --git a/app/main_window.cpp b/app/main_window.cpp deleted file mode 100644 index 48a9eed..0000000 --- a/app/main_window.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "main_window.h" - -#include - -#define ADAPTER_OFFSET 3000 - -namespace PacketHacker { -MainWindow::MainWindow() - : wxFrame(nullptr, wxID_ANY, _("Packet Hacker"), wxDefaultPosition, wxSize(1280, 720), wxDEFAULT_FRAME_STYLE) -{ - m_pContext = new Context(this); - m_mgr.SetManagedWindow(this); - - // Menu Bar - m_pMenuBar = new wxMenuBar(); - m_pAdapterMenu = new wxMenu(); - int adapterId = ADAPTER_OFFSET; - for (AdapterInfo info : Adapter::GetAvailableAdapters()) - m_pAdapterMenu->Append(adapterId++, info.friendlyName); - m_pMenuBar->Append(m_pAdapterMenu, "Adapters"); - - m_pPacketMenu = new wxMenu(); - m_pPacketMenu->Append(PacketType::ARP, "ARP"); - m_pPacketMenu->Append(PacketType::ETHERNET, "ETHERNET"); - m_pPacketMenu->Append(PacketType::IP, "IP"); - m_pPacketMenu->Append(PacketType::ICMP, "ICMP"); - m_pPacketMenu->Append(PacketType::DATA, "DATA"); - m_pPacketMenu->Append(PacketType::UDP, "UDP"); - m_pMenuBar->Append(m_pPacketMenu, "Add"); - // End menu - - wxAuiToolBar *toolbar = new wxAuiToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxAUI_TB_DEFAULT_STYLE | wxAUI_TB_OVERFLOW | wxAUI_TB_VERTICAL); - m_pSendButton = new wxButton(toolbar, ID_SENDBUTTON, "Send"); - toolbar->AddControl(m_pSendButton); - m_mgr.AddPane(toolbar, wxAuiPaneInfo().Name("tb1").Caption("Big Toolbar").ToolbarPane().Top()); - - m_pPacketTree = new UI::PacketTree(m_pContext, this); - m_mgr.AddPane(m_pPacketTree, wxCENTER, wxT("Current Packet")); - - m_pDetailsPane = new UI::DetailsPane(this); - m_mgr.AddPane(m_pDetailsPane, wxLEFT, wxT("Details Pane")); - - // m_pReceivedPane = new UI::ReceivedPane(m_pContext, this); - // m_mgr.AddPane(m_pReceivedPane, wxRIGHT, wxT("Received Packet")); - - m_pStreamPane = new UI::StreamPane(m_pContext, this); - m_mgr.AddPane(m_pStreamPane, wxRIGHT, wxT("Stream")); - - m_pByteViewer = new UI::ByteViewer(this); - m_mgr.AddPane(m_pByteViewer, wxBOTTOM, wxT("Byte Viewer")); - - this->Connect(ID_SENDBUTTON, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::OnButtonPressed)); - for (int j = ADAPTER_OFFSET; j < adapterId; j++) - this->Connect(j, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnAdapterSelected)); - - this->Connect(PacketType::ARP, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnPacketSelected)); - this->Connect(PacketType::ETHERNET, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnPacketSelected)); - this->Connect(PacketType::IP, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnPacketSelected)); - this->Connect(PacketType::ICMP, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnPacketSelected)); - this->Connect(PacketType::DATA, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnPacketSelected)); - this->Connect(PacketType::UDP, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainWindow::OnPacketSelected)); - - this->CreateStatusBar(1); - this->SetStatusText("Adapter not selected!", 0); - - this->SetMenuBar(m_pMenuBar); - - m_mgr.Update(); -} - -MainWindow::~MainWindow() -{ - m_mgr.UnInit(); - delete m_pContext; -} - -void MainWindow::OnButtonPressed(wxCommandEvent &event) -{ - if (!m_pContext->SendPacket()) { - wxLogMessage("Could not send data."); - return; - } - // m_pReceivedPane->OnPacketSent(); -} - -void MainWindow::OnAdapterSelected(wxCommandEvent &event) -{ - if (event.GetId() >= Adapter::GetAvailableAdapters().size() - ADAPTER_OFFSET) return; - AdapterInfo info = Adapter::GetAvailableAdapters()[event.GetId() - ADAPTER_OFFSET]; - m_pContext->SetAdapter(info); - m_pDetailsPane->SetAdapterInfo(info); - this->SetStatusText(wxString::Format("Selected: %s", info.friendlyName)); - // m_pStreamPane->StartRead(); -} - -void MainWindow::OnPacketSelected(wxCommandEvent &event) -{ - m_pContext->AddPacket(event.GetId()); - m_pPacketTree->SetPacket(m_pContext->GetBasePacket()); -} - -}// namespace PacketHacker \ No newline at end of file diff --git a/app/main_window.h b/app/main_window.h deleted file mode 100644 index 131147b..0000000 --- a/app/main_window.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once -#include - -#ifndef WX_PRECOMP -#include -#endif - -#include - -#include "context.h" -#include "packet/packet.h" -#include "ui/byte_viewer.h" -#include "ui/packet_tree.h" -#include "ui/details_pane.h" -#include "ui/received_pane.h" -#include "ui/stream_pane.h" - -namespace PacketHacker { -class MainWindow : public wxFrame -{ -public: - MainWindow(); - ~MainWindow(); - - void OnButtonPressed(wxCommandEvent &event); - void OnAdapterSelected(wxCommandEvent &event); - void OnPacketSelected(wxCommandEvent &event); - - UI::ByteViewer *GetByteViewer() const { return m_pByteViewer; } - Context *GetContext() const { return m_pContext; } - -private: - Context *m_pContext; - - wxAuiManager m_mgr; - - wxButton *m_pSendButton; - wxMenuBar *m_pMenuBar; - wxMenu *m_pAdapterMenu; - wxMenu *m_pPacketMenu; - - UI::PacketTree *m_pPacketTree; - UI::ByteViewer *m_pByteViewer; - UI::DetailsPane *m_pDetailsPane; - UI::ReceivedPane *m_pReceivedPane; - UI::StreamPane *m_pStreamPane; -}; - -const int ID_SENDBUTTON = 101; - -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/byte_viewer.cpp b/app/ui/byte_viewer.cpp deleted file mode 100644 index d892b66..0000000 --- a/app/ui/byte_viewer.cpp +++ /dev/null @@ -1,274 +0,0 @@ -#include "byte_viewer.h" - -namespace PacketHacker { -namespace UI { - - ByteEditor::ByteEditor() {} - void ByteEditor::Create(wxWindow *parent, - wxWindowID id, - wxEvtHandler *evtHandler) - { - wxGridCellTextEditor::Create(parent, id, evtHandler); - wxTextCtrl *txtCtrl = (wxTextCtrl *)m_control; - txtCtrl->SetMaxLength(2); - } - - void ByteEditor::BeginEdit(int row, int col, wxGrid *grid) - { - wxGridTableBase *table = grid->GetTable(); - m_Value = table->GetValueAsLong(row, col); - DoBeginEdit(GetString()); - } - - bool ByteEditor::EndEdit(int row, int col, const wxGrid *grid, const wxString &oldval, wxString *newval) - { - long value = 0; - wxString text = Text()->GetValue(); - if (text.empty()) { - if (oldval.empty()) - return false; - } else// non-empty text now (maybe 0) - { - if (!text.ToLong(&value, 16)) - return false; - - if (value == m_Value && (value || !oldval.empty())) - return false; - } - - m_Value = value; - - if (newval) - *newval = text; - - return true; - } - - void ByteEditor::ApplyEdit(int row, int col, wxGrid *grid) - { - wxGridTableBase *const table = grid->GetTable(); - table->SetValueAsLong(row, col, m_Value); - } - - void ByteEditor::Reset() - { - DoReset(GetString()); - } - - bool ByteEditor::IsAcceptedKey(wxKeyEvent &event) - { - if (wxGridCellEditor::IsAcceptedKey(event)) { - int keycode = event.GetKeyCode(); - if ((keycode < 128) && (wxIsdigit(keycode) || keycode == 'a' || keycode == 'b' || keycode == 'c' || keycode == 'd' || keycode == 'e' || keycode == 'f')) { - return true; - } - } - - return false; - } - - void ByteEditor::StartingKey(wxKeyEvent &event) - { - int keycode = event.GetKeyCode(); - - if (wxIsdigit(keycode) || keycode == 'a' || keycode == 'b' || keycode == 'c' || keycode == 'd' || keycode == 'e' || keycode == 'f') { - wxGridCellTextEditor::StartingKey(event); - return; - } - - event.Skip(); - } - - wxString ByteEditor::GetValue() const - { - return Text()->GetValue(); - } - - void ByteEditor::HandleReturn(wxKeyEvent &event) - { - } - - ByteTable::ByteTable(int intialSize) - : wxGridTableBase() - { - m_Data.resize(intialSize); - } - - wxString ByteTable::GetValue(int row, int col) - { - if (col >= GetNumberCols() - 1) - return ""; - int index = col + row * 16; - if (index >= m_Data.size()) - return "."; - uint8_t data = (uint8_t)GetValueAsLong(row, col); - char buffer[4]; - itoa(data, buffer, 16); - return wxString::Format("%02s", buffer); - } - - void ByteTable::SetValue(int row, int col, const wxString &value) - { - if (col >= GetNumberCols() - 1) - return; - long val = 0; - if (value.ToLong(&val, 16)) { - SetValueAsLong(row, col, val); - } - } - - long ByteTable::GetValueAsLong(int row, int col) - { - if (col >= GetNumberCols() - 1) - return 0xfff; - int index = col + row * 16; - if (index >= m_Data.size()) - return 0xfff; - uint8_t val = m_Data[index]; - return val; - } - - void ByteTable::SetValueAsLong(int row, int col, long value) - { - if (col >= GetNumberCols() - 1) - return; - int index = col + row * 16; - if (index >= m_Data.size()) { - m_Data.emplace_back((uint8_t)value); - return; - } - m_Data[index] = (uint8_t)value; - } - - wxString ByteTable::GetRowLabelValue(int row) - { - char buffer[4]; - itoa(row, buffer, 16); - return wxString::Format("%04s", buffer); - } - - void ByteTable::Clear() - { - m_Data.clear(); - } - - bool ByteTable::InsertRows(size_t pos, size_t numRows) { return true; } - - bool ByteTable::AppendRows(size_t numRows) - { - m_rows += numRows; - - if (GetView()) { - wxGridTableMessage msg(this, - wxGRIDTABLE_NOTIFY_ROWS_APPENDED, - numRows); - - GetView()->ProcessTableMessage(msg); - } - - return true; - } - - bool ByteTable::DeleteRows(size_t pos, size_t numRows) { return false; } - - void ByteTable::Resize(size_t size) - { - m_Data.resize(size); - } - - ByteViewer::ByteViewer(wxWindow *parent, - wxWindowID winid, - const wxPoint &pos, - const wxSize &size, - long style, - const wxString &name) - : wxPanel(parent, wxID_ANY, pos, size, style, name) - { - wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); - - m_pGrid = new wxGrid(this, wxID_ANY); - m_pByteTable = new ByteTable(8); - m_pGrid->SetTable(m_pByteTable, true); - m_pGrid->SetTabBehaviour(wxGrid::Tab_Wrap); - m_pGrid->HideColLabels(); - m_pGrid->EnableGridLines(false); - m_pGrid->SetCellHighlightPenWidth(1); - m_pGrid->SetDefaultColSize(18, true); - m_pGrid->SetRowLabelSize(40); - m_pGrid->SetDefaultEditor(new ByteEditor()); - m_pGrid->SetDefaultCellAlignment(wxALIGN_CENTRE, wxALIGN_BOTTOM); - for (int i = 0; i < 16; i++) { - m_pGrid->DisableColResize(i); - } - m_pGrid->SetColSize(16, 40); - sizer->Add(m_pGrid, 1, wxEXPAND); - - Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(ByteViewer::OnGridKeyDown)); - - this->SetSizerAndFit(sizer); - } - - void ByteViewer::OnGridKeyDown(wxKeyEvent &event) - { - if (event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER) { - if (m_pGrid->GetGridCursorRow() == m_pGrid->GetNumberRows() - 1) { - m_pGrid->AppendRows(); - } - } - event.Skip(); - } - - void OnGridCellChanged(wxGridEvent &event) - { - } - - std::vector ByteViewer::GetData() - { - return m_pByteTable->GetData(); - } - - void ByteViewer::SetByte(uint32_t offset, uint8_t byte) - { - int row = offset / 16; - int col = offset - row * 16; - if (row >= m_pGrid->GetNumberRows()) - m_pGrid->AppendRows(); - m_pGrid->GetTable()->SetValueAsLong(row, col, byte); - } - - void ByteViewer::WriteInt(uint32_t offset, uint64_t val, uint32_t size) - { - for (int i = 0; i < size; i++) { - SetByte(offset + (size - 1 - i), val >> (i * 8)); - } - } - - void ByteViewer::Update(Packet *packet) - { - m_pGrid->ClearGrid(); - uint32_t size = packet->Size(); - m_pByteTable->GetData().clear(); - m_pByteTable->GetData().resize(size); - packet->WriteToBuf(m_pByteTable->GetData().data(), size); - m_pGrid->ForceRefresh(); - } - - void ByteViewer::SetSize(uint32_t size) - { - if (m_pByteTable->GetData().size() < size) { - int numRows = size / 16; - while (numRows > 0) { - m_pGrid->AppendRows(); - numRows -= 1; - } - m_pByteTable->Resize(size); - } - } - - void ByteViewer::Refresh() - { - m_pGrid->ForceRefresh(); - } - -}// namespace UI -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/byte_viewer.h b/app/ui/byte_viewer.h deleted file mode 100644 index 683dec7..0000000 --- a/app/ui/byte_viewer.h +++ /dev/null @@ -1,107 +0,0 @@ -#pragma once -#include - -#ifndef WX_PRECOMP -#include -#endif - -#include -#include -#include - -#include "packet/packet.h" - -namespace PacketHacker { -namespace UI { - - class ByteEditor : public wxGridCellTextEditor - { - public: - ByteEditor(); - virtual void Create(wxWindow *parent, - wxWindowID id, - wxEvtHandler *evtHandler) override; - virtual void Reset() override; - - virtual bool IsAcceptedKey(wxKeyEvent &event) override; - virtual void StartingKey(wxKeyEvent &event) override; - virtual void BeginEdit(int row, int col, wxGrid *grid) override; - virtual bool EndEdit(int row, int col, const wxGrid *grid, const wxString &oldval, wxString *newval) override; - virtual void ApplyEdit(int row, int col, wxGrid *grid) override; - virtual void HandleReturn(wxKeyEvent &event) override; - - virtual wxString GetValue() const override; - virtual wxGridCellEditor *Clone() const override { return new ByteEditor(); } - - wxString GetString() const - { - char buffer[4]; - itoa(m_Value, buffer, 16); - return wxString::Format(wxT("%02s"), buffer); - } - - private: - long m_Value = 0; - }; - - class ByteTable : public wxGridTableBase - { - public: - explicit ByteTable(int intialSize); - - virtual int GetNumberRows() override { return m_rows; } - virtual int GetNumberCols() override { return m_cols; } - virtual wxString GetValue(int row, int col) override; - virtual void SetValue(int row, int col, const wxString &s) override; - - wxString GetRowLabelValue(int row) override; - - virtual long GetValueAsLong(int row, int col) override; - virtual void SetValueAsLong(int row, int col, long value) override; - - void Clear() override; - bool InsertRows(size_t pos = 0, size_t numRows = 1) override; - bool AppendRows(size_t numRows = 1) override; - bool DeleteRows(size_t pos = 0, size_t numRows = 1) override; - - void Resize(size_t size); - - std::vector &GetData() { return m_Data; } - - private: - int m_rows = 1; - const int m_cols = 17; - std::vector m_Data; - }; - - class ByteViewer : public wxPanel - { - public: - explicit ByteViewer(wxWindow *parent, - wxWindowID winid = wxID_ANY, - const wxPoint &pos = wxDefaultPosition, - const wxSize &size = wxDefaultSize, - long style = wxTAB_TRAVERSAL | wxNO_BORDER, - const wxString &name = wxPanelNameStr); - - void SetByte(uint32_t offset, uint8_t byte); - void WriteInt(uint32_t offset, uint64_t val, uint32_t size); - - void Update(Packet *packet); - void SetSize(uint32_t size); - void Refresh(); - - std::vector GetData(); - - private: - void OnGridKeyDown(wxKeyEvent &event); - void OnGridCellChanged(wxGridEvent &event); - - private: - wxGrid *m_pGrid = nullptr; - ByteTable *m_pByteTable = nullptr; - }; - -}// namespace UI - -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/details_pane.cpp b/app/ui/details_pane.cpp deleted file mode 100644 index 318c3a3..0000000 --- a/app/ui/details_pane.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "details_pane.h" - -namespace PacketHacker { -namespace UI { - DetailsPane::DetailsPane(wxWindow *parent, - wxWindowID winid, - const wxPoint &pos, - const wxSize &size, - long style, - const wxString &name) - : wxPanel(parent, winid, pos, size, style, name) - { - wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); - - m_pPropGrid = new wxPropertyGrid(this, wxID_ANY); - sizer->Add(m_pPropGrid, 1, wxEXPAND); - - wxPGProperty *adapterProp = m_pPropGrid->Append(new wxPropertyCategory("Current Adapter")); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("Index", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("Name", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("Unicast address", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("Anycast address", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("Multicast address", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("DnsServer address", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("Gateway address", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("DnsSuffix", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("Description", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("FriendlyName", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - m_pPropGrid->AppendIn(adapterProp, new wxStringProperty("Physical Address", wxPG_LABEL, "null"))->ChangeFlag(wxPG_PROP_READONLY, true); - - this->SetSizerAndFit(sizer); - } - - void DetailsPane::SetAdapterInfo(AdapterInfo &info) - { - m_pPropGrid->GetProperty("Index")->SetValue((long)info.index); - m_pPropGrid->GetProperty("Name")->SetValue(info.name); - m_pPropGrid->GetProperty("Unicast address")->SetValue(info.unicastAddress.ToString()); - m_pPropGrid->GetProperty("Anycast address")->SetValue(info.anycastAddress.ToString()); - m_pPropGrid->GetProperty("Multicast address")->SetValue(info.multicastAddress.ToString()); - m_pPropGrid->GetProperty("DnsServer address")->SetValue(info.dnsServerAddress.ToString()); - m_pPropGrid->GetProperty("Gateway address")->SetValue(info.gatewayAddress.ToString()); - m_pPropGrid->GetProperty("DnsSuffix")->SetValue(info.dnsSuffix); - m_pPropGrid->GetProperty("Description")->SetValue(info.description); - m_pPropGrid->GetProperty("FriendlyName")->SetValue(info.friendlyName); - m_pPropGrid->GetProperty("Physical Address")->SetValue(info.address.ToString()); - } - -}// namespace UI -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/details_pane.h b/app/ui/details_pane.h deleted file mode 100644 index 2e270b4..0000000 --- a/app/ui/details_pane.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include - -#ifndef WX_PRECOMP -#include -#endif - -#include - -#include "packet/adapter.h" - -namespace PacketHacker { -namespace UI { - - class DetailsPane : public wxPanel - { - public: - DetailsPane(wxWindow *parent, - wxWindowID winid = wxID_ANY, - const wxPoint &pos = wxDefaultPosition, - const wxSize &size = wxDefaultSize, - long style = wxTAB_TRAVERSAL | wxNO_BORDER, - const wxString &name = wxPanelNameStr); - - void SetAdapterInfo(AdapterInfo &info); - - private: - wxPropertyGrid *m_pPropGrid; - }; - -}// namespace UI -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/packet_tree.cpp b/app/ui/packet_tree.cpp deleted file mode 100644 index b95661d..0000000 --- a/app/ui/packet_tree.cpp +++ /dev/null @@ -1,189 +0,0 @@ -#include "packet_tree.h" - -#include "app/main_window.h" -#include "packet/utils/adapter_utils.h" - -namespace PacketHacker { -namespace UI { - - // HardwareAddressProperty - // ------------------------------------------------------ - HardwareAddressProperty::HardwareAddressProperty(const wxString &label, - const wxString &name, - const HardwareAddress &value) - : wxStringProperty(label, name, value.ToString()) - { - } - - bool HardwareAddressProperty::ValidateValue(wxVariant &value, - wxPGValidationInfo &validationInfo) const - { - return HardwareAddress::IsHardwareAddressValid(value.GetString().c_str().AsChar()); - } - - // ------------------------------------------------------ - - // HardwareAddressProperty - // ------------------------------------------------------ - IpAddressProperty::IpAddressProperty(const wxString &label, - const wxString &name, - const IPv4Address &value) - : wxStringProperty(label, name, value.ToString()) - { - } - - bool IpAddressProperty::ValidateValue(wxVariant &value, - wxPGValidationInfo &validationInfo) const - { - return IPv4Address::IsIpv4AddressValid(value.GetString().c_str().AsChar()); - } - - // ------------------------------------------------------ - - // PacketTree - // ------------------------------------------------------ - PacketTree::PacketTree(Context *context, - wxWindow *parent, - wxWindowID winid, - const wxPoint &pos, - const wxSize &size, - long style, - const wxString &name) - : wxPanel(parent, winid, pos, size, style, name), - m_pContext(context), - m_pPropGrid() - { - wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); - - m_pPropGrid = new wxPropertyGrid(this, wxID_ANY); - - sizer->Add(m_pPropGrid, 1, wxEXPAND); - - this->Connect(wxEVT_PG_CHANGED, wxPropertyGridEventHandler(PacketTree::OnPropertyGridChanged)); - this->Connect(wxEVT_PG_RIGHT_CLICK, wxPropertyGridEventHandler(PacketTree::OnPropertyGridRightClicked)); - - this->SetSizerAndFit(sizer); - } - - void PacketTree::SetPacket(Packet *packet) - { - MainWindow *window = m_pContext->GetMainWindow(); - if (packet) { - window->GetByteViewer()->SetSize(packet->Size()); - window->GetByteViewer()->Update(m_pContext->GetBasePacket()); - window->GetByteViewer()->Refresh(); - } - - m_pPropGrid->Clear(); - Packet *currentPacket = packet; - while (currentPacket) { - std::string name = currentPacket->GetName(); - wxPGProperty *currProp = m_pPropGrid->Append(new wxPropertyCategory(currentPacket->GetName())); - for (HeaderField *field : currentPacket->GetFields()) { - wxPGProperty *fieldProp; - switch (field->GetType()) { - case FieldType::FIELD_HARDWARE: - fieldProp = new HardwareAddressProperty(field->GetName(), wxPG_LABEL, std::get(field->GetCurrentVal())); - break; - case FieldType::FIELD_IP: - fieldProp = new IpAddressProperty(field->GetName(), wxPG_LABEL, std::get(field->GetCurrentVal())); - break; - case FieldType::FIELD_INT8: - fieldProp = new wxIntProperty(field->GetName(), wxPG_LABEL, std::get(field->GetCurrentVal())); - break; - case FieldType::FIELD_INT16: - fieldProp = new wxIntProperty(field->GetName(), wxPG_LABEL, std::get(field->GetCurrentVal())); - break; - case FieldType::FIELD_INT32: - fieldProp = new wxIntProperty(field->GetName(), wxPG_LABEL, std::get(field->GetCurrentVal())); - break; - case FieldType::FIELD_INT64: - fieldProp = new wxIntProperty(field->GetName(), wxPG_LABEL, std::get(field->GetCurrentVal())); - break; - default: - break; - } - fieldProp->Enable(field->IsEditable()); - m_pPropGrid->AppendIn(currProp, fieldProp); - } - - currentPacket = currentPacket->GetInnerPacket(); - } - } - - void PacketTree::Reload() - { - Packet *currentPacket = m_pContext->GetBasePacket(); - while (currentPacket) { - for (HeaderField *field : currentPacket->GetFields()) { - wxPGProperty *property = m_pPropGrid->GetProperty(field->GetName()); - // if (property->GetValue() != field->GetCurrentVal()) { - // property->SetValue(field->GetCurrentVal()); - // } - } - currentPacket = currentPacket->GetInnerPacket(); - } - } - - void PacketTree::OnPropertyGridChanged(wxPropertyGridEvent &event) - { - std::string name = event.GetProperty()->GetName(); - std::string outerPropName = event.GetProperty()->GetParent()->GetName(); - MainWindow *window = m_pContext->GetMainWindow(); - const Packet *packet = m_pContext->GetBasePacket()->GetPacket(outerPropName); - HeaderField *field = packet->GetField(name); - - switch (field->GetType()) { - case FieldType::FIELD_HARDWARE: - field->HandleData(HardwareAddress(event.GetValue().GetString().c_str().AsChar())); - break; - case FieldType::FIELD_IP: - field->HandleData(IPv4Address(event.GetValue().GetString().c_str().AsChar())); - break; - case FieldType::FIELD_INT8: - field->HandleData((uint8_t)event.GetValue().GetInteger()); - break; - case FieldType::FIELD_INT16: - field->HandleData((uint16_t)event.GetValue().GetInteger()); - break; - case FieldType::FIELD_INT32: - field->HandleData((uint32_t)event.GetValue().GetInteger()); - break; - case FieldType::FIELD_INT64: - field->HandleData((uint32_t)event.GetValue().GetInteger()); - break; - default: - break; - } - window->GetByteViewer()->Update(m_pContext->GetBasePacket()); - Reload(); - } - - void PacketTree::OnPopupClick(wxCommandEvent &event) - { - void *data = static_cast(event.GetEventObject())->GetClientData(); - wxPGProperty *property = static_cast(data); - MainWindow *window = static_cast(this->GetParent()); - if (property->GetParent() != m_pPropGrid->GetRoot()) { - property = property->GetParent(); - } - - if (event.GetId() == ID_REMOVE) { - m_pContext->RemovePacket(property->GetName().ToStdString()); - this->SetPacket(m_pContext->GetBasePacket()); - } - } - - void PacketTree::OnPropertyGridRightClicked(wxPropertyGridEvent &event) - { - void *data = reinterpret_cast(event.GetProperty()); - wxMenu mnu; - mnu.SetClientData(data); - mnu.SetTitle("Edit"); - mnu.Append(ID_REMOVE, "Remove"); - mnu.Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(PacketTree::OnPopupClick), NULL, this); - PopupMenu(&mnu); - } - -}// namespace UI -}// namespace PacketHacker diff --git a/app/ui/packet_tree.h b/app/ui/packet_tree.h deleted file mode 100644 index 088f1cd..0000000 --- a/app/ui/packet_tree.h +++ /dev/null @@ -1,69 +0,0 @@ -#pragma once - -#include - -#ifndef WX_PRECOMP -#include -#endif -#include - -#include -#include -#include - -#include "app/context.h" -#include "packet/packet.h" - -#define ID_REMOVE 2001 - -namespace PacketHacker { -namespace UI { - - class HardwareAddressProperty : public wxStringProperty - { - public: - HardwareAddressProperty(const wxString &label = wxPG_LABEL, - const wxString &name = wxPG_LABEL, - const HardwareAddress &value = HardwareAddress()); - - virtual bool ValidateValue(wxVariant &value, - wxPGValidationInfo &validationInfo) const override; - }; - - class IpAddressProperty : public wxStringProperty - { - public: - IpAddressProperty(const wxString &label = wxPG_LABEL, - const wxString &name = wxPG_LABEL, - const IPv4Address &value = IPv4Address()); - - virtual bool ValidateValue(wxVariant &value, - wxPGValidationInfo &validationInfo) const override; - }; - - class PacketTree : public wxPanel - { - public: - PacketTree(Context *context, - wxWindow *parent, - wxWindowID winid = wxID_ANY, - const wxPoint &pos = wxDefaultPosition, - const wxSize &size = wxDefaultSize, - long style = wxTAB_TRAVERSAL | wxNO_BORDER, - const wxString &name = wxPanelNameStr); - - void SetPacket(Packet *packet); - void Reload(); - - void OnPropertyGridChanged(wxPropertyGridEvent &event); - void OnPropertyGridRightClicked(wxPropertyGridEvent &event); - void OnPopupClick(wxCommandEvent &event); - - private: - wxPropertyGrid *m_pPropGrid; - Context *m_pContext; - }; - -}// namespace UI - -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/received_pane.cpp b/app/ui/received_pane.cpp deleted file mode 100644 index 3c978ca..0000000 --- a/app/ui/received_pane.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "received_pane.h" - -#include "packet/packet.h" - -namespace PacketHacker { -namespace UI { - - ReceivedPane::ReceivedPane(Context *context, - wxWindow *parent, - wxWindowID winid, - const wxPoint &pos, - const wxSize &size, - long style, - const wxString &name) - : wxPanel(parent, winid, pos, size, style, name), - m_pContext(context) - { - - wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); - - m_pPropGrid = new wxPropertyGrid(this, wxID_ANY); - - sizer->Add(m_pPropGrid, 1, wxEXPAND); - - this->SetSizerAndFit(sizer); - } - - void ReceivedPane::SetPacket(Packet *packet) - { - m_pPropGrid->Clear(); - Packet *currentPacket = packet; - while (currentPacket) { - wxPGProperty *currProp = m_pPropGrid->Append(new wxPropertyCategory(currentPacket->GetName())); - for (HeaderField *field : currentPacket->GetFields()) { - // m_pPropGrid->AppendIn(currProp, new wxStringProperty(field->GetName(), wxPG_LABEL, field->GetCurrentVal()))->ChangeFlag(wxPG_PROP_READONLY, true); - } - - currentPacket = currentPacket->GetInnerPacket(); - } - } - - - void ReceivedPane::OnPacketSent() - { - // if (!m_pContext->BeginStream()) { - // return; - // } - uint32_t size; - const uint8_t *data; - int counter = 0; - int timeout = 5; - - while ((data = m_pContext->ReadNextPacket(&size))) { - // if (counter >= timeout) { - // wxLogMessage("Reply timed out!"); - // break; - // } - Packet *sent = m_pContext->GetBasePacket(); - if (sent->DoesReplyMatch(data, size)) { - Packet *received = new EthernetPacket(data, size); - SetPacket(received); - delete received; - break; - } - counter++; - } - - // m_pContext->EndStream(); - } - -}// namespace UI -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/received_pane.h b/app/ui/received_pane.h deleted file mode 100644 index 0f18013..0000000 --- a/app/ui/received_pane.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include - -#ifndef WX_PRECOMP -#include -#endif - -#include - -#include "app/context.h" - - -namespace PacketHacker { -namespace UI { - - - class ReceivedPane : public wxPanel - { - public: - ReceivedPane(Context *context, - wxWindow *parent, - wxWindowID winid = wxID_ANY, - const wxPoint &pos = wxDefaultPosition, - const wxSize &size = wxDefaultSize, - long style = wxTAB_TRAVERSAL | wxNO_BORDER, - const wxString &name = wxPanelNameStr); - - void SetPacket(Packet *packet); - void OnPacketSent(); - - private: - Context *m_pContext; - wxPropertyGrid *m_pPropGrid; - }; - -}// namespace UI -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/stream_pane.cpp b/app/ui/stream_pane.cpp deleted file mode 100644 index 98f0280..0000000 --- a/app/ui/stream_pane.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "stream_pane.h" - - -namespace PacketHacker { -namespace UI { - - int index = 0; - - wxDEFINE_EVENT(myEVT_THREAD_UPDATE, wxThreadEvent); - - StreamPane::StreamPane(Context *context, - wxWindow *parent, - wxWindowID winid, - const wxPoint &pos, - const wxSize &size, - long style, - const wxString &name) - : wxPanel(parent, winid, pos, size, style, name), - m_pContext(context), - m_queue(5) - { - wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); - m_pListView = new wxListView(this, wxID_ANY); - m_pListView->AppendColumn("Column 1"); - sizer->Add(m_pListView, 1, wxEXPAND); - - this->Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(StreamPane::OnClose)); - this->Connect(myEVT_THREAD_UPDATE, wxThreadEventHandler(StreamPane::OnThreadUpdate)); - - this->SetSizerAndFit(sizer); - } - - void StreamPane::StartRead() - { - if (CreateThread(wxTHREAD_JOINABLE) != wxTHREAD_NO_ERROR) { - wxLogError("Could not create the worker thread!"); - return; - } - if (GetThread()->Run() != wxTHREAD_NO_ERROR) { - wxLogError("Could not run the worker thread!"); - return; - } - } - - wxThread::ExitCode StreamPane::Entry() - { - while (!GetThread()->TestDestroy()) { - if (m_pContext->IsAdapterSet()) { - uint32_t size; - const uint8_t *data; - if ((data = m_pContext->ReadNextPacket(&size))) { - wxCriticalSectionLocker lock(m_queueCS); - Packet *received = new EthernetPacket(data, size); - m_queue.InsertPacket(received); - wxQueueEvent(this, new wxThreadEvent(myEVT_THREAD_UPDATE)); - } - GetThread()->Sleep(100); - } - } - - return (wxThread::ExitCode)0; - } - - void StreamPane::OnClose(wxCloseEvent &) - { - if (GetThread() && GetThread()->IsRunning()) - GetThread()->Wait(); - Destroy(); - } - - void StreamPane::OnThreadUpdate(wxThreadEvent &evt) - { - wxCriticalSectionLocker lock(m_queueCS); - Packet *packet = m_queue.GetPacket(); - if (packet) { - m_pListView->InsertItem(index++, wxString::Format("%d", packet->Size())); - } - delete packet; - } - -}// namespace UI -}// namespace PacketHacker \ No newline at end of file diff --git a/app/ui/stream_pane.h b/app/ui/stream_pane.h deleted file mode 100644 index 8b832be..0000000 --- a/app/ui/stream_pane.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include - -#ifndef WX_PRECOMP -#include -#endif - -#include -#include - -#include "app/context.h" -#include "packet/packet_queue.h" - -namespace PacketHacker { -namespace UI { - - wxDECLARE_EVENT(myEVT_THREAD_UPDATE, wxThreadEvent); - - class StreamPane : public wxPanel - , public wxThreadHelper - { - public: - StreamPane(Context *context, - wxWindow *parent, - wxWindowID winid = wxID_ANY, - const wxPoint &pos = wxDefaultPosition, - const wxSize &size = wxDefaultSize, - long style = wxTAB_TRAVERSAL | wxNO_BORDER, - const wxString &name = wxPanelNameStr); - - void StartRead(); - void OnThreadUpdate(wxThreadEvent &evt); - void OnClose(wxCloseEvent &evt); - - protected: - virtual wxThread::ExitCode Entry(); - - PacketQueue m_queue; - wxCriticalSection m_queueCS; - - private: - Context *m_pContext; - wxListView *m_pListView; - }; - -}// namespace UI -}// namespace PacketHacker \ No newline at end of file diff --git a/include/packethacker/packet_stream.h b/include/packethacker/packet_stream.h index 006a066..379c477 100644 --- a/include/packethacker/packet_stream.h +++ b/include/packethacker/packet_stream.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "packet.h" #include "interface_table.h" @@ -38,13 +39,11 @@ class PacketStream /** * \brief Opens the stream to send or receive packets. - * * \warning Stream must be closed in order to open. - * @param errbuf buffer where error information is outputted - * @return true if packet stream could successfully be opened, - * false if any errors occur + * \exception Throws a stream exception if the stream is already opened + * or stream cannot be opened. */ - bool openPacketStream(char *errbuf); + void openPacketStream(); /** * \brief Closes the packet stream. @@ -58,23 +57,23 @@ class PacketStream * * \warning Packet stream must be opened before sending packets * can happen. + * \exception Throws an exception if stream is not open or if + * sending the packet fails. * @param packet packet to be sent - * @param errbuf buffer where error information is outputted - * @return true if packet successfully sent, false if any errors - * occur */ - bool sendPacket(Packet *packet, char *errbuf); + void sendPacket(Packet *packet); /** * \brief Reads the next packet from the stream and returns the byte buffer. * * \warning Packet stream must be opened before receiving packets. + * \exception Throws an exception if stream is not open or if there + * was an error reading the next packet. * @param size pointer to size integer, this is set to the size of the packet - * @param errbuf buffer where error information is outputted * @return byte array of data that contains packet info, nullptr is * returned if no packet received. */ - const uint8_t *getNextPacket(uint32_t *size, char *errbuf); + const uint8_t *getNextPacket(uint32_t *size); /** * \brief Returns whether stream is open or not. @@ -82,6 +81,33 @@ class PacketStream */ bool streamOpen() const { return m_streamOpen; } + /** + * \brief Excption class for throwing stream exceptions. + */ + class StreamException : public std::exception + { + public: + /** + * \brief Default constructor. + * @param message message of exception + */ + StreamException(const std::string &message) + : m_message(std::move(message)) + {} + + /** + * \brief Returns the exception message. + * @return message + */ + virtual const char *what() const throw() + { + return m_message.c_str(); + } + + private: + std::string m_message; + }; + private: Interface *m_streamInterface; pcap *m_handle; diff --git a/sandbox/sandbox.cpp b/sandbox/sandbox.cpp index edde7c9..6661d3c 100644 --- a/sandbox/sandbox.cpp +++ b/sandbox/sandbox.cpp @@ -16,15 +16,16 @@ int main() PacketStream stream(currInterface); - char errbuf[500]; - if (!stream.openPacketStream(errbuf)) { - std::cout << errbuf << "\n"; + try { + stream.openPacketStream(); + } catch (PacketStream::StreamException &e) { + std::cout << e.what() << "\n"; return 1; } while (stream.streamOpen()) { uint32_t size; - const uint8_t *data = stream.getNextPacket(&size, errbuf); + const uint8_t *data = stream.getNextPacket(&size); EthernetPacket eth(data, size); std::cout << "Eth{" << eth.dstMac() << ", " << eth.srcMac() << "}\n"; } diff --git a/src/packet_stream.cpp b/src/packet_stream.cpp index 60b45e5..83ac0ba 100644 --- a/src/packet_stream.cpp +++ b/src/packet_stream.cpp @@ -7,18 +7,16 @@ PacketStream::PacketStream(Interface *streamInterface) { } -bool PacketStream::openPacketStream(char *errbuf) +void PacketStream::openPacketStream() { if (m_streamOpen) { - sprintf(errbuf, "Stream already opened!"); - return false; + throw StreamException("Stream already opened!"); } char pcapErrbuf[PCAP_ERRBUF_SIZE]; if ((m_handle = pcap_create(m_streamInterface->name.c_str(), pcapErrbuf)) == nullptr) { - sprintf(errbuf, "Unable to open the adapter. %s is not supported by Npcap", m_streamInterface->name.c_str()); - return false; + throw StreamException("Unable to open the adapter."); } pcap_set_snaplen(m_handle, 100); @@ -26,14 +24,11 @@ bool PacketStream::openPacketStream(char *errbuf) pcap_set_timeout(m_handle, 20); if (pcap_activate(m_handle) != 0) { - sprintf(errbuf, "Error activating handle: %s", pcap_geterr(m_handle)); pcap_close(m_handle); - return false; + throw StreamException(pcap_geterr(m_handle)); } m_streamOpen = true; - - return true; } void PacketStream::closePacketStream() @@ -42,11 +37,10 @@ void PacketStream::closePacketStream() m_streamOpen = false; } -bool PacketStream::sendPacket(Packet *packet, char *errbuf) +void PacketStream::sendPacket(Packet *packet) { if (!m_streamOpen) { - sprintf(errbuf, "Stream not opened!"); - return false; + throw StreamException("Stream not opened!"); } const uint32_t size = packet->size(); @@ -54,18 +48,14 @@ bool PacketStream::sendPacket(Packet *packet, char *errbuf) packet->writeToBuf(data.data(), size); if (pcap_sendpacket(m_handle, data.data(), size) != 0) { - sprintf(errbuf, "Error sending the packet: %s", pcap_geterr(m_handle)); - return false; + throw StreamException(pcap_geterr(m_handle)); } - - return true; } -const uint8_t *PacketStream::getNextPacket(uint32_t *size, char *errbuf) +const uint8_t *PacketStream::getNextPacket(uint32_t *size) { if (!m_streamOpen) { - sprintf(errbuf, "Stream not opened!"); - return false; + throw StreamException("Stream not opened!"); } int res; @@ -81,7 +71,7 @@ const uint8_t *PacketStream::getNextPacket(uint32_t *size, char *errbuf) } if (res == -1) { - sprintf(errbuf, "Error reading the packets: %s\n", pcap_geterr(m_handle)); + throw StreamException(pcap_geterr(m_handle)); } return nullptr;