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