main.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #define PROTOCOL_MAGIC_V1 0xa26a
  24. #define PROTOCOL_MAGIC PROTOCOL_MAGIC_V1
  25. #define PROTOCOL_MAGIC_HIGH (PROTOCOL_MAGIC >> 8)
  26. #define PROTOCOL_MAGIC_LOW (PROTOCOL_MAGIC & 0xff)
  27. #define PACKET_TYPE_PONG 0x0100
  28. #define PACKET_TYPE_PING 0x0108
  29. #define PACKET_TYPE_REQUESTTUNNEL 0x0602
  30. #define PACKET_TYPE_ACKTUNNEL 0x0610
  31. #define PACKET_TYPE_TCP 0x0600
  32. #define PACKET_TYPE_TCP_FIN 0x0601
  33. #define INT16_AT(array,pos) ( (*((array)+(pos)))*256 + (*((array)+(pos)+1)) )
  34. #define BYTE2(number) (((number) / 256) & 0xff)
  35. #define BYTE1(number) ((number)&0xff)
  36. /* Offset of the data buffer in the packet */
  37. #define PROTOCOL_BUFFER_OFFSET 8
  38. #define READ_BUFFER_SIZE TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET
  39. #define PROTOCOL_MAX_PACKET_SIZE (READ_BUFFER_SIZE + PROTOCOL_BUFFER_OFFSET)
  40. typedef struct tunnel_t {
  41. /* The forwarded socket fd */
  42. int sockfd;
  43. /* Connection ID, must be int because of uthash */
  44. int connid;
  45. /* Friend number of remote end */
  46. int32_t friendnumber;
  47. UT_hash_handle hh;
  48. } tunnel;
  49. typedef struct protocol_frame_t {
  50. uint32_t friendnumber;
  51. /* Fields actually found in the protocol */
  52. uint16_t magic;
  53. uint16_t packet_type;
  54. uint16_t connid;
  55. uint16_t data_length;
  56. uint8_t *data;
  57. } protocol_frame;
  58. /**** GLOBAL VARIABLES ****/
  59. extern Tox *tox;
  60. /* Whether we're a client */
  61. extern int client_mode;
  62. /* Just send a ping and exit */
  63. extern int ping_mode;
  64. /* TOX_CONNECTION global variable */
  65. extern TOX_CONNECTION connection_status;
  66. /* Open a local port and forward it */
  67. extern int client_local_port_mode;
  68. /* Forward stdin/stdout to remote machine - SSH ProxyCommand mode */
  69. extern int client_pipe_mode;
  70. /* Remote Tox ID in client mode */
  71. extern char *remote_tox_id;
  72. /* Ports and hostname for port forwarding */
  73. extern int remote_port;
  74. extern char *remote_host;
  75. extern int local_port;
  76. /* Shared secret used for authentication */
  77. extern int use_shared_secret;
  78. extern char shared_secret[TOX_MAX_FRIEND_REQUEST_LENGTH];
  79. extern int select_nfds;
  80. extern tunnel *by_id;
  81. void parse_lossless_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, size_t len, void *tmp);
  82. tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber);
  83. void tunnel_delete(tunnel *t);
  84. void print_version(void);
  85. #endif