aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2012-05-21 00:04:35 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2012-05-21 00:04:35 +0000
commit414b0984713f8fa01ae4f8729e31c7fa15bf3773 (patch)
tree06179d76fca8d6ec07e3ec1c1acea65a8708a02c /src
parentfd98a44c4a4b6b6661034ab1a0100939700df769 (diff)
downloadgnunet-414b0984713f8fa01ae4f8729e31c7fa15bf3773.tar.gz
gnunet-414b0984713f8fa01ae4f8729e31c7fa15bf3773.zip
- more
Diffstat (limited to 'src')
-rw-r--r--src/gns/gnocksy/gnocksy.c180
-rw-r--r--src/gns/gnocksy/gns_glue.c25
-rw-r--r--src/gns/gnocksy/gns_glue.h24
-rw-r--r--src/gns/gnocksy/protocol.h6
4 files changed, 181 insertions, 54 deletions
diff --git a/src/gns/gnocksy/gnocksy.c b/src/gns/gnocksy/gnocksy.c
index 49b7d72bd..df5f06940 100644
--- a/src/gns/gnocksy/gnocksy.c
+++ b/src/gns/gnocksy/gnocksy.c
@@ -8,6 +8,11 @@
8 * Note: Only supports addr type 3 (domain) for now. 8 * Note: Only supports addr type 3 (domain) for now.
9 * Chrome uses it automatically 9 * Chrome uses it automatically
10 * For FF: about:config -> network.proxy.socks_remote_dns true 10 * For FF: about:config -> network.proxy.socks_remote_dns true
11 *
12 * TODO
13 * - zkey shorten
14 * - LEHO replacement and glue
15 * - SSL
11 */ 16 */
12 17
13#include <stdlib.h> 18#include <stdlib.h>
@@ -30,7 +35,8 @@
30 35
31#define MAXEVENTS 64 36#define MAXEVENTS 64
32 37
33#define DEBUG 1 38#define DEBUG 0
39#define VERBOSE 1
34 40
35#define HTML_HDR_CONTENT "Content-Type: text/html\r\n" 41#define HTML_HDR_CONTENT "Content-Type: text/html\r\n"
36 42
@@ -38,6 +44,9 @@
38 44
39#define RE_N_MATCHES 4 45#define RE_N_MATCHES 4
40 46
47#define HTTP_PORT 80
48#define HTTPS_PORT 443
49
41static struct MHD_Daemon *mhd_daemon; 50static struct MHD_Daemon *mhd_daemon;
42static regex_t re_dotplus; 51static regex_t re_dotplus;
43 52
@@ -65,7 +74,6 @@ curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls)
65 if (br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_MHD) 74 if (br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_MHD)
66 { 75 {
67 pthread_mutex_unlock ( &br->m_buf ); 76 pthread_mutex_unlock ( &br->m_buf );
68 printf( "waiting for mhd to process data... pausing curl\n");
69 return CURL_WRITEFUNC_PAUSE; 77 return CURL_WRITEFUNC_PAUSE;
70 } 78 }
71 79
@@ -83,10 +91,12 @@ curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls)
83 91
84 if (nomatch) 92 if (nomatch)
85 { 93 {
86 printf ("No more matches\n"); 94 if (DEBUG)
95 printf ("No more matches\n");
87 if ((p-new_buf) < 0) 96 if ((p-new_buf) < 0)
88 { 97 {
89 printf ("Error p<buf!\n"); 98 if (DEBUG)
99 printf ("Error p<buf!\n");
90 break; 100 break;
91 } 101 }
92 memcpy ( br->MHD_CURL_BUF+bytes_copied, p, bytes-(p-new_buf)); 102 memcpy ( br->MHD_CURL_BUF+bytes_copied, p, bytes-(p-new_buf));
@@ -121,7 +131,8 @@ curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls)
121 bytes_copied += strlen (new_host); 131 bytes_copied += strlen (new_host);
122 p += m[3].rm_so+1; 132 p += m[3].rm_so+1;
123 133
124 printf ("Done. Next in %d bytes\n", m[3].rm_so); 134 if (DEBUG)
135 printf ("Done. Next in %d bytes\n", m[3].rm_so);
125 136
126 //TODO check buf lenghts! 137 //TODO check buf lenghts!
127 } 138 }
@@ -140,7 +151,8 @@ curl_write_data (void *buffer, size_t size, size_t nmemb, void* cls)
140 151
141 152
142 //MHD_destroy_response (response); 153 //MHD_destroy_response (response);
143 printf( "buffer: %s\n", (char*)br->MHD_CURL_BUF ); 154 if (DEBUG)
155 printf( "buffer:\n%s\n", (char*)br->MHD_CURL_BUF );
144 return bytes; 156 return bytes;
145} 157}
146 158
@@ -153,8 +165,9 @@ curl_check_hdr (void *buffer, size_t size, size_t nmemb, void* cls)
153 165
154 memcpy(hdr, buffer, bytes); 166 memcpy(hdr, buffer, bytes);
155 hdr[bytes] = '\0'; 167 hdr[bytes] = '\0';
156 168
157 printf ("got hdr: %s\n", hdr); 169 if (DEBUG)
170 printf ("got hdr: %s", hdr);
158 171
159 if (0 == strcmp(hdr, HTML_HDR_CONTENT)) 172 if (0 == strcmp(hdr, HTML_HDR_CONTENT))
160 br->res_is_html = 1; 173 br->res_is_html = 1;
@@ -288,8 +301,9 @@ connect_to_domain (struct hostent* phost, uint16_t srv_port)
288 srv_addr.sin_family = AF_INET; 301 srv_addr.sin_family = AF_INET;
289 srv_addr.sin_addr.s_addr = srv_ip; 302 srv_addr.sin_addr.s_addr = srv_ip;
290 srv_addr.sin_port = srv_port; 303 srv_addr.sin_port = srv_port;
291 printf("target server: %s:%u\n", inet_ntoa(srv_addr.sin_addr), 304 if (DEBUG)
292 ntohs(srv_port)); 305 printf("target server: %s:%u\n", inet_ntoa(srv_addr.sin_addr),
306 ntohs(srv_port));
293 307
294 if (connect (conn_fd, (struct sockaddr*)&srv_addr, 308 if (connect (conn_fd, (struct sockaddr*)&srv_addr,
295 sizeof (struct sockaddr)) < 0) 309 sizeof (struct sockaddr)) < 0)
@@ -370,7 +384,6 @@ mhd_content_cb (void* cls,
370 pthread_mutex_lock ( &br->m_buf ); 384 pthread_mutex_lock ( &br->m_buf );
371 if ( br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_CURL ) 385 if ( br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_CURL )
372 { 386 {
373 printf("waiting for curl...\n");
374 pthread_mutex_unlock ( &br->m_buf ); 387 pthread_mutex_unlock ( &br->m_buf );
375 return 0; 388 return 0;
376 } 389 }
@@ -486,6 +499,9 @@ int main ( int argc, char *argv[] )
486 socklen_t in_len; 499 socklen_t in_len;
487 int infd; 500 int infd;
488 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; 501 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
502
503 /* port offset for ssl daemons */
504 int i_ssl = 1;
489 505
490 int done; 506 int done;
491 ssize_t count; 507 ssize_t count;
@@ -627,6 +643,8 @@ int main ( int argc, char *argv[] )
627 while (1) 643 while (1)
628 { 644 {
629 645
646 memset (buf, 0, sizeof (buf));
647
630 count = read (br->fd, buf, sizeof (buf)); 648 count = read (br->fd, buf, sizeof (buf));
631 649
632 if (count == -1) 650 if (count == -1)
@@ -659,6 +677,8 @@ int main ( int argc, char *argv[] )
659 677
660 if (br->status == SOCKS5_INIT) 678 if (br->status == SOCKS5_INIT)
661 { 679 {
680 if (DEBUG)
681 printf ("SOCKS5 init for %d\n", br->fd);
662 hello.version = 0x05; 682 hello.version = 0x05;
663 hello.auth_method = 0; 683 hello.auth_method = 0;
664 write (br->fd, &hello, sizeof (hello)); 684 write (br->fd, &hello, sizeof (hello));
@@ -677,37 +697,81 @@ int main ( int argc, char *argv[] )
677 strncpy(domain, (char*)(&(req->addr_type) + 2), dom_len); 697 strncpy(domain, (char*)(&(req->addr_type) + 2), dom_len);
678 req_port = *((uint16_t*)(&(req->addr_type) + 2 + dom_len)); 698 req_port = *((uint16_t*)(&(req->addr_type) + 2 + dom_len));
679 699
700 if (DEBUG)
701 printf ("Requested connection is %s:%d\n",
702 domain,
703 ntohs(req_port));
704
680 phost = (struct hostent*)gethostbyname (domain); 705 phost = (struct hostent*)gethostbyname (domain);
681 if (phost == NULL) 706 if (phost == NULL)
682 { 707 {
683 printf ("Resolve %s error!\n" , domain ); 708 if (VERBOSE)
709 printf ("Resolve %s error!\n" , domain );
684 resp.version = 0x05; 710 resp.version = 0x05;
685 resp.reply = 0x01; 711 resp.reply = 0x01;
686 write (br->fd, &resp, sizeof (struct socks5_server_response)); 712 write (br->fd, &resp, sizeof (struct socks5_server_response));
687 break; 713 break;
688 } 714 }
689 715
716 if (DEBUG)
717 printf ("trying to add %d to MHD\n", br->fd);
718
690 if ( -1 != is_tld (domain, ".gnunet") ) 719 if ( -1 != is_tld (domain, ".gnunet") )
691 { 720 {
692 strcpy (br->host, domain); 721 strcpy (br->host, domain);
693 if (NULL == mhd_daemon) 722 if (HTTP_PORT == ntohs(req_port))
694 { 723 {
695 mhd_daemon = MHD_start_daemon( MHD_USE_THREAD_PER_CONNECTION, 724 br->use_ssl = 0;
696 8080, 725 if (NULL == mhd_daemon)
697 &access_cb, br, 726 {
698 &accept_cb, br, 727 mhd_daemon =
699 MHD_OPTION_END); 728 MHD_start_daemon( MHD_USE_THREAD_PER_CONNECTION,
729 8080,
730 &access_cb, br,
731 &accept_cb, br,
732 MHD_OPTION_END);
733 }
700 734
735 if (MHD_YES != MHD_add_connection (mhd_daemon,
736 br->fd,
737 &br->addr,
738 br->addr_len))
739 {
740 if (VERBOSE)
741 printf ("Error adding %d to mhd\n", br->fd);
742 }
701 } 743 }
702 744
703 printf ("trying to add to MHD\n"); 745 if (HTTPS_PORT == ntohs(req_port))
704 if (MHD_YES != MHD_add_connection (mhd_daemon,
705 br->fd,
706 &br->addr,
707 br->addr_len))
708 { 746 {
709 printf ("Error adding %d to mhd\n", br->fd); 747 /*
748 * custom daemon for SSL requests
749 * TODO make more efficient with
750 * per name SSL daemons?
751 */
752 br->use_ssl = 1;
753 br->ssl_daemon =
754 MHD_start_daemon( MHD_USE_THREAD_PER_CONNECTION |
755 MHD_USE_SSL,
756 8080+i_ssl,
757 NULL, NULL,
758 &accept_cb, br,
759 MHD_OPTION_HTTPS_MEM_KEY, NULL,
760 MHD_OPTION_HTTPS_MEM_CERT, NULL,
761 MHD_OPTION_END);
762
763 i_ssl++;
764
765 if (MHD_YES != MHD_add_connection (br->ssl_daemon,
766 br->fd,
767 &br->addr,
768 br->addr_len))
769 {
770 if (VERBOSE)
771 printf ("Error adding %d to mhd\n", br->fd);
772 }
710 } 773 }
774
711 775
712 event.events = EPOLLIN | EPOLLET; 776 event.events = EPOLLIN | EPOLLET;
713 epoll_ctl (efd, EPOLL_CTL_DEL, br->fd, &event); 777 epoll_ctl (efd, EPOLL_CTL_DEL, br->fd, &event);
@@ -716,43 +780,51 @@ int main ( int argc, char *argv[] )
716 resp.reserved = 0x00; 780 resp.reserved = 0x00;
717 resp.addr_type = 0x01; 781 resp.addr_type = 0x01;
718 write (br->fd, &resp, 10); 782 write (br->fd, &resp, 10);
719 break;
720 } 783 }
784 else
785 {
721 786
722 conn_fd = connect_to_domain (phost, req_port); 787 conn_fd = connect_to_domain (phost, req_port);
723 788
724 if (-1 == conn_fd) 789 if (-1 == conn_fd)
725 { 790 {
791 if (VERBOSE)
792 printf("cannot create remote connection from %d to %s:%d\n",
793 br->fd, domain, ntohs(req_port));
726 resp.version = 0x05; 794 resp.version = 0x05;
727 resp.reply = 0x01; 795 resp.reply = 0x01;
728 write (br->fd, &resp, 10); 796 write (br->fd, &resp, 10);
729 } 797 }
730 else 798 else
731 { 799 {
732 if (DEBUG) 800 if (VERBOSE)
733 printf("new remote connection %d to %d\n", br->fd, conn_fd); 801 printf("new remote connection %d to %d\n", br->fd, conn_fd);
734 resp.version = 0x05; 802 resp.version = 0x05;
735 resp.reply = 0x00; 803 resp.reply = 0x00;
736 resp.reserved = 0x00; 804 resp.reserved = 0x00;
737 resp.addr_type = 0x01; 805 resp.addr_type = 0x01;
738 806
739 new_br = malloc (sizeof (struct socks5_bridge)); 807 new_br = malloc (sizeof (struct socks5_bridge));
740 br->remote_end = new_br; 808 if (br->remote_end != NULL)
741 br->status = SOCKS5_DATA_TRANSFER; 809 printf ("WARNING remote end was not NULL!\n");
742 new_br->fd = conn_fd; 810 br->remote_end = new_br;
743 new_br->remote_end = br; 811 br->status = SOCKS5_DATA_TRANSFER;
744 new_br->status = SOCKS5_DATA_TRANSFER; 812 new_br->fd = conn_fd;
745 813 new_br->remote_end = br;
746 event.data.ptr = new_br; 814 new_br->status = SOCKS5_DATA_TRANSFER;
747 event.events = EPOLLIN | EPOLLET; 815
748 epoll_ctl (efd, EPOLL_CTL_ADD, conn_fd, &event); 816 event.data.ptr = new_br;
749 write (br->fd, &resp, 10); 817 event.events = EPOLLIN | EPOLLET;
818 epoll_ctl (efd, EPOLL_CTL_ADD, conn_fd, &event);
819 write (br->fd, &resp, 10);
820 }
750 } 821 }
751 822
752 } 823 }
753 else 824 else
754 { 825 {
755 printf("not implemented address type %02X\n", (int)req->addr_type); 826 if (DEBUG)
827 printf("not implemented address type %02X\n", (int)req->addr_type);
756 } 828 }
757 } 829 }
758 830
@@ -766,14 +838,14 @@ int main ( int argc, char *argv[] )
766 838
767 if (done) 839 if (done)
768 { 840 {
769 close (br->fd); 841 //close (br->fd);
770 842
771 if (br->remote_end) 843 if (br->remote_end)
772 { 844 {
773 close (br->remote_end->fd); 845 //close (br->remote_end->fd);
774 free(br->remote_end); 846 //free(br->remote_end);
775 } 847 }
776 free(br); 848 //free(br);
777 } 849 }
778 } 850 }
779 } 851 }
diff --git a/src/gns/gnocksy/gns_glue.c b/src/gns/gnocksy/gns_glue.c
index 25a6d0e83..54e6916fc 100644
--- a/src/gns/gnocksy/gns_glue.c
+++ b/src/gns/gnocksy/gns_glue.c
@@ -1,6 +1,14 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <string.h> 2#include <string.h>
3 3
4/*
5 * Glue function to return the authoritative part
6 * of a name. i.e. the site of origin
7 *
8 * @param name the name to process
9 * @param auth pointer where the result is stored
10 * @return 0 on success < 0 on failure
11 */
4int 12int
5gns_glue_get_auth ( char* name, char* auth ) 13gns_glue_get_auth ( char* name, char* auth )
6{ 14{
@@ -31,6 +39,14 @@ gns_glue_get_auth ( char* name, char* auth )
31 return -1; 39 return -1;
32} 40}
33 41
42/*
43 * Glue function to return the short version of
44 * a given name
45 *
46 * @param name the name to shorten
47 * @param shortened pointer where the result will be stored
48 * @return 0 on success < 0 on failure
49 */
34int 50int
35gns_glue_shorten ( char* name, char* shortened ) 51gns_glue_shorten ( char* name, char* shortened )
36{ 52{
@@ -61,6 +77,15 @@ gns_glue_shorten ( char* name, char* shortened )
61 return -1; 77 return -1;
62} 78}
63 79
80
81/*
82 * Glue function to expand .+ urls and shorted the
83 * resulting name
84 *
85 * @param to_expand the .+ name to expand
86 * @param host the site of origin
87 * @param shortened the expanded and shortened result pointer
88 */
64int 89int
65gns_glue_expand_and_shorten( char* to_expand, char* host, char* shortened ) 90gns_glue_expand_and_shorten( char* to_expand, char* host, char* shortened )
66{ 91{
diff --git a/src/gns/gnocksy/gns_glue.h b/src/gns/gnocksy/gns_glue.h
index 403a1a973..7a872d683 100644
--- a/src/gns/gnocksy/gns_glue.h
+++ b/src/gns/gnocksy/gns_glue.h
@@ -1,9 +1,33 @@
1/*
2 * Glue function to return the authoritative part
3 * of a name. i.e. the site of origin
4 *
5 * @param name the name to process
6 * @param auth pointer where the result is stored
7 * @return 0 on success < 0 on failure
8 */
1int 9int
2gns_glue_get_auth ( char* name, char* auth ); 10gns_glue_get_auth ( char* name, char* auth );
3 11
12/*
13 * Glue function to return the short version of
14 * a given name
15 *
16 * @param name the name to shorten
17 * @param shortened pointer where the result will be stored
18 * @return 0 on success < 0 on failure
19 */
4int 20int
5gns_glue_shorten ( char* name, char* shortened); 21gns_glue_shorten ( char* name, char* shortened);
6 22
23/*
24 * Glue function to expand .+ urls and shorted the
25 * resulting name
26 *
27 * @param to_expand the .+ name to expand
28 * @param host the site of origin
29 * @param shortened the expanded and shortened result pointer
30 */
7int 31int
8gns_glue_expand_and_shorten (char* to_expand, 32gns_glue_expand_and_shorten (char* to_expand,
9 char* host, 33 char* host,
diff --git a/src/gns/gnocksy/protocol.h b/src/gns/gnocksy/protocol.h
index 4f7e23675..a6f22a64b 100644
--- a/src/gns/gnocksy/protocol.h
+++ b/src/gns/gnocksy/protocol.h
@@ -50,6 +50,12 @@ struct socks5_bridge
50 socklen_t addr_len; 50 socklen_t addr_len;
51 char host[256]; 51 char host[256];
52 int status; 52 int status;
53
54 /* This is an ssl bridge? */
55 int use_ssl;
56
57 /* if use_ssl=1 we have a daemon associated */
58 struct MHD_Daemon *ssl_daemon;
53 59
54 /* http url + host */ 60 /* http url + host */
55 char* full_url; 61 char* full_url;