Commit 94e12e95 authored by nanamicat's avatar nanamicat

dev name

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