pex.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #ifndef __UNETD_PEX_H
  6. #define __UNETD_PEX_H
  7. #include <sys/socket.h>
  8. #include <libubox/uloop.h>
  9. #include "stun.h"
  10. #define NETWORK_PEX_HOSTS_LIMIT 128
  11. struct network;
  12. struct network_pex_host {
  13. struct list_head list;
  14. uint64_t timeout;
  15. uint64_t last_active;
  16. uint64_t last_ping;
  17. union network_endpoint endpoint;
  18. };
  19. struct network_pex {
  20. struct uloop_fd fd;
  21. struct list_head hosts;
  22. int num_hosts;
  23. struct uloop_timeout request_update_timer;
  24. };
  25. enum network_stun_state {
  26. STUN_STATE_IDLE,
  27. STUN_STATE_PEX_QUERY_WAIT,
  28. STUN_STATE_STUN_QUERY_SEND,
  29. STUN_STATE_STUN_QUERY_WAIT,
  30. };
  31. struct network_stun_server {
  32. struct list_head list;
  33. struct avl_node pending_node;
  34. struct stun_request req;
  35. const char *host;
  36. uint8_t seq;
  37. bool req_auth_port;
  38. bool pending;
  39. };
  40. struct network_stun {
  41. struct list_head servers;
  42. struct avl_tree pending;
  43. struct uloop_timeout timer;
  44. enum network_stun_state state;
  45. bool wgport_disabled;
  46. uint16_t auth_port_ext;
  47. uint16_t port_local;
  48. uint16_t port_ext;
  49. int retry;
  50. struct uloop_fd socket;
  51. };
  52. enum pex_event {
  53. PEX_EV_HANDSHAKE,
  54. PEX_EV_ENDPOINT_CHANGE,
  55. PEX_EV_QUERY,
  56. PEX_EV_PING,
  57. };
  58. void network_pex_init(struct network *net);
  59. int network_pex_open(struct network *net);
  60. void network_pex_close(struct network *net);
  61. void network_pex_free(struct network *net);
  62. void network_pex_event(struct network *net, struct network_peer *peer,
  63. enum pex_event ev);
  64. void network_pex_create_host(struct network *net, union network_endpoint *ep,
  65. unsigned int timeout);
  66. void network_stun_init(struct network *net);
  67. void network_stun_free(struct network *net);
  68. void network_stun_server_add(struct network *net, const char *host);
  69. void network_stun_rx_packet(struct network *net, const void *data, size_t len);
  70. void network_stun_update_port(struct network *net, bool auth, uint16_t val);
  71. void network_stun_start(struct network *net);
  72. static inline bool network_pex_active(struct network_pex *pex)
  73. {
  74. return pex->fd.fd >= 0;
  75. }
  76. int global_pex_open(const char *unix_path);
  77. #endif