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){
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); po::options_description desc("Allowed options");
if(raw < 0){ desc.add_options()
perror("socket init error"); ("help", "produce help message")
return -1; ("IP,i", po::value<std::string>(), "IP to connect")
} ("dev,d", po::value<std::string>(), "tun device name");
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;
}
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; if (vm.count("help") || !vm.count("IP") || !vm.count("dev")) {
std::cout << tun << std::endl; 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); auto raw = socket(AF_INET, SOCK_RAW, IPPROTO_IPIP);
std::thread t2(outbound, raw, tun); if (raw < 0) {
t1.join(); perror("socket init error");
t2.join(); return -1;
}
return 0; 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."); if (ioctl(tun, TUNSETIFF, &ifr) < 0) {
return 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