indentation
[oweals/gnunet.git] / src / core / test_core_api_send_to_self.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff
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 core/test_core_api_send_to_self.c
23  * @brief
24  * @author Philipp Toelke
25  */
26 #include <platform.h>
27 #include <gnunet_common.h>
28 #include <gnunet_program_lib.h>
29 #include <gnunet_protocols.h>
30 #include <gnunet_core_service.h>
31 #include <gnunet_constants.h>
32
33 /**
34  * Final status code.
35  */
36 static int ret;
37
38 /**
39  * Handle to the cleanup task.
40  */
41 GNUNET_SCHEDULER_TaskIdentifier die_task;
42
43 static struct GNUNET_PeerIdentity myself;
44
45 /**
46  * Configuration to load for the new peer.
47  */
48 struct GNUNET_CONFIGURATION_Handle *core_cfg;
49
50 /**
51  * The handle to core
52  */
53 struct GNUNET_CORE_Handle *core;
54
55 /**
56  * Handle to gnunet-service-arm.
57  */
58 struct GNUNET_OS_Process *arm_proc;
59
60 /**
61  * Function scheduled as very last function, cleans up after us
62  */
63 static void
64 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tskctx)
65 {
66   die_task = GNUNET_SCHEDULER_NO_TASK;
67
68   if (core != NULL)
69   {
70     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting core.\n");
71     GNUNET_CORE_disconnect (core);
72     core = NULL;
73   }
74
75   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peer\n");
76   if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
77     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
78
79   if (GNUNET_OS_process_wait (arm_proc) != GNUNET_OK)
80     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
81
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
83               GNUNET_OS_process_get_pid (arm_proc));
84   GNUNET_OS_process_close (arm_proc);
85   arm_proc = NULL;
86
87   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
88 }
89
90 static int
91 receive (void *cls, const struct GNUNET_PeerIdentity *other,
92          const struct GNUNET_MessageHeader *message,
93          const struct GNUNET_TRANSPORT_ATS_Information *atsi)
94 {
95   if (die_task != GNUNET_SCHEDULER_NO_TASK)
96     GNUNET_SCHEDULER_cancel (die_task);
97   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer %s\n",
98               GNUNET_i2s (other));
99   GNUNET_SCHEDULER_add_now (&cleanup, NULL);
100   ret = 0;
101   return GNUNET_OK;
102 }
103
104 static size_t
105 send_message (void *cls, size_t size, void *buf)
106 {
107   if (size == 0 || buf == NULL)
108   {
109     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send; got 0 buffer\n");
110     return 0;
111   }
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending!\n");
113   struct GNUNET_MessageHeader *hdr = buf;
114
115   hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
116   hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
117   return ntohs (hdr->size);
118 }
119
120 static void
121 init (void *cls, struct GNUNET_CORE_Handle *core,
122       const struct GNUNET_PeerIdentity *my_identity,
123       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pk)
124 {
125   if (core == NULL)
126   {
127     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could NOT connect to CORE;\n");
128     return;
129   }
130   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131               "Correctly connected to CORE; we are the peer %s.\n",
132               GNUNET_i2s (my_identity));
133   memcpy (&myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
134 }
135
136 static void
137 connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
138             const struct GNUNET_TRANSPORT_ATS_Information *atsi)
139 {
140   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n",
141               GNUNET_i2s (peer));
142   if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity)))
143   {
144     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145                 "Connected to myself; sending message!\n");
146     GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES, 0,
147                                        GNUNET_TIME_UNIT_FOREVER_REL, peer,
148                                        sizeof (struct GNUNET_MessageHeader),
149                                        send_message, NULL);
150   }
151 }
152
153
154 /**
155  * Main function that will be run by the scheduler.
156  *
157  * @param cls closure
158  * @param args remaining command-line arguments
159  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
160  * @param cfg configuration
161  */
162 static void
163 run (void *cls, char *const *args, const char *cfgfile,
164      const struct GNUNET_CONFIGURATION_Handle *cfg)
165 {
166   const static struct GNUNET_CORE_MessageHandler handlers[] = {
167     {&receive, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP, 0},
168     {NULL, 0, 0}
169   };
170
171   core_cfg = GNUNET_CONFIGURATION_create ();
172
173   arm_proc =
174       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
175                                "gnunet-service-arm",
176 #if VERBOSE
177                                "-L", "DEBUG",
178 #endif
179                                "-c", "test_core_api_peer1.conf", NULL);
180
181   GNUNET_assert (GNUNET_OK ==
182                  GNUNET_CONFIGURATION_load (core_cfg,
183                                             "test_core_api_peer1.conf"));
184
185   core =
186       GNUNET_CORE_connect (core_cfg, 42, NULL, &init, &connect_cb, NULL, NULL,
187                            NULL, 0, NULL, 0, handlers);
188
189   die_task =
190       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
191                                     (GNUNET_TIME_UNIT_SECONDS, 60), &cleanup,
192                                     cls);
193 }
194
195
196 static int
197 check ()
198 {
199   char *const argv[] = { "test-core-api-send-to-self",
200     "-c",
201     "test_core_api_data.conf",
202 #if VERBOSE
203     "-L", "DEBUG",
204 #endif
205     NULL
206   };
207
208   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
209     GNUNET_GETOPT_OPTION_END
210   };
211
212   ret = 1;
213
214   return (GNUNET_OK ==
215           GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
216                               "test_core_api_send_to_self",
217                               gettext_noop ("help text"), options, &run,
218                               NULL)) ? ret : 1;
219 }
220
221 /**
222  * The main function to obtain template from gnunetd.
223  *
224  * @param argc number of arguments from the command line
225  * @param argv command line arguments
226  * @return 0 ok, 1 on error
227  */
228 int
229 main (int argc, char *argv[])
230 {
231   GNUNET_log_setup ("test-core-api-send-to-self",
232 #if VERBOSE
233                     "DEBUG",
234 #else
235                     "WARNING",
236 #endif
237                     NULL);
238   ret = check ();
239   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
240   return ret;
241 }
242
243 /* end of test_core_api_send_to_self.c */