xTachyon

Untitled

Jul 12th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <boost/endian/arithmetic.hpp>
  5. #include <boost/asio.hpp>
  6.  
  7. namespace asio = boost::asio;
  8. namespace endian = boost::endian;
  9.  
  10. int readVarInt(int32_t& output, unsigned char* buffer, size_t buflen)
  11. {
  12.     output = 0;
  13.     int v2 = 0;
  14.     signed char v3;
  15.     do
  16.     {
  17.         if (v2 >= buflen) break;
  18.         v3 = buffer[v2];
  19.         output |= (v3 & 127) << (v2++ * 7);
  20.         if (v2 > 5) return v2;
  21.     } while ((v3 & 128) == 128);
  22.     return v2;
  23. }
  24.  
  25. int writeVarInt(int32_t input, unsigned char* buffer)
  26. {
  27.     int i = 0;
  28.     while ((input & -128) != 0)
  29.     {
  30.         buffer[i++] = (input & 127) | 128;
  31.         input >>= 7;
  32.     }
  33.     buffer[i++] = input;
  34.     return i;
  35. }
  36.  
  37. void writeVarIntToStream(std::ostringstream& stream, std::int32_t number)
  38. {
  39.     unsigned char buffer[8];
  40.     int len = writeVarInt(number, buffer);
  41.     stream.write(reinterpret_cast<const char*>(buffer), len);
  42. }
  43.  
  44. int main(int argc, char** argv)
  45. {
  46.     if (argc != 3)
  47.     {
  48.         std::cerr << "Usage: host port";
  49.         return 1;
  50.     }
  51.  
  52.     std::string host(argv[1]);
  53.     int port;
  54.     try
  55.     {
  56.         port = std::stoi(argv[2]);
  57.     }
  58.     catch (std::exception&)
  59.     {
  60.         std::cerr<< "Invalid port number: " << argv[2];
  61.         return 2;
  62.     }
  63.  
  64.     std::ostringstream stream(std::ios::binary);
  65.  
  66.     writeVarIntToStream(stream, 0);
  67.     writeVarIntToStream(stream, 47);
  68.     writeVarIntToStream(stream, static_cast<int>(host.size()));
  69.     stream.write(host.data(), host.size());
  70.     endian::big_uint16_t portt(port);
  71.     stream.write(reinterpret_cast<const char*>(&portt), 2);
  72.     writeVarIntToStream(stream, 1);
  73.  
  74.     std::string fin = stream.str();
  75.     stream.str("");
  76.     writeVarIntToStream(stream, static_cast<int>(fin.size()));
  77.     stream.write(fin.data(), fin.size());
  78.  
  79.     fin = stream.str();
  80.  
  81.     try
  82.     {
  83.         asio::io_service io;
  84.         asio::ip::tcp::iostream con(host, std::to_string(port));
  85.         con.expires_from_now(boost::posix_time::seconds(20));
  86.  
  87.         if (!con)
  88.             std::cerr << "Error: " << con.error().message() << "\n";
  89.  
  90.         con << stream.str();
  91.  
  92.         std::string str;
  93.         con >> str;
  94.     }
  95.     catch (std::exception& e)
  96.     {
  97.         std::cerr << e.what() << '\n';
  98.     }
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment