src: for every AGPL3.0 file, add SPDX identifier.
[oweals/gnunet.git] / src / topology / friends.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 Christian Grothoff
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file topology/friends.c
23  * @brief library to read and write the FRIENDS file
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_friends_lib.h"
28
29
30 /**
31  * Parse the FRIENDS file.
32  *
33  * @param cfg our configuration
34  * @param cb function to call on each friend found
35  * @param cb_cls closure for @a cb
36  * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors
37  */
38 int
39 GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg,
40                       GNUNET_FRIENDS_Callback cb,
41                       void *cb_cls)
42 {
43   char *fn;
44   char *data;
45   size_t pos;
46   size_t start;
47   struct GNUNET_PeerIdentity pid;
48   uint64_t fsize;
49   ssize_t ssize;
50
51   if (GNUNET_OK !=
52       GNUNET_CONFIGURATION_get_value_filename (cfg,
53                                                "TOPOLOGY",
54                                                "FRIENDS",
55                                                &fn))
56   {
57     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
58                                "topology",
59                                "FRIENDS");
60     return GNUNET_SYSERR;
61   }
62   if (GNUNET_SYSERR ==
63       GNUNET_DISK_directory_create_for_file (fn))
64   {
65     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
66                               "mkdir",
67                               fn);
68     GNUNET_free (fn);
69     return GNUNET_SYSERR;
70   }
71   if ( (GNUNET_OK !=
72         GNUNET_DISK_file_test (fn)) &&
73        (GNUNET_OK !=
74         GNUNET_DISK_fn_write (fn,
75                               NULL,
76                               0,
77                               GNUNET_DISK_PERM_USER_READ |
78                               GNUNET_DISK_PERM_USER_WRITE |
79                               GNUNET_DISK_OPEN_CREATE)) )
80     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
81                               "write",
82                               fn);
83   if ( (GNUNET_OK !=
84         GNUNET_DISK_file_size (fn,
85                                &fsize,
86                                GNUNET_NO,
87                                GNUNET_YES)) ||
88        (0 == fsize) )
89   {
90     GNUNET_free (fn);
91     return GNUNET_OK;
92   }
93   data = GNUNET_malloc_large (fsize);
94   if (NULL == data)
95   {
96     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
97     GNUNET_free (fn);
98     return GNUNET_SYSERR;
99   }
100   ssize = GNUNET_DISK_fn_read (fn,
101                                data,
102                                fsize);
103   if ( (ssize < 0) ||
104        (fsize != (uint64_t) ssize) )
105   {
106     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
107                               "read",
108                               "fn");
109     GNUNET_free (fn);
110     GNUNET_free (data);
111     return GNUNET_SYSERR;
112   }
113   start = 0;
114   pos = 0;
115   while (pos < fsize)
116   {
117     while ( (pos < fsize) &&
118             (! isspace ((unsigned char) data[pos])) )
119       pos++;
120     if (GNUNET_OK !=
121         GNUNET_CRYPTO_eddsa_public_key_from_string (&data[start],
122                                                     pos - start,
123                                                     &pid.public_key))
124     {
125       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
126                   _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
127                   (unsigned long long) pos,
128                   (int) (pos - start),
129                   &data[start]);
130       pos++;
131       start = pos;
132       continue;
133     }
134     pos++;
135     start = pos;
136     cb (cb_cls, &pid);
137   }
138   GNUNET_free (data);
139   GNUNET_free (fn);
140   return GNUNET_OK;
141 }
142
143
144 /**
145  * Handle for writing a friends file.
146  */
147 struct GNUNET_FRIENDS_Writer
148 {
149   /**
150    * Handle to the file.
151    */
152   struct GNUNET_DISK_FileHandle *fh;
153 };
154
155
156 /**
157  * Start writing a fresh FRIENDS file.  Will make a backup of the
158  * old one.
159  *
160  * @param cfg configuration to use.
161  * @return NULL on error
162  */
163 struct GNUNET_FRIENDS_Writer *
164 GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
165 {
166   struct GNUNET_FRIENDS_Writer *w;
167   char *fn;
168
169   if (GNUNET_OK !=
170       GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
171   {
172     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
173                                "topology", "FRIENDS");
174     return NULL;
175   }
176   if (GNUNET_OK !=
177       GNUNET_DISK_directory_create_for_file (fn))
178   {
179     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
180                 _("Directory for file `%s' does not seem to be writable.\n"),
181                 fn);
182     GNUNET_free (fn);
183     return NULL;
184   }
185   if (GNUNET_OK == GNUNET_DISK_file_test (fn))
186     GNUNET_DISK_file_backup (fn);
187   w = GNUNET_new (struct GNUNET_FRIENDS_Writer);
188   w->fh = GNUNET_DISK_file_open  (fn,
189                                   GNUNET_DISK_OPEN_CREATE |
190                                   GNUNET_DISK_OPEN_WRITE |
191                                   GNUNET_DISK_OPEN_FAILIFEXISTS,
192                                   GNUNET_DISK_PERM_USER_READ);
193   GNUNET_free (fn);
194   if (NULL == w->fh)
195   {
196     GNUNET_free (w);
197     return NULL;
198   }
199   return w;
200 }
201
202
203 /**
204  * Finish writing out the friends file.
205  *
206  * @param w write handle
207  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
208  */
209 int
210 GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
211 {
212   int ret;
213
214   ret = GNUNET_DISK_file_close (w->fh);
215   GNUNET_free (w);
216   return ret;
217 }
218
219
220 /**
221  * Add a friend to the friends file.
222  *
223  * @param w write handle
224  * @param friend_id friend to add
225  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
226  */
227 int
228 GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w,
229                       const struct GNUNET_PeerIdentity *friend_id)
230 {
231   char *buf;
232   char *ret;
233   size_t slen;
234
235   ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&friend_id->public_key);
236   GNUNET_asprintf (&buf,
237                    "%s\n",
238                    ret);
239   GNUNET_free (ret);
240   slen = strlen (buf);
241   if (slen !=
242       GNUNET_DISK_file_write (w->fh,
243                               buf,
244                               slen))
245   {
246     GNUNET_free (buf);
247     return GNUNET_SYSERR;
248   }
249   GNUNET_free (buf);
250   return GNUNET_OK;
251 }
252
253
254 /* end of friends.c */