plugin_ats2_common.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011-2015, 2018 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. * @file ats/plugin_ats2_common.c
  18. * @brief ATS solver helper functions to be inlined
  19. * @author Matthias Wachs
  20. * @author Christian Grothoff
  21. */
  22. /**
  23. * Default bandwidth assigned to a network: 64 KB/s
  24. */
  25. #define DEFAULT_BANDWIDTH 65536
  26. /**
  27. * Parse @a cfg for @a quota as specified for @a direction of
  28. * network type @a nts.
  29. *
  30. * @param cfg configuration to parse
  31. * @param nts network type string to get quota for
  32. * @param direction direction to get quota for ("IN" or "OUT")
  33. * @param quota[out] set to quota, #DEFAULT_BANDWIDTH if @a cfg does not say anything useful
  34. */
  35. static void
  36. get_quota(const struct GNUNET_CONFIGURATION_Handle *cfg,
  37. const char *nts,
  38. const char *direction,
  39. unsigned long long *quota)
  40. {
  41. char *quota_str;
  42. char *quota_s;
  43. int res;
  44. GNUNET_asprintf(&quota_s,
  45. "%s_QUOTA_%s",
  46. nts,
  47. direction);
  48. if (GNUNET_OK !=
  49. GNUNET_CONFIGURATION_get_value_string(cfg,
  50. "ATS",
  51. quota_s,
  52. &quota_str))
  53. {
  54. GNUNET_log_config_missing(GNUNET_ERROR_TYPE_WARNING,
  55. "ATS",
  56. quota_s);
  57. GNUNET_free(quota_s);
  58. return;
  59. }
  60. GNUNET_free(quota_s);
  61. res = GNUNET_NO;
  62. if (0 == strcmp(quota_str,
  63. "unlimited"))
  64. {
  65. *quota = ULONG_MAX;
  66. res = GNUNET_YES;
  67. }
  68. if ((GNUNET_NO == res) &&
  69. (GNUNET_OK ==
  70. GNUNET_STRINGS_fancy_size_to_bytes(quota_str,
  71. quota)))
  72. res = GNUNET_YES;
  73. if ((GNUNET_NO == res) &&
  74. (1 ==
  75. sscanf(quota_str,
  76. "%llu",
  77. quota)))
  78. res = GNUNET_YES;
  79. if (GNUNET_NO == res)
  80. {
  81. GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
  82. _("Could not load %s quota for network `%s': `%s', assigning default bandwidth %llu\n"),
  83. direction,
  84. nts,
  85. quota_str,
  86. (unsigned long long)DEFAULT_BANDWIDTH);
  87. *quota = DEFAULT_BANDWIDTH;
  88. }
  89. GNUNET_free(quota_str);
  90. }