pdesc.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * pdesc.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 PDESC_H
  46. #define PDESC_H 1
  47. #include <stdint.h>
  48. #ifndef WIN32
  49. #include <sys/socket.h>
  50. #include <netinet/in.h>
  51. #include <arpa/inet.h>
  52. #endif
  53. #include "pkt.h"
  54. #include "challenge.h"
  55. #include "pconfig.h"
  56. /** forward_desc_t: Describes a piece of that needs to be forwarded. This
  57. * structure is used for receiving data from the network, and for subsequent
  58. * forwarding over TCP:
  59. *
  60. * 1. Client sends data to proxy over ICMP
  61. * 2. Proxy receives the data, and puts it into a forward_desc_t
  62. * 3. The proxy starts send()-ing the data over the TCP socket to the destination,
  63. * decreasing forward_desc_t->remaining with the number of bytes transferred.
  64. * 4. Once remaining reaches 0, the forward_desc_t is removed from the receive
  65. * ring.
  66. *
  67. * The same procedure is followed in proxy-to-client communication. Just replace
  68. * proxy with client and vice versa in the list above.
  69. */
  70. typedef struct forward_desc_t {
  71. /** ping_tunnel_pkt_t seq_no */
  72. uint16_t seq_no;
  73. /** length of data */
  74. uint16_t length;
  75. /** amount of data not yet transferred */
  76. size_t remaining;
  77. char data[0];
  78. } forward_desc_t;
  79. /** icmp_desc_t: This structure is used to track the ICMP packets sent by either
  80. * the client or proxy. The last_resend variable is used to prevent resending
  81. * the packet too often. Once the packet is acknowledged by the remote end,
  82. * it will be removed from the send-ring, freeing up space for more outgoing
  83. * ICMP packets.
  84. */
  85. typedef struct icmp_desc_t {
  86. /** total length of ICMP packet, including ICMP header and ptunnel data. */
  87. uint16_t pkt_len;
  88. double last_resend;
  89. uint16_t seq_no;
  90. uint16_t icmp_id;
  91. icmp_echo_packet_t * pkt;
  92. } icmp_desc_t;
  93. /** xfer_stats_t: Various transfer statistics, such as bytes sent and received,
  94. * number of ping packets sent/received, etc.
  95. */
  96. typedef struct xfer_stats_t {
  97. double bytes_in;
  98. double bytes_out;
  99. uint32_t icmp_in;
  100. uint32_t icmp_out;
  101. uint32_t icmp_resent;
  102. uint32_t icmp_ack_out;
  103. } xfer_stats_t;
  104. /** proxy_desc_t: This massive structure describes a tunnel instance.
  105. */
  106. typedef struct proxy_desc_t {
  107. /** ICMP or UDP socket */
  108. int sock;
  109. /** number of bytes in receive buffer */
  110. int bytes;
  111. /** set to true once this instance should be removed */
  112. int should_remove;
  113. /** data buffer, used to receive ping and pong packets */
  114. char * buf;
  115. uint16_t id_no;
  116. uint16_t my_seq;
  117. uint16_t ping_seq;
  118. uint16_t next_remote_seq;
  119. uint16_t pkt_type;
  120. uint16_t remote_ack_val;
  121. uint16_t icmp_id;
  122. /** first available slot in recv ring */
  123. int recv_idx;
  124. /** current slot in recv ring being transferred */
  125. int recv_xfer_idx;
  126. /** first available slot in send ring */
  127. int send_idx;
  128. /** first packet in send ring not yet acked */
  129. int send_first_ack;
  130. /** number of items in recv ring awaiting send */
  131. int recv_wait_send;
  132. /** number of items in send ring awaiting ack */
  133. int send_wait_ack;
  134. int next_resend_start;
  135. int authenticated;
  136. /** Contains the challenge, if used. */
  137. challenge_t * challenge;
  138. /** Protocol state */
  139. uint32_t state;
  140. /** Either kProxy_flag or kUser_flag */
  141. enum pkt_flag type_flag;
  142. /** IP and port to which data should be forwarded. */
  143. uint32_t dst_ip;
  144. uint32_t dst_port;
  145. /** Same as above */
  146. struct sockaddr_in dest_addr;
  147. /** Time when last ack packet was sent. */
  148. double last_ack;
  149. /** Time when a packet was last received. */
  150. double last_activity;
  151. double last_data_activity;
  152. uint16_t window_size;
  153. double ack_interval;
  154. double resend_interval;
  155. icmp_desc_t * send_ring;
  156. forward_desc_t ** recv_ring;
  157. xfer_stats_t xfer;
  158. struct proxy_desc_t * next;
  159. } proxy_desc_t;
  160. proxy_desc_t * create_and_insert_proxy_desc(uint16_t id_no,
  161. uint16_t icmp_id,
  162. int sock,
  163. struct sockaddr_in * addr,
  164. uint32_t dst_ip,
  165. uint32_t dst_port,
  166. uint32_t init_state,
  167. enum pkt_flag type);
  168. void remove_proxy_desc(proxy_desc_t * cur, proxy_desc_t * prev);
  169. void remove_proxy_desc_rings(proxy_desc_t * cur);
  170. forward_desc_t * create_fwd_desc(uint16_t seq_no, uint32_t data_len, char * data);
  171. int queue_packet(
  172. int sock_fd, proxy_desc_t * cur, char * buf, size_t bufsiz, uint32_t dest_ip, uint32_t dest_port, uint32_t state);
  173. uint32_t send_packets(forward_desc_t * ring[], int * xfer_idx, int * await_send, int * sock, uint16_t window_size);
  174. #endif