Advertisement
Guest User

Untitled

a guest
May 7th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <arpa/inet.h>
  4. #include <assert.h>
  5.  
  6. #define PROXY2_SIG "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"
  7.  
  8. uint16_t
  9. port(const char *portspec)
  10. {
  11. long int ptst = strtol(portspec, NULL, 10);
  12. if (ptst < 0 || ptst > 65535)
  13. assert(0);
  14. return htons(ptst);
  15. }
  16.  
  17. void
  18. dump_hex(const char *s, size_t len)
  19. {
  20. const char *p;
  21.  
  22. printf("\"");
  23. for (p = s; p != s + len; ++p) {
  24. printf("%02x ", *p);
  25. }
  26. printf("\"\n");
  27. }
  28.  
  29. int
  30. main(int argc, char **argv) {
  31. int af;
  32. long int ptst;
  33.  
  34. struct {
  35. uint8_t sig[12];
  36. uint8_t ver_cmd;
  37. uint8_t fam;
  38. uint16_t len;
  39. union {
  40. struct { /* for TCP/UDP over IPv4, len = 12 */
  41. uint32_t src_addr;
  42. uint32_t dst_addr;
  43. uint16_t src_port;
  44. uint16_t dst_port;
  45. } ip4;
  46. struct { /* for TCP/UDP over IPv6, len = 36 */
  47. uint8_t src_addr[16];
  48. uint8_t dst_addr[16];
  49. uint16_t src_port;
  50. uint16_t dst_port;
  51. } ip6;
  52. } addr;
  53. } v2 = { 0 };
  54.  
  55. if (argc != 6) {
  56. fprintf(stderr, "Usage: %s <proto> clientip serverip"
  57. " clientport serverport\n", argv[0]);
  58. return (1);
  59. }
  60.  
  61. if (strcmp("TCP4", argv[1]) == 0)
  62. af = AF_INET;
  63. else if (strcmp("TCP6", argv[1]) == 0)
  64. af = AF_INET6;
  65. else {
  66. fprintf(stderr, "proto must be one of 'TCP4' or 'TCP6'\n");
  67. return (1);
  68. }
  69.  
  70. memcpy(&v2.sig, PROXY2_SIG, sizeof v2.sig);
  71.  
  72. /* v2, PROXY */
  73. v2.ver_cmd = 0x21;
  74.  
  75. if (af == AF_INET) {
  76. v2.len = htons(sizeof v2.addr.ip4);
  77. v2.fam = 0x11;
  78. v2.addr.ip4.src_port = port(argv[4]);
  79. v2.addr.ip4.dst_port = port(argv[5]);
  80. assert(inet_pton(af, argv[2], &v2.addr.ip4.src_addr) == 1);
  81. assert(inet_pton(af, argv[3], &v2.addr.ip4.dst_addr) == 1);
  82.  
  83. } else if (af == AF_INET6) {
  84. v2.len = htons(sizeof v2.addr.ip6);
  85. v2.fam = 0x21;
  86. v2.addr.ip6.src_port = port(argv[4]);
  87. v2.addr.ip6.dst_port = port(argv[5]);
  88. assert(inet_pton(af, argv[2], &v2.addr.ip6.src_addr) == 1);
  89. assert(inet_pton(af, argv[3], &v2.addr.ip6.dst_addr) == 1);
  90. }
  91.  
  92. dump_hex((const char *) &v2, ntohs(v2.len) + 16);
  93.  
  94. return (0);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement