Commit d113b75d authored by Roger Dingledine's avatar Roger Dingledine
Browse files

more log conversions.

whee.


svn:r6003
parent 36484f87
Loading
Loading
Loading
Loading
+159 −147

File changed.

Preview size limit exceeded, changes collapsed.

+92 −83
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ conn_type_to_string(int type)
    case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
    case CONN_TYPE_CONTROL: return "Control";
    default:
      warn(LD_BUG, "Bug: unknown connection type %d", type);
      log_warn(LD_BUG, "Bug: unknown connection type %d", type);
      tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
      return buf;
  }
@@ -134,7 +134,7 @@ conn_state_to_string(int type, int state)
      break;
  }

  warn(LD_BUG, "Bug: unknown connection state %d (type %d)", state, type);
  log_warn(LD_BUG, "Bug: unknown connection state %d (type %d)", state, type);
  tor_snprintf(buf, sizeof(buf),
               "unknown state [%d] on unknown [%s] connection",
               state, conn_type_to_string(type));
@@ -191,12 +191,12 @@ connection_unregister(connection_t *conn)
{
  if (conn->read_event) {
    if (event_del(conn->read_event))
      warn(LD_BUG, "Error removing read event for %d", conn->s);
      log_warn(LD_BUG, "Error removing read event for %d", conn->s);
    tor_free(conn->read_event);
  }
  if (conn->write_event) {
    if (event_del(conn->write_event))
      warn(LD_BUG, "Error removing write event for %d", conn->s);
      log_warn(LD_BUG, "Error removing write event for %d", conn->s);
    tor_free(conn->write_event);
  }
}
@@ -234,13 +234,13 @@ _connection_free(connection_t *conn)
  tor_free(conn->requested_resource);

  if (conn->s >= 0) {
    debug(LD_NET,"closing fd %d.",conn->s);
    log_debug(LD_NET,"closing fd %d.",conn->s);
    tor_close_socket(conn->s);
  }

  if (conn->type == CONN_TYPE_OR &&
      !tor_digest_is_zero(conn->identity_digest)) {
    warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
    log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
    connection_or_remove_from_identity_map(conn);
  }

@@ -324,7 +324,7 @@ connection_about_to_close_connection(connection_t *conn)

  if (CONN_IS_EDGE(conn)) {
    if (!conn->has_sent_end) {
      warn(LD_BUG, "Harmless bug: Edge connection (marked at %s:%d) "
      log_warn(LD_BUG, "Harmless bug: Edge connection (marked at %s:%d) "
               "hasn't sent end yet?",
               conn->marked_for_close_file, conn->marked_for_close);
      tor_fragile_assert();
@@ -371,7 +371,7 @@ connection_about_to_close_connection(connection_t *conn)
      if (conn->socks_request->has_finished == 0) {
        /* since conn gets removed right after this function finishes,
         * there's no point trying to send back a reply at this point. */
        warn(LD_BUG,"Bug: Closing stream (marked at %s:%d) without sending"
        log_warn(LD_BUG,"Bug: Closing stream (marked at %s:%d) without sending"
                 " back a socks reply.",
                 conn->marked_for_close_file, conn->marked_for_close);
      } else {
@@ -403,12 +403,12 @@ connection_close_immediate(connection_t *conn)
{
  assert_connection_ok(conn,0);
  if (conn->s < 0) {
    err(LD_BUG,"Bug: Attempt to close already-closed connection.");
    log_err(LD_BUG,"Bug: Attempt to close already-closed connection.");
    tor_fragile_assert();
    return;
  }
  if (conn->outbuf_flushlen) {
    info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
    log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
             conn->s, conn_type_to_string(conn->type),
             conn_state_to_string(conn->type, conn->state),
             (int)conn->outbuf_flushlen);
@@ -515,7 +515,8 @@ connection_create_listener(const char *listenaddress, uint16_t listenport,

  memset(&listenaddr,0,sizeof(struct sockaddr_in));
  if (parse_addr_port(listenaddress, &address, &addr, &usePort)<0) {
    warn(LD_CONFIG, "Error parsing/resolving ListenAddress %s",listenaddress);
    log_warn(LD_CONFIG,
             "Error parsing/resolving ListenAddress %s", listenaddress);
    return NULL;
  }

@@ -525,15 +526,15 @@ connection_create_listener(const char *listenaddress, uint16_t listenport,
  listenaddr.sin_family = AF_INET;
  listenaddr.sin_port = htons((uint16_t) usePort);

  notice(LD_NET, "Opening %s on %s:%d",
  log_notice(LD_NET, "Opening %s on %s:%d",
             conn_type_to_string(type), address, usePort);

  s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  if (s < 0) {
    warn(LD_NET,"Socket creation failed.");
    log_warn(LD_NET,"Socket creation failed.");
    goto err;
  } else if (!SOCKET_IS_POLLABLE(s)) {
    warn(LD_NET,"Too many connections; can't create pollable listener.");
    log_warn(LD_NET,"Too many connections; can't create pollable listener.");
    tor_close_socket(s);
    goto err;
  }
@@ -551,13 +552,13 @@ connection_create_listener(const char *listenaddress, uint16_t listenport,
    int e = tor_socket_errno(s);
    if (ERRNO_IS_EADDRINUSE(e))
      helpfulhint = ". Is Tor already running?";
    warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
    log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
             tor_socket_strerror(e), helpfulhint);
    goto err;
  }

  if (listen(s,SOMAXCONN) < 0) {
    warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
    log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
             tor_socket_strerror(tor_socket_errno(s)));
    goto err;
  }
@@ -571,12 +572,13 @@ connection_create_listener(const char *listenaddress, uint16_t listenport,
  conn->port = usePort;

  if (connection_add(conn) < 0) { /* no space, forget it */
    warn(LD_NET,"connection_add failed. Giving up.");
    log_warn(LD_NET,"connection_add failed. Giving up.");
    connection_free(conn);
    goto err;
  }

  debug(LD_NET,"%s listening on port %u.",conn_type_to_string(type), usePort);
  log_debug(LD_NET,"%s listening on port %u.",
            conn_type_to_string(type), usePort);

  conn->state = LISTENER_STATE_READY;
  connection_start_reading(conn);
@@ -637,7 +639,7 @@ connection_handle_listener_read(connection_t *conn, int new_type)
    int e;
    if (news>=0) {
      /* Too many conns to poll. */
      warn(LD_NET,"Too many connections; couldn't accept connection.");
      log_warn(LD_NET,"Too many connections; couldn't accept connection.");
      tor_close_socket(news);
      return 0;
    }
@@ -645,31 +647,33 @@ connection_handle_listener_read(connection_t *conn, int new_type)
    if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
      return 0; /* he hung up before we could accept(). that's fine. */
    } else if (ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e)) {
      notice(LD_NET,"accept failed: %s. Dropping incoming connection.",
      log_notice(LD_NET,"accept failed: %s. Dropping incoming connection.",
                 tor_socket_strerror(e));
      return 0;
    }
    /* else there was a real error. */
    warn(LD_NET,"accept() failed: %s. Closing listener.",
    log_warn(LD_NET,"accept() failed: %s. Closing listener.",
             tor_socket_strerror(e));
    connection_mark_for_close(conn);
    return -1;
  }
  debug(LD_NET,"Connection accepted on socket %d (child of fd %d).",
  log_debug(LD_NET,
            "Connection accepted on socket %d (child of fd %d).",
            news,conn->s);

  set_socket_nonblocking(news);

  if (check_sockaddr_in((struct sockaddr*)addrbuf, remotelen, LOG_INFO)<0) {
    info(LD_NET, "accept() returned a strange address; trying getsockname().");
    log_info(LD_NET,
             "accept() returned a strange address; trying getsockname().");
    remotelen=256;
    memset(addrbuf, 0, sizeof(addrbuf));
    if (getsockname(news, (struct sockaddr*)addrbuf, &remotelen)<0) {
      warn(LD_NET, "getsockname() failed.");
      log_warn(LD_NET, "getsockname() failed.");
    } else {
      if (check_sockaddr_in((struct sockaddr*)addrbuf, remotelen,
                            LOG_WARN) < 0) {
        warn(LD_NET,"Something's wrong with this conn. Closing it.");
        log_warn(LD_NET,"Something's wrong with this conn. Closing it.");
        tor_close_socket(news);
        return 0;
      }
@@ -682,7 +686,7 @@ connection_handle_listener_read(connection_t *conn, int new_type)
    /* check sockspolicy to see if we should accept it */
    if (socks_policy_permits_address(ntohl(remote.sin_addr.s_addr)) == 0) {
      tor_inet_ntoa(&remote.sin_addr, tmpbuf, sizeof(tmpbuf));
      notice(LD_APP,"Denying socks connection from untrusted address %s.",
      log_notice(LD_APP,"Denying socks connection from untrusted address %s.",
                 tmpbuf);
      tor_close_socket(news);
      return 0;
@@ -692,7 +696,7 @@ connection_handle_listener_read(connection_t *conn, int new_type)
    /* check dirpolicy to see if we should accept it */
    if (dir_policy_permits_address(ntohl(remote.sin_addr.s_addr)) == 0) {
      tor_inet_ntoa(&remote.sin_addr, tmpbuf, sizeof(tmpbuf));
      notice(LD_DIRSERV,"Denying dir connection from address %s.",
      log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
                 tmpbuf);
      tor_close_socket(news);
      return 0;
@@ -762,11 +766,11 @@ connection_connect(connection_t *conn, char *address,

  s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  if (s < 0) {
    warn(LD_NET,"Error creating network socket: %s",
    log_warn(LD_NET,"Error creating network socket: %s",
             tor_socket_strerror(tor_socket_errno(-1)));
    return -1;
  } else if (!SOCKET_IS_POLLABLE(s)) {
    warn(LD_NET,
    log_warn(LD_NET,
            "Too many connections; can't create pollable connection to %s",
            safe_str(address));
    tor_close_socket(s);
@@ -780,11 +784,11 @@ connection_connect(connection_t *conn, char *address,
    ext_addr.sin_family = AF_INET;
    ext_addr.sin_port = 0;
    if (!tor_inet_aton(options->OutboundBindAddress, &ext_addr.sin_addr)) {
      warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
      log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
               options->OutboundBindAddress);
    } else {
      if (bind(s, (struct sockaddr*)&ext_addr, sizeof(ext_addr)) < 0) {
        warn(LD_NET,"Error binding network socket: %s",
        log_warn(LD_NET,"Error binding network socket: %s",
                 tor_socket_strerror(tor_socket_errno(s)));
        return -1;
      }
@@ -798,13 +802,14 @@ connection_connect(connection_t *conn, char *address,
  dest_addr.sin_port = htons(port);
  dest_addr.sin_addr.s_addr = htonl(addr);

  debug(LD_NET,"Connecting to %s:%u.",safe_str(address),port);
  log_debug(LD_NET,"Connecting to %s:%u.",safe_str(address),port);

  if (connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
    int e = tor_socket_errno(s);
    if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
      /* yuck. kill it. */
      info(LD_NET,"connect() to %s:%u failed: %s",safe_str(address),port,
      log_info(LD_NET,
               "connect() to %s:%u failed: %s",safe_str(address),port,
               tor_socket_strerror(e));
      tor_close_socket(s);
      return -1;
@@ -905,7 +910,7 @@ retry_listeners(int type, config_line_t *cfg,
      });
    if (! line) {
      /* This one isn't configured. Close it. */
      notice(LD_NET, "Closing %s on %s:%d",
      log_notice(LD_NET, "Closing %s on %s:%d",
                 conn_type_to_string(type), conn->address, conn->port);
      if (replaced_conns) {
        smartlist_add(replaced_conns, conn);
@@ -915,7 +920,7 @@ retry_listeners(int type, config_line_t *cfg,
      }
    } else {
      /* It's configured; we don't need to launch it. */
//      debug(LD_NET, "Already have %s on %s:%d",
//      log_debug(LD_NET, "Already have %s on %s:%d",
//                conn_type_to_string(type), conn->address, conn->port);
      smartlist_remove(launch, line);
      if (free_launch_elts)
@@ -1101,11 +1106,11 @@ connection_bucket_refill(struct timeval *now)
  /* refill the global buckets */
  if (global_read_bucket < (int)options->BandwidthBurst) {
    global_read_bucket += (int)options->BandwidthRate;
    debug(LD_NET,"global_read_bucket now %d.", global_read_bucket);
    log_debug(LD_NET,"global_read_bucket now %d.", global_read_bucket);
  }
  if (global_write_bucket < (int)options->BandwidthBurst) {
    global_write_bucket += (int)options->BandwidthRate;
    debug(LD_NET,"global_write_bucket now %d.", global_write_bucket);
    log_debug(LD_NET,"global_write_bucket now %d.", global_write_bucket);
  }

  /* refill the per-connection buckets */
@@ -1267,7 +1272,8 @@ connection_read_to_buf(connection_t *conn, int *max_to_read)
      return connection_tls_continue_handshake(conn);
    }

    debug(LD_NET,"%d: starting, inbuf_datalen %d (%d pending in tls object)."
    log_debug(LD_NET,
              "%d: starting, inbuf_datalen %d (%d pending in tls object)."
              " at_most %d.",
              conn->s,(int)buf_datalen(conn->inbuf),
              tor_tls_get_pending_bytes(conn->tls), at_most);
@@ -1277,12 +1283,12 @@ connection_read_to_buf(connection_t *conn, int *max_to_read)

    switch (result) {
      case TOR_TLS_CLOSE:
        info(LD_NET,"TLS connection closed on read. Closing. "
        log_info(LD_NET,"TLS connection closed on read. Closing. "
                 "(Nickname %s, address %s",
                 conn->nickname ? conn->nickname : "not set", conn->address);
        return -1;
      case TOR_TLS_ERROR:
        info(LD_NET,"tls error. breaking (nickname %s, address %s).",
        log_info(LD_NET,"tls error. breaking (nickname %s, address %s).",
                 conn->nickname ? conn->nickname : "not set", conn->address);
        return -1;
      case TOR_TLS_WANTWRITE:
@@ -1302,7 +1308,7 @@ connection_read_to_buf(connection_t *conn, int *max_to_read)
       * believing that SSL bytes are the same as TCP bytes anyway. */
      int r2 = read_to_buf_tls(conn->tls, pending, conn->inbuf);
      if (r2<0) {
        warn(LD_BUG, "Bug: apparently, reading pending bytes can fail.");
        log_warn(LD_BUG, "Bug: apparently, reading pending bytes can fail.");
        return -1;
      } else {
        result += r2;
@@ -1402,7 +1408,8 @@ connection_handle_write(connection_t *conn)
  /* Sometimes, "writable" means "connected". */
  if (connection_state_is_connecting(conn)) {
    if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
      warn(LD_BUG,"getsockopt() syscall failed?! Please report to tor-ops.");
      log_warn(LD_BUG,
               "getsockopt() syscall failed?! Please report to tor-ops.");
      if (CONN_IS_EDGE(conn))
        connection_edge_end_errno(conn, conn->cpath_layer);
      connection_mark_for_close(conn);
@@ -1411,7 +1418,7 @@ connection_handle_write(connection_t *conn)
    if (e) {
      /* some sort of error, but maybe just inprogress still */
      if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
        info(LD_NET,"in-progress connect failed. Removing.");
        log_info(LD_NET,"in-progress connect failed. Removing.");
        if (CONN_IS_EDGE(conn))
          connection_edge_end_errno(conn, conn->cpath_layer);

@@ -1453,19 +1460,19 @@ connection_handle_write(connection_t *conn)
    switch (result) {
      case TOR_TLS_ERROR:
      case TOR_TLS_CLOSE:
        info(LD_NET,result==TOR_TLS_ERROR?
        log_info(LD_NET,result==TOR_TLS_ERROR?
                 "tls error. breaking.":"TLS connection closed on flush");
        /* Don't flush; connection is dead. */
        connection_close_immediate(conn);
        connection_mark_for_close(conn);
        return -1;
      case TOR_TLS_WANTWRITE:
        debug(LD_NET,"wanted write.");
        log_debug(LD_NET,"wanted write.");
        /* we're already writing */
        return 0;
      case TOR_TLS_WANTREAD:
        /* Make sure to avoid a loop if the receive buckets are empty. */
        debug(LD_NET,"wanted read.");
        log_debug(LD_NET,"wanted read.");
        if (!connection_is_reading(conn)) {
          connection_stop_writing(conn);
          conn->wants_to_write = 1;
@@ -1565,11 +1572,13 @@ connection_write_to_buf(const char *string, size_t len, connection_t *conn)
    if (CONN_IS_EDGE(conn)) {
      /* if it failed, it means we have our package/delivery windows set
         wrong compared to our max outbuf size. close the whole circuit. */
      warn(LD_NET,"write_to_buf failed. Closing circuit (fd %d).", conn->s);
      log_warn(LD_NET,
               "write_to_buf failed. Closing circuit (fd %d).", conn->s);
      circuit_mark_for_close(circuit_get_by_edge_conn(conn),
                             END_CIRC_REASON_INTERNAL);
    } else {
      warn(LD_NET,"write_to_buf failed. Closing connection (fd %d).", conn->s);
      log_warn(LD_NET,
               "write_to_buf failed. Closing connection (fd %d).", conn->s);
      connection_mark_for_close(conn);
    }
    return;
@@ -1849,7 +1858,7 @@ client_check_address_changed(int sock)

  if (getsockname(sock, (struct sockaddr*)&out_addr, &out_addr_len)<0) {
    int e = tor_socket_errno(sock);
    warn(LD_NET, "getsockname() failed: %s", tor_socket_strerror(e));
    log_warn(LD_NET, "getsockname() failed: %s", tor_socket_strerror(e));
    return;
  }

@@ -1908,7 +1917,7 @@ connection_process_inbuf(connection_t *conn, int package_partial)
    case CONN_TYPE_CONTROL:
      return connection_control_process_inbuf(conn);
    default:
      err(LD_BUG,"Bug: got unexpected conn type %d.", conn->type);
      log_err(LD_BUG,"Bug: got unexpected conn type %d.", conn->type);
      tor_fragile_assert();
      return -1;
  }
@@ -1942,7 +1951,7 @@ connection_finished_flushing(connection_t *conn)
    case CONN_TYPE_CONTROL:
      return connection_control_finished_flushing(conn);
    default:
      err(LD_BUG,"Bug: got unexpected conn type %d.", conn->type);
      log_err(LD_BUG,"Bug: got unexpected conn type %d.", conn->type);
      tor_fragile_assert();
      return -1;
  }
@@ -1967,7 +1976,7 @@ connection_finished_connecting(connection_t *conn)
    case CONN_TYPE_DIR:
      return connection_dir_finished_connecting(conn);
    default:
      err(LD_BUG,"Bug: got unexpected conn type %d.", conn->type);
      log_err(LD_BUG,"Bug: got unexpected conn type %d.", conn->type);
      tor_fragile_assert();
      return -1;
  }
@@ -1992,7 +2001,7 @@ connection_reached_eof(connection_t *conn)
    case CONN_TYPE_CONTROL:
      return connection_control_reached_eof(conn);
    default:
      err(LD_BUG,"Bug: got unexpected conn type %d.", conn->type);
      log_err(LD_BUG,"Bug: got unexpected conn type %d.", conn->type);
      tor_fragile_assert();
      return -1;
  }
+119 −106

File changed.

Preview size limit exceeded, changes collapsed.

+41 −38
Original line number Diff line number Diff line
@@ -137,7 +137,7 @@ cell_unpack(cell_t *dest, const char *src)
int
connection_or_reached_eof(connection_t *conn)
{
  info(LD_OR,"OR connection reached EOF. Closing.");
  log_info(LD_OR,"OR connection reached EOF. Closing.");
  connection_mark_for_close(conn);
  return 0;
}
@@ -160,18 +160,19 @@ connection_or_read_proxy_response(connection_t *conn)
                              &headers, MAX_HEADERS_SIZE,
                              NULL, NULL, 10000, 0)) {
    case -1: /* overflow */
      warn(LD_PROTOCOL,"Your https proxy sent back an oversized response. "
           "Closing.");
      log_warn(LD_PROTOCOL,
               "Your https proxy sent back an oversized response. Closing.");
      return -1;
    case 0:
      info(LD_OR,"https proxy response not all here yet. Waiting.");
      log_info(LD_OR,"https proxy response not all here yet. Waiting.");
      return 0;
    /* case 1, fall through */
  }

  if (parse_http_response(headers, &status_code, &date_header,
                          &compression, &reason) < 0) {
    warn(LD_OR,"Unparseable headers from proxy (connecting to '%s'). Closing.",
    log_warn(LD_OR,
             "Unparseable headers from proxy (connecting to '%s'). Closing.",
             conn->address);
    tor_free(headers);
    return -1;
@@ -179,7 +180,7 @@ connection_or_read_proxy_response(connection_t *conn)
  if (!reason) reason = tor_strdup("[no reason given]");

  if (status_code == 200) {
    info(LD_OR,
    log_info(LD_OR,
             "HTTPS connect to '%s' successful! (200 \"%s\") Starting TLS.",
             conn->address, reason);
    tor_free(reason);
@@ -192,7 +193,7 @@ connection_or_read_proxy_response(connection_t *conn)
    return 0;
  }
  /* else, bad news on the status code */
  warn(LD_OR,
  log_warn(LD_OR,
           "The https proxy sent back an unexpected status code %d (\"%s\"). "
           "Closing.",
           status_code, reason);
@@ -240,7 +241,7 @@ connection_or_finished_flushing(connection_t *conn)

  switch (conn->state) {
    case OR_CONN_STATE_PROXY_FLUSHING:
      debug(LD_OR,"finished sending CONNECT to proxy.");
      log_debug(LD_OR,"finished sending CONNECT to proxy.");
      conn->state = OR_CONN_STATE_PROXY_READING;
      connection_stop_writing(conn);
      break;
@@ -248,7 +249,7 @@ connection_or_finished_flushing(connection_t *conn)
      connection_stop_writing(conn);
      break;
    default:
      err(LD_BUG,"BUG: called in unexpected state %d.", conn->state);
      log_err(LD_BUG,"BUG: called in unexpected state %d.", conn->state);
      tor_fragile_assert();
      return -1;
  }
@@ -264,7 +265,7 @@ connection_or_finished_connecting(connection_t *conn)
  tor_assert(conn->type == CONN_TYPE_OR);
  tor_assert(conn->state == OR_CONN_STATE_CONNECTING);

  debug(LD_OR,"OR connect() to router at %s:%u finished.",
  log_debug(LD_OR,"OR connect() to router at %s:%u finished.",
            conn->address,conn->port);

  if (get_options()->HttpsProxy) {
@@ -280,7 +281,7 @@ connection_or_finished_connecting(connection_t *conn)
    if (authenticator) {
      base64_authenticator = alloc_http_authenticator(authenticator);
      if (!base64_authenticator)
        warn(LD_OR, "Encoding https authenticator failed");
        log_warn(LD_OR, "Encoding https authenticator failed");
    }
    if (base64_authenticator) {
      tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
@@ -428,7 +429,7 @@ connection_or_connect(uint32_t addr, uint16_t port, const char *id_digest)

  if (server_mode(options) && (me=router_get_my_routerinfo()) &&
      router_digest_is_me(id_digest)) {
    info(LD_PROTOCOL,"Client asked me to connect to myself. Refusing.");
    log_info(LD_PROTOCOL,"Client asked me to connect to myself. Refusing.");
    return NULL;
  }

@@ -486,11 +487,11 @@ connection_tls_start_handshake(connection_t *conn, int receiving)
  conn->state = OR_CONN_STATE_HANDSHAKING;
  conn->tls = tor_tls_new(conn->s, receiving, 0);
  if (!conn->tls) {
    warn(LD_BUG,"tor_tls_new failed. Closing.");
    log_warn(LD_BUG,"tor_tls_new failed. Closing.");
    return -1;
  }
  connection_start_reading(conn);
  debug(LD_OR,"starting TLS handshake on fd %d", conn->s);
  log_debug(LD_OR,"starting TLS handshake on fd %d", conn->s);
  if (connection_tls_continue_handshake(conn) < 0) {
    return -1;
  }
@@ -509,16 +510,16 @@ connection_tls_continue_handshake(connection_t *conn)
  switch (tor_tls_handshake(conn->tls)) {
    case TOR_TLS_ERROR:
    case TOR_TLS_CLOSE:
      info(LD_OR,"tls error. breaking connection.");
      log_info(LD_OR,"tls error. breaking connection.");
      return -1;
    case TOR_TLS_DONE:
     return connection_tls_finish_handshake(conn);
    case TOR_TLS_WANTWRITE:
      connection_start_writing(conn);
      debug(LD_OR,"wanted write");
      log_debug(LD_OR,"wanted write");
      return 0;
    case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
      debug(LD_OR,"wanted read");
      log_debug(LD_OR,"wanted read");
      return 0;
  }
  return 0;
@@ -570,7 +571,7 @@ connection_or_check_valid_handshake(connection_t *conn, char *digest_rcvd)

  check_no_tls_errors();
  if (! tor_tls_peer_has_cert(conn->tls)) {
    info(LD_PROTOCOL,"Peer didn't send a cert! Closing.");
    log_info(LD_PROTOCOL,"Peer didn't send a cert! Closing.");
    return -1;
  }
  check_no_tls_errors();
@@ -581,7 +582,7 @@ connection_or_check_valid_handshake(connection_t *conn, char *digest_rcvd)
    return -1;
  }
  check_no_tls_errors();
  debug(LD_OR, "Other side (%s:%d) claims to be router '%s'",
  log_debug(LD_OR, "Other side (%s:%d) claims to be router '%s'",
            conn->address, conn->port, nickname);

  if (tor_tls_verify(severity, conn->tls, &identity_rcvd) < 0) {
@@ -591,7 +592,7 @@ connection_or_check_valid_handshake(connection_t *conn, char *digest_rcvd)
    return -1;
  }
  check_no_tls_errors();
  debug(LD_OR,"The router's cert is valid.");
  log_debug(LD_OR,"The router's cert is valid.");
  crypto_pk_get_digest(identity_rcvd, digest_rcvd);

  if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
@@ -661,7 +662,7 @@ connection_tls_finish_handshake(connection_t *conn)
  char digest_rcvd[DIGEST_LEN];
  int started_here = connection_or_nonopen_was_started_here(conn);

  debug(LD_OR,"tls handshake done. verifying.");
  log_debug(LD_OR,"tls handshake done. verifying.");
  if (connection_or_check_valid_handshake(conn, digest_rcvd) < 0)
    return -1;

@@ -669,7 +670,8 @@ connection_tls_finish_handshake(connection_t *conn)
#if 0
    connection_t *c;
    if ((c=connection_or_get_by_identity_digest(digest_rcvd))) {
      debug(LD_OR,"Router '%s' is already connected on fd %d. Dropping fd %d.",
      log_debug(LD_OR,
                "Router '%s' is already connected on fd %d. Dropping fd %d.",
                c->nickname, c->s, conn->s);
      return -1;
    }
@@ -730,7 +732,7 @@ connection_or_write_cell_to_buf(const cell_t *cell, connection_t *conn)
    if (connection_handle_write(conn) < 0) {
      if (!conn->marked_for_close) {
        /* this connection is broken. remove it. */
        warn(LD_BUG,
        log_warn(LD_BUG,
                 "Bug: unhandled error on write for OR conn (fd %d); removing",
                 conn->s);
        tor_fragile_assert();
@@ -761,7 +763,8 @@ connection_or_process_cells_from_inbuf(connection_t *conn)
  cell_t cell;

loop:
  debug(LD_OR,"%d: starting, inbuf_datalen %d (%d pending in tls object).",
  log_debug(LD_OR,
            "%d: starting, inbuf_datalen %d (%d pending in tls object).",
            conn->s,(int)buf_datalen(conn->inbuf),
            tor_tls_get_pending_bytes(conn->tls));
  if (buf_datalen(conn->inbuf) < CELL_NETWORK_SIZE) /* whole response
@@ -797,7 +800,7 @@ connection_or_send_destroy(uint16_t circ_id, connection_t *conn, int reason)
  cell.circ_id = circ_id;
  cell.command = CELL_DESTROY;
  cell.payload[0] = (uint8_t) reason;
  debug(LD_OR,"Sending destroy (circID %d).", circ_id);
  log_debug(LD_OR,"Sending destroy (circID %d).", circ_id);
  connection_or_write_cell_to_buf(&cell, conn);
  return 0;
}
+41 −34
Original line number Diff line number Diff line
@@ -700,7 +700,7 @@ control_setconf_helper(connection_t *conn, uint32_t len, char *body,
    *outp = '\0';

    if (config_get_lines(config, &lines) < 0) {
      warn(LD_CONTROL,"Controller gave us config lines we can't parse.");
      log_warn(LD_CONTROL,"Controller gave us config lines we can't parse.");
      connection_write_str_to_buf("551 Couldn't parse configuration\r\n",
                                  conn);
      tor_free(config);
@@ -709,7 +709,7 @@ control_setconf_helper(connection_t *conn, uint32_t len, char *body,
    tor_free(config);
  } else {
    if (config_get_lines(body, &lines) < 0) {
      warn(LD_CONTROL,"Controller gave us config lines we can't parse.");
      log_warn(LD_CONTROL,"Controller gave us config lines we can't parse.");
      send_control0_error(conn, ERR_SYNTAX, "Couldn't parse configuration");
      return 0;
    }
@@ -718,7 +718,8 @@ control_setconf_helper(connection_t *conn, uint32_t len, char *body,
  if ((r=options_trial_assign(lines, use_defaults, clear_first)) < 0) {
    int v0_err;
    const char *msg;
    warn(LD_CONTROL,"Controller gave us config lines that didn't validate.");
    log_warn(LD_CONTROL,
             "Controller gave us config lines that didn't validate.");
    switch (r) {
      case -1:
        v0_err = ERR_UNRECOGNIZED_CONFIG_KEY;
@@ -1024,7 +1025,8 @@ handle_control_authenticate(connection_t *conn, uint32_t len, const char *body)
    char expected[S2K_SPECIFIER_LEN+DIGEST_LEN];
    char received[DIGEST_LEN];
    if (decode_hashed_password(expected, options->HashedControlPassword)<0) {
      warn(LD_CONTROL,"Couldn't decode HashedControlPassword: invalid base16");
      log_warn(LD_CONTROL,
               "Couldn't decode HashedControlPassword: invalid base16");
      goto err;
    }
    secret_to_key(received,DIGEST_LEN,password,password_len,expected);
@@ -1052,7 +1054,7 @@ handle_control_authenticate(connection_t *conn, uint32_t len, const char *body)
  }
  return 0;
 ok:
  info(LD_CONTROL, "Authenticated control connection (%d)", conn->s);
  log_info(LD_CONTROL, "Authenticated control connection (%d)", conn->s);
  send_control_done(conn);
  if (STATE_IS_V0(conn->state))
    conn->state = CONTROL_CONN_STATE_OPEN_V0;
@@ -1168,16 +1170,18 @@ handle_control_mapaddress(connection_t *conn, uint32_t len, const char *body)
      const char *from = smartlist_get(elts,0);
      const char *to = smartlist_get(elts,1);
      if (!is_plausible_address(from)) {
        warn(LD_CONTROL,"Skipping invalid argument '%s' in MapAddress msg",
        log_warn(LD_CONTROL,
                 "Skipping invalid argument '%s' in MapAddress msg",
             from);
      } else if (!is_plausible_address(to)) {
        warn(LD_CONTROL,"Skipping invalid argument '%s' in MapAddress msg",to);
        log_warn(LD_CONTROL,
                 "Skipping invalid argument '%s' in MapAddress msg", to);
      } else if (!strcmp(from, ".") || !strcmp(from, "0.0.0.0")) {
        const char *address = addressmap_register_virtual_address(
              !strcmp(from,".") ? RESOLVED_TYPE_HOSTNAME : RESOLVED_TYPE_IPV4,
               tor_strdup(to));
        if (!address) {
          warn(LD_CONTROL,
          log_warn(LD_CONTROL,
                   "Unable to allocate address for '%s' in MapAddress msg",
                   safe_str(line));
        } else {
@@ -1201,7 +1205,8 @@ handle_control_mapaddress(connection_t *conn, uint32_t len, const char *body)
        }
      }
    } else {
      warn(LD_CONTROL, "Skipping MapAddress line with wrong number of items.");
      log_warn(LD_CONTROL,
               "Skipping MapAddress line with wrong number of items.");
    }
    SMARTLIST_FOREACH(elts, char *, cp, tor_free(cp));
    smartlist_clear(elts);
@@ -1375,7 +1380,7 @@ handle_getinfo_helper(const char *question, char **answer)
        case AP_CONN_STATE_OPEN:
          state = "SUCCEEDED"; break;
        default:
          warn(LD_BUG, "Asked for stream in unknown state %d",
          log_warn(LD_BUG, "Asked for stream in unknown state %d",
                   conns[i]->state);
          continue;
        }
@@ -1645,7 +1650,7 @@ handle_control_extendcircuit(connection_t *conn, uint32_t len,
    if (circ->state == CIRCUIT_STATE_OPEN) {
      circuit_set_state(circ, CIRCUIT_STATE_BUILDING);
      if (circuit_send_next_onion_skin(circ) < 0) {
        info(LD_CONTROL,
        log_info(LD_CONTROL,
                 "send_next_onion_skin failed; circuit marked for closing.");
        circuit_mark_for_close(circ, END_CIRC_AT_ORIGIN);
        if (v0)
@@ -1977,7 +1982,7 @@ handle_control_closecircuit(connection_t *conn, uint32_t len,
        if (!strcasecmp(smartlist_get(args, i), "IfUnused"))
          safe = 1;
        else
          info(LD_CONTROL, "Skipping unknown option %s",
          log_info(LD_CONTROL, "Skipping unknown option %s",
                   (char*)smartlist_get(args,i));
      }
    }
@@ -2006,7 +2011,7 @@ handle_control_fragments(connection_t *conn, uint16_t command_type,
{
  if (command_type == CONTROL0_CMD_FRAGMENTHEADER) {
    if (conn->incoming_cmd) {
      warn(LD_CONTROL, "Dropping incomplete fragmented command");
      log_warn(LD_CONTROL, "Dropping incomplete fragmented command");
      tor_free(conn->incoming_cmd);
    }
    if (body_len < 6) {
@@ -2058,7 +2063,7 @@ connection_control_reached_eof(connection_t *conn)
  tor_assert(conn);
  tor_assert(conn->type == CONN_TYPE_CONTROL);

  info(LD_CONTROL,"Control connection reached EOF. Closing.");
  log_info(LD_CONTROL,"Control connection reached EOF. Closing.");
  connection_mark_for_close(conn);
  return 0;
}
@@ -2223,13 +2228,14 @@ connection_control_process_inbuf_v0(connection_t *conn)
    {
    case -2:
      tor_free(body);
      info(LD_CONTROL, "Detected v1 control protocol on connection (fd %d)",
      log_info(LD_CONTROL,
               "Detected v1 control protocol on connection (fd %d)",
               conn->s);
      conn->state = CONTROL_CONN_STATE_NEEDAUTH_V1;
      return connection_control_process_inbuf_v1(conn);
    case -1:
      tor_free(body);
      warn(LD_CONTROL, "Error in control command. Failing.");
      log_warn(LD_CONTROL, "Error in control command. Failing.");
      return -1;
    case 0:
      /* Control command not all here yet. Wait. */
@@ -2245,7 +2251,7 @@ connection_control_process_inbuf_v0(connection_t *conn)
   * commands will be considered. */
  if (conn->state == CONTROL_CONN_STATE_NEEDAUTH_V0 &&
      command_type != CONTROL0_CMD_AUTHENTICATE) {
    info(LD_CONTROL, "Rejecting '%s' command; authentication needed.",
    log_info(LD_CONTROL, "Rejecting '%s' command; authentication needed.",
             control_cmd_to_string(command_type));
    send_control0_error(conn, ERR_UNAUTHORIZED, "Authentication required");
    tor_free(body);
@@ -2265,7 +2271,7 @@ connection_control_process_inbuf_v0(connection_t *conn)
    body = conn->incoming_cmd;
    conn->incoming_cmd = NULL;
  } else if (conn->incoming_cmd) {
    warn(LD_CONTROL, "Dropping incomplete fragmented command");
    log_warn(LD_CONTROL, "Dropping incomplete fragmented command");
    tor_free(conn->incoming_cmd);
  }

@@ -2333,17 +2339,18 @@ connection_control_process_inbuf_v0(connection_t *conn)
    case CONTROL0_CMD_CONFVALUE:
    case CONTROL0_CMD_EVENT:
    case CONTROL0_CMD_INFOVALUE:
      warn(LD_CONTROL, "Received client-only '%s' command; ignoring.",
      log_warn(LD_CONTROL, "Received client-only '%s' command; ignoring.",
               control_cmd_to_string(command_type));
      send_control0_error(conn, ERR_UNRECOGNIZED_TYPE,
                         "Command type only valid from server to tor client");
      break;
    case CONTROL0_CMD_FRAGMENTHEADER:
    case CONTROL0_CMD_FRAGMENT:
      warn(LD_CONTROL, "Recieved command fragment out of order; ignoring.");
      log_warn(LD_CONTROL,
               "Recieved command fragment out of order; ignoring.");
      send_control0_error(conn, ERR_SYNTAX, "Bad fragmentation on command.");
    default:
      warn(LD_CONTROL, "Received unrecognized command type %d; ignoring.",
      log_warn(LD_CONTROL, "Received unrecognized command type %d; ignoring.",
               (int)command_type);
      send_control0_error(conn, ERR_UNRECOGNIZED_TYPE,
                         "Unrecognized command type");
@@ -2399,7 +2406,7 @@ control_event_circuit_status(circuit_t *circ, circuit_status_event_t tp)
      case CIRC_EVENT_FAILED: status = "FAILED"; break;
      case CIRC_EVENT_CLOSED: status = "CLOSED"; break;
      default:
        warn(LD_BUG, "Unrecognized status code %d", (int)tp);
        log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
        return 0;
      }
    send_control1_event(EVENT_CIRCUIT_STATUS,
@@ -2472,7 +2479,7 @@ control_event_stream_status(connection_t *conn, stream_status_event_t tp)
      case STREAM_EVENT_NEW_RESOLVE: status = "NEWRESOLVE"; break;
      case STREAM_EVENT_FAILED_RETRIABLE: status = "DETACHED"; break;
      default:
        warn(LD_BUG, "Unrecognized status code %d", (int)tp);
        log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
        return 0;
      }
    circ = circuit_get_by_edge_conn(conn);
@@ -2514,7 +2521,7 @@ control_event_or_conn_status(connection_t *conn,or_conn_status_event_t tp)
      case OR_CONN_EVENT_FAILED: status = "FAILED"; break;
      case OR_CONN_EVENT_CLOSED: status = "CLOSED"; break;
      default:
        warn(LD_BUG, "Unrecognized status code %d", (int)tp);
        log_warn(LD_BUG, "Unrecognized status code %d", (int)tp);
        return 0;
      }
    send_control1_event(EVENT_OR_CONN_STATUS,
@@ -2731,7 +2738,7 @@ init_cookie_authentication(int enabled)
  authentication_cookie_is_set = 1;
  if (write_bytes_to_file(fname, authentication_cookie,
                          AUTHENTICATION_COOKIE_LEN, 1)) {
    warn(LD_FS,"Error writing authentication cookie.");
    log_warn(LD_FS,"Error writing authentication cookie.");
    return -1;
  }

Loading