Driver: Add TCP support

Device: Close Driver in heartbeat thread on disconnection
This commit is contained in:
Kyle Schwarz
2023-03-08 18:32:26 +00:00
parent ddee1254a0
commit 9b46d486cb
10 changed files with 674 additions and 12 deletions
+5
View File
@@ -103,6 +103,11 @@ public:
PCAPCouldNotStart = 0x3102,
PCAPCouldNotFindDevices = 0x3103,
PacketDecodingError = 0x3104,
SocketFailedToOpen = 0x3105,
FailedToBind = 0x3106,
ErrorSettingSocketOption = 0x3107,
GetIfAddrsError = 0x3108,
SendToError = 0x3109,
NoErrorFound = 0xFFFFFFFD,
TooManyEvents = 0xFFFFFFFE,
+57
View File
@@ -0,0 +1,57 @@
#ifndef __TCP_H_
#define __TCP_H_
#ifdef __cplusplus
#include <optional>
#include "icsneo/communication/driver.h"
#include "icsneo/device/founddevice.h"
namespace icsneo {
class TCP : public Driver {
public:
static void Find(std::vector<FoundDevice>& foundDevices);
struct NetworkInterface {
const std::string name;
const uint32_t ip;
};
TCP(const device_eventhandler_t& err, NetworkInterface on, uint32_t dstIP, uint16_t dstPort);
~TCP() override { if(isOpen()) close(); }
bool open() override;
bool isOpen() override;
bool close() override;
bool isEthernet() const override { return true; }
private:
#ifdef _WIN32
typedef size_t SocketFileDescriptor;
#else
typedef int SocketFileDescriptor;
#endif
class Socket {
public:
Socket(int domain, int type, int protocol, bool nonblocking = true);
~Socket();
explicit operator bool() const { return fd != -1; }
operator SocketFileDescriptor() const { return fd; }
private:
SocketFileDescriptor fd;
};
NetworkInterface interface;
uint32_t dstIP;
uint16_t dstPort;
std::optional<Socket> socket;
void readTask() override;
void writeTask() override;
};
}
#endif // __cplusplus
#endif