48dd0ee73db746d1a80bb0ef49e197661cb705f6
[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  * The handle to core
47  */
48 struct GNUNET_CORE_Handle *core;
49
50 /**
51  * Handle to gnunet-service-arm.
52  */
53 struct GNUNET_OS_Process *arm_proc;
54
55 /**
56  * Function scheduled as very last function, cleans up after us
57  */
58 static void
59 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tskctx)
60 {
61   die_task = GNUNET_SCHEDULER_NO_TASK;
62
63   if (core != NULL)
64     {
65       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting core.\n");
66       GNUNET_CORE_disconnect (core);
67       core = NULL;
68     }
69
70   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
71               "Stopping peer\n");
72   if (0 != GNUNET_OS_process_kill (arm_proc, SIGTERM))
73     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
74
75   if (GNUNET_OS_process_wait(arm_proc) != GNUNET_OK)
76     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
77
78   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
79               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (arm_proc));
80   GNUNET_OS_process_close (arm_proc);
81   arm_proc = NULL;
82
83   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
84 }
85
86 static int
87 receive(void* cls, const struct GNUNET_PeerIdentity* other, const struct GNUNET_MessageHeader* message, const struct GNUNET_TRANSPORT_ATS_Information* atsi)
88 {
89   if (die_task != GNUNET_SCHEDULER_NO_TASK)
90     GNUNET_SCHEDULER_cancel(die_task);
91   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message from peer %s\n", GNUNET_i2s(other));
92   GNUNET_SCHEDULER_add_now(&cleanup, NULL);
93   ret = 0;
94   return GNUNET_OK;
95 }
96
97 static size_t
98 send_message (void* cls, size_t size, void* buf)
99 {
100   if (size == 0 || buf == NULL)
101     {
102       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Could not send; got 0 buffer\n");
103       return 0;
104     }
105   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending!\n");
106   struct GNUNET_MessageHeader *hdr = buf;
107   hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
108   hdr->type = htons(GNUNET_MESSAGE_TYPE_SERVICE_UDP);
109   return ntohs(hdr->size);
110 }
111
112 static void
113 init (void *cls, struct GNUNET_CORE_Handle *core,
114       const struct GNUNET_PeerIdentity *my_identity,
115       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pk)
116 {
117   if (core == NULL)
118     {
119       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could NOT connect to CORE;\n");
120       return;
121     }
122   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123               "Correctly connected to CORE; we are the peer %s.\n",
124               GNUNET_i2s (my_identity));
125   memcpy (&myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
126 }
127
128 static void
129 connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
130             const struct GNUNET_TRANSPORT_ATS_Information *atsi)
131 {
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n",
133               GNUNET_i2s (peer));
134   if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity)))
135     {
136       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137                   "Connected to myself; sending message!\n");
138       GNUNET_CORE_notify_transmit_ready (core,
139                                          0, GNUNET_TIME_UNIT_FOREVER_REL,
140                                          peer,
141                                          sizeof (struct GNUNET_MessageHeader),
142                                          send_message, NULL);
143     }
144 }
145
146
147 /**
148  * Main function that will be run by the scheduler.
149  *
150  * @param cls closure
151  * @param args remaining command-line arguments
152  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
153  * @param cfg configuration
154  */
155 static void
156 run (void *cls,
157      char *const *args,
158      const char *cfgfile,
159      const struct GNUNET_CONFIGURATION_Handle *cfg)
160 {
161   const static struct GNUNET_CORE_MessageHandler handlers[] = {
162     {&receive, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
163     {NULL, 0, 0}
164   };
165
166   arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
167                                         "gnunet-service-arm",
168 #if VERBOSE
169                                         "-L", "DEBUG",
170 #endif
171                                         "-c", "test_core_api_data.conf", NULL);
172
173   core = GNUNET_CORE_connect (cfg,
174                               42,
175                               NULL,
176                               &init,
177                               &connect_cb,
178                               NULL, NULL, NULL, 0, NULL, 0, handlers);
179
180   die_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &cleanup, cls);
181 }
182
183 /**
184  * The main function to obtain template from gnunetd.
185  *
186  * @param argc number of arguments from the command line
187  * @param argv command line arguments
188  * @return 0 ok, 1 on error
189  */
190 int
191 main (int argc, char *const *argv)
192 {
193   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
194     GNUNET_GETOPT_OPTION_END
195   };
196
197   return (GNUNET_OK ==
198           GNUNET_PROGRAM_run (argc,
199                               argv,
200                               "test_core_api_send_to_self",
201                               gettext_noop ("help text"),
202                               options, &run, NULL)) ? ret : 1;
203 }
204
205 /* end of test_core_api_send_to_self.c */