Merge branch 'master' of ssh://gnunet.org/gnunet
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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", "FRIENDS");
59     return GNUNET_SYSERR;
60   }
61   if (GNUNET_SYSERR ==
62       GNUNET_DISK_directory_create_for_file (fn))
63   {
64     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
65                               "mkdir",
66                               fn);
67     GNUNET_free (fn);
68     return GNUNET_SYSERR;
69   }
70   if ( (GNUNET_OK !=
71         GNUNET_DISK_file_test (fn)) &&
72        (GNUNET_OK !=
73         GNUNET_DISK_fn_write (fn,
74                               NULL,
75                               0,
76                               GNUNET_DISK_PERM_USER_READ |
77                               GNUNET_DISK_PERM_USER_WRITE |
78                               GNUNET_DISK_OPEN_CREATE)) )
79     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
80                               "write",
81                               fn);
82   if ( (GNUNET_OK !=
83         GNUNET_DISK_file_size (fn,
84                                &fsize,
85                                GNUNET_NO,
86                                GNUNET_YES)) ||
87        (0 == fsize) )
88   {
89     GNUNET_free (fn);
90     return GNUNET_OK;
91   }
92   data = GNUNET_malloc_large (fsize);
93   if (NULL == data)
94   {
95     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc");
96     GNUNET_free (fn);
97     return GNUNET_SYSERR;
98   }
99   ssize = GNUNET_DISK_fn_read (fn,
100                                data,
101                                fsize);
102   if ( (ssize < 0) ||
103        (fsize != (uint64_t) ssize) )
104   {
105     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
106                               "read",
107                               "fn");
108     GNUNET_free (fn);
109     GNUNET_free (data);
110     return GNUNET_SYSERR;
111   }
112   start = 0;
113   pos = 0;
114   while (pos < fsize)
115   {
116     while ( (pos < fsize) &&
117             (! isspace ((unsigned char) data[pos])) )
118       pos++;
119     if (GNUNET_OK !=
120         GNUNET_CRYPTO_eddsa_public_key_from_string (&data[start],
121                                                     pos - start,
122                                                     &pid.public_key))
123     {
124       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
125                   _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
126                   (unsigned long long) pos,
127                   (int) (pos - start),
128                   &data[start]);
129       pos++;
130       start = pos;
131       continue;
132     }
133     pos++;
134     start = pos;
135     cb (cb_cls, &pid);
136   }
137   GNUNET_free (data);
138   GNUNET_free (fn);
139   return GNUNET_OK;
140 }
141
142
143 /**
144  * Handle for writing a friends file.
145  */
146 struct GNUNET_FRIENDS_Writer
147 {
148   /**
149    * Handle to the file.
150    */
151   struct GNUNET_DISK_FileHandle *fh;
152 };
153
154
155 /**
156  * Start writing a fresh FRIENDS file.  Will make a backup of the
157  * old one.
158  *
159  * @param cfg configuration to use.
160  * @return NULL on error
161  */
162 struct GNUNET_FRIENDS_Writer *
163 GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
164 {
165   struct GNUNET_FRIENDS_Writer *w;
166   char *fn;
167
168   if (GNUNET_OK !=
169       GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
170   {
171     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
172                                "topology", "FRIENDS");
173     return NULL;
174   }
175   if (GNUNET_OK !=
176       GNUNET_DISK_directory_create_for_file (fn))
177   {
178     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
179                 _("Directory for file `%s' does not seem to be writable.\n"),
180                 fn);
181     GNUNET_free (fn);
182     return NULL;
183   }
184   if (GNUNET_OK == GNUNET_DISK_file_test (fn))
185     GNUNET_DISK_file_backup (fn);
186   w = GNUNET_new (struct GNUNET_FRIENDS_Writer);
187   w->fh = GNUNET_DISK_file_open  (fn,
188                                   GNUNET_DISK_OPEN_CREATE |
189                                   GNUNET_DISK_OPEN_WRITE |
190                                   GNUNET_DISK_OPEN_FAILIFEXISTS,
191                                   GNUNET_DISK_PERM_USER_READ);
192   GNUNET_free (fn);
193   if (NULL == w->fh)
194   {
195     GNUNET_free (w);
196     return NULL;
197   }
198   return w;
199 }
200
201
202 /**
203  * Finish writing out the friends file.
204  *
205  * @param w write handle
206  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
207  */
208 int
209 GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
210 {
211   int ret;
212
213   ret = GNUNET_DISK_file_close (w->fh);
214   GNUNET_free (w);
215   return ret;
216 }
217
218
219 /**
220  * Add a friend to the friends file.
221  *
222  * @param w write handle
223  * @param friend_id friend to add
224  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
225  */
226 int
227 GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w,
228                       const struct GNUNET_PeerIdentity *friend_id)
229 {
230   char *buf;
231   char *ret;
232   size_t slen;
233
234   ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&friend_id->public_key);
235   GNUNET_asprintf (&buf,
236                    "%s\n",
237                    ret);
238   GNUNET_free (ret);
239   slen = strlen (buf);
240   if (slen !=
241       GNUNET_DISK_file_write (w->fh,
242                               buf,
243                               slen))
244   {
245     GNUNET_free (buf);
246     return GNUNET_SYSERR;
247   }
248   GNUNET_free (buf);
249   return GNUNET_OK;
250 }
251
252
253 /* end of friends.c */