bf2697eecb100d139fd72c1f72dd5a5266ec4d80
[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  * The handle to core
40  */
41 static struct GNUNET_CORE_Handle *core_handle;
42
43 /**
44  * Function scheduled as very last function, cleans up after us
45  */
46 static void
47 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tskctx)
48 {
49   GNUNET_assert (0 != (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
50
51   if (core_handle != NULL)
52     {
53       GNUNET_CORE_disconnect (core_handle);
54       core_handle = NULL;
55     }
56 }
57
58 static struct GNUNET_PeerIdentity myself;
59
60 struct GNUNET_CORE_Handle *core;
61
62 static int
63 receive(void* cls, const struct GNUNET_PeerIdentity* other, const struct GNUNET_MessageHeader* message, const struct GNUNET_TRANSPORT_ATS_Information* atsi)
64 {
65   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message from peer %s\n", GNUNET_i2s(other));
66   return GNUNET_OK;
67 }
68
69 static size_t
70 send_message (void* cls, size_t size, void* buf)
71 {
72   if (size == 0 || buf == NULL)
73     {
74       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Could not send; got 0 buffer\n");
75       return 0;
76     }
77   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending!\n");
78   struct GNUNET_MessageHeader *hdr = buf;
79   hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
80   hdr->type = htons(GNUNET_MESSAGE_TYPE_SERVICE_UDP);
81   return ntohs(hdr->size);
82 }
83
84 static void
85 init (void *cls, struct GNUNET_CORE_Handle *core,
86       const struct GNUNET_PeerIdentity *my_identity,
87       const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pk)
88 {
89   if (core == NULL)
90     {
91       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could NOT connect to CORE;\n");
92       return;
93     }
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95               "Correctly connected to CORE; we are the peer %s.\n",
96               GNUNET_i2s (my_identity));
97   memcpy (&myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
98 }
99
100 static void
101 connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
102             const struct GNUNET_TRANSPORT_ATS_Information *atsi)
103 {
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s.\n",
105               GNUNET_i2s (peer));
106   if (0 == memcmp (peer, &myself, sizeof (struct GNUNET_PeerIdentity)))
107     {
108       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109                   "Connected to myself; sending message!\n");
110       GNUNET_CORE_notify_transmit_ready (core,
111                                          0, GNUNET_TIME_UNIT_FOREVER_REL,
112                                          peer,
113                                          sizeof (struct GNUNET_MessageHeader),
114                                          send_message, NULL);
115     }
116 }
117
118
119 /**
120  * Main function that will be run by the scheduler.
121  *
122  * @param cls closure
123  * @param args remaining command-line arguments
124  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
125  * @param cfg configuration
126  */
127 static void
128 run (void *cls,
129      char *const *args,
130      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg_)
131 {
132   const static struct GNUNET_CORE_MessageHandler handlers[] = {
133     {receive, GNUNET_MESSAGE_TYPE_SERVICE_UDP, 0},
134     {NULL, 0, 0}
135   };
136   core = GNUNET_CORE_connect (cfg_,
137                               42,
138                               NULL,
139                               init,
140                               connect_cb,
141                               NULL, NULL, NULL, 0, NULL, 0, handlers);
142   GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
143 }
144
145 /**
146  * The main function to obtain template from gnunetd.
147  *
148  * @param argc number of arguments from the command line
149  * @param argv command line arguments
150  * @return 0 ok, 1 on error
151  */
152 int
153 main (int argc, char *const *argv)
154 {
155   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
156     GNUNET_GETOPT_OPTION_END
157   };
158
159   return (GNUNET_OK ==
160           GNUNET_PROGRAM_run (argc,
161                               argv,
162                               "test_core_api_send_to_self",
163                               gettext_noop ("help text"),
164                               options, &run, NULL)) ? ret : 1;
165 }
166
167 /* end of test_core_api_send_to_self.c */