friends.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013 Christian Grothoff
  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 topology/friends.c
  18. * @brief library to read and write the FRIENDS file
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_friends_lib.h"
  23. /**
  24. * Parse the FRIENDS file.
  25. *
  26. * @param cfg our configuration
  27. * @param cb function to call on each friend found
  28. * @param cb_cls closure for @a cb
  29. * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors
  30. */
  31. int
  32. GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg,
  33. GNUNET_FRIENDS_Callback cb,
  34. void *cb_cls)
  35. {
  36. char *fn;
  37. char *data;
  38. size_t pos;
  39. size_t start;
  40. struct GNUNET_PeerIdentity pid;
  41. uint64_t fsize;
  42. ssize_t ssize;
  43. if (GNUNET_OK !=
  44. GNUNET_CONFIGURATION_get_value_filename (cfg,
  45. "TOPOLOGY",
  46. "FRIENDS",
  47. &fn))
  48. {
  49. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  50. "topology",
  51. "FRIENDS");
  52. return GNUNET_SYSERR;
  53. }
  54. if (GNUNET_SYSERR ==
  55. GNUNET_DISK_directory_create_for_file (fn))
  56. {
  57. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  58. "mkdir",
  59. fn);
  60. GNUNET_free (fn);
  61. return GNUNET_SYSERR;
  62. }
  63. if ((GNUNET_OK !=
  64. GNUNET_DISK_file_test (fn)) &&
  65. (GNUNET_OK !=
  66. GNUNET_DISK_fn_write (fn,
  67. NULL,
  68. 0,
  69. GNUNET_DISK_PERM_USER_READ
  70. | GNUNET_DISK_PERM_USER_WRITE
  71. | GNUNET_DISK_OPEN_CREATE)))
  72. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  73. "write",
  74. fn);
  75. if ((GNUNET_OK !=
  76. GNUNET_DISK_file_size (fn,
  77. &fsize,
  78. GNUNET_NO,
  79. GNUNET_YES)) ||
  80. (0 == fsize))
  81. {
  82. GNUNET_free (fn);
  83. return GNUNET_OK;
  84. }
  85. data = GNUNET_malloc_large (fsize);
  86. if (NULL == data)
  87. {
  88. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
  89. GNUNET_free (fn);
  90. return GNUNET_SYSERR;
  91. }
  92. ssize = GNUNET_DISK_fn_read (fn,
  93. data,
  94. fsize);
  95. if ((ssize < 0) ||
  96. (fsize != (uint64_t) ssize))
  97. {
  98. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
  99. "read",
  100. "fn");
  101. GNUNET_free (fn);
  102. GNUNET_free (data);
  103. return GNUNET_SYSERR;
  104. }
  105. start = 0;
  106. pos = 0;
  107. while (pos < fsize)
  108. {
  109. while ((pos < fsize) &&
  110. (! isspace ((unsigned char) data[pos])))
  111. pos++;
  112. if (GNUNET_OK !=
  113. GNUNET_CRYPTO_eddsa_public_key_from_string (&data[start],
  114. pos - start,
  115. &pid.public_key))
  116. {
  117. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  118. _ (
  119. "Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
  120. (unsigned long long) pos,
  121. (int) (pos - start),
  122. &data[start]);
  123. pos++;
  124. start = pos;
  125. continue;
  126. }
  127. pos++;
  128. start = pos;
  129. cb (cb_cls, &pid);
  130. }
  131. GNUNET_free (data);
  132. GNUNET_free (fn);
  133. return GNUNET_OK;
  134. }
  135. /**
  136. * Handle for writing a friends file.
  137. */
  138. struct GNUNET_FRIENDS_Writer
  139. {
  140. /**
  141. * Handle to the file.
  142. */
  143. struct GNUNET_DISK_FileHandle *fh;
  144. };
  145. /**
  146. * Start writing a fresh FRIENDS file. Will make a backup of the
  147. * old one.
  148. *
  149. * @param cfg configuration to use.
  150. * @return NULL on error
  151. */
  152. struct GNUNET_FRIENDS_Writer *
  153. GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
  154. {
  155. struct GNUNET_FRIENDS_Writer *w;
  156. char *fn;
  157. if (GNUNET_OK !=
  158. GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
  159. {
  160. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  161. "topology", "FRIENDS");
  162. return NULL;
  163. }
  164. if (GNUNET_OK !=
  165. GNUNET_DISK_directory_create_for_file (fn))
  166. {
  167. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  168. _ ("Directory for file `%s' does not seem to be writable.\n"),
  169. fn);
  170. GNUNET_free (fn);
  171. return NULL;
  172. }
  173. if (GNUNET_OK == GNUNET_DISK_file_test (fn))
  174. GNUNET_DISK_file_backup (fn);
  175. w = GNUNET_new (struct GNUNET_FRIENDS_Writer);
  176. w->fh = GNUNET_DISK_file_open (fn,
  177. GNUNET_DISK_OPEN_CREATE
  178. | GNUNET_DISK_OPEN_WRITE
  179. | GNUNET_DISK_OPEN_FAILIFEXISTS,
  180. GNUNET_DISK_PERM_USER_READ);
  181. GNUNET_free (fn);
  182. if (NULL == w->fh)
  183. {
  184. GNUNET_free (w);
  185. return NULL;
  186. }
  187. return w;
  188. }
  189. /**
  190. * Finish writing out the friends file.
  191. *
  192. * @param w write handle
  193. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  194. */
  195. int
  196. GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
  197. {
  198. int ret;
  199. ret = GNUNET_DISK_file_close (w->fh);
  200. GNUNET_free (w);
  201. return ret;
  202. }
  203. /**
  204. * Add a friend to the friends file.
  205. *
  206. * @param w write handle
  207. * @param friend_id friend to add
  208. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  209. */
  210. int
  211. GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w,
  212. const struct GNUNET_PeerIdentity *friend_id)
  213. {
  214. char *buf;
  215. char *ret;
  216. size_t slen;
  217. ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&friend_id->public_key);
  218. GNUNET_asprintf (&buf,
  219. "%s\n",
  220. ret);
  221. GNUNET_free (ret);
  222. slen = strlen (buf);
  223. if (slen !=
  224. GNUNET_DISK_file_write (w->fh,
  225. buf,
  226. slen))
  227. {
  228. GNUNET_free (buf);
  229. return GNUNET_SYSERR;
  230. }
  231. GNUNET_free (buf);
  232. return GNUNET_OK;
  233. }
  234. /* end of friends.c */