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 if (httpd_task != GNUNET_SCHEDULER_NO_TASK)
192 GNUNET_SCHEDULER_cancel (httpd_task);
194 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
197 GNUNET_NETWORK_fdset_destroy (wrs);
198 GNUNET_NETWORK_fdset_destroy (wws);
199 GNUNET_NETWORK_fdset_destroy (wes);
203 * Task run whenever HTTP server operations are pending.
206 * @param tc sched context
210 const struct GNUNET_SCHEDULER_TaskContext *tc)
212 httpd_task = GNUNET_SCHEDULER_NO_TASK;
218 * Read data from socket
220 * @param cls the closure
221 * @param tc scheduler context
224 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
227 * Read from remote end
230 * @param tc scheduler context
233 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
236 * Write data to remote socket
238 * @param cls the closure
239 * @param tc scheduler context
242 do_write_remote (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 struct Socks5Request *s5r = cls;
247 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
249 if ((NULL != tc->read_ready) &&
250 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->remote_sock)) &&
251 (len = GNUNET_NETWORK_socket_send (s5r->remote_sock, s5r->rbuf,
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Successfully sent %d bytes to remote socket\n",
260 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write remote");
262 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
263 GNUNET_SCHEDULER_cancel (s5r->rtask);
264 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
265 GNUNET_SCHEDULER_cancel (s5r->wtask);
266 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
267 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
268 GNUNET_NETWORK_socket_close (s5r->remote_sock);
269 GNUNET_NETWORK_socket_close (s5r->sock);
275 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
282 * Write data to socket
284 * @param cls the closure
285 * @param tc scheduler context
288 do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct Socks5Request *s5r = cls;
293 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
295 if ((NULL != tc->read_ready) &&
296 (GNUNET_NETWORK_fdset_isset (tc->write_ready, s5r->sock)) &&
297 (len = GNUNET_NETWORK_socket_send (s5r->sock, s5r->wbuf,
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Successfully sent %d bytes to socket\n",
307 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write");
309 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
310 GNUNET_SCHEDULER_cancel (s5r->rtask);
311 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
312 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
313 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
314 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
315 GNUNET_NETWORK_socket_close (s5r->remote_sock);
316 GNUNET_NETWORK_socket_close (s5r->sock);
321 if ((s5r->state == SOCKS5_DATA_TRANSFER) &&
322 (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK))
324 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
326 &do_read_remote, s5r);
330 * Read from remote end
333 * @param tc scheduler context
336 do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
338 struct Socks5Request *s5r = cls;
340 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
343 if ((NULL != tc->write_ready) &&
344 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->remote_sock)) &&
345 (s5r->wbuf_len = GNUNET_NETWORK_socket_recv (s5r->remote_sock, s5r->wbuf,
346 sizeof (s5r->wbuf))))
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Successfully read %d bytes from remote socket\n",
354 if (s5r->wbuf_len == 0)
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "0 bytes received from remote... graceful shutdown!\n");
357 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
358 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
359 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
360 GNUNET_SCHEDULER_cancel (s5r->rtask);
362 GNUNET_NETWORK_socket_close (s5r->remote_sock);
363 s5r->remote_sock = NULL;
364 GNUNET_NETWORK_socket_close (s5r->sock);
370 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
378 add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h)
381 struct sockaddr *addr;
384 fd = GNUNET_NETWORK_get_fd (h);
385 addr = GNUNET_NETWORK_get_addr (h);
386 len = GNUNET_NETWORK_get_addrlen (h);
388 return MHD_add_connection (httpd, fd, addr, len);
392 * Read data from incoming connection
394 * @param cls the closure
395 * @param tc the scheduler context
398 do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
400 struct Socks5Request *s5r = cls;
401 struct socks5_client_hello *c_hello;
402 struct socks5_server_hello *s_hello;
403 struct socks5_client_request *c_req;
404 struct socks5_server_response *s_resp;
409 struct hostent *phost;
411 struct sockaddr_in remote_addr;
412 struct in_addr *r_sin_addr;
414 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
416 if ((NULL != tc->write_ready) &&
417 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) &&
418 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
419 sizeof (s5r->rbuf))))
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Successfully read %d bytes from socket\n",
427 if (s5r->rbuf_len != 0)
428 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read");
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
432 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
433 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
434 if (s5r->wtask != GNUNET_SCHEDULER_NO_TASK)
435 GNUNET_SCHEDULER_cancel (s5r->wtask);
436 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
437 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
438 GNUNET_NETWORK_socket_close (s5r->remote_sock);
439 GNUNET_NETWORK_socket_close (s5r->sock);
444 if (s5r->state == SOCKS5_INIT)
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 c_hello = (struct socks5_client_hello*)&s5r->rbuf;
450 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
452 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
453 s5r->wbuf_len = sizeof( struct socks5_server_hello );
455 s_hello->version = c_hello->version;
456 s_hello->auth_method = SOCKS_AUTH_NONE;
458 /* Write response to client */
459 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
463 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
467 s5r->state = SOCKS5_REQUEST;
471 if (s5r->state == SOCKS5_REQUEST)
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Processing SOCKS5 request\n");
475 c_req = (struct socks5_client_request*)&s5r->rbuf;
476 s_resp = (struct socks5_server_response*)&s5r->wbuf;
477 //Only 10byte for ipv4 response!
478 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
480 GNUNET_assert (c_req->addr_type == 3);
482 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
483 memset(domain, 0, sizeof(domain));
484 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
485 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "Requested connection is %s:%d\n",
492 if (is_tld (domain, GNUNET_GNS_TLD) ||
493 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Requested connection is gnunet tld\n",
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 _("Failed to start HTTP server\n"));
503 s_resp->version = 0x05;
504 s_resp->reply = 0x01;
506 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
510 //TODO! close socket after the write! schedule task
511 //GNUNET_NETWORK_socket_close (s5r->sock);
516 if (MHD_YES == add_handle_to_mhd ( s5r->sock ))
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Sucessfully added client to MHD!\n");
519 s_resp->version = 0x05;
520 s_resp->reply = 0x00;
521 s_resp->reserved = 0x00;
522 s_resp->addr_type = 0x01;
525 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
529 //GNUNET_free ( s5r );
530 //FIXME complete socks resp!
535 phost = (struct hostent*)gethostbyname (domain);
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Resolve %s error!\n", domain );
540 s_resp->version = 0x05;
541 s_resp->reply = 0x01;
543 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
547 //TODO! close socket after the write! schedule task
548 //GNUNET_NETWORK_socket_close (s5r->sock);
553 s5r->remote_sock = GNUNET_NETWORK_socket_create (AF_INET,
556 r_sin_addr = (struct in_addr*)(phost->h_addr);
557 remote_ip = r_sin_addr->s_addr;
558 memset(&remote_addr, 0, sizeof(remote_addr));
559 remote_addr.sin_family = AF_INET;
560 #if HAVE_SOCKADDR_IN_SIN_LEN
561 remote_addr.sin_len = sizeof (remote_addr);
563 remote_addr.sin_addr.s_addr = remote_ip;
564 remote_addr.sin_port = req_port;
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567 "target server: %s:%u\n", inet_ntoa(remote_addr.sin_addr),
571 GNUNET_NETWORK_socket_connect ( s5r->remote_sock,
572 (const struct sockaddr*)&remote_addr,
573 sizeof (remote_addr)))
574 && (errno != EINPROGRESS))
576 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "connect");
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
578 "socket request error...\n");
579 s_resp->version = 0x05;
580 s_resp->reply = 0x01;
582 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "new remote connection\n");
592 s_resp->version = 0x05;
593 s_resp->reply = 0x00;
594 s_resp->reserved = 0x00;
595 s_resp->addr_type = 0x01;
597 s5r->state = SOCKS5_DATA_TRANSFER;
600 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
604 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
612 if (s5r->state == SOCKS5_DATA_TRANSFER)
614 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "Closing connection to client\n");
618 if (s5r->rtask != GNUNET_SCHEDULER_NO_TASK)
619 GNUNET_SCHEDULER_cancel (s5r->rtask);
620 if (s5r->fwdwtask != GNUNET_SCHEDULER_NO_TASK)
621 GNUNET_SCHEDULER_cancel (s5r->fwdwtask);
622 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
623 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
624 if (s5r->fwdrtask != GNUNET_SCHEDULER_NO_TASK)
625 GNUNET_SCHEDULER_cancel (s5r->fwdrtask);
627 if (s5r->remote_sock != NULL)
628 GNUNET_NETWORK_socket_close (s5r->remote_sock);
629 GNUNET_NETWORK_socket_close (s5r->sock);
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "forwarding %d bytes from client\n", s5r->rbuf_len);
638 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
640 &do_write_remote, s5r);
642 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
645 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
647 &do_read_remote, s5r);
653 //GNUNET_CONTAINER_DLL_remove (s5conns.head, s5conns.tail, s5r);
658 * Accept new incoming connections
660 * @param cls the closure
661 * @param tc the scheduler context
664 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
666 struct GNUNET_NETWORK_Handle *s;
667 struct Socks5Request *s5r;
669 ltask = GNUNET_SCHEDULER_NO_TASK;
670 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
673 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
677 s = GNUNET_NETWORK_socket_accept (lsock, NULL, NULL);
681 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Got an inbound connection, waiting for data\n");
688 s5r = GNUNET_malloc (sizeof (struct Socks5Request));
690 s5r->state = SOCKS5_INIT;
691 s5r->wtask = GNUNET_SCHEDULER_NO_TASK;
692 s5r->fwdwtask = GNUNET_SCHEDULER_NO_TASK;
693 s5r->fwdrtask = GNUNET_SCHEDULER_NO_TASK;
694 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
697 //GNUNET_CONTAINER_DLL_insert (s5conns.head, s5conns.tail, s5r);
701 * Main function that will be run
704 * @param args remaining command-line arguments
705 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
706 * @param cfg configuration
709 run (void *cls, char *const *args, const char *cfgfile,
710 const struct GNUNET_CONFIGURATION_Handle *cfg)
712 struct sockaddr_in sa;
714 memset (&sa, 0, sizeof (sa));
715 sa.sin_family = AF_INET;
716 sa.sin_port = htons (port);
717 #if HAVE_SOCKADDR_IN_SIN_LEN
718 sa.sin_len = sizeof (sa);
721 lsock = GNUNET_NETWORK_socket_create (AF_INET,
725 if ((NULL == lsock) ||
727 GNUNET_NETWORK_socket_bind (lsock, (const struct sockaddr *) &sa,
730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731 "Failed to create listen socket bound to `%s'",
732 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
734 GNUNET_NETWORK_socket_close (lsock);
738 if (GNUNET_OK != GNUNET_NETWORK_socket_listen (lsock, 5))
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
741 "Failed to listen on socket bound to `%s'",
742 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)));
746 ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
747 lsock, &do_accept, NULL);
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Proxy listens on port %u\n",
753 httpd = MHD_start_daemon (MHD_USE_DEBUG, 4444,
755 &create_response, NULL,
756 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 128,
757 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
758 MHD_OPTION_NOTIFY_COMPLETED,
766 * The main function for gnunet-gns-proxy.
768 * @param argc number of arguments from the command line
769 * @param argv command line arguments
770 * @return 0 ok, 1 on error
773 main (int argc, char *const *argv)
775 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
777 gettext_noop ("listen on specified port"), 1,
778 &GNUNET_GETOPT_set_string, &port},
779 GNUNET_GETOPT_OPTION_END
784 GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL);
787 GNUNET_PROGRAM_run (argc, argv, "gnunet-gns-proxy",
788 _("GNUnet GNS proxy"),
790 &run, NULL)) ? 0 : 1;