pkt.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * pkt.h
  3. * ptunnel is licensed under the BSD license:
  4. *
  5. * Copyright (c) 2004-2011, Daniel Stoedle <daniels@cs.uit.no>,
  6. * Yellow Lemon Software. All rights reserved.
  7. *
  8. * Copyright (c) 2017-2019, Toni Uhlig <matzeton@googlemail.com>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. *
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. *
  20. * - Neither the name of the Yellow Lemon Software nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Contacting the author:
  37. * You can get in touch with me, Daniel Stødle (that's the Norwegian letter oe,
  38. * in case your text editor didn't realize), here: <daniels@cs.uit.no>
  39. *
  40. * The official ptunnel website is here:
  41. * <http://www.cs.uit.no/~daniels/PingTunnel/>
  42. *
  43. * Note that the source code is best viewed with tabs set to 4 spaces.
  44. */
  45. #ifndef PKT_H
  46. #define PKT_H 1
  47. #include <stdint.h>
  48. #ifndef __MINGW32__
  49. #define __PTATTR__ __attribute__ ((packed))
  50. #else
  51. #define __PTATTR__ __attribute__ ((packed, gcc_struct))
  52. #endif
  53. #ifdef WIN32
  54. #include <winsock2.h>
  55. typedef int socklen_t;
  56. typedef uint32_t in_addr_t;
  57. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  58. struct ether_header {
  59. uint8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
  60. uint8_t ether_shost[ETH_ALEN]; /* source ether addr */
  61. uint16_t ether_type; /* packet type ID field */
  62. } __PTATTR__;
  63. #endif /* WIN32 */
  64. /** Resend packets after this interval (in seconds) */
  65. #define kResend_interval 1.5
  66. /** ping_tunnel_pkt_t: This data structure represents the header of a ptunnel
  67. * packet, consisting of a magic number, the tunnel's destination IP and port,
  68. * as well as some other fields. Note that the dest IP and port is only valid
  69. * in packets from the client to the proxy.
  70. */
  71. typedef struct {
  72. /** magic number, used to identify ptunnel packets. */
  73. uint32_t magic;
  74. /** destination IP and port (used by proxy to figure */
  75. uint32_t dst_ip;
  76. /** out where to tunnel to) */
  77. uint32_t dst_port;
  78. /** current connection state; see constants above. */
  79. uint32_t state;
  80. /** sequence number of last packet received from other end */
  81. uint32_t ack;
  82. /** length of data buffer */
  83. uint32_t data_len;
  84. /** sequence number of this packet */
  85. uint16_t seq_no;
  86. /** id number, used to separate different tunnels from each other */
  87. uint16_t id_no;
  88. /** optional data buffer */
  89. char data[0];
  90. } __PTATTR__ ping_tunnel_pkt_t;
  91. /** ip_packet_t: This is basically my own definition of the IP packet, which
  92. * of course complies with the official definition ;) See any good book on IP
  93. * (or even the RFC) for info on the contents of this packet.
  94. */
  95. typedef struct {
  96. uint8_t vers_ihl;
  97. uint8_t tos;
  98. uint16_t pkt_len;
  99. uint16_t id;
  100. uint16_t flags_frag_offset;
  101. uint8_t ttl;
  102. uint8_t proto; // 1 for ICMP
  103. uint16_t checksum;
  104. uint32_t src_ip;
  105. uint32_t dst_ip;
  106. char data[0];
  107. } __PTATTR__ ip_packet_t;
  108. /** icmp_echo_packet_t: This is the definition of a standard ICMP header. The
  109. * ptunnel packets are constructed as follows:
  110. * [ ip header (20 bytes) ]
  111. * [ icmp header (8 bytes) ]
  112. * [ ptunnel header (28 bytes) ]
  113. *
  114. * We actually only create the ICMP and ptunnel headers, the IP header is
  115. * taken care of by the OS.
  116. */
  117. typedef struct {
  118. uint8_t type;
  119. uint8_t code;
  120. uint16_t checksum;
  121. uint16_t identifier;
  122. uint16_t seq;
  123. char data[0];
  124. } __PTATTR__ icmp_echo_packet_t;
  125. typedef struct forward_desc_t forward_desc_t;
  126. typedef struct icmp_desc_t icmp_desc_t;
  127. void handle_packet(char *buf, unsigned bytes, int is_pcap, struct sockaddr_in *addr, int icmp_sock);
  128. void handle_data(icmp_echo_packet_t *pkt, int total_len, forward_desc_t **ring,
  129. int *await_send, int *insert_idx, uint16_t *next_expected_seq);
  130. void handle_ack(uint16_t seq_no, icmp_desc_t *ring, int *packets_awaiting_ack,
  131. int one_ack_only, int insert_idx, int *first_ack,
  132. uint16_t *remote_ack, int is_pcap);
  133. #endif