tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

connection.c (203333B)


      1 /* Copyright (c) 2001 Matej Pfajfar.
      2 * Copyright (c) 2001-2004, Roger Dingledine.
      3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
      4 * Copyright (c) 2007-2021, The Tor Project, Inc. */
      5 /* See LICENSE for licensing information */
      6 
      7 /**
      8 * \file connection.c
      9 * \brief General high-level functions to handle reading and writing
     10 * on connections.
     11 *
     12 * Each connection (ideally) represents a TLS connection, a TCP socket, a unix
     13 * socket, or a UDP socket on which reads and writes can occur.  (But see
     14 * connection_edge.c for cases where connections can also represent streams
     15 * that do not have a corresponding socket.)
     16 *
     17 * The module implements the abstract type, connection_t.  The subtypes are:
     18 *  <ul>
     19 *   <li>listener_connection_t, implemented here in connection.c
     20 *   <li>dir_connection_t, implemented in directory.c
     21 *   <li>or_connection_t, implemented in connection_or.c
     22 *   <li>edge_connection_t, implemented in connection_edge.c, along with
     23 *      its subtype(s):
     24 *      <ul><li>entry_connection_t, also implemented in connection_edge.c
     25 *      </ul>
     26 *   <li>control_connection_t, implemented in control.c
     27 *  </ul>
     28 *
     29 * The base type implemented in this module is responsible for basic
     30 * rate limiting, flow control, and marshalling bytes onto and off of the
     31 * network (either directly or via TLS).
     32 *
     33 * Connections are registered with the main loop with connection_add(). As
     34 * they become able to read or write register the fact with the event main
     35 * loop by calling connection_watch_events(), connection_start_reading(), or
     36 * connection_start_writing().  When they no longer want to read or write,
     37 * they call connection_stop_reading() or connection_stop_writing().
     38 *
     39 * To queue data to be written on a connection, call
     40 * connection_buf_add().  When data arrives, the
     41 * connection_process_inbuf() callback is invoked, which dispatches to a
     42 * type-specific function (such as connection_edge_process_inbuf() for
     43 * example). Connection types that need notice of when data has been written
     44 * receive notification via connection_flushed_some() and
     45 * connection_finished_flushing().  These functions all delegate to
     46 * type-specific implementations.
     47 *
     48 * Additionally, beyond the core of connection_t, this module also implements:
     49 * <ul>
     50 * <li>Listeners, which wait for incoming sockets and launch connections
     51 * <li>Outgoing SOCKS proxy support
     52 * <li>Outgoing HTTP proxy support
     53 * <li>An out-of-sockets handler for dealing with socket exhaustion
     54 * </ul>
     55 **/
     56 
     57 #define CONNECTION_PRIVATE
     58 #include "core/or/or.h"
     59 #include "feature/client/bridges.h"
     60 #include "lib/buf/buffers.h"
     61 #include "lib/tls/buffers_tls.h"
     62 #include "lib/err/backtrace.h"
     63 
     64 /*
     65 * Define this so we get channel internal functions, since we're implementing
     66 * part of a subclass (channel_tls_t).
     67 */
     68 #define CHANNEL_OBJECT_PRIVATE
     69 #include "app/config/config.h"
     70 #include "app/config/resolve_addr.h"
     71 #include "core/mainloop/connection.h"
     72 #include "core/mainloop/mainloop.h"
     73 #include "core/mainloop/netstatus.h"
     74 #include "core/or/channel.h"
     75 #include "core/or/channeltls.h"
     76 #include "core/or/circuitbuild.h"
     77 #include "core/or/circuitlist.h"
     78 #include "core/or/circuituse.h"
     79 #include "core/or/connection_edge.h"
     80 #include "core/or/connection_or.h"
     81 #include "core/or/dos.h"
     82 #include "core/or/policies.h"
     83 #include "core/or/reasons.h"
     84 #include "core/or/relay.h"
     85 #include "core/or/status.h"
     86 #include "core/or/crypt_path.h"
     87 #include "core/proto/proto_haproxy.h"
     88 #include "core/proto/proto_http.h"
     89 #include "core/proto/proto_socks.h"
     90 #include "feature/client/dnsserv.h"
     91 #include "feature/client/entrynodes.h"
     92 #include "feature/client/transports.h"
     93 #include "feature/control/control.h"
     94 #include "feature/control/control_events.h"
     95 #include "feature/dirauth/authmode.h"
     96 #include "feature/dirauth/dirauth_config.h"
     97 #include "feature/dircache/dirserv.h"
     98 #include "feature/dircommon/directory.h"
     99 #include "feature/hibernate/hibernate.h"
    100 #include "feature/hs/hs_common.h"
    101 #include "feature/hs/hs_ident.h"
    102 #include "feature/hs/hs_metrics.h"
    103 #include "feature/metrics/metrics.h"
    104 #include "feature/nodelist/nodelist.h"
    105 #include "feature/nodelist/routerlist.h"
    106 #include "feature/relay/dns.h"
    107 #include "feature/relay/ext_orport.h"
    108 #include "feature/relay/routermode.h"
    109 #include "feature/rend/rendcommon.h"
    110 #include "feature/stats/connstats.h"
    111 #include "feature/stats/rephist.h"
    112 #include "feature/stats/bwhist.h"
    113 #include "lib/crypt_ops/crypto_util.h"
    114 #include "lib/crypt_ops/crypto_format.h"
    115 #include "lib/geoip/geoip.h"
    116 
    117 #include "lib/cc/ctassert.h"
    118 #include "lib/sandbox/sandbox.h"
    119 #include "lib/net/buffers_net.h"
    120 #include "lib/net/address.h"
    121 #include "lib/tls/tortls.h"
    122 #include "lib/evloop/compat_libevent.h"
    123 #include "lib/compress/compress.h"
    124 
    125 #ifdef HAVE_PWD_H
    126 #include <pwd.h>
    127 #endif
    128 
    129 #ifdef HAVE_UNISTD_H
    130 #include <unistd.h>
    131 #endif
    132 #ifdef HAVE_SYS_STAT_H
    133 #include <sys/stat.h>
    134 #endif
    135 
    136 #ifdef HAVE_SYS_UN_H
    137 #include <sys/socket.h>
    138 #include <sys/un.h>
    139 #endif
    140 
    141 #include "feature/dircommon/dir_connection_st.h"
    142 #include "feature/control/control_connection_st.h"
    143 #include "core/or/entry_connection_st.h"
    144 #include "core/or/listener_connection_st.h"
    145 #include "core/or/or_connection_st.h"
    146 #include "core/or/port_cfg_st.h"
    147 #include "feature/nodelist/routerinfo_st.h"
    148 #include "core/or/socks_request_st.h"
    149 
    150 #include "core/or/congestion_control_flow.h"
    151 
    152 /**
    153 * On Windows and Linux we cannot reliably bind() a socket to an
    154 * address and port if: 1) There's already a socket bound to wildcard
    155 * address (0.0.0.0 or ::) with the same port; 2) We try to bind()
    156 * to wildcard address and there's another socket bound to a
    157 * specific address and the same port.
    158 *
    159 * To address this problem on these two platforms we implement a
    160 * routine that:
    161 * 1) Checks if first attempt to bind() a new socket  failed with
    162 * EADDRINUSE.
    163 * 2) If so, it will close the appropriate old listener connection and
    164 * 3) Attempts bind()'ing the new listener socket again.
    165 *
    166 * Just to be safe, we are enabling listener rebind code on all platforms,
    167 * to account for unexpected cases where it may be needed.
    168 */
    169 #define ENABLE_LISTENER_REBIND
    170 
    171 static connection_t *connection_listener_new(
    172                               const struct sockaddr *listensockaddr,
    173                               socklen_t listensocklen, int type,
    174                               const char *address,
    175                               const port_cfg_t *portcfg,
    176                               int *addr_in_use);
    177 static connection_t *connection_listener_new_for_port(
    178                               const port_cfg_t *port,
    179                               int *defer, int *addr_in_use);
    180 static void connection_init(time_t now, connection_t *conn, int type,
    181                            int socket_family);
    182 static int connection_handle_listener_read(connection_t *conn, int new_type);
    183 static int connection_finished_flushing(connection_t *conn);
    184 static int connection_flushed_some(connection_t *conn);
    185 static int connection_finished_connecting(connection_t *conn);
    186 static int connection_reached_eof(connection_t *conn);
    187 static int connection_buf_read_from_socket(connection_t *conn,
    188                                           ssize_t *max_to_read,
    189                                           int *socket_error);
    190 static void client_check_address_changed(tor_socket_t sock);
    191 static void set_constrained_socket_buffers(tor_socket_t sock, int size);
    192 
    193 static const char *connection_proxy_state_to_string(int state);
    194 static int connection_read_https_proxy_response(connection_t *conn);
    195 static void connection_send_socks5_connect(connection_t *conn);
    196 static const char *proxy_type_to_string(int proxy_type);
    197 static int conn_get_proxy_type(const connection_t *conn);
    198 const tor_addr_t *conn_get_outbound_address(sa_family_t family,
    199                  const or_options_t *options, unsigned int conn_type);
    200 static void reenable_blocked_connection_init(const or_options_t *options);
    201 static void reenable_blocked_connection_schedule(void);
    202 
    203 /** The last addresses that our network interface seemed to have been
    204 * binding to.  We use this as one way to detect when our IP changes.
    205 *
    206 * XXXX+ We should really use the entire list of interfaces here.
    207 **/
    208 static tor_addr_t *last_interface_ipv4 = NULL;
    209 /* DOCDOC last_interface_ipv6 */
    210 static tor_addr_t *last_interface_ipv6 = NULL;
    211 /** A list of tor_addr_t for addresses we've used in outgoing connections.
    212 * Used to detect IP address changes. */
    213 static smartlist_t *outgoing_addrs = NULL;
    214 
    215 #define CASE_ANY_LISTENER_TYPE \
    216    case CONN_TYPE_OR_LISTENER: \
    217    case CONN_TYPE_EXT_OR_LISTENER: \
    218    case CONN_TYPE_AP_LISTENER: \
    219    case CONN_TYPE_DIR_LISTENER: \
    220    case CONN_TYPE_CONTROL_LISTENER: \
    221    case CONN_TYPE_AP_TRANS_LISTENER: \
    222    case CONN_TYPE_AP_NATD_LISTENER: \
    223    case CONN_TYPE_AP_DNS_LISTENER: \
    224    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: \
    225    case CONN_TYPE_METRICS_LISTENER
    226 
    227 /**************************************************************/
    228 
    229 /**
    230 * Cast a `connection_t *` to a `listener_connection_t *`.
    231 *
    232 * Exit with an assertion failure if the input is not a
    233 * `listener_connection_t`.
    234 **/
    235 listener_connection_t *
    236 TO_LISTENER_CONN(connection_t *c)
    237 {
    238  tor_assert(c->magic == LISTENER_CONNECTION_MAGIC);
    239  return DOWNCAST(listener_connection_t, c);
    240 }
    241 
    242 /**
    243 * Cast a `const connection_t *` to a `const listener_connection_t *`.
    244 *
    245 * Exit with an assertion failure if the input is not a
    246 * `listener_connection_t`.
    247 **/
    248 const listener_connection_t *
    249 CONST_TO_LISTENER_CONN(const connection_t *c)
    250 {
    251  return TO_LISTENER_CONN((connection_t *)c);
    252 }
    253 
    254 size_t
    255 connection_get_inbuf_len(const connection_t *conn)
    256 {
    257  return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
    258 }
    259 
    260 size_t
    261 connection_get_outbuf_len(const connection_t *conn)
    262 {
    263    return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
    264 }
    265 
    266 /**
    267 * Return the human-readable name for the connection type <b>type</b>
    268 */
    269 const char *
    270 conn_type_to_string(int type)
    271 {
    272  static char buf[64];
    273  switch (type) {
    274    case CONN_TYPE_OR_LISTENER: return "OR listener";
    275    case CONN_TYPE_OR: return "OR";
    276    case CONN_TYPE_EXIT: return "Exit";
    277    case CONN_TYPE_AP_LISTENER: return "Socks listener";
    278    case CONN_TYPE_AP_TRANS_LISTENER:
    279      return "Transparent pf/netfilter listener";
    280    case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
    281    case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
    282    case CONN_TYPE_AP: return "Socks";
    283    case CONN_TYPE_DIR_LISTENER: return "Directory listener";
    284    case CONN_TYPE_DIR: return "Directory";
    285    case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
    286    case CONN_TYPE_CONTROL: return "Control";
    287    case CONN_TYPE_EXT_OR: return "Extended OR";
    288    case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
    289    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: return "HTTP tunnel listener";
    290    case CONN_TYPE_METRICS_LISTENER: return "Metrics listener";
    291    case CONN_TYPE_METRICS: return "Metrics";
    292    default:
    293      log_warn(LD_BUG, "unknown connection type %d", type);
    294      tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
    295      return buf;
    296  }
    297 }
    298 
    299 /**
    300 * Return the human-readable name for the connection state <b>state</b>
    301 * for the connection type <b>type</b>
    302 */
    303 const char *
    304 conn_state_to_string(int type, int state)
    305 {
    306  static char buf[96];
    307  switch (type) {
    308    CASE_ANY_LISTENER_TYPE:
    309      if (state == LISTENER_STATE_READY)
    310        return "ready";
    311      break;
    312    case CONN_TYPE_OR:
    313      switch (state) {
    314        case OR_CONN_STATE_CONNECTING: return "connect()ing";
    315        case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
    316        case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
    317        case OR_CONN_STATE_SERVER_VERSIONS_WAIT:
    318          return "waiting for V3+ handshake";
    319        case OR_CONN_STATE_OR_HANDSHAKING_V3:
    320          return "handshaking (Tor, v3 handshake)";
    321        case OR_CONN_STATE_OPEN: return "open";
    322      }
    323      break;
    324    case CONN_TYPE_EXT_OR:
    325      switch (state) {
    326        case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
    327          return "waiting for authentication type";
    328        case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
    329          return "waiting for client nonce";
    330        case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
    331          return "waiting for client hash";
    332        case EXT_OR_CONN_STATE_OPEN: return "open";
    333        case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
    334      }
    335      break;
    336    case CONN_TYPE_EXIT:
    337      switch (state) {
    338        case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
    339        case EXIT_CONN_STATE_CONNECTING: return "connecting";
    340        case EXIT_CONN_STATE_OPEN: return "open";
    341        case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
    342      }
    343      break;
    344    case CONN_TYPE_AP:
    345      switch (state) {
    346        case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
    347        case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
    348        case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
    349        case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
    350        case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
    351        case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
    352        case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
    353        case AP_CONN_STATE_OPEN: return "open";
    354      }
    355      break;
    356    case CONN_TYPE_DIR:
    357      switch (state) {
    358        case DIR_CONN_STATE_CONNECTING: return "connecting";
    359        case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
    360        case DIR_CONN_STATE_CLIENT_READING: return "client reading";
    361        case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
    362        case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
    363        case DIR_CONN_STATE_SERVER_WRITING: return "writing";
    364      }
    365      break;
    366    case CONN_TYPE_CONTROL:
    367      switch (state) {
    368        case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
    369        case CONTROL_CONN_STATE_NEEDAUTH:
    370          return "waiting for authentication (protocol v1)";
    371      }
    372      break;
    373  }
    374 
    375  if (state == 0) {
    376    return "uninitialized";
    377  }
    378 
    379  log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
    380  tor_snprintf(buf, sizeof(buf),
    381               "unknown state [%d] on unknown [%s] connection",
    382               state, conn_type_to_string(type));
    383  tor_assert_nonfatal_unreached_once();
    384  return buf;
    385 }
    386 
    387 /**
    388 * Helper: describe the peer or address of connection @a conn in a
    389 * human-readable manner.
    390 *
    391 * Returns a pointer to a static buffer; future calls to
    392 * connection_describe_peer_internal() will invalidate this buffer.
    393 *
    394 * If <b>include_preposition</b> is true, include a preposition before the
    395 * peer address.
    396 *
    397 * Nobody should parse the output of this function; it can and will change in
    398 * future versions of tor.
    399 **/
    400 static const char *
    401 connection_describe_peer_internal(const connection_t *conn,
    402                                  bool include_preposition)
    403 {
    404  IF_BUG_ONCE(!conn) {
    405    return "null peer";
    406  }
    407 
    408  static char peer_buf[256];
    409  const tor_addr_t *addr = &conn->addr;
    410  const char *address = NULL;
    411  const char *prep;
    412  bool scrub = false;
    413  char extra_buf[128];
    414  extra_buf[0] = 0;
    415 
    416  /* First, figure out the preposition to use */
    417  switch (conn->type) {
    418    CASE_ANY_LISTENER_TYPE:
    419      prep = "on";
    420      break;
    421    case CONN_TYPE_EXIT:
    422      prep = "to";
    423      break;
    424    case CONN_TYPE_CONTROL:
    425    case CONN_TYPE_AP:
    426    case CONN_TYPE_EXT_OR:
    427      prep = "from";
    428      break;
    429    default:
    430      prep = "with";
    431      break;
    432  }
    433 
    434  /* Now figure out the address. */
    435  if (conn->socket_family == AF_UNIX) {
    436    /* For unix sockets, we always use the `address` string. */
    437    address = conn->address ? conn->address : "unix socket";
    438  } else if (conn->type == CONN_TYPE_OR) {
    439    /* For OR connections, we have a lot to do. */
    440    const or_connection_t *or_conn = CONST_TO_OR_CONN(conn);
    441    /* We report the IDs we're talking to... */
    442    if (fast_digest_is_zero(or_conn->identity_digest)) {
    443      // This could be a client, so scrub it.  No identity to report.
    444      scrub = true;
    445    } else {
    446      const ed25519_public_key_t *ed_id =
    447        connection_or_get_alleged_ed25519_id(or_conn);
    448      char ed_id_buf[ED25519_BASE64_LEN+1];
    449      char rsa_id_buf[HEX_DIGEST_LEN+1];
    450      if (ed_id) {
    451        ed25519_public_to_base64(ed_id_buf, ed_id);
    452      } else {
    453        strlcpy(ed_id_buf, "<none>", sizeof(ed_id_buf));
    454      }
    455      base16_encode(rsa_id_buf, sizeof(rsa_id_buf),
    456                    or_conn->identity_digest, DIGEST_LEN);
    457      tor_snprintf(extra_buf, sizeof(extra_buf),
    458                   " ID=%s RSA_ID=%s", ed_id_buf, rsa_id_buf);
    459    }
    460    if (! scrub && (! tor_addr_eq(addr, &or_conn->canonical_orport.addr) ||
    461                    conn->port != or_conn->canonical_orport.port)) {
    462      /* We report canonical address, if it's different */
    463      char canonical_addr_buf[TOR_ADDR_BUF_LEN];
    464      if (tor_addr_to_str(canonical_addr_buf, &or_conn->canonical_orport.addr,
    465                          sizeof(canonical_addr_buf), 1)) {
    466        tor_snprintf(extra_buf+strlen(extra_buf),
    467                     sizeof(extra_buf)-strlen(extra_buf),
    468                     " canonical_addr=%s:%"PRIu16,
    469                     canonical_addr_buf,
    470                     or_conn->canonical_orport.port);
    471      }
    472    }
    473  } else if (conn->type == CONN_TYPE_EXIT) {
    474    scrub = true; /* This is a client's request; scrub it with SafeLogging. */
    475    if (tor_addr_is_null(addr)) {
    476      address = conn->address;
    477      strlcpy(extra_buf, " (DNS lookup pending)", sizeof(extra_buf));
    478    }
    479  }
    480 
    481  char addr_buf[TOR_ADDR_BUF_LEN];
    482  if (address == NULL) {
    483    if (tor_addr_family(addr) == 0) {
    484      address = "<unset>";
    485    } else {
    486      address = tor_addr_to_str(addr_buf, addr, sizeof(addr_buf), 1);
    487      if (!address) {
    488        address = "<can't format!>";
    489        tor_assert_nonfatal_unreached_once();
    490      }
    491    }
    492  }
    493 
    494  char portbuf[7];
    495  portbuf[0]=0;
    496  if (scrub && get_options()->SafeLogging_ != SAFELOG_SCRUB_NONE) {
    497    address = "[scrubbed]";
    498  } else {
    499    /* Only set the port if we're not scrubbing the address. */
    500    if (conn->port != 0) {
    501      tor_snprintf(portbuf, sizeof(portbuf), ":%d", conn->port);
    502    }
    503  }
    504 
    505  const char *sp = include_preposition ? " " : "";
    506  if (! include_preposition)
    507    prep = "";
    508 
    509  tor_snprintf(peer_buf, sizeof(peer_buf),
    510               "%s%s%s%s%s", prep, sp, address, portbuf, extra_buf);
    511  return peer_buf;
    512 }
    513 
    514 /**
    515 * Describe the peer or address of connection @a conn in a
    516 * human-readable manner.
    517 *
    518 * Returns a pointer to a static buffer; future calls to
    519 * connection_describe_peer() or connection_describe() will invalidate this
    520 * buffer.
    521 *
    522 * Nobody should parse the output of this function; it can and will change in
    523 * future versions of tor.
    524 **/
    525 const char *
    526 connection_describe_peer(const connection_t *conn)
    527 {
    528  return connection_describe_peer_internal(conn, false);
    529 }
    530 
    531 /**
    532 * Describe a connection for logging purposes.
    533 *
    534 * Returns a pointer to a static buffer; future calls to connection_describe()
    535 * will invalidate this buffer.
    536 *
    537 * Nobody should parse the output of this function; it can and will change in
    538 * future versions of tor.
    539 **/
    540 const char *
    541 connection_describe(const connection_t *conn)
    542 {
    543  IF_BUG_ONCE(!conn) {
    544    return "null connection";
    545  }
    546  static char desc_buf[256];
    547  const char *peer = connection_describe_peer_internal(conn, true);
    548  tor_snprintf(desc_buf, sizeof(desc_buf),
    549               "%s connection (%s) %s",
    550               conn_type_to_string(conn->type),
    551               conn_state_to_string(conn->type, conn->state),
    552               peer);
    553  return desc_buf;
    554 }
    555 
    556 /** Allocate and return a new dir_connection_t, initialized as by
    557 * connection_init(). */
    558 dir_connection_t *
    559 dir_connection_new(int socket_family)
    560 {
    561  dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
    562  connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
    563  return dir_conn;
    564 }
    565 
    566 /** Allocate and return a new or_connection_t, initialized as by
    567 * connection_init().
    568 *
    569 * Initialize active_circuit_pqueue.
    570 *
    571 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
    572 */
    573 or_connection_t *
    574 or_connection_new(int type, int socket_family)
    575 {
    576  or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
    577  time_t now = time(NULL);
    578  tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
    579  connection_init(now, TO_CONN(or_conn), type, socket_family);
    580 
    581  tor_addr_make_unspec(&or_conn->canonical_orport.addr);
    582  connection_or_set_canonical(or_conn, 0);
    583 
    584  if (type == CONN_TYPE_EXT_OR) {
    585    /* If we aren't told an address for this connection, we should
    586     * presume it isn't local, and should be rate-limited. */
    587    TO_CONN(or_conn)->always_rate_limit_as_remote = 1;
    588  }
    589 
    590  return or_conn;
    591 }
    592 
    593 /** Allocate and return a new entry_connection_t, initialized as by
    594 * connection_init().
    595 *
    596 * Allocate space to store the socks_request.
    597 */
    598 entry_connection_t *
    599 entry_connection_new(int type, int socket_family)
    600 {
    601  entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
    602  tor_assert(type == CONN_TYPE_AP);
    603  connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
    604  entry_conn->socks_request = socks_request_new();
    605  /* If this is coming from a listener, we'll set it up based on the listener
    606   * in a little while.  Otherwise, we're doing this as a linked connection
    607   * of some kind, and we should set it up here based on the socket family */
    608  if (socket_family == AF_INET)
    609    entry_conn->entry_cfg.ipv4_traffic = 1;
    610  else if (socket_family == AF_INET6)
    611    entry_conn->entry_cfg.ipv6_traffic = 1;
    612 
    613  /* Initialize the read token bucket to the maximum value which is the same as
    614   * no rate limiting. */
    615  token_bucket_rw_init(&ENTRY_TO_EDGE_CONN(entry_conn)->bucket, INT32_MAX,
    616                       INT32_MAX, monotime_coarse_get_stamp());
    617  return entry_conn;
    618 }
    619 
    620 /** Allocate and return a new edge_connection_t, initialized as by
    621 * connection_init(). */
    622 edge_connection_t *
    623 edge_connection_new(int type, int socket_family)
    624 {
    625  edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
    626  tor_assert(type == CONN_TYPE_EXIT);
    627  connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
    628  /* Initialize the read token bucket to the maximum value which is the same as
    629   * no rate limiting. */
    630  token_bucket_rw_init(&edge_conn->bucket, INT32_MAX, INT32_MAX,
    631                       monotime_coarse_get_stamp());
    632  return edge_conn;
    633 }
    634 
    635 /** Allocate and return a new control_connection_t, initialized as by
    636 * connection_init(). */
    637 control_connection_t *
    638 control_connection_new(int socket_family)
    639 {
    640  control_connection_t *control_conn =
    641    tor_malloc_zero(sizeof(control_connection_t));
    642  connection_init(time(NULL),
    643                  TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
    644  return control_conn;
    645 }
    646 
    647 /** Allocate and return a new listener_connection_t, initialized as by
    648 * connection_init(). */
    649 listener_connection_t *
    650 listener_connection_new(int type, int socket_family)
    651 {
    652  listener_connection_t *listener_conn =
    653    tor_malloc_zero(sizeof(listener_connection_t));
    654  connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
    655  /* Listener connections aren't accounted for with note_connection() so do
    656   * this explicitly so to count them. */
    657  rep_hist_note_conn_opened(false, type, socket_family);
    658  return listener_conn;
    659 }
    660 
    661 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
    662 * to make or receive connections of address family <b>socket_family</b>.  The
    663 * type should be one of the CONN_TYPE_* constants. */
    664 connection_t *
    665 connection_new(int type, int socket_family)
    666 {
    667  switch (type) {
    668    case CONN_TYPE_OR:
    669    case CONN_TYPE_EXT_OR:
    670      return TO_CONN(or_connection_new(type, socket_family));
    671 
    672    case CONN_TYPE_EXIT:
    673      return TO_CONN(edge_connection_new(type, socket_family));
    674 
    675    case CONN_TYPE_AP:
    676      return ENTRY_TO_CONN(entry_connection_new(type, socket_family));
    677 
    678    case CONN_TYPE_DIR:
    679      return TO_CONN(dir_connection_new(socket_family));
    680 
    681    case CONN_TYPE_CONTROL:
    682      return TO_CONN(control_connection_new(socket_family));
    683 
    684    CASE_ANY_LISTENER_TYPE:
    685      return TO_CONN(listener_connection_new(type, socket_family));
    686 
    687    default: {
    688      connection_t *conn = tor_malloc_zero(sizeof(connection_t));
    689      connection_init(time(NULL), conn, type, socket_family);
    690      return conn;
    691    }
    692  }
    693 }
    694 
    695 /** Initializes conn. (you must call connection_add() to link it into the main
    696 * array).
    697 *
    698 * Set conn-\>magic to the correct value.
    699 *
    700 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
    701 * -1 to signify they are not yet assigned.
    702 *
    703 * Initialize conn's timestamps to now.
    704 */
    705 static void
    706 connection_init(time_t now, connection_t *conn, int type, int socket_family)
    707 {
    708  static uint64_t n_connections_allocated = 1;
    709 
    710  switch (type) {
    711    case CONN_TYPE_OR:
    712    case CONN_TYPE_EXT_OR:
    713      conn->magic = OR_CONNECTION_MAGIC;
    714      break;
    715    case CONN_TYPE_EXIT:
    716      conn->magic = EDGE_CONNECTION_MAGIC;
    717      break;
    718    case CONN_TYPE_AP:
    719      conn->magic = ENTRY_CONNECTION_MAGIC;
    720      break;
    721    case CONN_TYPE_DIR:
    722      conn->magic = DIR_CONNECTION_MAGIC;
    723      break;
    724    case CONN_TYPE_CONTROL:
    725      conn->magic = CONTROL_CONNECTION_MAGIC;
    726      break;
    727    CASE_ANY_LISTENER_TYPE:
    728      conn->magic = LISTENER_CONNECTION_MAGIC;
    729      break;
    730    default:
    731      conn->magic = BASE_CONNECTION_MAGIC;
    732      break;
    733  }
    734 
    735  conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
    736  conn->conn_array_index = -1; /* also default to 'not used' */
    737  conn->global_identifier = n_connections_allocated++;
    738 
    739  conn->type = type;
    740  conn->socket_family = socket_family;
    741  if (!connection_is_listener(conn)) {
    742    /* listeners never use their buf */
    743    conn->inbuf = buf_new();
    744    conn->outbuf = buf_new();
    745  }
    746 
    747  conn->timestamp_created = now;
    748  conn->timestamp_last_read_allowed = now;
    749  conn->timestamp_last_write_allowed = now;
    750 }
    751 
    752 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
    753 void
    754 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
    755 {
    756  tor_assert(! SOCKET_OK(conn_a->s));
    757  tor_assert(! SOCKET_OK(conn_b->s));
    758 
    759  conn_a->linked = 1;
    760  conn_b->linked = 1;
    761  conn_a->linked_conn = conn_b;
    762  conn_b->linked_conn = conn_a;
    763 }
    764 
    765 /** Return true iff the provided connection listener type supports AF_UNIX
    766 * sockets. */
    767 int
    768 conn_listener_type_supports_af_unix(int type)
    769 {
    770  /* For now only control ports or SOCKS ports can be Unix domain sockets
    771   * and listeners at the same time */
    772  switch (type) {
    773    case CONN_TYPE_CONTROL_LISTENER:
    774    case CONN_TYPE_AP_LISTENER:
    775      return 1;
    776    default:
    777      return 0;
    778  }
    779 }
    780 
    781 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
    782 * necessary, close its socket if necessary, and mark the directory as dirty
    783 * if <b>conn</b> is an OR or OP connection.
    784 */
    785 STATIC void
    786 connection_free_minimal(connection_t *conn)
    787 {
    788  void *mem;
    789  size_t memlen;
    790  if (!conn)
    791    return;
    792 
    793  switch (conn->type) {
    794    case CONN_TYPE_OR:
    795    case CONN_TYPE_EXT_OR:
    796      tor_assert(conn->magic == OR_CONNECTION_MAGIC);
    797      mem = TO_OR_CONN(conn);
    798      memlen = sizeof(or_connection_t);
    799      break;
    800    case CONN_TYPE_AP:
    801      tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
    802      mem = TO_ENTRY_CONN(conn);
    803      memlen = sizeof(entry_connection_t);
    804      break;
    805    case CONN_TYPE_EXIT:
    806      tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
    807      mem = TO_EDGE_CONN(conn);
    808      memlen = sizeof(edge_connection_t);
    809      break;
    810    case CONN_TYPE_DIR:
    811      tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
    812      mem = TO_DIR_CONN(conn);
    813      memlen = sizeof(dir_connection_t);
    814      break;
    815    case CONN_TYPE_CONTROL:
    816      tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
    817      mem = TO_CONTROL_CONN(conn);
    818      memlen = sizeof(control_connection_t);
    819      break;
    820    CASE_ANY_LISTENER_TYPE:
    821      tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
    822      mem = TO_LISTENER_CONN(conn);
    823      memlen = sizeof(listener_connection_t);
    824      break;
    825    default:
    826      tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
    827      mem = conn;
    828      memlen = sizeof(connection_t);
    829      break;
    830  }
    831 
    832  if (conn->linked) {
    833    log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
    834             "bytes on inbuf, %d on outbuf.",
    835             conn_type_to_string(conn->type),
    836             conn_state_to_string(conn->type, conn->state),
    837             (int)connection_get_inbuf_len(conn),
    838             (int)connection_get_outbuf_len(conn));
    839  }
    840 
    841  if (!connection_is_listener(conn)) {
    842    buf_free(conn->inbuf);
    843    buf_free(conn->outbuf);
    844  } else {
    845    if (conn->socket_family == AF_UNIX) {
    846      /* For now only control and SOCKS ports can be Unix domain sockets
    847       * and listeners at the same time */
    848      tor_assert(conn_listener_type_supports_af_unix(conn->type));
    849 
    850      if (unlink(conn->address) < 0 && errno != ENOENT) {
    851        log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
    852                         strerror(errno));
    853      }
    854    }
    855  }
    856 
    857  tor_str_wipe_and_free(conn->address);
    858 
    859  if (connection_speaks_cells(conn)) {
    860    or_connection_t *or_conn = TO_OR_CONN(conn);
    861    if (or_conn->tls) {
    862      if (! SOCKET_OK(conn->s)) {
    863        /* The socket has been closed by somebody else; we must tell the
    864         * TLS object not to close it. */
    865        tor_tls_release_socket(or_conn->tls);
    866      } else {
    867        /* The tor_tls_free() call below will close the socket; we must tell
    868         * the code below not to close it a second time. */
    869        tor_release_socket_ownership(conn->s);
    870        conn->s = TOR_INVALID_SOCKET;
    871      }
    872      tor_tls_free(or_conn->tls);
    873      or_conn->tls = NULL;
    874    }
    875    or_handshake_state_free(or_conn->handshake_state);
    876    or_conn->handshake_state = NULL;
    877    tor_str_wipe_and_free(or_conn->nickname);
    878    if (or_conn->chan) {
    879      /* Owww, this shouldn't happen, but... */
    880      channel_t *base_chan = TLS_CHAN_TO_BASE(or_conn->chan);
    881      tor_assert(base_chan);
    882      log_info(LD_CHANNEL,
    883               "Freeing orconn at %p, saw channel %p with ID "
    884               "%"PRIu64 " left un-NULLed",
    885               or_conn, base_chan,
    886               base_chan->global_identifier);
    887      if (!CHANNEL_FINISHED(base_chan)) {
    888        channel_close_for_error(base_chan);
    889      }
    890 
    891      or_conn->chan->conn = NULL;
    892      or_conn->chan = NULL;
    893    }
    894  }
    895  if (conn->type == CONN_TYPE_AP) {
    896    entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
    897    tor_str_wipe_and_free(entry_conn->chosen_exit_name);
    898    tor_str_wipe_and_free(entry_conn->original_dest_address);
    899    if (entry_conn->socks_request)
    900      socks_request_free(entry_conn->socks_request);
    901    if (entry_conn->pending_optimistic_data) {
    902      buf_free(entry_conn->pending_optimistic_data);
    903    }
    904    if (entry_conn->sending_optimistic_data) {
    905      buf_free(entry_conn->sending_optimistic_data);
    906    }
    907  }
    908  if (CONN_IS_EDGE(conn)) {
    909    hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
    910  }
    911  if (conn->type == CONN_TYPE_CONTROL) {
    912    control_connection_t *control_conn = TO_CONTROL_CONN(conn);
    913    tor_free(control_conn->safecookie_client_hash);
    914    tor_free(control_conn->incoming_cmd);
    915    tor_free(control_conn->current_cmd);
    916    if (control_conn->ephemeral_onion_services) {
    917      SMARTLIST_FOREACH(control_conn->ephemeral_onion_services, char *, cp, {
    918        memwipe(cp, 0, strlen(cp));
    919        tor_free(cp);
    920      });
    921      smartlist_free(control_conn->ephemeral_onion_services);
    922    }
    923  }
    924 
    925  /* Probably already freed by connection_free. */
    926  tor_event_free(conn->read_event);
    927  tor_event_free(conn->write_event);
    928  conn->read_event = conn->write_event = NULL;
    929 
    930  if (conn->type == CONN_TYPE_DIR) {
    931    dir_connection_t *dir_conn = TO_DIR_CONN(conn);
    932    tor_free(dir_conn->requested_resource);
    933 
    934    tor_compress_free(dir_conn->compress_state);
    935    dir_conn_clear_spool(dir_conn);
    936 
    937    hs_ident_dir_conn_free(dir_conn->hs_ident);
    938    if (dir_conn->guard_state) {
    939      /* Cancel before freeing, if it's still there. */
    940      entry_guard_cancel(&dir_conn->guard_state);
    941    }
    942    circuit_guard_state_free(dir_conn->guard_state);
    943  }
    944 
    945  if (SOCKET_OK(conn->s)) {
    946    log_debug(LD_NET,"closing fd %d.",(int)conn->s);
    947    tor_close_socket(conn->s);
    948    conn->s = TOR_INVALID_SOCKET;
    949  }
    950 
    951  if (conn->type == CONN_TYPE_OR &&
    952      !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
    953    log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
    954    connection_or_clear_identity(TO_OR_CONN(conn));
    955  }
    956  if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
    957    tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
    958    tor_free(TO_OR_CONN(conn)->ext_or_transport);
    959  }
    960 
    961  memwipe(mem, 0xCC, memlen); /* poison memory */
    962  tor_free(mem);
    963 }
    964 
    965 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
    966 */
    967 MOCK_IMPL(void,
    968 connection_free_,(connection_t *conn))
    969 {
    970  if (!conn)
    971    return;
    972  tor_assert(!connection_is_on_closeable_list(conn));
    973  tor_assert(!connection_in_array(conn));
    974  if (conn->linked_conn) {
    975    conn->linked_conn->linked_conn = NULL;
    976    if (! conn->linked_conn->marked_for_close &&
    977        conn->linked_conn->reading_from_linked_conn)
    978      connection_start_reading(conn->linked_conn);
    979    conn->linked_conn = NULL;
    980  }
    981  if (connection_speaks_cells(conn)) {
    982    if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
    983      connection_or_clear_identity(TO_OR_CONN(conn));
    984    }
    985  }
    986  if (conn->type == CONN_TYPE_CONTROL) {
    987    connection_control_closed(TO_CONTROL_CONN(conn));
    988  }
    989 #if 1
    990  /* DEBUGGING */
    991  if (conn->type == CONN_TYPE_AP) {
    992    connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
    993                                                  "connection_free");
    994  }
    995 #endif /* 1 */
    996 
    997  /* Notify the circuit creation DoS mitigation subsystem that an OR client
    998   * connection has been closed. And only do that if we track it. */
    999  if (conn->type == CONN_TYPE_OR) {
   1000    dos_close_client_conn(TO_OR_CONN(conn));
   1001  }
   1002 
   1003  connection_unregister_events(conn);
   1004  connection_free_minimal(conn);
   1005 }
   1006 
   1007 /**
   1008 * Called when we're about to finally unlink and free a connection:
   1009 * perform necessary accounting and cleanup
   1010 *   - Directory conns that failed to fetch a rendezvous descriptor
   1011 *     need to inform pending rendezvous streams.
   1012 *   - OR conns need to call rep_hist_note_*() to record status.
   1013 *   - AP conns need to send a socks reject if necessary.
   1014 *   - Exit conns need to call connection_dns_remove() if necessary.
   1015 *   - AP and Exit conns need to send an end cell if they can.
   1016 *   - DNS conns need to fail any resolves that are pending on them.
   1017 *   - OR and edge connections need to be unlinked from circuits.
   1018 */
   1019 void
   1020 connection_about_to_close_connection(connection_t *conn)
   1021 {
   1022  tor_assert(conn->marked_for_close);
   1023 
   1024  switch (conn->type) {
   1025    case CONN_TYPE_DIR:
   1026      connection_dir_about_to_close(TO_DIR_CONN(conn));
   1027      break;
   1028    case CONN_TYPE_OR:
   1029    case CONN_TYPE_EXT_OR:
   1030      connection_or_about_to_close(TO_OR_CONN(conn));
   1031      break;
   1032    case CONN_TYPE_AP:
   1033      connection_ap_about_to_close(TO_ENTRY_CONN(conn));
   1034      break;
   1035    case CONN_TYPE_EXIT:
   1036      connection_exit_about_to_close(TO_EDGE_CONN(conn));
   1037      break;
   1038  }
   1039 }
   1040 
   1041 /** Return true iff connection_close_immediate() has been called on this
   1042 * connection. */
   1043 #define CONN_IS_CLOSED(c) \
   1044  ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
   1045 
   1046 /** Close the underlying socket for <b>conn</b>, so we don't try to
   1047 * flush it. Must be used in conjunction with (right before)
   1048 * connection_mark_for_close().
   1049 */
   1050 void
   1051 connection_close_immediate(connection_t *conn)
   1052 {
   1053  assert_connection_ok(conn,0);
   1054  if (CONN_IS_CLOSED(conn)) {
   1055    log_err(LD_BUG,"Attempt to close already-closed connection.");
   1056    tor_fragile_assert();
   1057    return;
   1058  }
   1059  if (connection_get_outbuf_len(conn)) {
   1060    log_info(LD_NET,"fd %d, type %s, state %s, %"TOR_PRIuSZ" bytes on outbuf.",
   1061             (int)conn->s, conn_type_to_string(conn->type),
   1062             conn_state_to_string(conn->type, conn->state),
   1063             buf_datalen(conn->outbuf));
   1064  }
   1065 
   1066  connection_unregister_events(conn);
   1067 
   1068  /* Prevent the event from getting unblocked. */
   1069  conn->read_blocked_on_bw = 0;
   1070  conn->write_blocked_on_bw = 0;
   1071 
   1072  if (SOCKET_OK(conn->s))
   1073    tor_close_socket(conn->s);
   1074  conn->s = TOR_INVALID_SOCKET;
   1075  if (conn->linked)
   1076    conn->linked_conn_is_closed = 1;
   1077  if (conn->outbuf)
   1078    buf_clear(conn->outbuf);
   1079 }
   1080 
   1081 /** Mark <b>conn</b> to be closed next time we loop through
   1082 * conn_close_if_marked() in main.c. */
   1083 void
   1084 connection_mark_for_close_(connection_t *conn, int line, const char *file)
   1085 {
   1086  assert_connection_ok(conn,0);
   1087  tor_assert(line);
   1088  tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
   1089  tor_assert(file);
   1090 
   1091  if (conn->type == CONN_TYPE_OR) {
   1092    /*
   1093     * An or_connection should have been closed through one of the channel-
   1094     * aware functions in connection_or.c.  We'll assume this is an error
   1095     * close and do that, and log a bug warning.
   1096     */
   1097    log_warn(LD_CHANNEL | LD_BUG,
   1098             "Something tried to close an or_connection_t without going "
   1099             "through channels at %s:%d",
   1100             file, line);
   1101    connection_or_close_for_error(TO_OR_CONN(conn), 0);
   1102  } else {
   1103    /* Pass it down to the real function */
   1104    connection_mark_for_close_internal_(conn, line, file);
   1105  }
   1106 }
   1107 
   1108 /** Mark <b>conn</b> to be closed next time we loop through
   1109 * conn_close_if_marked() in main.c.
   1110 *
   1111 * This _internal version bypasses the CONN_TYPE_OR checks; this should be
   1112 * called when you either are sure that if this is an or_connection_t the
   1113 * controlling channel has been notified (e.g. with
   1114 * connection_or_notify_error()), or you actually are the
   1115 * connection_or_close_for_error() or connection_or_close_normally() function.
   1116 * For all other cases, use connection_mark_and_flush() which checks for
   1117 * or_connection_t properly, instead.  See below.
   1118 *
   1119 * We want to keep this function simple and quick, since it can be called from
   1120 * quite deep in the call chain, and hence it should avoid having side-effects
   1121 * that interfere with its callers view of the connection.
   1122 */
   1123 MOCK_IMPL(void,
   1124 connection_mark_for_close_internal_, (connection_t *conn,
   1125                                      int line, const char *file))
   1126 {
   1127  assert_connection_ok(conn,0);
   1128  tor_assert(line);
   1129  tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
   1130  tor_assert(file);
   1131 
   1132  if (conn->marked_for_close) {
   1133    log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
   1134        " (first at %s:%d)", file, line, conn->marked_for_close_file,
   1135        conn->marked_for_close);
   1136    tor_fragile_assert();
   1137    return;
   1138  }
   1139 
   1140  if (conn->type == CONN_TYPE_OR) {
   1141    /*
   1142     * Bad news if this happens without telling the controlling channel; do
   1143     * this so we can find things that call this wrongly when the asserts hit.
   1144     */
   1145    log_debug(LD_CHANNEL,
   1146              "Calling connection_mark_for_close_internal_() on an OR conn "
   1147              "at %s:%d",
   1148              file, line);
   1149  }
   1150 
   1151  conn->marked_for_close = line;
   1152  conn->marked_for_close_file = file;
   1153  add_connection_to_closeable_list(conn);
   1154 
   1155  /* in case we're going to be held-open-til-flushed, reset
   1156   * the number of seconds since last successful write, so
   1157   * we get our whole 15 seconds */
   1158  conn->timestamp_last_write_allowed = time(NULL);
   1159 
   1160  /* Note the connection close. */
   1161  rep_hist_note_conn_closed(conn->from_listener, conn->type,
   1162                            conn->socket_family);
   1163 }
   1164 
   1165 /** Find each connection that has hold_open_until_flushed set to
   1166 * 1 but hasn't written in the past 15 seconds, and set
   1167 * hold_open_until_flushed to 0. This means it will get cleaned
   1168 * up in the next loop through close_if_marked() in main.c.
   1169 */
   1170 void
   1171 connection_expire_held_open(void)
   1172 {
   1173  time_t now;
   1174  smartlist_t *conns = get_connection_array();
   1175 
   1176  now = time(NULL);
   1177 
   1178  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
   1179    /* If we've been holding the connection open, but we haven't written
   1180     * for 15 seconds...
   1181     */
   1182    if (conn->hold_open_until_flushed) {
   1183      tor_assert(conn->marked_for_close);
   1184      if (now - conn->timestamp_last_write_allowed >= 15) {
   1185        int severity;
   1186        if (conn->type == CONN_TYPE_EXIT ||
   1187            (conn->type == CONN_TYPE_DIR &&
   1188             conn->purpose == DIR_PURPOSE_SERVER))
   1189          severity = LOG_INFO;
   1190        else
   1191          severity = LOG_NOTICE;
   1192        log_fn(severity, LD_NET,
   1193               "Giving up on marked_for_close conn that's been flushing "
   1194               "for 15s (fd %d, type %s, state %s).",
   1195               (int)conn->s, conn_type_to_string(conn->type),
   1196               conn_state_to_string(conn->type, conn->state));
   1197        conn->hold_open_until_flushed = 0;
   1198      }
   1199    }
   1200  } SMARTLIST_FOREACH_END(conn);
   1201 }
   1202 
   1203 #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
   1204 /** Create an AF_UNIX listenaddr struct.
   1205 * <b>listenaddress</b> provides the path to the Unix socket.
   1206 *
   1207 * Eventually <b>listenaddress</b> will also optionally contain user, group,
   1208 * and file permissions for the new socket.  But not yet. XXX
   1209 * Also, since we do not create the socket here the information doesn't help
   1210 * here.
   1211 *
   1212 * If not NULL <b>readable_address</b> will contain a copy of the path part of
   1213 * <b>listenaddress</b>.
   1214 *
   1215 * The listenaddr struct has to be freed by the caller.
   1216 */
   1217 static struct sockaddr_un *
   1218 create_unix_sockaddr(const char *listenaddress, char **readable_address,
   1219                     socklen_t *len_out)
   1220 {
   1221  struct sockaddr_un *sockaddr = NULL;
   1222 
   1223  sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
   1224  sockaddr->sun_family = AF_UNIX;
   1225  if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
   1226      >= sizeof(sockaddr->sun_path)) {
   1227    log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
   1228             escaped(listenaddress));
   1229    tor_free(sockaddr);
   1230    return NULL;
   1231  }
   1232 
   1233  if (readable_address)
   1234    *readable_address = tor_strdup(listenaddress);
   1235 
   1236  *len_out = sizeof(struct sockaddr_un);
   1237  return sockaddr;
   1238 }
   1239 #else /* !(defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)) */
   1240 static struct sockaddr *
   1241 create_unix_sockaddr(const char *listenaddress, char **readable_address,
   1242                     socklen_t *len_out)
   1243 {
   1244  (void)listenaddress;
   1245  (void)readable_address;
   1246  log_fn(LOG_ERR, LD_BUG,
   1247         "Unix domain sockets not supported, yet we tried to create one.");
   1248  *len_out = 0;
   1249  tor_fragile_assert();
   1250  return NULL;
   1251 }
   1252 #endif /* defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN) */
   1253 
   1254 /* Log a rate-limited warning about resource exhaustion */
   1255 static void
   1256 warn_about_resource_exhaution(void)
   1257 {
   1258 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
   1259  static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
   1260  char *m;
   1261  if ((m = rate_limit_log(&last_warned, approx_time()))) {
   1262    int n_conns = get_n_open_sockets();
   1263    log_warn(LD_NET,"Failing because we have %d connections already. Please "
   1264             "read doc/TUNING for guidance.%s", n_conns, m);
   1265    tor_free(m);
   1266    control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
   1267                                 n_conns);
   1268  }
   1269 }
   1270 
   1271 /**
   1272 * A socket failed from file descriptor exhaustion.
   1273 *
   1274 * Note down file descriptor exhaustion and log a warning. */
   1275 static inline void
   1276 socket_failed_from_fd_exhaustion(void)
   1277 {
   1278  rep_hist_note_overload(OVERLOAD_FD_EXHAUSTED);
   1279  warn_about_resource_exhaution();
   1280 }
   1281 
   1282 /**
   1283 * A socket failed from TCP port exhaustion.
   1284 *
   1285 * Note down TCP port exhaustion and log a warning. */
   1286 static inline void
   1287 socket_failed_from_tcp_port_exhaustion(void)
   1288 {
   1289  rep_hist_note_tcp_exhaustion();
   1290  warn_about_resource_exhaution();
   1291 }
   1292 
   1293 #ifdef HAVE_SYS_UN_H
   1294 
   1295 #define UNIX_SOCKET_PURPOSE_CONTROL_SOCKET 0
   1296 #define UNIX_SOCKET_PURPOSE_SOCKS_SOCKET 1
   1297 
   1298 /** Check if the purpose isn't one of the ones we know what to do with */
   1299 
   1300 static int
   1301 is_valid_unix_socket_purpose(int purpose)
   1302 {
   1303  int valid = 0;
   1304 
   1305  switch (purpose) {
   1306    case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
   1307    case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
   1308      valid = 1;
   1309      break;
   1310  }
   1311 
   1312  return valid;
   1313 }
   1314 
   1315 /** Return a string description of a unix socket purpose */
   1316 static const char *
   1317 unix_socket_purpose_to_string(int purpose)
   1318 {
   1319  const char *s = "unknown-purpose socket";
   1320 
   1321  switch (purpose) {
   1322    case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
   1323      s = "control socket";
   1324      break;
   1325    case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
   1326      s = "SOCKS socket";
   1327      break;
   1328  }
   1329 
   1330  return s;
   1331 }
   1332 
   1333 /** Check whether we should be willing to open an AF_UNIX socket in
   1334 * <b>path</b>.  Return 0 if we should go ahead and -1 if we shouldn't. */
   1335 static int
   1336 check_location_for_unix_socket(const or_options_t *options, const char *path,
   1337                               int purpose, const port_cfg_t *port)
   1338 {
   1339  int r = -1;
   1340  char *p = NULL;
   1341 
   1342  tor_assert(is_valid_unix_socket_purpose(purpose));
   1343 
   1344  p = tor_strdup(path);
   1345  cpd_check_t flags = CPD_CHECK_MODE_ONLY;
   1346  if (get_parent_directory(p)<0 || p[0] != '/') {
   1347    log_warn(LD_GENERAL, "Bad unix socket address '%s'.  Tor does not support "
   1348             "relative paths for unix sockets.", path);
   1349    goto done;
   1350  }
   1351 
   1352  if (port->is_world_writable) {
   1353    /* World-writable sockets can go anywhere. */
   1354    r = 0;
   1355    goto done;
   1356  }
   1357 
   1358  if (port->is_group_writable) {
   1359    flags |= CPD_GROUP_OK;
   1360  }
   1361 
   1362  if (port->relax_dirmode_check) {
   1363    flags |= CPD_RELAX_DIRMODE_CHECK;
   1364  }
   1365 
   1366  if (check_private_dir(p, flags, options->User) < 0) {
   1367    char *escpath, *escdir;
   1368    escpath = esc_for_log(path);
   1369    escdir = esc_for_log(p);
   1370    log_warn(LD_GENERAL, "Before Tor can create a %s in %s, the directory "
   1371             "%s needs to exist, and to be accessible only by the user%s "
   1372             "account that is running Tor.  (On some Unix systems, anybody "
   1373             "who can list a socket can connect to it, so Tor is being "
   1374             "careful.)",
   1375             unix_socket_purpose_to_string(purpose), escpath, escdir,
   1376             port->is_group_writable ? " and group" : "");
   1377    tor_free(escpath);
   1378    tor_free(escdir);
   1379    goto done;
   1380  }
   1381 
   1382  r = 0;
   1383 done:
   1384  tor_free(p);
   1385  return r;
   1386 }
   1387 #endif /* defined(HAVE_SYS_UN_H) */
   1388 
   1389 /** Tell the TCP stack that it shouldn't wait for a long time after
   1390 * <b>sock</b> has closed before reusing its port. Return 0 on success,
   1391 * -1 on failure. */
   1392 static int
   1393 make_socket_reuseable(tor_socket_t sock)
   1394 {
   1395 #ifdef _WIN32
   1396  (void) sock;
   1397  return 0;
   1398 #else
   1399  int one=1;
   1400 
   1401  /* REUSEADDR on normal places means you can rebind to the port
   1402   * right after somebody else has let it go. But REUSEADDR on win32
   1403   * means you can bind to the port _even when somebody else
   1404   * already has it bound_. So, don't do that on Win32. */
   1405  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
   1406             (socklen_t)sizeof(one)) == -1) {
   1407    return -1;
   1408  }
   1409  return 0;
   1410 #endif /* defined(_WIN32) */
   1411 }
   1412 
   1413 #ifdef _WIN32
   1414 /** Tell the Windows TCP stack to prevent other applications from receiving
   1415 * traffic from tor's open ports. Return 0 on success, -1 on failure. */
   1416 static int
   1417 make_win32_socket_exclusive(tor_socket_t sock)
   1418 {
   1419 #ifdef SO_EXCLUSIVEADDRUSE
   1420  int one=1;
   1421 
   1422  /* Any socket that sets REUSEADDR on win32 can bind to a port _even when
   1423   * somebody else already has it bound_, and _even if the original socket
   1424   * didn't set REUSEADDR_. Use EXCLUSIVEADDRUSE to prevent this port-stealing
   1425   * on win32. */
   1426  if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (void*) &one,
   1427                 (socklen_t)sizeof(one))) {
   1428    return -1;
   1429  }
   1430  return 0;
   1431 #else /* !defined(SO_EXCLUSIVEADDRUSE) */
   1432  (void) sock;
   1433  return 0;
   1434 #endif /* defined(SO_EXCLUSIVEADDRUSE) */
   1435 }
   1436 #endif /* defined(_WIN32) */
   1437 
   1438 /** Max backlog to pass to listen.  We start at */
   1439 static int listen_limit = INT_MAX;
   1440 
   1441 /* Listen on <b>fd</b> with appropriate backlog. Return as for listen. */
   1442 static int
   1443 tor_listen(tor_socket_t fd)
   1444 {
   1445  int r;
   1446 
   1447  if ((r = listen(fd, listen_limit)) < 0) {
   1448    if (listen_limit == SOMAXCONN)
   1449      return r;
   1450    if ((r = listen(fd, SOMAXCONN)) == 0) {
   1451      listen_limit = SOMAXCONN;
   1452      log_warn(LD_NET, "Setting listen backlog to INT_MAX connections "
   1453               "didn't work, but SOMAXCONN did. Lowering backlog limit.");
   1454    }
   1455  }
   1456  return r;
   1457 }
   1458 
   1459 /** Bind a new non-blocking socket listening to the socket described
   1460 * by <b>listensockaddr</b>.
   1461 *
   1462 * <b>address</b> is only used for logging purposes and to add the information
   1463 * to the conn.
   1464 *
   1465 * Set <b>addr_in_use</b> to true in case socket binding fails with
   1466 * EADDRINUSE.
   1467 */
   1468 static connection_t *
   1469 connection_listener_new(const struct sockaddr *listensockaddr,
   1470                        socklen_t socklen,
   1471                        int type, const char *address,
   1472                        const port_cfg_t *port_cfg,
   1473                        int *addr_in_use)
   1474 {
   1475  listener_connection_t *lis_conn;
   1476  connection_t *conn = NULL;
   1477  tor_socket_t s = TOR_INVALID_SOCKET;  /* the socket we're going to make */
   1478  or_options_t const *options = get_options();
   1479  (void) options; /* Windows doesn't use this. */
   1480 #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
   1481  const struct passwd *pw = NULL;
   1482 #endif
   1483  uint16_t usePort = 0, gotPort = 0;
   1484  int start_reading = 0;
   1485  static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
   1486  tor_addr_t addr;
   1487  int exhaustion = 0;
   1488 
   1489  if (addr_in_use)
   1490    *addr_in_use = 0;
   1491 
   1492  if (listensockaddr->sa_family == AF_INET ||
   1493      listensockaddr->sa_family == AF_INET6) {
   1494    int is_stream = (type != CONN_TYPE_AP_DNS_LISTENER);
   1495    if (is_stream)
   1496      start_reading = 1;
   1497 
   1498    tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
   1499    log_notice(LD_NET, "Opening %s on %s",
   1500               conn_type_to_string(type), fmt_addrport(&addr, usePort));
   1501 
   1502    s = tor_open_socket_nonblocking(tor_addr_family(&addr),
   1503      is_stream ? SOCK_STREAM : SOCK_DGRAM,
   1504      is_stream ? IPPROTO_TCP: IPPROTO_UDP);
   1505    if (!SOCKET_OK(s)) {
   1506      int e = tor_socket_errno(s);
   1507      if (ERRNO_IS_RESOURCE_LIMIT(e)) {
   1508        socket_failed_from_fd_exhaustion();
   1509        /*
   1510         * We'll call the OOS handler at the error exit, so set the
   1511         * exhaustion flag for it.
   1512         */
   1513        exhaustion = 1;
   1514      } else {
   1515        log_warn(LD_NET, "Socket creation failed: %s",
   1516                 tor_socket_strerror(e));
   1517      }
   1518      goto err;
   1519    }
   1520 
   1521    if (make_socket_reuseable(s) < 0) {
   1522      log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
   1523               conn_type_to_string(type),
   1524               tor_socket_strerror(errno));
   1525    }
   1526 
   1527 #ifdef _WIN32
   1528    if (make_win32_socket_exclusive(s) < 0) {
   1529      log_warn(LD_NET, "Error setting SO_EXCLUSIVEADDRUSE flag on %s: %s",
   1530               conn_type_to_string(type),
   1531               tor_socket_strerror(errno));
   1532    }
   1533 #endif /* defined(_WIN32) */
   1534 
   1535 #if defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT)
   1536    if (options->TransProxyType_parsed == TPT_TPROXY &&
   1537        type == CONN_TYPE_AP_TRANS_LISTENER) {
   1538      int one = 1;
   1539      if (setsockopt(s, SOL_IP, IP_TRANSPARENT, (void*)&one,
   1540                     (socklen_t)sizeof(one)) < 0) {
   1541        const char *extra = "";
   1542        int e = tor_socket_errno(s);
   1543        if (e == EPERM)
   1544          extra = "TransTPROXY requires root privileges or similar"
   1545            " capabilities.";
   1546        log_warn(LD_NET, "Error setting IP_TRANSPARENT flag: %s.%s",
   1547                 tor_socket_strerror(e), extra);
   1548      }
   1549    }
   1550 #endif /* defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT) */
   1551 
   1552 #ifdef IPV6_V6ONLY
   1553    if (listensockaddr->sa_family == AF_INET6) {
   1554      int one = 1;
   1555      /* We need to set IPV6_V6ONLY so that this socket can't get used for
   1556       * IPv4 connections. */
   1557      if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
   1558                     (void*)&one, (socklen_t)sizeof(one)) < 0) {
   1559        int e = tor_socket_errno(s);
   1560        log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
   1561                 tor_socket_strerror(e));
   1562        /* Keep going; probably not harmful. */
   1563      }
   1564    }
   1565 #endif /* defined(IPV6_V6ONLY) */
   1566 
   1567    if (bind(s,listensockaddr,socklen) < 0) {
   1568      const char *helpfulhint = "";
   1569      int e = tor_socket_errno(s);
   1570      if (ERRNO_IS_EADDRINUSE(e)) {
   1571        helpfulhint = ". Is Tor already running?";
   1572        if (addr_in_use)
   1573          *addr_in_use = 1;
   1574      }
   1575      log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
   1576               tor_socket_strerror(e), helpfulhint);
   1577      goto err;
   1578    }
   1579 
   1580    if (is_stream) {
   1581      if (tor_listen(s) < 0) {
   1582        log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
   1583                 tor_socket_strerror(tor_socket_errno(s)));
   1584        goto err;
   1585      }
   1586    }
   1587 
   1588    if (usePort != 0) {
   1589      gotPort = usePort;
   1590    } else {
   1591      tor_addr_t addr2;
   1592      struct sockaddr_storage ss;
   1593      socklen_t ss_len=sizeof(ss);
   1594      if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
   1595        log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
   1596                 conn_type_to_string(type),
   1597                 tor_socket_strerror(tor_socket_errno(s)));
   1598        gotPort = 0;
   1599      }
   1600      tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
   1601    }
   1602 #ifdef HAVE_SYS_UN_H
   1603  /*
   1604   * AF_UNIX generic setup stuff
   1605   */
   1606  } else if (listensockaddr->sa_family == AF_UNIX) {
   1607    /* We want to start reading for both AF_UNIX cases */
   1608    start_reading = 1;
   1609 
   1610    tor_assert(conn_listener_type_supports_af_unix(type));
   1611 
   1612    if (check_location_for_unix_socket(options, address,
   1613          (type == CONN_TYPE_CONTROL_LISTENER) ?
   1614           UNIX_SOCKET_PURPOSE_CONTROL_SOCKET :
   1615           UNIX_SOCKET_PURPOSE_SOCKS_SOCKET, port_cfg) < 0) {
   1616        goto err;
   1617    }
   1618 
   1619    log_notice(LD_NET, "Opening %s on %s",
   1620               conn_type_to_string(type), address);
   1621 
   1622    tor_addr_make_unspec(&addr);
   1623 
   1624    if (unlink(address) < 0 && errno != ENOENT) {
   1625      log_warn(LD_NET, "Could not unlink %s: %s", address,
   1626                       strerror(errno));
   1627      goto err;
   1628    }
   1629 
   1630    s = tor_open_socket_nonblocking(AF_UNIX, SOCK_STREAM, 0);
   1631    if (! SOCKET_OK(s)) {
   1632      int e = tor_socket_errno(s);
   1633      if (ERRNO_IS_RESOURCE_LIMIT(e)) {
   1634        socket_failed_from_fd_exhaustion();
   1635        /*
   1636         * We'll call the OOS handler at the error exit, so set the
   1637         * exhaustion flag for it.
   1638         */
   1639        exhaustion = 1;
   1640      } else {
   1641        log_warn(LD_NET,"Socket creation failed: %s.", strerror(e));
   1642      }
   1643      goto err;
   1644    }
   1645 
   1646    if (bind(s, listensockaddr,
   1647             (socklen_t)sizeof(struct sockaddr_un)) == -1) {
   1648      log_warn(LD_NET,"Bind to %s failed: %s.", address,
   1649               tor_socket_strerror(tor_socket_errno(s)));
   1650      goto err;
   1651    }
   1652 
   1653 #ifdef HAVE_PWD_H
   1654    if (options->User) {
   1655      pw = tor_getpwnam(options->User);
   1656      struct stat st;
   1657      if (pw == NULL) {
   1658        log_warn(LD_NET,"Unable to chown() %s socket: user %s not found.",
   1659                 address, options->User);
   1660        goto err;
   1661      } else if (fstat(s, &st) == 0 &&
   1662                 st.st_uid == pw->pw_uid && st.st_gid == pw->pw_gid) {
   1663        /* No change needed */
   1664      } else if (chown(sandbox_intern_string(address),
   1665                       pw->pw_uid, pw->pw_gid) < 0) {
   1666        log_warn(LD_NET,"Unable to chown() %s socket: %s.",
   1667                 address, strerror(errno));
   1668        goto err;
   1669      }
   1670    }
   1671 #endif /* defined(HAVE_PWD_H) */
   1672 
   1673    {
   1674      unsigned mode;
   1675      const char *status;
   1676      struct stat st;
   1677      if (port_cfg->is_world_writable) {
   1678        mode = 0666;
   1679        status = "world-writable";
   1680      } else if (port_cfg->is_group_writable) {
   1681        mode = 0660;
   1682        status = "group-writable";
   1683      } else {
   1684        mode = 0600;
   1685        status = "private";
   1686      }
   1687      /* We need to use chmod; fchmod doesn't work on sockets on all
   1688       * platforms. */
   1689      if (fstat(s, &st) == 0 && (st.st_mode & 0777) == mode) {
   1690        /* no change needed */
   1691      } else if (chmod(sandbox_intern_string(address), mode) < 0) {
   1692        log_warn(LD_FS,"Unable to make %s %s.", address, status);
   1693        goto err;
   1694      }
   1695    }
   1696 
   1697    if (listen(s, SOMAXCONN) < 0) {
   1698      log_warn(LD_NET, "Could not listen on %s: %s", address,
   1699               tor_socket_strerror(tor_socket_errno(s)));
   1700      goto err;
   1701    }
   1702 
   1703 #ifndef __APPLE__
   1704    /* This code was introduced to help debug #28229. */
   1705    int value;
   1706    socklen_t len = sizeof(value);
   1707 
   1708    if (!getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, &value, &len)) {
   1709      if (value == 0) {
   1710        log_err(LD_NET, "Could not listen on %s - "
   1711                        "getsockopt(.,SO_ACCEPTCONN,.) yields 0.", address);
   1712        goto err;
   1713      }
   1714    }
   1715 #endif /* !defined(__APPLE__) */
   1716 #endif /* defined(HAVE_SYS_UN_H) */
   1717  } else {
   1718    log_err(LD_BUG, "Got unexpected address family %d.",
   1719            listensockaddr->sa_family);
   1720    tor_assert(0);
   1721  }
   1722 
   1723  lis_conn = listener_connection_new(type, listensockaddr->sa_family);
   1724  conn = TO_CONN(lis_conn);
   1725  conn->socket_family = listensockaddr->sa_family;
   1726  conn->s = s;
   1727  s = TOR_INVALID_SOCKET; /* Prevent double-close */
   1728  conn->address = tor_strdup(address);
   1729  conn->port = gotPort;
   1730  tor_addr_copy(&conn->addr, &addr);
   1731 
   1732  memcpy(&lis_conn->entry_cfg, &port_cfg->entry_cfg, sizeof(entry_port_cfg_t));
   1733 
   1734  if (port_cfg->entry_cfg.isolation_flags) {
   1735    lis_conn->entry_cfg.isolation_flags = port_cfg->entry_cfg.isolation_flags;
   1736    if (port_cfg->entry_cfg.session_group >= 0) {
   1737      lis_conn->entry_cfg.session_group = port_cfg->entry_cfg.session_group;
   1738    } else {
   1739      /* This can wrap after around INT_MAX listeners are opened.  But I don't
   1740       * believe that matters, since you would need to open a ridiculous
   1741       * number of listeners while keeping the early ones open before you ever
   1742       * hit this.  An OR with a dozen ports open, for example, would have to
   1743       * close and re-open its listeners every second for 4 years nonstop.
   1744       */
   1745      lis_conn->entry_cfg.session_group = global_next_session_group--;
   1746    }
   1747  }
   1748 
   1749  if (connection_add(conn) < 0) { /* no space, forget it */
   1750    log_warn(LD_NET,"connection_add for listener failed. Giving up.");
   1751    goto err;
   1752  }
   1753 
   1754  log_fn(usePort==gotPort ? LOG_DEBUG : LOG_NOTICE, LD_NET,
   1755         "%s listening on port %u.",
   1756         conn_type_to_string(type), gotPort);
   1757 
   1758  conn->state = LISTENER_STATE_READY;
   1759  if (start_reading) {
   1760    connection_start_reading(conn);
   1761  } else {
   1762    tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
   1763    dnsserv_configure_listener(conn);
   1764  }
   1765 
   1766  /*
   1767   * Normal exit; call the OOS handler since connection count just changed;
   1768   * the exhaustion flag will always be zero here though.
   1769   */
   1770  connection_check_oos(get_n_open_sockets(), 0);
   1771 
   1772  log_notice(LD_NET, "Opened %s", connection_describe(conn));
   1773 
   1774  return conn;
   1775 
   1776 err:
   1777  if (SOCKET_OK(s))
   1778    tor_close_socket(s);
   1779  if (conn)
   1780    connection_free(conn);
   1781 
   1782  /* Call the OOS handler, indicate if we saw an exhaustion-related error */
   1783  connection_check_oos(get_n_open_sockets(), exhaustion);
   1784 
   1785  return NULL;
   1786 }
   1787 
   1788 /**
   1789 * Create a new listener connection for a given <b>port</b>. In case we
   1790 * for a reason that is not an error condition, set <b>defer</b>
   1791 * to true. If we cannot bind listening socket because address is already
   1792 * in use, set <b>addr_in_use</b> to true.
   1793 */
   1794 static connection_t *
   1795 connection_listener_new_for_port(const port_cfg_t *port,
   1796                                 int *defer, int *addr_in_use)
   1797 {
   1798  connection_t *conn;
   1799  struct sockaddr *listensockaddr;
   1800  socklen_t listensocklen = 0;
   1801  char *address=NULL;
   1802  int real_port = port->port == CFG_AUTO_PORT ? 0 : port->port;
   1803  tor_assert(real_port <= UINT16_MAX);
   1804 
   1805  if (defer)
   1806    *defer = 0;
   1807 
   1808  if (port->server_cfg.no_listen) {
   1809    if (defer)
   1810      *defer = 1;
   1811    return NULL;
   1812  }
   1813 
   1814 #ifndef _WIN32
   1815  /* We don't need to be root to create a UNIX socket, so defer until after
   1816   * setuid. */
   1817  const or_options_t *options = get_options();
   1818  if (port->is_unix_addr && !geteuid() && (options->User) &&
   1819      strcmp(options->User, "root")) {
   1820    if (defer)
   1821      *defer = 1;
   1822    return NULL;
   1823  }
   1824 #endif /* !defined(_WIN32) */
   1825 
   1826  if (port->is_unix_addr) {
   1827    listensockaddr = (struct sockaddr *)
   1828      create_unix_sockaddr(port->unix_addr,
   1829                           &address, &listensocklen);
   1830  } else {
   1831    listensockaddr = tor_malloc(sizeof(struct sockaddr_storage));
   1832    listensocklen = tor_addr_to_sockaddr(&port->addr,
   1833                                         real_port,
   1834                                         listensockaddr,
   1835                                         sizeof(struct sockaddr_storage));
   1836    address = tor_addr_to_str_dup(&port->addr);
   1837  }
   1838 
   1839  if (listensockaddr) {
   1840    conn = connection_listener_new(listensockaddr, listensocklen,
   1841                                   port->type, address, port,
   1842                                   addr_in_use);
   1843    tor_free(listensockaddr);
   1844    tor_free(address);
   1845  } else {
   1846    conn = NULL;
   1847  }
   1848 
   1849  return conn;
   1850 }
   1851 
   1852 /** Do basic sanity checking on a newly received socket. Return 0
   1853 * if it looks ok, else return -1.
   1854 *
   1855 * Notably, some TCP stacks can erroneously have accept() return successfully
   1856 * with socklen 0, when the client sends an RST before the accept call (as
   1857 * nmap does).  We want to detect that, and not go on with the connection.
   1858 */
   1859 static int
   1860 check_sockaddr(const struct sockaddr *sa, int len, int level)
   1861 {
   1862  int ok = 1;
   1863 
   1864  if (sa->sa_family == AF_INET) {
   1865    struct sockaddr_in *sin=(struct sockaddr_in*)sa;
   1866    if (len != sizeof(struct sockaddr_in)) {
   1867      log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
   1868             len,(int)sizeof(struct sockaddr_in));
   1869      ok = 0;
   1870    }
   1871    if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
   1872      log_fn(level, LD_NET,
   1873             "Address for new connection has address/port equal to zero.");
   1874      ok = 0;
   1875    }
   1876  } else if (sa->sa_family == AF_INET6) {
   1877    struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
   1878    if (len != sizeof(struct sockaddr_in6)) {
   1879      log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
   1880             len,(int)sizeof(struct sockaddr_in6));
   1881      ok = 0;
   1882    }
   1883    if (fast_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
   1884        sin6->sin6_port == 0) {
   1885      log_fn(level, LD_NET,
   1886             "Address for new connection has address/port equal to zero.");
   1887      ok = 0;
   1888    }
   1889  } else if (sa->sa_family == AF_UNIX) {
   1890    ok = 1;
   1891  } else {
   1892    ok = 0;
   1893  }
   1894  return ok ? 0 : -1;
   1895 }
   1896 
   1897 /** Check whether the socket family from an accepted socket <b>got</b> is the
   1898 * same as the one that <b>listener</b> is waiting for.  If it isn't, log
   1899 * a useful message and return -1.  Else return 0.
   1900 *
   1901 * This is annoying, but can apparently happen on some Darwins. */
   1902 static int
   1903 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
   1904 {
   1905  if (got != listener->socket_family) {
   1906    log_info(LD_BUG, "A listener connection returned a socket with a "
   1907             "mismatched family. %s for addr_family %d gave us a socket "
   1908             "with address family %d.  Dropping.",
   1909             conn_type_to_string(listener->type),
   1910             (int)listener->socket_family,
   1911             (int)got);
   1912    return -1;
   1913  }
   1914  return 0;
   1915 }
   1916 
   1917 /** The listener connection <b>conn</b> told poll() it wanted to read.
   1918 * Call accept() on conn-\>s, and add the new connection if necessary.
   1919 */
   1920 static int
   1921 connection_handle_listener_read(connection_t *conn, int new_type)
   1922 {
   1923  tor_socket_t news; /* the new socket */
   1924  connection_t *newconn = 0;
   1925  /* information about the remote peer when connecting to other routers */
   1926  struct sockaddr_storage addrbuf;
   1927  struct sockaddr *remote = (struct sockaddr*)&addrbuf;
   1928  /* length of the remote address. Must be whatever accept() needs. */
   1929  socklen_t remotelen = (socklen_t)sizeof(addrbuf);
   1930  const or_options_t *options = get_options();
   1931 
   1932  tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
   1933  memset(&addrbuf, 0, sizeof(addrbuf));
   1934 
   1935  news = tor_accept_socket_nonblocking(conn->s,remote,&remotelen);
   1936  if (!SOCKET_OK(news)) { /* accept() error */
   1937    int e = tor_socket_errno(conn->s);
   1938    if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
   1939      /*
   1940       * they hung up before we could accept(). that's fine.
   1941       *
   1942       * give the OOS handler a chance to run though
   1943       */
   1944      connection_check_oos(get_n_open_sockets(), 0);
   1945      return 0;
   1946    } else if (ERRNO_IS_RESOURCE_LIMIT(e)) {
   1947      socket_failed_from_fd_exhaustion();
   1948      /* Exhaustion; tell the OOS handler */
   1949      connection_check_oos(get_n_open_sockets(), 1);
   1950      return 0;
   1951    }
   1952    /* else there was a real error. */
   1953    log_warn(LD_NET,"accept() failed: %s. Closing listener.",
   1954             tor_socket_strerror(e));
   1955    connection_mark_for_close(conn);
   1956    /* Tell the OOS handler about this too */
   1957    connection_check_oos(get_n_open_sockets(), 0);
   1958    return -1;
   1959  }
   1960  log_debug(LD_NET,
   1961            "Connection accepted on socket %d (child of fd %d).",
   1962            (int)news,(int)conn->s);
   1963 
   1964  /* We accepted a new conn; run OOS handler */
   1965  connection_check_oos(get_n_open_sockets(), 0);
   1966 
   1967  if (make_socket_reuseable(news) < 0) {
   1968    if (tor_socket_errno(news) == EINVAL) {
   1969      /* This can happen on OSX if we get a badly timed shutdown. */
   1970      log_debug(LD_NET, "make_socket_reuseable returned EINVAL");
   1971    } else {
   1972      log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
   1973               conn_type_to_string(new_type),
   1974               tor_socket_strerror(errno));
   1975    }
   1976    tor_close_socket(news);
   1977    return 0;
   1978  }
   1979 
   1980  if (options->ConstrainedSockets)
   1981    set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
   1982 
   1983  if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
   1984    tor_close_socket(news);
   1985    return 0;
   1986  }
   1987 
   1988  if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6 ||
   1989     (conn->socket_family == AF_UNIX && new_type == CONN_TYPE_AP)) {
   1990    tor_addr_t addr;
   1991    uint16_t port;
   1992    if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
   1993      log_info(LD_NET,
   1994               "accept() returned a strange address; closing connection.");
   1995      tor_close_socket(news);
   1996      return 0;
   1997    }
   1998 
   1999    tor_addr_from_sockaddr(&addr, remote, &port);
   2000 
   2001    /* process entrance policies here, before we even create the connection */
   2002    if (new_type == CONN_TYPE_AP) {
   2003      /* check sockspolicy to see if we should accept it */
   2004      if (socks_policy_permits_address(&addr) == 0) {
   2005        log_notice(LD_APP,
   2006                   "Denying socks connection from untrusted address %s.",
   2007                   fmt_and_decorate_addr(&addr));
   2008        rep_hist_note_conn_rejected(new_type, conn->socket_family);
   2009        tor_close_socket(news);
   2010        return 0;
   2011      }
   2012    }
   2013    if (new_type == CONN_TYPE_DIR) {
   2014      /* check dirpolicy to see if we should accept it */
   2015      if (dir_policy_permits_address(&addr) == 0) {
   2016        log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
   2017                   fmt_and_decorate_addr(&addr));
   2018        rep_hist_note_conn_rejected(new_type, conn->socket_family);
   2019        tor_close_socket(news);
   2020        return 0;
   2021      }
   2022    }
   2023    if (new_type == CONN_TYPE_OR) {
   2024      /* Assess with the connection DoS mitigation subsystem if this address
   2025       * can open a new connection. */
   2026      if (dos_conn_addr_get_defense_type(&addr) == DOS_CONN_DEFENSE_CLOSE) {
   2027        rep_hist_note_conn_rejected(new_type, conn->socket_family);
   2028        tor_close_socket(news);
   2029        return 0;
   2030      }
   2031    }
   2032 
   2033    newconn = connection_new(new_type, conn->socket_family);
   2034    newconn->s = news;
   2035 
   2036    /* remember the remote address */
   2037    tor_addr_copy(&newconn->addr, &addr);
   2038    if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
   2039      newconn->port = 0;
   2040      newconn->address = tor_strdup(conn->address);
   2041    } else {
   2042      newconn->port = port;
   2043      newconn->address = tor_addr_to_str_dup(&addr);
   2044    }
   2045 
   2046    if (new_type == CONN_TYPE_AP && conn->socket_family != AF_UNIX) {
   2047      log_info(LD_NET, "New SOCKS connection opened from %s.",
   2048               fmt_and_decorate_addr(&addr));
   2049    }
   2050    if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
   2051      log_info(LD_NET, "New SOCKS AF_UNIX connection opened");
   2052    }
   2053    if (new_type == CONN_TYPE_CONTROL) {
   2054      log_notice(LD_CONTROL, "New control connection opened from %s.",
   2055                 fmt_and_decorate_addr(&addr));
   2056    }
   2057    if (new_type == CONN_TYPE_METRICS) {
   2058      log_info(LD_CONTROL, "New metrics connection opened from %s.",
   2059               fmt_and_decorate_addr(&addr));
   2060    }
   2061 
   2062  } else if (conn->socket_family == AF_UNIX && conn->type != CONN_TYPE_AP) {
   2063    tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
   2064    tor_assert(new_type == CONN_TYPE_CONTROL);
   2065    log_notice(LD_CONTROL, "New control connection opened.");
   2066 
   2067    newconn = connection_new(new_type, conn->socket_family);
   2068    newconn->s = news;
   2069 
   2070    /* remember the remote address -- do we have anything sane to put here? */
   2071    tor_addr_make_unspec(&newconn->addr);
   2072    newconn->port = 1;
   2073    newconn->address = tor_strdup(conn->address);
   2074  } else {
   2075    tor_assert(0);
   2076  };
   2077 
   2078  /* We are receiving this connection. */
   2079  newconn->from_listener = 1;
   2080 
   2081  if (connection_add(newconn) < 0) { /* no space, forget it */
   2082    connection_free(newconn);
   2083    return 0; /* no need to tear down the parent */
   2084  }
   2085 
   2086  if (connection_init_accepted_conn(newconn, TO_LISTENER_CONN(conn)) < 0) {
   2087    if (! newconn->marked_for_close)
   2088      connection_mark_for_close(newconn);
   2089    return 0;
   2090  }
   2091 
   2092  note_connection(true /* inbound */, newconn);
   2093 
   2094  return 0;
   2095 }
   2096 
   2097 /** Initialize states for newly accepted connection <b>conn</b>.
   2098 *
   2099 * If conn is an OR, start the TLS handshake.
   2100 *
   2101 * If conn is a transparent AP, get its original destination
   2102 * and place it in circuit_wait.
   2103 *
   2104 * The <b>listener</b> parameter is only used for AP connections.
   2105 */
   2106 int
   2107 connection_init_accepted_conn(connection_t *conn,
   2108                              const listener_connection_t *listener)
   2109 {
   2110  int rv;
   2111 
   2112  connection_start_reading(conn);
   2113 
   2114  switch (conn->type) {
   2115    case CONN_TYPE_EXT_OR:
   2116      /* Initiate Extended ORPort authentication. */
   2117      return connection_ext_or_start_auth(TO_OR_CONN(conn));
   2118    case CONN_TYPE_OR:
   2119      connection_or_event_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
   2120      rv = connection_tls_start_handshake(TO_OR_CONN(conn), 1);
   2121      if (rv < 0) {
   2122        connection_or_close_for_error(TO_OR_CONN(conn), 0);
   2123      }
   2124      return rv;
   2125      break;
   2126    case CONN_TYPE_AP:
   2127      memcpy(&TO_ENTRY_CONN(conn)->entry_cfg, &listener->entry_cfg,
   2128             sizeof(entry_port_cfg_t));
   2129      TO_ENTRY_CONN(conn)->nym_epoch = get_signewnym_epoch();
   2130      TO_ENTRY_CONN(conn)->socks_request->listener_type = listener->base_.type;
   2131 
   2132      /* Any incoming connection on an entry port counts as user activity. */
   2133      note_user_activity(approx_time());
   2134 
   2135      switch (TO_CONN(listener)->type) {
   2136        case CONN_TYPE_AP_LISTENER:
   2137          conn->state = AP_CONN_STATE_SOCKS_WAIT;
   2138          TO_ENTRY_CONN(conn)->socks_request->socks_prefer_no_auth =
   2139            listener->entry_cfg.socks_prefer_no_auth;
   2140          TO_ENTRY_CONN(conn)->socks_request->socks_use_extended_errors =
   2141            listener->entry_cfg.extended_socks5_codes;
   2142          break;
   2143        case CONN_TYPE_AP_TRANS_LISTENER:
   2144          TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
   2145          /* XXXX028 -- is this correct still, with the addition of
   2146           * pending_entry_connections ? */
   2147          conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
   2148          return connection_ap_process_transparent(TO_ENTRY_CONN(conn));
   2149        case CONN_TYPE_AP_NATD_LISTENER:
   2150          TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
   2151          conn->state = AP_CONN_STATE_NATD_WAIT;
   2152          break;
   2153        case CONN_TYPE_AP_HTTP_CONNECT_LISTENER:
   2154          conn->state = AP_CONN_STATE_HTTP_CONNECT_WAIT;
   2155      }
   2156      break;
   2157    case CONN_TYPE_DIR:
   2158      conn->purpose = DIR_PURPOSE_SERVER;
   2159      conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
   2160      break;
   2161    case CONN_TYPE_CONTROL:
   2162      conn->state = CONTROL_CONN_STATE_NEEDAUTH;
   2163      break;
   2164  }
   2165  return 0;
   2166 }
   2167 
   2168 /** Take conn, make a nonblocking socket; try to connect to
   2169 * sa, binding to bindaddr if sa is not localhost. If fail, return -1 and if
   2170 * applicable put your best guess about errno into *<b>socket_error</b>.
   2171 * If connected return 1, if EAGAIN return 0.
   2172 */
   2173 MOCK_IMPL(STATIC int,
   2174 connection_connect_sockaddr,(connection_t *conn,
   2175                            const struct sockaddr *sa,
   2176                            socklen_t sa_len,
   2177                            const struct sockaddr *bindaddr,
   2178                            socklen_t bindaddr_len,
   2179                            int *socket_error))
   2180 {
   2181  tor_socket_t s;
   2182  int inprogress = 0;
   2183  const or_options_t *options = get_options();
   2184 
   2185  tor_assert(conn);
   2186  tor_assert(sa);
   2187  tor_assert(socket_error);
   2188 
   2189  if (net_is_completely_disabled()) {
   2190    /* We should never even try to connect anyplace if the network is
   2191     * completely shut off.
   2192     *
   2193     * (We don't check net_is_disabled() here, since we still sometimes
   2194     * want to open connections when we're in soft hibernation.)
   2195     */
   2196    static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
   2197    *socket_error = SOCK_ERRNO(ENETUNREACH);
   2198    log_fn_ratelim(&disablenet_violated, LOG_WARN, LD_BUG,
   2199                   "Tried to open a socket with DisableNetwork set.");
   2200    tor_fragile_assert();
   2201    return -1;
   2202  }
   2203 
   2204  const int protocol_family = sa->sa_family;
   2205  const int proto = (sa->sa_family == AF_INET6 ||
   2206                     sa->sa_family == AF_INET) ? IPPROTO_TCP : 0;
   2207 
   2208  s = tor_open_socket_nonblocking(protocol_family, SOCK_STREAM, proto);
   2209  if (! SOCKET_OK(s)) {
   2210    /*
   2211     * Early OOS handler calls; it matters if it's an exhaustion-related
   2212     * error or not.
   2213     */
   2214    *socket_error = tor_socket_errno(s);
   2215    if (ERRNO_IS_RESOURCE_LIMIT(*socket_error)) {
   2216      socket_failed_from_fd_exhaustion();
   2217      connection_check_oos(get_n_open_sockets(), 1);
   2218    } else {
   2219      log_warn(LD_NET,"Error creating network socket: %s",
   2220               tor_socket_strerror(*socket_error));
   2221      connection_check_oos(get_n_open_sockets(), 0);
   2222    }
   2223    return -1;
   2224  }
   2225 
   2226  if (make_socket_reuseable(s) < 0) {
   2227    log_warn(LD_NET, "Error setting SO_REUSEADDR flag on new connection: %s",
   2228             tor_socket_strerror(errno));
   2229  }
   2230 
   2231  /* From ip(7): Inform the kernel to not reserve an ephemeral port when using
   2232   * bind(2) with a port number of 0. The port will later be automatically
   2233   * chosen at connect(2) time, in a way that allows sharing a source port as
   2234   * long as the 4-tuple is unique.
   2235   *
   2236   * This is needed for relays using OutboundBindAddresses because the port
   2237   * value in the bind address is set to 0. */
   2238 #ifdef IP_BIND_ADDRESS_NO_PORT
   2239  static int try_ip_bind_address_no_port = 1;
   2240  if (bindaddr && try_ip_bind_address_no_port &&
   2241      setsockopt(s, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &(int){1}, sizeof(int))) {
   2242    if (errno == EINVAL) {
   2243      log_notice(LD_NET, "Tor was built with support for "
   2244                         "IP_BIND_ADDRESS_NO_PORT, but the current kernel "
   2245                         "doesn't support it. This might cause Tor to run out "
   2246                         "of ephemeral ports more quickly.");
   2247      try_ip_bind_address_no_port = 0;
   2248    } else {
   2249      log_warn(LD_NET, "Error setting IP_BIND_ADDRESS_NO_PORT on new "
   2250                       "connection: %s", tor_socket_strerror(errno));
   2251    }
   2252  }
   2253 #endif
   2254 
   2255  if (bindaddr && bind(s, bindaddr, bindaddr_len) < 0) {
   2256    *socket_error = tor_socket_errno(s);
   2257    if (ERRNO_IS_EADDRINUSE(*socket_error)) {
   2258      socket_failed_from_tcp_port_exhaustion();
   2259      connection_check_oos(get_n_open_sockets(), 1);
   2260    } else {
   2261      log_warn(LD_NET,"Error binding network socket: %s",
   2262               tor_socket_strerror(*socket_error));
   2263      connection_check_oos(get_n_open_sockets(), 0);
   2264    }
   2265    tor_close_socket(s);
   2266    return -1;
   2267  }
   2268 
   2269  /*
   2270   * We've got the socket open and bound; give the OOS handler a chance to
   2271   * check against configured maximum socket number, but tell it no exhaustion
   2272   * failure.
   2273   */
   2274  connection_check_oos(get_n_open_sockets(), 0);
   2275 
   2276  tor_assert(options);
   2277  if (options->ConstrainedSockets)
   2278    set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
   2279 
   2280  if (connect(s, sa, sa_len) < 0) {
   2281    int e = tor_socket_errno(s);
   2282    if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
   2283      /* yuck. kill it. */
   2284      *socket_error = e;
   2285      log_info(LD_NET,
   2286               "connect() to socket failed: %s",
   2287               tor_socket_strerror(e));
   2288      tor_close_socket(s);
   2289      return -1;
   2290    } else {
   2291      inprogress = 1;
   2292    }
   2293  }
   2294 
   2295  note_connection(false /* outbound */, conn);
   2296 
   2297  /* it succeeded. we're connected. */
   2298  log_fn(inprogress ? LOG_DEBUG : LOG_INFO, LD_NET,
   2299         "Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").",
   2300         inprogress ? "in progress" : "established", s);
   2301  conn->s = s;
   2302  if (connection_add_connecting(conn) < 0) {
   2303    /* no space, forget it */
   2304    *socket_error = SOCK_ERRNO(ENOBUFS);
   2305    return -1;
   2306  }
   2307 
   2308  return inprogress ? 0 : 1;
   2309 }
   2310 
   2311 /* Log a message if connection attempt is made when IPv4 or IPv6 is disabled.
   2312 * Log a less severe message if we couldn't conform to ClientPreferIPv6ORPort
   2313 * or ClientPreferIPv6ORPort. */
   2314 static void
   2315 connection_connect_log_client_use_ip_version(const connection_t *conn)
   2316 {
   2317  const or_options_t *options = get_options();
   2318 
   2319  /* Only clients care about ClientUseIPv4/6, bail out early on servers, and
   2320   * on connections we don't care about */
   2321  if (server_mode(options) || !conn || conn->type == CONN_TYPE_EXIT) {
   2322    return;
   2323  }
   2324 
   2325  /* We're only prepared to log OR and DIR connections here */
   2326  if (conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR) {
   2327    return;
   2328  }
   2329 
   2330  const int must_ipv4 = !reachable_addr_use_ipv6(options);
   2331  const int must_ipv6 = (options->ClientUseIPv4 == 0);
   2332  const int pref_ipv6 = (conn->type == CONN_TYPE_OR
   2333                         ? reachable_addr_prefer_ipv6_orport(options)
   2334                         : reachable_addr_prefer_ipv6_dirport(options));
   2335  tor_addr_t real_addr;
   2336  tor_addr_copy(&real_addr, &conn->addr);
   2337 
   2338  /* Check if we broke a mandatory address family restriction */
   2339  if ((must_ipv4 && tor_addr_family(&real_addr) == AF_INET6)
   2340      || (must_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
   2341    log_info(LD_BUG, "Outgoing %s connection to %s violated ClientUseIPv%s 0.",
   2342             conn->type == CONN_TYPE_OR ? "OR" : "Dir",
   2343             fmt_addr(&real_addr),
   2344             options->ClientUseIPv4 == 0 ? "4" : "6");
   2345    log_backtrace_once(LOG_INFO, LD_BUG, "Address came from");
   2346  }
   2347 
   2348  /* Bridges are allowed to break IPv4/IPv6 ORPort preferences to connect to
   2349   * the node's configured address when ClientPreferIPv6ORPort is auto */
   2350  if (options->UseBridges && conn->type == CONN_TYPE_OR
   2351      && options->ClientPreferIPv6ORPort == -1) {
   2352    return;
   2353  }
   2354 
   2355  if (reachable_addr_use_ipv6(options)) {
   2356    log_info(LD_NET, "Our outgoing connection is using IPv%d.",
   2357             tor_addr_family(&real_addr) == AF_INET6 ? 6 : 4);
   2358  }
   2359 
   2360  /* Check if we couldn't satisfy an address family preference */
   2361  if ((!pref_ipv6 && tor_addr_family(&real_addr) == AF_INET6)
   2362      || (pref_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
   2363    log_info(LD_NET, "Outgoing connection to %s doesn't satisfy "
   2364             "ClientPreferIPv6%sPort %d, with ClientUseIPv4 %d, and "
   2365             "reachable_addr_use_ipv6 %d (ClientUseIPv6 %d and UseBridges "
   2366             "%d).",
   2367             fmt_addr(&real_addr),
   2368             conn->type == CONN_TYPE_OR ? "OR" : "Dir",
   2369             conn->type == CONN_TYPE_OR ? options->ClientPreferIPv6ORPort
   2370                                        : options->ClientPreferIPv6DirPort,
   2371             options->ClientUseIPv4, reachable_addr_use_ipv6(options),
   2372             options->ClientUseIPv6, options->UseBridges);
   2373  }
   2374 }
   2375 
   2376 /** Retrieve the outbound address depending on the protocol (IPv4 or IPv6)
   2377 * and the connection type (relay, exit, ...)
   2378 * Return a socket address or NULL in case nothing is configured.
   2379 **/
   2380 const tor_addr_t *
   2381 conn_get_outbound_address(sa_family_t family,
   2382             const or_options_t *options, unsigned int conn_type)
   2383 {
   2384  const tor_addr_t *ext_addr = NULL;
   2385 
   2386  int fam_index;
   2387  switch (family) {
   2388    case AF_INET:
   2389      fam_index = 0;
   2390      break;
   2391    case AF_INET6:
   2392      fam_index = 1;
   2393      break;
   2394    default:
   2395      return NULL;
   2396  }
   2397 
   2398  // If an exit connection, use the exit address (if present)
   2399  if (conn_type == CONN_TYPE_EXIT) {
   2400    if (!tor_addr_is_null(
   2401        &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT][fam_index])) {
   2402      ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT]
   2403                 [fam_index];
   2404    } else if (!tor_addr_is_null(
   2405                 &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
   2406                 [fam_index])) {
   2407      ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
   2408                 [fam_index];
   2409    }
   2410  } else { // All non-exit connections
   2411    if (!tor_addr_is_null(
   2412           &options->OutboundBindAddresses[OUTBOUND_ADDR_OR][fam_index])) {
   2413      ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_OR]
   2414                 [fam_index];
   2415    } else if (!tor_addr_is_null(
   2416                 &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
   2417                 [fam_index])) {
   2418      ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
   2419                 [fam_index];
   2420    }
   2421  }
   2422  return ext_addr;
   2423 }
   2424 
   2425 /** Take conn, make a nonblocking socket; try to connect to
   2426 * addr:port (port arrives in *host order*). If fail, return -1 and if
   2427 * applicable put your best guess about errno into *<b>socket_error</b>.
   2428 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
   2429 *
   2430 * addr:port can be different to conn->addr:conn->port if connecting through
   2431 * a proxy.
   2432 *
   2433 * address is used to make the logs useful.
   2434 *
   2435 * On success, add conn to the list of polled connections.
   2436 */
   2437 int
   2438 connection_connect(connection_t *conn, const char *address,
   2439                   const tor_addr_t *addr, uint16_t port, int *socket_error)
   2440 {
   2441  struct sockaddr_storage addrbuf;
   2442  struct sockaddr_storage bind_addr_ss;
   2443  struct sockaddr *bind_addr = NULL;
   2444  struct sockaddr *dest_addr;
   2445  int dest_addr_len, bind_addr_len = 0;
   2446 
   2447  /* Log if we didn't stick to ClientUseIPv4/6 or ClientPreferIPv6OR/DirPort
   2448   */
   2449  connection_connect_log_client_use_ip_version(conn);
   2450 
   2451  if (!tor_addr_is_loopback(addr)) {
   2452    const tor_addr_t *ext_addr = NULL;
   2453    ext_addr = conn_get_outbound_address(tor_addr_family(addr), get_options(),
   2454                                         conn->type);
   2455    if (ext_addr) {
   2456      memset(&bind_addr_ss, 0, sizeof(bind_addr_ss));
   2457      bind_addr_len = tor_addr_to_sockaddr(ext_addr, 0,
   2458                                           (struct sockaddr *) &bind_addr_ss,
   2459                                           sizeof(bind_addr_ss));
   2460      if (bind_addr_len == 0) {
   2461        log_warn(LD_NET,
   2462                 "Error converting OutboundBindAddress %s into sockaddr. "
   2463                 "Ignoring.", fmt_and_decorate_addr(ext_addr));
   2464      } else {
   2465        bind_addr = (struct sockaddr *)&bind_addr_ss;
   2466      }
   2467    }
   2468  }
   2469 
   2470  memset(&addrbuf,0,sizeof(addrbuf));
   2471  dest_addr = (struct sockaddr*) &addrbuf;
   2472  dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
   2473  tor_assert(dest_addr_len > 0);
   2474 
   2475  log_debug(LD_NET, "Connecting to %s:%u.",
   2476            escaped_safe_str_client(address), port);
   2477 
   2478  return connection_connect_sockaddr(conn, dest_addr, dest_addr_len,
   2479                                     bind_addr, bind_addr_len, socket_error);
   2480 }
   2481 
   2482 #ifdef HAVE_SYS_UN_H
   2483 
   2484 /** Take conn, make a nonblocking socket; try to connect to
   2485 * an AF_UNIX socket at socket_path. If fail, return -1 and if applicable
   2486 * put your best guess about errno into *<b>socket_error</b>. Else assign s
   2487 * to conn-\>s: if connected return 1, if EAGAIN return 0.
   2488 *
   2489 * On success, add conn to the list of polled connections.
   2490 */
   2491 int
   2492 connection_connect_unix(connection_t *conn, const char *socket_path,
   2493                        int *socket_error)
   2494 {
   2495  struct sockaddr_un dest_addr;
   2496 
   2497  tor_assert(socket_path);
   2498 
   2499  /* Check that we'll be able to fit it into dest_addr later */
   2500  if (strlen(socket_path) + 1 > sizeof(dest_addr.sun_path)) {
   2501    log_warn(LD_NET,
   2502             "Path %s is too long for an AF_UNIX socket\n",
   2503             escaped_safe_str_client(socket_path));
   2504    *socket_error = SOCK_ERRNO(ENAMETOOLONG);
   2505    return -1;
   2506  }
   2507 
   2508  memset(&dest_addr, 0, sizeof(dest_addr));
   2509  dest_addr.sun_family = AF_UNIX;
   2510  strlcpy(dest_addr.sun_path, socket_path, sizeof(dest_addr.sun_path));
   2511 
   2512  log_debug(LD_NET,
   2513            "Connecting to AF_UNIX socket at %s.",
   2514            escaped_safe_str_client(socket_path));
   2515 
   2516  return connection_connect_sockaddr(conn,
   2517                       (struct sockaddr *)&dest_addr, sizeof(dest_addr),
   2518                       NULL, 0, socket_error);
   2519 }
   2520 
   2521 #endif /* defined(HAVE_SYS_UN_H) */
   2522 
   2523 /** Convert state number to string representation for logging purposes.
   2524 */
   2525 static const char *
   2526 connection_proxy_state_to_string(int state)
   2527 {
   2528  static const char *unknown = "???";
   2529  static const char *states[] = {
   2530    "PROXY_NONE",
   2531    "PROXY_INFANT",
   2532    "PROXY_HTTPS_WANT_CONNECT_OK",
   2533    "PROXY_SOCKS4_WANT_CONNECT_OK",
   2534    "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
   2535    "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
   2536    "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
   2537    "PROXY_SOCKS5_WANT_CONNECT_OK",
   2538    "PROXY_HAPROXY_WAIT_FOR_FLUSH",
   2539    "PROXY_CONNECTED",
   2540  };
   2541 
   2542  CTASSERT(ARRAY_LENGTH(states) == PROXY_CONNECTED+1);
   2543 
   2544  if (state < PROXY_NONE || state > PROXY_CONNECTED)
   2545    return unknown;
   2546 
   2547  return states[state];
   2548 }
   2549 
   2550 /** Returns the proxy type used by tor for a single connection, for
   2551 *  logging or high-level purposes. Don't use it to fill the
   2552 *  <b>proxy_type</b> field of or_connection_t; use the actual proxy
   2553 *  protocol instead.*/
   2554 static int
   2555 conn_get_proxy_type(const connection_t *conn)
   2556 {
   2557  const or_options_t *options = get_options();
   2558 
   2559  if (options->ClientTransportPlugin) {
   2560    /* If we have plugins configured *and* this addr/port is a known bridge
   2561     * with a transport, then we should be PROXY_PLUGGABLE. */
   2562    const transport_t *transport = NULL;
   2563    int r;
   2564    r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
   2565    if (r == 0 && transport)
   2566      return PROXY_PLUGGABLE;
   2567  }
   2568 
   2569  /* In all other cases, we're using a global proxy. */
   2570  if (options->HTTPSProxy)
   2571    return PROXY_CONNECT;
   2572  else if (options->Socks4Proxy)
   2573    return PROXY_SOCKS4;
   2574  else if (options->Socks5Proxy)
   2575    return PROXY_SOCKS5;
   2576  else if (options->TCPProxy) {
   2577    /* The only supported protocol in TCPProxy is haproxy. */
   2578    tor_assert(options->TCPProxyProtocol == TCP_PROXY_PROTOCOL_HAPROXY);
   2579    return PROXY_HAPROXY;
   2580  } else
   2581    return PROXY_NONE;
   2582 }
   2583 
   2584 /* One byte for the version, one for the command, two for the
   2585   port, and four for the addr... and, one more for the
   2586   username NUL: */
   2587 #define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1)
   2588 
   2589 /** Write a proxy request of https to conn for conn->addr:conn->port,
   2590 * authenticating with the auth details given in the configuration
   2591 * (if available).
   2592 *
   2593 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
   2594 * 0 otherwise.
   2595 */
   2596 static int
   2597 connection_https_proxy_connect(connection_t *conn)
   2598 {
   2599  tor_assert(conn);
   2600 
   2601  const or_options_t *options = get_options();
   2602  char buf[1024];
   2603  char *base64_authenticator = NULL;
   2604  const char *authenticator = options->HTTPSProxyAuthenticator;
   2605 
   2606  /* Send HTTP CONNECT and authentication (if available) in
   2607   * one request */
   2608 
   2609  if (authenticator) {
   2610    base64_authenticator = alloc_http_authenticator(authenticator);
   2611    if (!base64_authenticator)
   2612      log_warn(LD_OR, "Encoding https authenticator failed");
   2613  }
   2614 
   2615  if (base64_authenticator) {
   2616    const char *addrport = fmt_addrport(&conn->addr, conn->port);
   2617    tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.1\r\n"
   2618        "Host: %s\r\n"
   2619        "Proxy-Authorization: Basic %s\r\n\r\n",
   2620        addrport,
   2621        addrport,
   2622        base64_authenticator);
   2623    tor_free(base64_authenticator);
   2624  } else {
   2625    tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.0\r\n\r\n",
   2626        fmt_addrport(&conn->addr, conn->port));
   2627  }
   2628 
   2629  connection_buf_add(buf, strlen(buf), conn);
   2630  conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
   2631 
   2632  return 0;
   2633 }
   2634 
   2635 /** Write a proxy request of socks4 to conn for conn->addr:conn->port.
   2636 *
   2637 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
   2638 * 0 otherwise.
   2639 */
   2640 static int
   2641 connection_socks4_proxy_connect(connection_t *conn)
   2642 {
   2643  tor_assert(conn);
   2644 
   2645  unsigned char *buf;
   2646  uint16_t portn;
   2647  uint32_t ip4addr;
   2648  size_t buf_size = 0;
   2649  char *socks_args_string = NULL;
   2650 
   2651  /* Send a SOCKS4 connect request */
   2652 
   2653  if (tor_addr_family(&conn->addr) != AF_INET) {
   2654    log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
   2655    return -1;
   2656  }
   2657 
   2658  { /* If we are here because we are trying to connect to a
   2659       pluggable transport proxy, check if we have any SOCKS
   2660       arguments to transmit. If we do, compress all arguments to
   2661       a single string in 'socks_args_string': */
   2662 
   2663    if (conn_get_proxy_type(conn) == PROXY_PLUGGABLE) {
   2664      socks_args_string =
   2665        pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
   2666      if (socks_args_string)
   2667        log_debug(LD_NET, "Sending out '%s' as our SOCKS argument string.",
   2668            socks_args_string);
   2669    }
   2670  }
   2671 
   2672  { /* Figure out the buffer size we need for the SOCKS message: */
   2673 
   2674    buf_size = SOCKS4_STANDARD_BUFFER_SIZE;
   2675 
   2676    /* If we have a SOCKS argument string, consider its size when
   2677       calculating the buffer size: */
   2678    if (socks_args_string)
   2679      buf_size += strlen(socks_args_string);
   2680  }
   2681 
   2682  buf = tor_malloc_zero(buf_size);
   2683 
   2684  ip4addr = tor_addr_to_ipv4n(&conn->addr);
   2685  portn = htons(conn->port);
   2686 
   2687  buf[0] = 4; /* version */
   2688  buf[1] = SOCKS_COMMAND_CONNECT; /* command */
   2689  memcpy(buf + 2, &portn, 2); /* port */
   2690  memcpy(buf + 4, &ip4addr, 4); /* addr */
   2691 
   2692  /* Next packet field is the userid. If we have pluggable
   2693     transport SOCKS arguments, we have to embed them
   2694     there. Otherwise, we use an empty userid.  */
   2695  if (socks_args_string) { /* place the SOCKS args string: */
   2696    tor_assert(strlen(socks_args_string) > 0);
   2697    tor_assert(buf_size >=
   2698        SOCKS4_STANDARD_BUFFER_SIZE + strlen(socks_args_string));
   2699    strlcpy((char *)buf + 8, socks_args_string, buf_size - 8);
   2700    tor_free(socks_args_string);
   2701  } else {
   2702    buf[8] = 0; /* no userid */
   2703  }
   2704 
   2705  connection_buf_add((char *)buf, buf_size, conn);
   2706  tor_free(buf);
   2707 
   2708  conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
   2709  return 0;
   2710 }
   2711 
   2712 /** Write a proxy request of socks5 to conn for conn->addr:conn->port,
   2713 * authenticating with the auth details given in the configuration
   2714 * (if available).
   2715 *
   2716 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
   2717 * 0 otherwise.
   2718 */
   2719 static int
   2720 connection_socks5_proxy_connect(connection_t *conn)
   2721 {
   2722  tor_assert(conn);
   2723 
   2724  const or_options_t *options = get_options();
   2725  unsigned char buf[4]; /* fields: vers, num methods, method list */
   2726 
   2727  /* Send a SOCKS5 greeting (connect request must wait) */
   2728 
   2729  buf[0] = 5; /* version */
   2730 
   2731  /* We have to use SOCKS5 authentication, if we have a
   2732     Socks5ProxyUsername or if we want to pass arguments to our
   2733     pluggable transport proxy: */
   2734  if ((options->Socks5ProxyUsername) ||
   2735      (conn_get_proxy_type(conn) == PROXY_PLUGGABLE &&
   2736       (get_socks_args_by_bridge_addrport(&conn->addr, conn->port)))) {
   2737  /* number of auth methods */
   2738    buf[1] = 2;
   2739    buf[2] = 0x00; /* no authentication */
   2740    buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
   2741    conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
   2742  } else {
   2743    buf[1] = 1;
   2744    buf[2] = 0x00; /* no authentication */
   2745    conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
   2746  }
   2747 
   2748  connection_buf_add((char *)buf, 2 + buf[1], conn);
   2749  return 0;
   2750 }
   2751 
   2752 /** Write a proxy request of haproxy to conn for conn->addr:conn->port.
   2753 *
   2754 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
   2755 * 0 otherwise.
   2756 */
   2757 static int
   2758 connection_haproxy_proxy_connect(connection_t *conn)
   2759 {
   2760  int ret = 0;
   2761  tor_addr_port_t *addr_port = tor_addr_port_new(&conn->addr, conn->port);
   2762  char *buf = haproxy_format_proxy_header_line(addr_port);
   2763 
   2764  if (buf == NULL) {
   2765    ret = -1;
   2766    goto done;
   2767  }
   2768 
   2769  connection_buf_add(buf, strlen(buf), conn);
   2770  /* In haproxy, we don't have to wait for the response, but we wait for ack.
   2771   * So we can set the state to be PROXY_HAPROXY_WAIT_FOR_FLUSH. */
   2772  conn->proxy_state = PROXY_HAPROXY_WAIT_FOR_FLUSH;
   2773 
   2774  ret = 0;
   2775 done:
   2776  tor_free(buf);
   2777  tor_free(addr_port);
   2778  return ret;
   2779 }
   2780 
   2781 /** Write a proxy request of <b>type</b> (socks4, socks5, https, haproxy)
   2782 * to conn for conn->addr:conn->port, authenticating with the auth details
   2783 * given in the configuration (if available). SOCKS 5 and HTTP CONNECT
   2784 * proxies support authentication.
   2785 *
   2786 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
   2787 * 0 otherwise.
   2788 *
   2789 * Use connection_read_proxy_handshake() to complete the handshake.
   2790 */
   2791 int
   2792 connection_proxy_connect(connection_t *conn, int type)
   2793 {
   2794  int ret = 0;
   2795 
   2796  tor_assert(conn);
   2797 
   2798  switch (type) {
   2799    case PROXY_CONNECT:
   2800      ret = connection_https_proxy_connect(conn);
   2801      break;
   2802 
   2803    case PROXY_SOCKS4:
   2804      ret = connection_socks4_proxy_connect(conn);
   2805      break;
   2806 
   2807    case PROXY_SOCKS5:
   2808      ret = connection_socks5_proxy_connect(conn);
   2809      break;
   2810 
   2811    case PROXY_HAPROXY:
   2812      ret = connection_haproxy_proxy_connect(conn);
   2813      break;
   2814 
   2815    default:
   2816      log_err(LD_BUG, "Invalid proxy protocol, %d", type);
   2817      tor_fragile_assert();
   2818      ret = -1;
   2819      break;
   2820  }
   2821 
   2822  if (ret == 0) {
   2823    log_debug(LD_NET, "set state %s",
   2824              connection_proxy_state_to_string(conn->proxy_state));
   2825  }
   2826 
   2827  return ret;
   2828 }
   2829 
   2830 /** Read conn's inbuf. If the http response from the proxy is all
   2831 * here, make sure it's good news, then return 1. If it's bad news,
   2832 * return -1. Else return 0 and hope for better luck next time.
   2833 */
   2834 static int
   2835 connection_read_https_proxy_response(connection_t *conn)
   2836 {
   2837  char *headers;
   2838  char *reason=NULL;
   2839  int status_code;
   2840  time_t date_header;
   2841 
   2842  switch (fetch_from_buf_http(conn->inbuf,
   2843                              &headers, MAX_HEADERS_SIZE,
   2844                              NULL, NULL, 10000, 0)) {
   2845    case -1: /* overflow */
   2846      log_warn(LD_PROTOCOL,
   2847               "Your https proxy sent back an oversized response. Closing.");
   2848      return -1;
   2849    case 0:
   2850      log_info(LD_NET,"https proxy response not all here yet. Waiting.");
   2851      return 0;
   2852    /* case 1, fall through */
   2853  }
   2854 
   2855  if (parse_http_response(headers, &status_code, &date_header,
   2856                          NULL, &reason) < 0) {
   2857    log_warn(LD_NET,
   2858             "Unparseable headers from proxy (%s). Closing.",
   2859             connection_describe(conn));
   2860    tor_free(headers);
   2861    return -1;
   2862  }
   2863  tor_free(headers);
   2864  if (!reason) reason = tor_strdup("[no reason given]");
   2865 
   2866  if (status_code == 200) {
   2867    log_info(LD_NET,
   2868             "HTTPS connect for %s successful! (200 %s) Starting TLS.",
   2869             connection_describe(conn), escaped(reason));
   2870    tor_free(reason);
   2871    return 1;
   2872  }
   2873  /* else, bad news on the status code */
   2874  switch (status_code) {
   2875    case 403:
   2876      log_warn(LD_NET,
   2877             "The https proxy refused to allow connection to %s "
   2878             "(status code %d, %s). Closing.",
   2879             conn->address, status_code, escaped(reason));
   2880      break;
   2881    default:
   2882      log_warn(LD_NET,
   2883             "The https proxy sent back an unexpected status code %d (%s). "
   2884             "Closing.",
   2885             status_code, escaped(reason));
   2886      break;
   2887  }
   2888  tor_free(reason);
   2889  return -1;
   2890 }
   2891 
   2892 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
   2893 * and <b>conn->port</b> into the request.
   2894 */
   2895 static void
   2896 connection_send_socks5_connect(connection_t *conn)
   2897 {
   2898  unsigned char buf[1024];
   2899  size_t reqsize = 6;
   2900  uint16_t port = htons(conn->port);
   2901 
   2902  buf[0] = 5; /* version */
   2903  buf[1] = SOCKS_COMMAND_CONNECT; /* command */
   2904  buf[2] = 0; /* reserved */
   2905 
   2906  if (tor_addr_family(&conn->addr) == AF_INET) {
   2907    uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
   2908 
   2909    buf[3] = 1;
   2910    reqsize += 4;
   2911    memcpy(buf + 4, &addr, 4);
   2912    memcpy(buf + 8, &port, 2);
   2913  } else { /* AF_INET6 */
   2914    buf[3] = 4;
   2915    reqsize += 16;
   2916    memcpy(buf + 4, tor_addr_to_in6_addr8(&conn->addr), 16);
   2917    memcpy(buf + 20, &port, 2);
   2918  }
   2919 
   2920  connection_buf_add((char *)buf, reqsize, conn);
   2921 
   2922  conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
   2923 }
   2924 
   2925 /** Wrapper around fetch_from_buf_socks_client: see that functions
   2926 * for documentation of its behavior. */
   2927 static int
   2928 connection_fetch_from_buf_socks_client(connection_t *conn,
   2929                                       int state, char **reason)
   2930 {
   2931  return fetch_from_buf_socks_client(conn->inbuf, state, reason);
   2932 }
   2933 
   2934 /** Call this from connection_*_process_inbuf() to advance the proxy
   2935 * handshake.
   2936 *
   2937 * No matter what proxy protocol is used, if this function returns 1, the
   2938 * handshake is complete, and the data remaining on inbuf may contain the
   2939 * start of the communication with the requested server.
   2940 *
   2941 * Returns 0 if the current buffer contains an incomplete response, and -1
   2942 * on error.
   2943 */
   2944 int
   2945 connection_read_proxy_handshake(connection_t *conn)
   2946 {
   2947  int ret = 0;
   2948  char *reason = NULL;
   2949 
   2950  log_debug(LD_NET, "enter state %s",
   2951            connection_proxy_state_to_string(conn->proxy_state));
   2952 
   2953  switch (conn->proxy_state) {
   2954    case PROXY_HTTPS_WANT_CONNECT_OK:
   2955      ret = connection_read_https_proxy_response(conn);
   2956      if (ret == 1)
   2957        conn->proxy_state = PROXY_CONNECTED;
   2958      break;
   2959 
   2960    case PROXY_SOCKS4_WANT_CONNECT_OK:
   2961      ret = connection_fetch_from_buf_socks_client(conn,
   2962                                                   conn->proxy_state,
   2963                                                   &reason);
   2964      if (ret == 1)
   2965        conn->proxy_state = PROXY_CONNECTED;
   2966      break;
   2967 
   2968    case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
   2969      ret = connection_fetch_from_buf_socks_client(conn,
   2970                                                   conn->proxy_state,
   2971                                                   &reason);
   2972      /* no auth needed, do connect */
   2973      if (ret == 1) {
   2974        connection_send_socks5_connect(conn);
   2975        ret = 0;
   2976      }
   2977      break;
   2978 
   2979    case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
   2980      ret = connection_fetch_from_buf_socks_client(conn,
   2981                                                   conn->proxy_state,
   2982                                                   &reason);
   2983 
   2984      /* send auth if needed, otherwise do connect */
   2985      if (ret == 1) {
   2986        connection_send_socks5_connect(conn);
   2987        ret = 0;
   2988      } else if (ret == 2) {
   2989        unsigned char buf[1024];
   2990        size_t reqsize, usize, psize;
   2991        const char *user, *pass;
   2992        char *socks_args_string = NULL;
   2993 
   2994        if (conn_get_proxy_type(conn) == PROXY_PLUGGABLE) {
   2995          socks_args_string =
   2996            pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
   2997          if (!socks_args_string) {
   2998            log_warn(LD_NET, "Could not create SOCKS args string for PT.");
   2999            ret = -1;
   3000            break;
   3001          }
   3002 
   3003          log_debug(LD_NET, "PT SOCKS5 arguments: %s", socks_args_string);
   3004          tor_assert(strlen(socks_args_string) > 0);
   3005          tor_assert(strlen(socks_args_string) <= MAX_SOCKS5_AUTH_SIZE_TOTAL);
   3006 
   3007          if (strlen(socks_args_string) > MAX_SOCKS5_AUTH_FIELD_SIZE) {
   3008            user = socks_args_string;
   3009            usize = MAX_SOCKS5_AUTH_FIELD_SIZE;
   3010            pass = socks_args_string + MAX_SOCKS5_AUTH_FIELD_SIZE;
   3011            psize = strlen(socks_args_string) - MAX_SOCKS5_AUTH_FIELD_SIZE;
   3012          } else {
   3013            user = socks_args_string;
   3014            usize = strlen(socks_args_string);
   3015            pass = "\0";
   3016            psize = 1;
   3017          }
   3018        } else if (get_options()->Socks5ProxyUsername) {
   3019          user = get_options()->Socks5ProxyUsername;
   3020          pass = get_options()->Socks5ProxyPassword;
   3021          tor_assert(user && pass);
   3022          usize = strlen(user);
   3023          psize = strlen(pass);
   3024        } else {
   3025          log_err(LD_BUG, "We entered %s for no reason!", __func__);
   3026          tor_fragile_assert();
   3027          ret = -1;
   3028          break;
   3029        }
   3030 
   3031        /* Username and password lengths should have been checked
   3032           above and during torrc parsing. */
   3033        tor_assert(usize <= MAX_SOCKS5_AUTH_FIELD_SIZE &&
   3034                   psize <= MAX_SOCKS5_AUTH_FIELD_SIZE);
   3035        reqsize = 3 + usize + psize;
   3036 
   3037        buf[0] = 1; /* negotiation version */
   3038        buf[1] = usize;
   3039        memcpy(buf + 2, user, usize);
   3040        buf[2 + usize] = psize;
   3041        memcpy(buf + 3 + usize, pass, psize);
   3042 
   3043        if (socks_args_string)
   3044          tor_free(socks_args_string);
   3045 
   3046        connection_buf_add((char *)buf, reqsize, conn);
   3047 
   3048        conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
   3049        ret = 0;
   3050      }
   3051      break;
   3052 
   3053    case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
   3054      ret = connection_fetch_from_buf_socks_client(conn,
   3055                                                   conn->proxy_state,
   3056                                                   &reason);
   3057      /* send the connect request */
   3058      if (ret == 1) {
   3059        connection_send_socks5_connect(conn);
   3060        ret = 0;
   3061      }
   3062      break;
   3063 
   3064    case PROXY_SOCKS5_WANT_CONNECT_OK:
   3065      ret = connection_fetch_from_buf_socks_client(conn,
   3066                                                   conn->proxy_state,
   3067                                                   &reason);
   3068      if (ret == 1)
   3069        conn->proxy_state = PROXY_CONNECTED;
   3070      break;
   3071 
   3072    default:
   3073      log_err(LD_BUG, "Invalid proxy_state for reading, %d",
   3074              conn->proxy_state);
   3075      tor_fragile_assert();
   3076      ret = -1;
   3077      break;
   3078  }
   3079 
   3080  log_debug(LD_NET, "leaving state %s",
   3081            connection_proxy_state_to_string(conn->proxy_state));
   3082 
   3083  if (ret < 0) {
   3084    if (reason) {
   3085      log_warn(LD_NET, "Proxy Client: unable to connect %s (%s)",
   3086               connection_describe(conn), escaped(reason));
   3087      tor_free(reason);
   3088    } else {
   3089      log_warn(LD_NET, "Proxy Client: unable to connect %s",
   3090               connection_describe(conn));
   3091    }
   3092  } else if (ret == 1) {
   3093    log_info(LD_NET, "Proxy Client: %s successful",
   3094             connection_describe(conn));
   3095  }
   3096 
   3097  return ret;
   3098 }
   3099 
   3100 /** Given a list of listener connections in <b>old_conns</b>, and list of
   3101 * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
   3102 * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
   3103 *
   3104 * Remove from <b>old_conns</b> every connection that has a corresponding
   3105 * entry in <b>ports</b>.  Add to <b>new_conns</b> new every connection we
   3106 * launch. If we may need to perform socket rebind when creating new
   3107 * listener that replaces old one, create a <b>listener_replacement_t</b>
   3108 * struct for affected pair  and add it to <b>replacements</b>.
   3109 *
   3110 * If <b>control_listeners_only</b> is true, then we only open control
   3111 * listeners, and we do not remove any noncontrol listeners from
   3112 * old_conns.
   3113 *
   3114 * Return 0 on success, -1 on failure.
   3115 **/
   3116 static int
   3117 retry_listener_ports(smartlist_t *old_conns,
   3118                     const smartlist_t *ports,
   3119                     smartlist_t *new_conns,
   3120                     smartlist_t *replacements,
   3121                     int control_listeners_only)
   3122 {
   3123 #ifndef ENABLE_LISTENER_REBIND
   3124  (void)replacements;
   3125 #endif
   3126 
   3127  smartlist_t *launch = smartlist_new();
   3128  int r = 0;
   3129 
   3130  if (control_listeners_only) {
   3131    SMARTLIST_FOREACH(ports, port_cfg_t *, p, {
   3132        if (p->type == CONN_TYPE_CONTROL_LISTENER)
   3133          smartlist_add(launch, p);
   3134    });
   3135  } else {
   3136    smartlist_add_all(launch, ports);
   3137  }
   3138 
   3139  /* Iterate through old_conns, comparing it to launch: remove from both lists
   3140   * each pair of elements that corresponds to the same port. */
   3141  SMARTLIST_FOREACH_BEGIN(old_conns, connection_t *, conn) {
   3142    const port_cfg_t *found_port = NULL;
   3143 
   3144    /* Okay, so this is a listener.  Is it configured? */
   3145    /* That is, is it either: 1) exact match - address and port
   3146     * pair match exactly between old listener and new port; or 2)
   3147     * wildcard match - port matches exactly, but *one* of the
   3148     * addresses is wildcard (0.0.0.0 or ::)?
   3149     */
   3150    SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, wanted) {
   3151      if (conn->type != wanted->type)
   3152        continue;
   3153      if ((conn->socket_family != AF_UNIX && wanted->is_unix_addr) ||
   3154          (conn->socket_family == AF_UNIX && ! wanted->is_unix_addr))
   3155        continue;
   3156 
   3157      if (wanted->server_cfg.no_listen)
   3158        continue; /* We don't want to open a listener for this one */
   3159 
   3160      if (wanted->is_unix_addr) {
   3161        if (conn->socket_family == AF_UNIX &&
   3162            !strcmp(wanted->unix_addr, conn->address)) {
   3163          found_port = wanted;
   3164          break;
   3165        }
   3166      } else {
   3167        /* Numeric values of old and new port match exactly. */
   3168        const int port_matches_exact = (wanted->port == conn->port);
   3169        /* Ports match semantically - either their specific values
   3170           match exactly, or new port is 'auto'.
   3171         */
   3172        const int port_matches = (wanted->port == CFG_AUTO_PORT ||
   3173                                  port_matches_exact);
   3174 
   3175        if (port_matches && tor_addr_eq(&wanted->addr, &conn->addr)) {
   3176          found_port = wanted;
   3177          break;
   3178        }
   3179 #ifdef ENABLE_LISTENER_REBIND
   3180        /* Rebinding may be needed if all of the following are true:
   3181         * 1) Address family is the same in old and new listeners.
   3182         * 2) Port number matches exactly (numeric value is the same).
   3183         * 3) *One* of listeners (either old one or new one) has a
   3184         *    wildcard IP address (0.0.0.0 or [::]).
   3185         *
   3186         * These are the exact conditions for a first bind() syscall
   3187         * to fail with EADDRINUSE.
   3188         */
   3189        const int may_need_rebind =
   3190          tor_addr_family(&wanted->addr) == tor_addr_family(&conn->addr) &&
   3191          port_matches_exact && bool_neq(tor_addr_is_null(&wanted->addr),
   3192                                         tor_addr_is_null(&conn->addr));
   3193        if (replacements && may_need_rebind) {
   3194          listener_replacement_t *replacement =
   3195            tor_malloc(sizeof(listener_replacement_t));
   3196 
   3197          replacement->old_conn = conn;
   3198          replacement->new_port = wanted;
   3199          smartlist_add(replacements, replacement);
   3200 
   3201          SMARTLIST_DEL_CURRENT(launch, wanted);
   3202          SMARTLIST_DEL_CURRENT(old_conns, conn);
   3203          break;
   3204        }
   3205 #endif /* defined(ENABLE_LISTENER_REBIND) */
   3206      }
   3207    } SMARTLIST_FOREACH_END(wanted);
   3208 
   3209    if (found_port) {
   3210      /* This listener is already running; we don't need to launch it. */
   3211      //log_debug(LD_NET, "Already have %s on %s:%d",
   3212      //    conn_type_to_string(found_port->type), conn->address, conn->port);
   3213      smartlist_remove(launch, found_port);
   3214      /* And we can remove the connection from old_conns too. */
   3215      SMARTLIST_DEL_CURRENT(old_conns, conn);
   3216    }
   3217  } SMARTLIST_FOREACH_END(conn);
   3218 
   3219  /* Now open all the listeners that are configured but not opened. */
   3220  SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, port) {
   3221    int skip = 0;
   3222    connection_t *conn = connection_listener_new_for_port(port, &skip, NULL);
   3223 
   3224    if (conn && new_conns)
   3225      smartlist_add(new_conns, conn);
   3226    else if (!skip)
   3227      r = -1;
   3228  } SMARTLIST_FOREACH_END(port);
   3229 
   3230  smartlist_free(launch);
   3231 
   3232  return r;
   3233 }
   3234 
   3235 /** Launch listeners for each port you should have open.  Only launch
   3236 * listeners who are not already open, and only close listeners we no longer
   3237 * want.
   3238 *
   3239 * Add all new connections to <b>new_conns</b>.
   3240 *
   3241 * If <b>close_all_noncontrol</b> is true, then we only open control
   3242 * listeners, and we close all other listeners.
   3243 */
   3244 int
   3245 retry_all_listeners(smartlist_t *new_conns, int close_all_noncontrol)
   3246 {
   3247  smartlist_t *listeners = smartlist_new();
   3248  smartlist_t *replacements = smartlist_new();
   3249  const or_options_t *options = get_options();
   3250  int retval = 0;
   3251  const uint16_t old_or_port = routerconf_find_or_port(options, AF_INET);
   3252  const uint16_t old_or_port_ipv6 =
   3253    routerconf_find_or_port(options,AF_INET6);
   3254  const uint16_t old_dir_port = routerconf_find_dir_port(options, 0);
   3255 
   3256  SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
   3257    if (connection_is_listener(conn) && !conn->marked_for_close)
   3258      smartlist_add(listeners, conn);
   3259  } SMARTLIST_FOREACH_END(conn);
   3260 
   3261  if (retry_listener_ports(listeners,
   3262                           get_configured_ports(),
   3263                           new_conns,
   3264                           replacements,
   3265                           close_all_noncontrol) < 0)
   3266    retval = -1;
   3267 
   3268 #ifdef ENABLE_LISTENER_REBIND
   3269  if (smartlist_len(replacements))
   3270    log_debug(LD_NET, "%d replacements - starting rebinding loop.",
   3271              smartlist_len(replacements));
   3272 
   3273  SMARTLIST_FOREACH_BEGIN(replacements, listener_replacement_t *, r) {
   3274    int addr_in_use = 0;
   3275    int skip = 0;
   3276 
   3277    tor_assert(r->new_port);
   3278    tor_assert(r->old_conn);
   3279 
   3280    connection_t *new_conn =
   3281      connection_listener_new_for_port(r->new_port, &skip, &addr_in_use);
   3282    connection_t *old_conn = r->old_conn;
   3283 
   3284    if (skip) {
   3285      log_debug(LD_NET, "Skipping creating new listener for %s",
   3286                connection_describe(old_conn));
   3287      continue;
   3288    }
   3289 
   3290    connection_close_immediate(old_conn);
   3291    connection_mark_for_close(old_conn);
   3292 
   3293    if (addr_in_use) {
   3294      new_conn = connection_listener_new_for_port(r->new_port,
   3295                                                  &skip, &addr_in_use);
   3296    }
   3297 
   3298    /* There are many reasons why we can't open a new listener port so in case
   3299     * we hit those, bail early so tor can stop. */
   3300    if (!new_conn) {
   3301      log_warn(LD_NET, "Unable to create listener port: %s:%d",
   3302               fmt_and_decorate_addr(&r->new_port->addr), r->new_port->port);
   3303      retval = -1;
   3304      break;
   3305    }
   3306 
   3307    smartlist_add(new_conns, new_conn);
   3308 
   3309    char *old_desc = tor_strdup(connection_describe(old_conn));
   3310    log_notice(LD_NET, "Closed no-longer-configured %s "
   3311                       "(replaced by %s)",
   3312               old_desc, connection_describe(new_conn));
   3313    tor_free(old_desc);
   3314  } SMARTLIST_FOREACH_END(r);
   3315 #endif /* defined(ENABLE_LISTENER_REBIND) */
   3316 
   3317  /* Any members that were still in 'listeners' don't correspond to
   3318   * any configured port.  Kill 'em. */
   3319  SMARTLIST_FOREACH_BEGIN(listeners, connection_t *, conn) {
   3320    log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
   3321               conn_type_to_string(conn->type),
   3322               fmt_and_decorate_addr(&conn->addr), conn->port);
   3323    connection_close_immediate(conn);
   3324    connection_mark_for_close(conn);
   3325  } SMARTLIST_FOREACH_END(conn);
   3326 
   3327  smartlist_free(listeners);
   3328  /* Cleanup any remaining listener replacement. */
   3329  SMARTLIST_FOREACH(replacements, listener_replacement_t *, r, tor_free(r));
   3330  smartlist_free(replacements);
   3331 
   3332  if (old_or_port != routerconf_find_or_port(options, AF_INET) ||
   3333      old_or_port_ipv6 != routerconf_find_or_port(options, AF_INET6) ||
   3334      old_dir_port != routerconf_find_dir_port(options, 0)) {
   3335    /* Our chosen ORPort or DirPort is not what it used to be: the
   3336     * descriptor we had (if any) should be regenerated.  (We won't
   3337     * automatically notice this because of changes in the option,
   3338     * since the value could be "auto".) */
   3339    mark_my_descriptor_dirty("Chosen Or/DirPort changed");
   3340  }
   3341 
   3342  return retval;
   3343 }
   3344 
   3345 /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
   3346 void
   3347 connection_mark_all_noncontrol_listeners(void)
   3348 {
   3349  SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
   3350    if (conn->marked_for_close)
   3351      continue;
   3352    if (conn->type == CONN_TYPE_CONTROL_LISTENER)
   3353      continue;
   3354    if (connection_is_listener(conn))
   3355      connection_mark_for_close(conn);
   3356  } SMARTLIST_FOREACH_END(conn);
   3357 }
   3358 
   3359 /** Mark every external connection not used for controllers for close. */
   3360 void
   3361 connection_mark_all_noncontrol_connections(void)
   3362 {
   3363  SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
   3364    if (conn->marked_for_close)
   3365      continue;
   3366    switch (conn->type) {
   3367      case CONN_TYPE_CONTROL_LISTENER:
   3368      case CONN_TYPE_CONTROL:
   3369        break;
   3370      case CONN_TYPE_AP:
   3371        connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
   3372                                      END_STREAM_REASON_HIBERNATING);
   3373        break;
   3374      case CONN_TYPE_OR:
   3375        {
   3376          or_connection_t *orconn = TO_OR_CONN(conn);
   3377          if (orconn->chan) {
   3378            connection_or_close_normally(orconn, 0);
   3379          } else {
   3380            /*
   3381             * There should have been one, but mark for close and hope
   3382             * for the best..
   3383             */
   3384            connection_mark_for_close(conn);
   3385          }
   3386        }
   3387        break;
   3388      default:
   3389        connection_mark_for_close(conn);
   3390        break;
   3391    }
   3392  } SMARTLIST_FOREACH_END(conn);
   3393 }
   3394 
   3395 /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
   3396 * otherwise.
   3397 * Right now this just checks if it's an internal IP address or an
   3398 * internal connection. We also should, but don't, check if the connection
   3399 * uses pluggable transports, since we should then limit it even if it
   3400 * comes from an internal IP address. */
   3401 static int
   3402 connection_is_rate_limited(const connection_t *conn)
   3403 {
   3404  const or_options_t *options = get_options();
   3405  if (conn->linked)
   3406    return 0; /* Internal connection */
   3407  else if (! options->CountPrivateBandwidth &&
   3408           ! conn->always_rate_limit_as_remote &&
   3409           (tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
   3410            tor_addr_family(&conn->addr) == AF_UNIX ||   /* no address */
   3411            tor_addr_is_internal(&conn->addr, 0)))
   3412    return 0; /* Internal address */
   3413  else
   3414    return 1;
   3415 }
   3416 
   3417 /** When was either global write bucket last empty? If this was recent, then
   3418 * we're probably low on bandwidth, and we should be stingy with our bandwidth
   3419 * usage. */
   3420 static time_t write_buckets_last_empty_at = -100;
   3421 
   3422 /** How many seconds of no active local circuits will make the
   3423 * connection revert to the "relayed" bandwidth class? */
   3424 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
   3425 
   3426 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
   3427 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
   3428 * class 1, and directory conns that are serving data out, count.
   3429 */
   3430 static int
   3431 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
   3432 {
   3433  if (conn->type == CONN_TYPE_OR &&
   3434      connection_or_client_used(TO_OR_CONN(conn)) +
   3435                                CLIENT_IDLE_TIME_FOR_PRIORITY < now)
   3436    return 1;
   3437  if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
   3438    return 1;
   3439  return 0;
   3440 }
   3441 
   3442 /** Helper function to decide how many bytes out of <b>global_bucket</b>
   3443 * we're willing to use for this transaction. <b>base</b> is the size
   3444 * of a cell on the network; <b>priority</b> says whether we should
   3445 * write many of them or just a few; and <b>conn_bucket</b> (if
   3446 * non-negative) provides an upper limit for our answer. */
   3447 static ssize_t
   3448 connection_bucket_get_share(int base, int priority,
   3449                            ssize_t global_bucket_val, ssize_t conn_bucket)
   3450 {
   3451  ssize_t at_most;
   3452  ssize_t num_bytes_high = (priority ? 32 : 16) * base;
   3453  ssize_t num_bytes_low = (priority ? 4 : 2) * base;
   3454 
   3455  /* Do a rudimentary limiting so one circuit can't hog a connection.
   3456   * Pick at most 32 cells, at least 4 cells if possible, and if we're in
   3457   * the middle pick 1/8 of the available bandwidth. */
   3458  at_most = global_bucket_val / 8;
   3459  at_most -= (at_most % base); /* round down */
   3460  if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
   3461    at_most = num_bytes_high;
   3462  else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
   3463    at_most = num_bytes_low;
   3464 
   3465  if (at_most > global_bucket_val)
   3466    at_most = global_bucket_val;
   3467 
   3468  if (conn_bucket >= 0 && at_most > conn_bucket)
   3469    at_most = conn_bucket;
   3470 
   3471  if (at_most < 0)
   3472    return 0;
   3473  return at_most;
   3474 }
   3475 
   3476 /** How many bytes at most can we read onto this connection? */
   3477 static ssize_t
   3478 connection_bucket_read_limit(connection_t *conn, time_t now)
   3479 {
   3480  int base = RELAY_PAYLOAD_SIZE_MAX;
   3481  int priority = conn->type != CONN_TYPE_DIR;
   3482  ssize_t conn_bucket = -1;
   3483  size_t global_bucket_val = token_bucket_rw_get_read(&global_bucket);
   3484  if (global_bucket_val == 0) {
   3485    /* We reached our global read limit: count this as an overload.
   3486     *
   3487     * The token bucket is always initialized (see connection_bucket_init() and
   3488     * options_validate_relay_bandwidth()) and hence we can assume that if the
   3489     * token ever hits zero, it's a limit that got popped and not the bucket
   3490     * being uninitialized.
   3491     */
   3492    rep_hist_note_overload(OVERLOAD_READ);
   3493  }
   3494 
   3495  if (connection_speaks_cells(conn)) {
   3496    or_connection_t *or_conn = TO_OR_CONN(conn);
   3497    if (conn->state == OR_CONN_STATE_OPEN)
   3498      conn_bucket = token_bucket_rw_get_read(&or_conn->bucket);
   3499    base = get_cell_network_size(or_conn->wide_circ_ids);
   3500  }
   3501 
   3502  /* Edge connection have their own read bucket due to flow control being able
   3503   * to set a rate limit for them. However, for exit connections, we still need
   3504   * to honor the global bucket as well. */
   3505  if (CONN_IS_EDGE(conn)) {
   3506    const edge_connection_t *edge_conn = CONST_TO_EDGE_CONN(conn);
   3507    conn_bucket = token_bucket_rw_get_read(&edge_conn->bucket);
   3508    if (conn->type == CONN_TYPE_EXIT) {
   3509      /* Decide between our limit and the global one. */
   3510      goto end;
   3511    }
   3512    return conn_bucket;
   3513  }
   3514 
   3515  if (!connection_is_rate_limited(conn)) {
   3516    /* be willing to read on local conns even if our buckets are empty */
   3517    return conn_bucket>=0 ? conn_bucket : 1<<14;
   3518  }
   3519 
   3520  if (connection_counts_as_relayed_traffic(conn, now)) {
   3521    size_t relayed = token_bucket_rw_get_read(&global_relayed_bucket);
   3522    global_bucket_val = MIN(global_bucket_val, relayed);
   3523  }
   3524 
   3525 end:
   3526  return connection_bucket_get_share(base, priority,
   3527                                     global_bucket_val, conn_bucket);
   3528 }
   3529 
   3530 /** How many bytes at most can we write onto this connection? */
   3531 ssize_t
   3532 connection_bucket_write_limit(connection_t *conn, time_t now)
   3533 {
   3534  int base = RELAY_PAYLOAD_SIZE_MAX;
   3535  int priority = conn->type != CONN_TYPE_DIR;
   3536  size_t conn_bucket = buf_datalen(conn->outbuf);
   3537  size_t global_bucket_val = token_bucket_rw_get_write(&global_bucket);
   3538  if (global_bucket_val == 0) {
   3539    /* We reached our global write limit: We should count this as an overload.
   3540     * See above function for more information */
   3541    rep_hist_note_overload(OVERLOAD_WRITE);
   3542  }
   3543 
   3544  if (!connection_is_rate_limited(conn)) {
   3545    /* be willing to write to local conns even if our buckets are empty */
   3546    return conn_bucket;
   3547  }
   3548 
   3549  if (connection_speaks_cells(conn)) {
   3550    /* use the per-conn write limit if it's lower */
   3551    or_connection_t *or_conn = TO_OR_CONN(conn);
   3552    if (conn->state == OR_CONN_STATE_OPEN)
   3553      conn_bucket = MIN(conn_bucket,
   3554                        token_bucket_rw_get_write(&or_conn->bucket));
   3555    base = get_cell_network_size(or_conn->wide_circ_ids);
   3556  }
   3557 
   3558  if (connection_counts_as_relayed_traffic(conn, now)) {
   3559    size_t relayed = token_bucket_rw_get_write(&global_relayed_bucket);
   3560    global_bucket_val = MIN(global_bucket_val, relayed);
   3561  }
   3562 
   3563  return connection_bucket_get_share(base, priority,
   3564                                     global_bucket_val, conn_bucket);
   3565 }
   3566 
   3567 /** Return true iff the global write buckets are low enough that we
   3568 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
   3569 * out to <b>conn</b>.
   3570 *
   3571 * If we are a directory authority, always answer dir requests thus true is
   3572 * always returned.
   3573 *
   3574 * Note: There are a lot of parameters we could use here:
   3575 * - global_relayed_write_bucket. Low is bad.
   3576 * - global_write_bucket. Low is bad.
   3577 * - bandwidthrate. Low is bad.
   3578 * - bandwidthburst. Not a big factor?
   3579 * - attempt. High is bad.
   3580 * - total bytes queued on outbufs. High is bad. But I'm wary of
   3581 *   using this, since a few slow-flushing queues will pump up the
   3582 *   number without meaning what we meant to mean. What we really
   3583 *   mean is "total directory bytes added to outbufs recently", but
   3584 *   that's harder to quantify and harder to keep track of.
   3585 */
   3586 bool
   3587 connection_dir_is_global_write_low(const connection_t *conn, size_t attempt)
   3588 {
   3589  size_t smaller_bucket =
   3590    MIN(token_bucket_rw_get_write(&global_bucket),
   3591        token_bucket_rw_get_write(&global_relayed_bucket));
   3592 
   3593  /* Special case for authorities (directory only). */
   3594  if (authdir_mode_v3(get_options())) {
   3595    /* Are we configured to possibly reject requests under load? */
   3596    if (!dirauth_should_reject_requests_under_load()) {
   3597      /* Answer request no matter what. */
   3598      return false;
   3599    }
   3600    /* Always answer requests from a known relay which includes the other
   3601     * authorities. The following looks up the addresses for relays that we
   3602     * have their descriptor _and_ any configured trusted directories. */
   3603    if (nodelist_probably_contains_address(&conn->addr)) {
   3604      return false;
   3605    }
   3606  }
   3607 
   3608  if (!connection_is_rate_limited(conn))
   3609    return false; /* local conns don't get limited */
   3610 
   3611  if (smaller_bucket < attempt)
   3612    return true; /* not enough space. */
   3613 
   3614  {
   3615    const time_t diff = approx_time() - write_buckets_last_empty_at;
   3616    if (diff <= 1)
   3617      return true; /* we're already hitting our limits, no more please */
   3618  }
   3619  return false;
   3620 }
   3621 
   3622 /** When did we last tell the accounting subsystem about transmitted
   3623 * bandwidth? */
   3624 static time_t last_recorded_accounting_at = 0;
   3625 
   3626 /** Helper: adjusts our bandwidth history and informs the controller as
   3627 * appropriate, given that we have just read <b>num_read</b> bytes and written
   3628 * <b>num_written</b> bytes on <b>conn</b>. */
   3629 static void
   3630 record_num_bytes_transferred_impl(connection_t *conn,
   3631                             time_t now, size_t num_read, size_t num_written)
   3632 {
   3633  /* Count bytes of answering direct and tunneled directory requests */
   3634  if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
   3635    if (num_read > 0)
   3636      bwhist_note_dir_bytes_read(num_read, now);
   3637    if (num_written > 0)
   3638      bwhist_note_dir_bytes_written(num_written, now);
   3639  }
   3640 
   3641  /* Linked connections and internal IPs aren't counted for statistics or
   3642   * accounting:
   3643   *  - counting linked connections would double-count BEGINDIR bytes, because
   3644   *    they are sent as Dir bytes on the linked connection, and OR bytes on
   3645   *    the OR connection;
   3646   *  - relays and clients don't connect to internal IPs, unless specifically
   3647   *    configured to do so. If they are configured that way, we don't count
   3648   *    internal bytes.
   3649   */
   3650  if (!connection_is_rate_limited(conn))
   3651    return;
   3652 
   3653  const bool is_ipv6 = (conn->socket_family == AF_INET6);
   3654  if (conn->type == CONN_TYPE_OR)
   3655    conn_stats_note_or_conn_bytes(conn->global_identifier, num_read,
   3656                                  num_written, now, is_ipv6);
   3657 
   3658  if (num_read > 0) {
   3659    bwhist_note_bytes_read(num_read, now, is_ipv6);
   3660  }
   3661  if (num_written > 0) {
   3662    bwhist_note_bytes_written(num_written, now, is_ipv6);
   3663  }
   3664  if (conn->type == CONN_TYPE_EXIT)
   3665    rep_hist_note_exit_bytes(conn->port, num_written, num_read);
   3666 
   3667  /* Remember these bytes towards statistics. */
   3668  stats_increment_bytes_read_and_written(num_read, num_written);
   3669 
   3670  /* Remember these bytes towards accounting. */
   3671  if (accounting_is_enabled(get_options())) {
   3672    if (now > last_recorded_accounting_at && last_recorded_accounting_at) {
   3673      accounting_add_bytes(num_read, num_written,
   3674                           (int)(now - last_recorded_accounting_at));
   3675    } else {
   3676      accounting_add_bytes(num_read, num_written, 0);
   3677    }
   3678    last_recorded_accounting_at = now;
   3679  }
   3680 }
   3681 
   3682 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
   3683 * onto <b>conn</b>. Decrement buckets appropriately. */
   3684 static void
   3685 connection_buckets_decrement(connection_t *conn, time_t now,
   3686                             size_t num_read, size_t num_written)
   3687 {
   3688  if (num_written >= INT_MAX || num_read >= INT_MAX) {
   3689    log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
   3690             "connection type=%s, state=%s",
   3691             (unsigned long)num_read, (unsigned long)num_written,
   3692             conn_type_to_string(conn->type),
   3693             conn_state_to_string(conn->type, conn->state));
   3694    tor_assert_nonfatal_unreached();
   3695    if (num_written >= INT_MAX)
   3696      num_written = 1;
   3697    if (num_read >= INT_MAX)
   3698      num_read = 1;
   3699  }
   3700 
   3701  record_num_bytes_transferred_impl(conn, now, num_read, num_written);
   3702 
   3703  /* Edge connection need to decrement the read side of the bucket used by our
   3704   * congestion control. */
   3705  if (CONN_IS_EDGE(conn) && num_read > 0) {
   3706    edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
   3707    token_bucket_rw_dec(&edge_conn->bucket, num_read, 0);
   3708  }
   3709 
   3710  if (!connection_is_rate_limited(conn))
   3711    return; /* local IPs are free */
   3712 
   3713  unsigned flags = 0;
   3714  if (connection_counts_as_relayed_traffic(conn, now)) {
   3715    flags = token_bucket_rw_dec(&global_relayed_bucket, num_read, num_written);
   3716  }
   3717  flags |= token_bucket_rw_dec(&global_bucket, num_read, num_written);
   3718 
   3719  if (flags & TB_WRITE) {
   3720    write_buckets_last_empty_at = now;
   3721  }
   3722  if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
   3723    or_connection_t *or_conn = TO_OR_CONN(conn);
   3724    token_bucket_rw_dec(&or_conn->bucket, num_read, num_written);
   3725  }
   3726 }
   3727 
   3728 /**
   3729 * Mark <b>conn</b> as needing to stop reading because bandwidth has been
   3730 * exhausted.  If <b>is_global_bw</b>, it is closing because global bandwidth
   3731 * limit has been exhausted.  Otherwise, it is closing because its own
   3732 * bandwidth limit has been exhausted.
   3733 */
   3734 void
   3735 connection_read_bw_exhausted(connection_t *conn, bool is_global_bw)
   3736 {
   3737  (void)is_global_bw;
   3738  // Double-calls to stop-reading are correlated with stalling for
   3739  // ssh uploads. Might as well prevent this from happening,
   3740  // especially the read_blocked_on_bw flag. That was clearly getting
   3741  // set when it should not be, during an already-blocked XOFF
   3742  // condition.
   3743  if (!CONN_IS_EDGE(conn) || !TO_EDGE_CONN(conn)->xoff_received) {
   3744    conn->read_blocked_on_bw = 1;
   3745    connection_stop_reading(conn);
   3746    reenable_blocked_connection_schedule();
   3747  }
   3748 }
   3749 
   3750 /**
   3751 * Mark <b>conn</b> as needing to stop reading because write bandwidth has
   3752 * been exhausted.  If <b>is_global_bw</b>, it is closing because global
   3753 * bandwidth limit has been exhausted.  Otherwise, it is closing because its
   3754 * own bandwidth limit has been exhausted.
   3755 */
   3756 void
   3757 connection_write_bw_exhausted(connection_t *conn, bool is_global_bw)
   3758 {
   3759  (void)is_global_bw;
   3760  conn->write_blocked_on_bw = 1;
   3761  connection_stop_writing(conn);
   3762  reenable_blocked_connection_schedule();
   3763 }
   3764 
   3765 /** If we have exhausted our global buckets, or the buckets for conn,
   3766 * stop reading. */
   3767 void
   3768 connection_consider_empty_read_buckets(connection_t *conn)
   3769 {
   3770  int is_global = 1;
   3771  const char *reason;
   3772 
   3773  if (CONN_IS_EDGE(conn) &&
   3774             token_bucket_rw_get_read(&TO_EDGE_CONN(conn)->bucket) <= 0) {
   3775    reason = "edge connection read bucket exhausted. Pausing.";
   3776    is_global = false;
   3777  } else if (!connection_is_rate_limited(conn)) {
   3778    return; /* Always okay. */
   3779  } else if (token_bucket_rw_get_read(&global_bucket) <= 0) {
   3780    reason = "global read bucket exhausted. Pausing.";
   3781  } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
   3782             token_bucket_rw_get_read(&global_relayed_bucket) <= 0) {
   3783    reason = "global relayed read bucket exhausted. Pausing.";
   3784  } else if (connection_speaks_cells(conn) &&
   3785             conn->state == OR_CONN_STATE_OPEN &&
   3786             token_bucket_rw_get_read(&TO_OR_CONN(conn)->bucket) <= 0) {
   3787    reason = "connection read bucket exhausted. Pausing.";
   3788    is_global = false;
   3789  } else {
   3790    return; /* all good, no need to stop it */
   3791  }
   3792 
   3793  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
   3794  connection_read_bw_exhausted(conn, is_global);
   3795 }
   3796 
   3797 /** If we have exhausted our global buckets, or the buckets for conn,
   3798 * stop writing. */
   3799 void
   3800 connection_consider_empty_write_buckets(connection_t *conn)
   3801 {
   3802  const char *reason;
   3803 
   3804  if (!connection_is_rate_limited(conn))
   3805    return; /* Always okay. */
   3806 
   3807  bool is_global = true;
   3808  if (token_bucket_rw_get_write(&global_bucket) <= 0) {
   3809    reason = "global write bucket exhausted. Pausing.";
   3810  } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
   3811             token_bucket_rw_get_write(&global_relayed_bucket) <= 0) {
   3812    reason = "global relayed write bucket exhausted. Pausing.";
   3813  } else if (connection_speaks_cells(conn) &&
   3814             conn->state == OR_CONN_STATE_OPEN &&
   3815             token_bucket_rw_get_write(&TO_OR_CONN(conn)->bucket) <= 0) {
   3816    reason = "connection write bucket exhausted. Pausing.";
   3817    is_global = false;
   3818  } else
   3819    return; /* all good, no need to stop it */
   3820 
   3821  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
   3822  connection_write_bw_exhausted(conn, is_global);
   3823 }
   3824 
   3825 /** Initialize the global buckets to the values configured in the
   3826 * options */
   3827 void
   3828 connection_bucket_init(void)
   3829 {
   3830  const or_options_t *options = get_options();
   3831  const uint32_t now_ts = monotime_coarse_get_stamp();
   3832  token_bucket_rw_init(&global_bucket,
   3833                    (int32_t)options->BandwidthRate,
   3834                    (int32_t)options->BandwidthBurst,
   3835                    now_ts);
   3836  if (options->RelayBandwidthRate) {
   3837    token_bucket_rw_init(&global_relayed_bucket,
   3838                      (int32_t)options->RelayBandwidthRate,
   3839                      (int32_t)options->RelayBandwidthBurst,
   3840                      now_ts);
   3841  } else {
   3842    token_bucket_rw_init(&global_relayed_bucket,
   3843                      (int32_t)options->BandwidthRate,
   3844                      (int32_t)options->BandwidthBurst,
   3845                      now_ts);
   3846  }
   3847 
   3848  reenable_blocked_connection_init(options);
   3849 }
   3850 
   3851 /** Update the global connection bucket settings to a new value. */
   3852 void
   3853 connection_bucket_adjust(const or_options_t *options)
   3854 {
   3855  token_bucket_rw_adjust(&global_bucket,
   3856                      (int32_t)options->BandwidthRate,
   3857                      (int32_t)options->BandwidthBurst);
   3858  if (options->RelayBandwidthRate) {
   3859    token_bucket_rw_adjust(&global_relayed_bucket,
   3860                        (int32_t)options->RelayBandwidthRate,
   3861                        (int32_t)options->RelayBandwidthBurst);
   3862  } else {
   3863    token_bucket_rw_adjust(&global_relayed_bucket,
   3864                        (int32_t)options->BandwidthRate,
   3865                        (int32_t)options->BandwidthBurst);
   3866  }
   3867 }
   3868 
   3869 /**
   3870 * Cached value of the last coarse-timestamp when we refilled the
   3871 * global buckets.
   3872 */
   3873 static uint32_t last_refilled_global_buckets_ts=0;
   3874 /**
   3875 * Refill the token buckets for a single connection <b>conn</b>, and the
   3876 * global token buckets as appropriate.  Requires that <b>now_ts</b> is
   3877 * the time in coarse timestamp units.
   3878 */
   3879 static void
   3880 connection_bucket_refill_single(connection_t *conn, uint32_t now_ts)
   3881 {
   3882  /* Note that we only check for equality here: the underlying
   3883   * token bucket functions can handle moving backwards in time if they
   3884   * need to. */
   3885  if (now_ts != last_refilled_global_buckets_ts) {
   3886    token_bucket_rw_refill(&global_bucket, now_ts);
   3887    token_bucket_rw_refill(&global_relayed_bucket, now_ts);
   3888    last_refilled_global_buckets_ts = now_ts;
   3889  }
   3890 
   3891  if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
   3892    or_connection_t *or_conn = TO_OR_CONN(conn);
   3893    token_bucket_rw_refill(&or_conn->bucket, now_ts);
   3894  }
   3895 
   3896  if (CONN_IS_EDGE(conn)) {
   3897    token_bucket_rw_refill(&TO_EDGE_CONN(conn)->bucket, now_ts);
   3898  }
   3899 }
   3900 
   3901 /**
   3902 * Event to re-enable all connections that were previously blocked on read or
   3903 * write.
   3904 */
   3905 static mainloop_event_t *reenable_blocked_connections_ev = NULL;
   3906 
   3907 /** True iff reenable_blocked_connections_ev is currently scheduled. */
   3908 static int reenable_blocked_connections_is_scheduled = 0;
   3909 
   3910 /** Delay after which to run reenable_blocked_connections_ev. */
   3911 static struct timeval reenable_blocked_connections_delay;
   3912 
   3913 /**
   3914 * Re-enable all connections that were previously blocked on read or write.
   3915 * This event is scheduled after enough time has elapsed to be sure
   3916 * that the buckets will refill when the connections have something to do.
   3917 */
   3918 static void
   3919 reenable_blocked_connections_cb(mainloop_event_t *ev, void *arg)
   3920 {
   3921  (void)ev;
   3922  (void)arg;
   3923  SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
   3924    /* For conflux, we noticed logs of connection_start_reading() called
   3925     * multiple times while we were blocked from a previous XOFF, and this
   3926     * was log was correlated with stalls during ssh uploads. So we added
   3927     * this additional check, to avoid connection_start_reading() without
   3928     * getting an XON. The most important piece is always allowing
   3929     * the read_blocked_on_bw to get cleared, either way. */
   3930    if (conn->read_blocked_on_bw == 1 &&
   3931        (!CONN_IS_EDGE(conn) || !TO_EDGE_CONN(conn)->xoff_received)) {
   3932      connection_start_reading(conn);
   3933    }
   3934    conn->read_blocked_on_bw = 0;
   3935    if (conn->write_blocked_on_bw == 1) {
   3936      connection_start_writing(conn);
   3937      conn->write_blocked_on_bw = 0;
   3938    }
   3939  } SMARTLIST_FOREACH_END(conn);
   3940 
   3941  reenable_blocked_connections_is_scheduled = 0;
   3942 }
   3943 
   3944 /**
   3945 * Initialize the mainloop event that we use to wake up connections that
   3946 * find themselves blocked on bandwidth.
   3947 */
   3948 static void
   3949 reenable_blocked_connection_init(const or_options_t *options)
   3950 {
   3951  if (! reenable_blocked_connections_ev) {
   3952    reenable_blocked_connections_ev =
   3953      mainloop_event_new(reenable_blocked_connections_cb, NULL);
   3954    reenable_blocked_connections_is_scheduled = 0;
   3955  }
   3956  time_t sec = options->TokenBucketRefillInterval / 1000;
   3957  int msec = (options->TokenBucketRefillInterval % 1000);
   3958  reenable_blocked_connections_delay.tv_sec = sec;
   3959  reenable_blocked_connections_delay.tv_usec = msec * 1000;
   3960 }
   3961 
   3962 /**
   3963 * Called when we have blocked a connection for being low on bandwidth:
   3964 * schedule an event to reenable such connections, if it is not already
   3965 * scheduled.
   3966 */
   3967 static void
   3968 reenable_blocked_connection_schedule(void)
   3969 {
   3970  if (reenable_blocked_connections_is_scheduled)
   3971    return;
   3972  if (BUG(reenable_blocked_connections_ev == NULL)) {
   3973    reenable_blocked_connection_init(get_options());
   3974  }
   3975  mainloop_event_schedule(reenable_blocked_connections_ev,
   3976                          &reenable_blocked_connections_delay);
   3977  reenable_blocked_connections_is_scheduled = 1;
   3978 }
   3979 
   3980 /** Read bytes from conn-\>s and process them.
   3981 *
   3982 * It calls connection_buf_read_from_socket() to bring in any new bytes,
   3983 * and then calls connection_process_inbuf() to process them.
   3984 *
   3985 * Mark the connection and return -1 if you want to close it, else
   3986 * return 0.
   3987 */
   3988 static int
   3989 connection_handle_read_impl(connection_t *conn)
   3990 {
   3991  ssize_t max_to_read=-1, try_to_read;
   3992  size_t before, n_read = 0;
   3993  int socket_error = 0;
   3994 
   3995  if (conn->marked_for_close)
   3996    return 0; /* do nothing */
   3997 
   3998  conn->timestamp_last_read_allowed = approx_time();
   3999 
   4000  connection_bucket_refill_single(conn, monotime_coarse_get_stamp());
   4001 
   4002  switch (conn->type) {
   4003    case CONN_TYPE_OR_LISTENER:
   4004      return connection_handle_listener_read(conn, CONN_TYPE_OR);
   4005    case CONN_TYPE_EXT_OR_LISTENER:
   4006      return connection_handle_listener_read(conn, CONN_TYPE_EXT_OR);
   4007    case CONN_TYPE_AP_LISTENER:
   4008    case CONN_TYPE_AP_TRANS_LISTENER:
   4009    case CONN_TYPE_AP_NATD_LISTENER:
   4010    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER:
   4011      return connection_handle_listener_read(conn, CONN_TYPE_AP);
   4012    case CONN_TYPE_DIR_LISTENER:
   4013      return connection_handle_listener_read(conn, CONN_TYPE_DIR);
   4014    case CONN_TYPE_CONTROL_LISTENER:
   4015      return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
   4016    case CONN_TYPE_METRICS_LISTENER:
   4017      return connection_handle_listener_read(conn, CONN_TYPE_METRICS);
   4018    case CONN_TYPE_AP_DNS_LISTENER:
   4019      /* This should never happen; eventdns.c handles the reads here. */
   4020      tor_fragile_assert();
   4021      return 0;
   4022  }
   4023 
   4024 loop_again:
   4025  try_to_read = max_to_read;
   4026  tor_assert(!conn->marked_for_close);
   4027 
   4028  before = buf_datalen(conn->inbuf);
   4029  if (connection_buf_read_from_socket(conn, &max_to_read, &socket_error) < 0) {
   4030    /* There's a read error; kill the connection.*/
   4031    if (conn->type == CONN_TYPE_OR) {
   4032      connection_or_notify_error(TO_OR_CONN(conn),
   4033                                 socket_error != 0 ?
   4034                                   errno_to_orconn_end_reason(socket_error) :
   4035                                   END_OR_CONN_REASON_CONNRESET,
   4036                                 socket_error != 0 ?
   4037                                   tor_socket_strerror(socket_error) :
   4038                                   "(unknown, errno was 0)");
   4039    }
   4040    if (CONN_IS_EDGE(conn)) {
   4041      edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
   4042      connection_edge_end_errno(edge_conn);
   4043      if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
   4044        /* broken, don't send a socks reply back */
   4045        TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
   4046      }
   4047    }
   4048    connection_close_immediate(conn); /* Don't flush; connection is dead. */
   4049    /*
   4050     * This can bypass normal channel checking since we did
   4051     * connection_or_notify_error() above.
   4052     */
   4053    connection_mark_for_close_internal(conn);
   4054    return -1;
   4055  }
   4056  n_read += buf_datalen(conn->inbuf) - before;
   4057  if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
   4058    /* instruct it not to try to package partial cells. */
   4059    if (connection_process_inbuf(conn, 0) < 0) {
   4060      return -1;
   4061    }
   4062    if (!conn->marked_for_close &&
   4063        connection_is_reading(conn) &&
   4064        !conn->inbuf_reached_eof &&
   4065        max_to_read > 0)
   4066      goto loop_again; /* try reading again, in case more is here now */
   4067  }
   4068  /* one last try, packaging partial cells and all. */
   4069  if (!conn->marked_for_close &&
   4070      connection_process_inbuf(conn, 1) < 0) {
   4071    return -1;
   4072  }
   4073  if (conn->linked_conn) {
   4074    /* The other side's handle_write() will never actually get called, so
   4075     * we need to invoke the appropriate callbacks ourself. */
   4076    connection_t *linked = conn->linked_conn;
   4077 
   4078    if (n_read) {
   4079      /* Probably a no-op, since linked conns typically don't count for
   4080       * bandwidth rate limiting. But do it anyway so we can keep stats
   4081       * accurately. Note that since we read the bytes from conn, and
   4082       * we're writing the bytes onto the linked connection, we count
   4083       * these as <i>written</i> bytes. */
   4084      connection_buckets_decrement(linked, approx_time(), 0, n_read);
   4085 
   4086      if (connection_flushed_some(linked) < 0)
   4087        connection_mark_for_close(linked);
   4088      if (!connection_wants_to_flush(linked))
   4089        connection_finished_flushing(linked);
   4090    }
   4091 
   4092    if (!buf_datalen(linked->outbuf) && conn->active_on_link)
   4093      connection_stop_reading_from_linked_conn(conn);
   4094  }
   4095  /* If we hit the EOF, call connection_reached_eof(). */
   4096  if (!conn->marked_for_close &&
   4097      conn->inbuf_reached_eof &&
   4098      connection_reached_eof(conn) < 0) {
   4099    return -1;
   4100  }
   4101  return 0;
   4102 }
   4103 
   4104 /* DOCDOC connection_handle_read */
   4105 int
   4106 connection_handle_read(connection_t *conn)
   4107 {
   4108  int res;
   4109  update_current_time(time(NULL));
   4110  res = connection_handle_read_impl(conn);
   4111  return res;
   4112 }
   4113 
   4114 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
   4115 * either directly or via TLS. Reduce the token buckets by the number of bytes
   4116 * read.
   4117 *
   4118 * If *max_to_read is -1, then decide it ourselves, else go with the
   4119 * value passed to us. When returning, if it's changed, subtract the
   4120 * number of bytes we read from *max_to_read.
   4121 *
   4122 * Return -1 if we want to break conn, else return 0.
   4123 */
   4124 static int
   4125 connection_buf_read_from_socket(connection_t *conn, ssize_t *max_to_read,
   4126                       int *socket_error)
   4127 {
   4128  int result;
   4129  ssize_t at_most = *max_to_read;
   4130  size_t slack_in_buf, more_to_read;
   4131  size_t n_read = 0, n_written = 0;
   4132 
   4133  if (at_most == -1) { /* we need to initialize it */
   4134    /* how many bytes are we allowed to read? */
   4135    at_most = connection_bucket_read_limit(conn, approx_time());
   4136  }
   4137 
   4138  /* Do not allow inbuf to grow past BUF_MAX_LEN. */
   4139  const ssize_t maximum = BUF_MAX_LEN - buf_datalen(conn->inbuf);
   4140  if (at_most > maximum) {
   4141    at_most = maximum;
   4142  }
   4143 
   4144  slack_in_buf = buf_slack(conn->inbuf);
   4145 again:
   4146  if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
   4147    more_to_read = at_most - slack_in_buf;
   4148    at_most = slack_in_buf;
   4149  } else {
   4150    more_to_read = 0;
   4151  }
   4152 
   4153  if (connection_speaks_cells(conn) &&
   4154      conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
   4155    int pending;
   4156    or_connection_t *or_conn = TO_OR_CONN(conn);
   4157    size_t initial_size;
   4158    if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING) {
   4159      /* continue handshaking even if global token bucket is empty */
   4160      return connection_tls_continue_handshake(or_conn);
   4161    }
   4162 
   4163    log_debug(LD_NET,
   4164              "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
   4165              " at_most %ld.",
   4166              (int)conn->s,(long)buf_datalen(conn->inbuf),
   4167              tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
   4168 
   4169    initial_size = buf_datalen(conn->inbuf);
   4170    /* else open, or closing */
   4171    result = buf_read_from_tls(conn->inbuf, or_conn->tls, at_most);
   4172    if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
   4173      or_conn->tls_error = result;
   4174    else
   4175      or_conn->tls_error = 0;
   4176 
   4177    switch (result) {
   4178      case TOR_TLS_CLOSE:
   4179      case TOR_TLS_ERROR_IO:
   4180        log_debug(LD_NET,"TLS %s closed %son read. Closing.",
   4181                  connection_describe(conn),
   4182                  result == TOR_TLS_CLOSE ? "cleanly " : "");
   4183        return result;
   4184      CASE_TOR_TLS_ERROR_ANY_NONIO:
   4185        log_debug(LD_NET,"tls error [%s] from %s. Breaking.",
   4186                 tor_tls_err_to_string(result),
   4187                  connection_describe(conn));
   4188        return result;
   4189      case TOR_TLS_WANTWRITE:
   4190        connection_start_writing(conn);
   4191        return 0;
   4192      case TOR_TLS_WANTREAD:
   4193        if (conn->in_connection_handle_write) {
   4194          /* We've been invoked from connection_handle_write, because we're
   4195           * waiting for a TLS renegotiation, the renegotiation started, and
   4196           * SSL_read returned WANTWRITE.  But now SSL_read is saying WANTREAD
   4197           * again.  Stop waiting for write events now, or else we'll
   4198           * busy-loop until data arrives for us to read.
   4199           * XXX: remove this when v2 handshakes support is dropped. */
   4200          // XXXX Try to make sense of what is going on here.
   4201          connection_stop_writing(conn);
   4202          if (!connection_is_reading(conn))
   4203            connection_start_reading(conn);
   4204        }
   4205        /* we're already reading, one hopes */
   4206        break;
   4207      case TOR_TLS_DONE: /* no data read, so nothing to process */
   4208        break; /* so we call bucket_decrement below */
   4209      default:
   4210        break;
   4211    }
   4212    pending = tor_tls_get_pending_bytes(or_conn->tls);
   4213    if (pending) {
   4214      /* If we have any pending bytes, we read them now.  This *can*
   4215       * take us over our read allotment, but really we shouldn't be
   4216       * believing that SSL bytes are the same as TCP bytes anyway. */
   4217      int r2 = buf_read_from_tls(conn->inbuf, or_conn->tls, pending);
   4218      if (BUG(r2<0)) {
   4219        log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
   4220        return -1;
   4221      }
   4222    }
   4223    result = (int)(buf_datalen(conn->inbuf)-initial_size);
   4224    tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
   4225    log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
   4226              result, (long)n_read, (long)n_written);
   4227  } else if (conn->linked) {
   4228    if (conn->linked_conn) {
   4229      result = (int) buf_move_all(conn->inbuf, conn->linked_conn->outbuf);
   4230    } else {
   4231      result = 0;
   4232    }
   4233    //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
   4234    /* If the other side has disappeared, or if it's been marked for close and
   4235     * we flushed its outbuf, then we should set our inbuf_reached_eof. */
   4236    if (!conn->linked_conn ||
   4237        (conn->linked_conn->marked_for_close &&
   4238         buf_datalen(conn->linked_conn->outbuf) == 0))
   4239      conn->inbuf_reached_eof = 1;
   4240 
   4241    n_read = (size_t) result;
   4242  } else {
   4243    /* !connection_speaks_cells, !conn->linked_conn. */
   4244    int reached_eof = 0;
   4245    CONN_LOG_PROTECT(conn,
   4246                     result = buf_read_from_socket(conn->inbuf, conn->s,
   4247                                                   at_most,
   4248                                                   &reached_eof,
   4249                                                   socket_error));
   4250    if (reached_eof)
   4251      conn->inbuf_reached_eof = 1;
   4252 
   4253 //  log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
   4254 
   4255    if (result < 0)
   4256      return -1;
   4257    n_read = (size_t) result;
   4258  }
   4259 
   4260  if (n_read > 0) {
   4261     /* change *max_to_read */
   4262    *max_to_read = at_most - n_read;
   4263 
   4264    /* Onion service application connection. Note read bytes for metrics. */
   4265    if (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->hs_ident) {
   4266      edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
   4267      hs_metrics_app_read_bytes(&edge_conn->hs_ident->identity_pk,
   4268                                edge_conn->hs_ident->orig_virtual_port,
   4269                                n_read);
   4270    }
   4271 
   4272    /* Update edge_conn->n_read */
   4273    if (conn->type == CONN_TYPE_AP) {
   4274      edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
   4275 
   4276      /* Check for overflow: */
   4277      if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
   4278        edge_conn->n_read += (int)n_read;
   4279      else
   4280        edge_conn->n_read = UINT32_MAX;
   4281    }
   4282 
   4283    /* If CONN_BW events are enabled, update conn->n_read_conn_bw for
   4284     * OR/DIR/EXIT connections, checking for overflow. */
   4285    if (get_options()->TestingEnableConnBwEvent &&
   4286       (conn->type == CONN_TYPE_OR ||
   4287        conn->type == CONN_TYPE_DIR ||
   4288        conn->type == CONN_TYPE_EXIT)) {
   4289      if (PREDICT_LIKELY(UINT32_MAX - conn->n_read_conn_bw > n_read))
   4290        conn->n_read_conn_bw += (int)n_read;
   4291      else
   4292        conn->n_read_conn_bw = UINT32_MAX;
   4293    }
   4294  }
   4295 
   4296  connection_buckets_decrement(conn, approx_time(), n_read, n_written);
   4297 
   4298  if (more_to_read && result == at_most) {
   4299    slack_in_buf = buf_slack(conn->inbuf);
   4300    at_most = more_to_read;
   4301    goto again;
   4302  }
   4303 
   4304  /* Call even if result is 0, since the global read bucket may
   4305   * have reached 0 on a different conn, and this connection needs to
   4306   * know to stop reading. */
   4307  connection_consider_empty_read_buckets(conn);
   4308  if (n_written > 0 && connection_is_writing(conn))
   4309    connection_consider_empty_write_buckets(conn);
   4310 
   4311  return 0;
   4312 }
   4313 
   4314 /** A pass-through to fetch_from_buf. */
   4315 int
   4316 connection_buf_get_bytes(char *string, size_t len, connection_t *conn)
   4317 {
   4318  return buf_get_bytes(conn->inbuf, string, len);
   4319 }
   4320 
   4321 /** As buf_get_line(), but read from a connection's input buffer. */
   4322 int
   4323 connection_buf_get_line(connection_t *conn, char *data,
   4324                               size_t *data_len)
   4325 {
   4326  return buf_get_line(conn->inbuf, data, data_len);
   4327 }
   4328 
   4329 /** As fetch_from_buf_http, but fetches from a connection's input buffer_t as
   4330 * appropriate. */
   4331 int
   4332 connection_fetch_from_buf_http(connection_t *conn,
   4333                               char **headers_out, size_t max_headerlen,
   4334                               char **body_out, size_t *body_used,
   4335                               size_t max_bodylen, int force_complete)
   4336 {
   4337  return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
   4338                             body_out, body_used, max_bodylen, force_complete);
   4339 }
   4340 
   4341 /** Return true if this connection has data to flush. */
   4342 int
   4343 connection_wants_to_flush(connection_t *conn)
   4344 {
   4345  return connection_get_outbuf_len(conn) > 0;
   4346 }
   4347 
   4348 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
   4349 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
   4350 * connection_edge_consider_sending_sendme().
   4351 */
   4352 int
   4353 connection_outbuf_too_full(connection_t *conn)
   4354 {
   4355  return connection_get_outbuf_len(conn) > 10*CELL_PAYLOAD_SIZE;
   4356 }
   4357 
   4358 /**
   4359 * On Windows Vista and Windows 7, tune the send buffer size according to a
   4360 * hint from the OS.
   4361 *
   4362 * This should help fix slow upload rates.
   4363 */
   4364 static void
   4365 update_send_buffer_size(tor_socket_t sock)
   4366 {
   4367 #ifdef _WIN32
   4368  /* We only do this on Vista and 7, because earlier versions of Windows
   4369   * don't have the SIO_IDEAL_SEND_BACKLOG_QUERY functionality, and on
   4370   * later versions it isn't necessary. */
   4371  static int isVistaOr7 = -1;
   4372  if (isVistaOr7 == -1) {
   4373    isVistaOr7 = 0;
   4374    OSVERSIONINFO osvi = { 0 };
   4375    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
   4376    GetVersionEx(&osvi);
   4377    if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion < 2)
   4378      isVistaOr7 = 1;
   4379  }
   4380  if (!isVistaOr7)
   4381    return;
   4382  if (get_options()->ConstrainedSockets)
   4383    return;
   4384  ULONG isb = 0;
   4385  DWORD bytesReturned = 0;
   4386  if (!WSAIoctl(sock, SIO_IDEAL_SEND_BACKLOG_QUERY, NULL, 0,
   4387      &isb, sizeof(isb), &bytesReturned, NULL, NULL)) {
   4388    setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&isb, sizeof(isb));
   4389  }
   4390 #else /* !defined(_WIN32) */
   4391  (void) sock;
   4392 #endif /* defined(_WIN32) */
   4393 }
   4394 
   4395 /** Try to flush more bytes onto <b>conn</b>-\>s.
   4396 *
   4397 * This function is called in connection_handle_write(), which gets
   4398 * called from conn_write_callback() in main.c when libevent tells us
   4399 * that <b>conn</b> wants to write.
   4400 *
   4401 * Update <b>conn</b>-\>timestamp_last_write_allowed to now, and call flush_buf
   4402 * or flush_buf_tls appropriately. If it succeeds and there are no more
   4403 * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
   4404 * on it too.
   4405 *
   4406 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
   4407 * limits.  (Used for flushing messages to controller connections on fatal
   4408 * errors.)
   4409 *
   4410 * Mark the connection and return -1 if you want to close it, else
   4411 * return 0.
   4412 */
   4413 static int
   4414 connection_handle_write_impl(connection_t *conn, int force)
   4415 {
   4416  int e;
   4417  socklen_t len=(socklen_t)sizeof(e);
   4418  int result;
   4419  ssize_t max_to_write;
   4420  time_t now = approx_time();
   4421  size_t n_read = 0, n_written = 0;
   4422  int dont_stop_writing = 0;
   4423 
   4424  tor_assert(!connection_is_listener(conn));
   4425 
   4426  if (conn->marked_for_close || !SOCKET_OK(conn->s))
   4427    return 0; /* do nothing */
   4428 
   4429  if (conn->in_flushed_some) {
   4430    log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
   4431    return 0;
   4432  }
   4433 
   4434  conn->timestamp_last_write_allowed = now;
   4435 
   4436  connection_bucket_refill_single(conn, monotime_coarse_get_stamp());
   4437 
   4438  /* Sometimes, "writable" means "connected". */
   4439  if (connection_state_is_connecting(conn)) {
   4440    if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
   4441      log_warn(LD_BUG, "getsockopt() syscall failed");
   4442      if (conn->type == CONN_TYPE_OR) {
   4443        or_connection_t *orconn = TO_OR_CONN(conn);
   4444        connection_or_close_for_error(orconn, 0);
   4445      } else {
   4446        if (CONN_IS_EDGE(conn)) {
   4447          connection_edge_end_errno(TO_EDGE_CONN(conn));
   4448        }
   4449        connection_mark_for_close(conn);
   4450      }
   4451      return -1;
   4452    }
   4453    if (e) {
   4454      /* some sort of error, but maybe just inprogress still */
   4455      if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
   4456        log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
   4457                 tor_socket_strerror(e));
   4458        if (CONN_IS_EDGE(conn))
   4459          connection_edge_end_errno(TO_EDGE_CONN(conn));
   4460        if (conn->type == CONN_TYPE_OR)
   4461          connection_or_notify_error(TO_OR_CONN(conn),
   4462                                     errno_to_orconn_end_reason(e),
   4463                                     tor_socket_strerror(e));
   4464 
   4465        connection_close_immediate(conn);
   4466        /*
   4467         * This can bypass normal channel checking since we did
   4468         * connection_or_notify_error() above.
   4469         */
   4470        connection_mark_for_close_internal(conn);
   4471        return -1;
   4472      } else {
   4473        return 0; /* no change, see if next time is better */
   4474      }
   4475    }
   4476    /* The connection is successful. */
   4477    if (connection_finished_connecting(conn)<0)
   4478      return -1;
   4479  }
   4480 
   4481  max_to_write = force ? (ssize_t)buf_datalen(conn->outbuf)
   4482    : connection_bucket_write_limit(conn, now);
   4483 
   4484  if (connection_speaks_cells(conn) &&
   4485      conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
   4486    or_connection_t *or_conn = TO_OR_CONN(conn);
   4487    size_t initial_size;
   4488    if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING) {
   4489      connection_stop_writing(conn);
   4490      if (connection_tls_continue_handshake(or_conn) < 0) {
   4491        /* Don't flush; connection is dead. */
   4492        connection_or_notify_error(or_conn,
   4493                                   END_OR_CONN_REASON_MISC,
   4494                                   "TLS error in connection_tls_"
   4495                                   "continue_handshake()");
   4496        connection_close_immediate(conn);
   4497        /*
   4498         * This can bypass normal channel checking since we did
   4499         * connection_or_notify_error() above.
   4500         */
   4501        connection_mark_for_close_internal(conn);
   4502        return -1;
   4503      }
   4504      return 0;
   4505    } else if (conn->state == OR_CONN_STATE_SERVER_VERSIONS_WAIT) {
   4506      return connection_handle_read(conn);
   4507    }
   4508 
   4509    /* else open, or closing */
   4510    initial_size = buf_datalen(conn->outbuf);
   4511    result = buf_flush_to_tls(conn->outbuf, or_conn->tls,
   4512                              max_to_write);
   4513 
   4514    if (result >= 0)
   4515      update_send_buffer_size(conn->s);
   4516 
   4517    /* If we just flushed the last bytes, tell the channel on the
   4518     * or_conn to check if it needs to geoip_change_dirreq_state() */
   4519    /* XXXX move this to flushed_some or finished_flushing -NM */
   4520    if (buf_datalen(conn->outbuf) == 0 && or_conn->chan)
   4521      channel_notify_flushed(TLS_CHAN_TO_BASE(or_conn->chan));
   4522 
   4523    switch (result) {
   4524      CASE_TOR_TLS_ERROR_ANY:
   4525      case TOR_TLS_CLOSE:
   4526        or_conn->tls_error = result;
   4527        log_info(LD_NET, result != TOR_TLS_CLOSE ?
   4528                 "tls error. breaking.":"TLS connection closed on flush");
   4529        /* Don't flush; connection is dead. */
   4530        connection_or_notify_error(or_conn,
   4531                                   END_OR_CONN_REASON_MISC,
   4532                                   result != TOR_TLS_CLOSE ?
   4533                                     "TLS error in during flush" :
   4534                                     "TLS closed during flush");
   4535        connection_close_immediate(conn);
   4536        /*
   4537         * This can bypass normal channel checking since we did
   4538         * connection_or_notify_error() above.
   4539         */
   4540        connection_mark_for_close_internal(conn);
   4541        return -1;
   4542      case TOR_TLS_WANTWRITE:
   4543        log_debug(LD_NET,"wanted write.");
   4544        /* we're already writing */
   4545        dont_stop_writing = 1;
   4546        break;
   4547      case TOR_TLS_WANTREAD:
   4548        /* Make sure to avoid a loop if the receive buckets are empty. */
   4549        log_debug(LD_NET,"wanted read.");
   4550        if (!connection_is_reading(conn)) {
   4551          connection_write_bw_exhausted(conn, true);
   4552          /* we'll start reading again when we get more tokens in our
   4553           * read bucket; then we'll start writing again too.
   4554           */
   4555        }
   4556        /* else no problem, we're already reading */
   4557        return 0;
   4558      /* case TOR_TLS_DONE:
   4559       * for TOR_TLS_DONE, fall through to check if the flushlen
   4560       * is empty, so we can stop writing.
   4561       */
   4562    }
   4563 
   4564    tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
   4565    log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
   4566              result, (long)n_read, (long)n_written);
   4567    or_conn->bytes_xmitted += result;
   4568    or_conn->bytes_xmitted_by_tls += n_written;
   4569    /* So we notice bytes were written even on error */
   4570    /* XXXX This cast is safe since we can never write INT_MAX bytes in a
   4571     * single set of TLS operations. But it looks kinda ugly. If we refactor
   4572     * the *_buf_tls functions, we should make them return ssize_t or size_t
   4573     * or something. */
   4574    result = (int)(initial_size-buf_datalen(conn->outbuf));
   4575  } else {
   4576    CONN_LOG_PROTECT(conn,
   4577                     result = buf_flush_to_socket(conn->outbuf, conn->s,
   4578                                                  max_to_write));
   4579    if (result < 0) {
   4580      if (CONN_IS_EDGE(conn))
   4581        connection_edge_end_errno(TO_EDGE_CONN(conn));
   4582      if (conn->type == CONN_TYPE_AP) {
   4583        /* writing failed; we couldn't send a SOCKS reply if we wanted to */
   4584        TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
   4585      }
   4586 
   4587      connection_close_immediate(conn); /* Don't flush; connection is dead. */
   4588      connection_mark_for_close(conn);
   4589      return -1;
   4590    }
   4591    update_send_buffer_size(conn->s);
   4592    n_written = (size_t) result;
   4593  }
   4594 
   4595  if (n_written && conn->type == CONN_TYPE_AP) {
   4596    edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
   4597 
   4598    /* Check for overflow: */
   4599    if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
   4600      edge_conn->n_written += (int)n_written;
   4601    else
   4602      edge_conn->n_written = UINT32_MAX;
   4603  }
   4604 
   4605  /* If CONN_BW events are enabled, update conn->n_written_conn_bw for
   4606   * OR/DIR/EXIT connections, checking for overflow. */
   4607  if (n_written && get_options()->TestingEnableConnBwEvent &&
   4608     (conn->type == CONN_TYPE_OR ||
   4609      conn->type == CONN_TYPE_DIR ||
   4610      conn->type == CONN_TYPE_EXIT)) {
   4611    if (PREDICT_LIKELY(UINT32_MAX - conn->n_written_conn_bw > n_written))
   4612      conn->n_written_conn_bw += (int)n_written;
   4613    else
   4614      conn->n_written_conn_bw = UINT32_MAX;
   4615  }
   4616 
   4617  connection_buckets_decrement(conn, approx_time(), n_read, n_written);
   4618 
   4619  if (result > 0) {
   4620    /* If we wrote any bytes from our buffer, then call the appropriate
   4621     * functions. */
   4622    if (connection_flushed_some(conn) < 0) {
   4623      if (connection_speaks_cells(conn)) {
   4624        connection_or_notify_error(TO_OR_CONN(conn),
   4625                                   END_OR_CONN_REASON_MISC,
   4626                                   "Got error back from "
   4627                                   "connection_flushed_some()");
   4628      }
   4629 
   4630      /*
   4631       * This can bypass normal channel checking since we did
   4632       * connection_or_notify_error() above.
   4633       */
   4634      connection_mark_for_close_internal(conn);
   4635    }
   4636  }
   4637 
   4638  if (!connection_wants_to_flush(conn) &&
   4639      !dont_stop_writing) { /* it's done flushing */
   4640    if (connection_finished_flushing(conn) < 0) {
   4641      /* already marked */
   4642      goto err;
   4643    }
   4644    goto done;
   4645  }
   4646 
   4647  /* Call even if result is 0, since the global write bucket may
   4648   * have reached 0 on a different conn, and this connection needs to
   4649   * know to stop writing. */
   4650  connection_consider_empty_write_buckets(conn);
   4651  if (n_read > 0 && connection_is_reading(conn))
   4652    connection_consider_empty_read_buckets(conn);
   4653 
   4654 done:
   4655  /* If this is an edge connection with congestion control, check to see
   4656   * if it is time to send an xon */
   4657  if (conn_uses_flow_control(conn)) {
   4658    flow_control_decide_xon(TO_EDGE_CONN(conn), n_written);
   4659  }
   4660 
   4661  return 0;
   4662 
   4663 err:
   4664  return -1;
   4665 }
   4666 
   4667 /* DOCDOC connection_handle_write */
   4668 int
   4669 connection_handle_write(connection_t *conn, int force)
   4670 {
   4671    int res;
   4672    update_current_time(time(NULL));
   4673    /* connection_handle_write_impl() might call connection_handle_read()
   4674     * if we're in the middle of a v2 handshake, in which case it needs this
   4675     * flag set. */
   4676    conn->in_connection_handle_write = 1;
   4677    res = connection_handle_write_impl(conn, force);
   4678    conn->in_connection_handle_write = 0;
   4679    return res;
   4680 }
   4681 
   4682 /**
   4683 * Try to flush data that's waiting for a write on <b>conn</b>.  Return
   4684 * -1 on failure, 0 on success.
   4685 *
   4686 * Don't use this function for regular writing; the buffers
   4687 * system should be good enough at scheduling writes there.  Instead, this
   4688 * function is for cases when we're about to exit or something and we want
   4689 * to report it right away.
   4690 */
   4691 int
   4692 connection_flush(connection_t *conn)
   4693 {
   4694  return connection_handle_write(conn, 1);
   4695 }
   4696 
   4697 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
   4698 *
   4699 * Return true iff it is okay to queue bytes on <b>conn</b>'s outbuf for
   4700 * writing.
   4701 */
   4702 static int
   4703 connection_may_write_to_buf(connection_t *conn)
   4704 {
   4705  /* if it's marked for close, only allow write if we mean to flush it */
   4706  if (conn->marked_for_close && !conn->hold_open_until_flushed)
   4707    return 0;
   4708 
   4709  return 1;
   4710 }
   4711 
   4712 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
   4713 *
   4714 * Called when an attempt to add bytes on <b>conn</b>'s outbuf has failed;
   4715 * mark the connection and warn as appropriate.
   4716 */
   4717 static void
   4718 connection_write_to_buf_failed(connection_t *conn)
   4719 {
   4720  if (CONN_IS_EDGE(conn)) {
   4721    /* if it failed, it means we have our package/delivery windows set
   4722       wrong compared to our max outbuf size. close the whole circuit. */
   4723    log_warn(LD_NET,
   4724             "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
   4725    circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
   4726                           END_CIRC_REASON_INTERNAL);
   4727  } else if (conn->type == CONN_TYPE_OR) {
   4728    or_connection_t *orconn = TO_OR_CONN(conn);
   4729    log_warn(LD_NET,
   4730             "write_to_buf failed on an orconn; notifying of error "
   4731             "(fd %d)", (int)(conn->s));
   4732    connection_or_close_for_error(orconn, 0);
   4733  } else {
   4734    log_warn(LD_NET,
   4735             "write_to_buf failed. Closing connection (fd %d).",
   4736             (int)conn->s);
   4737    connection_mark_for_close(conn);
   4738  }
   4739 }
   4740 
   4741 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
   4742 *
   4743 * Called when an attempt to add bytes on <b>conn</b>'s outbuf has succeeded:
   4744 * start writing if appropriate.
   4745 */
   4746 static void
   4747 connection_write_to_buf_commit(connection_t *conn)
   4748 {
   4749  /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
   4750   * state, we don't want to try to write it right away, since
   4751   * conn->write_event won't be set yet.  Otherwise, write data from
   4752   * this conn as the socket is available. */
   4753  if (conn->write_event) {
   4754    connection_start_writing(conn);
   4755  }
   4756 }
   4757 
   4758 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
   4759 * outbuf, and ask it to start writing.
   4760 *
   4761 * If <b>zlib</b> is nonzero, this is a directory connection that should get
   4762 * its contents compressed or decompressed as they're written.  If zlib is
   4763 * negative, this is the last data to be compressed, and the connection's zlib
   4764 * state should be flushed.
   4765 */
   4766 MOCK_IMPL(void,
   4767 connection_write_to_buf_impl_,(const char *string, size_t len,
   4768                               connection_t *conn, int zlib))
   4769 {
   4770  /* XXXX This function really needs to return -1 on failure. */
   4771  int r;
   4772  if (!len && !(zlib<0))
   4773    return;
   4774 
   4775  if (!connection_may_write_to_buf(conn))
   4776    return;
   4777 
   4778  if (zlib) {
   4779    dir_connection_t *dir_conn = TO_DIR_CONN(conn);
   4780    int done = zlib < 0;
   4781    CONN_LOG_PROTECT(conn, r = buf_add_compress(conn->outbuf,
   4782                                                dir_conn->compress_state,
   4783                                                string, len, done));
   4784  } else {
   4785    CONN_LOG_PROTECT(conn, r = buf_add(conn->outbuf, string, len));
   4786  }
   4787  if (r < 0) {
   4788    connection_write_to_buf_failed(conn);
   4789    return;
   4790  }
   4791  connection_write_to_buf_commit(conn);
   4792 }
   4793 
   4794 /**
   4795 * Write a <b>string</b> (of size <b>len</b> to directory connection
   4796 * <b>dir_conn</b>. Apply compression if connection is configured to use
   4797 * it and finalize it if <b>done</b> is true.
   4798 */
   4799 void
   4800 connection_dir_buf_add(const char *string, size_t len,
   4801                       dir_connection_t *dir_conn, int done)
   4802 {
   4803  if (dir_conn->compress_state != NULL) {
   4804    connection_buf_add_compress(string, len, dir_conn, done);
   4805    return;
   4806  }
   4807 
   4808  connection_buf_add(string, len, TO_CONN(dir_conn));
   4809 }
   4810 
   4811 void
   4812 connection_buf_add_compress(const char *string, size_t len,
   4813                            dir_connection_t *conn, int done)
   4814 {
   4815  connection_write_to_buf_impl_(string, len, TO_CONN(conn), done ? -1 : 1);
   4816 }
   4817 
   4818 /**
   4819 * Add all bytes from <b>buf</b> to <b>conn</b>'s outbuf, draining them
   4820 * from <b>buf</b>. (If the connection is marked and will soon be closed,
   4821 * nothing is drained.)
   4822 */
   4823 void
   4824 connection_buf_add_buf(connection_t *conn, buf_t *buf)
   4825 {
   4826  tor_assert(conn);
   4827  tor_assert(buf);
   4828  size_t len = buf_datalen(buf);
   4829  if (len == 0)
   4830    return;
   4831 
   4832  if (!connection_may_write_to_buf(conn))
   4833    return;
   4834 
   4835  buf_move_all(conn->outbuf, buf);
   4836  connection_write_to_buf_commit(conn);
   4837 }
   4838 
   4839 #define CONN_GET_ALL_TEMPLATE(var, test) \
   4840  STMT_BEGIN \
   4841    smartlist_t *conns = get_connection_array();   \
   4842    smartlist_t *ret_conns = smartlist_new();     \
   4843    SMARTLIST_FOREACH_BEGIN(conns, connection_t *, var) { \
   4844      if (var && (test) && !var->marked_for_close) \
   4845        smartlist_add(ret_conns, var); \
   4846    } SMARTLIST_FOREACH_END(var);                                            \
   4847    return ret_conns; \
   4848  STMT_END
   4849 
   4850 /* Return a list of connections that aren't close and matches the given type
   4851 * and state. The returned list can be empty and must be freed using
   4852 * smartlist_free(). The caller does NOT have ownership of the objects in the
   4853 * list so it must not free them nor reference them as they can disappear. */
   4854 smartlist_t *
   4855 connection_list_by_type_state(int type, int state)
   4856 {
   4857  CONN_GET_ALL_TEMPLATE(conn, (conn->type == type && conn->state == state));
   4858 }
   4859 
   4860 /* Return a list of connections that aren't close and matches the given type
   4861 * and purpose. The returned list can be empty and must be freed using
   4862 * smartlist_free(). The caller does NOT have ownership of the objects in the
   4863 * list so it must not free them nor reference them as they can disappear. */
   4864 smartlist_t *
   4865 connection_list_by_type_purpose(int type, int purpose)
   4866 {
   4867  CONN_GET_ALL_TEMPLATE(conn,
   4868                        (conn->type == type && conn->purpose == purpose));
   4869 }
   4870 
   4871 /** Return a connection_t * from get_connection_array() that satisfies test on
   4872 * var, and that is not marked for close. */
   4873 #define CONN_GET_TEMPLATE(var, test)               \
   4874  STMT_BEGIN                                       \
   4875    smartlist_t *conns = get_connection_array();   \
   4876    SMARTLIST_FOREACH(conns, connection_t *, var,  \
   4877    {                                              \
   4878      if (var && (test) && !var->marked_for_close) \
   4879        return var;                                \
   4880    });                                            \
   4881    return NULL;                                   \
   4882  STMT_END
   4883 
   4884 /** Return a connection with given type, address, port, and purpose;
   4885 * or NULL if no such connection exists (or if all such connections are marked
   4886 * for close). */
   4887 MOCK_IMPL(connection_t *,
   4888 connection_get_by_type_addr_port_purpose,(int type,
   4889                                         const tor_addr_t *addr, uint16_t port,
   4890                                         int purpose))
   4891 {
   4892  CONN_GET_TEMPLATE(conn,
   4893       (conn->type == type &&
   4894        tor_addr_eq(&conn->addr, addr) &&
   4895        conn->port == port &&
   4896        conn->purpose == purpose));
   4897 }
   4898 
   4899 /** Return the stream with id <b>id</b> if it is not already marked for
   4900 * close.
   4901 */
   4902 connection_t *
   4903 connection_get_by_global_id(uint64_t id)
   4904 {
   4905  CONN_GET_TEMPLATE(conn, conn->global_identifier == id);
   4906 }
   4907 
   4908 /** Return a connection of type <b>type</b> that is not marked for close.
   4909 */
   4910 connection_t *
   4911 connection_get_by_type(int type)
   4912 {
   4913  CONN_GET_TEMPLATE(conn, conn->type == type);
   4914 }
   4915 
   4916 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
   4917 * and that is not marked for close.
   4918 */
   4919 connection_t *
   4920 connection_get_by_type_state(int type, int state)
   4921 {
   4922  CONN_GET_TEMPLATE(conn, conn->type == type && conn->state == state);
   4923 }
   4924 
   4925 /**
   4926 * Return a connection of type <b>type</b> that is not an internally linked
   4927 * connection, and is not marked for close.
   4928 **/
   4929 MOCK_IMPL(connection_t *,
   4930 connection_get_by_type_nonlinked,(int type))
   4931 {
   4932  CONN_GET_TEMPLATE(conn, conn->type == type && !conn->linked);
   4933 }
   4934 
   4935 /** Return a new smartlist of dir_connection_t * from get_connection_array()
   4936 * that satisfy conn_test on connection_t *conn_var, and dirconn_test on
   4937 * dir_connection_t *dirconn_var. conn_var must be of CONN_TYPE_DIR and not
   4938 * marked for close to be included in the list. */
   4939 #define DIR_CONN_LIST_TEMPLATE(conn_var, conn_test,             \
   4940                               dirconn_var, dirconn_test)       \
   4941  STMT_BEGIN                                                    \
   4942    smartlist_t *conns = get_connection_array();                \
   4943    smartlist_t *dir_conns = smartlist_new();                   \
   4944    SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn_var) {  \
   4945      if (conn_var && (conn_test)                               \
   4946          && conn_var->type == CONN_TYPE_DIR                    \
   4947          && !conn_var->marked_for_close) {                     \
   4948        dir_connection_t *dirconn_var = TO_DIR_CONN(conn_var);  \
   4949        if (dirconn_var && (dirconn_test)) {                    \
   4950          smartlist_add(dir_conns, dirconn_var);                \
   4951        }                                                       \
   4952      }                                                         \
   4953    } SMARTLIST_FOREACH_END(conn_var);                          \
   4954    return dir_conns;                                           \
   4955  STMT_END
   4956 
   4957 /** Return a list of directory connections that are fetching the item
   4958 * described by <b>purpose</b>/<b>resource</b>. If there are none,
   4959 * return an empty list. This list must be freed using smartlist_free,
   4960 * but the pointers in it must not be freed.
   4961 * Note that this list should not be cached, as the pointers in it can be
   4962 * freed if their connections close. */
   4963 smartlist_t *
   4964 connection_dir_list_by_purpose_and_resource(
   4965                                            int purpose,
   4966                                            const char *resource)
   4967 {
   4968  DIR_CONN_LIST_TEMPLATE(conn,
   4969                         conn->purpose == purpose,
   4970                         dirconn,
   4971                         0 == strcmp_opt(resource,
   4972                                         dirconn->requested_resource));
   4973 }
   4974 
   4975 /** Return a list of directory connections that are fetching the item
   4976 * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. If there are
   4977 * none, return an empty list. This list must be freed using smartlist_free,
   4978 * but the pointers in it must not be freed.
   4979 * Note that this list should not be cached, as the pointers in it can be
   4980 * freed if their connections close. */
   4981 smartlist_t *
   4982 connection_dir_list_by_purpose_resource_and_state(
   4983                                                  int purpose,
   4984                                                  const char *resource,
   4985                                                  int state)
   4986 {
   4987  DIR_CONN_LIST_TEMPLATE(conn,
   4988                         conn->purpose == purpose && conn->state == state,
   4989                         dirconn,
   4990                         0 == strcmp_opt(resource,
   4991                                         dirconn->requested_resource));
   4992 }
   4993 
   4994 #undef DIR_CONN_LIST_TEMPLATE
   4995 
   4996 /** Return an arbitrary active OR connection that isn't <b>this_conn</b>.
   4997 *
   4998 * We use this to guess if we should tell the controller that we
   4999 * didn't manage to connect to any of our bridges. */
   5000 static connection_t *
   5001 connection_get_another_active_or_conn(const or_connection_t *this_conn)
   5002 {
   5003  CONN_GET_TEMPLATE(conn,
   5004                    conn != TO_CONN(this_conn) && conn->type == CONN_TYPE_OR);
   5005 }
   5006 
   5007 /** Return 1 if there are any active OR connections apart from
   5008 * <b>this_conn</b>.
   5009 *
   5010 * We use this to guess if we should tell the controller that we
   5011 * didn't manage to connect to any of our bridges. */
   5012 int
   5013 any_other_active_or_conns(const or_connection_t *this_conn)
   5014 {
   5015  connection_t *conn = connection_get_another_active_or_conn(this_conn);
   5016  if (conn != NULL) {
   5017    log_debug(LD_DIR, "%s: Found an OR connection: %s",
   5018              __func__, connection_describe(conn));
   5019    return 1;
   5020  }
   5021 
   5022  return 0;
   5023 }
   5024 
   5025 #undef CONN_GET_TEMPLATE
   5026 
   5027 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
   5028 int
   5029 connection_is_listener(connection_t *conn)
   5030 {
   5031  if (conn->type == CONN_TYPE_OR_LISTENER ||
   5032      conn->type == CONN_TYPE_EXT_OR_LISTENER ||
   5033      conn->type == CONN_TYPE_AP_LISTENER ||
   5034      conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
   5035      conn->type == CONN_TYPE_AP_DNS_LISTENER ||
   5036      conn->type == CONN_TYPE_AP_NATD_LISTENER ||
   5037      conn->type == CONN_TYPE_AP_HTTP_CONNECT_LISTENER ||
   5038      conn->type == CONN_TYPE_DIR_LISTENER ||
   5039      conn->type == CONN_TYPE_METRICS_LISTENER ||
   5040      conn->type == CONN_TYPE_CONTROL_LISTENER)
   5041    return 1;
   5042  return 0;
   5043 }
   5044 
   5045 /** Return 1 if <b>conn</b> is in state "open" and is not marked
   5046 * for close, else return 0.
   5047 */
   5048 int
   5049 connection_state_is_open(connection_t *conn)
   5050 {
   5051  tor_assert(conn);
   5052 
   5053  if (conn->marked_for_close)
   5054    return 0;
   5055 
   5056  if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
   5057      (conn->type == CONN_TYPE_EXT_OR) ||
   5058      (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
   5059      (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
   5060      (conn->type == CONN_TYPE_CONTROL &&
   5061       conn->state == CONTROL_CONN_STATE_OPEN))
   5062    return 1;
   5063 
   5064  return 0;
   5065 }
   5066 
   5067 /** Return 1 if conn is in 'connecting' state, else return 0. */
   5068 int
   5069 connection_state_is_connecting(connection_t *conn)
   5070 {
   5071  tor_assert(conn);
   5072 
   5073  if (conn->marked_for_close)
   5074    return 0;
   5075  switch (conn->type)
   5076    {
   5077    case CONN_TYPE_OR:
   5078      return conn->state == OR_CONN_STATE_CONNECTING;
   5079    case CONN_TYPE_EXIT:
   5080      return conn->state == EXIT_CONN_STATE_CONNECTING;
   5081    case CONN_TYPE_DIR:
   5082      return conn->state == DIR_CONN_STATE_CONNECTING;
   5083    }
   5084 
   5085  return 0;
   5086 }
   5087 
   5088 /** Allocates a base64'ed authenticator for use in http or https
   5089 * auth, based on the input string <b>authenticator</b>. Returns it
   5090 * if success, else returns NULL. */
   5091 char *
   5092 alloc_http_authenticator(const char *authenticator)
   5093 {
   5094  /* an authenticator in Basic authentication
   5095   * is just the string "username:password" */
   5096  const size_t authenticator_length = strlen(authenticator);
   5097  const size_t base64_authenticator_length =
   5098      base64_encode_size(authenticator_length, 0) + 1;
   5099  char *base64_authenticator = tor_malloc(base64_authenticator_length);
   5100  if (base64_encode(base64_authenticator, base64_authenticator_length,
   5101                    authenticator, authenticator_length, 0) < 0) {
   5102    tor_free(base64_authenticator); /* free and set to null */
   5103  }
   5104  return base64_authenticator;
   5105 }
   5106 
   5107 /** Given a socket handle, check whether the local address (sockname) of the
   5108 * socket is one that we've connected from before.  If so, double-check
   5109 * whether our address has changed and we need to generate keys.  If we do,
   5110 * call init_keys().
   5111 */
   5112 static void
   5113 client_check_address_changed(tor_socket_t sock)
   5114 {
   5115  tor_addr_t out_addr, iface_addr;
   5116  tor_addr_t **last_interface_ip_ptr;
   5117  sa_family_t family;
   5118 
   5119  if (!outgoing_addrs)
   5120    outgoing_addrs = smartlist_new();
   5121 
   5122  if (tor_addr_from_getsockname(&out_addr, sock) < 0) {
   5123    int e = tor_socket_errno(sock);
   5124    log_warn(LD_NET, "getsockname() to check for address change failed: %s",
   5125             tor_socket_strerror(e));
   5126    return;
   5127  }
   5128  family = tor_addr_family(&out_addr);
   5129 
   5130  if (family == AF_INET)
   5131    last_interface_ip_ptr = &last_interface_ipv4;
   5132  else if (family == AF_INET6)
   5133    last_interface_ip_ptr = &last_interface_ipv6;
   5134  else
   5135    return;
   5136 
   5137  if (! *last_interface_ip_ptr) {
   5138    tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
   5139    if (get_interface_address6(LOG_INFO, family, a)==0) {
   5140      *last_interface_ip_ptr = a;
   5141    } else {
   5142      tor_free(a);
   5143    }
   5144  }
   5145 
   5146  /* If we've used this address previously, we're okay. */
   5147  SMARTLIST_FOREACH(outgoing_addrs, const tor_addr_t *, a_ptr,
   5148                    if (tor_addr_eq(a_ptr, &out_addr))
   5149                      return;
   5150                    );
   5151 
   5152  /* Uh-oh.  We haven't connected from this address before. Has the interface
   5153   * address changed? */
   5154  if (get_interface_address6(LOG_INFO, family, &iface_addr)<0)
   5155    return;
   5156 
   5157  if (tor_addr_eq(&iface_addr, *last_interface_ip_ptr)) {
   5158    /* Nope, it hasn't changed.  Add this address to the list. */
   5159    smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
   5160  } else {
   5161    /* The interface changed.  We're a client, so we need to regenerate our
   5162     * keys.  First, reset the state. */
   5163    log_notice(LD_NET, "Our IP address has changed.  Rotating keys...");
   5164    tor_addr_copy(*last_interface_ip_ptr, &iface_addr);
   5165    SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t*, a_ptr, tor_free(a_ptr));
   5166    smartlist_clear(outgoing_addrs);
   5167    smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
   5168    /* We'll need to resolve ourselves again. */
   5169    resolved_addr_reset_last(AF_INET);
   5170    /* Okay, now change our keys. */
   5171    ip_address_changed(1);
   5172  }
   5173 }
   5174 
   5175 /** Some systems have limited system buffers for recv and xmit on
   5176 * sockets allocated in a virtual server or similar environment. For a Tor
   5177 * server this can produce the "Error creating network socket: No buffer
   5178 * space available" error once all available TCP buffer space is consumed.
   5179 * This method will attempt to constrain the buffers allocated for the socket
   5180 * to the desired size to stay below system TCP buffer limits.
   5181 */
   5182 static void
   5183 set_constrained_socket_buffers(tor_socket_t sock, int size)
   5184 {
   5185  void *sz = (void*)&size;
   5186  socklen_t sz_sz = (socklen_t) sizeof(size);
   5187  if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
   5188    int e = tor_socket_errno(sock);
   5189    log_warn(LD_NET, "setsockopt() to constrain send "
   5190             "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
   5191  }
   5192  if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
   5193    int e = tor_socket_errno(sock);
   5194    log_warn(LD_NET, "setsockopt() to constrain recv "
   5195             "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
   5196  }
   5197 }
   5198 
   5199 /** Process new bytes that have arrived on conn-\>inbuf.
   5200 *
   5201 * This function just passes conn to the connection-specific
   5202 * connection_*_process_inbuf() function. It also passes in
   5203 * package_partial if wanted.
   5204 */
   5205 int
   5206 connection_process_inbuf(connection_t *conn, int package_partial)
   5207 {
   5208  tor_assert(conn);
   5209 
   5210  switch (conn->type) {
   5211    case CONN_TYPE_OR:
   5212      return connection_or_process_inbuf(TO_OR_CONN(conn));
   5213    case CONN_TYPE_EXT_OR:
   5214      return connection_ext_or_process_inbuf(TO_OR_CONN(conn));
   5215    case CONN_TYPE_EXIT:
   5216    case CONN_TYPE_AP:
   5217      return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
   5218                                           package_partial);
   5219    case CONN_TYPE_DIR:
   5220      return connection_dir_process_inbuf(TO_DIR_CONN(conn));
   5221    case CONN_TYPE_CONTROL:
   5222      return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
   5223    case CONN_TYPE_METRICS:
   5224      return metrics_connection_process_inbuf(conn);
   5225    default:
   5226      log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
   5227      tor_fragile_assert();
   5228      return -1;
   5229  }
   5230 }
   5231 
   5232 /** Called whenever we've written data on a connection. */
   5233 static int
   5234 connection_flushed_some(connection_t *conn)
   5235 {
   5236  int r = 0;
   5237  tor_assert(!conn->in_flushed_some);
   5238  conn->in_flushed_some = 1;
   5239  if (conn->type == CONN_TYPE_DIR &&
   5240      conn->state == DIR_CONN_STATE_SERVER_WRITING) {
   5241    r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
   5242  } else if (conn->type == CONN_TYPE_OR) {
   5243    r = connection_or_flushed_some(TO_OR_CONN(conn));
   5244  } else if (CONN_IS_EDGE(conn)) {
   5245    r = connection_edge_flushed_some(TO_EDGE_CONN(conn));
   5246  }
   5247  conn->in_flushed_some = 0;
   5248  return r;
   5249 }
   5250 
   5251 /** We just finished flushing bytes to the appropriately low network layer,
   5252 * and there are no more bytes remaining in conn-\>outbuf or
   5253 * conn-\>tls to be flushed.
   5254 *
   5255 * This function just passes conn to the connection-specific
   5256 * connection_*_finished_flushing() function.
   5257 */
   5258 static int
   5259 connection_finished_flushing(connection_t *conn)
   5260 {
   5261  tor_assert(conn);
   5262 
   5263  /* If the connection is closed, don't try to do anything more here. */
   5264  if (CONN_IS_CLOSED(conn))
   5265    return 0;
   5266 
   5267 //  log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
   5268 
   5269  connection_stop_writing(conn);
   5270 
   5271  switch (conn->type) {
   5272    case CONN_TYPE_OR:
   5273      return connection_or_finished_flushing(TO_OR_CONN(conn));
   5274    case CONN_TYPE_EXT_OR:
   5275      return connection_ext_or_finished_flushing(TO_OR_CONN(conn));
   5276    case CONN_TYPE_AP:
   5277    case CONN_TYPE_EXIT:
   5278      return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
   5279    case CONN_TYPE_DIR:
   5280      return connection_dir_finished_flushing(TO_DIR_CONN(conn));
   5281    case CONN_TYPE_CONTROL:
   5282      return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
   5283    case CONN_TYPE_METRICS:
   5284      return metrics_connection_finished_flushing(conn);
   5285    default:
   5286      log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
   5287      tor_fragile_assert();
   5288      return -1;
   5289  }
   5290 }
   5291 
   5292 /** Called when our attempt to connect() to a server has just succeeded.
   5293 *
   5294 * This function checks if the interface address has changed (clients only),
   5295 * and then passes conn to the connection-specific
   5296 * connection_*_finished_connecting() function.
   5297 */
   5298 static int
   5299 connection_finished_connecting(connection_t *conn)
   5300 {
   5301  tor_assert(conn);
   5302 
   5303  if (!server_mode(get_options())) {
   5304    /* See whether getsockname() says our address changed.  We need to do this
   5305     * now that the connection has finished, because getsockname() on Windows
   5306     * won't work until then. */
   5307    client_check_address_changed(conn->s);
   5308  }
   5309 
   5310  switch (conn->type)
   5311    {
   5312    case CONN_TYPE_OR:
   5313      return connection_or_finished_connecting(TO_OR_CONN(conn));
   5314    case CONN_TYPE_EXIT:
   5315      return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
   5316    case CONN_TYPE_DIR:
   5317      return connection_dir_finished_connecting(TO_DIR_CONN(conn));
   5318    default:
   5319      log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
   5320      tor_fragile_assert();
   5321      return -1;
   5322  }
   5323 }
   5324 
   5325 /** Callback: invoked when a connection reaches an EOF event. */
   5326 static int
   5327 connection_reached_eof(connection_t *conn)
   5328 {
   5329  switch (conn->type) {
   5330    case CONN_TYPE_OR:
   5331    case CONN_TYPE_EXT_OR:
   5332      return connection_or_reached_eof(TO_OR_CONN(conn));
   5333    case CONN_TYPE_AP:
   5334    case CONN_TYPE_EXIT:
   5335      return connection_edge_reached_eof(TO_EDGE_CONN(conn));
   5336    case CONN_TYPE_DIR:
   5337      return connection_dir_reached_eof(TO_DIR_CONN(conn));
   5338    case CONN_TYPE_CONTROL:
   5339      return connection_control_reached_eof(TO_CONTROL_CONN(conn));
   5340    case CONN_TYPE_METRICS:
   5341      return metrics_connection_reached_eof(conn);
   5342    default:
   5343      log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
   5344      tor_fragile_assert();
   5345      return -1;
   5346  }
   5347 }
   5348 
   5349 /** Comparator for the two-orconn case in OOS victim sort */
   5350 static int
   5351 oos_victim_comparator_for_orconns(or_connection_t *a, or_connection_t *b)
   5352 {
   5353  int a_circs, b_circs;
   5354  /* Fewer circuits == higher priority for OOS kill, sort earlier */
   5355 
   5356  a_circs = connection_or_get_num_circuits(a);
   5357  b_circs = connection_or_get_num_circuits(b);
   5358 
   5359  if (a_circs < b_circs) return 1;
   5360  else if (a_circs > b_circs) return -1;
   5361  else return 0;
   5362 }
   5363 
   5364 /** Sort comparator for OOS victims; better targets sort before worse
   5365 * ones. */
   5366 static int
   5367 oos_victim_comparator(const void **a_v, const void **b_v)
   5368 {
   5369  connection_t *a = NULL, *b = NULL;
   5370 
   5371  /* Get connection pointers out */
   5372 
   5373  a = (connection_t *)(*a_v);
   5374  b = (connection_t *)(*b_v);
   5375 
   5376  tor_assert(a != NULL);
   5377  tor_assert(b != NULL);
   5378 
   5379  /*
   5380   * We always prefer orconns as victims currently; we won't even see
   5381   * these non-orconn cases, but if we do, sort them after orconns.
   5382   */
   5383  if (a->type == CONN_TYPE_OR && b->type == CONN_TYPE_OR) {
   5384    return oos_victim_comparator_for_orconns(TO_OR_CONN(a), TO_OR_CONN(b));
   5385  } else {
   5386    /*
   5387     * One isn't an orconn; if one is, it goes first.  We currently have no
   5388     * opinions about cases where neither is an orconn.
   5389     */
   5390    if (a->type == CONN_TYPE_OR) return -1;
   5391    else if (b->type == CONN_TYPE_OR) return 1;
   5392    else return 0;
   5393  }
   5394 }
   5395 
   5396 /** Pick n victim connections for the OOS handler and return them in a
   5397 * smartlist.
   5398 */
   5399 MOCK_IMPL(STATIC smartlist_t *,
   5400 pick_oos_victims, (int n))
   5401 {
   5402  smartlist_t *eligible = NULL, *victims = NULL;
   5403  smartlist_t *conns;
   5404  int conn_counts_by_type[CONN_TYPE_MAX_ + 1], i;
   5405 
   5406  /*
   5407   * Big damn assumption (someone improve this someday!):
   5408   *
   5409   * Socket exhaustion normally happens on high-volume relays, and so
   5410   * most of the connections involved are orconns.  We should pick victims
   5411   * by assembling a list of all orconns, and sorting them in order of
   5412   * how much 'damage' by some metric we'd be doing by dropping them.
   5413   *
   5414   * If we move on from orconns, we should probably think about incoming
   5415   * directory connections next, or exit connections.  Things we should
   5416   * probably never kill are controller connections and listeners.
   5417   *
   5418   * This function will count how many connections of different types
   5419   * exist and log it for purposes of gathering data on typical OOS
   5420   * situations to guide future improvements.
   5421   */
   5422 
   5423  /* First, get the connection array */
   5424  conns = get_connection_array();
   5425  /*
   5426   * Iterate it and pick out eligible connection types, and log some stats
   5427   * along the way.
   5428   */
   5429  eligible = smartlist_new();
   5430  memset(conn_counts_by_type, 0, sizeof(conn_counts_by_type));
   5431  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
   5432    /* Bump the counter */
   5433    tor_assert(c->type <= CONN_TYPE_MAX_);
   5434    ++(conn_counts_by_type[c->type]);
   5435 
   5436    /* Skip anything without a socket we can free */
   5437    if (!(SOCKET_OK(c->s))) {
   5438      continue;
   5439    }
   5440 
   5441    /* Skip anything we would count as moribund */
   5442    if (connection_is_moribund(c)) {
   5443      continue;
   5444    }
   5445 
   5446    switch (c->type) {
   5447      case CONN_TYPE_OR:
   5448        /* We've got an orconn, it's eligible to be OOSed */
   5449        smartlist_add(eligible, c);
   5450        break;
   5451      default:
   5452        /* We don't know what to do with it, ignore it */
   5453        break;
   5454    }
   5455  } SMARTLIST_FOREACH_END(c);
   5456 
   5457  /* Log some stats */
   5458  if (smartlist_len(conns) > 0) {
   5459    /* At least one counter must be non-zero */
   5460    log_info(LD_NET, "Some stats on conn types seen during OOS follow");
   5461    for (i = CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
   5462      /* Did we see any? */
   5463      if (conn_counts_by_type[i] > 0) {
   5464        log_info(LD_NET, "%s: %d conns",
   5465                 conn_type_to_string(i),
   5466                 conn_counts_by_type[i]);
   5467      }
   5468    }
   5469    log_info(LD_NET, "Done with OOS conn type stats");
   5470  }
   5471 
   5472  /* Did we find more eligible targets than we want to kill? */
   5473  if (smartlist_len(eligible) > n) {
   5474    /* Sort the list in order of target preference */
   5475    smartlist_sort(eligible, oos_victim_comparator);
   5476    /* Pick first n as victims */
   5477    victims = smartlist_new();
   5478    for (i = 0; i < n; ++i) {
   5479      smartlist_add(victims, smartlist_get(eligible, i));
   5480    }
   5481    /* Free the original list */
   5482    smartlist_free(eligible);
   5483  } else {
   5484    /* No, we can just call them all victims */
   5485    victims = eligible;
   5486  }
   5487 
   5488  return victims;
   5489 }
   5490 
   5491 /** Kill a list of connections for the OOS handler. */
   5492 MOCK_IMPL(STATIC void,
   5493 kill_conn_list_for_oos, (smartlist_t *conns))
   5494 {
   5495  if (!conns) return;
   5496 
   5497  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
   5498    /* Make sure the channel layer gets told about orconns */
   5499    if (c->type == CONN_TYPE_OR) {
   5500      connection_or_close_for_error(TO_OR_CONN(c), 1);
   5501    } else {
   5502      connection_mark_for_close(c);
   5503    }
   5504  } SMARTLIST_FOREACH_END(c);
   5505 
   5506  log_notice(LD_NET,
   5507             "OOS handler marked %d connections",
   5508             smartlist_len(conns));
   5509 }
   5510 
   5511 /** Check if a connection is on the way out so the OOS handler doesn't try
   5512 * to kill more than it needs. */
   5513 int
   5514 connection_is_moribund(connection_t *conn)
   5515 {
   5516  if (conn != NULL &&
   5517      (conn->conn_array_index < 0 ||
   5518       conn->marked_for_close)) {
   5519    return 1;
   5520  } else {
   5521    return 0;
   5522  }
   5523 }
   5524 
   5525 /** Out-of-Sockets handler; n_socks is the current number of open
   5526 * sockets, and failed is non-zero if a socket exhaustion related
   5527 * error immediately preceded this call.  This is where to do
   5528 * circuit-killing heuristics as needed.
   5529 */
   5530 void
   5531 connection_check_oos(int n_socks, int failed)
   5532 {
   5533  int target_n_socks = 0, moribund_socks, socks_to_kill;
   5534  smartlist_t *conns;
   5535 
   5536  /* Early exit: is OOS checking disabled? */
   5537  if (get_options()->DisableOOSCheck) {
   5538    return;
   5539  }
   5540 
   5541  /* Sanity-check args */
   5542  tor_assert(n_socks >= 0);
   5543 
   5544  /*
   5545   * Make some log noise; keep it at debug level since this gets a chance
   5546   * to run on every connection attempt.
   5547   */
   5548  log_debug(LD_NET,
   5549            "Running the OOS handler (%d open sockets, %s)",
   5550            n_socks, (failed != 0) ? "exhaustion seen" : "no exhaustion");
   5551 
   5552  /*
   5553   * Check if we're really handling an OOS condition, and if so decide how
   5554   * many sockets we want to get down to.  Be sure we check if the threshold
   5555   * is distinct from zero first; it's possible for this to be called a few
   5556   * times before we've finished reading the config.
   5557   */
   5558  if (n_socks >= get_options()->ConnLimit_high_thresh &&
   5559      get_options()->ConnLimit_high_thresh != 0 &&
   5560      get_options()->ConnLimit_ != 0) {
   5561    /* Try to get down to the low threshold */
   5562    target_n_socks = get_options()->ConnLimit_low_thresh;
   5563    log_notice(LD_NET,
   5564               "Current number of sockets %d is greater than configured "
   5565               "limit %d; OOS handler trying to get down to %d",
   5566               n_socks, get_options()->ConnLimit_high_thresh,
   5567               target_n_socks);
   5568  } else if (failed) {
   5569    /*
   5570     * If we're not at the limit but we hit a socket exhaustion error, try to
   5571     * drop some (but not as aggressively as ConnLimit_low_threshold, which is
   5572     * 3/4 of ConnLimit_)
   5573     */
   5574    target_n_socks = (n_socks * 9) / 10;
   5575    log_notice(LD_NET,
   5576               "We saw socket exhaustion at %d open sockets; OOS handler "
   5577               "trying to get down to %d",
   5578               n_socks, target_n_socks);
   5579  }
   5580 
   5581  if (target_n_socks > 0) {
   5582    /*
   5583     * It's an OOS!
   5584     *
   5585     * Count moribund sockets; it's be important that anything we decide
   5586     * to get rid of here but don't immediately close get counted as moribund
   5587     * on subsequent invocations so we don't try to kill too many things if
   5588     * connection_check_oos() gets called multiple times.
   5589     */
   5590    moribund_socks = connection_count_moribund();
   5591 
   5592    if (moribund_socks < n_socks - target_n_socks) {
   5593      socks_to_kill = n_socks - target_n_socks - moribund_socks;
   5594 
   5595      conns = pick_oos_victims(socks_to_kill);
   5596      if (conns) {
   5597        kill_conn_list_for_oos(conns);
   5598        log_notice(LD_NET,
   5599                   "OOS handler killed %d conns", smartlist_len(conns));
   5600        smartlist_free(conns);
   5601      } else {
   5602        log_notice(LD_NET, "OOS handler failed to pick any victim conns");
   5603      }
   5604    } else {
   5605      log_notice(LD_NET,
   5606                 "Not killing any sockets for OOS because there are %d "
   5607                 "already moribund, and we only want to eliminate %d",
   5608                 moribund_socks, n_socks - target_n_socks);
   5609    }
   5610  }
   5611 }
   5612 
   5613 /** Log how many bytes are used by buffers of different kinds and sizes. */
   5614 void
   5615 connection_dump_buffer_mem_stats(int severity)
   5616 {
   5617  uint64_t used_by_type[CONN_TYPE_MAX_+1];
   5618  uint64_t alloc_by_type[CONN_TYPE_MAX_+1];
   5619  int n_conns_by_type[CONN_TYPE_MAX_+1];
   5620  uint64_t total_alloc = 0;
   5621  uint64_t total_used = 0;
   5622  int i;
   5623  smartlist_t *conns = get_connection_array();
   5624 
   5625  memset(used_by_type, 0, sizeof(used_by_type));
   5626  memset(alloc_by_type, 0, sizeof(alloc_by_type));
   5627  memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
   5628 
   5629  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
   5630    int tp = c->type;
   5631    ++n_conns_by_type[tp];
   5632    if (c->inbuf) {
   5633      used_by_type[tp] += buf_datalen(c->inbuf);
   5634      alloc_by_type[tp] += buf_allocation(c->inbuf);
   5635    }
   5636    if (c->outbuf) {
   5637      used_by_type[tp] += buf_datalen(c->outbuf);
   5638      alloc_by_type[tp] += buf_allocation(c->outbuf);
   5639    }
   5640  } SMARTLIST_FOREACH_END(c);
   5641  for (i=0; i <= CONN_TYPE_MAX_; ++i) {
   5642    total_used += used_by_type[i];
   5643    total_alloc += alloc_by_type[i];
   5644  }
   5645 
   5646  tor_log(severity, LD_GENERAL,
   5647     "In buffers for %d connections: %"PRIu64" used/%"PRIu64" allocated",
   5648      smartlist_len(conns),
   5649      (total_used), (total_alloc));
   5650  for (i=CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
   5651    if (!n_conns_by_type[i])
   5652      continue;
   5653    tor_log(severity, LD_GENERAL,
   5654        "  For %d %s connections: %"PRIu64" used/%"PRIu64" allocated",
   5655        n_conns_by_type[i], conn_type_to_string(i),
   5656        (used_by_type[i]), (alloc_by_type[i]));
   5657  }
   5658 }
   5659 
   5660 /** Verify that connection <b>conn</b> has all of its invariants
   5661 * correct. Trigger an assert if anything is invalid.
   5662 */
   5663 void
   5664 assert_connection_ok(connection_t *conn, time_t now)
   5665 {
   5666  (void) now; /* XXXX unused. */
   5667  tor_assert(conn);
   5668  tor_assert(conn->type >= CONN_TYPE_MIN_);
   5669  tor_assert(conn->type <= CONN_TYPE_MAX_);
   5670 
   5671  switch (conn->type) {
   5672    case CONN_TYPE_OR:
   5673    case CONN_TYPE_EXT_OR:
   5674      tor_assert(conn->magic == OR_CONNECTION_MAGIC);
   5675      break;
   5676    case CONN_TYPE_AP:
   5677      tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
   5678      break;
   5679    case CONN_TYPE_EXIT:
   5680      tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
   5681      break;
   5682    case CONN_TYPE_DIR:
   5683      tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
   5684      break;
   5685    case CONN_TYPE_CONTROL:
   5686      tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
   5687      break;
   5688    CASE_ANY_LISTENER_TYPE:
   5689      tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
   5690      break;
   5691    default:
   5692      tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
   5693      break;
   5694  }
   5695 
   5696  if (conn->linked_conn) {
   5697    tor_assert(conn->linked_conn->linked_conn == conn);
   5698    tor_assert(conn->linked);
   5699  }
   5700  if (conn->linked)
   5701    tor_assert(!SOCKET_OK(conn->s));
   5702 
   5703  if (conn->hold_open_until_flushed)
   5704    tor_assert(conn->marked_for_close);
   5705 
   5706  /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
   5707   * marked_for_close. */
   5708 
   5709  /* buffers */
   5710  if (conn->inbuf)
   5711    buf_assert_ok(conn->inbuf);
   5712  if (conn->outbuf)
   5713    buf_assert_ok(conn->outbuf);
   5714 
   5715  if (conn->type == CONN_TYPE_OR) {
   5716    or_connection_t *or_conn = TO_OR_CONN(conn);
   5717    if (conn->state == OR_CONN_STATE_OPEN) {
   5718      /* tor_assert(conn->bandwidth > 0); */
   5719      /* the above isn't necessarily true: if we just did a TLS
   5720       * handshake but we didn't recognize the other peer, or it
   5721       * gave a bad cert/etc, then we won't have assigned bandwidth,
   5722       * yet it will be open. -RD
   5723       */
   5724 //      tor_assert(conn->read_bucket >= 0);
   5725    }
   5726 //    tor_assert(conn->addr && conn->port);
   5727    tor_assert(conn->address);
   5728    if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
   5729      tor_assert(or_conn->tls);
   5730  }
   5731 
   5732  if (CONN_IS_EDGE(conn)) {
   5733    /* XXX unchecked: package window, deliver window. */
   5734    if (conn->type == CONN_TYPE_AP) {
   5735      entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
   5736      if (entry_conn->chosen_exit_optional || entry_conn->chosen_exit_retries)
   5737        tor_assert(entry_conn->chosen_exit_name);
   5738 
   5739      tor_assert(entry_conn->socks_request);
   5740      if (conn->state == AP_CONN_STATE_OPEN) {
   5741        tor_assert(entry_conn->socks_request->has_finished);
   5742        if (!conn->marked_for_close) {
   5743          tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
   5744          cpath_assert_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
   5745        }
   5746      }
   5747    }
   5748    if (conn->type == CONN_TYPE_EXIT) {
   5749      tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
   5750                 conn->purpose == EXIT_PURPOSE_RESOLVE);
   5751    }
   5752  } else if (conn->type == CONN_TYPE_DIR) {
   5753  } else {
   5754    /* Purpose is only used for dir and exit types currently */
   5755    tor_assert(!conn->purpose);
   5756  }
   5757 
   5758  switch (conn->type)
   5759    {
   5760    CASE_ANY_LISTENER_TYPE:
   5761      tor_assert(conn->state == LISTENER_STATE_READY);
   5762      break;
   5763    case CONN_TYPE_OR:
   5764      tor_assert(conn->state >= OR_CONN_STATE_MIN_);
   5765      tor_assert(conn->state <= OR_CONN_STATE_MAX_);
   5766      break;
   5767    case CONN_TYPE_EXT_OR:
   5768      tor_assert(conn->state >= EXT_OR_CONN_STATE_MIN_);
   5769      tor_assert(conn->state <= EXT_OR_CONN_STATE_MAX_);
   5770      break;
   5771    case CONN_TYPE_EXIT:
   5772      tor_assert(conn->state >= EXIT_CONN_STATE_MIN_);
   5773      tor_assert(conn->state <= EXIT_CONN_STATE_MAX_);
   5774      tor_assert(conn->purpose >= EXIT_PURPOSE_MIN_);
   5775      tor_assert(conn->purpose <= EXIT_PURPOSE_MAX_);
   5776      break;
   5777    case CONN_TYPE_AP:
   5778      tor_assert(conn->state >= AP_CONN_STATE_MIN_);
   5779      tor_assert(conn->state <= AP_CONN_STATE_MAX_);
   5780      tor_assert(TO_ENTRY_CONN(conn)->socks_request);
   5781      break;
   5782    case CONN_TYPE_DIR:
   5783      tor_assert(conn->state >= DIR_CONN_STATE_MIN_);
   5784      tor_assert(conn->state <= DIR_CONN_STATE_MAX_);
   5785      tor_assert(conn->purpose >= DIR_PURPOSE_MIN_);
   5786      tor_assert(conn->purpose <= DIR_PURPOSE_MAX_);
   5787      break;
   5788    case CONN_TYPE_CONTROL:
   5789      tor_assert(conn->state >= CONTROL_CONN_STATE_MIN_);
   5790      tor_assert(conn->state <= CONTROL_CONN_STATE_MAX_);
   5791      break;
   5792    case CONN_TYPE_METRICS:
   5793      /* No state. */
   5794      break;
   5795    default:
   5796      tor_assert(0);
   5797  }
   5798 }
   5799 
   5800 /** Fills <b>addr</b> and <b>port</b> with the details of the global
   5801 *  proxy server we are using. Store a 1 to the int pointed to by
   5802 *  <b>is_put_out</b> if the connection is using a pluggable
   5803 *  transport; store 0 otherwise. <b>conn</b> contains the connection
   5804 *  we are using the proxy for.
   5805 *
   5806 *  Return 0 on success, -1 on failure.
   5807 */
   5808 int
   5809 get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
   5810                   int *is_pt_out, const connection_t *conn)
   5811 {
   5812  const or_options_t *options = get_options();
   5813 
   5814  *is_pt_out = 0;
   5815  /* Client Transport Plugins can use another proxy, but that should be hidden
   5816   * from the rest of tor (as the plugin is responsible for dealing with the
   5817   * proxy), check it first, then check the rest of the proxy types to allow
   5818   * the config to have unused ClientTransportPlugin entries.
   5819   */
   5820  if (options->ClientTransportPlugin) {
   5821    const transport_t *transport = NULL;
   5822    int r;
   5823    r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
   5824    if (r<0)
   5825      return -1;
   5826    if (transport) { /* transport found */
   5827      tor_addr_copy(addr, &transport->addr);
   5828      *port = transport->port;
   5829      *proxy_type = transport->socks_version;
   5830      *is_pt_out = 1;
   5831      return 0;
   5832    }
   5833 
   5834    /* Unused ClientTransportPlugin. */
   5835  }
   5836 
   5837  if (options->HTTPSProxy) {
   5838    tor_addr_copy(addr, &options->HTTPSProxyAddr);
   5839    *port = options->HTTPSProxyPort;
   5840    *proxy_type = PROXY_CONNECT;
   5841    return 0;
   5842  } else if (options->Socks4Proxy) {
   5843    tor_addr_copy(addr, &options->Socks4ProxyAddr);
   5844    *port = options->Socks4ProxyPort;
   5845    *proxy_type = PROXY_SOCKS4;
   5846    return 0;
   5847  } else if (options->Socks5Proxy) {
   5848    tor_addr_copy(addr, &options->Socks5ProxyAddr);
   5849    *port = options->Socks5ProxyPort;
   5850    *proxy_type = PROXY_SOCKS5;
   5851    return 0;
   5852  } else if (options->TCPProxy) {
   5853    tor_addr_copy(addr, &options->TCPProxyAddr);
   5854    *port = options->TCPProxyPort;
   5855    /* The only supported protocol in TCPProxy is haproxy. */
   5856    tor_assert(options->TCPProxyProtocol == TCP_PROXY_PROTOCOL_HAPROXY);
   5857    *proxy_type = PROXY_HAPROXY;
   5858    return 0;
   5859  }
   5860 
   5861  tor_addr_make_unspec(addr);
   5862  *port = 0;
   5863  *proxy_type = PROXY_NONE;
   5864  return 0;
   5865 }
   5866 
   5867 /** Log a failed connection to a proxy server.
   5868 *  <b>conn</b> is the connection we use the proxy server for. */
   5869 void
   5870 log_failed_proxy_connection(connection_t *conn)
   5871 {
   5872  tor_addr_t proxy_addr;
   5873  uint16_t proxy_port;
   5874  int proxy_type, is_pt;
   5875 
   5876  if (get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, &is_pt,
   5877                         conn) != 0)
   5878    return; /* if we have no proxy set up, leave this function. */
   5879 
   5880  (void)is_pt;
   5881  log_warn(LD_NET,
   5882           "The connection to the %s proxy server at %s just failed. "
   5883           "Make sure that the proxy server is up and running.",
   5884           proxy_type_to_string(proxy_type),
   5885           fmt_addrport(&proxy_addr, proxy_port));
   5886 }
   5887 
   5888 /** Return string representation of <b>proxy_type</b>. */
   5889 static const char *
   5890 proxy_type_to_string(int proxy_type)
   5891 {
   5892  switch (proxy_type) {
   5893  case PROXY_CONNECT:   return "HTTP";
   5894  case PROXY_SOCKS4:    return "SOCKS4";
   5895  case PROXY_SOCKS5:    return "SOCKS5";
   5896  case PROXY_HAPROXY:   return "HAPROXY";
   5897  case PROXY_PLUGGABLE: return "pluggable transports SOCKS";
   5898  case PROXY_NONE:      return "NULL";
   5899  default:              tor_assert(0);
   5900  }
   5901  return NULL; /*Unreached*/
   5902 }
   5903 
   5904 /** Call connection_free_minimal() on every connection in our array, and
   5905 * release all storage held by connection.c.
   5906 *
   5907 * Don't do the checks in connection_free(), because they will
   5908 * fail.
   5909 */
   5910 void
   5911 connection_free_all(void)
   5912 {
   5913  smartlist_t *conns = get_connection_array();
   5914 
   5915  /* We don't want to log any messages to controllers. */
   5916  SMARTLIST_FOREACH(conns, connection_t *, conn,
   5917    if (conn->type == CONN_TYPE_CONTROL)
   5918      TO_CONTROL_CONN(conn)->event_mask = 0);
   5919 
   5920  control_update_global_event_mask();
   5921 
   5922  /* Unlink everything from the identity map. */
   5923  connection_or_clear_identity_map();
   5924 
   5925  /* Clear out our list of broken connections */
   5926  clear_broken_connection_map(0);
   5927 
   5928  SMARTLIST_FOREACH(conns, connection_t *, conn,
   5929                    connection_free_minimal(conn));
   5930 
   5931  if (outgoing_addrs) {
   5932    SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr));
   5933    smartlist_free(outgoing_addrs);
   5934    outgoing_addrs = NULL;
   5935  }
   5936 
   5937  tor_free(last_interface_ipv4);
   5938  tor_free(last_interface_ipv6);
   5939  last_recorded_accounting_at = 0;
   5940 
   5941  mainloop_event_free(reenable_blocked_connections_ev);
   5942  reenable_blocked_connections_is_scheduled = 0;
   5943  memset(&reenable_blocked_connections_delay, 0, sizeof(struct timeval));
   5944 }
   5945 
   5946 /** Log a warning, and possibly emit a control event, that <b>received</b> came
   5947 * at a skewed time.  <b>trusted</b> indicates that the <b>source</b> was one
   5948 * that we had more faith in and therefore the warning level should have higher
   5949 * severity.
   5950 */
   5951 MOCK_IMPL(void,
   5952 clock_skew_warning, (const connection_t *conn, long apparent_skew, int trusted,
   5953                     log_domain_mask_t domain, const char *received,
   5954                     const char *source))
   5955 {
   5956  char dbuf[64];
   5957  char *ext_source = NULL, *warn = NULL;
   5958  format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
   5959  if (conn)
   5960    tor_asprintf(&ext_source, "%s:%s:%d", source,
   5961                 fmt_and_decorate_addr(&conn->addr), conn->port);
   5962  else
   5963    ext_source = tor_strdup(source);
   5964  log_fn(trusted ? LOG_WARN : LOG_INFO, domain,
   5965         "Received %s with skewed time (%s): "
   5966         "It seems that our clock is %s by %s, or that theirs is %s%s. "
   5967         "Tor requires an accurate clock to work: please check your time, "
   5968         "timezone, and date settings.", received, ext_source,
   5969         apparent_skew > 0 ? "ahead" : "behind", dbuf,
   5970         apparent_skew > 0 ? "behind" : "ahead",
   5971         (!conn || trusted) ? "" : ", or they are sending us the wrong time");
   5972  if (trusted) {
   5973    control_event_general_status(LOG_WARN, "CLOCK_SKEW SKEW=%ld SOURCE=%s",
   5974                                 apparent_skew, ext_source);
   5975    tor_asprintf(&warn, "Clock skew %ld in %s from %s", apparent_skew,
   5976                 received, source);
   5977    control_event_bootstrap_problem(warn, "CLOCK_SKEW", conn, 1);
   5978  }
   5979  tor_free(warn);
   5980  tor_free(ext_source);
   5981 }