diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-10-18 07:46:05 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-10-18 07:46:05 +0000 |
commit | 5c44769d262656d490e68c094b1206e5e4f96416 (patch) | |
tree | 6606c54cfe32e21442051caaf9d621fee8f952a3 /src/gns | |
parent | dc9fef5ddf97c451b08dbabbb957a0f3aac73a44 (diff) | |
download | gnunet-5c44769d262656d490e68c094b1206e5e4f96416.tar.gz gnunet-5c44769d262656d490e68c094b1206e5e4f96416.zip |
-code cleanup, handle -1 timeout from curl better
Diffstat (limited to 'src/gns')
-rw-r--r-- | src/gns/gnunet-gns-proxy.c | 86 |
1 files changed, 38 insertions, 48 deletions
diff --git a/src/gns/gnunet-gns-proxy.c b/src/gns/gnunet-gns-proxy.c index 9c8fde861..5cef44154 100644 --- a/src/gns/gnunet-gns-proxy.c +++ b/src/gns/gnunet-gns-proxy.c | |||
@@ -17,6 +17,12 @@ | |||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | /** | ||
21 | * @author Martin Schanzenbach | ||
22 | * @file src/gns/gnunet-gns-proxy.c | ||
23 | * @brief HTTP(S) proxy that rewrites URIs and fakes certificats to make GADS work | ||
24 | * with legacy browsers | ||
25 | */ | ||
20 | #include "platform.h" | 26 | #include "platform.h" |
21 | #include <microhttpd.h> | 27 | #include <microhttpd.h> |
22 | #include <curl/curl.h> | 28 | #include <curl/curl.h> |
@@ -741,6 +747,7 @@ mhd_content_cb (void *cls, | |||
741 | char* buf, | 747 | char* buf, |
742 | size_t max); | 748 | size_t max); |
743 | 749 | ||
750 | |||
744 | /** | 751 | /** |
745 | * Check HTTP response header for mime | 752 | * Check HTTP response header for mime |
746 | * | 753 | * |
@@ -837,14 +844,11 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls) | |||
837 | MHD_HTTP_HEADER_SET_COOKIE, | 844 | MHD_HTTP_HEADER_SET_COOKIE, |
838 | cookie_hdr_len)) | 845 | cookie_hdr_len)) |
839 | { | 846 | { |
847 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
848 | "Looking for cookie in: `%s'\n", hdr_generic); | ||
840 | ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1); | 849 | ndup = GNUNET_strdup (hdr_generic+cookie_hdr_len+1); |
841 | memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr)); | 850 | memset (new_cookie_hdr, 0, sizeof (new_cookie_hdr)); |
842 | tok = strtok (ndup, ";"); | 851 | for (tok = strtok (ndup, ";"); tok != NULL; tok = strtok (NULL, ";")) |
843 | |||
844 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
845 | "Looking for cookie in : %s\n", hdr_generic); | ||
846 | |||
847 | for (; tok != NULL; tok = strtok (NULL, ";")) | ||
848 | { | 852 | { |
849 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 853 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
850 | "Got Cookie token: %s\n", tok); | 854 | "Got Cookie token: %s\n", tok); |
@@ -966,6 +970,7 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void *cls) | |||
966 | return bytes; | 970 | return bytes; |
967 | } | 971 | } |
968 | 972 | ||
973 | |||
969 | /** | 974 | /** |
970 | * schedule mhd | 975 | * schedule mhd |
971 | * | 976 | * |
@@ -982,6 +987,7 @@ run_httpd (struct MhdHttpList *hd); | |||
982 | static void | 987 | static void |
983 | run_httpds (void); | 988 | run_httpds (void); |
984 | 989 | ||
990 | |||
985 | /** | 991 | /** |
986 | * Task run whenever HTTP server operations are pending. | 992 | * Task run whenever HTTP server operations are pending. |
987 | * | 993 | * |
@@ -992,6 +998,7 @@ static void | |||
992 | do_httpd (void *cls, | 998 | do_httpd (void *cls, |
993 | const struct GNUNET_SCHEDULER_TaskContext *tc); | 999 | const struct GNUNET_SCHEDULER_TaskContext *tc); |
994 | 1000 | ||
1001 | |||
995 | static void | 1002 | static void |
996 | run_mhd_now (struct MhdHttpList *hd) | 1003 | run_mhd_now (struct MhdHttpList *hd) |
997 | { | 1004 | { |
@@ -1006,12 +1013,14 @@ run_mhd_now (struct MhdHttpList *hd) | |||
1006 | hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd); | 1013 | hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd, hd); |
1007 | } | 1014 | } |
1008 | 1015 | ||
1016 | |||
1009 | /** | 1017 | /** |
1010 | * Ask cURL for the select sets and schedule download | 1018 | * Ask cURL for the select sets and schedule download |
1011 | */ | 1019 | */ |
1012 | static void | 1020 | static void |
1013 | curl_download_prepare (void); | 1021 | curl_download_prepare (void); |
1014 | 1022 | ||
1023 | |||
1015 | /** | 1024 | /** |
1016 | * Callback to free content | 1025 | * Callback to free content |
1017 | * | 1026 | * |
@@ -1491,6 +1500,7 @@ post_read_callback (void *buf, size_t size, size_t nmemb, void *cls) | |||
1491 | return to_copy; | 1500 | return to_copy; |
1492 | } | 1501 | } |
1493 | 1502 | ||
1503 | |||
1494 | /** | 1504 | /** |
1495 | * Task that is run when we are ready to receive more data | 1505 | * Task that is run when we are ready to receive more data |
1496 | * from curl | 1506 | * from curl |
@@ -1501,6 +1511,7 @@ post_read_callback (void *buf, size_t size, size_t nmemb, void *cls) | |||
1501 | static void | 1511 | static void |
1502 | curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | 1512 | curl_task_download (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); |
1503 | 1513 | ||
1514 | |||
1504 | /** | 1515 | /** |
1505 | * Ask cURL for the select sets and schedule download | 1516 | * Ask cURL for the select sets and schedule download |
1506 | */ | 1517 | */ |
@@ -1521,9 +1532,7 @@ curl_download_prepare () | |||
1521 | FD_ZERO (&rs); | 1532 | FD_ZERO (&rs); |
1522 | FD_ZERO (&ws); | 1533 | FD_ZERO (&ws); |
1523 | FD_ZERO (&es); | 1534 | FD_ZERO (&es); |
1524 | mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max); | 1535 | if (CURLM_OK != (mret = curl_multi_fdset (curl_multi, &rs, &ws, &es, &max))) |
1525 | |||
1526 | if (mret != CURLM_OK) | ||
1527 | { | 1536 | { |
1528 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 1537 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
1529 | "%s failed at %s:%d: `%s'\n", | 1538 | "%s failed at %s:%d: `%s'\n", |
@@ -1532,23 +1541,20 @@ curl_download_prepare () | |||
1532 | //TODO cleanup here? | 1541 | //TODO cleanup here? |
1533 | return; | 1542 | return; |
1534 | } | 1543 | } |
1535 | 1544 | to = -1; | |
1536 | mret = curl_multi_timeout (curl_multi, &to); | 1545 | GNUNET_break (CURLM_OK == curl_multi_timeout (curl_multi, &to)); |
1537 | rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to); | ||
1538 | |||
1539 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1546 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1540 | "cURL multi fds: max=%d timeout=%llu\n", max, to); | 1547 | "cURL multi fds: max=%d timeout=%lld\n", max, (long long) to); |
1541 | 1548 | if (-1 == to) | |
1549 | rtime = GNUNET_TIME_UNIT_FOREVER_REL; | ||
1550 | else | ||
1551 | rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to); | ||
1542 | grs = GNUNET_NETWORK_fdset_create (); | 1552 | grs = GNUNET_NETWORK_fdset_create (); |
1543 | gws = GNUNET_NETWORK_fdset_create (); | 1553 | gws = GNUNET_NETWORK_fdset_create (); |
1544 | GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1); | 1554 | GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1); |
1545 | GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1); | 1555 | GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1); |
1546 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1547 | "Scheduling task cURL\n"); | ||
1548 | |||
1549 | if (curl_download_task != GNUNET_SCHEDULER_NO_TASK) | 1556 | if (curl_download_task != GNUNET_SCHEDULER_NO_TASK) |
1550 | GNUNET_SCHEDULER_cancel (curl_download_task); | 1557 | GNUNET_SCHEDULER_cancel (curl_download_task); |
1551 | |||
1552 | if (-1 != max) | 1558 | if (-1 != max) |
1553 | { | 1559 | { |
1554 | curl_download_task = | 1560 | curl_download_task = |
@@ -1854,6 +1860,7 @@ process_leho_lookup (void *cls, | |||
1854 | 1860 | ||
1855 | } | 1861 | } |
1856 | 1862 | ||
1863 | |||
1857 | /** | 1864 | /** |
1858 | * Initialize download and trigger curl | 1865 | * Initialize download and trigger curl |
1859 | * | 1866 | * |
@@ -1890,6 +1897,7 @@ process_get_authority (void *cls, | |||
1890 | ctask); | 1897 | ctask); |
1891 | } | 1898 | } |
1892 | 1899 | ||
1900 | |||
1893 | static void* | 1901 | static void* |
1894 | mhd_log_callback (void* cls, const char* url) | 1902 | mhd_log_callback (void* cls, const char* url) |
1895 | { | 1903 | { |
@@ -2272,7 +2280,6 @@ do_httpd (void *cls, | |||
2272 | } | 2280 | } |
2273 | 2281 | ||
2274 | 2282 | ||
2275 | |||
2276 | /** | 2283 | /** |
2277 | * Read data from socket | 2284 | * Read data from socket |
2278 | * | 2285 | * |
@@ -2282,6 +2289,7 @@ do_httpd (void *cls, | |||
2282 | static void | 2289 | static void |
2283 | do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | 2290 | do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); |
2284 | 2291 | ||
2292 | |||
2285 | /** | 2293 | /** |
2286 | * Read from remote end | 2294 | * Read from remote end |
2287 | * | 2295 | * |
@@ -2291,6 +2299,7 @@ do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | |||
2291 | static void | 2299 | static void |
2292 | do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | 2300 | do_read_remote (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc); |
2293 | 2301 | ||
2302 | |||
2294 | /** | 2303 | /** |
2295 | * Write data to remote socket | 2304 | * Write data to remote socket |
2296 | * | 2305 | * |
@@ -2358,6 +2367,7 @@ cleanup_s5r (struct Socks5Request *s5r) | |||
2358 | GNUNET_free(s5r); | 2367 | GNUNET_free(s5r); |
2359 | } | 2368 | } |
2360 | 2369 | ||
2370 | |||
2361 | /** | 2371 | /** |
2362 | * Write data to socket | 2372 | * Write data to socket |
2363 | * | 2373 | * |
@@ -2406,6 +2416,7 @@ do_write (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
2406 | &do_read_remote, s5r); | 2416 | &do_read_remote, s5r); |
2407 | } | 2417 | } |
2408 | 2418 | ||
2419 | |||
2409 | /** | 2420 | /** |
2410 | * Read from remote end | 2421 | * Read from remote end |
2411 | * | 2422 | * |
@@ -2475,6 +2486,7 @@ add_handle_to_mhd (struct GNUNET_NETWORK_Handle *h, struct MHD_Daemon *daemon) | |||
2475 | return MHD_add_connection (daemon, fd, addr, len); | 2486 | return MHD_add_connection (daemon, fd, addr, len); |
2476 | } | 2487 | } |
2477 | 2488 | ||
2489 | |||
2478 | /** | 2490 | /** |
2479 | * Read file in filename | 2491 | * Read file in filename |
2480 | * | 2492 | * |
@@ -2648,7 +2660,7 @@ generate_gns_certificate (const char *name) | |||
2648 | } | 2660 | } |
2649 | 2661 | ||
2650 | 2662 | ||
2651 | /* | 2663 | /** |
2652 | * Accept policy for mhdaemons | 2664 | * Accept policy for mhdaemons |
2653 | * | 2665 | * |
2654 | * @param cls NULL | 2666 | * @param cls NULL |
@@ -2755,7 +2767,6 @@ add_handle_to_ssl_mhd (struct GNUNET_NETWORK_Handle *h, const char* domain) | |||
2755 | } | 2767 | } |
2756 | 2768 | ||
2757 | 2769 | ||
2758 | |||
2759 | /** | 2770 | /** |
2760 | * Read data from incoming connection | 2771 | * Read data from incoming connection |
2761 | * | 2772 | * |
@@ -3032,10 +3043,7 @@ do_read (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
3032 | s5r->remote_sock, | 3043 | s5r->remote_sock, |
3033 | &do_read_remote, s5r); | 3044 | &do_read_remote, s5r); |
3034 | } | 3045 | } |
3035 | |||
3036 | |||
3037 | } | 3046 | } |
3038 | |||
3039 | } | 3047 | } |
3040 | 3048 | ||
3041 | 3049 | ||
@@ -3330,6 +3338,7 @@ load_local_zone_key (const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
3330 | return GNUNET_YES; | 3338 | return GNUNET_YES; |
3331 | } | 3339 | } |
3332 | 3340 | ||
3341 | |||
3333 | /** | 3342 | /** |
3334 | * Main function that will be run | 3343 | * Main function that will be run |
3335 | * | 3344 | * |
@@ -3446,13 +3455,9 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
3446 | GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa))); | 3455 | GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa))); |
3447 | return; | 3456 | return; |
3448 | } | 3457 | } |
3449 | |||
3450 | ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | 3458 | ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, |
3451 | lsock, &do_accept, NULL); | 3459 | lsock, &do_accept, NULL); |
3452 | 3460 | ||
3453 | ctasks_head = NULL; | ||
3454 | ctasks_tail = NULL; | ||
3455 | |||
3456 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) | 3461 | if (0 != curl_global_init (CURL_GLOBAL_WIN32)) |
3457 | { | 3462 | { |
3458 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 3463 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -3463,10 +3468,6 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
3463 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 3468 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
3464 | "Proxy listens on port %u\n", | 3469 | "Proxy listens on port %u\n", |
3465 | port); | 3470 | port); |
3466 | |||
3467 | mhd_httpd_head = NULL; | ||
3468 | mhd_httpd_tail = NULL; | ||
3469 | total_mhd_connections = 0; | ||
3470 | #if ! HAVE_MHD_NO_LISTEN_SOCKET | 3471 | #if ! HAVE_MHD_NO_LISTEN_SOCKET |
3471 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy", | 3472 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns-proxy", |
3472 | "PROXY_UNIXPATH", | 3473 | "PROXY_UNIXPATH", |
@@ -3548,18 +3549,13 @@ run (void *cls, char *const *args, const char *cfgfile, | |||
3548 | MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL, | 3549 | MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback, NULL, |
3549 | MHD_OPTION_END); | 3550 | MHD_OPTION_END); |
3550 | #endif | 3551 | #endif |
3551 | 3552 | GNUNET_break (httpd != NULL); | |
3552 | GNUNET_assert (httpd != NULL); | ||
3553 | hd->daemon = httpd; | 3553 | hd->daemon = httpd; |
3554 | hd->httpd_task = GNUNET_SCHEDULER_NO_TASK; | 3554 | hd->httpd_task = GNUNET_SCHEDULER_NO_TASK; |
3555 | |||
3556 | GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd); | 3555 | GNUNET_CONTAINER_DLL_insert (mhd_httpd_head, mhd_httpd_tail, hd); |
3557 | |||
3558 | run_httpds (); | 3556 | run_httpds (); |
3559 | |||
3560 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | 3557 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, |
3561 | &do_shutdown, NULL); | 3558 | &do_shutdown, NULL); |
3562 | |||
3563 | } | 3559 | } |
3564 | 3560 | ||
3565 | 3561 | ||
@@ -3582,13 +3578,10 @@ main (int argc, char *const *argv) | |||
3582 | &GNUNET_GETOPT_set_string, &cafile_opt}, | 3578 | &GNUNET_GETOPT_set_string, &cafile_opt}, |
3583 | GNUNET_GETOPT_OPTION_END | 3579 | GNUNET_GETOPT_OPTION_END |
3584 | }; | 3580 | }; |
3585 | |||
3586 | int ret; | 3581 | int ret; |
3587 | 3582 | ||
3588 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 3583 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) |
3589 | return 2; | 3584 | return 2; |
3590 | |||
3591 | |||
3592 | GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL); | 3585 | GNUNET_log_setup ("gnunet-gns-proxy", "WARNING", NULL); |
3593 | ret = | 3586 | ret = |
3594 | (GNUNET_OK == | 3587 | (GNUNET_OK == |
@@ -3596,11 +3589,8 @@ main (int argc, char *const *argv) | |||
3596 | _("GNUnet GNS proxy"), | 3589 | _("GNUnet GNS proxy"), |
3597 | options, | 3590 | options, |
3598 | &run, NULL)) ? 0 : 1; | 3591 | &run, NULL)) ? 0 : 1; |
3599 | GNUNET_free_non_null ((char*)argv); | 3592 | GNUNET_free_non_null ((char *) argv); |
3600 | |||
3601 | return ret; | 3593 | return ret; |
3602 | } | 3594 | } |
3603 | 3595 | ||
3604 | 3596 | /* end of gnunet-gns-proxy.c */ | |
3605 | |||
3606 | |||