main.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef _MAIN_H
  2. #define _MAIN_H
  3. #include <arpa/inet.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <netdb.h>
  7. #include <netinet/in.h>
  8. #include <pwd.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/socket.h>
  14. #include <sys/stat.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <syslog.h>
  18. #include <time.h>
  19. #include <tox/tox.h>
  20. #include <unistd.h>
  21. #include "util.h"
  22. #include "uthash.h"
  23. #include "utlist.h"
  24. #define PROTOCOL_MAGIC_V1 0xa26a
  25. #define PROTOCOL_MAGIC PROTOCOL_MAGIC_V1
  26. #define PROTOCOL_MAGIC_HIGH (PROTOCOL_MAGIC >> 8)
  27. #define PROTOCOL_MAGIC_LOW (PROTOCOL_MAGIC & 0xff)
  28. #define PACKET_TYPE_PONG 0x0100
  29. #define PACKET_TYPE_PING 0x0108
  30. #define PACKET_TYPE_REQUESTTUNNEL 0x0602
  31. #define PACKET_TYPE_ACKTUNNEL 0x0610
  32. #define PACKET_TYPE_TCP 0x0600
  33. #define PACKET_TYPE_TCP_FIN 0x0601
  34. #define INT16_AT(array,pos) ( (*((array)+(pos)))*256 + (*((array)+(pos)+1)) )
  35. #define BYTE2(number) (((number) / 256) & 0xff)
  36. #define BYTE1(number) ((number)&0xff)
  37. /* Offset of the data buffer in the packet */
  38. #define PROTOCOL_BUFFER_OFFSET 8
  39. #define READ_BUFFER_SIZE TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET
  40. #define PROTOCOL_MAX_PACKET_SIZE (READ_BUFFER_SIZE + PROTOCOL_BUFFER_OFFSET)
  41. typedef struct tunnel_t {
  42. /* The forwarded socket fd */
  43. int sockfd;
  44. /* Connection ID, must be int because of uthash */
  45. int connid;
  46. /* Friend number of remote end */
  47. uint32_t friendnumber;
  48. UT_hash_handle hh;
  49. } tunnel;
  50. typedef struct allowed_toxid {
  51. uint8_t toxid[TOX_ADDRESS_SIZE];
  52. struct allowed_toxid *next;
  53. } allowed_toxid;
  54. typedef struct protocol_frame_t {
  55. uint32_t friendnumber;
  56. /* Fields actually found in the protocol */
  57. uint16_t magic;
  58. uint16_t packet_type;
  59. uint16_t connid;
  60. uint16_t data_length;
  61. uint8_t *data;
  62. } protocol_frame;
  63. /* Rules policy */
  64. enum rules_policy_enum { VALIDATE, NONE };
  65. typedef struct rule {
  66. uint16_t port;
  67. char * host;
  68. struct rule *next;
  69. } rule;
  70. /**** GLOBAL VARIABLES ****/
  71. extern Tox *tox;
  72. /* Whether we're a client */
  73. extern int client_mode;
  74. /* Just send a ping and exit */
  75. extern int ping_mode;
  76. /* TOX_CONNECTION global variable */
  77. extern TOX_CONNECTION connection_status;
  78. /* Open a local port and forward it */
  79. extern int client_local_port_mode;
  80. /* Forward stdin/stdout to remote machine - SSH ProxyCommand mode */
  81. extern int client_pipe_mode;
  82. /* Remote Tox ID in client mode */
  83. extern uint8_t *remote_tox_id;
  84. /* Ports and hostname for port forwarding */
  85. extern int remote_port;
  86. extern char *remote_host;
  87. extern int local_port;
  88. /* Shared secret used for authentication */
  89. extern int use_shared_secret;
  90. extern char shared_secret[TOX_MAX_FRIEND_REQUEST_LENGTH];
  91. extern int select_nfds;
  92. extern tunnel *by_id;
  93. void parse_lossless_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, size_t len, void *tmp);
  94. tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber);
  95. void tunnel_delete(tunnel *t);
  96. void update_select_nfds(int fd);
  97. int send_frame(protocol_frame *frame, uint8_t *data);
  98. int send_tunnel_request_packet(char *remote_host, int remote_port, int friend_number);
  99. void update_select_nfds(int fd);
  100. int send_frame(protocol_frame *frame, uint8_t *data);
  101. int send_tunnel_request_packet(char *remote_host, int remote_port, int friend_number);
  102. void print_version(void);
  103. #endif