kb
[oweals/gnunet.git] / src / transport / gnunet-transport.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file src/transport/gnunet-transport.c
23  * @brief Tool to help configure, measure and control the transport subsystem.
24  * @author Christian Grothoff
25  *
26  * This utility can be used to test if a transport mechanism for
27  * GNUnet is properly configured.
28  */
29
30 #include "platform.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_transport_service.h"
34
35 static char *cpid;
36
37 static struct GNUNET_TRANSPORT_Handle *handle;
38
39 static int benchmark_send;
40
41 static int benchmark_receive;
42
43 static int ret;
44
45 static unsigned long long traffic_received;
46
47 static unsigned long long traffic_sent;
48
49 static struct GNUNET_TIME_Absolute start_time;
50
51 static struct GNUNET_TRANSPORT_TransmitHandle *th;
52
53 static struct GNUNET_PeerIdentity pid;
54
55 static GNUNET_SCHEDULER_TaskIdentifier end;
56
57
58 /**
59  * Shutdown, print statistics.
60  */
61 static void
62 do_disconnect (void *cls,
63                const struct GNUNET_SCHEDULER_TaskContext *tc)
64 {
65   struct GNUNET_TIME_Relative duration;
66
67   if (NULL != th)
68   {
69     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
70     th = NULL;
71   }
72   GNUNET_TRANSPORT_disconnect (handle);
73   if (benchmark_receive)
74   {
75     duration = GNUNET_TIME_absolute_get_duration (start_time);
76     fprintf (stderr,
77              "Received %llu bytes/s (%llu bytes in %llu ms)\n",
78              1000 * traffic_received / (1+duration.rel_value),
79              traffic_received,
80              (unsigned long long) duration.rel_value);
81   }
82   if (benchmark_send)
83   {
84     duration = GNUNET_TIME_absolute_get_duration (start_time);
85     fprintf (stderr,
86              "Transmitted %llu bytes/s (%llu bytes in %llu ms)\n",
87              1000 * traffic_sent / (1+duration.rel_value),
88              traffic_sent,
89              (unsigned long long) duration.rel_value);
90   }
91 }
92
93
94 /**
95  * Function called to notify a client about the socket
96  * begin ready to queue more data.  "buf" will be
97  * NULL and "size" zero if the socket was closed for
98  * writing in the meantime.
99  *
100  * @param cls closure
101  * @param size number of bytes available in buf
102  * @param buf where the callee should write the message
103  * @return number of bytes written to buf
104  */
105 static size_t
106 transmit_data (void *cls, size_t size,
107                void *buf)
108 {
109   struct GNUNET_MessageHeader *m = buf;
110
111   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
112   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
113   m->size = ntohs (size);
114   m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
115   memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
116   traffic_sent += size;
117   th = GNUNET_TRANSPORT_notify_transmit_ready (handle,
118                                                &pid,
119                                                32 * 1024,
120                                                0,
121                                                GNUNET_TIME_UNIT_FOREVER_REL,
122                                                &transmit_data, NULL);
123   fprintf (stderr,
124            "Transmitting %u bytes to %s\n",
125            (unsigned int) size,
126            GNUNET_i2s (&pid));
127   return size;
128 }
129
130
131 /**
132  * Function called to notify transport users that another
133  * peer connected to us.
134  *
135  * @param cls closure
136  * @param peer the peer that connected
137  * @param ats performance data
138  * @param ats_count number of entries in ats (excluding 0-termination)
139  */
140 static void
141 notify_connect (void *cls,
142                 const struct GNUNET_PeerIdentity
143                 * peer,
144                 const struct
145                 GNUNET_ATS_Information
146                 * ats, uint32_t ats_count)
147 {
148   fprintf (stderr,
149            "Connected to %s\n",
150            GNUNET_i2s (peer));
151   if (0 != memcmp (&pid,
152                    peer,
153                    sizeof (struct GNUNET_PeerIdentity)))
154     return;
155   ret = 0;
156   if (benchmark_send) 
157   {
158     start_time = GNUNET_TIME_absolute_get ();
159     th = GNUNET_TRANSPORT_notify_transmit_ready (handle,
160                                                  peer,
161                                                  32 * 1024,
162                                                  0,
163                                                  GNUNET_TIME_UNIT_FOREVER_REL,
164                                                  &transmit_data, NULL);
165   }
166   else
167   {
168     /* all done, terminate instantly */
169     GNUNET_SCHEDULER_cancel (end);
170     end = GNUNET_SCHEDULER_add_now (&do_disconnect,
171                                     NULL);    
172   }  
173 }
174
175
176 /**
177  * Function called to notify transport users that another
178  * peer disconnected from us.
179  *
180  * @param cls closure
181  * @param peer the peer that disconnected
182  */
183 static void
184 notify_disconnect (void *cls,
185                    const struct
186                    GNUNET_PeerIdentity * peer)
187 {
188   fprintf (stderr,
189            "Disconnected from %s\n",
190            GNUNET_i2s (peer));
191   if ( (0 == memcmp (&pid,
192                      peer,
193                      sizeof (struct GNUNET_PeerIdentity))) &&
194        (NULL != th) )
195   {
196     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
197     th = NULL;
198     GNUNET_SCHEDULER_cancel (end);
199     end = GNUNET_SCHEDULER_add_now (&do_disconnect,
200                                     NULL);    
201   }
202 }
203
204
205 /**
206  * Function called by the transport for each received message.
207  *
208  * @param cls closure
209  * @param peer (claimed) identity of the other peer
210  * @param message the message
211  * @param ats performance data
212  * @param ats_count number of entries in ats 
213  */
214 static void
215 notify_receive (void *cls,
216                 const struct
217                 GNUNET_PeerIdentity * peer,
218                 const struct
219                 GNUNET_MessageHeader *
220                 message,
221                 const struct
222                 GNUNET_ATS_Information
223                 * ats, uint32_t ats_count)
224 {
225   if (! benchmark_receive)
226     return;
227   fprintf (stderr,
228            "Received %u bytes from %s\n",
229            (unsigned int) ntohs (message->size),
230            GNUNET_i2s (peer));
231   if (traffic_received == 0)
232     start_time = GNUNET_TIME_absolute_get ();
233   traffic_received += ntohs (message->size);
234 }
235
236
237 /**
238  * Main function that will be run by the scheduler.
239  *
240  * @param cls closure
241  * @param args remaining command-line arguments
242  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
243  * @param cfg configuration
244  */
245 static void
246 run (void *cls, char *const *args, const char *cfgfile,
247      const struct GNUNET_CONFIGURATION_Handle *cfg)
248 {
249   if (benchmark_send && (NULL == cpid))
250   {
251     fprintf (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
252              "-s", "-C");
253     return;
254   }
255   if (NULL != cpid)
256   {
257     ret = 1;
258     if (GNUNET_OK !=
259         GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
260     {
261       fprintf (stderr,
262                _("Failed to parse peer identity `%s'\n"),
263                cpid);
264       return;
265     }
266     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
267                                        &notify_receive, 
268                                        &notify_connect,
269                                        &notify_disconnect);
270     GNUNET_TRANSPORT_try_connect (handle, &pid);
271     end = GNUNET_SCHEDULER_add_delayed (benchmark_send
272                                         ? GNUNET_TIME_UNIT_FOREVER_REL
273                                         : GNUNET_TIME_UNIT_SECONDS,
274                                         &do_disconnect,
275                                         NULL);    
276   } else if (benchmark_receive)
277   {
278     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
279                                        &notify_receive, 
280                                        &notify_connect,
281                                        &notify_disconnect);
282     GNUNET_TRANSPORT_try_connect (handle, &pid);
283     end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
284                                         &do_disconnect,
285                                         NULL); 
286   }
287 }
288
289
290 int
291 main (int argc, char *const *argv)
292 {
293   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
294     {'b', "benchmark", NULL,
295      gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
296      0, &GNUNET_GETOPT_set_one, &benchmark_receive},
297     {'C', "connect", "PEER",
298      gettext_noop ("try to connect to the given peer"),
299      1, &GNUNET_GETOPT_set_string, &cpid},
300     {'s', "send", NULL,
301      gettext_noop ("send data for benchmarking to the other peer (until CTRL-C)"),
302      0, &GNUNET_GETOPT_set_one, &benchmark_send},  
303     GNUNET_GETOPT_OPTION_END
304   };
305   return (GNUNET_OK ==
306           GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
307                               gettext_noop ("Direct access to transport service."),
308                               options, &run, NULL)) ? ret : 1;
309 }
310
311
312 /* end of gnunet-transport.c */