GNUnet 0.22.2
gnunet-hello.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2024 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 */
20
26#include "platform.h"
27
28#include "gnunet_common.h"
29#include "gnunet_time_lib.h"
30#include "gnunet_util_lib.h"
32#include "gnunet_util_lib.h"
34
38static int ret;
39
40/*
41 * Handle to PEERSTORE service
42 */
44
49
54
59
64
69
73static char *expirationstring;
74
79
84
88static int print_hellos;
89
94
101static void
102shutdown_task (void *cls)
103{
104 (void) cls;
105 if (NULL != shc)
106 {
108 shc = NULL;
109 }
110 if (NULL != iter_ctx)
111 {
113 }
114 if (NULL != peerstore_handle)
115 {
117 peerstore_handle = NULL;
118 }
119}
120
121
129static void
131 const struct GNUNET_PeerIdentity *pid,
132 const char *uri)
133{
134 (void) cls;
135
136
137 printf ("|- %s\n", uri);
138}
139
140
141static void
142hello_iter (void *cls, const struct GNUNET_PEERSTORE_Record *record,
143 const char *emsg)
144{
145 struct GNUNET_HELLO_Parser *hp;
146 struct GNUNET_TIME_Absolute hello_exp;
147 const struct GNUNET_PeerIdentity *pid;
148
149 if ((NULL == record) && (NULL == emsg))
150 {
154 iter_ctx = NULL;
156 return;
157 }
158 if (NULL != emsg)
159 {
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", emsg);
162 return;
163 }
168 {
169 if (0 == GNUNET_memcmp (&my_full_id,
170 pid))
171 {
172 struct GNUNET_HELLO_Builder *hb;
173
176 {
177 char *url;
178 if (NULL != expirationstring)
179 {
183 }
184 else
185 {
187 }
188 printf ("%s\n", url);
189 GNUNET_free (url);
191 iter_ctx = NULL;
195 }
196 else
197 {
198 struct GNUNET_MQ_Envelope *env;
199 struct GNUNET_TIME_Relative validity_tmp;
200 const struct GNUNET_MessageHeader *msg;
201
202 if (NULL != expirationstring)
203 {
207 }
208 else
209 {
210 validity_tmp = GNUNET_TIME_absolute_get_duration (hello_exp);
213 validity_tmp);
214 }
216 fwrite (msg, 1, ntohs (msg->size), stdout);
219 iter_ctx = NULL;
223 }
224 return;
225 }
226 }
227 else if (print_hellos)
228 {
229 printf ("`%s' (expires: %s):\n", GNUNET_i2s (pid),
232 }
235}
236
237
238static void
239hello_store_success (void *cls, int success)
240{
241 shc = NULL;
242 if (GNUNET_OK != success)
243 {
245 "Storing hello uri failed\n");
246 }
247 printf ("HELLO imported.");
249}
250
251
260static void
261run (void *cls,
262 char *const *args,
263 const char *cfgfile,
264 const struct GNUNET_CONFIGURATION_Handle *cfg)
265{
266 struct GNUNET_HELLO_Parser *hp;
267 struct GNUNET_MQ_Envelope *env;
268 char *keyfile;
269 (void) cls;
270 (void) cfgfile;
271
272 if (NULL != args[0])
273 {
274 fprintf (stderr, _ ("Invalid command line argument `%s'\n"), args[0]);
275 return;
276 }
277 if (! print_hellos &&
280 {
281 fprintf (stderr, "%s", _ ("No argument given.\n"));
282 ret = 1;
284 return;
285 }
287 if (GNUNET_OK !=
289 "PEER",
290 "PRIVATE_KEY",
291 &keyfile))
292 {
293 GNUNET_log (
295 _ ("Core service is lacking HOSTKEY configuration setting. Exiting.\n"));
297 ret = 1;
298 return;
299 }
300 if (GNUNET_SYSERR ==
304 {
306 "Failed to read peer's private key!\n");
308 ret = 1;
309 GNUNET_free (keyfile);
310 return;
311 }
312 GNUNET_free (keyfile);
317 if (NULL != expirationstring)
318 {
321 {
322 fprintf (stderr, "Invalid expiration time `%s'", expirationstring);
324 return;
325 }
326 }
328 {
329 char buffer[GNUNET_MAX_MESSAGE_SIZE - 1];
330 char *write_ptr;
331 ssize_t nread;
332 size_t read_total = 0;
333
334 write_ptr = buffer;
335 while (0 < (nread = fread (write_ptr, 1,
336 sizeof buffer - read_total, stdin)))
337 {
338 read_total += nread;
339 write_ptr += nread;
340 }
341 buffer[read_total] = '\0';
342 if (strlen ("gnunet://hello/") > read_total)
343 {
344 fprintf (stderr, "HELLO malformed\n");
346 return;
347 }
348 if (0 == strncasecmp ("gnunet://hello/",
349 buffer, strlen ("gnunet://hello/")))
350 {
351 // Remove newline
352 buffer[read_total - 1] = '\0';
353 hp = GNUNET_HELLO_parser_from_url (buffer);
354 if (NULL == hp)
355 {
356 fprintf (stderr, "Unable to parse URI `%s'\n", buffer);
358 return;
359 }
363 &hello_store_success, NULL);
366 }
367 else if (read_total > sizeof (struct GNUNET_MessageHeader))
368 {
370 (const struct GNUNET_MessageHeader*)
371 buffer,
372 &hello_store_success, NULL);
373 }
374 else
375 {
376 fprintf (stderr, "HELLO malformed\n");
378 }
379 return;
380 }
381
383 "peerstore",
384 NULL,
386 &hello_iter,
387 NULL);
388
389}
390
391
399int
400main (int argc, char *const *argv)
401{
402 int res;
405 "export-hello",
407 "Print a HELLO for our peer identity"),
410 "binary",
412 "Output HELLO in binary format. Use with `--export'."),
415 "import-hello",
416 gettext_noop ("Import a HELLO"),
417 &import_hello),
419 "expiration",
420 "TIME",
422 "Expiration time to set for exported hello. (Default: 1 day)"),
425 "dump-hellos",
427 "List all known HELLOs in peerstore"),
428 &print_hellos),
430 };
431
433 argc,
434 argv,
435 "gnunet-hello",
437 "Import/export/print HELLOs."),
438 options,
439 &run,
440 NULL);
441
442 if (GNUNET_OK == res)
443 return ret;
444 return 1;
445}
446
447
448/* end of gnunet-hello.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define gettext_noop(String)
Definition: gettext.h:74
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static struct GNUNET_TIME_Relative hello_validity
Expiration time for exported hello.
Definition: gnunet-hello.c:78
static struct GNUNET_CRYPTO_EddsaPrivateKey my_private_key
Peer private key.
Definition: gnunet-hello.c:58
static struct GNUNET_PeerIdentity my_full_id
Peer identity.
Definition: gnunet-hello.c:63
static void print_hello_addrs(void *cls, const struct GNUNET_PeerIdentity *pid, const char *uri)
Callback function used to extract URIs from a builder.
Definition: gnunet-hello.c:130
static int export_own_hello
HELLO export option -H.
Definition: gnunet-hello.c:68
static void hello_iter(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Definition: gnunet-hello.c:142
static enum GNUNET_GenericReturnValue import_hello
HELLO import option -I.
Definition: gnunet-hello.c:93
static int print_hellos
Hello list option -D.
Definition: gnunet-hello.c:88
static void shutdown_task(void *cls)
Task run in monitor mode when the user presses CTRL-C to abort.
Definition: gnunet-hello.c:102
static int ret
Return code.
Definition: gnunet-hello.c:38
static struct GNUNET_PEERSTORE_IterateContext * iter_ctx
PEERSTORE iteration context.
Definition: gnunet-hello.c:48
static enum GNUNET_GenericReturnValue binary_output
HELLO export/import format option.
Definition: gnunet-hello.c:83
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
Definition: gnunet-hello.c:261
int main(int argc, char *const *argv)
The main function to obtain peer information from CORE.
Definition: gnunet-hello.c:400
static char * expirationstring
Optional expiration string -E.
Definition: gnunet-hello.c:73
static struct GNUNET_PEERSTORE_Handle * peerstore_handle
Definition: gnunet-hello.c:43
static struct GNUNET_PEERSTORE_StoreHelloContext * shc
HELLO store context handle.
Definition: gnunet-hello.c:53
static void hello_store_success(void *cls, int success)
Definition: gnunet-hello.c:239
static char * res
Currently read line or NULL on EOF.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Helper library for handling HELLO URIs.
API to the peerstore service.
Functions related to time.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
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_CRYPTO_eddsa_key_from_file(const char *filename, int do_create, struct GNUNET_CRYPTO_EddsaPrivateKey *pkey)
Create a new private key by reading it from a file.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:201
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
struct GNUNET_HELLO_Builder * GNUNET_HELLO_builder_from_parser(const struct GNUNET_HELLO_Parser *parser)
Allocate builder from parser.
Definition: hello-uri.c:419
void GNUNET_HELLO_parser_free(struct GNUNET_HELLO_Parser *parser)
Release resources of a builder.
Definition: hello-uri.c:435
struct GNUNET_HELLO_Parser * GNUNET_HELLO_parser_from_msg(const struct GNUNET_MessageHeader *msg)
Parse msg.
Definition: hello-uri.c:471
void GNUNET_HELLO_builder_free(struct GNUNET_HELLO_Builder *builder)
Release resources of a builder.
Definition: hello-uri.c:453
const struct GNUNET_PeerIdentity * GNUNET_HELLO_parser_iterate(const struct GNUNET_HELLO_Parser *parser, GNUNET_HELLO_UriCallback uc, void *uc_cls)
Iterate over URIs in a parser.
Definition: hello-uri.c:1201
struct GNUNET_MQ_Envelope * GNUNET_HELLO_parser_to_env(const struct GNUNET_HELLO_Parser *parser)
Generate envelope with GNUnet HELLO message (including peer ID) from a parser.
Definition: hello-uri.c:1103
struct GNUNET_TIME_Absolute GNUNET_HELLO_get_expiration_time_from_msg(const struct GNUNET_MessageHeader *msg)
Get the expiration time for this HELLO.
Definition: hello-uri.c:619
const struct GNUNET_PeerIdentity * GNUNET_HELLO_parser_get_id(const struct GNUNET_HELLO_Parser *parser)
Get the PeerIdentity for this builder.
Definition: hello-uri.c:412
struct GNUNET_HELLO_Parser * GNUNET_HELLO_parser_from_url(const char *url)
Parse GNUnet HELLO url.
Definition: hello-uri.c:682
char * GNUNET_HELLO_builder_to_url(const struct GNUNET_HELLO_Builder *builder, const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
Generate GNUnet HELLO URI from a builder.
Definition: hello-uri.c:997
struct GNUNET_MQ_Envelope * GNUNET_HELLO_builder_to_env(const struct GNUNET_HELLO_Builder *builder, const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_TIME_Relative expiration_time)
Generate envelope with GNUnet HELLO message (including peer ID) from a builder.
Definition: hello-uri.c:1168
char * GNUNET_HELLO_builder_to_url2(const struct GNUNET_HELLO_Builder *builder, const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_TIME_Relative expiration_time)
Generate GNUnet HELLO URI from a builder.
Definition: hello-uri.c:873
#define GNUNET_log(kind,...)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
GNUNET_GenericReturnValue
Named constants for return values.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_MessageHeader * GNUNET_MQ_env_get_msg(const struct GNUNET_MQ_Envelope *env)
Obtain message contained in envelope.
Definition: mq.c:896
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
struct GNUNET_PEERSTORE_IterateContext * GNUNET_PEERSTORE_iteration_start(struct GNUNET_PEERSTORE_Handle *h, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, GNUNET_PEERSTORE_Processor callback, void *callback_cls)
Iterate over peerstore entries.
void GNUNET_PEERSTORE_iteration_next(struct GNUNET_PEERSTORE_IterateContext *ic, uint64_t limit)
Continue an iteration.
void GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
Disconnect from the PEERSTORE service.
struct GNUNET_PEERSTORE_Handle * GNUNET_PEERSTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the PEERSTORE service.
void GNUNET_PEERSTORE_hello_add_cancel(struct GNUNET_PEERSTORE_StoreHelloContext *huc)
Cancel the request to add a hello.
void GNUNET_PEERSTORE_iteration_stop(struct GNUNET_PEERSTORE_IterateContext *ic)
Cancel an iteration.
#define GNUNET_PEERSTORE_HELLO_KEY
Key used for storing HELLO in the peerstore.
struct GNUNET_PEERSTORE_StoreHelloContext * GNUNET_PEERSTORE_hello_add(struct GNUNET_PEERSTORE_Handle *h, const struct GNUNET_MessageHeader *msg, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Add hello to peerstore.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:407
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1339
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, struct GNUNET_TIME_Relative *rtime)
Convert a given fancy human-readable time to our internal representation.
Definition: strings.c:259
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:438
#define GNUNET_TIME_UNIT_DAYS
One day.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:640
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
Private ECC key encoded for transmission.
Definition of a command line option.
Context for building (or parsing) HELLO URIs.
Definition: hello-uri.c:205
Context for parsing HELLOs.
Definition: hello-uri.c:258
Header for all communications.
Handle to the PEERSTORE service.
Definition: peerstore_api.c:44
Context for a iterate request.
Single PEERSTORE record.
Context for a add hello uri request.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.