2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 #include <gnunet_util_lib.h>
23 #include <microhttpd.h>
24 #include "gns_proxy_proto.h"
27 #define GNUNET_GNS_PROXY_PORT 7777
29 //TODO maybe make this an api call
31 * Checks if name is in tld
33 * @param name the name to check
34 * @param tld the TLD to check for
35 * @return GNUNET_YES or GNUNET_NO
38 is_tld(const char* name, const char* tld)
42 if (strlen(name) <= strlen(tld))
47 offset = strlen(name)-strlen(tld);
48 if (strcmp (name+offset, tld) != 0)
50 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
51 "%s is not in .%s TLD\n", name, tld);
60 struct GNUNET_NETWORK_Handle *sock;
61 struct GNUNET_NETWORK_Handle *remote_sock;
65 GNUNET_SCHEDULER_TaskIdentifier rtask;
66 GNUNET_SCHEDULER_TaskIdentifier fwdrtask;
67 GNUNET_SCHEDULER_TaskIdentifier wtask;
68 GNUNET_SCHEDULER_TaskIdentifier fwdwtask;
72 unsigned int rbuf_len;
73 unsigned int wbuf_len;
76 unsigned long port = GNUNET_GNS_PROXY_PORT;
77 static struct GNUNET_NETWORK_Handle *lsock;
78 GNUNET_SCHEDULER_TaskIdentifier ltask;
79 static struct MHD_Daemon *httpd;
80 static GNUNET_SCHEDULER_TaskIdentifier httpd_task;
83 * Main MHD callback for handling requests.
86 * @param connection MHD connection handle
87 * @param method the HTTP method used ("GET", "PUT", etc.)
88 * @param version the HTTP version string (i.e. "HTTP/1.1")
89 * @param upload_data the data being uploaded (excluding HEADERS,
90 * for a POST that fits into memory and that is encoded
91 * with a supported encoding, the POST data will NOT be
92 * given in upload_data and is instead available as
93 * part of MHD_get_connection_values; very large POST
94 * data *will* be made available incrementally in
96 * @param upload_data_size set initially to the size of the
97 * upload_data provided; the method must update this
98 * value to the number of bytes NOT processed;
99 * @param ptr pointer to location where we store the 'struct Request'
100 * @return MHD_YES if the connection was handled successfully,
101 * MHD_NO if the socket must be closed due to a serious
102 * error while handling the request
105 create_response (void *cls,
106 struct MHD_Connection *con,
110 const char *upload_data,
111 size_t *upload_data_size,
115 const char* page = "<html><head><title>gnoxy</title>"\
116 "</head><body>gnoxy demo</body></html>";
117 struct MHD_Response *response;
120 if (0 != strcmp (meth, "GET"))
122 if (&dummy != *con_cls)
128 if (0 != *upload_data_size)
133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 response = MHD_create_response_from_buffer (strlen (page),
138 MHD_RESPMEM_PERSISTENT);
139 ret = MHD_queue_response (con,
142 MHD_destroy_response (response);
147 * Task run whenever HTTP server operations are pending.
150 * @param tc sched context
154 const struct GNUNET_SCHEDULER_TaskContext *tc);
165 struct GNUNET_NETWORK_FDSet *wrs;
166 struct GNUNET_NETWORK_FDSet *wws;
167 struct GNUNET_NETWORK_FDSet *wes;
170 unsigned MHD_LONG_LONG timeout;
171 struct GNUNET_TIME_Relative tv;
176 wrs = GNUNET_NETWORK_fdset_create ();
177 wes = GNUNET_NETWORK_fdset_create ();
178 wws = GNUNET_NETWORK_fdset_create ();
180 GNUNET_assert (MHD_YES == MHD_get_fdset (httpd, &rs, &ws, &es, &max));
181 haveto = MHD_get_timeout (httpd, &timeout);
183 if (haveto == MHD_YES)
184 tv.rel_value = (uint64_t) timeout;
186 tv = GNUNET_TIME_UNIT_FOREVER_REL;
187 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
188 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
189 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
191 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
194 GNUNET_NETWORK_fdset_destroy (wrs);
195 GNUNET_NETWORK_fdset_destroy (wws);
196 GNUNET_NETWORK_fdset_destroy (wes);
200 * Task run whenever HTTP server operations are pending.
203 * @param tc sched context
207 const struct GNUNET_SCHEDULER_TaskContext *tc)
209 httpd_task = GNUNET_SCHEDULER_NO_TASK;
215 * Read data from socket
217 * @param cls the closure
218 * @param tc scheduler context
221 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
224 * Read from remote end
227 * @param tc scheduler context
230 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
233 * Write data to remote socket
235 * @param cls the closure
236 * @param tc scheduler context
239 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
241 struct Socks5Request *s5r = cls;
244 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
246 if ((NULL != tc->read_ready) &&
247 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
248 (len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Successfully sent %d bytes to remote socket\n",
257 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
259 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
260 GNUNET_SCHEDULER_cancel (s5r->rtask);
261 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
262 GNUNET_SCHEDULER_cancel (s5r->wtask);
263 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
264 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
265 GNUNET_NETWORK_socket_close (s5r->remote_sock);
266 GNUNET_NETWORK_socket_close (s5r->sock);
272 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
279 * Write data to socket
281 * @param cls the closure
282 * @param tc scheduler context
285 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
287 struct Socks5Request *s5r = cls;
290 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
292 if ((NULL != tc->read_ready) &&
293 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
294 (len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Successfully sent %d bytes to socket\n",
304 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
306 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
307 GNUNET_SCHEDULER_cancel (s5r->rtask);
308 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
309 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
310 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
311 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
312 GNUNET_NETWORK_socket_close (s5r->remote_sock);
313 GNUNET_NETWORK_socket_close (s5r->sock);
318 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
319 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
321 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
323 &do_read_remote, s5r);
327 * Read from remote end
330 * @param tc scheduler context
333 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
335 struct Socks5Request *s5r = cls;
337 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
340 if ((NULL != tc->write_ready) &&
341 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
342 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
343 sizeof (s5r->wbuf))))
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Successfully read %d bytes from remote socket\n",
351 if (s5r->wbuf_len == 0)
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "0 bytes received from remote... graceful shutdown!\n");
354 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
355 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
356 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
357 GNUNET_SCHEDULER_cancel (s5r->rtask);
359 GNUNET_NETWORK_socket_close (s5r->remote_sock);
360 s5r->remote_sock = NULL;
361 GNUNET_NETWORK_socket_close (s5r->sock);
367 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
376 * Read data from incoming connection
378 * @param cls the closure
379 * @param tc the scheduler context
382 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
384 struct Socks5Request *s5r = cls;
385 struct socks5_client_hello *c_hello;
386 struct socks5_server_hello *s_hello;
387 struct socks5_client_request *c_req;
388 struct socks5_server_response *s_resp;
393 struct hostent *phost;
395 struct sockaddr_in remote_addr;
396 struct in_addr *r_sin_addr;
398 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
400 if ((NULL != tc->write_ready) &&
401 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
402 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
403 sizeof (s5r->rbuf))))
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Successfully read %d bytes from socket\n",
411 if (s5r->rbuf_len != 0)
412 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
416 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
417 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
418 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
419 GNUNET_SCHEDULER_cancel (s5r->wtask);
420 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
421 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
422 GNUNET_NETWORK_socket_close (s5r->remote_sock);
423 GNUNET_NETWORK_socket_close (s5r->sock);
428 if (s5r->state == SOCKS5_INIT)
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
434 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
436 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
437 s5r->wbuf_len = sizeof( struct socks5_server_hello );
439 s_hello->version = c_hello->version;
440 s_hello->auth_method = SOCKS_AUTH_NONE;
442 /* Write response to client */
443 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
447 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
451 s5r->state = SOCKS5_REQUEST;
455 if (s5r->state == SOCKS5_REQUEST)
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Processing SOCKS5 request\n");
459 c_req = (struct socks5_client_request*)&s5r->rbuf;
460 s_resp = (struct socks5_server_response*)&s5r->wbuf;
461 //Only 10byte for ipv4 response!
462 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
464 GNUNET_assert (c_req->addr_type == 3);
466 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
467 memset(domain, 0, sizeof(domain));
468 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
469 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Requested connection is %s:%d\n",
476 if (is_tld (domain, GNUNET_GNS_TLD) ||
477 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Requested connection is gnunet tld\n",
489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
490 _("Failed to start HTTP server\n"));
491 s_resp->version = 0x05;
492 s_resp->reply = 0x01;
494 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
498 //TODO! close socket after the write! schedule task
499 //GNUNET_NETWORK_socket_close (s5r->sock);
507 phost = (struct hostent*)gethostbyname (domain);
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Resolve %s error!\n", domain );
512 s_resp->version = 0x05;
513 s_resp->reply = 0x01;
515 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
519 //TODO! close socket after the write! schedule task
520 //GNUNET_NETWORK_socket_close (s5r->sock);
525 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
528 r_sin_addr = (struct in_addr*)(phost->h_addr);
529 remote_ip = r_sin_addr->s_addr;
530 memset(&remote_addr, 0, sizeof(remote_addr));
531 remote_addr.sin_family = AF_INET;
532 #if HAVE_SOCKADDR_IN_SIN_LEN
533 remote_addr.sin_len = sizeof (remote_addr);
535 remote_addr.sin_addr.s_addr = remote_ip;
536 remote_addr.sin_port = req_port;
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
543 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
544 (const struct sockaddr*)&remote_addr,
545 sizeof (remote_addr)))
546 && (errno != EINPROGRESS))
548 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "socket request error...\n");
551 s_resp->version = 0x05;
552 s_resp->reply = 0x01;
554 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "new remote connection\n");
564 s_resp->version = 0x05;
565 s_resp->reply = 0x00;
566 s_resp->reserved = 0x00;
567 s_resp->addr_type = 0x01;
569 s5r->state = SOCKS5_DATA_TRANSFER;
572 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
576 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
584 if (s5r->state == SOCKS5_DATA_TRANSFER)
586 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Closing connection to client\n");
590 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
591 GNUNET_SCHEDULER_cancel (s5r->rtask);
592 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
593 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
594 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
595 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
596 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
597 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
599 if (s5r->remote_sock != NULL)
600 GNUNET_NETWORK_socket_close (s5r->remote_sock);
601 GNUNET_NETWORK_socket_close (s5r->sock);
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "forwarding %d bytes from client\n", s5r->rbuf_len);
610 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
612 &do_write_remote, s5r);
614 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
617 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
619 &do_read_remote, s5r);
625 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
630 * Accept new incoming connections
632 * @param cls the closure
633 * @param tc the scheduler context
636 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
638 struct GNUNET_NETWORK_Handle *s;
639 struct Socks5Request *s5r;
641 ltask = GNUNET_SCHEDULER_NO_TASK;
642 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
645 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
649 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
653 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Got an inbound connection, waiting for data\n");
660 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
662 s5r->state = SOCKS5_INIT;
663 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
664 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
665 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
666 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
669 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
673 * Main function that will be run
676 * @param args remaining command-line arguments
677 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
678 * @param cfg configuration
681 run (void *cls, char *const *args, const char *cfgfile,
682 const struct GNUNET_CONFIGURATION_Handle *cfg)
684 struct sockaddr_in sa;
686 memset (&sa, 0, sizeof (sa));
687 sa.sin_family = AF_INET;
688 sa.sin_port = htons (port);
689 #if HAVE_SOCKADDR_IN_SIN_LEN
690 sa.sin_len = sizeof (sa);
693 lsock = GNUNET_NETWORK_socket_create (AF_INET,
697 if ((NULL == lsock) ||
699 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
703 "Failed to create listen socket bound to `%s'",
704 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
706 GNUNET_NETWORK_socket_close (lsock);
710 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
712 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
713 "Failed to listen on socket bound to `%s'",
714 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
718 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
719 lsock, &do_accept, NULL);
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Proxy listens on port %u\n",
725 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444,
727 &create_response, NULL,
728 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
729 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
730 MHD_OPTION_NOTIFY_COMPLETED,
738 * The main function for gnunet-gns-proxy.
740 * @param argc number of arguments from the command line
741 * @param argv command line arguments
742 * @return 0 ok, 1 on error
745 main (int argc, char *const *argv)
747 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
749 gettext_noop ("listen on specified port"), 1,
750 &GNUNET_GETOPT_set_string, &port},
751 GNUNET_GETOPT_OPTION_END
756 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
759 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
760 _("GNUnet GNS proxy"),
762 &run, NULL)) ? 0 : 1;