GNUnet  0.18.1
Friends library

Library to read and write the FRIENDS file. More...

Collaboration diagram for Friends library:


typedef void(* GNUNET_FRIENDS_Callback) (void *cls, const struct GNUNET_PeerIdentity *friend_id)
 Signature of a function called on each friend found. More...


int GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_FRIENDS_Callback cb, void *cb_cls)
 Parse the FRIENDS file. More...
struct GNUNET_FRIENDS_WriterGNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Start writing a fresh FRIENDS file. More...
int GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
 Finish writing out the friends file. More...
int GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w, const struct GNUNET_PeerIdentity *friend_id)
 Add a friend to the friends file. More...

Detailed Description

Library to read and write the FRIENDS file.

Typedef Documentation


typedef void(* GNUNET_FRIENDS_Callback) (void *cls, const struct GNUNET_PeerIdentity *friend_id)

Signature of a function called on each friend found.

friend_idpeer identity of the friend

Definition at line 55 of file gnunet_friends_lib.h.

Function Documentation


int GNUNET_FRIENDS_parse ( const struct GNUNET_CONFIGURATION_Handle cfg,
GNUNET_FRIENDS_Callback  cb,
void *  cb_cls 

Parse the FRIENDS file.

cfgour configuration
cbfunction to call on each friend found
cb_clsclosure for cb
GNUNET_OK on success, GNUNET_SYSERR on parsing errors

Definition at line 39 of file friends.c.

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;
51  if (GNUNET_OK !=
54  "FRIENDS",
55  &fn))
56  {
58  "topology",
59  "FRIENDS");
60  return GNUNET_SYSERR;
61  }
62  if ((GNUNET_OK !=
63  GNUNET_DISK_file_test (fn)) &&
64  (GNUNET_OK !=
66  NULL,
67  0,
72  "write",
73  fn);
74  if ((GNUNET_OK !=
76  &fsize,
78  GNUNET_YES)) ||
79  (0 == fsize))
80  {
81  GNUNET_free (fn);
82  return GNUNET_OK;
83  }
84  data = GNUNET_malloc_large (fsize);
85  if (NULL == data)
86  {
88  GNUNET_free (fn);
89  return GNUNET_SYSERR;
90  }
91  ssize = GNUNET_DISK_fn_read (fn,
92  data,
93  fsize);
94  if ((ssize < 0) ||
95  (fsize != (uint64_t) ssize))
96  {
98  "read",
99  "fn");
100  GNUNET_free (fn);
101  GNUNET_free (data);
102  return GNUNET_SYSERR;
103  }
104  start = 0;
105  pos = 0;
106  while (pos < fsize)
107  {
108  while ((pos < fsize) &&
109  (! isspace ((unsigned char) data[pos])))
110  pos++;
111  if (GNUNET_OK !=
113  pos - start,
114  &pid.public_key))
115  {
117  _ (
118  "Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
119  (unsigned long long) pos,
120  (int) (pos - start),
121  &data[start]);
122  pos++;
123  start = pos;
124  continue;
125  }
126  pos++;
127  start = pos;
128  cb (cb_cls, &pid);
129  }
130  GNUNET_free (data);
131  GNUNET_free (fn);
132  return GNUNET_OK;
133 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
uint32_t data
The data value.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
enum GNUNET_GenericReturnValue 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.
enum GNUNET_GenericReturnValue 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:481
enum GNUNET_GenericReturnValue 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:221
enum GNUNET_GenericReturnValue GNUNET_DISK_fn_write(const char *fn, const void *buf, size_t buf_size, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file atomically.
Definition: disk.c:724
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:663
Create file if it doesn't exist.
Owner can read.
Owner can write.
#define GNUNET_log(kind,...)
enum GNUNET_GenericReturnValue 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:357
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(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
#define GNUNET_malloc_large(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key

References _, cfg, data, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CRYPTO_eddsa_public_key_from_string(), GNUNET_DISK_file_size(), GNUNET_DISK_file_test(), GNUNET_DISK_fn_read(), GNUNET_DISK_fn_write(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_config_missing(), GNUNET_log_strerror, GNUNET_log_strerror_file, GNUNET_malloc_large, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, pid, GNUNET_PeerIdentity::public_key, and start.

Referenced by read_friends_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_FRIENDS_write_start()

struct GNUNET_FRIENDS_Writer* GNUNET_FRIENDS_write_start ( const struct GNUNET_CONFIGURATION_Handle cfg)

Start writing a fresh FRIENDS file.

Will make a backup of the old one.

cfgconfiguration to use.
NULL on error

Definition at line 156 of file friends.c.

157 {
158  struct GNUNET_FRIENDS_Writer *w;
159  char *fn;
161  if (GNUNET_OK !=
162  GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
163  {
165  "topology", "FRIENDS");
166  return NULL;
167  }
168  if (GNUNET_OK !=
170  {
172  _ ("Directory for file `%s' does not seem to be writable.\n"),
173  fn);
174  GNUNET_free (fn);
175  return NULL;
176  }
177  if (GNUNET_OK == GNUNET_DISK_file_test (fn))
179  w = GNUNET_new (struct GNUNET_FRIENDS_Writer);
180  w->fh = GNUNET_DISK_file_open (fn,
185  GNUNET_free (fn);
186  if (NULL == w->fh)
187  {
188  GNUNET_free (w);
189  return NULL;
190  }
191  return w;
192 }
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:1234
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:359
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:581
Fail if file already exists.
Open the file for writing.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Handle for writing a friends file.
Definition: friends.c:140
struct GNUNET_DISK_FileHandle * fh
Handle to the file.
Definition: friends.c:144


Here is the call graph for this function:

◆ GNUNET_FRIENDS_write_stop()

int GNUNET_FRIENDS_write_stop ( struct GNUNET_FRIENDS_Writer w)

Finish writing out the friends file.

wwrite handle
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 202 of file friends.c.

203 {
204  int ret;
207  GNUNET_free (w);
208  return ret;
209 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1305

References GNUNET_FRIENDS_Writer::fh, GNUNET_DISK_file_close(), GNUNET_free, and ret.

Here is the call graph for this function:


int GNUNET_FRIENDS_write ( struct GNUNET_FRIENDS_Writer w,
const struct GNUNET_PeerIdentity friend_id 

Add a friend to the friends file.

wwrite handle
friend_idfriend to add
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 220 of file friends.c.

222 {
223  char *buf;
224  char *ret;
225  size_t slen;
229  "%s\n",
230  ret);
231  GNUNET_free (ret);
232  slen = strlen (buf);
233  if (slen !=
235  buf,
236  slen))
237  {
238  GNUNET_free (buf);
239  return GNUNET_SYSERR;
240  }
241  GNUNET_free (buf);
242  return GNUNET_OK;
243 }
static char buf[2048]
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:685
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:251
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.

References buf, GNUNET_FRIENDS_Writer::fh, GNUNET_asprintf(), GNUNET_CRYPTO_eddsa_public_key_to_string(), GNUNET_DISK_file_write(), GNUNET_free, GNUNET_OK, GNUNET_SYSERR, GNUNET_PeerIdentity::public_key, and ret.

Here is the call graph for this function: