From fd98a44c4a4b6b6661034ab1a0100939700df769 Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Sun, 20 May 2012 16:12:23 +0000 Subject: [PATCH] -add gns glue --- src/gns/gnocksy/gnocksy.c | 21 ++++--- src/gns/gnocksy/gns_glue.c | 119 +++++++++++++++++++++++++++++++++++++ src/gns/gnocksy/gns_glue.h | 10 ++++ 3 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 src/gns/gnocksy/gns_glue.c create mode 100644 src/gns/gnocksy/gns_glue.h diff --git a/src/gns/gnocksy/gnocksy.c b/src/gns/gnocksy/gnocksy.c index faccfa6d2..49b7d72bd 100644 --- a/src/gns/gnocksy/gnocksy.c +++ b/src/gns/gnocksy/gnocksy.c @@ -26,6 +26,7 @@ #include #include "protocol.h" +#include "gns_glue.h" #define MAXEVENTS 64 @@ -40,12 +41,6 @@ static struct MHD_Daemon *mhd_daemon; static regex_t re_dotplus; -void -gns_glue_expand_and_shorten ( char* sorig, char* new ) -{ - memcpy (new, "foo.bar.gnunet", strlen("foo.bar.gnunet")); -} - static size_t curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls) { @@ -60,6 +55,7 @@ curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls) char* plusptr; char* p; char new_host[256]; + char to_exp[256]; uint64_t bytes_copied = 0; char new_buf[CURL_MAX_WRITE_SIZE+1]; @@ -103,13 +99,17 @@ curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls) if (m[1].rm_so != -1) { - hostptr = p+m[1].rm_eo; + hostptr = p+m[1].rm_so; if (DEBUG) printf ("Copying %d bytes.\n", (hostptr-p)); memcpy (br->MHD_CURL_BUF+bytes_copied, p, (hostptr-p)); bytes_copied += (hostptr-p); memset (new_host, 0, sizeof(new_host)); - gns_glue_expand_and_shorten ( br->full_url, + memset (to_exp, 0, sizeof (to_exp)); + memcpy (to_exp, hostptr, (m[1].rm_eo-m[1].rm_so)); + + gns_glue_expand_and_shorten ( to_exp, + br->host, new_host ); if (DEBUG) { @@ -366,7 +366,7 @@ mhd_content_cb (void* cls, return MHD_CONTENT_READER_END_OF_STREAM; } pthread_mutex_unlock ( &br->m_done ); - + pthread_mutex_lock ( &br->m_buf ); if ( br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_CURL ) { @@ -375,6 +375,8 @@ mhd_content_cb (void* cls, return 0; } + + if ( br->MHD_CURL_BUF_SIZE > max ) { printf("buffer in mhd response too small!\n"); @@ -389,6 +391,7 @@ mhd_content_cb (void* cls, memcpy ( buf, br->MHD_CURL_BUF, br->MHD_CURL_BUF_SIZE ); } br->MHD_CURL_BUF_STATUS = BUF_WAIT_FOR_CURL; + curl_easy_pause (br->curl, CURLPAUSE_CONT); pthread_mutex_unlock ( &br->m_buf ); return br->MHD_CURL_BUF_SIZE; diff --git a/src/gns/gnocksy/gns_glue.c b/src/gns/gnocksy/gns_glue.c new file mode 100644 index 000000000..25a6d0e83 --- /dev/null +++ b/src/gns/gnocksy/gns_glue.c @@ -0,0 +1,119 @@ +#include +#include + +int +gns_glue_get_auth ( char* name, char* auth ) +{ + char cmd[1024]; + char line[1024]; + FILE *p; + + sprintf (cmd, "%s %s", "gnunet-gns -a", name); + + p = popen(cmd, "r"); + + if (p != NULL) + { + while (fgets (line, sizeof(line), p) != NULL) + { + if (line[strlen(line)-1] == '\n') + { + line[strlen(line)-1] = '\0'; + strcpy (auth, line); + return 0; + } + } + + } + + fclose (p); + + return -1; +} + +int +gns_glue_shorten ( char* name, char* shortened ) +{ + char cmd[1024]; + char line[1024]; + FILE *p; + + sprintf (cmd, "%s %s", "gnunet-gns -r -s", name); + + p = popen(cmd, "r"); + + if (p != NULL) + { + while (fgets (line, sizeof(line), p) != NULL) + { + if (line[strlen(line)-1] == '\n') + { + line[strlen(line)-1] = '\0'; + strcpy (shortened, line); + return 0; + } + } + + } + + fclose (p); + + return -1; +} + +int +gns_glue_expand_and_shorten( char* to_expand, char* host, char* shortened ) +{ + char cmd[1024]; + char line[1024]; + FILE *p; + char sorig[256]; + char expanded[256]; + + sprintf (shortened, "%s%s", to_expand, host); //TODO this is a mockup + return 0; + + sprintf (cmd, "%s %s", "gnunet-gns -a", host); + + p = popen(cmd, "r"); + + if (p != NULL) + { + while (fgets (line, sizeof(line), p) != NULL) + { + if (line[strlen(line)-1] == '\n') + { + line[strlen(line)-1] = '\0'; + strcpy (sorig, line); + return 0; + } + } + + } + + fclose (p); + + sprintf (expanded, "%s.%s", to_expand, sorig); + + sprintf (cmd, "%s %s", "gnunet-gns -r -s", expanded); + + p = popen(cmd, "r"); + + if (p != NULL) + { + while (fgets (line, sizeof(line), p) != NULL) + { + if (line[strlen(line)-1] == '\n') + { + line[strlen(line)-1] = '\0'; + strcpy (shortened, line); + return 0; + } + } + + } + + fclose (p); + + return -1; +} diff --git a/src/gns/gnocksy/gns_glue.h b/src/gns/gnocksy/gns_glue.h new file mode 100644 index 000000000..403a1a973 --- /dev/null +++ b/src/gns/gnocksy/gns_glue.h @@ -0,0 +1,10 @@ +int +gns_glue_get_auth ( char* name, char* auth ); + +int +gns_glue_shorten ( char* name, char* shortened); + +int +gns_glue_expand_and_shorten (char* to_expand, + char* host, + char* shortened); -- 2.25.1