diff options
Diffstat (limited to 'src/setup/gnunet-setup-transport-tcp.c')
-rw-r--r-- | src/setup/gnunet-setup-transport-tcp.c | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/src/setup/gnunet-setup-transport-tcp.c b/src/setup/gnunet-setup-transport-tcp.c new file mode 100644 index 00000000..ba592846 --- /dev/null +++ b/src/setup/gnunet-setup-transport-tcp.c | |||
@@ -0,0 +1,182 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2010, 2011 Christian Grothoff (and other contributing authors) | ||
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 2, 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 src/gnunet-setup-transport-tcp.c | ||
23 | * @brief support for TCP configuration | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "gnunet-setup.h" | ||
27 | #include <gnunet/gnunet_resolver_service.h> | ||
28 | #include <gnunet/gnunet_nat_lib.h> | ||
29 | |||
30 | /** | ||
31 | * How long do we wait for the NAT test to report success? | ||
32 | */ | ||
33 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) | ||
34 | |||
35 | /** | ||
36 | * Handle to the active NAT test. | ||
37 | */ | ||
38 | static struct GNUNET_NAT_Test *tst; | ||
39 | |||
40 | /** | ||
41 | * Task identifier for the timeout. | ||
42 | */ | ||
43 | static GNUNET_SCHEDULER_TaskIdentifier tsk; | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Function called by NAT on success. | ||
48 | * Clean up and update GUI (with success). | ||
49 | * | ||
50 | * @param cls closure (unused) | ||
51 | * @param success currently always GNUNET_OK | ||
52 | */ | ||
53 | static void | ||
54 | result_callback (void *cls, | ||
55 | int success) | ||
56 | { | ||
57 | int *ok = cls; | ||
58 | |||
59 | *ok = success; | ||
60 | GNUNET_SCHEDULER_cancel (tsk); | ||
61 | tsk = GNUNET_SCHEDULER_NO_TASK; | ||
62 | GNUNET_NAT_test_stop (tst); | ||
63 | tst = NULL; | ||
64 | } | ||
65 | |||
66 | |||
67 | /** | ||
68 | * Function called if NAT failed to confirm success. | ||
69 | * Clean up and update GUI (with failure). | ||
70 | * | ||
71 | * @param cls closure (unused) | ||
72 | * @param tc scheduler callback | ||
73 | */ | ||
74 | static void | ||
75 | fail_timeout (void *cls, | ||
76 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
77 | { | ||
78 | int *ok = cls; | ||
79 | |||
80 | GNUNET_assert (NULL != tst); | ||
81 | *ok = GNUNET_NO; | ||
82 | tsk = GNUNET_SCHEDULER_NO_TASK; | ||
83 | GNUNET_NAT_test_stop (tst); | ||
84 | tst = NULL; | ||
85 | } | ||
86 | |||
87 | |||
88 | /** | ||
89 | * Main function for the NAT test. | ||
90 | * | ||
91 | * @param cls the 'int*' for the result | ||
92 | * @param tc scheduler context | ||
93 | */ | ||
94 | static void | ||
95 | test (void *cls, | ||
96 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
97 | { | ||
98 | int *ok = cls; | ||
99 | unsigned long long bnd_port; | ||
100 | unsigned long long adv_port; | ||
101 | |||
102 | GNUNET_assert (NULL != cfg); | ||
103 | GNUNET_RESOLVER_connect (cfg); | ||
104 | if (GNUNET_OK != | ||
105 | GNUNET_CONFIGURATION_get_value_number (cfg, | ||
106 | "transport-tcp", | ||
107 | "PORT", | ||
108 | &bnd_port)) | ||
109 | { | ||
110 | GNUNET_break (0); | ||
111 | return; | ||
112 | } | ||
113 | if (GNUNET_OK != | ||
114 | GNUNET_CONFIGURATION_get_value_number (cfg, | ||
115 | "transport-tcp", | ||
116 | "ADVERTISED_PORT", | ||
117 | &adv_port)) | ||
118 | adv_port = bnd_port; | ||
119 | tst = GNUNET_NAT_test_start (cfg, | ||
120 | GNUNET_YES, | ||
121 | (uint16_t) bnd_port, | ||
122 | (uint16_t) adv_port, | ||
123 | &result_callback, | ||
124 | ok); | ||
125 | if (NULL == tst) | ||
126 | { | ||
127 | *ok = GNUNET_SYSERR; | ||
128 | return; | ||
129 | } | ||
130 | tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
131 | &fail_timeout, | ||
132 | ok); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Function called whenever the user wants to test the | ||
137 | * TCP configuration. | ||
138 | */ | ||
139 | void | ||
140 | GNUNET_setup_transport_tcp_test_button_clicked_cb () | ||
141 | { | ||
142 | GtkWidget *w; | ||
143 | int ok; | ||
144 | struct GNUNET_OS_Process *resolver; | ||
145 | |||
146 | GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == tsk); | ||
147 | GNUNET_assert (NULL == tst); | ||
148 | w = GTK_WIDGET (gtk_builder_get_object (builder, | ||
149 | "GNUNET_setup_transport_tcp_test_success_image")); | ||
150 | gtk_widget_hide (w); | ||
151 | resolver = GNUNET_OS_start_process (NULL, NULL, | ||
152 | "gnunet-service-resolver", | ||
153 | "gnunet-service-resolver", NULL); | ||
154 | ok = GNUNET_NO; | ||
155 | GNUNET_SCHEDULER_run (&test, &ok); | ||
156 | if (NULL != resolver) | ||
157 | { | ||
158 | GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM)); | ||
159 | GNUNET_OS_process_close (resolver); | ||
160 | } | ||
161 | if (GNUNET_YES != ok) | ||
162 | { | ||
163 | w = GTK_WIDGET (gtk_builder_get_object (builder, | ||
164 | "GNUNET_setup_transport_tcp_test_fail_image")); | ||
165 | gtk_widget_show (w); | ||
166 | w = GTK_WIDGET (gtk_builder_get_object (builder, | ||
167 | "GNUNET_setup_transport_tcp_test_success_image")); | ||
168 | gtk_widget_hide (w); | ||
169 | } | ||
170 | else | ||
171 | { | ||
172 | w = GTK_WIDGET (gtk_builder_get_object (builder, | ||
173 | "GNUNET_setup_transport_tcp_test_fail_image")); | ||
174 | gtk_widget_hide (w); | ||
175 | w = GTK_WIDGET (gtk_builder_get_object (builder, | ||
176 | "GNUNET_setup_transport_tcp_test_success_image")); | ||
177 | gtk_widget_show (w); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | |||
182 | /* end of gnunet-setup-transport-tcp.c */ | ||