GNUnet  0.10.x
Typedefs | Functions
Friends library

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

Typedefs

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

Functions

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

◆ GNUNET_FRIENDS_Callback

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

Signature of a function called on each friend found.

Parameters
clsclosure
friend_idpeer identity of the friend

Definition at line 51 of file gnunet_friends_lib.h.

Function Documentation

◆ GNUNET_FRIENDS_parse()

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

Parse the FRIENDS file.

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

Definition at line 39 of file friends.c.

References _, data, fn, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CRYPTO_eddsa_public_key_from_string(), GNUNET_DISK_directory_create_for_file(), 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, GNUNET_PeerIdentity::public_key, and start.

Referenced by read_friends_file().

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  _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
127  (unsigned long long)pos,
128  (int)(pos - start),
129  &data[start]);
130  pos++;
131  start = pos;
132  continue;
133  }
134  pos++;
135  start = pos;
136  cb(cb_cls, &pid);
137  }
138  GNUNET_free(data);
139  GNUNET_free(fn);
140  return GNUNET_OK;
141 }
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:541
Create file if it doesn&#39;t exist.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:681
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#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:877
#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...
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.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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:254
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:501
The identity of the host (wraps the signing key of the peer).
#define GNUNET_log(kind,...)
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
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
uint32_t data
The data value.
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:791
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
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.

Parameters
cfgconfiguration to use.
Returns
NULL on error

Definition at line 163 of file friends.c.

References _, GNUNET_FRIENDS_Writer::fh, fn, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_file_backup(), GNUNET_DISK_file_open(), GNUNET_DISK_file_test(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_FAILIFEXISTS, GNUNET_DISK_OPEN_WRITE, GNUNET_DISK_PERM_USER_READ, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_config_missing(), GNUNET_new, and GNUNET_OK.

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  {
172  "topology", "FRIENDS");
173  return NULL;
174  }
175  if (GNUNET_OK !=
177  {
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))
186  w = GNUNET_new(struct GNUNET_FRIENDS_Writer);
187  w->fh = GNUNET_DISK_file_open(fn,
192  GNUNET_free(fn);
193  if (NULL == w->fh)
194  {
195  GNUNET_free(w);
196  return NULL;
197  }
198  return w;
199 }
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:541
Create file if it doesn&#39;t exist.
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:681
#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 _(String)
GNU gettext support macro.
Definition: platform.h:181
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
static char * fn
Filename of the unique file.
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:433
Handle for writing a friends file.
Definition: friends.c:147
Open the file for writing.
#define GNUNET_log(kind,...)
struct GNUNET_DISK_FileHandle * fh
Handle to the file.
Definition: friends.c:151
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.
Fail if file already exists.
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:1262
#define GNUNET_free(ptr)
Wrapper around free.
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.

Parameters
wwrite handle
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 209 of file friends.c.

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

210 {
211  int ret;
212 
213  ret = GNUNET_DISK_file_close(w->fh);
214  GNUNET_free(w);
215  return ret;
216 }
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1339
static int ret
Final status code.
Definition: gnunet-arm.c:89
struct GNUNET_DISK_FileHandle * fh
Handle to the file.
Definition: friends.c:151
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:

◆ GNUNET_FRIENDS_write()

int GNUNET_FRIENDS_write ( struct GNUNET_FRIENDS_Writer w,
const struct GNUNET_PeerIdentity friend_id 
)

Add a friend to the friends file.

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

Definition at line 227 of file friends.c.

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.

229 {
230  char *buf;
231  char *ret;
232  size_t slen;
233 
235  GNUNET_asprintf(&buf,
236  "%s\n",
237  ret);
238  GNUNET_free(ret);
239  slen = strlen(buf);
240  if (slen !=
242  buf,
243  slen))
244  {
245  GNUNET_free(buf);
246  return GNUNET_SYSERR;
247  }
248  GNUNET_free(buf);
249  return GNUNET_OK;
250 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static int ret
Final status code.
Definition: gnunet-arm.c:89
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
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:367
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:817
static char buf[2048]
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_DISK_FileHandle * fh
Handle to the file.
Definition: friends.c:151
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
Here is the call graph for this function: