GNUnet  0.10.x
gnunet-hello.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2012 GNUnet e.V.
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  */
25 #include "platform.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_hello_lib.h"
28 
32 struct AddContext {
36  char *buf;
37 
41  size_t max;
42 
46  size_t ret;
47 };
48 
49 static int address_count;
50 
51 
60 static int
61 add_to_buf(void *cls,
62  const struct GNUNET_HELLO_Address *address,
64 {
65  struct AddContext *ac = cls;
66  size_t ret;
67 
68  ret = GNUNET_HELLO_add_address(address,
70  ac->buf,
71  ac->max);
72  ac->buf += ret;
73  ac->max -= ret;
74  ac->ret += ret;
75  address_count++;
76  return GNUNET_OK;
77 }
78 
79 
88 static ssize_t
89 add_from_hello(void *cls, size_t max, void *buf)
90 {
91  struct GNUNET_HELLO_Message **orig = cls;
92  struct AddContext ac;
93 
94  if (NULL == *orig)
95  return GNUNET_SYSERR; /* already done */
96  ac.buf = buf;
97  ac.max = max;
98  ac.ret = 0;
100  NULL ==
102  *orig = NULL;
103  return ac.ret;
104 }
105 
106 
107 int
108 main(int argc, char *argv[])
109 {
110  struct GNUNET_DISK_FileHandle *fh;
111  struct GNUNET_HELLO_Message *orig;
113  struct GNUNET_PeerIdentity pid;
114  uint64_t fsize;
115 
116  address_count = 0;
117 
118  GNUNET_log_setup("gnunet-hello", "INFO", NULL);
119  if (argc != 2)
120  {
121  fprintf(stderr, "%s", _("Call with name of HELLO file to modify.\n"));
122  return 1;
123  }
124  if (GNUNET_OK !=
125  GNUNET_DISK_file_size(argv[1], &fsize, GNUNET_YES, GNUNET_YES))
126  {
127  fprintf(stderr,
128  _("Error accessing file `%s': %s\n"),
129  argv[1],
130  strerror(errno));
131  return 1;
132  }
133  if (fsize > 65536)
134  {
135  fprintf(stderr, _("File `%s' is too big to be a HELLO\n"), argv[1]);
136  return 1;
137  }
138  if (fsize < sizeof(struct GNUNET_MessageHeader))
139  {
140  fprintf(stderr, _("File `%s' is too small to be a HELLO\n"), argv[1]);
141  return 1;
142  }
143  fh = GNUNET_DISK_file_open(argv[1],
146  if (NULL == fh)
147  {
148  fprintf(stderr,
149  _("Error opening file `%s': %s\n"),
150  argv[1],
151  strerror(errno));
152  return 1;
153  }
154  {
155  char buf[fsize] GNUNET_ALIGN;
156 
157  GNUNET_assert(fsize == GNUNET_DISK_file_read(fh, buf, fsize));
159  orig = (struct GNUNET_HELLO_Message *)buf;
160  if ((fsize < GNUNET_HELLO_size(orig)) ||
161  (GNUNET_OK != GNUNET_HELLO_get_id(orig, &pid)))
162  {
163  fprintf(stderr,
164  _("Did not find well-formed HELLO in file `%s'\n"),
165  argv[1]);
166  return 1;
167  }
168  {
169  char *pids;
170 
172  fprintf(stdout, "Processing HELLO for peer `%s'\n", pids);
173  GNUNET_free(pids);
174  }
175  result = GNUNET_HELLO_create(&pid.public_key,
177  &orig,
179  GNUNET_assert(NULL != result);
180  fh =
181  GNUNET_DISK_file_open(argv[1],
185  if (NULL == fh)
186  {
187  fprintf(stderr,
188  _("Error opening file `%s': %s\n"),
189  argv[1],
190  strerror(errno));
191  GNUNET_free(result);
192  return 1;
193  }
194  fsize = GNUNET_HELLO_size(result);
195  if (fsize != GNUNET_DISK_file_write(fh, result, fsize))
196  {
197  fprintf(stderr,
198  _("Error writing HELLO to file `%s': %s\n"),
199  argv[1],
200  strerror(errno));
201  (void)GNUNET_DISK_file_close(fh);
202  return 1;
203  }
205  }
206  fprintf(stderr,
207  _("Modified %u addresses, wrote %u bytes\n"),
209  (unsigned int)fsize);
210  return 0;
211 }
212 
213 /* end of gnunet-hello.c */
Open the file for reading.
int GNUNET_HELLO_get_id(const struct GNUNET_HELLO_Message *hello, struct GNUNET_PeerIdentity *peer)
Get the peer identity from a HELLO message.
Definition: hello.c:662
static ssize_t add_from_hello(void *cls, size_t max, void *buf)
Add addresses from the address list to the HELLO.
Definition: gnunet-hello.c:89
A HELLO message is used to exchange information about transports with other peers.
static char * expiration
Credential TTL.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1339
struct GNUNET_HELLO_Message * GNUNET_HELLO_create(const struct GNUNET_CRYPTO_EddsaPublicKey *public_key, GNUNET_HELLO_GenerateAddressListCallback addrgen, void *addrgen_cls, int friend_only)
Construct a HELLO message given the public key, expiration time and an iterator that spews the transp...
Definition: hello.c:202
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:729
char * buf
Where to add.
Definition: gnunet-hello.c:36
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
size_t max
Maximum number of bytes left.
Definition: gnunet-hello.c:41
int main(int argc, char *argv[])
Definition: gnunet-hello.c:108
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
static int add_to_buf(void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
Add the given address with infinit expiration to the buffer.
Definition: gnunet-hello.c:61
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
struct GNUNET_HELLO_Message * GNUNET_HELLO_iterate_addresses(const struct GNUNET_HELLO_Message *msg, int return_modified, GNUNET_HELLO_AddressIterator it, void *it_cls)
Iterate over all of the addresses in the HELLO.
Definition: hello.c:252
Truncate file if it exists.
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 int result
Global testing status.
static int fh
Handle to the unique file.
static int address_count
Definition: gnunet-hello.c:49
size_t GNUNET_HELLO_add_address(const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration, char *target, size_t max)
Copy the given address information into the given buffer using the format of HELLOs.
Definition: hello.c:107
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
Open the file for writing.
int GNUNET_HELLO_is_friend_only(const struct GNUNET_HELLO_Message *h)
Return HELLO type.
Definition: hello.c:87
static struct GNUNET_MQ_Envelope * ac
Handle to current GNUNET_PEERINFO_add_peer() operation.
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
The identity of the host (wraps the signing key of the peer).
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to &#39;struct&#39;s...
An address for communicating with a peer.
uint16_t GNUNET_HELLO_size(const struct GNUNET_HELLO_Message *hello)
Return the size of the given HELLO message.
Definition: hello.c:643
Closure for add_to_buf().
Definition: gnunet-hello.c:32
Header for all communications.
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:77
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
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
static char * address
GNS address for this phone.
Handle used to access files (and pipes).
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_CRYPTO_EddsaPublicKey public_key
size_t ret
Number of bytes added so far.
Definition: gnunet-hello.c:46