fix rps getopt
[oweals/gnunet.git] / src / rps / gnunet-rps.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file rps/gnunet-rps.c
23  * @brief random peer sampling
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_rps_service.h"
29 #include <inttypes.h>
30
31 static int ret;
32
33 /**
34  * RPS handle
35  */
36 static struct GNUNET_RPS_Handle *rps_handle;
37
38 /**
39  * Request handle
40  */
41 static struct GNUNET_RPS_Request_Handle *req_handle;
42
43 /**
44  * PeerID in string representation (Option --seed)
45  */
46 static char *peer_id_str;
47
48
49 /**
50  * Task run when user presses CTRL-C to abort.
51  * Cancels pending request and disconnects.
52  *
53  * @param cls NULL
54  */
55 static void
56 do_shutdown (void *cls)
57 {
58   if (NULL != req_handle)
59     GNUNET_RPS_request_cancel (req_handle);
60   GNUNET_RPS_disconnect (rps_handle);
61 }
62
63
64 /**
65  * Callback called on receipt of reply.
66  * Prints replied PeerIDs.
67  *
68  * @param cls closure
69  * @param n number of peers
70  * @param recv_peers the received peers
71  */
72 static void
73 reply_handle (void *cls,
74               uint64_t n,
75               const struct GNUNET_PeerIdentity *recv_peers)
76 {
77   uint64_t i;
78
79   req_handle = NULL;
80   for (i = 0; i < n; i++)
81   {
82     FPRINTF (stdout, "%s\n",
83         GNUNET_i2s_full (&recv_peers[i]));
84   }
85   ret = 0;
86
87   GNUNET_SCHEDULER_shutdown ();
88 }
89
90
91 /**
92  * Main function that will be run by the scheduler.
93  *
94  * @param cls closure
95  * @param args remaining command-line arguments
96  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
97  * @param cfg configuration
98  */
99 static void
100 run (void *cls,
101      char *const *args,
102      const char *cfgfile,
103      const struct GNUNET_CONFIGURATION_Handle *cfg)
104 {
105   static uint64_t num_peers;
106   struct GNUNET_PeerIdentity peer_id;
107
108   rps_handle = GNUNET_RPS_connect (cfg);
109
110   if (NULL == peer_id_str)
111   { /* Request n PeerIDs */
112     /* If number was specified use it, else request single peer. */
113     num_peers = (NULL == args[0]) ? 1 : atoi (args[0]);
114     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115         "Requesting %" PRIu64 " PeerIDs\n", num_peers);
116     req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL);
117     GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
118   }
119   else
120   { /* Seed PeerID */
121     if (GNUNET_OK !=
122         GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id_str,
123                                                     strlen (peer_id_str),
124                                                     &peer_id.public_key))
125     {
126       FPRINTF (stderr,
127                _("Failed to parse peer identity `%s'\n"),
128                peer_id_str);
129       return;
130     }
131     GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
132     FPRINTF (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
133     ret = 0;
134     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
135   }
136 }
137
138 /**
139  * The main function to rps.
140  *
141  * @param argc number of arguments from the command line
142  * @param argv command line arguments
143  * @return 0 ok, 1 on error
144  */
145 int
146 main (int argc, char *const *argv)
147 {
148   const char helpstr[] =
149     "Get random GNUnet peers. If none is specified a single is requested.";
150   struct GNUNET_GETOPT_CommandLineOption options[] = {
151     GNUNET_GETOPT_OPTION_STRING ('s',
152                                  "seed",
153                                  "PEER_ID",
154                                  gettext_noop ("Seed a PeerID"),
155                                  &peer_id_str),
156     GNUNET_GETOPT_OPTION_END
157   };
158   return (GNUNET_OK ==
159           GNUNET_PROGRAM_run (argc,
160                               argv,
161                               "gnunet-rps [NUMBER_OF_PEERS]",
162                               gettext_noop
163                               (helpstr),
164                               options, &run, NULL)) ? ret : 1;
165 }
166
167 /* end of gnunet-rps.c */