main.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <signal.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <sys/socket.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. #include <time.h>
  16. #include <tox/tox.h>
  17. #include <unistd.h>
  18. #include "util.h"
  19. #include "uthash.h"
  20. #define PROTOCOL_MAGIC_V1 0xa26a
  21. #define PROTOCOL_MAGIC PROTOCOL_MAGIC_V1
  22. #define PROTOCOL_MAGIC_HIGH (PROTOCOL_MAGIC >> 8)
  23. #define PROTOCOL_MAGIC_LOW (PROTOCOL_MAGIC & 0xff)
  24. #define PACKET_TYPE_PONG 0x0100
  25. #define PACKET_TYPE_PING 0x0108
  26. #define PACKET_TYPE_REQUESTTUNNEL 0x0602
  27. #define PACKET_TYPE_ACKTUNNEL 0x0610
  28. #define PACKET_TYPE_TCP 0x0600
  29. #define PACKET_TYPE_TCP_FIN 0x0601
  30. #define INT16_AT(array,pos) ( (*((array)+(pos)))*256 + (*((array)+(pos)+1)) )
  31. #define BYTE2(number) (((number) / 256) & 0xff)
  32. #define BYTE1(number) ((number)&0xff)
  33. /* Offset of the data buffer in the packet */
  34. #define PROTOCOL_BUFFER_OFFSET 8
  35. #define READ_BUFFER_SIZE TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET
  36. #define PROTOCOL_MAX_PACKET_SIZE (READ_BUFFER_SIZE + PROTOCOL_BUFFER_OFFSET)
  37. typedef struct tunnel_t {
  38. /* The forwarded socket fd */
  39. int sockfd;
  40. /* Connection ID, must be int because of uthash */
  41. int connid;
  42. /* Friend number of remote end */
  43. int32_t friendnumber;
  44. UT_hash_handle hh;
  45. } tunnel;
  46. typedef struct protocol_frame_t {
  47. uint32_t friendnumber;
  48. /* Fields actually found in the protocol */
  49. uint16_t magic;
  50. uint16_t packet_type;
  51. uint16_t connid;
  52. uint16_t data_length;
  53. uint8_t *data;
  54. } protocol_frame;
  55. /**** GLOBAL VARIABLES ****/
  56. extern Tox *tox;
  57. /* Whether we're a client */
  58. extern int client_mode;
  59. /* Just send a ping and exit */
  60. extern int ping_mode;
  61. /* Open a local port and forward it */
  62. extern int client_local_port_mode;
  63. /* Forward stdin/stdout to remote machine - SSH ProxyCommand mode */
  64. extern int client_pipe_mode;
  65. /* Remote Tox ID in client mode */
  66. extern char *remote_tox_id;
  67. /* Ports and hostname for port forwarding */
  68. extern int remote_port;
  69. extern char *remote_host;
  70. extern int local_port;
  71. extern int select_nfds;
  72. extern tunnel *by_id;
  73. int parse_lossless_packet(void *sender_uc, const uint8_t *data, uint32_t len);
  74. tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber);
  75. void tunnel_delete(tunnel *t);
  76. #endif