1
0

AddrSet.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Bits.h"
  16. #include "subnode/AddrSet.h"
  17. #include "util/Identity.h"
  18. struct Elem
  19. {
  20. struct Address addr;
  21. struct Allocator* alloc;
  22. Identity
  23. };
  24. static int comparePeerAddresses(struct Elem* a, struct Elem* b)
  25. {
  26. if (a->addr.path == b->addr.path) { return 0; }
  27. return (a->addr.path < b->addr.path) ? 1 : -1;
  28. }
  29. #define ArrayList_TYPE struct Elem
  30. #define ArrayList_COMPARE comparePeerAddresses
  31. #define ArrayList_NAME OfAddrs
  32. #include "util/ArrayList.h"
  33. struct AddrSet_pvt
  34. {
  35. struct AddrSet pub;
  36. struct ArrayList_OfAddrs* addrList;
  37. struct Allocator* alloc;
  38. Identity
  39. };
  40. static int indexOf(struct AddrSet_pvt* ap, struct Address* addr)
  41. {
  42. for (int i = 0; i < ap->addrList->length; i++) {
  43. struct Elem* el = ArrayList_OfAddrs_get(ap->addrList, i);
  44. // We will just match on the same IP, even if the path is not the same
  45. if (Address_isSameIp(addr, &el->addr)) { return i; }
  46. }
  47. return -1;
  48. }
  49. int AddrSet_indexOf(struct AddrSet* as, struct Address* addr)
  50. {
  51. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  52. return indexOf(ap, addr);
  53. }
  54. void AddrSet_add(struct AddrSet* as, struct Address* addr)
  55. {
  56. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  57. int idx = indexOf(ap, addr);
  58. if (idx != -1) { return; }
  59. struct Allocator* alloc = Allocator_child(ap->alloc);
  60. struct Elem* el = Allocator_calloc(alloc, sizeof(struct Elem), 1);
  61. el->alloc = alloc;
  62. Bits_memcpy(&el->addr, addr, sizeof(struct Address));
  63. Identity_set(el);
  64. ArrayList_OfAddrs_add(ap->addrList, el);
  65. ArrayList_OfAddrs_sort(ap->addrList);
  66. ap->pub.length = ap->addrList->length;
  67. }
  68. void AddrSet_remove(struct AddrSet* as, struct Address* addr)
  69. {
  70. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  71. int idx = indexOf(ap, addr);
  72. if (idx == -1) { return; }
  73. struct Elem* el = ArrayList_OfAddrs_get(ap->addrList, idx);
  74. ArrayList_OfAddrs_remove(ap->addrList, idx);
  75. Allocator_free(el->alloc);
  76. ap->pub.length = ap->addrList->length;
  77. }
  78. void AddrSet_flush(struct AddrSet* as)
  79. {
  80. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  81. for (int i = 0; i < ap->addrList->length; i++) {
  82. struct Elem* el = ArrayList_OfAddrs_get(ap->addrList, i);
  83. ArrayList_OfAddrs_remove(ap->addrList, i);
  84. Allocator_free(el->alloc);
  85. }
  86. ap->pub.length = 0;
  87. }
  88. struct Address* AddrSet_get(struct AddrSet* as, int i)
  89. {
  90. struct AddrSet_pvt* ap = Identity_check((struct AddrSet_pvt*) as);
  91. struct Elem* el = ArrayList_OfAddrs_get(ap->addrList, i);
  92. if (el) { return &el->addr; }
  93. return NULL;
  94. }
  95. struct AddrSet* AddrSet_new(struct Allocator* allocator)
  96. {
  97. struct Allocator* alloc = Allocator_child(allocator);
  98. struct AddrSet_pvt* out = Allocator_calloc(alloc, sizeof(struct AddrSet_pvt), 1);
  99. out->alloc = alloc;
  100. out->addrList = ArrayList_OfAddrs_new(alloc);
  101. Identity_set(out);
  102. return &out->pub;
  103. }