GNUnet  0.11.x
friends.c
Go to the documentation of this file.
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 
26 #include "platform.h"
27 #include "gnunet_friends_lib.h"
28 
29 
38 int
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 !=
53  "TOPOLOGY",
54  "FRIENDS",
55  &fn))
56  {
58  "topology",
59  "FRIENDS");
60  return GNUNET_SYSERR;
61  }
62  if (GNUNET_SYSERR ==
64  {
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 !=
75  NULL,
76  0,
81  "write",
82  fn);
83  if ((GNUNET_OK !=
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  {
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  {
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 !=
122  pos - start,
123  &pid.public_key))
124  {
126  _ (
127  "Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
128  (unsigned long long) pos,
129  (int) (pos - start),
130  &data[start]);
131  pos++;
132  start = pos;
133  continue;
134  }
135  pos++;
136  start = pos;
137  cb (cb_cls, &pid);
138  }
139  GNUNET_free (data);
140  GNUNET_free (fn);
141  return GNUNET_OK;
142 }
143 
144 
149 {
154 };
155 
156 
164 struct GNUNET_FRIENDS_Writer *
166 {
167  struct GNUNET_FRIENDS_Writer *w;
168  char *fn;
169 
170  if (GNUNET_OK !=
171  GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
172  {
174  "topology", "FRIENDS");
175  return NULL;
176  }
177  if (GNUNET_OK !=
179  {
181  _ ("Directory for file `%s' does not seem to be writable.\n"),
182  fn);
183  GNUNET_free (fn);
184  return NULL;
185  }
186  if (GNUNET_OK == GNUNET_DISK_file_test (fn))
188  w = GNUNET_new (struct GNUNET_FRIENDS_Writer);
189  w->fh = GNUNET_DISK_file_open (fn,
194  GNUNET_free (fn);
195  if (NULL == w->fh)
196  {
197  GNUNET_free (w);
198  return NULL;
199  }
200  return w;
201 }
202 
203 
210 int
212 {
213  int ret;
214 
215  ret = GNUNET_DISK_file_close (w->fh);
216  GNUNET_free (w);
217  return ret;
218 }
219 
220 
228 int
230  const struct GNUNET_PeerIdentity *friend_id)
231 {
232  char *buf;
233  char *ret;
234  size_t slen;
235 
237  GNUNET_asprintf (&buf,
238  "%s\n",
239  ret);
240  GNUNET_free (ret);
241  slen = strlen (buf);
242  if (slen !=
244  buf,
245  slen))
246  {
247  GNUNET_free (buf);
248  return GNUNET_SYSERR;
249  }
250  GNUNET_free (buf);
251  return GNUNET_OK;
252 }
253 
254 
255 /* end of friends.c */
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:544
Create file if it doesn&#39;t exist.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1345
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
void(* GNUNET_FRIENDS_Callback)(void *cls, const struct GNUNET_PeerIdentity *friend_id)
Signature of a function called on each friend found.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
int GNUNET_FRIENDS_parse(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_FRIENDS_Callback cb, void *cb_cls)
Parse the FRIENDS file.
Definition: friends.c:39
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:684
struct GNUNET_FRIENDS_Writer * GNUNET_FRIENDS_write_start(const struct GNUNET_CONFIGURATION_Handle *cfg)
Start writing a fresh FRIENDS file.
Definition: friends.c:165
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc_large(size)
Wrapper around malloc.
ssize_t GNUNET_DISK_fn_write(const char *fn, const void *buffer, size_t n, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file.
Definition: disk.c:880
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * fn
Filename of the unique file.
char * GNUNET_CRYPTO_eddsa_public_key_to_string(const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a public key to a string.
Definition: crypto_ecc.c:365
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:820
static char buf[2048]
void GNUNET_DISK_file_backup(const char *fil)
Move a file out of the way (create a backup) by renaming it to "orig.NUM~" where NUM is the smallest ...
Definition: disk.c:436
Handle for writing a friends file.
Definition: friends.c:148
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
Open the file for writing.
int GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:257
int GNUNET_CRYPTO_eddsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition: crypto_ecc.c:499
int GNUNET_FRIENDS_write(struct GNUNET_FRIENDS_Writer *w, const struct GNUNET_PeerIdentity *friend_id)
Add a friend to the friends file.
Definition: friends.c:229
The identity of the host (wraps the signing key of the peer).
int GNUNET_FRIENDS_write_stop(struct GNUNET_FRIENDS_Writer *w)
Finish writing out the friends file.
Definition: friends.c:211
configuration data
Definition: configuration.c:85
#define GNUNET_log(kind,...)
struct GNUNET_DISK_FileHandle * fh
Handle to the file.
Definition: friends.c:153
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
#define GNUNET_YES
Definition: gnunet_common.h:77
Fail if file already exists.
uint32_t data
The data value.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1268
Handle used to access files (and pipes).
ssize_t GNUNET_DISK_fn_read(const char *fn, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:794
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EddsaPublicKey public_key