gnunet-service-xdht_hello.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file dht/gnunet-service-xdht_hello.c
  19. * @brief GNUnet DHT integration with peerinfo
  20. * @author Christian Grothoff
  21. *
  22. * TODO:
  23. * - consider adding mechanism to remove expired HELLOs
  24. */
  25. #include "platform.h"
  26. #include "gnunet-service-xdht.h"
  27. #include "gnunet-service-xdht_hello.h"
  28. #include "gnunet_peerinfo_service.h"
  29. /**
  30. * Handle for peerinfo notifications.
  31. */
  32. static struct GNUNET_PEERINFO_NotifyContext *pnc;
  33. /**
  34. * Hash map of peers to HELLOs.
  35. */
  36. static struct GNUNET_CONTAINER_MultiPeerMap *peer_to_hello;
  37. /**
  38. * Obtain a peer's HELLO if available
  39. *
  40. * @param peer peer to look for a HELLO from
  41. * @return HELLO for the given peer
  42. */
  43. const struct GNUNET_HELLO_Message *
  44. GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer)
  45. {
  46. if (NULL == peer_to_hello)
  47. return NULL;
  48. return GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
  49. }
  50. /**
  51. * Function called for each HELLO known to PEERINFO.
  52. *
  53. * @param cls closure
  54. * @param peer id of the peer, NULL for last call
  55. * @param hello hello message for the peer (can be NULL)
  56. * @param err_msg error message (not used)
  57. */
  58. static void
  59. process_hello (void *cls, const struct GNUNET_PeerIdentity *peer,
  60. const struct GNUNET_HELLO_Message *hello, const char *err_msg)
  61. {
  62. struct GNUNET_TIME_Absolute ex;
  63. struct GNUNET_HELLO_Message *hm;
  64. if (hello == NULL)
  65. return;
  66. ex = GNUNET_HELLO_get_last_expiration (hello);
  67. if (0 == GNUNET_TIME_absolute_get_remaining (ex).rel_value_us)
  68. return;
  69. GNUNET_STATISTICS_update (GDS_stats,
  70. gettext_noop ("# HELLOs obtained from peerinfo"), 1,
  71. GNUNET_NO);
  72. hm = GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer);
  73. GNUNET_free_non_null (hm);
  74. hm = GNUNET_malloc (GNUNET_HELLO_size (hello));
  75. memcpy (hm, hello, GNUNET_HELLO_size (hello));
  76. GNUNET_assert (GNUNET_SYSERR !=
  77. GNUNET_CONTAINER_multipeermap_put (peer_to_hello,
  78. peer, hm,
  79. GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
  80. }
  81. /**
  82. * Initialize HELLO subsystem.
  83. */
  84. void
  85. GDS_HELLO_init ()
  86. {
  87. pnc = GNUNET_PEERINFO_notify (GDS_cfg, GNUNET_NO, &process_hello, NULL);
  88. peer_to_hello = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
  89. }
  90. /**
  91. * Free memory occopied by the HELLO.
  92. */
  93. static int
  94. free_hello (void *cls,
  95. const struct GNUNET_PeerIdentity *key,
  96. void *hello)
  97. {
  98. GNUNET_free (hello);
  99. return GNUNET_OK;
  100. }
  101. /**
  102. * Shutdown HELLO subsystem.
  103. */
  104. void
  105. GDS_HELLO_done ()
  106. {
  107. if (NULL != pnc)
  108. {
  109. GNUNET_PEERINFO_notify_cancel (pnc);
  110. pnc = NULL;
  111. }
  112. if (NULL != peer_to_hello)
  113. {
  114. GNUNET_CONTAINER_multipeermap_iterate (peer_to_hello, &free_hello, NULL);
  115. GNUNET_CONTAINER_multipeermap_destroy (peer_to_hello);
  116. }
  117. }
  118. /* end of gnunet-service-dht_hello.c */