aboutsummaryrefslogtreecommitdiff
path: root/src/nat/gnunet-nat-server.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-06-24 15:15:15 +0000
committerChristian Grothoff <christian@grothoff.org>2016-06-24 15:15:15 +0000
commita5b71afba9ee175223fc90e53359adfe6157c33a (patch)
tree5648b65e1a40efddba75693744ff01596a4f06ba /src/nat/gnunet-nat-server.c
parent4c1969ee4afbb673085e1850f4bf6d5c3950344b (diff)
downloadgnunet-a5b71afba9ee175223fc90e53359adfe6157c33a.tar.gz
gnunet-a5b71afba9ee175223fc90e53359adfe6157c33a.zip
migrate nat_test to MQ
Diffstat (limited to 'src/nat/gnunet-nat-server.c')
-rw-r--r--src/nat/gnunet-nat-server.c108
1 files changed, 75 insertions, 33 deletions
diff --git a/src/nat/gnunet-nat-server.c b/src/nat/gnunet-nat-server.c
index db51ef89a..6722deefb 100644
--- a/src/nat/gnunet-nat-server.c
+++ b/src/nat/gnunet-nat-server.c
@@ -47,18 +47,25 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
47 * 47 *
48 * @param dst_ipv4 IPv4 address to send the fake ICMP message 48 * @param dst_ipv4 IPv4 address to send the fake ICMP message
49 * @param dport destination port to include in ICMP message 49 * @param dport destination port to include in ICMP message
50 * @param is_tcp mark for TCP (GNUNET_YES) or UDP (GNUNET_NO) 50 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO)
51 */ 51 */
52static void 52static void
53try_anat (uint32_t dst_ipv4, uint16_t dport, int is_tcp) 53try_anat (uint32_t dst_ipv4,
54 uint16_t dport,
55 int is_tcp)
54{ 56{
55 struct GNUNET_NAT_Handle *h; 57 struct GNUNET_NAT_Handle *h;
56 struct sockaddr_in sa; 58 struct sockaddr_in sa;
57 59
58 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 60 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
59 "Asking for connection reversal with %x and code %u\n", 61 "Asking for connection reversal with %x and code %u\n",
60 (unsigned int) dst_ipv4, (unsigned int) dport); 62 (unsigned int) dst_ipv4,
61 h = GNUNET_NAT_register (cfg, is_tcp, dport, 0, NULL, NULL, NULL, NULL, NULL, NULL); 63 (unsigned int) dport);
64 h = GNUNET_NAT_register (cfg,
65 is_tcp,
66 dport,
67 0,
68 NULL, NULL, NULL, NULL, NULL, NULL);
62 memset (&sa, 0, sizeof (sa)); 69 memset (&sa, 0, sizeof (sa));
63 sa.sin_family = AF_INET; 70 sa.sin_family = AF_INET;
64#if HAVE_SOCKADDR_IN_SIN_LEN 71#if HAVE_SOCKADDR_IN_SIN_LEN
@@ -116,24 +123,29 @@ tcp_send (void *cls)
116 123
117 124
118/** 125/**
119 * Try to send 'data' to the 126 * Try to send @a data to the
120 * IP 'dst_ipv4' at port 'dport' via TCP. 127 * IP @a dst_ipv4' at port @a dport via TCP.
121 * 128 *
122 * @param dst_ipv4 target IP 129 * @param dst_ipv4 target IP
123 * @param dport target port 130 * @param dport target port
124 * @param data data to send 131 * @param data data to send
125 */ 132 */
126static void 133static void
127try_send_tcp (uint32_t dst_ipv4, uint16_t dport, uint16_t data) 134try_send_tcp (uint32_t dst_ipv4,
135 uint16_t dport,
136 uint16_t data)
128{ 137{
129 struct GNUNET_NETWORK_Handle *s; 138 struct GNUNET_NETWORK_Handle *s;
130 struct sockaddr_in sa; 139 struct sockaddr_in sa;
131 struct TcpContext *ctx; 140 struct TcpContext *ctx;
132 141
133 s = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0); 142 s = GNUNET_NETWORK_socket_create (AF_INET,
143 SOCK_STREAM,
144 0);
134 if (NULL == s) 145 if (NULL == s)
135 { 146 {
136 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket"); 147 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
148 "socket");
137 return; 149 return;
138 } 150 }
139 memset (&sa, 0, sizeof (sa)); 151 memset (&sa, 0, sizeof (sa));
@@ -143,41 +155,54 @@ try_send_tcp (uint32_t dst_ipv4, uint16_t dport, uint16_t data)
143#endif 155#endif
144 sa.sin_addr.s_addr = dst_ipv4; 156 sa.sin_addr.s_addr = dst_ipv4;
145 sa.sin_port = htons (dport); 157 sa.sin_port = htons (dport);
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending TCP message to `%s'\n", 158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 GNUNET_a2s ((struct sockaddr *) &sa, sizeof (sa))); 159 "Sending TCP message to `%s'\n",
148 if ((GNUNET_OK != 160 GNUNET_a2s ((struct sockaddr *) &sa,
149 GNUNET_NETWORK_socket_connect (s, (const struct sockaddr *) &sa, 161 sizeof (sa)));
150 sizeof (sa))) && (errno != EINPROGRESS)) 162 if ( (GNUNET_OK !=
163 GNUNET_NETWORK_socket_connect (s,
164 (const struct sockaddr *) &sa,
165 sizeof (sa))) &&
166 (errno != EINPROGRESS) )
151 { 167 {
152 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect"); 168 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
169 "connect");
153 GNUNET_NETWORK_socket_close (s); 170 GNUNET_NETWORK_socket_close (s);
154 return; 171 return;
155 } 172 }
156 ctx = GNUNET_new (struct TcpContext); 173 ctx = GNUNET_new (struct TcpContext);
157 ctx->s = s; 174 ctx->s = s;
158 ctx->data = data; 175 ctx->data = data;
159 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS, s, &tcp_send, ctx); 176 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS,
177 s,
178 &tcp_send,
179 ctx);
160} 180}
161 181
162 182
163/** 183/**
164 * Try to send 'data' to the 184 * Try to send @a data to the
165 * IP 'dst_ipv4' at port 'dport' via UDP. 185 * IP @a dst_ipv4' at port @a dport via UDP.
166 * 186 *
167 * @param dst_ipv4 target IP 187 * @param dst_ipv4 target IP
168 * @param dport target port 188 * @param dport target port
169 * @param data data to send 189 * @param data data to send
170 */ 190 */
171static void 191static void
172try_send_udp (uint32_t dst_ipv4, uint16_t dport, uint16_t data) 192try_send_udp (uint32_t dst_ipv4,
193 uint16_t dport,
194 uint16_t data)
173{ 195{
174 struct GNUNET_NETWORK_Handle *s; 196 struct GNUNET_NETWORK_Handle *s;
175 struct sockaddr_in sa; 197 struct sockaddr_in sa;
176 198
177 s = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0); 199 s = GNUNET_NETWORK_socket_create (AF_INET,
200 SOCK_DGRAM,
201 0);
178 if (NULL == s) 202 if (NULL == s)
179 { 203 {
180 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket"); 204 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
205 "socket");
181 return; 206 return;
182 } 207 }
183 memset (&sa, 0, sizeof (sa)); 208 memset (&sa, 0, sizeof (sa));
@@ -187,12 +212,18 @@ try_send_udp (uint32_t dst_ipv4, uint16_t dport, uint16_t data)
187#endif 212#endif
188 sa.sin_addr.s_addr = dst_ipv4; 213 sa.sin_addr.s_addr = dst_ipv4;
189 sa.sin_port = htons (dport); 214 sa.sin_port = htons (dport);
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending UDP packet to `%s'\n", 215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 GNUNET_a2s ((struct sockaddr *) &sa, sizeof (sa))); 216 "Sending UDP packet to `%s'\n",
217 GNUNET_a2s ((struct sockaddr *) &sa,
218 sizeof (sa)));
192 if (-1 == 219 if (-1 ==
193 GNUNET_NETWORK_socket_sendto (s, &data, sizeof (data), 220 GNUNET_NETWORK_socket_sendto (s,
194 (const struct sockaddr *) &sa, sizeof (sa))) 221 &data,
195 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto"); 222 sizeof (data),
223 (const struct sockaddr *) &sa,
224 sizeof (sa)))
225 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
226 "sendto");
196 GNUNET_NETWORK_socket_close (s); 227 GNUNET_NETWORK_socket_close (s);
197} 228}
198 229
@@ -206,22 +237,31 @@ try_send_udp (uint32_t dst_ipv4, uint16_t dport, uint16_t data)
206 * @param msg message with details about what to test 237 * @param msg message with details about what to test
207 */ 238 */
208static void 239static void
209test (void *cls, struct GNUNET_SERVER_Client *client, 240test (void *cls,
241 struct GNUNET_SERVER_Client *client,
210 const struct GNUNET_MessageHeader *msg) 242 const struct GNUNET_MessageHeader *msg)
211{ 243{
212 const struct GNUNET_NAT_TestMessage *tm; 244 const struct GNUNET_NAT_TestMessage *tm;
213 uint16_t dport; 245 uint16_t dport;
214 246
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received test request\n"); 247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Received test request\n");
216 tm = (const struct GNUNET_NAT_TestMessage *) msg; 249 tm = (const struct GNUNET_NAT_TestMessage *) msg;
217 dport = ntohs (tm->dport); 250 dport = ntohs (tm->dport);
218 if (0 == dport) 251 if (0 == dport)
219 try_anat (tm->dst_ipv4, ntohs (tm->data), (int) ntohl (tm->is_tcp)); 252 try_anat (tm->dst_ipv4,
253 ntohs (tm->data),
254 (int) ntohl (tm->is_tcp));
220 else if (GNUNET_YES == ntohl (tm->is_tcp)) 255 else if (GNUNET_YES == ntohl (tm->is_tcp))
221 try_send_tcp (tm->dst_ipv4, dport, tm->data); 256 try_send_tcp (tm->dst_ipv4,
257 dport,
258 tm->data);
222 else 259 else
223 try_send_udp (tm->dst_ipv4, dport, tm->data); 260 try_send_udp (tm->dst_ipv4,
224 GNUNET_SERVER_receive_done (client, GNUNET_NO); 261 dport,
262 tm->data);
263 GNUNET_SERVER_receive_done (client,
264 GNUNET_NO);
225} 265}
226 266
227 267
@@ -247,7 +287,9 @@ shutdown_task (void *cls)
247 * @param c configuration 287 * @param c configuration
248 */ 288 */
249static void 289static void
250run (void *cls, char *const *args, const char *cfgfile, 290run (void *cls,
291 char *const *args,
292 const char *cfgfile,
251 const struct GNUNET_CONFIGURATION_Handle *c) 293 const struct GNUNET_CONFIGURATION_Handle *c)
252{ 294{
253 static const struct GNUNET_SERVER_MessageHandler handlers[] = { 295 static const struct GNUNET_SERVER_MessageHandler handlers[] = {