diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-05-20 14:44:07 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2012-05-20 14:44:07 +0000 |
commit | 13d3775c4dbe1e17751f6dfe4ec1d8e419be3b8d (patch) | |
tree | a85cef6d96553a267f55a369f65fe3c9971e9aad /src | |
parent | 9bf22db9cf42ff29f51e9ea1474b8a1915c4f339 (diff) | |
download | gnunet-13d3775c4dbe1e17751f6dfe4ec1d8e419be3b8d.tar.gz gnunet-13d3775c4dbe1e17751f6dfe4ec1d8e419be3b8d.zip |
-start replacement code
Diffstat (limited to 'src')
-rw-r--r-- | src/gns/gnocksy/gnocksy.c | 109 |
1 files changed, 102 insertions, 7 deletions
diff --git a/src/gns/gnocksy/gnocksy.c b/src/gns/gnocksy/gnocksy.c index 3f2254778..faccfa6d2 100644 --- a/src/gns/gnocksy/gnocksy.c +++ b/src/gns/gnocksy/gnocksy.c | |||
@@ -23,6 +23,7 @@ | |||
23 | #include <arpa/inet.h> | 23 | #include <arpa/inet.h> |
24 | #include <microhttpd.h> | 24 | #include <microhttpd.h> |
25 | #include <curl/curl.h> | 25 | #include <curl/curl.h> |
26 | #include <regex.h> | ||
26 | 27 | ||
27 | #include "protocol.h" | 28 | #include "protocol.h" |
28 | 29 | ||
@@ -32,16 +33,38 @@ | |||
32 | 33 | ||
33 | #define HTML_HDR_CONTENT "Content-Type: text/html\r\n" | 34 | #define HTML_HDR_CONTENT "Content-Type: text/html\r\n" |
34 | 35 | ||
35 | struct MHD_Daemon *mhd_daemon; | 36 | #define RE_DOTPLUS "<a href=\"http://(([A-Za-z]+[.])+)([+])" |
37 | |||
38 | #define RE_N_MATCHES 4 | ||
39 | |||
40 | static struct MHD_Daemon *mhd_daemon; | ||
41 | static regex_t re_dotplus; | ||
42 | |||
43 | void | ||
44 | gns_glue_expand_and_shorten ( char* sorig, char* new ) | ||
45 | { | ||
46 | memcpy (new, "foo.bar.gnunet", strlen("foo.bar.gnunet")); | ||
47 | } | ||
36 | 48 | ||
37 | static size_t | 49 | static size_t |
38 | curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls) | 50 | curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls) |
39 | { | 51 | { |
40 | const char* page = buffer; | 52 | const char* page = buffer; |
41 | size_t bytes = size * nmemb; | 53 | uint64_t bytes = size * nmemb; |
42 | struct socks5_bridge* br = cls; | 54 | struct socks5_bridge* br = cls; |
43 | int ret; | 55 | int ret; |
44 | 56 | ||
57 | int nomatch; | ||
58 | regmatch_t m[RE_N_MATCHES]; | ||
59 | char* hostptr; | ||
60 | char* plusptr; | ||
61 | char* p; | ||
62 | char new_host[256]; | ||
63 | uint64_t bytes_copied = 0; | ||
64 | |||
65 | char new_buf[CURL_MAX_WRITE_SIZE+1]; | ||
66 | p = new_buf; | ||
67 | |||
45 | pthread_mutex_lock ( &br->m_buf ); | 68 | pthread_mutex_lock ( &br->m_buf ); |
46 | if (br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_MHD) | 69 | if (br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_MHD) |
47 | { | 70 | { |
@@ -54,11 +77,62 @@ curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls) | |||
54 | if ( br->res_is_html ) | 77 | if ( br->res_is_html ) |
55 | { | 78 | { |
56 | printf ("result is html text\n"); | 79 | printf ("result is html text\n"); |
57 | //void | 80 | memset (new_buf, 0, sizeof(new_buf)); |
58 | } | 81 | memcpy (new_buf, page, bytes); |
59 | 82 | ||
60 | memcpy (br->MHD_CURL_BUF, buffer, bytes); | 83 | |
61 | br->MHD_CURL_BUF_SIZE = bytes; | 84 | while (1) |
85 | { | ||
86 | nomatch = regexec ( &re_dotplus, p, RE_N_MATCHES, m, 0); | ||
87 | |||
88 | if (nomatch) | ||
89 | { | ||
90 | printf ("No more matches\n"); | ||
91 | if ((p-new_buf) < 0) | ||
92 | { | ||
93 | printf ("Error p<buf!\n"); | ||
94 | break; | ||
95 | } | ||
96 | memcpy ( br->MHD_CURL_BUF+bytes_copied, p, bytes-(p-new_buf)); | ||
97 | bytes_copied += bytes-(p-new_buf); | ||
98 | break; | ||
99 | } | ||
100 | |||
101 | if (DEBUG) | ||
102 | printf ("Got match\n"); | ||
103 | |||
104 | if (m[1].rm_so != -1) | ||
105 | { | ||
106 | hostptr = p+m[1].rm_eo; | ||
107 | if (DEBUG) | ||
108 | printf ("Copying %d bytes.\n", (hostptr-p)); | ||
109 | memcpy (br->MHD_CURL_BUF+bytes_copied, p, (hostptr-p)); | ||
110 | bytes_copied += (hostptr-p); | ||
111 | memset (new_host, 0, sizeof(new_host)); | ||
112 | gns_glue_expand_and_shorten ( br->full_url, | ||
113 | new_host ); | ||
114 | if (DEBUG) | ||
115 | { | ||
116 | printf ("Glue fin\n"); | ||
117 | printf ("Copying new name %s \n", new_host); | ||
118 | } | ||
119 | memcpy ( br->MHD_CURL_BUF+bytes_copied, new_host, | ||
120 | strlen (new_host) ); | ||
121 | bytes_copied += strlen (new_host); | ||
122 | p += m[3].rm_so+1; | ||
123 | |||
124 | printf ("Done. Next in %d bytes\n", m[3].rm_so); | ||
125 | |||
126 | //TODO check buf lenghts! | ||
127 | } | ||
128 | } | ||
129 | br->MHD_CURL_BUF_SIZE = bytes_copied; | ||
130 | } | ||
131 | else | ||
132 | { | ||
133 | memcpy (br->MHD_CURL_BUF, buffer, bytes); | ||
134 | br->MHD_CURL_BUF_SIZE = bytes; | ||
135 | } | ||
62 | 136 | ||
63 | br->MHD_CURL_BUF_STATUS = BUF_WAIT_FOR_MHD; | 137 | br->MHD_CURL_BUF_STATUS = BUF_WAIT_FOR_MHD; |
64 | 138 | ||
@@ -66,7 +140,7 @@ curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls) | |||
66 | 140 | ||
67 | 141 | ||
68 | //MHD_destroy_response (response); | 142 | //MHD_destroy_response (response); |
69 | printf( "buffer: %s\n", (char*)buffer ); | 143 | printf( "buffer: %s\n", (char*)br->MHD_CURL_BUF ); |
70 | return bytes; | 144 | return bytes; |
71 | } | 145 | } |
72 | 146 | ||
@@ -378,6 +452,23 @@ accept_cb (void *cls, | |||
378 | 452 | ||
379 | } | 453 | } |
380 | 454 | ||
455 | static int | ||
456 | compile_regex (regex_t *re, const char* rt) | ||
457 | { | ||
458 | int status; | ||
459 | char err[1024]; | ||
460 | |||
461 | status = regcomp (re, rt, REG_EXTENDED|REG_NEWLINE); | ||
462 | if (status) | ||
463 | { | ||
464 | regerror (status, re, err, 1024); | ||
465 | printf ("Regex error compiling '%s': %s\n", rt, err); | ||
466 | return 1; | ||
467 | } | ||
468 | return 0; | ||
469 | } | ||
470 | |||
471 | |||
381 | int main ( int argc, char *argv[] ) | 472 | int main ( int argc, char *argv[] ) |
382 | { | 473 | { |
383 | int sfd, s; | 474 | int sfd, s; |
@@ -408,6 +499,9 @@ int main ( int argc, char *argv[] ) | |||
408 | 499 | ||
409 | mhd_daemon = NULL; | 500 | mhd_daemon = NULL; |
410 | curl_global_init(CURL_GLOBAL_ALL); | 501 | curl_global_init(CURL_GLOBAL_ALL); |
502 | |||
503 | //compile_regex ( &re_htmlhdr, RE_HTML ); | ||
504 | compile_regex( &re_dotplus, (char*)RE_DOTPLUS ); | ||
411 | 505 | ||
412 | for (j = 0; j < MAXEVENTS; j++) | 506 | for (j = 0; j < MAXEVENTS; j++) |
413 | ev_states[j] = SOCKS5_INIT; | 507 | ev_states[j] = SOCKS5_INIT; |
@@ -684,6 +778,7 @@ int main ( int argc, char *argv[] ) | |||
684 | 778 | ||
685 | free (events); | 779 | free (events); |
686 | MHD_stop_daemon (mhd_daemon); | 780 | MHD_stop_daemon (mhd_daemon); |
781 | regfree ( &re_dotplus ); | ||
687 | close (sfd); | 782 | close (sfd); |
688 | 783 | ||
689 | return EXIT_SUCCESS; | 784 | return EXIT_SUCCESS; |