48 lines
1.4 KiB
C++
48 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <poll.h>
|
|
#include "util.hpp"
|
|
|
|
class TunDevice {
|
|
public:
|
|
class InitializationError : public MsgException {
|
|
public:
|
|
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) {}
|
|
};
|
|
|
|
TunDevice(const std::string& dev);
|
|
~TunDevice();
|
|
|
|
const std::string& get_dev_name() const { return _dev_name; }
|
|
int get_fd() const { return _fd; }
|
|
|
|
/* Reads a packet from the device.
|
|
* Timeouts after `timeout` ms, or never if `timeout < 0`.
|
|
* Upon timeout, returns 0.
|
|
*/
|
|
size_t poll_packet(char* read_buffer, size_t buf_size, int timeout=-1);
|
|
|
|
/* 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);
|
|
|
|
private:
|
|
int _fd;
|
|
std::string _dev_name;
|
|
struct pollfd _poll_fd;
|
|
size_t _last_read_size;
|
|
};
|