Advertisement
oaktree

client.cpp (tunnel)

Jul 10th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. /*
  2.  
  3.     client.cpp
  4.     Tunnel client by oaktree & unh0lys0da
  5.  
  6.     compile with
  7.     g++ -std=c++11 -Wall -Werror client.cpp -o client
  8.  
  9. */
  10.  
  11. #include <iostream>
  12. #include <string>
  13. #include <vector>
  14.  
  15. #include <cstdlib>
  16. #include <cstdio>
  17. #include <cstring>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <unistd.h>
  21. #include <netinet/in.h>
  22. #include <netdb.h>
  23.  
  24. #define DEBUG(x) printf("%s\n", x);
  25.  
  26. int start_connection(int& sockfd, const std::string& host, const int& port);
  27.  
  28. int main(int argc, char** argv) {
  29.     int sockfd;
  30.  
  31.     if (argc != 3) {
  32.         std::cout << "[*] Usage: ./tunnel [host] [port]" << std::endl;
  33.     }
  34.  
  35.     if (start_connection(sockfd, std::string(argv[1]), atoi(argv[2])) != 0) {
  36.         std::cout << "[*] Something went wrong in connecting." << std::endl;
  37.         exit(2);
  38.     }
  39. }
  40.  
  41. int start_connection(int& sockfd, const std::string& host, const int& port) {
  42.    
  43.     if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  44.         std::cout << "[*] Socket could not be opened." << std::endl;
  45.         return 1;
  46.     }
  47.  
  48.     struct hostent *server;
  49.     if ( (server = gethostbyname( host.c_str() ) ) == nullptr) {
  50.         std::cout << "[*] Host not found." << std::endl;
  51.         close(sockfd);
  52.         return 2;          
  53.     }
  54.  
  55.     struct sockaddr_in serv_addr;
  56.     memset(&serv_addr, 0, sizeof(serv_addr));
  57.  
  58.     bcopy(
  59.         (char*)server->h_addr,
  60.         (char*)&serv_addr.sin_addr.s_addr,
  61.         server->h_length
  62.         );
  63.  
  64.     serv_addr.sin_family = AF_INET;
  65.     serv_addr.sin_port = htons(port);
  66.  
  67.     // if ( connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
  68.     //  std::cout << "[*] Could not connect." << std::endl;
  69.     //  close(sockfd);
  70.     //  return 3;
  71.     // }
  72.    
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement