Libraryize test.cc
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
cc_library(
|
||||
name = "tun_tap_device_lib",
|
||||
hdrs = [
|
||||
|
||||
@@ -1 +1,41 @@
|
||||
#include "lib/tun_tap_device.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <linux/if.h>
|
||||
#include <glog/logging.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace funstraw {
|
||||
|
||||
namespace {
|
||||
const std::string kCloneDevice = "/dev/net/tun";
|
||||
}
|
||||
|
||||
TunTapDevice::TunTapDevice(const std::string& name, uint64_t if_flags, uint64_t fd_flags) {
|
||||
auto fd_ = open(kCloneDevice.c_str(), fd_flags);
|
||||
CHECK_NE(fd_, -1) << strerror(errno);
|
||||
|
||||
struct ifreq ifr = {0};
|
||||
ifr.ifr_flags = if_flags;
|
||||
if (!name.empty()) {
|
||||
CHECK_LT(name.length(), IFNAMSIZ);
|
||||
strcpy(ifr.ifr_name, name.c_str());
|
||||
}
|
||||
|
||||
CHECK_EQ(ioctl(fd_, TUNSETIFF, &ifr), 0) << strerror(errno);
|
||||
|
||||
name_ = ifr.ifr_name;
|
||||
}
|
||||
|
||||
TunTapDevice::~TunTapDevice() {
|
||||
CHECK_EQ(close(fd_), 0);
|
||||
}
|
||||
|
||||
size_t TunTapDevice::Read(char* buf) {
|
||||
auto bytes = read(fd_, buf, 2048);
|
||||
CHECK_NE(bytes, -1) << strerror(errno);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
} // namespace funstraw
|
||||
|
||||
@@ -1,21 +1,33 @@
|
||||
#include <string>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <linux/if_tun.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
class TapTunDevice {
|
||||
namespace funstraw {
|
||||
|
||||
class TunTapDevice {
|
||||
public:
|
||||
static const auto kTunFormat = IFF_TUN;
|
||||
static const auto kTapFormat = IFF_TAP;
|
||||
static const auto kNoPacketInfo = IFF_NO_PI;
|
||||
static const auto kIfFormatTun = IFF_TUN;
|
||||
static const auto kIfFormatTap = IFF_TAP;
|
||||
static const auto kIfNoPacketInfo = IFF_NO_PI;
|
||||
|
||||
TapTunDevice(const std::string& name, uint64_t flags);
|
||||
TapTunDevice(uint64_t flags);
|
||||
static const auto kFdReadOnly = O_RDONLY;
|
||||
static const auto kFdWriteOnly = O_WRONLY;
|
||||
static const auto kFdReadWrite = O_RDWR;
|
||||
|
||||
const std::string& Name() {
|
||||
return name_;
|
||||
}
|
||||
TunTapDevice(const std::string& name, uint64_t if_flags, uint64_t fd_flags);
|
||||
~TunTapDevice();
|
||||
|
||||
const std::string& Name() { return name_; }
|
||||
|
||||
// TODO: real buffer object
|
||||
size_t Read(char* buf);
|
||||
|
||||
private:
|
||||
int fd_;
|
||||
std::string name_;
|
||||
};
|
||||
|
||||
} // namespace funstraw
|
||||
|
||||
Reference in New Issue
Block a user