ptunnel.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * ptunnel.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. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. *
  18. * - Neither the name of the Yellow Lemon Software nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * Contacting the author:
  35. * You can get in touch with me, Daniel Stoedle (that's the Norwegian letter oe,
  36. * in case your text editor didn't realize), here: <daniels@cs.uit.no>
  37. *
  38. * The official ptunnel website is here:
  39. * <http://www.cs.uit.no/~daniels/PingTunnel/>
  40. *
  41. * Note that the source code is best viewed with tabs set to 4 spaces.
  42. */
  43. #ifndef PING_TUNNEL_H
  44. #define PING_TUNNEL_H 1
  45. #ifndef WIN32
  46. #ifdef HAVE_ICMPFILTER
  47. #include <linux/icmp.h>
  48. #endif
  49. #ifdef HAVE_SYS_UNISTD_H
  50. #include <sys/unistd.h>
  51. #endif
  52. #include <sys/types.h>
  53. #include <sys/socket.h>
  54. #include <netinet/in.h>
  55. #include <arpa/inet.h>
  56. #include <netdb.h>
  57. #include <pthread.h>
  58. #include <errno.h>
  59. #include <net/ethernet.h>
  60. #include <syslog.h>
  61. #include <pwd.h>
  62. #include <grp.h>
  63. #endif /* !WIN32 */
  64. #include <stdarg.h>
  65. #include <unistd.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include <time.h>
  70. #include <signal.h>
  71. #include <stdint.h>
  72. #include <stdbool.h>
  73. #ifdef HAVE_PCAP
  74. #include <pcap.h>
  75. #endif
  76. #include "pkt.h"
  77. #include "pdesc.h"
  78. #include "challenge.h"
  79. #ifdef WIN32
  80. /* pthread porting to windows */
  81. typedef CRITICAL_SECTION pthread_mutex_t;
  82. typedef unsigned long pthread_t;
  83. #define pthread_mutex_init InitializeCriticalSectionAndSpinCount
  84. #define pthread_mutex_lock EnterCriticalSection
  85. #define pthread_mutex_unlock LeaveCriticalSection
  86. #endif
  87. extern pthread_mutex_t chain_lock;
  88. extern uint32_t num_tunnels;
  89. extern const int icmp_receive_buf_len;
  90. extern proxy_desc_t *chain;
  91. extern time_t *seq_expiry_tbl;
  92. extern const char *state_name[kNum_proto_types];
  93. /* pt_thread_info_t: A simple (very simple, in fact) structure that allows us
  94. * to pass an arbitrary number of params to the threads we create. Currently,
  95. * that's just one single parameter: The socket which the thread should listen
  96. * to.
  97. */
  98. typedef struct {
  99. int sock;
  100. } pt_thread_info_t;
  101. #ifdef HAVE_PCAP
  102. /* pqueue_elem_t: An queue element in the pqueue structure (below).
  103. */
  104. typedef struct pqueue_elem_t {
  105. /** size of data buffer */
  106. unsigned long bytes;
  107. /** next queue element (if any) */
  108. struct pqueue_elem_t *next;
  109. /** optional data */
  110. char data[0];
  111. } pqueue_elem_t;
  112. /* pqueue_t: A simple queue strucutre.
  113. */
  114. typedef struct {
  115. pqueue_elem_t *head;
  116. pqueue_elem_t *tail;
  117. int elems;
  118. } pqueue_t;
  119. /* pcap_info_t: Structure to hold information related to packet capturing.
  120. */
  121. typedef struct {
  122. pcap_t *pcap_desc;
  123. /** compiled filter program */
  124. struct bpf_program fp;
  125. uint32_t netp;
  126. uint32_t netmask;
  127. /** buffers for error info */
  128. char *pcap_err_buf;
  129. /** buffers for packet info */
  130. char *pcap_data_buf;
  131. /** queue of packets to process */
  132. pqueue_t pkt_q;
  133. } pcap_info_t;
  134. #endif
  135. /* function Prototypes */
  136. #ifndef WIN32
  137. void * pt_proxy(void *args);
  138. #else
  139. unsigned int __stdcall pt_proxy(void *args);
  140. #endif
  141. #ifdef HAVE_PCAP
  142. void pcap_packet_handler(u_char *refcon, const struct pcap_pkthdr *hdr,
  143. const u_char* pkt);
  144. #endif
  145. void pt_forwarder(void);
  146. void print_statistics(xfer_stats_t *xfer, int is_continuous);
  147. uint16_t calc_icmp_checksum(uint16_t *data, int bytes);
  148. void send_termination_msg(proxy_desc_t *cur, int icmp_sock);
  149. #endif