aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-09-16 12:34:06 +0000
committerChristian Grothoff <christian@grothoff.org>2013-09-16 12:34:06 +0000
commit31ad43059a029b3905031dbd017869a72e18544e (patch)
tree14645f39e298832b23e6af933c561a750c00e854 /src
parent6c57a74f95e4c48d5a71aa48dd9af15baf9bc60f (diff)
downloadgnunet-31ad43059a029b3905031dbd017869a72e18544e.tar.gz
gnunet-31ad43059a029b3905031dbd017869a72e18544e.zip
-towards a compiling gns proxy
Diffstat (limited to 'src')
-rw-r--r--src/gns/gnunet-gns-proxy.c216
1 files changed, 66 insertions, 150 deletions
diff --git a/src/gns/gnunet-gns-proxy.c b/src/gns/gnunet-gns-proxy.c
index 26c397b21..dca4b9342 100644
--- a/src/gns/gnunet-gns-proxy.c
+++ b/src/gns/gnunet-gns-proxy.c
@@ -183,28 +183,6 @@ struct Socks5Request
183 183
184 184
185/** 185/**
186 * DLL for Network Handles
187 */
188struct NetworkHandleList
189{
190 /**
191 * DLL
192 */
193 struct NetworkHandleList *next;
194
195 /**
196 * DLL
197 */
198 struct NetworkHandleList *prev;
199
200 /**
201 * The handle
202 */
203 struct GNUNET_NETWORK_Handle *h;
204};
205
206
207/**
208 * A structure for all running Httpds 186 * A structure for all running Httpds
209 */ 187 */
210struct MhdHttpList 188struct MhdHttpList
@@ -244,15 +222,6 @@ struct MhdHttpList
244 */ 222 */
245 GNUNET_SCHEDULER_TaskIdentifier httpd_task; 223 GNUNET_SCHEDULER_TaskIdentifier httpd_task;
246 224
247 /**
248 * Handles associated with this daemon
249 */
250 struct NetworkHandleList *socket_handles_head;
251
252 /**
253 * Handles associated with this daemon
254 */
255 struct NetworkHandleList *socket_handles_tail;
256}; 225};
257 226
258 227
@@ -2305,7 +2274,7 @@ do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2305 * 2274 *
2306 * @param h the handle to the socket to add 2275 * @param h the handle to the socket to add
2307 * @param daemon the daemon to add the fd to 2276 * @param daemon the daemon to add the fd to
2308 * @return whatever MHD_add_connection returns 2277 * @return whatever #MHD_add_connection returns
2309 */ 2278 */
2310static int 2279static int
2311add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon) 2280add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon)
@@ -2522,38 +2491,33 @@ accept_cb (void* cls, const struct sockaddr *addr, socklen_t addrlen)
2522 2491
2523 2492
2524/** 2493/**
2525 * Adds a socket to an SSL MHD instance 2494 * Adds a socket to an SSL MHD instance It is important that the
2526 * It is important the the domain name is 2495 * domain name is correct. In most cases we need to start a new daemon.
2527 * correct. In most cases we need to start a new daemon
2528 * 2496 *
2529 * @param h the handle to add to a daemon 2497 * @param h the handle to add to a daemon
2530 * @param domain the domain the ssl daemon has to serve 2498 * @param domain the domain the SSL daemon has to serve
2531 * @return #MHD_YES on success 2499 * @return #MHD_YES on success
2532 */ 2500 */
2533static int 2501static int
2534add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain) 2502add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h,
2503 const char* domain)
2535{ 2504{
2536 struct MhdHttpList *hd; 2505 struct MhdHttpList *hd;
2537 struct ProxyGNSCertificate *pgc; 2506 struct ProxyGNSCertificate *pgc;
2538 struct NetworkHandleList *nh;
2539 2507
2540 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next) 2508 for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
2541 if (0 == strcmp (hd->domain, domain)) 2509 if (0 == strcmp (hd->domain, domain))
2542 break; 2510 break;
2543
2544 if (NULL == hd) 2511 if (NULL == hd)
2545 { 2512 {
2546 pgc = generate_gns_certificate (domain); 2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2547 2514 "Starting fresh MHD HTTPS instance for domain `%s'\n",
2515 domain);
2516 pgc = generate_gns_certificate (domain);
2548 hd = GNUNET_new (struct MhdHttpList); 2517 hd = GNUNET_new (struct MhdHttpList);
2549 hd->is_ssl = GNUNET_YES; 2518 hd->is_ssl = GNUNET_YES;
2550 strcpy (hd->domain, domain); 2519 strcpy (hd->domain, domain); /* FIXME: avoid fixed-sized buffers... */
2551 hd->proxy_cert = pgc; 2520 hd->proxy_cert = pgc;
2552
2553 /* Start new MHD */
2554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2555 "No previous SSL instance found... starting new one for %s\n",
2556 domain);
2557 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL | MHD_USE_NO_LISTEN_SOCKET, 2521 hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL | MHD_USE_NO_LISTEN_SOCKET,
2558 0, 2522 0,
2559 &accept_cb, NULL, 2523 &accept_cb, NULL,
@@ -2567,19 +2531,10 @@ add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain)
2567 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, 2531 MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
2568 NULL, 2532 NULL,
2569 MHD_OPTION_END); 2533 MHD_OPTION_END);
2570 GNUNET_assert (hd->daemon != NULL); 2534 /* FIXME: rather than assert, handle error! */
2571 hd->httpd_task = GNUNET_SCHEDULER_NO_TASK; 2535 GNUNET_assert (NULL != hd->daemon);
2572
2573 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd); 2536 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd);
2574 } 2537 }
2575
2576 nh = GNUNET_new (struct NetworkHandleList);
2577 nh->h = h;
2578
2579 GNUNET_CONTAINER_DLL_insert (hd->socket_handles_head,
2580 hd->socket_handles_tail,
2581 nh);
2582
2583 return add_handle_to_mhd (h, hd->daemon); 2538 return add_handle_to_mhd (h, hd->daemon);
2584} 2539}
2585 2540
@@ -2606,85 +2561,64 @@ do_s5r_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2606 uint32_t remote_ip; 2561 uint32_t remote_ip;
2607 struct sockaddr_in remote_addr; 2562 struct sockaddr_in remote_addr;
2608 struct in_addr *r_sin_addr; 2563 struct in_addr *r_sin_addr;
2609 struct NetworkHandleList *nh;
2610 2564
2611 s5r->rtask = GNUNET_SCHEDULER_NO_TASK; 2565 s5r->rtask = GNUNET_SCHEDULER_NO_TASK;
2612 if ((NULL != tc->write_ready) && 2566 if ( (NULL != tc->read_ready) &&
2613 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) && 2567 (GNUNET_NETWORK_fdset_isset (tc->read_ready, s5r->sock)) )
2614 (s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf, 2568 s5r->rbuf_len = GNUNET_NETWORK_socket_recv (s5r->sock, s5r->rbuf,
2615 sizeof (s5r->rbuf)))) 2569 sizeof (s5r->rbuf));
2616 {
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 "Successfully read %d bytes from socket\n",
2619 s5r->rbuf_len);
2620 }
2621 else 2570 else
2571 s5r->rbuf_len = 0;
2572 if (0 == s5r->rbuf_len)
2622 { 2573 {
2623 if (s5r->rbuf_len != 0) 2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2624 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "read"); 2575 "socks5 client disconnected.\n");
2625 else
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disco!\n");
2627 cleanup_s5r (s5r); 2576 cleanup_s5r (s5r);
2628 return; 2577 return;
2629 } 2578 }
2630 2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2631 if (s5r->state == SOCKS5_INIT) 2580 "Processing socks data in state %d\n",
2581 s5r->state);
2582 switch (s5r->state)
2632 { 2583 {
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2584 case SOCKS5_INIT:
2634 "SOCKS5 init\n"); 2585 /* FIXME: failed to check if we got enough data yet! */
2635 c_hello = (struct socks5_client_hello*)&s5r->rbuf; 2586 c_hello = (struct socks5_client_hello*) &s5r->rbuf;
2636
2637 GNUNET_assert (c_hello->version == SOCKS_VERSION_5); 2587 GNUNET_assert (c_hello->version == SOCKS_VERSION_5);
2638 2588 s_hello = (struct socks5_server_hello*) &s5r->wbuf;
2639 s_hello = (struct socks5_server_hello*)&s5r->wbuf;
2640 s5r->wbuf_len = sizeof( struct socks5_server_hello ); 2589 s5r->wbuf_len = sizeof( struct socks5_server_hello );
2641
2642 s_hello->version = c_hello->version; 2590 s_hello->version = c_hello->version;
2643 s_hello->auth_method = SOCKS_AUTH_NONE; 2591 s_hello->auth_method = SOCKS_AUTH_NONE;
2644
2645 /* Write response to client */ 2592 /* Write response to client */
2646 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, 2593 s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2647 s5r->sock, 2594 s5r->sock,
2648 &do_write, s5r); 2595 &do_write, s5r);
2649
2650 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 2596 s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2651 s5r->sock, 2597 s5r->sock,
2652 &do_s5r_read, s5r); 2598 &do_s5r_read, s5r);
2653
2654 s5r->state = SOCKS5_REQUEST; 2599 s5r->state = SOCKS5_REQUEST;
2655 return; 2600 return;
2656 } 2601 case SOCKS5_REQUEST:
2657 2602 /* FIXME: failed to check if we got enough data yet!? */
2658 if (s5r->state == SOCKS5_REQUEST) 2603 c_req = (struct socks5_client_request *) &s5r->rbuf;
2659 { 2604 s_resp = (struct socks5_server_response *) &s5r->wbuf;
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2605 //Only 10 byte for ipv4 response!
2661 "Processing SOCKS5 request\n");
2662 c_req = (struct socks5_client_request*)&s5r->rbuf;
2663 s_resp = (struct socks5_server_response*)&s5r->wbuf;
2664 //Only 10byte for ipv4 response!
2665 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response); 2606 s5r->wbuf_len = 10;//sizeof (struct socks5_server_response);
2666
2667 GNUNET_assert (c_req->addr_type == 3); 2607 GNUNET_assert (c_req->addr_type == 3);
2668
2669 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1)); 2608 dom_len = *((uint8_t*)(&(c_req->addr_type) + 1));
2670 memset(domain, 0, sizeof(domain)); 2609 memset(domain, 0, sizeof(domain));
2671 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len); 2610 strncpy(domain, (char*)(&(c_req->addr_type) + 2), dom_len);
2672 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len)); 2611 req_port = *((uint16_t*)(&(c_req->addr_type) + 2 + dom_len));
2673
2674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2675 "Requested connection is %s:%d\n", 2613 "Requested connection is to %s:%d\n",
2676 domain, 2614 domain,
2677 ntohs(req_port)); 2615 ntohs(req_port));
2678
2679 if (is_tld (domain, GNUNET_GNS_TLD) || 2616 if (is_tld (domain, GNUNET_GNS_TLD) ||
2680 is_tld (domain, GNUNET_GNS_TLD_ZKEY)) 2617 is_tld (domain, GNUNET_GNS_TLD_ZKEY))
2681 { 2618 {
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2619 /* GNS TLD */
2683 "Requested connection is gnunet tld\n",
2684 domain);
2685
2686 ret = MHD_NO; 2620 ret = MHD_NO;
2687 if (ntohs(req_port) == HTTPS_PORT) 2621 if (ntohs (req_port) == HTTPS_PORT)
2688 { 2622 {
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2690 "Requested connection is HTTPS\n"); 2624 "Requested connection is HTTPS\n");
@@ -2694,13 +2628,6 @@ do_s5r_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2694 { 2628 {
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2696 "Requested connection is HTTP\n"); 2630 "Requested connection is HTTP\n");
2697 nh = GNUNET_new (struct NetworkHandleList);
2698 nh->h = s5r->sock;
2699
2700 GNUNET_CONTAINER_DLL_insert (mhd_httpd_head->socket_handles_head,
2701 mhd_httpd_head->socket_handles_tail,
2702 nh);
2703
2704 ret = add_handle_to_mhd ( s5r->sock, httpd ); 2631 ret = add_handle_to_mhd ( s5r->sock, httpd );
2705 } 2632 }
2706 2633
@@ -2736,7 +2663,9 @@ do_s5r_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2736 } 2663 }
2737 else 2664 else
2738 { 2665 {
2739 phost = (struct hostent*)gethostbyname (domain); 2666 /* non-GNS TLD, use DNS to resolve */
2667 /* FIXME: make asynchronous! */
2668 phost = (struct hostent *) gethostbyname (domain);
2740 if (phost == NULL) 2669 if (phost == NULL)
2741 { 2670 {
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -2787,17 +2716,13 @@ do_s5r_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2787 //TODO see above 2716 //TODO see above
2788 return; 2717 return;
2789 } 2718 }
2790
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2792 "new remote connection\n"); 2720 "new remote connection\n");
2793
2794 s_resp->version = 0x05; 2721 s_resp->version = 0x05;
2795 s_resp->reply = 0x00; 2722 s_resp->reply = 0x00;
2796 s_resp->reserved = 0x00; 2723 s_resp->reserved = 0x00;
2797 s_resp->addr_type = 0x01; 2724 s_resp->addr_type = 0x01;
2798
2799 s5r->state = SOCKS5_DATA_TRANSFER; 2725 s5r->state = SOCKS5_DATA_TRANSFER;
2800
2801 s5r->wtask = 2726 s5r->wtask =
2802 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, 2727 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2803 s5r->sock, 2728 s5r->sock,
@@ -2806,36 +2731,35 @@ do_s5r_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2806 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 2731 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2807 s5r->sock, 2732 s5r->sock,
2808 &do_s5r_read, s5r); 2733 &do_s5r_read, s5r);
2809
2810 } 2734 }
2811 return; 2735 return;
2812 } 2736 case SOCKS5_DATA_TRANSFER:
2813
2814 if (s5r->state == SOCKS5_DATA_TRANSFER)
2815 {
2816 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2817 { 2737 {
2738 if ((s5r->remote_sock == NULL) || (s5r->rbuf_len == 0))
2739 {
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 "Closing connection to client\n");
2742 cleanup_s5r (s5r);
2743 return;
2744 }
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2819 "Closing connection to client\n"); 2746 "forwarding %d bytes from client\n", s5r->rbuf_len);
2820 cleanup_s5r (s5r); 2747 s5r->fwdwtask =
2821 return; 2748 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
2822 } 2749 s5r->remote_sock,
2823 2750 &do_write_remote, s5r);
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2751 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2825 "forwarding %d bytes from client\n", s5r->rbuf_len); 2752 {
2826 2753 s5r->fwdrtask =
2827 s5r->fwdwtask = 2754 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2828 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, 2755 s5r->remote_sock,
2829 s5r->remote_sock, 2756 &do_read_remote, s5r);
2830 &do_write_remote, s5r); 2757 }
2831
2832 if (s5r->fwdrtask == GNUNET_SCHEDULER_NO_TASK)
2833 {
2834 s5r->fwdrtask =
2835 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2836 s5r->remote_sock,
2837 &do_read_remote, s5r);
2838 } 2758 }
2759 return;
2760 default:
2761 GNUNET_break (0);
2762 return;
2839 } 2763 }
2840} 2764}
2841 2765
@@ -2888,8 +2812,6 @@ do_shutdown (void *cls,
2888{ 2812{
2889 struct MhdHttpList *hd; 2813 struct MhdHttpList *hd;
2890 struct MhdHttpList *tmp_hd; 2814 struct MhdHttpList *tmp_hd;
2891 struct NetworkHandleList *nh;
2892 struct NetworkHandleList *tmp_nh;
2893 struct ProxyCurlTask *ctask; 2815 struct ProxyCurlTask *ctask;
2894 struct ProxyCurlTask *ctask_tmp; 2816 struct ProxyCurlTask *ctask_tmp;
2895 struct ProxyUploadData *pdata; 2817 struct ProxyUploadData *pdata;
@@ -2909,12 +2831,6 @@ do_shutdown (void *cls,
2909 MHD_stop_daemon (hd->daemon); 2831 MHD_stop_daemon (hd->daemon);
2910 hd->daemon = NULL; 2832 hd->daemon = NULL;
2911 } 2833 }
2912 for (nh = hd->socket_handles_head; NULL != nh; nh = tmp_nh)
2913 {
2914 tmp_nh = nh->next;
2915 GNUNET_NETWORK_socket_close (nh->h);
2916 GNUNET_free (nh);
2917 }
2918 GNUNET_free_non_null (hd->proxy_cert); 2834 GNUNET_free_non_null (hd->proxy_cert);
2919 GNUNET_free (hd); 2835 GNUNET_free (hd);
2920 } 2836 }