fix
[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\n",
78              traffic_received / (1+duration.rel_value));
79   }
80   if (benchmark_send)
81   {
82     duration = GNUNET_TIME_absolute_get_duration (start_time);
83     fprintf (stderr,
84              "Transmitted %llu bytes/s\n",
85              traffic_sent / (1+duration.rel_value));
86   }
87 }
88
89
90 /**
91  * Function called to notify a client about the socket
92  * begin ready to queue more data.  "buf" will be
93  * NULL and "size" zero if the socket was closed for
94  * writing in the meantime.
95  *
96  * @param cls closure
97  * @param size number of bytes available in buf
98  * @param buf where the callee should write the message
99  * @return number of bytes written to buf
100  */
101 static size_t
102 transmit_data (void *cls, size_t size,
103                void *buf)
104 {
105   struct GNUNET_MessageHeader *m = buf;
106
107   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
108   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
109   m->size = ntohs (size);
110   m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
111   memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
112   traffic_sent += size;
113   th = GNUNET_TRANSPORT_notify_transmit_ready (handle,
114                                                &pid,
115                                                32 * 1024,
116                                                0,
117                                                GNUNET_TIME_UNIT_FOREVER_REL,
118                                                &transmit_data, NULL);
119   return size;
120 }
121
122
123 /**
124  * Function called to notify transport users that another
125  * peer connected to us.
126  *
127  * @param cls closure
128  * @param peer the peer that connected
129  * @param ats performance data
130  * @param ats_count number of entries in ats (excluding 0-termination)
131  */
132 static void
133 notify_connect (void *cls,
134                 const struct GNUNET_PeerIdentity
135                 * peer,
136                 const struct
137                 GNUNET_ATS_Information
138                 * ats, uint32_t ats_count)
139 {
140   if (0 != memcmp (&pid,
141                    peer,
142                    sizeof (struct GNUNET_PeerIdentity)))
143     return;
144   ret = 0;
145   if (benchmark_send) 
146   {
147     start_time = GNUNET_TIME_absolute_get ();
148     th = GNUNET_TRANSPORT_notify_transmit_ready (handle,
149                                                  peer,
150                                                  32 * 1024,
151                                                  0,
152                                                  GNUNET_TIME_UNIT_FOREVER_REL,
153                                                  &transmit_data, NULL);
154   }
155   else
156   {
157     /* all done, terminate instantly */
158     GNUNET_SCHEDULER_cancel (end);
159     end = GNUNET_SCHEDULER_add_now (&do_disconnect,
160                                     NULL);    
161   }  
162 }
163
164
165 /**
166  * Function called to notify transport users that another
167  * peer disconnected from us.
168  *
169  * @param cls closure
170  * @param peer the peer that disconnected
171  */
172 static void
173 notify_disconnect (void *cls,
174                    const struct
175                    GNUNET_PeerIdentity * peer)
176 {
177   if ( (0 == memcmp (&pid,
178                      peer,
179                      sizeof (struct GNUNET_PeerIdentity))) &&
180        (NULL != th) )
181   {
182     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
183     th = NULL;
184     GNUNET_SCHEDULER_cancel (end);
185     end = GNUNET_SCHEDULER_add_now (&do_disconnect,
186                                     NULL);    
187   }
188 }
189
190
191 /**
192  * Function called by the transport for each received message.
193  *
194  * @param cls closure
195  * @param peer (claimed) identity of the other peer
196  * @param message the message
197  * @param ats performance data
198  * @param ats_count number of entries in ats 
199  */
200 static void
201 notify_receive (void *cls,
202                 const struct
203                 GNUNET_PeerIdentity * peer,
204                 const struct
205                 GNUNET_MessageHeader *
206                 message,
207                 const struct
208                 GNUNET_ATS_Information
209                 * ats, uint32_t ats_count)
210 {
211   if (! benchmark_receive)
212     return;
213   if (traffic_received == 0)
214     start_time = GNUNET_TIME_absolute_get ();
215   traffic_received += ntohs (message->size);
216 }
217
218
219 /**
220  * Main function that will be run by the scheduler.
221  *
222  * @param cls closure
223  * @param args remaining command-line arguments
224  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
225  * @param cfg configuration
226  */
227 static void
228 run (void *cls, char *const *args, const char *cfgfile,
229      const struct GNUNET_CONFIGURATION_Handle *cfg)
230 {
231   if (benchmark_send && (NULL == cpid))
232   {
233     fprintf (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
234              "-s", "-C");
235     return;
236   }
237   if (NULL != cpid)
238   {
239     ret = 1;
240     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
241                                        &notify_receive, 
242                                        &notify_connect,
243                                        &notify_disconnect);
244     if (GNUNET_OK !=
245         GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
246     {
247       fprintf (stderr,
248                _("Failed to parse peer identity `%s'\n"),
249                cpid);
250       GNUNET_TRANSPORT_disconnect (handle);
251       return;
252     }
253     GNUNET_TRANSPORT_try_connect (handle, &pid);
254     end = GNUNET_SCHEDULER_add_delayed (benchmark_send
255                                         ? GNUNET_TIME_UNIT_FOREVER_REL
256                                         : GNUNET_TIME_UNIT_SECONDS,
257                                         &do_disconnect,
258                                         NULL);    
259   } else if (benchmark_receive)
260   {
261     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
262                                        &notify_receive, 
263                                        &notify_connect,
264                                        &notify_disconnect);
265     GNUNET_TRANSPORT_try_connect (handle, &pid);
266     end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL
267                                         &do_disconnect,
268                                         NULL); 
269   }
270 }
271
272
273 int
274 main (int argc, char *const *argv)
275 {
276   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
277     {'b', "benchmark", NULL,
278      gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
279      0, &GNUNET_GETOPT_set_one, &benchmark_receive},
280     {'C', "connect", "PEER",
281      gettext_noop ("try to connect to the given peer"),
282      1, &GNUNET_GETOPT_set_string, &cpid},
283     {'s', "send", NULL,
284      gettext_noop ("send data for benchmarking to the other peer (until CTRL-C)"),
285      0, &GNUNET_GETOPT_set_one, &benchmark_send},  
286     GNUNET_GETOPT_OPTION_END
287   };
288   return (GNUNET_OK ==
289           GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
290                               gettext_noop ("Direct access to transport service."),
291                               options, &run, NULL)) ? ret : 1;
292 }
293
294
295 /* end of gnunet-transport.c */