[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r15115: consolidate all our edge/circ/orconn reason-to-foo-or-back f (tor/trunk/src/or)



Author: arma
Date: 2008-06-10 20:17:02 -0400 (Tue, 10 Jun 2008)
New Revision: 15115

Added:
   tor/trunk/src/or/reasons.c
Modified:
   tor/trunk/src/or/Makefile.am
   tor/trunk/src/or/connection.c
   tor/trunk/src/or/connection_edge.c
   tor/trunk/src/or/control.c
   tor/trunk/src/or/or.h
   tor/trunk/src/or/relay.c
Log:
consolidate all our edge/circ/orconn reason-to-foo-or-back functions


Modified: tor/trunk/src/or/Makefile.am
===================================================================
--- tor/trunk/src/or/Makefile.am	2008-06-10 23:42:40 UTC (rev 15114)
+++ tor/trunk/src/or/Makefile.am	2008-06-11 00:17:02 UTC (rev 15115)
@@ -17,8 +17,8 @@
 	connection.c connection_edge.c connection_or.c control.c \
 	cpuworker.c directory.c dirserv.c dirvote.c \
 	dns.c dnsserv.c geoip.c hibernate.c main.c $(tor_platform_source) \
-	networkstatus.c \
-	onion.c policies.c relay.c rendcommon.c rendclient.c rendmid.c \
+	networkstatus.c onion.c policies.c \
+	reasons.c relay.c rendcommon.c rendclient.c rendmid.c \
 	rendservice.c rephist.c router.c routerlist.c routerparse.c \
 	eventdns.c \
 	tor_main.c
@@ -39,8 +39,8 @@
 	connection.c connection_edge.c connection_or.c control.c \
 	cpuworker.c directory.c dirserv.c dirvote.c \
 	dns.c dnsserv.c geoip.c hibernate.c main.c $(tor_platform_source) \
-	networkstatus.c \
-	onion.c policies.c relay.c rendcommon.c rendclient.c rendmid.c \
+	networkstatus.c onion.c policies.c \
+	reasons.c relay.c rendcommon.c rendclient.c rendmid.c \
 	rendservice.c rephist.c router.c routerlist.c routerparse.c \
 	eventdns.c \
 	test_data.c test.c

Modified: tor/trunk/src/or/connection.c
===================================================================
--- tor/trunk/src/or/connection.c	2008-06-10 23:42:40 UTC (rev 15114)
+++ tor/trunk/src/or/connection.c	2008-06-11 00:17:02 UTC (rev 15115)
@@ -504,7 +504,7 @@
             control_event_bootstrap_problem(
               tor_socket_strerror(or_conn->socket_error), 0);
           } else {
-            int reason = control_tls_error_to_reason(or_conn->tls_error);
+            int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
             control_event_or_conn_status(or_conn, OR_CONN_EVENT_FAILED,
                                          reason);
             control_event_bootstrap_problem("foo", reason);
@@ -518,11 +518,11 @@
          * closing a connection. */
         rep_hist_note_disconnect(or_conn->identity_digest, now);
         control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
-                control_tls_error_to_reason(or_conn->tls_error));
+                tls_error_to_orconn_end_reason(or_conn->tls_error));
       } else if (or_conn->identity_digest) {
         rep_hist_note_connection_died(or_conn->identity_digest, now);
         control_event_or_conn_status(or_conn, OR_CONN_EVENT_CLOSED,
-                control_tls_error_to_reason(or_conn->tls_error));
+                tls_error_to_orconn_end_reason(or_conn->tls_error));
       }
       /* Now close all the attached circuits on it. */
       circuit_unlink_all_from_or_conn(TO_OR_CONN(conn),

Modified: tor/trunk/src/or/connection_edge.c
===================================================================
--- tor/trunk/src/or/connection_edge.c	2008-06-10 23:42:40 UTC (rev 15114)
+++ tor/trunk/src/or/connection_edge.c	2008-06-11 00:17:02 UTC (rev 15115)
@@ -236,7 +236,7 @@
 {
   uint8_t reason;
   tor_assert(conn);
-  reason = (uint8_t)errno_to_end_stream_reason(tor_socket_errno(conn->_base.s));
+  reason = (uint8_t)errno_to_stream_end_reason(tor_socket_errno(conn->_base.s));
   return connection_edge_end(conn, reason);
 }
 
@@ -2312,7 +2312,7 @@
 {
   char buf[256];
   socks5_reply_status_t status =
-    connection_edge_end_reason_socks5_response(endreason);
+    stream_end_reason_to_socks5_response(endreason);
 
   tor_assert(conn->socks_request); /* make sure it's an AP stream */
 

Modified: tor/trunk/src/or/control.c
===================================================================
--- tor/trunk/src/or/control.c	2008-06-10 23:42:40 UTC (rev 15114)
+++ tor/trunk/src/or/control.c	2008-06-11 00:17:02 UTC (rev 15115)
@@ -2918,52 +2918,6 @@
   goto again;
 }
 
-/** Convert a numeric reason for destroying a circuit into a string for a
- * CIRCUIT event. */
-static const char *
-circuit_end_reason_to_string(int reason)
-{
-  if (reason >= 0 && reason & END_CIRC_REASON_FLAG_REMOTE)
-    reason &= ~END_CIRC_REASON_FLAG_REMOTE;
-  switch (reason) {
-    case END_CIRC_AT_ORIGIN:
-      /* This shouldn't get passed here; it's a catch-all reason. */
-      return "ORIGIN";
-    case END_CIRC_REASON_NONE:
-      /* This shouldn't get passed here; it's a catch-all reason. */
-      return "NONE";
-    case END_CIRC_REASON_TORPROTOCOL:
-      return "TORPROTOCOL";
-    case END_CIRC_REASON_INTERNAL:
-      return "INTERNAL";
-    case END_CIRC_REASON_REQUESTED:
-      return "REQUESTED";
-    case END_CIRC_REASON_HIBERNATING:
-      return "HIBERNATING";
-    case END_CIRC_REASON_RESOURCELIMIT:
-      return "RESOURCELIMIT";
-    case END_CIRC_REASON_CONNECTFAILED:
-      return "CONNECTFAILED";
-    case END_CIRC_REASON_OR_IDENTITY:
-      return "OR_IDENTITY";
-    case END_CIRC_REASON_OR_CONN_CLOSED:
-      return "OR_CONN_CLOSED";
-    case END_CIRC_REASON_FINISHED:
-      return "FINISHED";
-    case END_CIRC_REASON_TIMEOUT:
-      return "TIMEOUT";
-    case END_CIRC_REASON_DESTROYED:
-      return "DESTROYED";
-    case END_CIRC_REASON_NOPATH:
-      return "NOPATH";
-    case END_CIRC_REASON_NOSUCHSERVICE:
-      return "NOSUCHSERVICE";
-    default:
-      log_warn(LD_BUG, "Unrecognized reason code %d", (int)reason);
-      return NULL;
-  }
-}
-
 /** Something has happened to circuit <b>circ</b>: tell any interested
  * control connections. */
 int
@@ -2994,7 +2948,7 @@
     }
 
   if (tp == CIRC_EVENT_FAILED || tp == CIRC_EVENT_CLOSED) {
-    const char *reason_str = circuit_end_reason_to_string(reason_code);
+    const char *reason_str = circuit_end_reason_to_control_string(reason_code);
     char *reason = NULL;
     providing_reason=1;
     if (!reason_str) {
@@ -3067,35 +3021,6 @@
   return 0;
 }
 
-/** Convert the reason for ending a stream <b>reason</b> into the format used
- * in STREAM events. Return NULL if the reason is unrecognized. */
-static const char *
-stream_end_reason_to_string(int reason)
-{
-  reason &= END_STREAM_REASON_MASK;
-  switch (reason) {
-    case END_STREAM_REASON_MISC: return "MISC";
-    case END_STREAM_REASON_RESOLVEFAILED: return "RESOLVEFAILED";
-    case END_STREAM_REASON_CONNECTREFUSED: return "CONNECTREFUSED";
-    case END_STREAM_REASON_EXITPOLICY: return "EXITPOLICY";
-    case END_STREAM_REASON_DESTROY: return "DESTROY";
-    case END_STREAM_REASON_DONE: return "DONE";
-    case END_STREAM_REASON_TIMEOUT: return "TIMEOUT";
-    case END_STREAM_REASON_HIBERNATING: return "HIBERNATING";
-    case END_STREAM_REASON_INTERNAL: return "INTERNAL";
-    case END_STREAM_REASON_RESOURCELIMIT: return "RESOURCELIMIT";
-    case END_STREAM_REASON_CONNRESET: return "CONNRESET";
-    case END_STREAM_REASON_TORPROTOCOL: return "TORPROTOCOL";
-    case END_STREAM_REASON_NOTDIRECTORY: return "NOTDIRECTORY";
-
-    case END_STREAM_REASON_CANT_ATTACH: return "CANT_ATTACH";
-    case END_STREAM_REASON_NET_UNREACHABLE: return "NET_UNREACHABLE";
-    case END_STREAM_REASON_SOCKSPROTOCOL: return "SOCKS_PROTOCOL";
-
-    default: return NULL;
-  }
-}
-
 /** Something has happened to the stream associated with AP connection
  * <b>conn</b>: tell any interested control connections. */
 int
@@ -3240,61 +3165,6 @@
   }
 }
 
-/** Convert a TOR_TLS_* error code into an END_OR_CONN_* reason. */
-int
-control_tls_error_to_reason(int e)
-{
-  switch (e) {
-    case TOR_TLS_ERROR_IO:
-      return END_OR_CONN_REASON_TLS_IO_ERROR;
-    case TOR_TLS_ERROR_CONNREFUSED:
-      return END_OR_CONN_REASON_TCP_REFUSED;
-    case TOR_TLS_ERROR_CONNRESET:
-      return END_OR_CONN_REASON_TLS_CONNRESET;
-    case TOR_TLS_ERROR_NO_ROUTE:
-      return END_OR_CONN_REASON_TLS_NO_ROUTE;
-    case TOR_TLS_ERROR_TIMEOUT:
-      return END_OR_CONN_REASON_TLS_TIMEOUT;
-    case TOR_TLS_WANTREAD:
-    case TOR_TLS_WANTWRITE:
-    case TOR_TLS_CLOSE:
-    case TOR_TLS_DONE:
-      return END_OR_CONN_REASON_DONE;
-    default:
-      return END_OR_CONN_REASON_TLS_MISC;
-  }
-}
-
-/** Convert the reason for ending an OR connection <b>r</b> into the format
- * used in ORCONN events. Return "UNKNOWN" if the reason is unrecognized. */
-static const char *
-or_conn_end_reason_to_string(int r)
-{
-  switch (r) {
-    case END_OR_CONN_REASON_DONE:
-      return "DONE";
-    case END_OR_CONN_REASON_TCP_REFUSED:
-      return "CONNECTREFUSED";
-    case END_OR_CONN_REASON_OR_IDENTITY:
-      return "IDENTITY";
-    case END_OR_CONN_REASON_TLS_CONNRESET:
-      return "CONNECTRESET";
-    case END_OR_CONN_REASON_TLS_TIMEOUT:
-      return "TIMEOUT";
-    case END_OR_CONN_REASON_TLS_NO_ROUTE:
-      return "NOROUTE";
-    case END_OR_CONN_REASON_TLS_IO_ERROR:
-      return "IOERROR";
-    case END_OR_CONN_REASON_TLS_MISC:
-      return "MISC";
-    case 0:
-      return "";
-    default:
-      log_warn(LD_BUG, "Unrecognized or_conn reason code %d", r);
-      return "UNKNOWN";
-  }
-}
-
 /** Called when the status of an OR connection <b>conn</b> changes: tell any
  * interested control connections. <b>tp</b> is the new status for the
  * connection.  If <b>conn</b> has just closed or failed, then <b>reason</b>
@@ -3336,7 +3206,8 @@
                           "650 ORCONN %s %s@%s%s%s\r\n",
                           name, status,
                           reason ? "REASON=" : "",
-                          or_conn_end_reason_to_string(reason), ncircs_buf);
+                          orconn_end_reason_to_control_string(reason),
+                          ncircs_buf);
   }
   if (EVENT_IS_INTERESTING1L(EVENT_OR_CONN_STATUS)) {
     orconn_target_get_name(1, name, sizeof(name), conn);
@@ -3344,7 +3215,8 @@
                           "650 ORCONN %s %s@%s%s%s\r\n",
                           name, status,
                           reason ? "REASON=" : "",
-                          or_conn_end_reason_to_string(reason), ncircs_buf);
+                          orconn_end_reason_to_control_string(reason),
+                          ncircs_buf);
   }
 
   return 0;
@@ -3976,10 +3848,10 @@
 
   log_warn(LD_CONTROL, "Problem bootstrapping. Stuck at %d%%: %s. (%s; %s)",
            status, summary, warn,
-           or_conn_end_reason_to_string(reason));
+           orconn_end_reason_to_control_string(reason));
   control_event_client_status(LOG_WARN,
       "BOOTSTRAP PROGRESS=%d TAG=%s SUMMARY=\"%s\" WARNING=\"%s\" REASON=%s",
       bootstrap_percent, tag, summary, warn,
-      or_conn_end_reason_to_string(reason));
+      orconn_end_reason_to_control_string(reason));
 }
 

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2008-06-10 23:42:40 UTC (rev 15114)
+++ tor/trunk/src/or/or.h	2008-06-11 00:17:02 UTC (rev 15115)
@@ -2990,7 +2990,6 @@
 int control_event_stream_status(edge_connection_t *conn,
                                 stream_status_event_t e,
                                 int reason);
-int control_tls_error_to_reason(int e);
 int control_event_or_conn_status(or_connection_t *conn,
                                  or_conn_status_event_t e, int reason);
 int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written);
@@ -3585,6 +3584,18 @@
 void addr_policy_free(addr_policy_t *p);
 void policies_free_all(void);
 
+/********************************* reasons.c ***************************/
+
+const char *stream_end_reason_to_control_string(int reason);
+const char *stream_end_reason_to_string(int reason);
+socks5_reply_status_t stream_end_reason_to_socks5_response(int reason);
+int errno_to_stream_end_reason(int e);
+
+const char *orconn_end_reason_to_control_string(int r);
+int tls_error_to_orconn_end_reason(int e);
+
+const char *circuit_end_reason_to_control_string(int reason);
+
 /********************************* relay.c ***************************/
 
 extern uint64_t stats_n_relay_cells_relayed;
@@ -3604,8 +3615,6 @@
 int connection_edge_package_raw_inbuf(edge_connection_t *conn,
                                       int package_partial);
 void connection_edge_consider_sending_sendme(edge_connection_t *conn);
-socks5_reply_status_t connection_edge_end_reason_socks5_response(int reason);
-int errno_to_end_stream_reason(int e);
 
 extern uint64_t stats_n_data_cells_packaged;
 extern uint64_t stats_n_data_bytes_packaged;

Added: tor/trunk/src/or/reasons.c
===================================================================
--- tor/trunk/src/or/reasons.c	                        (rev 0)
+++ tor/trunk/src/or/reasons.c	2008-06-11 00:17:02 UTC (rev 15115)
@@ -0,0 +1,289 @@
+/* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2008, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+/* $Id$ */
+const char reasons_c_id[] =
+  "$Id$";
+
+/**
+ * \file reasons.c
+ * \brief Convert circuit, stream, and orconn error reasons to and/or from
+ * strings and errno values.
+ **/
+
+#include "or.h"
+
+/***************************** Edge (stream) reasons **********************/
+
+/** Convert the reason for ending a stream <b>reason</b> into the format used
+ * in STREAM events. Return NULL if the reason is unrecognized. */
+const char *
+stream_end_reason_to_control_string(int reason)
+{
+  reason &= END_STREAM_REASON_MASK;
+  switch (reason) {
+    case END_STREAM_REASON_MISC: return "MISC";
+    case END_STREAM_REASON_RESOLVEFAILED: return "RESOLVEFAILED";
+    case END_STREAM_REASON_CONNECTREFUSED: return "CONNECTREFUSED";
+    case END_STREAM_REASON_EXITPOLICY: return "EXITPOLICY";
+    case END_STREAM_REASON_DESTROY: return "DESTROY";
+    case END_STREAM_REASON_DONE: return "DONE";
+    case END_STREAM_REASON_TIMEOUT: return "TIMEOUT";
+    case END_STREAM_REASON_HIBERNATING: return "HIBERNATING";
+    case END_STREAM_REASON_INTERNAL: return "INTERNAL";
+    case END_STREAM_REASON_RESOURCELIMIT: return "RESOURCELIMIT";
+    case END_STREAM_REASON_CONNRESET: return "CONNRESET";
+    case END_STREAM_REASON_TORPROTOCOL: return "TORPROTOCOL";
+    case END_STREAM_REASON_NOTDIRECTORY: return "NOTDIRECTORY";
+
+    case END_STREAM_REASON_CANT_ATTACH: return "CANT_ATTACH";
+    case END_STREAM_REASON_NET_UNREACHABLE: return "NET_UNREACHABLE";
+    case END_STREAM_REASON_SOCKSPROTOCOL: return "SOCKS_PROTOCOL";
+
+    default: return NULL;
+  }
+}
+
+/** Translate <b>reason</b>, which came from a relay 'end' cell,
+ * into a static const string describing why the stream is closing.
+ * <b>reason</b> is -1 if no reason was provided.
+ */
+const char *
+stream_end_reason_to_string(int reason)
+{
+  switch (reason) {
+    case -1:
+      log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
+             "End cell arrived with length 0. Should be at least 1.");
+      return "MALFORMED";
+    case END_STREAM_REASON_MISC:           return "misc error";
+    case END_STREAM_REASON_RESOLVEFAILED:  return "resolve failed";
+    case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
+    case END_STREAM_REASON_EXITPOLICY:     return "exit policy failed";
+    case END_STREAM_REASON_DESTROY:        return "destroyed";
+    case END_STREAM_REASON_DONE:           return "closed normally";
+    case END_STREAM_REASON_TIMEOUT:        return "gave up (timeout)";
+    case END_STREAM_REASON_HIBERNATING:    return "server is hibernating";
+    case END_STREAM_REASON_INTERNAL:       return "internal error at server";
+    case END_STREAM_REASON_RESOURCELIMIT:  return "server out of resources";
+    case END_STREAM_REASON_CONNRESET:      return "connection reset";
+    case END_STREAM_REASON_TORPROTOCOL:    return "Tor protocol error";
+    case END_STREAM_REASON_NOTDIRECTORY:   return "not a directory";
+    default:
+      log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
+             "Reason for ending (%d) not recognized.",reason);
+      return "unknown";
+  }
+}
+
+/** Translate <b>reason</b> (as from a relay 'end' cell) into an
+ * appropriate SOCKS5 reply code.
+ *
+ * A reason of 0 means that we're not actually expecting to send
+ * this code back to the socks client; we just call it 'succeeded'
+ * to keep things simple.
+ */
+socks5_reply_status_t
+stream_end_reason_to_socks5_response(int reason)
+{
+  switch (reason & END_STREAM_REASON_MASK) {
+    case 0:
+      return SOCKS5_SUCCEEDED;
+    case END_STREAM_REASON_MISC:
+      return SOCKS5_GENERAL_ERROR;
+    case END_STREAM_REASON_RESOLVEFAILED:
+      return SOCKS5_HOST_UNREACHABLE;
+    case END_STREAM_REASON_CONNECTREFUSED:
+      return SOCKS5_CONNECTION_REFUSED;
+    case END_STREAM_REASON_ENTRYPOLICY:
+      return SOCKS5_NOT_ALLOWED;
+    case END_STREAM_REASON_EXITPOLICY:
+      return SOCKS5_NOT_ALLOWED;
+    case END_STREAM_REASON_DESTROY:
+      return SOCKS5_GENERAL_ERROR;
+    case END_STREAM_REASON_DONE:
+      return SOCKS5_SUCCEEDED;
+    case END_STREAM_REASON_TIMEOUT:
+      return SOCKS5_TTL_EXPIRED;
+    case END_STREAM_REASON_RESOURCELIMIT:
+      return SOCKS5_GENERAL_ERROR;
+    case END_STREAM_REASON_HIBERNATING:
+      return SOCKS5_GENERAL_ERROR;
+    case END_STREAM_REASON_INTERNAL:
+      return SOCKS5_GENERAL_ERROR;
+    case END_STREAM_REASON_CONNRESET:
+      return SOCKS5_CONNECTION_REFUSED;
+    case END_STREAM_REASON_TORPROTOCOL:
+      return SOCKS5_GENERAL_ERROR;
+
+    case END_STREAM_REASON_CANT_ATTACH:
+      return SOCKS5_GENERAL_ERROR;
+    case END_STREAM_REASON_NET_UNREACHABLE:
+      return SOCKS5_NET_UNREACHABLE;
+    case END_STREAM_REASON_SOCKSPROTOCOL:
+      return SOCKS5_GENERAL_ERROR;
+    default:
+      log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
+             "Reason for ending (%d) not recognized; "
+             "sending generic socks error.", reason);
+      return SOCKS5_GENERAL_ERROR;
+  }
+}
+
+/* We need to use a few macros to deal with the fact that Windows
+ * decided that their sockets interface should be a permakludge.
+ * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
+ * version, and S_CASE is for errors where windows has only a WSAEFOO
+ * version.  (The E is for 'error', the S is for 'socket'). */
+#ifdef MS_WINDOWS
+#define E_CASE(s) case s: case WSA ## s
+#define S_CASE(s) case WSA ## s
+#else
+#define E_CASE(s) case s
+#define S_CASE(s) case s
+#endif
+
+/** Given an errno from a failed exit connection, return a reason code
+ * appropriate for use in a RELAY END cell.
+ */
+int
+errno_to_stream_end_reason(int e)
+{
+  switch (e) {
+    case EPIPE:
+      return END_STREAM_REASON_DONE;
+    E_CASE(EBADF):
+    E_CASE(EFAULT):
+    E_CASE(EINVAL):
+    S_CASE(EISCONN):
+    S_CASE(ENOTSOCK):
+    S_CASE(EPROTONOSUPPORT):
+    S_CASE(EAFNOSUPPORT):
+    E_CASE(EACCES):
+    S_CASE(ENOTCONN):
+    S_CASE(ENETUNREACH):
+      return END_STREAM_REASON_INTERNAL;
+    S_CASE(ECONNREFUSED):
+      return END_STREAM_REASON_CONNECTREFUSED;
+    S_CASE(ECONNRESET):
+      return END_STREAM_REASON_CONNRESET;
+    S_CASE(ETIMEDOUT):
+      return END_STREAM_REASON_TIMEOUT;
+    S_CASE(ENOBUFS):
+    case ENOMEM:
+    case ENFILE:
+    E_CASE(EMFILE):
+      return END_STREAM_REASON_RESOURCELIMIT;
+    default:
+      log_info(LD_EXIT, "Didn't recognize errno %d (%s); telling the client "
+               "that we are ending a stream for 'misc' reason.",
+               e, tor_socket_strerror(e));
+      return END_STREAM_REASON_MISC;
+  }
+}
+
+/***************************** ORConn reasons *****************************/
+
+/** Convert the reason for ending an OR connection <b>r</b> into the format
+ * used in ORCONN events. Return "UNKNOWN" if the reason is unrecognized. */
+const char *
+orconn_end_reason_to_control_string(int r)
+{
+  switch (r) {
+    case END_OR_CONN_REASON_DONE:
+      return "DONE";
+    case END_OR_CONN_REASON_TCP_REFUSED:
+      return "CONNECTREFUSED";
+    case END_OR_CONN_REASON_OR_IDENTITY:
+      return "IDENTITY";
+    case END_OR_CONN_REASON_TLS_CONNRESET:
+      return "CONNECTRESET";
+    case END_OR_CONN_REASON_TLS_TIMEOUT:
+      return "TIMEOUT";
+    case END_OR_CONN_REASON_TLS_NO_ROUTE:
+      return "NOROUTE";
+    case END_OR_CONN_REASON_TLS_IO_ERROR:
+      return "IOERROR";
+    case END_OR_CONN_REASON_TLS_MISC:
+      return "MISC";
+    case 0:
+      return "";
+    default:
+      log_warn(LD_BUG, "Unrecognized or_conn reason code %d", r);
+      return "UNKNOWN";
+  }
+}
+
+/** Convert a TOR_TLS_* error code into an END_OR_CONN_* reason. */
+int
+tls_error_to_orconn_end_reason(int e)
+{
+  switch (e) {
+    case TOR_TLS_ERROR_IO:
+      return END_OR_CONN_REASON_TLS_IO_ERROR;
+    case TOR_TLS_ERROR_CONNREFUSED:
+      return END_OR_CONN_REASON_TCP_REFUSED;
+    case TOR_TLS_ERROR_CONNRESET:
+      return END_OR_CONN_REASON_TLS_CONNRESET;
+    case TOR_TLS_ERROR_NO_ROUTE:
+      return END_OR_CONN_REASON_TLS_NO_ROUTE;
+    case TOR_TLS_ERROR_TIMEOUT:
+      return END_OR_CONN_REASON_TLS_TIMEOUT;
+    case TOR_TLS_WANTREAD:
+    case TOR_TLS_WANTWRITE:
+    case TOR_TLS_CLOSE:
+    case TOR_TLS_DONE:
+      return END_OR_CONN_REASON_DONE;
+    default:
+      return END_OR_CONN_REASON_TLS_MISC;
+  }
+}
+
+/***************************** Circuit reasons *****************************/
+
+/** Convert a numeric reason for destroying a circuit into a string for a
+ * CIRCUIT event. */
+const char *
+circuit_end_reason_to_control_string(int reason)
+{
+  if (reason >= 0 && reason & END_CIRC_REASON_FLAG_REMOTE)
+    reason &= ~END_CIRC_REASON_FLAG_REMOTE;
+  switch (reason) {
+    case END_CIRC_AT_ORIGIN:
+      /* This shouldn't get passed here; it's a catch-all reason. */
+      return "ORIGIN";
+    case END_CIRC_REASON_NONE:
+      /* This shouldn't get passed here; it's a catch-all reason. */
+      return "NONE";
+    case END_CIRC_REASON_TORPROTOCOL:
+      return "TORPROTOCOL";
+    case END_CIRC_REASON_INTERNAL:
+      return "INTERNAL";
+    case END_CIRC_REASON_REQUESTED:
+      return "REQUESTED";
+    case END_CIRC_REASON_HIBERNATING:
+      return "HIBERNATING";
+    case END_CIRC_REASON_RESOURCELIMIT:
+      return "RESOURCELIMIT";
+    case END_CIRC_REASON_CONNECTFAILED:
+      return "CONNECTFAILED";
+    case END_CIRC_REASON_OR_IDENTITY:
+      return "OR_IDENTITY";
+    case END_CIRC_REASON_OR_CONN_CLOSED:
+      return "OR_CONN_CLOSED";
+    case END_CIRC_REASON_FINISHED:
+      return "FINISHED";
+    case END_CIRC_REASON_TIMEOUT:
+      return "TIMEOUT";
+    case END_CIRC_REASON_DESTROYED:
+      return "DESTROYED";
+    case END_CIRC_REASON_NOPATH:
+      return "NOPATH";
+    case END_CIRC_REASON_NOSUCHSERVICE:
+      return "NOSUCHSERVICE";
+    default:
+      log_warn(LD_BUG, "Unrecognized reason code %d", (int)reason);
+      return NULL;
+  }
+}
+


Property changes on: tor/trunk/src/or/reasons.c
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Modified: tor/trunk/src/or/relay.c
===================================================================
--- tor/trunk/src/or/relay.c	2008-06-10 23:42:40 UTC (rev 15114)
+++ tor/trunk/src/or/relay.c	2008-06-11 00:17:02 UTC (rev 15115)
@@ -560,144 +560,6 @@
                                       payload_len, fromconn->cpath_layer);
 }
 
-/** Translate <b>reason</b>, which came from a relay 'end' cell,
- * into a static const string describing why the stream is closing.
- * <b>reason</b> is -1 if no reason was provided.
- */
-static const char *
-connection_edge_end_reason_str(int reason)
-{
-  switch (reason) {
-    case -1:
-      log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
-             "End cell arrived with length 0. Should be at least 1.");
-      return "MALFORMED";
-    case END_STREAM_REASON_MISC:           return "misc error";
-    case END_STREAM_REASON_RESOLVEFAILED:  return "resolve failed";
-    case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
-    case END_STREAM_REASON_EXITPOLICY:     return "exit policy failed";
-    case END_STREAM_REASON_DESTROY:        return "destroyed";
-    case END_STREAM_REASON_DONE:           return "closed normally";
-    case END_STREAM_REASON_TIMEOUT:        return "gave up (timeout)";
-    case END_STREAM_REASON_HIBERNATING:    return "server is hibernating";
-    case END_STREAM_REASON_INTERNAL:       return "internal error at server";
-    case END_STREAM_REASON_RESOURCELIMIT:  return "server out of resources";
-    case END_STREAM_REASON_CONNRESET:      return "connection reset";
-    case END_STREAM_REASON_TORPROTOCOL:    return "Tor protocol error";
-    case END_STREAM_REASON_NOTDIRECTORY:   return "not a directory";
-    default:
-      log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
-             "Reason for ending (%d) not recognized.",reason);
-      return "unknown";
-  }
-}
-
-/** Translate <b>reason</b> (as from a relay 'end' cell) into an
- * appropriate SOCKS5 reply code.
- *
- * A reason of 0 means that we're not actually expecting to send
- * this code back to the socks client; we just call it 'succeeded'
- * to keep things simple.
- */
-socks5_reply_status_t
-connection_edge_end_reason_socks5_response(int reason)
-{
-  switch (reason & END_STREAM_REASON_MASK) {
-    case 0:
-      return SOCKS5_SUCCEEDED;
-    case END_STREAM_REASON_MISC:
-      return SOCKS5_GENERAL_ERROR;
-    case END_STREAM_REASON_RESOLVEFAILED:
-      return SOCKS5_HOST_UNREACHABLE;
-    case END_STREAM_REASON_CONNECTREFUSED:
-      return SOCKS5_CONNECTION_REFUSED;
-    case END_STREAM_REASON_ENTRYPOLICY:
-      return SOCKS5_NOT_ALLOWED;
-    case END_STREAM_REASON_EXITPOLICY:
-      return SOCKS5_NOT_ALLOWED;
-    case END_STREAM_REASON_DESTROY:
-      return SOCKS5_GENERAL_ERROR;
-    case END_STREAM_REASON_DONE:
-      return SOCKS5_SUCCEEDED;
-    case END_STREAM_REASON_TIMEOUT:
-      return SOCKS5_TTL_EXPIRED;
-    case END_STREAM_REASON_RESOURCELIMIT:
-      return SOCKS5_GENERAL_ERROR;
-    case END_STREAM_REASON_HIBERNATING:
-      return SOCKS5_GENERAL_ERROR;
-    case END_STREAM_REASON_INTERNAL:
-      return SOCKS5_GENERAL_ERROR;
-    case END_STREAM_REASON_CONNRESET:
-      return SOCKS5_CONNECTION_REFUSED;
-    case END_STREAM_REASON_TORPROTOCOL:
-      return SOCKS5_GENERAL_ERROR;
-
-    case END_STREAM_REASON_CANT_ATTACH:
-      return SOCKS5_GENERAL_ERROR;
-    case END_STREAM_REASON_NET_UNREACHABLE:
-      return SOCKS5_NET_UNREACHABLE;
-    case END_STREAM_REASON_SOCKSPROTOCOL:
-      return SOCKS5_GENERAL_ERROR;
-    default:
-      log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
-             "Reason for ending (%d) not recognized; "
-             "sending generic socks error.", reason);
-      return SOCKS5_GENERAL_ERROR;
-  }
-}
-
-/* We need to use a few macros to deal with the fact that Windows
- * decided that their sockets interface should be a permakludge.
- * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
- * version, and S_CASE is for errors where windows has only a WSAEFOO
- * version.  (The E is for 'error', the S is for 'socket'). */
-#ifdef MS_WINDOWS
-#define E_CASE(s) case s: case WSA ## s
-#define S_CASE(s) case WSA ## s
-#else
-#define E_CASE(s) case s
-#define S_CASE(s) case s
-#endif
-
-/** Given an errno from a failed exit connection, return a reason code
- * appropriate for use in a RELAY END cell.
- */
-int
-errno_to_end_stream_reason(int e)
-{
-  switch (e) {
-    case EPIPE:
-      return END_STREAM_REASON_DONE;
-    E_CASE(EBADF):
-    E_CASE(EFAULT):
-    E_CASE(EINVAL):
-    S_CASE(EISCONN):
-    S_CASE(ENOTSOCK):
-    S_CASE(EPROTONOSUPPORT):
-    S_CASE(EAFNOSUPPORT):
-    E_CASE(EACCES):
-    S_CASE(ENOTCONN):
-    S_CASE(ENETUNREACH):
-      return END_STREAM_REASON_INTERNAL;
-    S_CASE(ECONNREFUSED):
-      return END_STREAM_REASON_CONNECTREFUSED;
-    S_CASE(ECONNRESET):
-      return END_STREAM_REASON_CONNRESET;
-    S_CASE(ETIMEDOUT):
-      return END_STREAM_REASON_TIMEOUT;
-    S_CASE(ENOBUFS):
-    case ENOMEM:
-    case ENFILE:
-    E_CASE(EMFILE):
-      return END_STREAM_REASON_RESOURCELIMIT;
-    default:
-      log_info(LD_EXIT, "Didn't recognize errno %d (%s); telling the client "
-               "that we are ending a stream for 'misc' reason.",
-               e, tor_socket_strerror(e));
-      return END_STREAM_REASON_MISC;
-  }
-}
-
 /** How many times will I retry a stream that fails due to DNS
  * resolve failure or misc error?
  */
@@ -733,7 +595,7 @@
       conn->_base.type == CONN_TYPE_AP) {
     log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
              safe_str(conn->socks_request->address),
-             connection_edge_end_reason_str(reason));
+             stream_end_reason_to_string(reason));
     exitrouter =
       router_get_by_digest(circ->build_state->chosen_exit->identity_digest);
     switch (reason) {
@@ -842,7 +704,7 @@
 
   log_info(LD_APP,
            "Edge got end (%s) before we're connected. Marking for close.",
-       connection_edge_end_reason_str(rh->length > 0 ? reason : -1));
+       stream_end_reason_to_string(rh->length > 0 ? reason : -1));
   if (conn->_base.type == CONN_TYPE_AP) {
     circuit_log_path(LOG_INFO,LD_APP,circ);
     /* need to test because of detach_retriable*/
@@ -1123,13 +985,13 @@
         *(uint8_t *)(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
       if (!conn) {
         log_info(domain,"end cell (%s) dropped, unknown stream.",
-                 connection_edge_end_reason_str(reason));
+                 stream_end_reason_to_string(reason));
         return 0;
       }
 /* XXX add to this log_fn the exit node's nickname? */
       log_info(domain,"%d: end cell (%s) for stream %d. Removing stream.",
                conn->_base.s,
-               connection_edge_end_reason_str(reason),
+               stream_end_reason_to_string(reason),
                conn->stream_id);
       if (conn->socks_request && !conn->socks_request->has_finished)
         log_warn(LD_BUG,