diff options
Diffstat (limited to 'src/transport/transport_api_cmd_send_simple_v2.c')
-rw-r--r-- | src/transport/transport_api_cmd_send_simple_v2.c | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/transport/transport_api_cmd_send_simple_v2.c b/src/transport/transport_api_cmd_send_simple_v2.c new file mode 100644 index 000000000..d43c0b425 --- /dev/null +++ b/src/transport/transport_api_cmd_send_simple_v2.c | |||
@@ -0,0 +1,156 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2021 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file testing_api_cmd_start_peer.c | ||
23 | * @brief cmd to start a peer. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | #include "transport-testing2.h" | ||
30 | #include "transport-testing-cmds.h" | ||
31 | |||
32 | /** | ||
33 | * Struct to hold information for callbacks. | ||
34 | * | ||
35 | */ | ||
36 | struct SendSimpleState | ||
37 | { | ||
38 | /** | ||
39 | * Number globally identifying the node. | ||
40 | * | ||
41 | */ | ||
42 | uint32_t num; | ||
43 | |||
44 | /** | ||
45 | * Label of the cmd to start a peer. | ||
46 | * | ||
47 | */ | ||
48 | const char *start_peer_label; | ||
49 | }; | ||
50 | |||
51 | |||
52 | /** | ||
53 | * Trait function of this cmd does nothing. | ||
54 | * | ||
55 | */ | ||
56 | static int | ||
57 | send_simple_traits (void *cls, | ||
58 | const void **ret, | ||
59 | const char *trait, | ||
60 | unsigned int index) | ||
61 | { | ||
62 | return GNUNET_OK; | ||
63 | } | ||
64 | |||
65 | |||
66 | /** | ||
67 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
68 | * | ||
69 | */ | ||
70 | static void | ||
71 | send_simple_cleanup (void *cls, | ||
72 | const struct GNUNET_TESTING_Command *cmd) | ||
73 | { | ||
74 | struct SendSimpleState *sss = cls; | ||
75 | |||
76 | GNUNET_free (sss); | ||
77 | } | ||
78 | |||
79 | |||
80 | /** | ||
81 | * The run method of this cmd will send a simple message to the connected peer. | ||
82 | * | ||
83 | */ | ||
84 | static void | ||
85 | send_simple_run (void *cls, | ||
86 | const struct GNUNET_TESTING_Command *cmd, | ||
87 | struct GNUNET_TESTING_Interpreter *is) | ||
88 | { | ||
89 | struct SendSimpleState *sss = cls; | ||
90 | struct GNUNET_MQ_Envelope *env; | ||
91 | struct GNUNET_TRANSPORT_TESTING_TestMessage *test; | ||
92 | struct GNUNET_MQ_Handle *mq; | ||
93 | struct GNUNET_CONTAINER_MultiShortmap *connected_peers_map; | ||
94 | const struct GNUNET_TESTING_Command *peer1_cmd; | ||
95 | struct GNUNET_ShortHashCode *key = GNUNET_new (struct GNUNET_ShortHashCode); | ||
96 | struct GNUNET_HashCode hc; | ||
97 | int node_number; | ||
98 | |||
99 | peer1_cmd = GNUNET_TESTING_interpreter_lookup_command (sss->start_peer_label); | ||
100 | GNUNET_TRANSPORT_get_trait_connected_peers_map (peer1_cmd, | ||
101 | &connected_peers_map); | ||
102 | |||
103 | node_number = 1; | ||
104 | GNUNET_CRYPTO_hash (&node_number, sizeof(node_number), &hc); | ||
105 | memcpy (key, | ||
106 | &hc, | ||
107 | sizeof (*key)); | ||
108 | |||
109 | mq = GNUNET_CONTAINER_multishortmap_get (connected_peers_map, | ||
110 | key); | ||
111 | |||
112 | env = GNUNET_MQ_msg_extra (test, | ||
113 | 2600 - sizeof(*test), | ||
114 | GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE); | ||
115 | test->num = htonl (sss->num); | ||
116 | memset (&test[1], | ||
117 | sss->num, | ||
118 | 2600 - sizeof(*test)); | ||
119 | GNUNET_MQ_send (mq, | ||
120 | env); | ||
121 | GNUNET_free (key); | ||
122 | |||
123 | } | ||
124 | |||
125 | |||
126 | /** | ||
127 | * Create command. | ||
128 | * | ||
129 | * @param label name for command. | ||
130 | * @param m The number of the local node of the actual network namespace. | ||
131 | * @param n The number of the actual namespace. | ||
132 | * @param num Number globally identifying the node. | ||
133 | * @param start_peer_label Label of the cmd to start a peer. | ||
134 | * @return command. | ||
135 | */ | ||
136 | struct GNUNET_TESTING_Command | ||
137 | GNUNET_TRANSPORT_cmd_send_simple_v2 (const char *label, | ||
138 | const char *start_peer_label, | ||
139 | uint32_t num) | ||
140 | { | ||
141 | struct SendSimpleState *sss; | ||
142 | |||
143 | sss = GNUNET_new (struct SendSimpleState); | ||
144 | sss->num = num; | ||
145 | sss->start_peer_label = start_peer_label; | ||
146 | |||
147 | struct GNUNET_TESTING_Command cmd = { | ||
148 | .cls = sss, | ||
149 | .label = label, | ||
150 | .run = &send_simple_run, | ||
151 | .cleanup = &send_simple_cleanup, | ||
152 | .traits = &send_simple_traits | ||
153 | }; | ||
154 | |||
155 | return cmd; | ||
156 | } | ||