GNUnet 0.21.2
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"
28#include "gnunet_transport_plugin.h"
29
34{
38 char *buf;
39
43 size_t max;
44
48 size_t ret;
49
51};
52
57{
62
67
72 struct GNUNET_TRANSPORT_PluginFunctions *api;
73
78
82 char *lib_name;
83
88 struct GNUNET_TRANSPORT_PluginEnvironment env;
89};
90
91static int address_count;
92
97
102
106static char *hello_file;
107
112
117
118static void
120{
121 struct TransportPlugin *plug;
122 struct TransportPlugin *next;
123 char *libname;
124 char *plugs;
125 char *pos;
126
127 if (NULL != plugins_head)
128 return; /* already loaded */
129 if (GNUNET_OK !=
130 GNUNET_CONFIGURATION_get_value_string (cfg, "TRANSPORT", "PLUGINS",
131 &plugs))
132 return;
133 fprintf (stdout,"Starting transport plugins `%s'\n",
134 plugs);
135 for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
136 {
137 fprintf (stdout,"Loading `%s' transport plugin\n",
138 pos);
139 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", pos);
140 plug = GNUNET_new (struct TransportPlugin);
141 plug->short_name = GNUNET_strdup (pos);
142 plug->lib_name = libname;
143 plug->env.cfg = cfg;
144 plug->env.cls = plug->short_name;
146 }
147 GNUNET_free (plugs);
149 while (next != NULL)
150 {
151 plug = next;
152 next = plug->next;
153 plug->api = GNUNET_PLUGIN_load (plug->lib_name, &plug->env);
154 if (plug->api == NULL)
155 {
156 fprintf (stdout,"Failed to load transport plugin for `%s'\n",
157 plug->lib_name);
159 GNUNET_free (plug->short_name);
160 GNUNET_free (plug->lib_name);
161 GNUNET_free (plug);
162 }
163 }
164}
165
166
167static int
168add_to_builder (void *cls,
169 const struct GNUNET_HELLO_Address *address,
171{
172 struct GNUNET_HELLO_Builder *builder= cls;
173 struct TransportPlugin *pos = plugins_head;
174 const char *addr;
175 char *uri;
176
177 while (NULL != pos)
178 {
180 "short_name: %s transport_name: %s\n",
181 pos->short_name,
182 address->transport_name);
183 if (0 == strcmp (address->transport_name, pos->short_name))
184 {
186 "short_name: %s transport_name: %s are the same\n",
187 pos->short_name,
188 address->transport_name);
189 addr = strchr (strchr (pos->api->address_to_string (pos, address, address->address_length), '.')+1, '.') + 1;
190 }
191 pos = pos->next;
192 }
193
195 "Hello address string: %s\n",
196 addr);
197 GNUNET_asprintf (&uri, "%s://%s", address->transport_name, addr);
199 "Hello address uri string: %s\n",
200 uri);
202 uri);
203}
204
205
214static int
215add_to_buf (void *cls,
216 const struct GNUNET_HELLO_Address *address,
218{
219 struct AddContext *ac = cls;
220 size_t ret;
221
222 ret = GNUNET_HELLO_add_address (address,
224 ac->buf,
225 ac->max);
226
227 ac->buf += ret;
228 ac->max -= ret;
229 ac->ret += ret;
231 return GNUNET_OK;
232}
233
234
243static ssize_t
244add_from_hello (void *cls, size_t max, void *buf)
245{
246 struct GNUNET_HELLO_Message **orig = cls;
247 struct AddContext ac;
248
249 if (NULL == *orig)
250 return GNUNET_SYSERR; /* already done */
251 ac.buf = buf;
252 ac.max = max;
253 ac.ret = 0;
255 NULL ==
256 GNUNET_HELLO_iterate_addresses (*orig, GNUNET_NO, &add_to_buf, &ac));
257 *orig = NULL;
258 return ac.ret;
259}
260
261
270static void
271run (void *cls,
272 char *const *args,
273 const char *cfgfile,
274 const struct GNUNET_CONFIGURATION_Handle *c)
275{
276 struct GNUNET_DISK_FileHandle *fh;
277 struct GNUNET_HELLO_Message *orig;
278 struct GNUNET_HELLO_Message *result;
280 uint64_t fsize;
281 ssize_t size_written;
283 char *url;
284 const struct GNUNET_MessageHeader *msg;
285 struct GNUNET_MQ_Envelope *env;
286
287 plugins_load (c);
288 address_count = 0;
289
294 fprintf (stdout,"We are peer %s\n", GNUNET_i2s (&my_full_id));
295
296 GNUNET_log_setup ("gnunet-hello", "DEBUG", NULL);
297
298 if (GNUNET_OK !=
300 {
301 fprintf (stderr,
302 _ ("Error accessing file `%s': %s\n"),
304 strerror (errno));
305 return;
306 }
307 if (fsize > 65536)
308 {
309 fprintf (stderr, _ ("File `%s' is too big to be a HELLO\n"), hello_file);
310 return;
311 }
312 if (fsize < sizeof(struct GNUNET_MessageHeader))
313 {
314 fprintf (stderr, _ ("File `%s' is too small to be a HELLO\n"), hello_file);
315 return;
316 }
320 if (NULL == fh)
321 {
322 fprintf (stderr,
323 _ ("Error opening file `%s': %s\n"),
325 strerror (errno));
326 return;
327 }
328 {
329 char buf[fsize] GNUNET_ALIGN;
330
331 GNUNET_assert (fsize == GNUNET_DISK_file_read (fh, buf, fsize));
333 orig = (struct GNUNET_HELLO_Message *) buf;
334 if ((fsize < GNUNET_HELLO_size (orig)) ||
335 (GNUNET_OK != GNUNET_HELLO_get_id (orig, &pid)))
336 {
337 fprintf (stderr,
338 _ ("Did not find well-formed HELLO in file `%s'\n"),
339 hello_file);
340 return;
341 }
342 {
343 char *pids;
344
346 fprintf (stdout, "Processing HELLO for peer `%s'\n", pids);
347 GNUNET_free (pids);
348 }
349 /* result = GNUNET_HELLO_create (&pid.public_key, */
350 /* &add_from_hello, */
351 /* &orig, */
352 /* GNUNET_HELLO_is_friend_only (orig)); */
353
356 NULL ==
357 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) orig, GNUNET_NO, &add_to_builder, builder));
359 fprintf (stdout,"url: %s\n", url);
364 //GNUNET_assert (NULL != result);
365 GNUNET_assert (NULL != msg);
366 fh =
371 if (NULL == fh)
372 {
373 fprintf (stderr,
374 _ ("Error opening file `%s': %s\n"),
376 strerror (errno));
378 return;
379 }
380 //fsize = GNUNET_HELLO_size (result);
381 size_written = GNUNET_DISK_file_write (fh, msg, ntohs (msg->size));
382 if (ntohs (msg->size) != size_written)
383 {
384 fprintf (stderr,
385 _ ("Error writing HELLO to file `%s': %s expected size %u size written %u\n"),
387 strerror (errno));
388 (void) GNUNET_DISK_file_close (fh);
389 return;
390 }
392 }
393 fprintf (stderr,
394 _ ("Modified %u addresses, wrote %u bytes\n"),
396 (unsigned int) ntohs (msg->size));
398}
399
400
401int
402main (int argc, char *argv[])
403{
406 "hello-file",
407 "HELLO_FILE",
408 gettext_noop ("Hello file to read"),
409 &hello_file),
411 int ret;
412
413 ret = (GNUNET_OK ==
415 argv,
416 "gnunet-peerinfo",
417 gettext_noop ("Print information about peers."),
418 options,
419 &run,
420 NULL,
421 GNUNET_YES));
422 return ret;
423}
424
425
426/* 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:70
static int ret
Final status code.
Definition: gnunet-arm.c:94
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
static char * address
GNS address for this phone.
static struct HostSet * builder
NULL if we are not currently iterating over peer information.
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
int main(int argc, char *argv[])
Definition: gnunet-hello.c:402
struct GNUNET_PeerIdentity my_full_id
Local peer own ID.
Definition: gnunet-hello.c:101
static int add_to_builder(void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
Definition: gnunet-hello.c:168
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:244
static int address_count
Definition: gnunet-hello.c:91
static struct TransportPlugin * plugins_head
Head of DLL of all loaded plugins.
Definition: gnunet-hello.c:111
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run without the scheduler.
Definition: gnunet-hello.c:271
static int add_to_buf(void *cls, const struct GNUNET_HELLO_Address *address, struct GNUNET_TIME_Absolute expiration)
Add the given address with infinite expiration to the buffer.
Definition: gnunet-hello.c:215
static char * hello_file
The file with hello in old style which we like to replace with the new one.
Definition: gnunet-hello.c:106
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Our private key.
Definition: gnunet-hello.c:96
static void plugins_load(const struct GNUNET_CONFIGURATION_Handle *cfg)
Definition: gnunet-hello.c:119
static struct TransportPlugin * plugins_tail
Head of DLL of all loaded plugins.
Definition: gnunet-hello.c:116
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 int result
Global testing status.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
Helper library for handling HELLO URIs.
Constants for network protocols.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
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:198
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_configuration(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new private key by reading our peer's key from the file specified in the configuration.
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:1237
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:686
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_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1308
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:622
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
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_builder_free(struct GNUNET_HELLO_Builder *builder)
Release resources of a builder.
Definition: hello-uri.c:373
struct GNUNET_HELLO_Builder * GNUNET_HELLO_builder_new(const struct GNUNET_PeerIdentity *pid)
Allocate builder.
Definition: hello-uri.c:355
enum GNUNET_GenericReturnValue GNUNET_HELLO_builder_add_address(struct GNUNET_HELLO_Builder *builder, const char *address)
Add individual address to the builder.
Definition: hello-uri.c:835
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:717
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:631
#define GNUNET_log(kind,...)
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:252
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
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.
enum GNUNET_GenericReturnValue GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#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:879
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:198
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run2(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls, int run_without_scheduler)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:132
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define max(x, y)
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Closure for add_to_buf().
Definition: gnunet-hello.c:34
size_t ret
Number of bytes added so far.
Definition: gnunet-hello.c:48
struct GNUNET_HELLO_Builder * builder
Definition: gnunet-hello.c:50
char * buf
Where to add.
Definition: gnunet-hello.c:38
size_t max
Maximum number of bytes left.
Definition: gnunet-hello.c:43
Private ECC key encoded for transmission.
Handle used to access files (and pipes).
Definition of a command line option.
Context for building (or parsing) HELLO URIs.
Definition: hello-uri.c:205
Header for all communications.
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.
Entry in doubly-linked list of all of our plugins.
Definition: gnunet-hello.c:57
char * lib_name
Name of the library (e.g.
Definition: gnunet-hello.c:82
struct GNUNET_TRANSPORT_PluginEnvironment env
Environment this transport service is using for this plugin.
Definition: gnunet-hello.c:88
struct TransportPlugin * next
This is a doubly-linked list.
Definition: gnunet-hello.c:61
char * short_name
Short name for the plugin (e.g.
Definition: gnunet-hello.c:77
struct TransportPlugin * prev
This is a doubly-linked list.
Definition: gnunet-hello.c:66
struct GNUNET_TRANSPORT_PluginFunctions * api
API of the transport as returned by the plugin's initialization function.
Definition: gnunet-hello.c:72