GNUnet debian-0.24.3-24-gfea921bd2
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_pils_service.h"
33#include "gnunet_util_lib.h"
35
39static int ret;
40
41/*
42 * Handle to PEERSTORE service
43 */
45
46/*
47 * Handle to PILS service
48 */
50
55
60
65
69static char *expirationstring;
70
75
80
84static int print_hellos;
85
90
95
102static void
103shutdown_task (void *cls)
104{
105 (void) cls;
106 if (NULL != shc)
107 {
109 shc = NULL;
110 }
111 if (NULL != iter_ctx)
112 {
114 }
115 if (NULL != peerstore_handle)
116 {
118 peerstore_handle = NULL;
119 }
120 if (NULL != op)
122 if (NULL != pils_handle)
123 {
125 peerstore_handle = NULL;
126 }
127}
128
129
137static void
139 const struct GNUNET_PeerIdentity *pid,
140 const char *uri)
141{
142 (void) cls;
143
144
145 printf ("|- %s\n", uri);
146}
147
148
149static void
150url_resign_cb (void *cls,
151 const struct GNUNET_PeerIdentity *pid,
152 const struct GNUNET_CRYPTO_EddsaSignature *sig)
153{
154 struct GNUNET_HELLO_Builder *builder = cls;
155 char *url;
156
158 pid,
159 sig,
161 &url);
162
163 printf ("%s\n", url);
164 GNUNET_free (url); // TODO is this right?
165}
166
167
168static void
170{
171 const struct GNUNET_MessageHeader *msg;
172
174 fwrite (msg, 1, ntohs (msg->size), stdout);
175}
176
177
178static void
180{
182 {
183 char *url;
185 printf ("%s\n", url);
186 GNUNET_free (url);
188 return;
189 }
190 else
191 {
192 struct GNUNET_MQ_Envelope *env;
194 output_env (env);
196 }
197}
198
199
200static void
202 const struct GNUNET_PeerIdentity *pid,
203 const struct GNUNET_CRYPTO_EddsaSignature *sig)
204{
205 struct GNUNET_HELLO_Builder *builder = cls;
206 struct GNUNET_MQ_Envelope *env;
207
209 pid,
210 sig,
212 output_env (env);
214}
215
216
217static void
218hello_iter (void *cls, const struct GNUNET_PEERSTORE_Record *record,
219 const char *emsg)
220{
221 struct GNUNET_HELLO_Parser *hp;
222 struct GNUNET_TIME_Absolute hello_exp;
223 const struct GNUNET_PeerIdentity *pid;
224
225 if ((NULL == record) && (NULL == emsg))
226 {
230 iter_ctx = NULL;
232 return;
233 }
234 if (NULL != emsg)
235 {
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", emsg);
238 return;
239 }
241 if (NULL == hp)
242 {
244 "The HELLO is invalid. Skipping.\n");
246 return;
247 }
250 if (print_hellos)
251 {
252 printf ("`%s' (expires: %s):\n", GNUNET_i2s (pid),
255 }
258}
259
260
261static void
262hello_store_success (void *cls, int success)
263{
264 shc = NULL;
265 if (GNUNET_OK != success)
266 {
268 "Storing hello uri failed\n");
269 }
270 printf ("HELLO imported.");
272}
273
274
275static void
276pid_changed_cb (void *cls,
277 const struct GNUNET_HELLO_Parser *parser,
278 const struct GNUNET_HashCode *addr_hash)
279{
281 if (! export_own_hello)
282 return;
283
284 if (NULL != expirationstring)
285 {
289 builder,
292 builder);
294 return;
295 }
298}
299
300
309static void
310run (void *cls,
311 char *const *args,
312 const char *cfgfile,
313 const struct GNUNET_CONFIGURATION_Handle *cfg)
314{
315 struct GNUNET_HELLO_Parser *hp;
316 struct GNUNET_MQ_Envelope *env;
317 struct GNUNET_TIME_Relative hello_validity_rel;
318 char *keyfile;
319 (void) cls;
320 (void) cfgfile;
321
322 if (NULL != args[0])
323 {
324 fprintf (stderr, _ ("Invalid command line argument `%s'\n"), args[0]);
325 return;
326 }
327 if (! print_hellos &&
330 {
331 fprintf (stderr, "%s", _ ("No argument given.\n"));
332 ret = 1;
334 return;
335 }
337 if (GNUNET_OK !=
339 "PEER",
340 "PRIVATE_KEY",
341 &keyfile))
342 {
343 GNUNET_log (
345 _ ("Core service is lacking HOSTKEY configuration setting. Exiting.\n"));
347 ret = 1;
348 return;
349 }
353 GNUNET_assert (NULL != pils_handle);
354 hello_validity_rel = GNUNET_TIME_UNIT_DAYS;
355 if (NULL != expirationstring)
356 {
358 &hello_validity_rel)
359 )
360 {
361 fprintf (stderr, "Invalid expiration time `%s'", expirationstring);
363 return;
364 }
365 }
368 {
369 char buffer[GNUNET_MAX_MESSAGE_SIZE - 1];
370 char *write_ptr;
371 ssize_t nread;
372 size_t read_total = 0;
373
374 write_ptr = buffer;
375 while (0 < (nread = fread (write_ptr, 1,
376 sizeof buffer - read_total, stdin)))
377 {
378 read_total += nread;
379 write_ptr += nread;
380 }
381 buffer[read_total] = '\0';
382 if (strlen ("gnunet://hello/") > read_total)
383 {
384 fprintf (stderr, "HELLO malformed\n");
386 return;
387 }
388 if (0 == strncasecmp ("gnunet://hello/",
389 buffer, strlen ("gnunet://hello/")))
390 {
391 // Remove newline
392 buffer[read_total - 1] = '\0';
393 hp = GNUNET_HELLO_parser_from_url (buffer);
394 if (NULL == hp)
395 {
396 fprintf (stderr, "Unable to parse URI `%s'\n", buffer);
398 return;
399 }
403 &hello_store_success, NULL);
406 }
407 else if (read_total > sizeof (struct GNUNET_MessageHeader))
408 {
410 (const struct GNUNET_MessageHeader*)
411 buffer,
412 &hello_store_success, NULL);
413 }
414 else
415 {
416 fprintf (stderr, "HELLO malformed\n");
418 }
419 return;
420 }
421
422 if (print_hellos)
423 {
425 "peerstore",
426 NULL,
428 &hello_iter,
429 NULL);
430 }
431}
432
433
441int
442main (int argc, char *const *argv)
443{
444 int res;
447 "export-hello",
449 "Print a HELLO for our peer identity"),
452 "binary",
454 "Output HELLO in binary format. Use with `--export'."),
457 "import-hello",
458 gettext_noop ("Import a HELLO"),
459 &import_hello),
461 "expiration",
462 "TIME",
464 "Expiration time to set for exported hello. (Default: 1 day)"),
467 "dump-hellos",
469 "List all known HELLOs in peerstore"),
470 &print_hellos),
472 };
473
475 argc,
476 argv,
477 "gnunet-hello",
479 "Import/export/print HELLOs."),
480 options,
481 &run,
482 NULL);
483
484 if (GNUNET_OK == res)
485 return ret;
486 return 1;
487}
488
489
490/* 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 HostSet * builder
NULL if we are not currently iterating over peer information.
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:138
static int export_own_hello
HELLO export option -H.
Definition: gnunet-hello.c:64
static void env_resign_cb(void *cls, const struct GNUNET_PeerIdentity *pid, const struct GNUNET_CRYPTO_EddsaSignature *sig)
Definition: gnunet-hello.c:201
static void hello_iter(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Definition: gnunet-hello.c:218
static void output_parser(const struct GNUNET_HELLO_Parser *parser)
Definition: gnunet-hello.c:179
static void url_resign_cb(void *cls, const struct GNUNET_PeerIdentity *pid, const struct GNUNET_CRYPTO_EddsaSignature *sig)
Definition: gnunet-hello.c:150
static struct GNUNET_PILS_Handle * pils_handle
Definition: gnunet-hello.c:49
static enum GNUNET_GenericReturnValue import_hello
HELLO import option -I.
Definition: gnunet-hello.c:89
static int print_hellos
Hello list option -D.
Definition: gnunet-hello.c:84
static void shutdown_task(void *cls)
Task run in monitor mode when the user presses CTRL-C to abort.
Definition: gnunet-hello.c:103
static int ret
Return code.
Definition: gnunet-hello.c:39
static struct GNUNET_TIME_Absolute hello_validity
Expiration time for exported hello.
Definition: gnunet-hello.c:74
static struct GNUNET_PEERSTORE_IterateContext * iter_ctx
PEERSTORE iteration context.
Definition: gnunet-hello.c:54
struct GNUNET_PILS_Operation * op
PILS op.
Definition: gnunet-hello.c:94
static void output_env(const struct GNUNET_MQ_Envelope *env)
Definition: gnunet-hello.c:169
static enum GNUNET_GenericReturnValue binary_output
HELLO export/import format option.
Definition: gnunet-hello.c:79
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:310
int main(int argc, char *const *argv)
The main function to obtain peer information from CORE.
Definition: gnunet-hello.c:442
static char * expirationstring
Optional expiration string -E.
Definition: gnunet-hello.c:69
static struct GNUNET_PEERSTORE_Handle * peerstore_handle
Definition: gnunet-hello.c:44
static void pid_changed_cb(void *cls, const struct GNUNET_HELLO_Parser *parser, const struct GNUNET_HashCode *addr_hash)
Definition: gnunet-hello.c:276
static struct GNUNET_PEERSTORE_StoreHelloContext * shc
HELLO store context handle.
Definition: gnunet-hello.c:59
static void hello_store_success(void *cls, int success)
Definition: gnunet-hello.c:262
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.
struct GNUNET_PILS_Handle * GNUNET_PILS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_PILS_PidChangeCallback pid_change_cb, void *cls)
Connect to the PILS service.
Definition: pils_api.c:367
void GNUNET_PILS_disconnect(struct GNUNET_PILS_Handle *handle)
Disconnect from the PILS service.
Definition: pils_api.c:390
struct GNUNET_PILS_Operation * GNUNET_PILS_sign_hello(struct GNUNET_PILS_Handle *handle, const struct GNUNET_HELLO_Builder *builder, struct GNUNET_TIME_Absolute et, GNUNET_PILS_SignResultCallback cb, void *cb_cls)
Create HELLO signature.
Definition: pils_api.c:576
void GNUNET_PILS_cancel(struct GNUNET_PILS_Operation *op)
Cancel request.
Definition: pils_api.c:495
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.
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.
void GNUNET_HELLO_parser_free(struct GNUNET_HELLO_Parser *parser)
Release resources of a builder.
Definition: hello-uri.c:379
struct GNUNET_HELLO_Parser * GNUNET_HELLO_parser_from_msg(const struct GNUNET_MessageHeader *msg)
Parse msg.
Definition: hello-uri.c:415
void GNUNET_HELLO_builder_free(struct GNUNET_HELLO_Builder *builder)
Release resources of a builder.
Definition: hello-uri.c:397
char * GNUNET_HELLO_parser_to_url(const struct GNUNET_HELLO_Parser *parser)
Generate GNUnet HELLO URI from a parser.
Definition: hello-uri.c:803
struct GNUNET_HELLO_Builder * GNUNET_HELLO_builder_from_parser(const struct GNUNET_HELLO_Parser *parser, struct GNUNET_PeerIdentity *pid)
Allocate builder from parser.
Definition: hello-uri.c:360
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:975
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:903
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:597
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:353
struct GNUNET_HELLO_Parser * GNUNET_HELLO_parser_from_url(const char *url)
Parse GNUnet HELLO url.
Definition: hello-uri.c:663
enum GNUNET_GenericReturnValue GNUNET_HELLO_builder_to_url2(const struct GNUNET_HELLO_Builder *builder, const struct GNUNET_PeerIdentity *pid, const struct GNUNET_CRYPTO_EddsaSignature *sig, struct GNUNET_TIME_Absolute validity, char **result)
Generate GNUnet HELLO URI from a builder.
Definition: hello-uri.c:1279
struct GNUNET_MQ_Envelope * GNUNET_HELLO_builder_to_env(const struct GNUNET_HELLO_Builder *builder, const struct GNUNET_PeerIdentity *pid, const struct GNUNET_CRYPTO_EddsaSignature *sig, struct GNUNET_TIME_Absolute expiration_time)
Generate envelope with GNUnet HELLO message (including peer ID) from a builder.
Definition: hello-uri.c:1142
#define GNUNET_log(kind,...)
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
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
@ GNUNET_ERROR_TYPE_DEBUG
#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
#define GNUNET_TIME_UNIT_DAYS
One day.
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:316
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:660
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
an ECC signature using EdDSA.
Definition of a command line option.
Context for building (or parsing) HELLO URIs.
Definition: hello-uri.c:184
Context for parsing HELLOs.
Definition: hello-uri.c:232
A 512-bit hashcode.
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.
A handle for the PILS service.
Definition: pils_api.c:82
The identity of the host (wraps the signing key of the peer).
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.