2020-06-02 13:08:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
2020-06-03 14:54:01 +02:00
|
|
|
#include <poll.h>
|
2020-06-02 13:08:23 +02:00
|
|
|
#include "util.hpp"
|
|
|
|
|
|
|
|
class TunDevice {
|
|
|
|
public:
|
|
|
|
class InitializationError : public MsgException {
|
|
|
|
public:
|
2020-06-03 14:54:01 +02:00
|
|
|
InitializationError(
|
|
|
|
const std::string& msg,
|
|
|
|
int code=0,
|
|
|
|
bool is_perror=false)
|
|
|
|
: MsgException(msg, code, is_perror) {}
|
|
|
|
};
|
|
|
|
class NetError : public MsgException {
|
|
|
|
public:
|
|
|
|
NetError(
|
|
|
|
const std::string& msg,
|
|
|
|
int code=0,
|
|
|
|
bool is_perror=false)
|
|
|
|
: MsgException(msg, code, is_perror) {}
|
2020-06-02 13:08:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TunDevice(const std::string& dev);
|
|
|
|
~TunDevice();
|
|
|
|
|
|
|
|
const std::string& get_dev_name() const { return _dev_name; }
|
|
|
|
int get_fd() const { return _fd; }
|
2020-06-03 14:54:01 +02:00
|
|
|
|
2020-06-16 18:57:08 +02:00
|
|
|
/** Get the interface's MTU */
|
|
|
|
uint16_t get_mtu() const;
|
|
|
|
|
|
|
|
/** Set the interface's MTU */
|
|
|
|
bool set_mtu(uint16_t mtu);
|
|
|
|
|
2020-06-03 14:54:01 +02:00
|
|
|
/* Reads a packet from the device.
|
|
|
|
* Timeouts after `timeout` ms, or never if `timeout < 0`.
|
|
|
|
* Upon timeout, returns 0.
|
|
|
|
*/
|
2020-06-04 11:46:09 +02:00
|
|
|
size_t poll_packet(char* read_buffer, size_t buf_size, int timeout=-1);
|
2020-06-03 14:54:01 +02:00
|
|
|
|
2020-06-04 11:46:09 +02:00
|
|
|
/* Reads a packet, blocking if none is available. */
|
|
|
|
size_t read(char* read_buffer, size_t buf_size);
|
|
|
|
|
|
|
|
size_t write(const char* data, size_t len);
|
2020-06-03 14:54:01 +02:00
|
|
|
|
2020-06-02 13:08:23 +02:00
|
|
|
private:
|
|
|
|
int _fd;
|
|
|
|
std::string _dev_name;
|
2020-06-03 14:54:01 +02:00
|
|
|
struct pollfd _poll_fd;
|
|
|
|
size_t _last_read_size;
|
2020-06-02 13:08:23 +02:00
|
|
|
};
|