summaryrefslogtreecommitdiff
path: root/src/rps/gnunet-rps.c
blob: 50b5b67397e7bbd1140510a83faa25b952576322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
     This file is part of GNUnet.
     Copyright (C)

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.
    
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
*/

/**
 * @file rps/gnunet-rps.c
 * @brief random peer sampling
 * @author Julius Bünger
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_rps_service.h"
#include <inttypes.h>

static int ret;

/**
 * RPS handle
 */
static struct GNUNET_RPS_Handle *rps_handle;

/**
 * Request handle
 */
static struct GNUNET_RPS_Request_Handle *req_handle;

/**
 * PeerID (Option --seed)
 */
static struct GNUNET_PeerIdentity peer_id;

/**
 * @brief Do we want to receive updates of the view? (Option --view)
 */
static int view_update;

/**
 * @brief Do we want to receive updates of the view? (Option --view)
 */
static int stream_input;

/**
 * @brief Number of updates we want to receive
 */
static uint64_t num_view_updates;


/**
 * Task run when user presses CTRL-C to abort.
 * Cancels pending request and disconnects.
 *
 * @param cls NULL
 */
static void
do_shutdown (void *cls)
{
  (void) cls;

  if (NULL != req_handle)
    GNUNET_RPS_request_cancel (req_handle);
  GNUNET_RPS_disconnect (rps_handle);
}


/**
 * Callback called on receipt of reply.
 * Prints replied PeerIDs.
 *
 * @param cls closure
 * @param n number of peers
 * @param recv_peers the received peers
 */
static void
reply_handle (void *cls,
              uint64_t n,
              const struct GNUNET_PeerIdentity *recv_peers)
{
  uint64_t i;
  (void) cls;

  req_handle = NULL;
  for (i = 0; i < n; i++)
  {
    FPRINTF (stdout, "%s\n",
        GNUNET_i2s_full (&recv_peers[i]));
  }
  ret = 0;

  GNUNET_SCHEDULER_shutdown ();
}

/**
 * Callback called on receipt view update.
 * Prints view.
 *
 * @param n number of peers
 * @param recv_peers the received peers
 */
static void
view_update_handle (void *cls,
                    uint64_t n,
                    const struct GNUNET_PeerIdentity *recv_peers)
{
  uint64_t i;
  (void) cls;

  if (0 == n)
  {
    FPRINTF (stdout, "Empty view\n");
  }
  req_handle = NULL;
  for (i = 0; i < n; i++)
  {
    FPRINTF (stdout, "%s\n",
        GNUNET_i2s_full (&recv_peers[i]));
  }

  if (1 == num_view_updates)
  {
    ret = 0;
    GNUNET_SCHEDULER_shutdown ();
  }
  else if (1 < num_view_updates)
  {
    num_view_updates--;
  }
}


/**
 * Callback called on receipt of peer from biased stream
 *
 * @param n number of peers
 * @param recv_peers the received peers
 */
static void
stream_input_handle (void *cls,
                     uint64_t num_peers,
                     const struct GNUNET_PeerIdentity *recv_peers)
{
  uint64_t i;
  (void) cls;

  if (0 == num_peers)
  {
    FPRINTF (stdout, "No peer was returned\n");
  }
  req_handle = NULL;
  for (i = 0; i < num_peers; i++)
  {
    FPRINTF (stdout, "%s\n",
             GNUNET_i2s_full (&recv_peers[i]));
  }
}


/**
 * Main function that will be run by the scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  static uint64_t num_peers;
  static struct GNUNET_PeerIdentity zero_pid;
  (void) cls;
  (void) cfgfile;

  rps_handle = GNUNET_RPS_connect (cfg);
  if (NULL == rps_handle)
  {
    FPRINTF (stderr, "Failed to connect to the rps service\n");
    return;
  }

  if ((0 == memcmp (&zero_pid, &peer_id, sizeof (peer_id))) &&
      (!view_update) &&
      (!stream_input))
  { /* Request n PeerIDs */
    /* If number was specified use it, else request single peer. */
    if (NULL == args[0] ||
        0 == sscanf (args[0], "%lu", &num_peers))
    {
      num_peers = 1;
    }
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
        "Requesting %" PRIu64 " PeerIDs\n", num_peers);
    req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL);
    GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  } else if (view_update)
  {
    /* Get updates of view */
    if (NULL == args[0] ||
        0 == sscanf (args[0], "%lu", &num_view_updates))
    {
      num_view_updates = 0;
    }
    GNUNET_RPS_view_request (rps_handle, num_view_updates, view_update_handle, NULL);
    if (0 != num_view_updates)
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
          "Requesting %" PRIu64 " view updates\n", num_view_updates);
    else
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
          "Requesting continuous view updates\n");
    GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  } else if (stream_input)
  {
    /* Get updates of view */
    GNUNET_RPS_stream_request (rps_handle, stream_input_handle, NULL);
    GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  }
  else
  { /* Seed PeerID */
    GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
    FPRINTF (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
    ret = 0;
    GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  }
}

/**
 * The main function to rps.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  const char helpstr[] =
    "Get random GNUnet peers. If none is specified a single is requested.";
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_base32_auto ('s',
                                          "seed",
                                          "PEER_ID",
                                          gettext_noop ("Seed a PeerID"),
                                          &peer_id),
    GNUNET_GETOPT_option_flag ('V',
                               "view",
                               gettext_noop ("Get updates of view (0 for infinite updates)"),
                               &view_update),
    GNUNET_GETOPT_option_flag ('S',
                               "stream",
                               gettext_noop ("Get peers from biased stream"),
                               &stream_input),
    GNUNET_GETOPT_OPTION_END
  };
  return (GNUNET_OK ==
          GNUNET_PROGRAM_run (argc,
                              argv,
                              "gnunet-rps [NUMBER_OF_PEERS]",
                              gettext_noop
                              (helpstr),
                              options, &run, NULL)) ? ret : 1;
}

/* end of gnunet-rps.c */