pex-msg.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef __PEX_MSG_H
  2. #define __PEX_MSG_H
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include "curve25519.h"
  7. #include "siphash.h"
  8. #include "utils.h"
  9. #define UNETD_GLOBAL_PEX_PORT 51819
  10. #define PEX_BUF_SIZE 1024
  11. #define PEX_RX_BUF_SIZE 16384
  12. #define UNETD_NET_DATA_SIZE_MAX (128 * 1024)
  13. enum pex_opcode {
  14. PEX_MSG_HELLO,
  15. PEX_MSG_NOTIFY_PEERS,
  16. PEX_MSG_QUERY,
  17. PEX_MSG_PING,
  18. PEX_MSG_PONG,
  19. PEX_MSG_UPDATE_REQUEST,
  20. PEX_MSG_UPDATE_RESPONSE,
  21. PEX_MSG_UPDATE_RESPONSE_DATA,
  22. PEX_MSG_UPDATE_RESPONSE_NO_DATA,
  23. PEX_MSG_ENDPOINT_NOTIFY,
  24. PEX_MSG_ENDPOINT_PORT_NOTIFY,
  25. };
  26. #define PEX_ID_LEN 8
  27. struct pex_hdr {
  28. uint8_t version;
  29. uint8_t opcode;
  30. uint16_t len;
  31. uint8_t id[PEX_ID_LEN];
  32. };
  33. struct pex_ext_hdr {
  34. uint64_t nonce;
  35. uint8_t auth_id[PEX_ID_LEN];
  36. };
  37. #define PEER_EP_F_IPV6 (1 << 0)
  38. #define PEER_EP_F_LOCAL (1 << 1)
  39. struct pex_peer_endpoint {
  40. uint16_t flags;
  41. uint16_t port;
  42. uint8_t peer_id[PEX_ID_LEN];
  43. uint8_t addr[16];
  44. };
  45. struct pex_hello {
  46. uint16_t flags;
  47. uint8_t local_addr[16];
  48. };
  49. struct pex_update_request {
  50. uint64_t req_id; /* must be first */
  51. uint64_t cur_version;
  52. };
  53. struct pex_update_response {
  54. uint64_t req_id; /* must be first */
  55. uint32_t data_len;
  56. uint8_t e_key[CURVE25519_KEY_SIZE];
  57. };
  58. struct pex_update_response_data {
  59. uint64_t req_id; /* must be first */
  60. uint32_t offset;
  61. };
  62. struct pex_update_response_no_data {
  63. uint64_t req_id; /* must be first */
  64. uint64_t cur_version;
  65. };
  66. struct pex_endpoint_port_notify {
  67. uint16_t port;
  68. };
  69. struct pex_msg_update_send_ctx {
  70. const uint8_t *pubkey;
  71. const uint8_t *auth_key;
  72. uint64_t req_id;
  73. bool ext;
  74. void *data;
  75. void *cur;
  76. int rem;
  77. };
  78. struct pex_msg_local_control {
  79. int msg_type;
  80. uint8_t auth_id[PEX_ID_LEN];
  81. union network_endpoint ep;
  82. int timeout;
  83. };
  84. struct pex_hdr *pex_rx_accept(void *data, size_t len, bool ext);
  85. typedef void (*pex_recv_cb_t)(void *data, size_t len, struct sockaddr_in6 *addr);
  86. typedef void (*pex_recv_control_cb_t)(struct pex_msg_local_control *msg, int len);
  87. int pex_open(void *addr, size_t addr_len, pex_recv_cb_t cb, bool server);
  88. int pex_unix_open(const char *path, pex_recv_control_cb_t cb);
  89. void pex_close(void);
  90. int pex_socket(void);
  91. int pex_raw_socket(int family);
  92. uint64_t pex_network_hash(const uint8_t *auth_key, uint64_t req_id);
  93. struct pex_hdr *__pex_msg_init(const uint8_t *pubkey, uint8_t opcode);
  94. struct pex_hdr *__pex_msg_init_ext(const uint8_t *pubkey, const uint8_t *auth_key,
  95. uint8_t opcode, bool ext);
  96. int __pex_msg_send(int fd, const void *addr, void *ip_hdr, size_t ip_hdrlen);
  97. void *pex_msg_append(size_t len);
  98. struct pex_update_request *
  99. pex_msg_update_request_init(const uint8_t *pubkey, const uint8_t *priv_key,
  100. const uint8_t *auth_key, union network_endpoint *addr,
  101. uint64_t cur_version, bool ext);
  102. void *pex_msg_update_response_recv(const void *data, int len, enum pex_opcode op,
  103. int *data_len, uint64_t *timestamp);
  104. void pex_msg_update_response_init(struct pex_msg_update_send_ctx *ctx,
  105. const uint8_t *pubkey, const uint8_t *auth_key,
  106. const uint8_t *peer_key, bool ext,
  107. struct pex_update_request *req,
  108. const void *data, int len);
  109. bool pex_msg_update_response_continue(struct pex_msg_update_send_ctx *ctx);
  110. #endif