host.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #ifndef __UNETD_HOST_H
  6. #define __UNETD_HOST_H
  7. enum peer_endpoint_type {
  8. ENDPOINT_TYPE_STATIC,
  9. ENDPOINT_TYPE_PEX,
  10. ENDPOINT_TYPE_ENDPOINT_NOTIFY,
  11. ENDPOINT_TYPE_ENDPOINT_PORT_NOTIFY,
  12. __ENDPOINT_TYPE_MAX,
  13. };
  14. struct network_peer {
  15. struct vlist_node node;
  16. uint8_t key[CURVE25519_KEY_SIZE];
  17. union network_addr local_addr;
  18. const char *endpoint;
  19. struct blob_attr *ipaddr;
  20. struct blob_attr *subnet;
  21. int port;
  22. int pex_port;
  23. bool dynamic;
  24. bool indirect;
  25. struct {
  26. int connect_attempt;
  27. bool connected;
  28. bool handshake;
  29. bool has_local_ep_addr;
  30. bool pinged;
  31. union network_addr local_ep_addr;
  32. union network_endpoint endpoint;
  33. uint8_t next_endpoint_idx;
  34. union network_endpoint next_endpoint[__ENDPOINT_TYPE_MAX];
  35. uint64_t last_ep_update;
  36. uint64_t rx_bytes;
  37. uint64_t last_handshake;
  38. uint64_t last_request;
  39. uint64_t last_query_sent;
  40. int idle;
  41. int num_net_queries;
  42. } state;
  43. };
  44. struct network_dynamic_peer {
  45. struct list_head list;
  46. struct network_peer peer;
  47. };
  48. struct network_host {
  49. struct avl_node node;
  50. const char *gateway;
  51. struct network_peer peer;
  52. };
  53. struct network_group {
  54. struct avl_node node;
  55. const char *name;
  56. int n_members;
  57. struct network_host **members;
  58. };
  59. static inline const char *network_host_name(struct network_host *host)
  60. {
  61. if (!host)
  62. return "(none)";
  63. return host->node.key;
  64. }
  65. static inline bool network_host_is_peer(struct network_host *host)
  66. {
  67. return !!host->peer.node.avl.key;
  68. }
  69. static inline const char *network_peer_name(struct network_peer *peer)
  70. {
  71. struct network_host *host;
  72. if (!peer || peer->dynamic)
  73. return "(none)";
  74. host = container_of(peer, struct network_host, peer);
  75. return network_host_name(host);
  76. }
  77. static inline bool
  78. network_host_uses_peer_route(struct network_host *host, struct network *net,
  79. struct network_peer *peer)
  80. {
  81. struct network_host *peer_host = container_of(peer, struct network_host, peer);
  82. if (host == peer_host || host == net->net_config.local_host)
  83. return false;
  84. if (net->net_config.local_host->gateway &&
  85. !strcmp(net->net_config.local_host->gateway, network_peer_name(peer)))
  86. return true;
  87. if (peer_host->gateway &&
  88. !strcmp(peer_host->gateway, network_host_name(net->net_config.local_host)))
  89. return true;
  90. if (!host->gateway)
  91. return false;
  92. return !strcmp(host->gateway, network_peer_name(peer));
  93. }
  94. #define for_each_routed_host(cur_host, net, peer) \
  95. avl_for_each_element(&(net)->hosts, cur_host, node) \
  96. if (network_host_uses_peer_route(host, net, peer))
  97. void network_hosts_update_start(struct network *net);
  98. void network_hosts_update_done(struct network *net);
  99. void network_hosts_add(struct network *net, struct blob_attr *hosts);
  100. void network_hosts_reload_dynamic_peers(struct network *net);
  101. void network_hosts_init(struct network *net);
  102. void network_hosts_free(struct network *net);
  103. #endif