gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r21555 - gnunet/src/gns/gnocksy


From: gnunet
Subject: [GNUnet-SVN] r21555 - gnunet/src/gns/gnocksy
Date: Mon, 21 May 2012 02:04:35 +0200

Author: schanzen
Date: 2012-05-21 02:04:35 +0200 (Mon, 21 May 2012)
New Revision: 21555

Modified:
   gnunet/src/gns/gnocksy/gnocksy.c
   gnunet/src/gns/gnocksy/gns_glue.c
   gnunet/src/gns/gnocksy/gns_glue.h
   gnunet/src/gns/gnocksy/protocol.h
Log:
- more


Modified: gnunet/src/gns/gnocksy/gnocksy.c
===================================================================
--- gnunet/src/gns/gnocksy/gnocksy.c    2012-05-20 16:12:23 UTC (rev 21554)
+++ gnunet/src/gns/gnocksy/gnocksy.c    2012-05-21 00:04:35 UTC (rev 21555)
@@ -8,6 +8,11 @@
  * Note: Only supports addr type 3 (domain) for now.
  * Chrome uses it automatically
  * For FF: about:config -> network.proxy.socks_remote_dns true
+ *
+ * TODO
+ * - zkey shorten
+ * - LEHO replacement and glue
+ * - SSL
  */
 
 #include <stdlib.h>
@@ -30,7 +35,8 @@
 
 #define MAXEVENTS 64
 
-#define DEBUG 1
+#define DEBUG 0
+#define VERBOSE 1
 
 #define HTML_HDR_CONTENT "Content-Type: text/html\r\n"
 
@@ -38,6 +44,9 @@
 
 #define RE_N_MATCHES 4
 
+#define HTTP_PORT 80
+#define HTTPS_PORT 443
+
 static struct MHD_Daemon *mhd_daemon;
 static regex_t re_dotplus;
 
@@ -65,7 +74,6 @@
   if (br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_MHD)
   {
     pthread_mutex_unlock ( &br->m_buf );
-    printf( "waiting for mhd to process data... pausing curl\n");
     return CURL_WRITEFUNC_PAUSE;
   }
 
@@ -83,10 +91,12 @@
 
       if (nomatch)
       {
-        printf ("No more matches\n");
+        if (DEBUG)
+          printf ("No more matches\n");
         if ((p-new_buf) < 0)
         {
-          printf ("Error p<buf!\n");
+          if (DEBUG)
+            printf ("Error p<buf!\n");
           break;
         }
         memcpy ( br->MHD_CURL_BUF+bytes_copied, p, bytes-(p-new_buf));
@@ -121,7 +131,8 @@
         bytes_copied += strlen (new_host);
         p += m[3].rm_so+1;
 
-        printf ("Done. Next in %d bytes\n", m[3].rm_so);
+        if (DEBUG)
+          printf ("Done. Next in %d bytes\n", m[3].rm_so);
 
         //TODO check buf lenghts!
       }
@@ -140,7 +151,8 @@
 
 
   //MHD_destroy_response (response);
-  printf( "buffer: %s\n", (char*)br->MHD_CURL_BUF );
+  if (DEBUG)
+    printf( "buffer:\n%s\n", (char*)br->MHD_CURL_BUF );
   return bytes;
 }
 
@@ -153,9 +165,10 @@
 
   memcpy(hdr, buffer, bytes);
   hdr[bytes] = '\0';
+  
+  if (DEBUG)
+    printf ("got hdr: %s", hdr);
 
-  printf ("got hdr: %s\n", hdr);
-
   if (0 == strcmp(hdr, HTML_HDR_CONTENT))
     br->res_is_html = 1;
 
@@ -288,8 +301,9 @@
   srv_addr.sin_family = AF_INET;
   srv_addr.sin_addr.s_addr = srv_ip;
   srv_addr.sin_port = srv_port;
-  printf("target server: %s:%u\n", inet_ntoa(srv_addr.sin_addr), 
-         ntohs(srv_port));
+  if (DEBUG)
+    printf("target server: %s:%u\n", inet_ntoa(srv_addr.sin_addr), 
+           ntohs(srv_port));
 
   if (connect (conn_fd, (struct sockaddr*)&srv_addr,
                sizeof (struct sockaddr)) < 0)
@@ -370,7 +384,6 @@
   pthread_mutex_lock ( &br->m_buf );
   if ( br->MHD_CURL_BUF_STATUS == BUF_WAIT_FOR_CURL )
   {
-    printf("waiting for curl...\n");
     pthread_mutex_unlock ( &br->m_buf );
     return 0;
   }
@@ -486,6 +499,9 @@
   socklen_t in_len;
   int infd;
   char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
+
+  /* port offset for ssl daemons */
+  int i_ssl = 1;
   
   int done;
   ssize_t count;
@@ -627,6 +643,8 @@
         while (1)
         {
 
+          memset (buf, 0, sizeof (buf));
+
           count = read (br->fd, buf, sizeof (buf));
 
           if (count == -1)
@@ -659,6 +677,8 @@
           
           if (br->status == SOCKS5_INIT)
           {
+            if (DEBUG)
+              printf ("SOCKS5 init for %d\n", br->fd);
             hello.version = 0x05;
             hello.auth_method = 0;
             write (br->fd, &hello, sizeof (hello));
@@ -677,37 +697,81 @@
               strncpy(domain, (char*)(&(req->addr_type) + 2), dom_len);
               req_port = *((uint16_t*)(&(req->addr_type) + 2 + dom_len));
 
+              if (DEBUG)
+                printf ("Requested connection is %s:%d\n",
+                        domain,
+                        ntohs(req_port));
+
               phost = (struct hostent*)gethostbyname (domain);
               if (phost == NULL)
               {
-                printf ("Resolve %s error!\n" , domain );
+                if (VERBOSE)
+                  printf ("Resolve %s error!\n" , domain );
                 resp.version = 0x05;
                 resp.reply = 0x01;
                 write (br->fd, &resp, sizeof (struct socks5_server_response));
                 break;
               }
 
+              if (DEBUG)
+                printf ("trying to add %d to MHD\n", br->fd);
+              
               if ( -1 != is_tld (domain, ".gnunet") )
               {
                 strcpy (br->host, domain);
-                if (NULL == mhd_daemon)
+                if (HTTP_PORT == ntohs(req_port))
                 {
-                  mhd_daemon = MHD_start_daemon( MHD_USE_THREAD_PER_CONNECTION,
-                                                 8080,
-                                                 &access_cb, br,
-                                                 &accept_cb, br,
-                                                 MHD_OPTION_END);
+                  br->use_ssl = 0;
+                  if (NULL == mhd_daemon)
+                  {
+                    mhd_daemon =
+                      MHD_start_daemon( MHD_USE_THREAD_PER_CONNECTION,
+                                        8080,
+                                        &access_cb, br,
+                                        &accept_cb, br,
+                                        MHD_OPTION_END);
+                  }
                   
+                  if (MHD_YES != MHD_add_connection (mhd_daemon,
+                                                     br->fd,
+                                                     &br->addr,
+                                                     br->addr_len))
+                  {
+                    if (VERBOSE)
+                      printf ("Error adding %d to mhd\n", br->fd);
+                  }
                 }
-                
-                printf ("trying to add to MHD\n");
-                if (MHD_YES != MHD_add_connection (mhd_daemon,
-                                                   br->fd,
-                                                   &br->addr,
-                                                   br->addr_len))
+
+                if (HTTPS_PORT == ntohs(req_port))
                 {
-                  printf ("Error adding %d to mhd\n", br->fd);
+                  /*
+                   * custom daemon for SSL requests
+                   * TODO make more efficient with
+                   * per name SSL daemons?
+                   */
+                  br->use_ssl = 1;
+                  br->ssl_daemon = 
+                    MHD_start_daemon( MHD_USE_THREAD_PER_CONNECTION |
+                                      MHD_USE_SSL,
+                                      8080+i_ssl,
+                                      NULL, NULL,
+                                      &accept_cb, br,
+                                      MHD_OPTION_HTTPS_MEM_KEY, NULL,
+                                      MHD_OPTION_HTTPS_MEM_CERT, NULL,
+                                      MHD_OPTION_END);
+                  
+                  i_ssl++;
+                  
+                  if (MHD_YES != MHD_add_connection (br->ssl_daemon,
+                                                     br->fd,
+                                                     &br->addr,
+                                                     br->addr_len))
+                  {
+                    if (VERBOSE)
+                      printf ("Error adding %d to mhd\n", br->fd);
+                  }
                 }
+
                 
                 event.events = EPOLLIN | EPOLLET;
                 epoll_ctl (efd, EPOLL_CTL_DEL, br->fd, &event);
@@ -716,43 +780,51 @@
                 resp.reserved = 0x00;
                 resp.addr_type = 0x01;
                 write (br->fd, &resp, 10);
-                break;
               }
+              else
+              {
 
-              conn_fd = connect_to_domain (phost, req_port);
+                conn_fd = connect_to_domain (phost, req_port);
               
-              if (-1 == conn_fd)
-              {
+                if (-1 == conn_fd)
+                {
+                  if (VERBOSE)
+                    printf("cannot create remote connection from %d to 
%s:%d\n",
+                           br->fd, domain, ntohs(req_port));
                   resp.version = 0x05;
                   resp.reply = 0x01;
                   write (br->fd, &resp, 10);
-              }
-              else
-              {
-                if (DEBUG)
-                  printf("new remote connection %d to %d\n", br->fd, conn_fd);
-                resp.version = 0x05;
-                resp.reply = 0x00;
-                resp.reserved = 0x00;
-                resp.addr_type = 0x01;
+                }
+                else
+                {
+                  if (VERBOSE)
+                    printf("new remote connection %d to %d\n", br->fd, 
conn_fd);
+                  resp.version = 0x05;
+                  resp.reply = 0x00;
+                  resp.reserved = 0x00;
+                  resp.addr_type = 0x01;
                 
-                new_br = malloc (sizeof (struct socks5_bridge));
-                br->remote_end = new_br;
-                br->status = SOCKS5_DATA_TRANSFER;
-                new_br->fd = conn_fd;
-                new_br->remote_end = br;
-                new_br->status = SOCKS5_DATA_TRANSFER;
+                  new_br = malloc (sizeof (struct socks5_bridge));
+                  if (br->remote_end != NULL)
+                    printf ("WARNING remote end was not NULL!\n");
+                  br->remote_end = new_br;
+                  br->status = SOCKS5_DATA_TRANSFER;
+                  new_br->fd = conn_fd;
+                  new_br->remote_end = br;
+                  new_br->status = SOCKS5_DATA_TRANSFER;
 
-                event.data.ptr = new_br;
-                event.events = EPOLLIN | EPOLLET;
-                epoll_ctl (efd, EPOLL_CTL_ADD, conn_fd, &event);
-                write (br->fd, &resp, 10);
+                  event.data.ptr = new_br;
+                  event.events = EPOLLIN | EPOLLET;
+                  epoll_ctl (efd, EPOLL_CTL_ADD, conn_fd, &event);
+                  write (br->fd, &resp, 10);
+                }
               }
 
             }
             else
             {
-              printf("not implemented address type %02X\n", 
(int)req->addr_type);
+              if (DEBUG)
+                printf("not implemented address type %02X\n", 
(int)req->addr_type);
             }
           }
           
@@ -766,14 +838,14 @@
 
         if (done)
         {
-          close (br->fd);
+          //close (br->fd);
 
           if (br->remote_end)
           {
-            close (br->remote_end->fd);
-            free(br->remote_end);
+            //close (br->remote_end->fd);
+            //free(br->remote_end);
           }
-          free(br);
+          //free(br);
         }
       }
     }

Modified: gnunet/src/gns/gnocksy/gns_glue.c
===================================================================
--- gnunet/src/gns/gnocksy/gns_glue.c   2012-05-20 16:12:23 UTC (rev 21554)
+++ gnunet/src/gns/gnocksy/gns_glue.c   2012-05-21 00:04:35 UTC (rev 21555)
@@ -1,6 +1,14 @@
 #include <stdio.h>
 #include <string.h>
 
+/*
+ * Glue function to return the authoritative part
+ * of a name. i.e. the site of origin
+ *
+ * @param name the name to process
+ * @param auth pointer where the result is stored
+ * @return 0 on success < 0 on failure
+ */
 int
 gns_glue_get_auth ( char* name, char* auth )
 {
@@ -31,6 +39,14 @@
   return -1;
 }
 
+/*
+ * Glue function to return the short version of
+ * a given name
+ *
+ * @param name the name to shorten
+ * @param shortened pointer where the result will be stored
+ * @return 0 on success < 0 on failure
+ */
 int
 gns_glue_shorten ( char* name, char* shortened )
 {
@@ -61,6 +77,15 @@
   return -1;
 }
 
+
+/*
+ * Glue function to expand .+ urls and shorted the
+ * resulting name
+ *
+ * @param to_expand the .+ name to expand
+ * @param host the site of origin
+ * @param shortened the expanded and shortened result pointer
+ */
 int
 gns_glue_expand_and_shorten( char* to_expand, char* host, char* shortened )
 {

Modified: gnunet/src/gns/gnocksy/gns_glue.h
===================================================================
--- gnunet/src/gns/gnocksy/gns_glue.h   2012-05-20 16:12:23 UTC (rev 21554)
+++ gnunet/src/gns/gnocksy/gns_glue.h   2012-05-21 00:04:35 UTC (rev 21555)
@@ -1,9 +1,33 @@
+/*
+ * Glue function to return the authoritative part
+ * of a name. i.e. the site of origin
+ *
+ * @param name the name to process
+ * @param auth pointer where the result is stored
+ * @return 0 on success < 0 on failure
+ */
 int
 gns_glue_get_auth ( char* name, char* auth );
 
+/*
+ * Glue function to return the short version of
+ * a given name
+ *
+ * @param name the name to shorten
+ * @param shortened pointer where the result will be stored
+ * @return 0 on success < 0 on failure
+ */
 int
 gns_glue_shorten ( char* name, char* shortened);
 
+/*
+ * Glue function to expand .+ urls and shorted the
+ * resulting name
+ *
+ * @param to_expand the .+ name to expand
+ * @param host the site of origin
+ * @param shortened the expanded and shortened result pointer
+ */
 int
 gns_glue_expand_and_shorten (char* to_expand,
                              char* host,

Modified: gnunet/src/gns/gnocksy/protocol.h
===================================================================
--- gnunet/src/gns/gnocksy/protocol.h   2012-05-20 16:12:23 UTC (rev 21554)
+++ gnunet/src/gns/gnocksy/protocol.h   2012-05-21 00:04:35 UTC (rev 21555)
@@ -50,6 +50,12 @@
   socklen_t addr_len;
   char host[256];
   int status;
+
+  /* This is an ssl bridge? */
+  int use_ssl;
+
+  /* if use_ssl=1 we have a daemon associated */
+  struct MHD_Daemon *ssl_daemon;
   
   /* http url + host */
   char* full_url;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]