pkt.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef PKT_H
  2. #define PKT_H 1
  3. #include <stdint.h>
  4. #ifdef WIN32
  5. #include <winsock2.h>
  6. typedef int socklen_t;
  7. typedef uint32_t in_addr_t;
  8. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  9. struct ether_header {
  10. u_int8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
  11. u_int8_t ether_shost[ETH_ALEN]; /* source ether addr */
  12. u_int16_t ether_type; /* packet type ID field */
  13. };
  14. #endif /* WIN32 */
  15. /** Resend packets after this interval (in seconds) */
  16. #define kResend_interval 1.5
  17. /** ping_tunnel_pkt_t: This data structure represents the header of a ptunnel
  18. * packet, consisting of a magic number, the tunnel's destination IP and port,
  19. * as well as some other fields. Note that the dest IP and port is only valid
  20. * in packets from the client to the proxy.
  21. */
  22. typedef struct {
  23. /** magic number, used to identify ptunnel packets. */
  24. uint32_t magic;
  25. /** destination IP and port (used by proxy to figure */
  26. uint32_t dst_ip;
  27. /** out where to tunnel to) */
  28. uint32_t dst_port;
  29. /** current connection state; see constants above. */
  30. uint32_t state;
  31. /** sequence number of last packet received from other end */
  32. uint32_t ack;
  33. /** length of data buffer */
  34. uint32_t data_len;
  35. /** sequence number of this packet */
  36. uint16_t seq_no;
  37. /** id number, used to separate different tunnels from each other */
  38. uint16_t id_no;
  39. /** optional data buffer */
  40. char data[0];
  41. } __attribute__ ((packed)) ping_tunnel_pkt_t;
  42. /** ip_packet_t: This is basically my own definition of the IP packet, which
  43. * of course complies with the official definition ;) See any good book on IP
  44. * (or even the RFC) for info on the contents of this packet.
  45. */
  46. typedef struct {
  47. uint8_t vers_ihl;
  48. uint8_t tos;
  49. uint16_t pkt_len;
  50. uint16_t id;
  51. uint16_t flags_frag_offset;
  52. uint8_t ttl;
  53. uint8_t proto; // 1 for ICMP
  54. uint16_t checksum;
  55. uint32_t src_ip;
  56. uint32_t dst_ip;
  57. char data[0];
  58. } __attribute__ ((packed)) ip_packet_t;
  59. /** icmp_echo_packet_t: This is the definition of a standard ICMP header. The
  60. * ptunnel packets are constructed as follows:
  61. * [ ip header (20 bytes) ]
  62. * [ icmp header (8 bytes) ]
  63. * [ ptunnel header (28 bytes) ]
  64. *
  65. * We actually only create the ICMP and ptunnel headers, the IP header is
  66. * taken care of by the OS.
  67. */
  68. typedef struct {
  69. uint8_t type;
  70. uint8_t code;
  71. uint16_t checksum;
  72. uint16_t identifier;
  73. uint16_t seq;
  74. char data[0];
  75. } __attribute__ ((packed)) icmp_echo_packet_t;
  76. typedef struct forward_desc_t forward_desc_t;
  77. typedef struct icmp_desc_t icmp_desc_t;
  78. void handle_packet(char *buf, unsigned bytes, int is_pcap, struct sockaddr_in *addr, int icmp_sock);
  79. void handle_data(icmp_echo_packet_t *pkt, int total_len, forward_desc_t **ring,
  80. int *await_send, int *insert_idx, uint16_t *next_expected_seq);
  81. void handle_ack(uint16_t seq_no, icmp_desc_t *ring, int *packets_awaiting_ack,
  82. int one_ack_only, int insert_idx, int *first_ack,
  83. uint16_t *remote_ack, int is_pcap);
  84. #endif