Fix perf_crypto_rsa.c after various changes
[oweals/gnunet.git] / src / util / socks.c
index d54a086f936bea6365069592354624419b44a1f3..bda8765ad9a4c18863db2be5bda7a21d459976ed 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009-2013 Jeffrey Burdges (and other contributing authors)
+     Copyright (C) 2009-2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -105,7 +105,7 @@ unsigned char * SOCK5_proto_string(unsigned char * b, const char * s)
 /**
  * State of the SOCKS5 handshake.
  */
-struct GNUNET_SOCKS_Handshake 
+struct GNUNET_SOCKS_Handshake
 {
 
   /**
@@ -124,7 +124,7 @@ struct GNUNET_SOCKS_Handshake
   struct GNUNET_CONNECTION_TransmitHandle *th;
 
   /**
-   * Our stage in the SOCKS5 handshake 
+   * Our stage in the SOCKS5 handshake
    */
   int step;
 
@@ -199,7 +199,7 @@ SOCKS5_handshake_step (struct GNUNET_SOCKS_Handshake *ih)
   GNUNET_assert (SOCKS5_step_done > ih->step && ih->step >= 0);
   switch (ih->step) {
     case SOCKS5_step_greet:  /* SOCKS5 server's greeting */
-      if (b[0] != 5) 
+      if (b[0] != 5)
       {
         LOG (GNUNET_ERROR_TYPE_ERROR,
              "Not a SOCKS5 server\n");
@@ -234,7 +234,7 @@ SOCKS5_handshake_step (struct GNUNET_SOCKS_Handshake *ih)
       b += 2;
       break;
     case SOCKS5_step_cmd:  /* SOCKS5 server's responce to command */
-      if (b[0] != 5) 
+      if (b[0] != 5)
       {
         LOG (GNUNET_ERROR_TYPE_ERROR,
              "SOCKS5 protocol error\n");
@@ -271,12 +271,11 @@ SOCKS5_handshake_step (struct GNUNET_SOCKS_Handshake *ih)
       ih->instart = b;
       SOCKS5_handshake_done (ih);
       return;
-    case SOCKS5_step_done: 
+    case SOCKS5_step_done:
       GNUNET_assert (0);
   }
-  ++ih->step;
   ih->instart = b;
-  /* Do not reschedule the sender unless we're done reading. 
+  /* Do not reschedule the sender unless we're done reading.
    * I imagine this lets us avoid ever cancelling the transmit handle. */
   register_sender (ih);
 }
@@ -290,12 +289,11 @@ SOCKS5_handshake_step (struct GNUNET_SOCKS_Handshake *ih)
  * @param handler_cls closure for @a handler
  */
 void
-reciever (void *cls, 
+reciever (void *cls,
           const void *buf, size_t available,
           const struct sockaddr * addr,
           socklen_t addrlen, int errCode)
 {
-printf("Meow(%d)",available);
   struct GNUNET_SOCKS_Handshake * ih = cls;
   GNUNET_assert (&ih->inend[available] < &ih->inbuf[1024]);
   memcpy(ih->inend, buf, available);
@@ -314,7 +312,6 @@ printf("Meow(%d)",available);
 void
 register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want)
 {
-  printf("register_reciever on step %u for %d bytes.\n", ih->step, want );
   GNUNET_CONNECTION_receive (ih->socks5_connection,
                              want,
                              GNUNET_TIME_relative_get_minute_ (),
@@ -331,9 +328,8 @@ register_reciever (struct GNUNET_SOCKS_Handshake *ih, int want)
  * @param buf where the callee should write the message
  * @return number of bytes written to @a buf
  */
-
 size_t
-transmit_ready (void *cls, 
+transmit_ready (void *cls,
                 size_t size,
                 void *buf)
 {
@@ -348,38 +344,29 @@ transmit_ready (void *cls,
    * GNUNET_CONNECTION_notify_transmit_ready() can schedule :
    *   transmit_timeout() - DNS still working
    *   connect_error() - DNS done but no socket?
-   * transmit_ready() - scheduler shutdown or timeout, or signal_transmit_error() 
+   * transmit_ready() - scheduler shutdown or timeout, or signal_transmit_error()
    * We'd need to dig into the scheduler to guess at the reason, as
    * connection.c tells us nothing itself, but mostly its timouts.
    * Initially, we'll simply ignore this and leave massive timeouts, but
    * maybe that should change for error handling pruposes.  It appears that
    * successful operations, including DNS resolution, do not use this.  */
-  if (NULL==buf)
+  if (NULL == buf)
   {
-    enum GNUNET_SCHEDULER_Reason reason = GNUNET_SCHEDULER_get_reason ();
-    if (0 != (reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-      return 0;
-    if (0 != (reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) {
-      if (0==ih->step) {
-        LOG (GNUNET_ERROR_TYPE_WARNING,
-             "Timeout contacting SOCKS server, retrying indefinitely, but probably hopeless.\n");
-        register_sender (ih);
-      } else {
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             "Timeout during mid SOCKS handshake (step %u), probably not a SOCKS server.\n",
-             ih->step);
-        GNUNET_break (0);
-      }
-      return 0;
+    if (0 == ih->step)
+    {
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+          "Timeout contacting SOCKS server, retrying indefinitely, but probably hopeless.\n");
+      register_sender (ih);
+    }
+    else
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+          "Timeout during mid SOCKS handshake (step %u), probably not a SOCKS server.\n",
+          ih->step);
+      GNUNET_break (0);
     }
-    printf("Erronious socks.c transmit_ready() callback on step %u with reason %u.\n", 
-       ih->step, reason );
-    /* if (reason == 48) register_sender (ih); */
-    /* GNUNET_break(0); */
     return 0;
-  } else 
-    printf("Good socks.c transmit_ready() callback on step %u with reason %u.\n", 
-       ih->step, GNUNET_SCHEDULER_get_reason () );
+  }
 
   GNUNET_assert (1024 >= size && size > 0);
   GNUNET_assert (SOCKS5_step_done > ih->step && ih->step >= 0);
@@ -390,7 +377,6 @@ transmit_ready (void *cls,
   GNUNET_assert (size >= l && l >= 0);
   memcpy(buf, b, l);
   register_reciever (ih, register_reciever_wants(ih));
-  printf("sent(%d)\n",l);
   return l;
 }
 
@@ -407,13 +393,13 @@ register_sender (struct GNUNET_SOCKS_Handshake *ih)
 {
   struct GNUNET_TIME_Relative timeout = GNUNET_TIME_UNIT_MINUTES;
 
-  GNUNET_assert (SOCKS5_step_done > ih->step && ih->step >= 0);
+  GNUNET_assert (SOCKS5_step_done > ih->step);
+  GNUNET_assert (ih->step >= 0);
   if (0 == ih->step)
     timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 3);
   unsigned char * b = ih->outstep[ih->step];
   unsigned char * e = ih->outstep[ih->step+1];
   GNUNET_assert (ih->outbuf <= b && b < e && e < &ih->outbuf[1024]);
-  printf("register_sender on step %u for %u bytes.\n", ih->step, (unsigned)(e - b) );
   ih->th = GNUNET_CONNECTION_notify_transmit_ready (ih->socks5_connection,
                                                     e - b,
                                                     timeout,
@@ -426,7 +412,7 @@ register_sender (struct GNUNET_SOCKS_Handshake *ih)
 /**
  * Initialize a SOCKS5 handshake for authentication via username and
  * password.  Tor uses SOCKS username and password authentication to assign
- * programs unique circuits. 
+ * programs unique circuits.
  *
  * @param user username for the proxy
  * @param pass password for the proxy
@@ -443,7 +429,7 @@ GNUNET_SOCKS_init_handshake (const char *user, const char *pass)
   unsigned char * n = b++;
   *n = 1; /* Number of authentication methods */
   /* We support no authentication even when requesting authentication,
-   * but this appears harmless, given the way that Tor uses authentication. 
+   * but this appears harmless, given the way that Tor uses authentication.
    * And some SOCKS5 servers might require this.  */
   *(b++) = SOCKS5_AUTH_NOAUTH;
   if (NULL != user) {
@@ -453,7 +439,7 @@ GNUNET_SOCKS_init_handshake (const char *user, const char *pass)
   /* There is no apperent reason to support authentication methods beyond
    * username and password since afaik Tor does not support them. */
 
-  /* We authenticate with an empty username and password if the server demands 
+  /* We authenticate with an empty username and password if the server demands
    * them but we do not have any. */
   if (user == NULL)
     user = "";
@@ -467,12 +453,14 @@ GNUNET_SOCKS_init_handshake (const char *user, const char *pass)
 
   ih->outstep[SOCKS5_step_cmd] = b;
 
+  ih->inend = ih->instart = ih->inbuf;
+
   return ih;
 }
 
 
 /**
- * Initialize a SOCKS5 handshake without authentication, thereby possibly 
+ * Initialize a SOCKS5 handshake without authentication, thereby possibly
  * sharing a Tor circuit with another process.
  *
  * @return Valid SOCKS5 hanbdshake handle
@@ -486,11 +474,11 @@ GNUNET_SOCKS_init_handshake_noauth ()
 
 /**
  * Build request that the SOCKS5 proxy open a TCP/IP stream to the given host
- * and port.  
+ * and port.
  *
  * @param ih SOCKS5 handshake
- * @param hostname 
- * @param port 
+ * @param hostname
+ * @param port
  */
 void
 GNUNET_SOCKS_set_handshake_destination (struct GNUNET_SOCKS_Handshake *ih,
@@ -535,7 +523,7 @@ GNUNET_SOCKS_set_handshake_destination (struct GNUNET_SOCKS_Handshake *ih,
  * @param c open unused connection, consumed here.
  * @return Connection handle that becomes usable when the SOCKS5 handshake completes.
  */
-struct GNUNET_CONNECTION_Handle * 
+struct GNUNET_CONNECTION_Handle *
 GNUNET_SOCKS_run_handshake(struct GNUNET_SOCKS_Handshake *ih,
                             struct GNUNET_CONNECTION_Handle *c)
 {
@@ -586,7 +574,7 @@ GNUNET_SOCKS_do_connect (const char *service_name,
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (cfg, service_name, "SOCKSPORT", &port0))
     port0 = 9050;
-  /* A typical Tor client should usually try port 9150 for the TBB too, but 
+  /* A typical Tor client should usually try port 9150 for the TBB too, but
    * GUNNet can probably assume a system Tor instalation. */
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "SOCKSHOST", &host0))