main.c 99.8 KB
Newer Older
Roger Dingledine's avatar
Roger Dingledine committed
1
2
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
3
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4
 * Copyright (c) 2007-2013, The Tor Project, Inc. */
5
/* See LICENSE for licensing information */
Roger Dingledine's avatar
Roger Dingledine committed
6

Roger Dingledine's avatar
Roger Dingledine committed
7
8
/**
 * \file main.c
9
10
 * \brief Toplevel module. Handles signals, multiplexes between
 * connections, implements main loop, and drives scheduled events.
Roger Dingledine's avatar
Roger Dingledine committed
11
 **/
12

13
#define MAIN_PRIVATE
Roger Dingledine's avatar
Roger Dingledine committed
14
#include "or.h"
15
#include "addressmap.h"
Nick Mathewson's avatar
Nick Mathewson committed
16
#include "backtrace.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
17
#include "buffers.h"
18
19
#include "channel.h"
#include "channeltls.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
20
#include "circuitbuild.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
21
#include "circuitlist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
22
#include "circuituse.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
23
#include "command.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
24
#include "config.h"
25
#include "confparse.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
26
#include "connection.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
27
#include "connection_edge.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
28
#include "connection_or.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
29
#include "control.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
30
#include "cpuworker.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
31
#include "directory.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
32
#include "dirserv.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
33
#include "dirvote.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
34
#include "dns.h"
35
#include "dnsserv.h"
36
#include "entrynodes.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
37
#include "geoip.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
38
#include "hibernate.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
39
#include "main.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
40
#include "microdesc.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
41
#include "networkstatus.h"
42
#include "nodelist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
43
#include "ntmain.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
44
#include "onion.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
45
#include "policies.h"
46
#include "transports.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
47
#include "relay.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
48
#include "rendclient.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
49
#include "rendcommon.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
50
#include "rendservice.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
51
#include "rephist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
52
#include "router.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
53
#include "routerlist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
54
#include "routerparse.h"
55
#include "scheduler.h"
56
#include "statefile.h"
57
#include "status.h"
58
#include "util_process.h"
59
#include "ext_orport.h"
60
61
#ifdef USE_DMALLOC
#include <dmalloc.h>
62
#include <openssl/crypto.h>
63
#endif
64
#include "memarea.h"
65
#include "../common/sandbox.h"
66

67
68
69
70
71
72
#ifdef HAVE_EVENT2_EVENT_H
#include <event2/event.h>
#else
#include <event.h>
#endif

73
74
75
76
#ifdef USE_BUFFEREVENTS
#include <event2/bufferevent.h>
#endif

77
void evdns_shutdown(int);
Roger Dingledine's avatar
Roger Dingledine committed
78

79
/********* PROTOTYPES **********/
Roger Dingledine's avatar
Roger Dingledine committed
80

81
static void dumpmemusage(int severity);
82
static void dumpstats(int severity); /* log stats */
83
84
static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
85
static void second_elapsed_callback(periodic_timer_t *timer, void *args);
86
static int conn_close_if_marked(int i);
87
static void connection_start_reading_from_linked_conn(connection_t *conn);
88
static int connection_should_read_from_linked_conn(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
89

Roger Dingledine's avatar
Roger Dingledine committed
90
91
/********* START VARIABLES **********/

92
#ifndef USE_BUFFEREVENTS
93
int global_read_bucket; /**< Max number of bytes I can read this second. */
94
int global_write_bucket; /**< Max number of bytes I can write this second. */
Roger Dingledine's avatar
Roger Dingledine committed
95

96
97
98
99
/** Max number of relayed (bandwidth class 1) bytes I can read this second. */
int global_relayed_read_bucket;
/** Max number of relayed (bandwidth class 1) bytes I can write this second. */
int global_relayed_write_bucket;
100
/** What was the read bucket before the last second_elapsed_callback() call?
101
 * (used to determine how many bytes we've read). */
102
static int stats_prev_global_read_bucket;
103
/** What was the write bucket before the last second_elapsed_callback() call?
104
105
 * (used to determine how many bytes we've written). */
static int stats_prev_global_write_bucket;
106
107
#endif

108
/* DOCDOC stats_prev_n_read */
109
static uint64_t stats_prev_n_read = 0;
110
/* DOCDOC stats_prev_n_written */
111
112
static uint64_t stats_prev_n_written = 0;

113
/* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
114
/** How many bytes have we read since we started the process? */
115
static uint64_t stats_n_bytes_read = 0;
116
/** How many bytes have we written since we started the process? */
117
static uint64_t stats_n_bytes_written = 0;
118
/** What time did this process start up? */
119
time_t time_of_process_start = 0;
Roger Dingledine's avatar
Roger Dingledine committed
120
/** How many seconds have we been running? */
121
long stats_n_seconds_working = 0;
122
123
/** When do we next launch DNS wildcarding checks? */
static time_t time_to_check_for_correct_dns = 0;
124
125
126
127
128
129
130

/** How often will we honor SIGNEWNYM requests? */
#define MAX_SIGNEWNYM_RATE 10
/** When did we last process a SIGNEWNYM request? */
static time_t time_of_last_signewnym = 0;
/** Is there a signewnym request we're currently waiting to handle? */
static int signewnym_is_pending = 0;
131
132
/** How many times have we called newnym? */
static unsigned newnym_epoch = 0;
133

134
135
/** Smartlist of all open connections. */
static smartlist_t *connection_array = NULL;
136
137
/** List of connections that have been marked for close and need to be freed
 * and removed from connection_array. */
138
static smartlist_t *closeable_connection_lst = NULL;
139
140
/** List of linked connections that are currently reading data into their
 * inbuf from their partner's outbuf. */
141
static smartlist_t *active_linked_connection_lst = NULL;
142
143
144
/** Flag: Set to true iff we entered the current libevent main loop via
 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
 * to handle linked connections. */
145
static int called_loop_once = 0;
Roger Dingledine's avatar
Roger Dingledine committed
146

Roger Dingledine's avatar
Roger Dingledine committed
147
/** We set this to 1 when we've opened a circuit, so we can print a log
148
149
150
151
152
153
 * entry to inform the user that Tor is working.  We set it to 0 when
 * we think the fact that we once opened a circuit doesn't mean we can do so
 * any longer (a big time jump happened, when we notice our directory is
 * heinously out-of-date, etc.
 */
int can_complete_circuit=0;
154

155
156
157
158
159
160
/** How often do we check for router descriptors that we should download
 * when we have too little directory info? */
#define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
/** How often do we check for router descriptors that we should download
 * when we have enough directory info? */
#define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
161
162
163
164
/** How often do we 'forgive' undownloadable router descriptors and attempt
 * to download them again? */
#define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60)

165
166
167
168
169
170
/** Decides our behavior when no logs are configured/before any
 * logs have been configured.  For 0, we log notice to stdout as normal.
 * For 1, we log warnings only.  For 2, we log nothing.
 */
int quiet_level = 0;

Roger Dingledine's avatar
Roger Dingledine committed
171
172
/********* END VARIABLES ************/

Roger Dingledine's avatar
Roger Dingledine committed
173
174
175
/****************************************************************************
*
* This section contains accessors and other methods on the connection_array
176
* variables (which are global within this file and unavailable outside it).
Roger Dingledine's avatar
Roger Dingledine committed
177
178
179
*
****************************************************************************/

180
#if 0 && defined(USE_BUFFEREVENTS)
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
static void
free_old_inbuf(connection_t *conn)
{
  if (! conn->inbuf)
    return;

  tor_assert(conn->outbuf);
  tor_assert(buf_datalen(conn->inbuf) == 0);
  tor_assert(buf_datalen(conn->outbuf) == 0);
  buf_free(conn->inbuf);
  buf_free(conn->outbuf);
  conn->inbuf = conn->outbuf = NULL;

  if (conn->read_event) {
    event_del(conn->read_event);
    tor_event_free(conn->read_event);
  }
  if (conn->write_event) {
    event_del(conn->read_event);
    tor_event_free(conn->write_event);
  }
  conn->read_event = conn->write_event = NULL;
}
#endif

206
#if defined(_WIN32) && defined(USE_BUFFEREVENTS)
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/** Remove the kernel-space send and receive buffers for <b>s</b>. For use
 * with IOCP only. */
static int
set_buffer_lengths_to_zero(tor_socket_t s)
{
  int zero = 0;
  int r = 0;
  if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&zero, sizeof(zero))) {
    log_warn(LD_NET, "Unable to clear SO_SNDBUF");
    r = -1;
  }
  if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&zero, sizeof(zero))) {
    log_warn(LD_NET, "Unable to clear SO_RCVBUF");
    r = -1;
  }
  return r;
}
#endif

Roger Dingledine's avatar
Roger Dingledine committed
226
/** Add <b>conn</b> to the array of connections that we can poll on.  The
227
228
229
 * connection's socket must be set; the connection starts out
 * non-reading and non-writing.
 */
230
int
231
connection_add_impl(connection_t *conn, int is_connecting)
232
{
Roger Dingledine's avatar
Roger Dingledine committed
233
  tor_assert(conn);
234
  tor_assert(SOCKET_OK(conn->s) ||
235
236
             conn->linked ||
             (conn->type == CONN_TYPE_AP &&
237
              TO_EDGE_CONN(conn)->is_dns_request));
Roger Dingledine's avatar
Roger Dingledine committed
238

239
  tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
240
241
  conn->conn_array_index = smartlist_len(connection_array);
  smartlist_add(connection_array, conn);
Roger Dingledine's avatar
Roger Dingledine committed
242

243
#ifdef USE_BUFFEREVENTS
244
  if (connection_type_uses_bufferevent(conn)) {
245
    if (SOCKET_OK(conn->s) && !conn->linked) {
246

247
#ifdef _WIN32
248
249
250
251
252
253
      if (tor_libevent_using_iocp_bufferevents() &&
          get_options()->UserspaceIOCPBuffers) {
        set_buffer_lengths_to_zero(conn->s);
      }
#endif

254
      conn->bufev = bufferevent_socket_new(
255
256
257
                         tor_libevent_get_base(),
                         conn->s,
                         BEV_OPT_DEFER_CALLBACKS);
258
259
260
261
262
263
      if (!conn->bufev) {
        log_warn(LD_BUG, "Unable to create socket bufferevent");
        smartlist_del(connection_array, conn->conn_array_index);
        conn->conn_array_index = -1;
        return -1;
      }
264
265
266
267
268
269
270
271
      if (is_connecting) {
        /* Put the bufferevent into a "connecting" state so that we'll get
         * a "connected" event callback on successful write. */
        bufferevent_socket_connect(conn->bufev, NULL, 0);
      }
      connection_configure_bufferevent_callbacks(conn);
    } else if (conn->linked && conn->linked_conn &&
               connection_type_uses_bufferevent(conn->linked_conn)) {
Robert Ransom's avatar
Robert Ransom committed
272
      tor_assert(!(SOCKET_OK(conn->s)));
273
274
      if (!conn->bufev) {
        struct bufferevent *pair[2] = { NULL, NULL };
275
276
277
278
279
280
281
282
        if (bufferevent_pair_new(tor_libevent_get_base(),
                                 BEV_OPT_DEFER_CALLBACKS,
                                 pair) < 0) {
          log_warn(LD_BUG, "Unable to create bufferevent pair");
          smartlist_del(connection_array, conn->conn_array_index);
          conn->conn_array_index = -1;
          return -1;
        }
283
284
285
286
287
        tor_assert(pair[0]);
        conn->bufev = pair[0];
        conn->linked_conn->bufev = pair[1];
      } /* else the other side already was added, and got a bufferevent_pair */
      connection_configure_bufferevent_callbacks(conn);
288
289
    } else {
      tor_assert(!conn->linked);
290
291
    }

292
293
    if (conn->bufev)
      tor_assert(conn->inbuf == NULL);
294

295
296
    if (conn->linked_conn && conn->linked_conn->bufev)
      tor_assert(conn->linked_conn->inbuf == NULL);
297
  }
298
299
#else
  (void) is_connecting;
300
301
#endif

302
  if (!HAS_BUFFEREVENT(conn) && (SOCKET_OK(conn->s) || conn->linked)) {
303
304
305
306
    conn->read_event = tor_event_new(tor_libevent_get_base(),
         conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
    conn->write_event = tor_event_new(tor_libevent_get_base(),
         conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
307
    /* XXXX CHECK FOR NULL RETURN! */
308
  }
Roger Dingledine's avatar
Roger Dingledine committed
309

Roger Dingledine's avatar
Roger Dingledine committed
310
  log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
311
            conn_type_to_string(conn->type), (int)conn->s, conn->address,
312
            smartlist_len(connection_array));
Roger Dingledine's avatar
Roger Dingledine committed
313
314
315
316

  return 0;
}

317
318
319
320
321
322
/** Tell libevent that we don't care about <b>conn</b> any more. */
void
connection_unregister_events(connection_t *conn)
{
  if (conn->read_event) {
    if (event_del(conn->read_event))
323
      log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
324
325
326
327
    tor_free(conn->read_event);
  }
  if (conn->write_event) {
    if (event_del(conn->write_event))
328
      log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
329
330
    tor_free(conn->write_event);
  }
331
332
333
334
335
336
#ifdef USE_BUFFEREVENTS
  if (conn->bufev) {
    bufferevent_free(conn->bufev);
    conn->bufev = NULL;
  }
#endif
337
  if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
338
339
340
341
    dnsserv_close_listener(conn);
  }
}

Roger Dingledine's avatar
Roger Dingledine committed
342
/** Remove the connection from the global list, and remove the
Nick Mathewson's avatar
Nick Mathewson committed
343
344
345
 * corresponding poll entry.  Calling this function will shift the last
 * connection (if any) into the position occupied by conn.
 */
346
347
348
int
connection_remove(connection_t *conn)
{
Roger Dingledine's avatar
Roger Dingledine committed
349
  int current_index;
350
  connection_t *tmp;
Roger Dingledine's avatar
Roger Dingledine committed
351

Roger Dingledine's avatar
Roger Dingledine committed
352
  tor_assert(conn);
Roger Dingledine's avatar
Roger Dingledine committed
353

354
  log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
355
            (int)conn->s, conn_type_to_string(conn->type),
356
            smartlist_len(connection_array));
Roger Dingledine's avatar
Roger Dingledine committed
357

Karsten Loesing's avatar
Karsten Loesing committed
358
359
  control_event_conn_bandwidth(conn);

360
361
  tor_assert(conn->conn_array_index >= 0);
  current_index = conn->conn_array_index;
362
  connection_unregister_events(conn); /* This is redundant, but cheap. */
363
364
  if (current_index == smartlist_len(connection_array)-1) { /* at the end */
    smartlist_del(connection_array, current_index);
Roger Dingledine's avatar
Roger Dingledine committed
365
    return 0;
Roger Dingledine's avatar
Roger Dingledine committed
366
  }
Roger Dingledine's avatar
Roger Dingledine committed
367

368
  /* replace this one with the one at the end */
369
370
371
  smartlist_del(connection_array, current_index);
  tmp = smartlist_get(connection_array, current_index);
  tmp->conn_array_index = current_index;
Roger Dingledine's avatar
Roger Dingledine committed
372

Roger Dingledine's avatar
Roger Dingledine committed
373
  return 0;
Roger Dingledine's avatar
Roger Dingledine committed
374
375
}

376
/** If <b>conn</b> is an edge conn, remove it from the list
377
378
379
 * of conn's on this circuit. If it's not on an edge,
 * flush and send destroys for all circuits on this conn.
 *
380
381
 * Remove it from connection_array (if applicable) and
 * from closeable_connection_list.
382
383
384
 *
 * Then free it.
 */
385
static void
386
connection_unlink(connection_t *conn)
387
{
388
  connection_about_to_close_connection(conn);
389
  if (conn->conn_array_index >= 0) {
390
391
    connection_remove(conn);
  }
392
393
394
395
396
397
398
  if (conn->linked_conn) {
    conn->linked_conn->linked_conn = NULL;
    if (! conn->linked_conn->marked_for_close &&
        conn->linked_conn->reading_from_linked_conn)
      connection_start_reading(conn->linked_conn);
    conn->linked_conn = NULL;
  }
399
  smartlist_remove(closeable_connection_lst, conn);
400
  smartlist_remove(active_linked_connection_lst, conn);
401
  if (conn->type == CONN_TYPE_EXIT) {
402
    assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
403
  }
404
405
406
  if (conn->type == CONN_TYPE_OR) {
    if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
      connection_or_remove_from_identity_map(TO_OR_CONN(conn));
407
408
409
410
411
412
413
414
415
416
417
418
    /* connection_unlink() can only get called if the connection
     * was already on the closeable list, and it got there by
     * connection_mark_for_close(), which was called from
     * connection_or_close_normally() or
     * connection_or_close_for_error(), so the channel should
     * already be in CHANNEL_STATE_CLOSING, and then the
     * connection_about_to_close_connection() goes to
     * connection_or_about_to_close(), which calls channel_closed()
     * to notify the channel_t layer, and closed the channel, so
     * nothing more to do here to deal with the channel associated
     * with an orconn.
     */
419
  }
420
421
422
  connection_free(conn);
}

423
424
425
426
427
428
429
430
431
432
433
434
435
/** Initialize the global connection list, closeable connection list,
 * and active connection list. */
STATIC void
init_connection_lists(void)
{
  if (!connection_array)
    connection_array = smartlist_new();
  if (!closeable_connection_lst)
    closeable_connection_lst = smartlist_new();
  if (!active_linked_connection_lst)
    active_linked_connection_lst = smartlist_new();
}

436
/** Schedule <b>conn</b> to be closed. **/
437
438
439
void
add_connection_to_closeable_list(connection_t *conn)
{
Nick Mathewson's avatar
Nick Mathewson committed
440
  tor_assert(!smartlist_contains(closeable_connection_lst, conn));
441
  tor_assert(conn->marked_for_close);
442
  assert_connection_ok(conn, time(NULL));
443
444
445
  smartlist_add(closeable_connection_lst, conn);
}

446
/** Return 1 if conn is on the closeable list, else return 0. */
447
448
449
int
connection_is_on_closeable_list(connection_t *conn)
{
Nick Mathewson's avatar
Nick Mathewson committed
450
  return smartlist_contains(closeable_connection_lst, conn);
451
452
}

453
/** Return true iff conn is in the current poll array. */
454
455
456
int
connection_in_array(connection_t *conn)
{
Nick Mathewson's avatar
Nick Mathewson committed
457
  return smartlist_contains(connection_array, conn);
458
459
}

Roger Dingledine's avatar
Roger Dingledine committed
460
461
462
/** Set <b>*array</b> to an array of all connections, and <b>*n</b>
 * to the length of the array. <b>*array</b> and <b>*n</b> must not
 * be modified.
463
 */
464
465
smartlist_t *
get_connection_array(void)
466
{
467
  if (!connection_array)
468
    connection_array = smartlist_new();
469
  return connection_array;
470
471
}

472
473
/** Provides the traffic read and written over the life of the process. */

474
475
MOCK_IMPL(uint64_t,
get_bytes_read,(void))
476
477
478
479
{
  return stats_n_bytes_read;
}

480
/* DOCDOC get_bytes_written */
481
482
MOCK_IMPL(uint64_t,
get_bytes_written,(void))
483
484
485
486
{
  return stats_n_bytes_written;
}

487
/** Set the event mask on <b>conn</b> to <b>events</b>.  (The event
488
 * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
489
 */
490
void
491
connection_watch_events(connection_t *conn, watchable_events_t events)
492
{
493
494
495
  IF_HAS_BUFFEREVENT(conn, {
      short ev = ((short)events) & (EV_READ|EV_WRITE);
      short old_ev = bufferevent_get_enabled(conn->bufev);
496
      if ((ev & ~old_ev) != 0) {
497
        bufferevent_enable(conn->bufev, ev);
498
499
      }
      if ((old_ev & ~ev) != 0) {
500
        bufferevent_disable(conn->bufev, old_ev & ~ev);
501
      }
502
503
      return;
  });
504
  if (events & READ_EVENT)
505
506
507
508
    connection_start_reading(conn);
  else
    connection_stop_reading(conn);

509
  if (events & WRITE_EVENT)
510
511
512
    connection_start_writing(conn);
  else
    connection_stop_writing(conn);
Roger Dingledine's avatar
Roger Dingledine committed
513
514
}

Roger Dingledine's avatar
Roger Dingledine committed
515
/** Return true iff <b>conn</b> is listening for read events. */
516
517
518
int
connection_is_reading(connection_t *conn)
{
519
  tor_assert(conn);
520

521
522
523
  IF_HAS_BUFFEREVENT(conn,
    return (bufferevent_get_enabled(conn->bufev) & EV_READ) != 0;
  );
524
525
  return conn->reading_from_linked_conn ||
    (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
Roger Dingledine's avatar
Roger Dingledine committed
526
527
}

Roger Dingledine's avatar
Roger Dingledine committed
528
/** Tell the main loop to stop notifying <b>conn</b> of any read events. */
529
530
MOCK_IMPL(void,
connection_stop_reading,(connection_t *conn))
531
{
532
  tor_assert(conn);
533
534
535
536
537
538

  IF_HAS_BUFFEREVENT(conn, {
      bufferevent_disable(conn->bufev, EV_READ);
      return;
  });

539
  tor_assert(conn->read_event);
540

541
542
543
544
545
546
547
  if (conn->linked) {
    conn->reading_from_linked_conn = 0;
    connection_stop_reading_from_linked_conn(conn);
  } else {
    if (event_del(conn->read_event))
      log_warn(LD_NET, "Error from libevent setting read event state for %d "
               "to unwatched: %s",
548
               (int)conn->s,
549
550
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
551
552
}

Roger Dingledine's avatar
Roger Dingledine committed
553
/** Tell the main loop to start notifying <b>conn</b> of any read events. */
554
555
MOCK_IMPL(void,
connection_start_reading,(connection_t *conn))
556
{
557
  tor_assert(conn);
558
559
560
561
562
563

  IF_HAS_BUFFEREVENT(conn, {
      bufferevent_enable(conn->bufev, EV_READ);
      return;
  });

564
565
  tor_assert(conn->read_event);

566
567
568
569
570
571
572
573
  if (conn->linked) {
    conn->reading_from_linked_conn = 1;
    if (connection_should_read_from_linked_conn(conn))
      connection_start_reading_from_linked_conn(conn);
  } else {
    if (event_add(conn->read_event, NULL))
      log_warn(LD_NET, "Error from libevent setting read event state for %d "
               "to watched: %s",
574
               (int)conn->s,
575
576
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
577
578
}

Roger Dingledine's avatar
Roger Dingledine committed
579
/** Return true iff <b>conn</b> is listening for write events. */
580
581
582
int
connection_is_writing(connection_t *conn)
{
583
584
  tor_assert(conn);

585
586
587
588
  IF_HAS_BUFFEREVENT(conn,
    return (bufferevent_get_enabled(conn->bufev) & EV_WRITE) != 0;
  );

589
590
  return conn->writing_to_linked_conn ||
    (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
591
592
}

Roger Dingledine's avatar
Roger Dingledine committed
593
/** Tell the main loop to stop notifying <b>conn</b> of any write events. */
594
595
MOCK_IMPL(void,
connection_stop_writing,(connection_t *conn))
596
{
597
  tor_assert(conn);
598
599
600
601
602
603

  IF_HAS_BUFFEREVENT(conn, {
      bufferevent_disable(conn->bufev, EV_WRITE);
      return;
  });

604
605
  tor_assert(conn->write_event);

606
607
608
609
610
611
612
613
  if (conn->linked) {
    conn->writing_to_linked_conn = 0;
    if (conn->linked_conn)
      connection_stop_reading_from_linked_conn(conn->linked_conn);
  } else {
    if (event_del(conn->write_event))
      log_warn(LD_NET, "Error from libevent setting write event state for %d "
               "to unwatched: %s",
614
               (int)conn->s,
615
616
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
617
618
}

Roger Dingledine's avatar
Roger Dingledine committed
619
/** Tell the main loop to start notifying <b>conn</b> of any write events. */
620
621
MOCK_IMPL(void,
connection_start_writing,(connection_t *conn))
622
{
623
  tor_assert(conn);
624

625
626
627
628
629
  IF_HAS_BUFFEREVENT(conn, {
      bufferevent_enable(conn->bufev, EV_WRITE);
      return;
  });

630
631
  tor_assert(conn->write_event);

632
633
634
635
636
637
638
639
640
  if (conn->linked) {
    conn->writing_to_linked_conn = 1;
    if (conn->linked_conn &&
        connection_should_read_from_linked_conn(conn->linked_conn))
      connection_start_reading_from_linked_conn(conn->linked_conn);
  } else {
    if (event_add(conn->write_event, NULL))
      log_warn(LD_NET, "Error from libevent setting write event state for %d "
               "to watched: %s",
641
               (int)conn->s,
642
643
644
645
               tor_socket_strerror(tor_socket_errno(conn->s)));
  }
}

646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/** Return true iff <b>conn</b> is linked conn, and reading from the conn
 * linked to it would be good and feasible.  (Reading is "feasible" if the
 * other conn exists and has data in its outbuf, and is "good" if we have our
 * reading_from_linked_conn flag set and the other conn has its
 * writing_to_linked_conn flag set.)*/
static int
connection_should_read_from_linked_conn(connection_t *conn)
{
  if (conn->linked && conn->reading_from_linked_conn) {
    if (! conn->linked_conn ||
        (conn->linked_conn->writing_to_linked_conn &&
         buf_datalen(conn->linked_conn->outbuf)))
      return 1;
  }
  return 0;
}

663
664
665
666
/** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
 * its linked connection, if it is not doing so already.  Called by
 * connection_start_reading and connection_start_writing as appropriate. */
static void
667
668
669
670
671
672
673
674
675
676
connection_start_reading_from_linked_conn(connection_t *conn)
{
  tor_assert(conn);
  tor_assert(conn->linked == 1);

  if (!conn->active_on_link) {
    conn->active_on_link = 1;
    smartlist_add(active_linked_connection_lst, conn);
    if (!called_loop_once) {
      /* This is the first event on the list; we won't be in LOOP_ONCE mode,
677
678
679
       * so we need to make sure that the event_base_loop() actually exits at
       * the end of its run through the current connections and lets us
       * activate read events for linked connections. */
680
      struct timeval tv = { 0, 0 };
681
      tor_event_base_loopexit(tor_libevent_get_base(), &tv);
682
683
    }
  } else {
Nick Mathewson's avatar
Nick Mathewson committed
684
    tor_assert(smartlist_contains(active_linked_connection_lst, conn));
685
686
687
  }
}

688
689
690
/** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
 * connection, if is currently doing so.  Called by connection_stop_reading,
 * connection_stop_writing, and connection_read. */
691
692
693
694
695
696
697
698
void
connection_stop_reading_from_linked_conn(connection_t *conn)
{
  tor_assert(conn);
  tor_assert(conn->linked == 1);

  if (conn->active_on_link) {
    conn->active_on_link = 0;
699
700
701
    /* FFFF We could keep an index here so we can smartlist_del
     * cleanly.  On the other hand, this doesn't show up on profiles,
     * so let's leave it alone for now. */
702
703
    smartlist_remove(active_linked_connection_lst, conn);
  } else {
Nick Mathewson's avatar
Nick Mathewson committed
704
    tor_assert(!smartlist_contains(active_linked_connection_lst, conn));
705
  }
706
707
}

708
/** Close all connections that have been scheduled to get closed. */
709
STATIC void
710
711
712
713
714
close_closeable_connections(void)
{
  int i;
  for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
    connection_t *conn = smartlist_get(closeable_connection_lst, i);
715
    if (conn->conn_array_index < 0) {
716
      connection_unlink(conn); /* blow it away right now */
717
    } else {
718
      if (!conn_close_if_marked(conn->conn_array_index))
719
720
        ++i;
    }
721
722
723
  }
}

724
725
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
 * some data to read. */
726
static void
727
conn_read_callback(evutil_socket_t fd, short event, void *_conn)
728
729
{
  connection_t *conn = _conn;
730
731
  (void)fd;
  (void)event;
732

733
  log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
734

735
  /* assert_connection_ok(conn, time(NULL)); */
736
737
738

  if (connection_handle_read(conn) < 0) {
    if (!conn->marked_for_close) {
739
#ifndef _WIN32
740
      log_warn(LD_BUG,"Unhandled error on read for %s connection "
741
               "(fd %d); removing",
742
               conn_type_to_string(conn->type), (int)conn->s);
743
      tor_fragile_assert();
744
#endif
745
      if (CONN_IS_EDGE(conn))
746
        connection_edge_end_errno(TO_EDGE_CONN(conn));
747
748
749
750
751
752
753
754
755
      connection_mark_for_close(conn);
    }
  }
  assert_connection_ok(conn, time(NULL));

  if (smartlist_len(closeable_connection_lst))
    close_closeable_connections();
}

756
757
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
 * some data to write. */
758
static void
759
conn_write_callback(evutil_socket_t fd, short events, void *_conn)
760
761
{
  connection_t *conn = _conn;
762
763
  (void)fd;
  (void)events;
764

765
766
  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
                     (int)conn->s));
767

768
  /* assert_connection_ok(conn, time(NULL)); */
769

770
  if (connection_handle_write(conn, 0) < 0) {
771
772
    if (!conn->marked_for_close) {
      /* this connection is broken. remove it. */
773
      log_fn(LOG_WARN,LD_BUG,
Roger Dingledine's avatar
Roger Dingledine committed
774
             "unhandled error on write for %s connection (fd %d); removing",
775
             conn_type_to_string(conn->type), (int)conn->s);
776
      tor_fragile_assert();
777
778
      if (CONN_IS_EDGE(conn)) {
        /* otherwise we cry wolf about duplicate close */
779
780
781
        edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
        if (!edge_conn->end_reason)
          edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
782
        edge_conn->edge_has_sent_end = 1;
783
      }
784
      connection_close_immediate(conn); /* So we don't try to flush. */
785
786
787
788
789
790
791
792
793
      connection_mark_for_close(conn);
    }
  }
  assert_connection_ok(conn, time(NULL));

  if (smartlist_len(closeable_connection_lst))
    close_closeable_connections();
}

Roger Dingledine's avatar
Roger Dingledine committed
794
/** If the connection at connection_array[i] is marked for close, then:
795
796
797
798
799
 *    - If it has data that it wants to flush, try to flush it.
 *    - If it _still_ has data to flush, and conn->hold_open_until_flushed is
 *      true, then leave the connection open and return.
 *    - Otherwise, remove the connection from connection_array and from
 *      all other lists, close it, and free it.
800
 * Returns 1 if the connection was closed, 0 otherwise.
801
 */
802
803
804
static int
conn_close_if_marked(int i)
{
Roger Dingledine's avatar
Roger Dingledine committed
805
  connection_t *conn;
806
  int retval;
807
  time_t now;
Roger Dingledine's avatar
Roger Dingledine committed
808

809
  conn = smartlist_get(connection_array, i);
810
  if (!conn->marked_for_close)
811
    return 0; /* nothing to see here, move along */
812
813
  now = time(NULL);
  assert_connection_ok(conn, now);
814
  /* assert_all_pending_dns_resolves_ok(); */
815

816
#ifdef USE_BUFFEREVENTS
817
818
819
820
821
822
823
  if (conn->bufev) {
    if (conn->hold_open_until_flushed &&
        evbuffer_get_length(bufferevent_get_output(conn->bufev))) {
      /* don't close yet. */
      return 0;
    }
    if (conn->linked_conn && ! conn->linked_conn->marked_for_close) {
824
825
826
827
828
      /* We need to do this explicitly so that the linked connection
       * notices that there was an EOF. */
      bufferevent_flush(conn->bufev, EV_WRITE, BEV_FINISHED);
    }
  }
829
830
#endif

Nick Mathewson's avatar
Nick Mathewson committed
831
832
  log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
            conn->s);
833
834
835

  /* If the connection we are about to close was trying to connect to
  a proxy server and failed, the client won't be able to use that
836
  proxy. We should warn the user about this. */
837
838
  if (conn->proxy_state == PROXY_INFANT)
    log_failed_proxy_connection(conn);
839

840
  IF_HAS_BUFFEREVENT(conn, goto unlink);
841
842
  if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
      connection_wants_to_flush(conn)) {
843
    /* s == -1 means it's an incomplete edge connection, or that the socket
844
     * has already been closed as unflushable. */
845
    ssize_t sz = connection_bucket_write_limit(conn, now);
846
    if (!conn->hold_open_until_flushed)
847
848
849
      log_info(LD_NET,
               "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
               "to flush %d bytes. (Marked at %s:%d)",
850
               escaped_safe_str_client(conn->address),
851
               (int)conn->s, conn_type_to_string(conn->type), conn->state,
852
853
               (int)conn->outbuf_flushlen,
                conn->marked_for_close_file, conn->marked_for_close);
854
855
856
857
858
859
860
861
    if (conn->linked_conn) {
      retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
                               &conn->outbuf_flushlen);
      if (retval >= 0) {
        /* The linked conn will notice that it has data when it notices that
         * we're gone. */
        connection_start_reading_from_linked_conn(conn->linked_conn);
      }
862
      log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
863
               "%d left; flushlen %d; wants-to-flush==%d", retval,
864
865
                (int)connection_get_outbuf_len(conn),
                (int)conn->outbuf_flushlen,
866
                connection_wants_to_flush(conn));
867
    } else if (connection_speaks_cells(conn)) {
868
      if (conn->state == OR_CONN_STATE_OPEN) {
869
        retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
870
                               &conn->outbuf_flushlen);
871
      } else
872
        retval = -1; /* never flush non-open broken tls connections */
873
    } else {
874
      retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
875
    }
876
877
878
    if (retval >= 0 && /* Technically, we could survive things like
                          TLS_WANT_WRITE here. But don't bother for now. */
        conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
879
      if (retval > 0) {
880
881
        LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
                           "Holding conn (fd %d) open for more flushing.",
882
                           (int)conn->s));
883
        conn->timestamp_lastwritten = now; /* reset so we can flush more */
884
885
886
887
      } else if (sz == 0) {
        /* Also, retval==0.  If we get here, we didn't want to write anything
         * (because of rate-limiting) and we didn't. */

888
889
890
891
892
893
        /* Connection must flush before closing, but it's being rate-limited.
         * Let's remove from Libevent, and mark it as blocked on bandwidth
         * so it will be re-added on next token bucket refill. Prevents
         * busy Libevent loops where we keep ending up here and returning
         * 0 until we are no longer blocked on bandwidth.
         */
894
895
896
897
        if (connection_is_writing(conn)) {
          conn->write_blocked_on_bw = 1;
          connection_stop_writing(conn);
        }
898
        if (connection_is_reading(conn)) {
899
900
901
902
903
904
905
          /* XXXX024 We should make this code unreachable; if a connection is
           * marked for close and flushing, there is no point in reading to it
           * at all. Further, checking at this point is a bit of a hack: it
           * would make much more sense to react in
           * connection_handle_read_impl, or to just stop reading in
           * mark_and_flush */
#if 0
906
907
908
909
910
911
912
913
914
915
#define MARKED_READING_RATE 180
          static ratelim_t marked_read_lim = RATELIM_INIT(MARKED_READING_RATE);
          char *m;
          if ((m = rate_limit_log(&marked_read_lim, now))) {
            log_warn(LD_BUG, "Marked connection (fd %d, type %s, state %s) "
                     "is still reading; that shouldn't happen.%s",
                     (int)conn->s, conn_type_to_string(conn->type),
                     conn_state_to_string(conn->type, conn->state), m);
            tor_free(m);
          }
916
#endif
917
918
919
          conn->read_blocked_on_bw = 1;
          connection_stop_reading(conn);
        }
920
      }
921
      return 0;
922
    }
923
    if (connection_wants_to_flush(conn)) {
924
      log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
925
             "bytes to address %s.  If this happens a lot, either "
926
927
928
             "something is wrong with your network connection, or "
             "something is wrong with theirs. "
             "(fd %d, type %s, state %d, marked at %s:%d).",
929
             (int)connection_get_outbuf_len(conn),
930
             escaped_safe_str_client(conn->address),
931
             (int)conn->s, conn_type_to_string(conn->type), conn->state,
932
             conn->marked_for_close_file,
933
             conn->marked_for_close);
Roger Dingledine's avatar
Roger Dingledine committed
934
935
    }
  }
936
937
938
939

#ifdef USE_BUFFEREVENTS
 unlink:
#endif
940
  connection_unlink(conn); /* unlink, remove, free */
941
  return 1;
Roger Dingledine's avatar
Roger Dingledine committed
942
943
}

944
945
946
947
948
949
/** We've just tried every dirserver we know about, and none of
 * them were reachable. Assume the network is down. Change state
 * so next time an application connection arrives we'll delay it
 * and try another directory fetch. Kill off all the circuit_wait
 * streams that are waiting now, since they will all timeout anyway.
 */