Commit 94e12e95 authored by nanamicat's avatar nanamicat

dev name

parent 931872c2
......@@ -7,6 +7,7 @@ add_executable(tun main.cpp)
find_package(Threads REQUIRED)
target_link_libraries(tun Threads::Threads)
find_package(Boost COMPONENTS program_options REQUIRED)
target_link_libraries(tun Boost::program_options)
target_link_libraries(tun -static-libgcc -static-libstdc++)
......@@ -11,6 +11,8 @@
#include <unistd.h>
#include <arpa/inet.h>
#include <boost/program_options.hpp>
in_addr_t remote;
// internet -> tun
......@@ -44,57 +46,50 @@ void outbound(int raw, int tun) {
perror("outbound read");
}
void show_usage() {
printf("Usage Help:\n\n");
printf("-i <IP>: IP to connect\n");
printf("-h: show this usage help");
return;
}
namespace po = boost::program_options;
int main(int argc, char *argv[]) {
if(argc <= 1){
printf("Wrong usage! Type -h to see usage help.");
return 0;
}
if(argv[1] == "-h"){
show_usage();
return 0;
}
if(argv[1] == "-i"){
ifreq ifr{};
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
auto raw = socket(AF_INET, SOCK_RAW, IPPROTO_IPIP);
if(raw < 0){
perror("socket init error");
return -1;
}
auto tun = open("/dev/net/tun", O_RDWR);
if(tun < 0){
perror("tun init error");
return -1;
}
if(ioctl(tun, TUNSETIFF, &ifr) < 0){
perror("ioctl error");
return -1;
}
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("IP,i", po::value<std::string>(), "IP to connect")
("dev,d", po::value<std::string>(), "tun device name");
remote = inet_addr(argv[2]);
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
std::cout << raw << std::endl;
std::cout << tun << std::endl;
if (vm.count("help") || !vm.count("IP") || !vm.count("dev")) {
std::cout << desc << std::endl;
return -1;
}
ifreq ifr{};
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
strcpy(ifr.ifr_name, vm["dev"].as<std::string>().c_str());
remote = inet_addr(vm["IP"].as<std::string>().c_str());
std::thread t1(inbound, raw, tun);
std::thread t2(outbound, raw, tun);
t1.join();
t2.join();
return 0;
auto raw = socket(AF_INET, SOCK_RAW, IPPROTO_IPIP);
if (raw < 0) {
perror("socket init error");
return -1;
}
auto tun = open("/dev/net/tun", O_RDWR);
if (tun < 0) {
perror("tun init error");
return -1;
}
else {
printf("Wrong usage! Type -h to see usage help.");
return 0;
if (ioctl(tun, TUNSETIFF, &ifr) < 0) {
perror("ioctl error");
return -1;
}
std::thread t1(inbound, raw, tun);
std::thread t2(outbound, raw, tun);
t1.join();
t2.join();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment