aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport_api_cmd_stop_peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api_cmd_stop_peer.c')
-rw-r--r--src/transport/transport_api_cmd_stop_peer.c163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/transport/transport_api_cmd_stop_peer.c b/src/transport/transport_api_cmd_stop_peer.c
new file mode 100644
index 000000000..2277520ec
--- /dev/null
+++ b/src/transport/transport_api_cmd_stop_peer.c
@@ -0,0 +1,163 @@
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_stop_peer.c
23 * @brief cmd to stop 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 "gnunet_peerstore_service.h"
30#include "gnunet_transport_core_service.h"
31#include "gnunet_transport_application_service.h"
32#include "transport-testing-ng.h"
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
38
39
40/**
41 * Struct to hold information for callbacks.
42 *
43 */
44struct StopPeerState
45{
46 // Label of the cmd to start the peer.
47 const char *start_label;
48};
49
50
51/**
52 * The run method of this cmd will stop all services of a peer which were used to test the transport service.
53 *
54 */
55static void
56stop_peer_run (void *cls,
57 const struct GNUNET_TESTING_Command *cmd,
58 struct GNUNET_TESTING_Interpreter *is)
59{
60 struct StopPeerState *stop_ps = cls;
61 struct StartPeerState *sps;
62 const struct GNUNET_TESTING_Command *start_cmd;
63
64 start_cmd = GNUNET_TESTING_interpreter_lookup_command (stop_ps->start_label);
65 GNUNET_TRANSPORT_get_trait_state (start_cmd,
66 &sps);
67
68 if (NULL != sps->pic)
69 {
70 GNUNET_PEERSTORE_iterate_cancel (sps->pic);
71 sps->pic = NULL;
72 }
73 if (NULL != sps->th)
74 {
75 GNUNET_TRANSPORT_core_disconnect (sps->th);
76 sps->th = NULL;
77 }
78 if (NULL != sps->ah)
79 {
80 GNUNET_TRANSPORT_application_done (sps->ah);
81 sps->ah = NULL;
82 }
83 if (NULL != sps->ph)
84 {
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 "Disconnecting from PEERSTORE service\n");
87 GNUNET_PEERSTORE_disconnect (sps->ph, GNUNET_NO);
88 sps->ph = NULL;
89 }
90 if (NULL != sps->peer)
91 {
92 if (GNUNET_OK !=
93 GNUNET_TESTING_peer_stop (sps->peer))
94 {
95 LOG (GNUNET_ERROR_TYPE_DEBUG,
96 "Testing lib failed to stop peer %u (`%s')\n",
97 sps->no,
98 GNUNET_i2s (&sps->id));
99 }
100 GNUNET_TESTING_peer_destroy (sps->peer);
101 sps->peer = NULL;
102 }
103 if (NULL != sps->rh_task)
104 GNUNET_SCHEDULER_cancel (sps->rh_task);
105 sps->rh_task = NULL;
106
107}
108
109
110/**
111 * The cleanup function of this cmd frees resources the cmd allocated.
112 *
113 */
114static void
115stop_peer_cleanup (void *cls,
116 const struct GNUNET_TESTING_Command *cmd)
117{
118 struct StopPeerState *sps = cls;
119
120 GNUNET_free (sps);
121}
122
123
124/**
125 * Trait function of this cmd does nothing.
126 *
127 */
128static int
129stop_peer_traits (void *cls,
130 const void **ret,
131 const char *trait,
132 unsigned int index)
133{
134 return GNUNET_OK;
135}
136
137
138/**
139 * Create command.
140 *
141 * @param label name for command.
142 * @param start_label Label of the cmd to start the peer.
143 * @return command.
144 */
145struct GNUNET_TESTING_Command
146GNUNET_TRANSPORT_cmd_stop_peer (const char *label,
147 const char *start_label)
148{
149 struct StopPeerState *sps;
150
151 sps = GNUNET_new (struct StopPeerState);
152 sps->start_label = start_label;
153
154 struct GNUNET_TESTING_Command cmd = {
155 .cls = sps,
156 .label = label,
157 .run = &stop_peer_run,
158 .cleanup = &stop_peer_cleanup,
159 .traits = &stop_peer_traits
160 };
161
162 return cmd;
163}