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