pdesc.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. int seq_no;
  73. /** length of data */
  74. int length;
  75. /** amount of data not yet transferred */
  76. int 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. int pkt_len;
  88. double last_resend;
  89. int resend_count;
  90. uint16_t seq_no;
  91. uint16_t icmp_id;
  92. icmp_echo_packet_t *pkt;
  93. } icmp_desc_t;
  94. /** xfer_stats_t: Various transfer statistics, such as bytes sent and received,
  95. * number of ping packets sent/received, etc.
  96. */
  97. typedef struct xfer_stats_t {
  98. double bytes_in;
  99. double bytes_out;
  100. uint32_t icmp_in;
  101. uint32_t icmp_out;
  102. uint32_t icmp_resent;
  103. uint32_t icmp_ack_out;
  104. } xfer_stats_t;
  105. /** proxy_desc_t: This massive structure describes a tunnel instance.
  106. */
  107. typedef struct proxy_desc_t {
  108. /** ICMP or UDP socket */
  109. int sock;
  110. /** number of bytes in receive buffer */
  111. int bytes;
  112. /** set to true once this instance should be removed */
  113. int should_remove;
  114. /** data buffer, used to receive ping and pong packets */
  115. char *buf;
  116. uint16_t id_no;
  117. uint16_t my_seq;
  118. uint16_t ping_seq;
  119. uint16_t next_remote_seq;
  120. uint16_t pkt_type;
  121. uint16_t remote_ack_val;
  122. uint16_t icmp_id;
  123. /** first available slot in recv ring */
  124. int recv_idx;
  125. /** current slot in recv ring being transferred */
  126. int recv_xfer_idx;
  127. /** first available slot in send ring */
  128. int send_idx;
  129. /** first packet in send ring not yet acked */
  130. int send_first_ack;
  131. /** number of items in recv ring awaiting send */
  132. int recv_wait_send;
  133. /** number of items in send ring awaiting ack */
  134. int send_wait_ack;
  135. int next_resend_start;
  136. int authenticated;
  137. /** Contains the challenge, if used. */
  138. challenge_t *challenge;
  139. /** Protocol state */
  140. uint32_t state;
  141. /** Either kProxy_flag or kUser_flag */
  142. uint32_t type_flag;
  143. /** IP and port to which data should be forwarded. */
  144. uint32_t dst_ip;
  145. uint32_t dst_port;
  146. /** Same as above */
  147. struct sockaddr_in dest_addr;
  148. /** Time when last ack packet was sent. */
  149. double last_ack;
  150. /** Time when a packet was last received. */
  151. double last_activity;
  152. icmp_desc_t send_ring[kPing_window_size];
  153. forward_desc_t *recv_ring[kPing_window_size];
  154. xfer_stats_t xfer;
  155. struct proxy_desc_t *next;
  156. } proxy_desc_t;
  157. proxy_desc_t* create_and_insert_proxy_desc(uint16_t id_no, uint16_t icmp_id,
  158. int sock, struct sockaddr_in *addr,
  159. uint32_t dst_ip, uint32_t dst_port,
  160. uint32_t init_state, uint32_t type);
  161. void remove_proxy_desc(proxy_desc_t *cur, proxy_desc_t *prev);
  162. forward_desc_t* create_fwd_desc(uint16_t seq_no, uint32_t data_len, char *data);
  163. int queue_packet(int icmp_sock, uint8_t type, char *buf, int num_bytes,
  164. uint16_t id_no, uint16_t icmp_id, uint16_t *seq, icmp_desc_t ring[],
  165. int *insert_idx, int *await_send, uint32_t ip, uint32_t port,
  166. uint32_t state, struct sockaddr_in *dest_addr, uint16_t next_expected_seq,
  167. int *first_ack, uint16_t *ping_seq);
  168. uint32_t send_packets(forward_desc_t *ring[], int *xfer_idx, int *await_send, int *sock);
  169. #endif