main.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef _MAIN_H
  2. #define _MAIN_H
  3. #include <arpa/inet.h>
  4. #include <ctype.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <limits.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <pwd.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/socket.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19. #include <syslog.h>
  20. #include <time.h>
  21. #include <tox/tox.h>
  22. #include <unistd.h>
  23. #include "util.h"
  24. #include "uthash.h"
  25. #include "utlist.h"
  26. #define PROTOCOL_MAGIC_V1 0xa26a
  27. #define PROTOCOL_MAGIC_V2 0xa26b
  28. #define PROTOCOL_MAGIC PROTOCOL_MAGIC_V2
  29. #define PROTOCOL_MAGIC_HIGH (PROTOCOL_MAGIC >> 8)
  30. #define PROTOCOL_MAGIC_LOW (PROTOCOL_MAGIC & 0xff)
  31. #define PACKET_TYPE_PONG 0x0100
  32. #define PACKET_TYPE_PING 0x0108
  33. #define PACKET_TYPE_REQUESTTUNNEL 0x0602 /* TODO - currently unused */
  34. #define PACKET_TYPE_DENYTUNNEL 0x0604
  35. #define PACKET_TYPE_ACKTUNNEL 0x0610
  36. #define PACKET_TYPE_TCP 0x0600
  37. #define PACKET_TYPE_TCP_FIN 0x0601
  38. #define INT16_AT(array,pos) ( (*((array)+(pos)))*256 + (*((array)+(pos)+1)) )
  39. #define INT32_AT(array,pos) ( (*((array)+(pos)))*256*256*256 + (*((array)+(pos)+1))*256*256 +(*((array)+(pos)+2))*256 + (*((array)+(pos)+3)) )
  40. #define BYTE1(number) ((number)&0xff)
  41. #define BYTE2(number) (((number) / 256) & 0xff)
  42. #define BYTE3(number) (((number) / (256*256)) & 0xff)
  43. #define BYTE4(number) (((number) / (256*256*256)) & 0xff)
  44. /* Offset of the data buffer in the packet */
  45. #define PROTOCOL_BUFFER_OFFSET 8
  46. #define READ_BUFFER_SIZE TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET
  47. #define PROTOCOL_MAX_PACKET_SIZE (READ_BUFFER_SIZE + PROTOCOL_BUFFER_OFFSET)
  48. typedef struct tunnel_t {
  49. /* The forwarded socket fd */
  50. int sockfd;
  51. /* Connection ID, must be int because of uthash */
  52. int connid;
  53. /* Friend number of remote end */
  54. uint32_t friendnumber;
  55. UT_hash_handle hh;
  56. } tunnel;
  57. typedef struct tunnel_list_t {
  58. tunnel *tun;
  59. struct tunnel_list_t *next;
  60. } tunnel_list;
  61. typedef struct allowed_toxid {
  62. uint8_t toxid[TOX_ADDRESS_SIZE];
  63. struct allowed_toxid *next;
  64. } allowed_toxid;
  65. typedef struct protocol_frame_t {
  66. uint32_t friendnumber;
  67. /* Fields actually found in the protocol */
  68. uint16_t magic;
  69. uint16_t packet_type;
  70. uint16_t connid;
  71. uint16_t data_length;
  72. uint8_t *data;
  73. } protocol_frame;
  74. /* A list of local port forwards (listen locally, forward to server) */
  75. typedef struct local_port_forward_t {
  76. int local_port;
  77. char *remote_host;
  78. int remote_port;
  79. /* Sock representing the local port - call accept() on it */
  80. int bind_sockfd;
  81. /* If tunnel open is pending, accepted sockfd is temporarly stored here */
  82. /* -1 = we can accept another connection */
  83. int accept_sockfd;
  84. /* When the forward has been created - used in ack timeouts */
  85. time_t created;
  86. /* ID - used to identify the ack frame */
  87. uint32_t forward_id;
  88. struct local_port_forward_t *next;
  89. } local_port_forward;
  90. /* Rules policy */
  91. enum rules_policy_enum { VALIDATE, NONE };
  92. typedef struct rule {
  93. uint16_t port;
  94. char * host;
  95. struct rule *next;
  96. } rule;
  97. /**** GLOBAL VARIABLES ****/
  98. extern Tox *tox;
  99. /* Whether we're a client */
  100. extern int client_mode;
  101. /* Just send a ping and exit */
  102. extern int ping_mode;
  103. /* TOX_CONNECTION global variable */
  104. extern TOX_CONNECTION connection_status;
  105. /* Open a local port and forward it */
  106. extern int client_local_port_mode;
  107. /* Forward stdin/stdout to remote machine - SSH ProxyCommand mode */
  108. extern int client_pipe_mode;
  109. /* Remote Tox ID in client mode */
  110. extern uint8_t *remote_tox_id;
  111. /* Ports and hostname for port forwarding */
  112. extern int remote_port;
  113. extern char *remote_host;
  114. extern int local_port;
  115. /* Shared secret used for authentication */
  116. extern int use_shared_secret;
  117. extern char shared_secret[TOX_MAX_FRIEND_REQUEST_LENGTH];
  118. extern int select_nfds;
  119. extern tunnel *by_id;
  120. extern local_port_forward *local_port_forwards;
  121. local_port_forward *find_pending_forward_by_id(uint32_t local_forward_id);
  122. void parse_lossless_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, size_t len, void *tmp);
  123. tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber);
  124. void tunnel_delete(tunnel *t);
  125. void update_select_nfds(int fd);
  126. int send_frame(protocol_frame *frame, uint8_t *data);
  127. int send_tunnel_request_packet(char *remote_host, int remote_port, uint32_t local_forward_id, int friend_number);
  128. void print_version(void);
  129. void print_version_stdout(void);
  130. #endif