gnunet-service-messenger_contact.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2020 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Tobias Frisch
  18. * @file src/messenger/gnunet-service-messenger_contact.c
  19. * @brief GNUnet MESSENGER service
  20. */
  21. #include "gnunet-service-messenger_contact.h"
  22. struct GNUNET_MESSENGER_SrvContact*
  23. create_contact (const struct GNUNET_IDENTITY_PublicKey *key)
  24. {
  25. struct GNUNET_MESSENGER_SrvContact *contact = GNUNET_new(struct GNUNET_MESSENGER_SrvContact);
  26. contact->name = NULL;
  27. contact->rc = 0;
  28. GNUNET_memcpy(&(contact->public_key), key, sizeof(contact->public_key));
  29. return contact;
  30. }
  31. void
  32. destroy_contact (struct GNUNET_MESSENGER_SrvContact *contact)
  33. {
  34. if (contact->name)
  35. GNUNET_free(contact->name);
  36. GNUNET_free(contact);
  37. }
  38. const char*
  39. get_contact_name (const struct GNUNET_MESSENGER_SrvContact *contact)
  40. {
  41. return contact->name;
  42. }
  43. void
  44. set_contact_name (struct GNUNET_MESSENGER_SrvContact *contact, const char *name)
  45. {
  46. GNUNET_assert(name);
  47. if (contact->name)
  48. GNUNET_free(contact->name);
  49. contact->name = GNUNET_strdup(name);
  50. }
  51. const struct GNUNET_IDENTITY_PublicKey*
  52. get_contact_key (const struct GNUNET_MESSENGER_SrvContact *contact)
  53. {
  54. return &(contact->public_key);
  55. }
  56. void
  57. increase_contact_rc (struct GNUNET_MESSENGER_SrvContact *contact)
  58. {
  59. contact->rc++;
  60. }
  61. int
  62. decrease_contact_rc (struct GNUNET_MESSENGER_SrvContact *contact)
  63. {
  64. if (contact->rc > 0)
  65. contact->rc--;
  66. return contact->rc ? GNUNET_NO : GNUNET_YES;
  67. }
  68. const struct GNUNET_HashCode*
  69. get_contact_id_from_key (const struct GNUNET_MESSENGER_SrvContact *contact)
  70. {
  71. static struct GNUNET_HashCode id;
  72. GNUNET_CRYPTO_hash (&(contact->public_key), sizeof(contact->public_key), &id);
  73. return &id;
  74. }