connection.c 38.5 KB
Newer Older
1
/* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2
3
/* See LICENSE for licensing information */
/* $Id$ */
Roger Dingledine's avatar
Roger Dingledine committed
4

5
6
/**
 * \file connection.c
7
8
 * \brief General high-level functions to handle reading and writing
 * on connections.
9
10
 **/

Roger Dingledine's avatar
Roger Dingledine committed
11
12
13
14
#include "or.h"

/********* START VARIABLES **********/

15
extern or_options_t options; /* command-line and config-file options */
16

17
/** Array of strings to make conn-\>type human-readable. */
Roger Dingledine's avatar
Roger Dingledine committed
18
char *conn_type_to_string[] = {
19
20
21
22
23
24
25
  "",            /* 0 */
  "OP listener", /* 1 */
  "OP",          /* 2 */
  "OR listener", /* 3 */
  "OR",          /* 4 */
  "Exit",        /* 5 */
  "App listener",/* 6 */
26
27
28
  "App",         /* 7 */
  "Dir listener",/* 8 */
  "Dir",         /* 9 */
Roger Dingledine's avatar
Roger Dingledine committed
29
30
  "DNS worker",  /* 10 */
  "CPU worker",  /* 11 */
Roger Dingledine's avatar
Roger Dingledine committed
31
32
};

33
/** Array of string arrays to make {conn-\>type,conn-\>state} human-readable. */
34
char *conn_state_to_string[][_CONN_TYPE_MAX+1] = {
35
36
37
  { NULL }, /* no type associated with 0 */
  { NULL }, /* op listener, obsolete */
  { NULL }, /* op, obsolete */
Roger Dingledine's avatar
Roger Dingledine committed
38
  { "ready" }, /* or listener, 0 */
39
40
41
42
43
44
45
  { "",                         /* OR, 0 */
    "connect()ing",                 /* 1 */
    "handshaking",                  /* 2 */
    "open" },                       /* 3 */
  { "",                          /* exit, 0 */
    "waiting for dest info",           /* 1 */
    "connecting",                      /* 2 */
46
    "open",                            /* 3 */
47
    "resolve failed" },                /* 4 */
48
  { "ready" }, /* app listener, 0 */
49
50
51
  { "", /* 0 */
    "", /* 1 */
    "", /* 2 */
52
    "", /* 3 */
53
54
    "", /* 4 */
    "awaiting dest info",         /* app, 5 */
55
56
57
58
    "waiting for rendezvous desc",     /* 6 */
    "waiting for safe circuit",        /* 7 */
    "waiting for connected",           /* 8 */
    "open" },                          /* 9 */
59
  { "ready" }, /* dir listener, 0 */
60
  { "",                           /* dir, 0 */
Roger Dingledine's avatar
Roger Dingledine committed
61
62
63
64
65
    "connecting",                      /* 1 */
    "client sending",                  /* 2 */
    "client reading",                  /* 3 */
    "awaiting command",                /* 4 */
    "writing" },                       /* 5 */
66
67
68
69
70
71
72
  { "",                    /* dns worker, 0 */
    "idle",                            /* 1 */
    "busy" },                          /* 2 */
  { "",                    /* cpu worker, 0 */
    "idle",                            /* 1 */
    "busy with onion",                 /* 2 */
    "busy with handshake" },           /* 3 */
Roger Dingledine's avatar
Roger Dingledine committed
73
74
75
76
};

/********* END VARIABLES ************/

77
static int connection_init_accepted_conn(connection_t *conn);
Nick Mathewson's avatar
Nick Mathewson committed
78
static int connection_handle_listener_read(connection_t *conn, int new_type);
79
static int connection_receiver_bucket_should_increase(connection_t *conn);
80
81
82

/**************************************************************/

83
/** Allocate space for a new connection_t. This function just initializes
84
85
 * conn; you must call connection_add() to link it into the main array.
 *
Nick Mathewson's avatar
Nick Mathewson committed
86
 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>poll_index to
87
88
89
90
91
92
93
94
95
 * -1 to signify they are not yet assigned.
 *
 * If conn is not a listener type, allocate buffers for it. If it's
 * an AP type, allocate space to store the socks_request.
 *
 * Assign a pseudorandom next_circ_id between 0 and 2**15.
 *
 * Initialize conn's timestamps to now.
 */
Roger Dingledine's avatar
Roger Dingledine committed
96
97
connection_t *connection_new(int type) {
  connection_t *conn;
98
  time_t now = time(NULL);
Roger Dingledine's avatar
Roger Dingledine committed
99

100
  conn = tor_malloc_zero(sizeof(connection_t));
101
  conn->magic = CONNECTION_MAGIC;
102
  conn->s = -1; /* give it a default of 'not used' */
103
  conn->poll_index = -1; /* also default to 'not used' */
Roger Dingledine's avatar
Roger Dingledine committed
104
105

  conn->type = type;
106
107
108
109
  if(!connection_is_listener(conn)) { /* listeners never use their buf */
    conn->inbuf = buf_new();
    conn->outbuf = buf_new();
  }
110
  if (type == CONN_TYPE_AP) {
111
    conn->socks_request = tor_malloc_zero(sizeof(socks_request_t));
112
  }
Roger Dingledine's avatar
Roger Dingledine committed
113

114
115
  conn->next_circ_id = crypto_pseudo_rand_int(1<<15);

116
117
118
  conn->timestamp_created = now;
  conn->timestamp_lastread = now;
  conn->timestamp_lastwritten = now;
119

Roger Dingledine's avatar
Roger Dingledine committed
120
121
122
  return conn;
}

123
124
/** Deallocate memory used by <b>conn</b>. Deallocate its buffers if necessary,
 * close its socket if necessary, and mark the directory as dirty if <b>conn</b>
125
126
 * is an OR or OP connection.
 */
Roger Dingledine's avatar
Roger Dingledine committed
127
void connection_free(connection_t *conn) {
Roger Dingledine's avatar
Roger Dingledine committed
128
129
  tor_assert(conn);
  tor_assert(conn->magic == CONNECTION_MAGIC);
Roger Dingledine's avatar
Roger Dingledine committed
130

131
132
133
134
  if(!connection_is_listener(conn)) {
    buf_free(conn->inbuf);
    buf_free(conn->outbuf);
  }
135
  tor_free(conn->address);
Roger Dingledine's avatar
Roger Dingledine committed
136

137
  if(connection_speaks_cells(conn)) {
138
    directory_set_dirty(); /* XXX should only do this for an open OR conn */
Roger Dingledine's avatar
Roger Dingledine committed
139
140
    if (conn->tls)
      tor_tls_free(conn->tls);
Roger Dingledine's avatar
Roger Dingledine committed
141
142
  }

143
144
  if (conn->identity_pkey)
    crypto_free_pk_env(conn->identity_pkey);
145
  tor_free(conn->nickname);
146
  tor_free(conn->socks_request);
147

148
  if(conn->s >= 0) {
149
    log_fn(LOG_INFO,"closing fd %d.",conn->s);
150
    tor_close_socket(conn->s);
151
  }
152
  memset(conn, 0xAA, sizeof(connection_t)); /* poison memory */
Roger Dingledine's avatar
Roger Dingledine committed
153
154
155
  free(conn);
}

156
157
158
159
/** Call connection_free() on every connection in our array.
 * This is used by cpuworkers and dnsworkers when they fork,
 * so they don't keep resources held open (especially sockets).
 */
160
161
162
163
164
165
166
167
168
void connection_free_all(void) {
  int i, n;
  connection_t **carray;

  get_connection_array(&carray,&n);
  for(i=0;i<n;i++)
    connection_free(carray[i]);
}

169
170
171
/** Close the underlying socket for <b>conn</b>, so we don't try to
 * flush it. Must be used in conjunction with (right before)
 * connection_mark_for_close().
172
173
174
175
176
177
178
179
 */
void connection_close_immediate(connection_t *conn)
{
  assert_connection_ok(conn,0);
  if (conn->s < 0) {
    log_fn(LOG_WARN,"Attempt to close already-closed connection.");
    return;
  }
180
  if (conn->outbuf_flushlen) {
181
182
    log_fn(LOG_INFO,"Closing connection (fd %d, type %s, state %d) with data on outbuf.",
           conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state);
183
  }
184
  tor_close_socket(conn->s);
185
  conn->s = -1;
186
187
188
189
  if(!connection_is_listener(conn)) {
    buf_clear(conn->outbuf);
    conn->outbuf_flushlen = 0;
  }
190
191
}

192
193
194
195
196
197
198
199
200
201
/** Mark <b>conn</b> to be closed next time we loop through
 * conn_close_if_marked() in main.c. Do any cleanup needed:
 *   - Directory conns that fail to fetch a rendezvous descriptor need
 *     to inform pending rendezvous streams.
 *   - OR conns need to call rep_hist_note_*() to record status.
 *   - AP conns need to send a socks reject if necessary.
 *   - Exit conns need to call connection_dns_remove() if necessary.
 *   - AP and Exit conns need to send an end cell if they can.
 *   - DNS conns need to fail any resolves that are pending on them.
 */
202
203
204
int
_connection_mark_for_close(connection_t *conn, char reason)
{
205
  int retval = 0;
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  assert_connection_ok(conn,0);

  if (conn->marked_for_close) {
    log(LOG_WARN, "Double mark-for-close on connection.");
    return -1;
  }

  switch (conn->type)
    {
    case CONN_TYPE_OR_LISTENER:
    case CONN_TYPE_AP_LISTENER:
    case CONN_TYPE_DIR_LISTENER:
    case CONN_TYPE_CPUWORKER:
      /* No special processing needed. */
      break;
221
222
223
224
    case CONN_TYPE_DIR:
      if(conn->purpose == DIR_PURPOSE_FETCH_RENDDESC)
        rend_client_desc_fetched(conn->rend_query, 0);
      break;
225
    case CONN_TYPE_OR:
226
227
      /* Remember why we're closing this connection. */
      if (conn->state != OR_CONN_STATE_OPEN) {
228
229
230
231
232
        /* XXX Nick: this still isn't right, because it might be
         * dying even though we didn't initiate the connect. Can
         * you look at this more? -RD */
        if(conn->nickname)
          rep_hist_note_connect_failed(conn->nickname, time(NULL));
233
234
235
236
237
      } else if (reason == CLOSE_REASON_UNUSED_OR_CONN) {
        rep_hist_note_disconnect(conn->nickname, time(NULL));
      } else {
        rep_hist_note_connection_died(conn->nickname, time(NULL));
      }
238
239
      break;
    case CONN_TYPE_AP:
Roger Dingledine's avatar
Roger Dingledine committed
240
241
242
243
      if (conn->socks_request->has_finished == 0) {
        log_fn(LOG_INFO,"Cleaning up AP -- sending socks reject.");
        connection_ap_handshake_socks_reply(conn, NULL, 0, 0);
        conn->socks_request->has_finished = 1;
244
        conn->hold_open_until_flushed = 1;
Roger Dingledine's avatar
Roger Dingledine committed
245
246
247
      }
      /* fall through, to do things for both ap and exit */
    case CONN_TYPE_EXIT:
248
      if (conn->state == EXIT_CONN_STATE_RESOLVING)
249
        connection_dns_remove(conn);
250
      if (!conn->has_sent_end && reason &&
251
          connection_edge_end(conn, reason, conn->cpath_layer) < 0)
252
        retval = -1;
253
254
255
      break;
    case CONN_TYPE_DNSWORKER:
      if (conn->state == DNSWORKER_STATE_BUSY) {
256
        dns_cancel_pending_resolve(conn->address);
257
258
259
260
261
262
263
      }
      break;
    default:
      log(LOG_ERR, "Unknown connection type %d", conn->type);
      ;
    }
  conn->marked_for_close = 1;
264
265
266
267
268
269

  /* in case we're going to be held-open-til-flushed, reset
   * the number of seconds since last successful write, so
   * we get our whole 15 seconds */
  conn->timestamp_lastwritten = time(NULL);

270
  return retval;
271
272
}

273
274
275
276
277
/** Find each connection that has hold_open_until_flushed set to
 * 1 but hasn't written in the past 15 seconds, and set
 * hold_open_until_flushed to 0. This means it will get cleaned
 * up in the next loop through close_if_marked() in main.c.
 */
278
279
280
281
282
283
284
285
286
287
288
289
290
291
void connection_expire_held_open(void)
{
  connection_t **carray, *conn;
  int n, i;
  time_t now;

  now = time(NULL);

  get_connection_array(&carray, &n);
  for (i = 0; i < n; ++i) {
    conn = carray[i];
    /* If we've been holding the connection open, but we haven't written
     * for 15 seconds...
     */
292
    if (conn->hold_open_until_flushed) {
Roger Dingledine's avatar
Roger Dingledine committed
293
      tor_assert(conn->marked_for_close);
294
      if (now - conn->timestamp_lastwritten >= 15) {
295
        log_fn(LOG_WARN,"Giving up on marked_for_close conn that's been flushing for 15s (fd %d, type %s, state %d).",
Roger Dingledine's avatar
Roger Dingledine committed
296
               conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state);
297
298
        conn->hold_open_until_flushed = 0;
      }
299
300
301
302
    }
  }
}

303
304
305
306
/** Bind a new non-blocking socket listening to
 * <b>bindaddress</b>:<b>bindport</b>, and add this new connection
 * (of type <b>type</b>) to the connection array.
 */
307
308
int connection_create_listener(char *bindaddress, uint16_t bindport, int type) {
  struct sockaddr_in bindaddr; /* where to bind */
Roger Dingledine's avatar
Roger Dingledine committed
309
  connection_t *conn;
310
  int s; /* the socket we're going to make */
Roger Dingledine's avatar
Roger Dingledine committed
311
312
  int one=1;

313
314
315
  memset(&bindaddr,0,sizeof(struct sockaddr_in));
  bindaddr.sin_family = AF_INET;
  bindaddr.sin_port = htons(bindport);
316
  if(tor_lookup_hostname(bindaddress, &(bindaddr.sin_addr.s_addr)) != 0) {
317
318
319
320
    log_fn(LOG_WARN,"Can't resolve BindAddress %s",bindaddress);
    return -1;
  }

Roger Dingledine's avatar
Roger Dingledine committed
321
  s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
Roger Dingledine's avatar
Roger Dingledine committed
322
  if (s < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
323
    log_fn(LOG_WARN,"Socket creation failed.");
Roger Dingledine's avatar
Roger Dingledine committed
324
325
326
    return -1;
  }

327
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*) &one, sizeof(one));
Roger Dingledine's avatar
Roger Dingledine committed
328

329
  if(bind(s,(struct sockaddr *)&bindaddr,sizeof(bindaddr)) < 0) {
330
    log_fn(LOG_WARN,"Could not bind to port %u: %s",bindport,
331
           tor_socket_strerror(tor_socket_errno(s)));
Roger Dingledine's avatar
Roger Dingledine committed
332
333
334
335
    return -1;
  }

  if(listen(s,SOMAXCONN) < 0) {
336
    log_fn(LOG_WARN,"Could not listen on port %u: %s",bindport,
337
           tor_socket_strerror(tor_socket_errno(s)));
Roger Dingledine's avatar
Roger Dingledine committed
338
339
340
    return -1;
  }

341
  set_socket_nonblocking(s);
Roger Dingledine's avatar
Roger Dingledine committed
342
343
344
345
346

  conn = connection_new(type);
  conn->s = s;

  if(connection_add(conn) < 0) { /* no space, forget it */
Roger Dingledine's avatar
Roger Dingledine committed
347
    log_fn(LOG_WARN,"connection_add failed. Giving up.");
Roger Dingledine's avatar
Roger Dingledine committed
348
349
350
351
    connection_free(conn);
    return -1;
  }

352
  log_fn(LOG_DEBUG,"%s listening on port %u.",conn_type_to_string[type], bindport);
Roger Dingledine's avatar
Roger Dingledine committed
353
354

  conn->state = LISTENER_STATE_READY;
355
  connection_start_reading(conn);
Roger Dingledine's avatar
Roger Dingledine committed
356
357
358
359

  return 0;
}

360
/** The listener connection <b>conn</b> told poll() it wanted to read.
Nick Mathewson's avatar
Nick Mathewson committed
361
 * Call accept() on conn-\>s, and add the new connection if necessary.
362
 */
Nick Mathewson's avatar
Nick Mathewson committed
363
static int connection_handle_listener_read(connection_t *conn, int new_type) {
Roger Dingledine's avatar
Roger Dingledine committed
364
365
366
367
368
369
370
  int news; /* the new socket */
  connection_t *newconn;
  struct sockaddr_in remote; /* information about the remote peer when connecting to other routers */
  int remotelen = sizeof(struct sockaddr_in); /* length of the remote address */

  news = accept(conn->s,(struct sockaddr *)&remote,&remotelen);
  if (news == -1) { /* accept() error */
371
    if(ERRNO_IS_EAGAIN(tor_socket_errno(conn->s))) {
Roger Dingledine's avatar
Roger Dingledine committed
372
      return 0; /* he hung up before we could accept(). that's fine. */
373
    }
Roger Dingledine's avatar
Roger Dingledine committed
374
    /* else there was a real error. */
Roger Dingledine's avatar
Roger Dingledine committed
375
    log_fn(LOG_WARN,"accept() failed. Closing listener.");
376
    connection_mark_for_close(conn,0);
Roger Dingledine's avatar
Roger Dingledine committed
377
378
    return -1;
  }
379
  log(LOG_INFO,"Connection accepted on socket %d (child of fd %d).",news, conn->s);
Roger Dingledine's avatar
Roger Dingledine committed
380

381
  set_socket_nonblocking(news);
382

Roger Dingledine's avatar
Roger Dingledine committed
383
384
385
  newconn = connection_new(new_type);
  newconn->s = news;

386
  newconn->address = tor_strdup(inet_ntoa(remote.sin_addr)); /* remember the remote address */
387
388
  newconn->addr = ntohl(remote.sin_addr.s_addr);
  newconn->port = ntohs(remote.sin_port);
Roger Dingledine's avatar
Roger Dingledine committed
389
390
391

  if(connection_add(newconn) < 0) { /* no space, forget it */
    connection_free(newconn);
392
    return 0; /* no need to tear down the parent */
Roger Dingledine's avatar
Roger Dingledine committed
393
394
  }

395
  if(connection_init_accepted_conn(newconn) < 0) {
396
    connection_mark_for_close(newconn,0);
397
398
399
400
401
    return 0;
  }
  return 0;
}

402
403
404
/** Initialize states for newly accepted connection <b>conn</b>.
 * If conn is an OR, start the tls handshake.
 */
405
406
407
408
409
410
static int connection_init_accepted_conn(connection_t *conn) {

  connection_start_reading(conn);

  switch(conn->type) {
    case CONN_TYPE_OR:
411
      return connection_tls_start_handshake(conn, 1);
412
413
414
415
    case CONN_TYPE_AP:
      conn->state = AP_CONN_STATE_SOCKS_WAIT;
      break;
    case CONN_TYPE_DIR:
Roger Dingledine's avatar
Roger Dingledine committed
416
      conn->purpose = DIR_PURPOSE_SERVER;
417
      conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
418
419
420
421
422
      break;
  }
  return 0;
}

423
/** Take conn, make a nonblocking socket; try to connect to
424
 * addr:port (they arrive in *host order*). If fail, return -1. Else
Nick Mathewson's avatar
Nick Mathewson committed
425
 * assign s to conn->\s: if connected return 1, if EAGAIN return 0.
426
427
428
 *
 * address is used to make the logs useful.
 *
Nick Mathewson's avatar
Nick Mathewson committed
429
 * On success, add conn to the list of polled connections.
430
431
432
433
434
435
436
 */
int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port) {
  int s;
  struct sockaddr_in dest_addr;

  s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  if (s < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
437
    log_fn(LOG_WARN,"Error creating network socket.");
438
439
440
441
    return -1;
  }
  set_socket_nonblocking(s);

442
  memset(&dest_addr,0,sizeof(dest_addr));
443
444
445
446
447
448
449
  dest_addr.sin_family = AF_INET;
  dest_addr.sin_port = htons(port);
  dest_addr.sin_addr.s_addr = htonl(addr);

  log_fn(LOG_DEBUG,"Connecting to %s:%u.",address,port);

  if(connect(s,(struct sockaddr *)&dest_addr,sizeof(dest_addr)) < 0) {
450
    if(!ERRNO_IS_CONN_EINPROGRESS(tor_socket_errno(s))) {
451
      /* yuck. kill it. */
452
      log_fn(LOG_INFO,"Connect() to %s:%u failed: %s",address,port,
453
             tor_socket_strerror(tor_socket_errno(s)));
454
      tor_close_socket(s);
455
456
457
458
      return -1;
    } else {
      /* it's in progress. set state appropriately and return. */
      conn->s = s;
459
460
      if(connection_add(conn) < 0) /* no space, forget it */
        return -1;
461
462
463
464
465
466
      log_fn(LOG_DEBUG,"connect in progress, socket %d.",s);
      return 0;
    }
  }

  /* it succeeded. we're connected. */
467
  log_fn(LOG_INFO,"Connection to %s:%u established.",address,port);
468
  conn->s = s;
469
470
  if(connection_add(conn) < 0) /* no space, forget it */
    return -1;
471
472
473
  return 1;
}

474
475
476
/** If there exists a listener of type <b>type</b> in the connection
 * array, mark it for close.
 */
477
static void listener_close_if_present(int type) {
478
  connection_t *conn;
Roger Dingledine's avatar
Roger Dingledine committed
479
480
481
  tor_assert(type == CONN_TYPE_OR_LISTENER ||
             type == CONN_TYPE_AP_LISTENER ||
             type == CONN_TYPE_DIR_LISTENER);
482
  conn = connection_get_by_type(type);
483
  if (conn) {
484
    connection_close_immediate(conn);
485
    connection_mark_for_close(conn,0);
486
487
488
  }
}

489
490
491
492
/** Start all connections that should be up but aren't.
 *  - Connect to all ORs if you're an OR.
 *  - Relaunch listeners for each port you have open.
 */
493
int retry_all_connections(void) {
494

495
  if(options.ORPort) {
496
    router_retry_connections();
Roger Dingledine's avatar
Roger Dingledine committed
497
498
  }

499
500
  if(options.ORPort) {
    listener_close_if_present(CONN_TYPE_OR_LISTENER);
Roger Dingledine's avatar
Roger Dingledine committed
501
502
    if(connection_create_listener(options.ORBindAddress,
                                  (uint16_t) options.ORPort,
503
504
                                  CONN_TYPE_OR_LISTENER) < 0)
      return -1;
Roger Dingledine's avatar
Roger Dingledine committed
505
  }
506

507
508
  if(options.DirPort) {
    listener_close_if_present(CONN_TYPE_DIR_LISTENER);
509
    if(connection_create_listener(options.DirBindAddress,
Roger Dingledine's avatar
Roger Dingledine committed
510
                                  (uint16_t) options.DirPort,
511
512
                                  CONN_TYPE_DIR_LISTENER) < 0)
      return -1;
513
  }
514
515
516

  if(options.SocksPort) {
    listener_close_if_present(CONN_TYPE_AP_LISTENER);
517
    if(connection_create_listener(options.SocksBindAddress,
Roger Dingledine's avatar
Roger Dingledine committed
518
                                  (uint16_t) options.SocksPort,
519
520
                                  CONN_TYPE_AP_LISTENER) < 0)
      return -1;
521
522
  }

Roger Dingledine's avatar
Roger Dingledine committed
523
524
525
  return 0;
}

Nick Mathewson's avatar
Nick Mathewson committed
526
extern int global_read_bucket;
527

528
/** How many bytes at most can we read onto this connection? */
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
int connection_bucket_read_limit(connection_t *conn) {
  int at_most;

  if(options.LinkPadding) {
    at_most = global_read_bucket;
  } else {
    /* do a rudimentary round-robin so one circuit can't hog a connection */
    if(connection_speaks_cells(conn)) {
      at_most = 32*(CELL_NETWORK_SIZE);
    } else {
      at_most = 32*(RELAY_PAYLOAD_SIZE);
    }

    if(at_most > global_read_bucket)
      at_most = global_read_bucket;
  }

  if(connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN)
    if(at_most > conn->receiver_bucket)
      at_most = conn->receiver_bucket;

  return at_most;
}

553
/** We just read num_read onto conn. Decrement buckets appropriately. */
554
void connection_bucket_decrement(connection_t *conn, int num_read) {
Roger Dingledine's avatar
Roger Dingledine committed
555
  global_read_bucket -= num_read; tor_assert(global_read_bucket >= 0);
556
  if(connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
Roger Dingledine's avatar
Roger Dingledine committed
557
    conn->receiver_bucket -= num_read; tor_assert(conn->receiver_bucket >= 0);
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  }
  if(global_read_bucket == 0) {
    log_fn(LOG_DEBUG,"global bucket exhausted. Pausing.");
    conn->wants_to_read = 1;
    connection_stop_reading(conn);
    return;
  }
  if(connection_speaks_cells(conn) &&
     conn->state == OR_CONN_STATE_OPEN &&
     conn->receiver_bucket == 0) {
      log_fn(LOG_DEBUG,"receiver bucket exhausted. Pausing.");
      conn->wants_to_read = 1;
      connection_stop_reading(conn);
  }
}

574
/** Keep a timeval to know when time has passed enough to refill buckets */
575
576
static struct timeval current_time;

577
578
/** Initiatialize the global read bucket to options.BandwidthBurst,
 * and current_time to the current time. */
579
void connection_bucket_init(void) {
580
581
582
583
  tor_gettimeofday(&current_time);
  global_read_bucket = options.BandwidthBurst; /* start it at max traffic */
}

584
/** Some time has passed; increment buckets appropriately. */
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
void connection_bucket_refill(struct timeval *now) {
  int i, n;
  connection_t *conn;
  connection_t **carray;

  if(now->tv_sec <= current_time.tv_sec)
    return; /* wait until the second has rolled over */

  current_time.tv_sec = now->tv_sec; /* update current_time */
  /* (ignore usecs for now) */

  /* refill the global bucket */
  if(global_read_bucket < options.BandwidthBurst) {
    global_read_bucket += options.BandwidthRate;
    log_fn(LOG_DEBUG,"global_read_bucket now %d.", global_read_bucket);
  }

  /* refill the per-connection buckets */
  get_connection_array(&carray,&n);
  for(i=0;i<n;i++) {
    conn = carray[i];

    if(connection_receiver_bucket_should_increase(conn)) {
      conn->receiver_bucket += conn->bandwidth;
      //log_fn(LOG_DEBUG,"Receiver bucket %d now %d.", i, conn->receiver_bucket);
    }

    if(conn->wants_to_read == 1 /* it's marked to turn reading back on now */
       && global_read_bucket > 0 /* and we're allowed to read */
       && (!connection_speaks_cells(conn) ||
           conn->state != OR_CONN_STATE_OPEN ||
           conn->receiver_bucket > 0)) {
      /* and either a non-cell conn or a cell conn with non-empty bucket */
618
      log_fn(LOG_DEBUG,"waking up conn (fd %d)",conn->s);
619
620
621
622
623
624
625
626
627
628
      conn->wants_to_read = 0;
      connection_start_reading(conn);
      if(conn->wants_to_write == 1) {
        conn->wants_to_write = 0;
        connection_start_writing(conn);
      }
    }
  }
}

629
630
631
/** Is the receiver bucket for connection <b>conn</b> low enough that we
 * should add another pile of tokens to it?
 */
632
static int connection_receiver_bucket_should_increase(connection_t *conn) {
Roger Dingledine's avatar
Roger Dingledine committed
633
  tor_assert(conn);
634
635
636
637
638
639

  if(!connection_speaks_cells(conn))
    return 0; /* edge connections don't use receiver_buckets */
  if(conn->state != OR_CONN_STATE_OPEN)
    return 0; /* only open connections play the rate limiting game */

Roger Dingledine's avatar
Roger Dingledine committed
640
  tor_assert(conn->bandwidth > 0);
641
642
643
644
645
646
  if(conn->receiver_bucket > 9*conn->bandwidth)
    return 0;

  return 1;
}

Nick Mathewson's avatar
Nick Mathewson committed
647
/** Read bytes from conn->\s and process them.
648
649
650
651
652
653
654
655
656
657
658
 *
 * This function gets called from conn_read() in main.c, either
 * when poll() has declared that conn wants to read, or (for OR conns)
 * when there are pending TLS bytes.
 *
 * It calls connection_read_to_buf() to bring in any new bytes,
 * and then calls connection_process_inbuf() to process them.
 *
 * Mark the connection and return -1 if you want to close it, else
 * return 0.
 */
659
660
int connection_handle_read(connection_t *conn) {

661
  conn->timestamp_lastread = time(NULL);
662
663
664

  switch(conn->type) {
    case CONN_TYPE_OR_LISTENER:
665
      return connection_handle_listener_read(conn, CONN_TYPE_OR);
666
    case CONN_TYPE_AP_LISTENER:
667
      return connection_handle_listener_read(conn, CONN_TYPE_AP);
668
    case CONN_TYPE_DIR_LISTENER:
669
670
      return connection_handle_listener_read(conn, CONN_TYPE_DIR);
  }
671

672
  if(connection_read_to_buf(conn) < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
673
    if(conn->type == CONN_TYPE_DIR &&
Roger Dingledine's avatar
Roger Dingledine committed
674
       conn->state == DIR_CONN_STATE_CONNECTING) {
675
676
       /* it's a directory server and connecting failed: forget about this router */
       /* XXX I suspect pollerr may make Windows not get to this point. :( */
677
       router_mark_as_down(conn->nickname);
678
    }
679
    /* There's a read error; kill the connection.*/
680
    connection_close_immediate(conn); /* Don't flush; connection is dead. */
681
    connection_mark_for_close(conn, END_STREAM_REASON_MISC);
682
683
684
    return -1;
  }
  if(connection_process_inbuf(conn) < 0) {
685
//    log_fn(LOG_DEBUG,"connection_process_inbuf returned -1.");
686
687
    return -1;
  }
Roger Dingledine's avatar
Roger Dingledine committed
688
  return 0;
689
690
}

Nick Mathewson's avatar
Nick Mathewson committed
691
/** Pull in new bytes from conn-\>s onto conn-\>inbuf, either
692
693
694
695
696
 * directly or via TLS. Reduce the token buckets by the number of
 * bytes read.
 *
 * Return -1 if we want to break conn, else return 0.
 */
Roger Dingledine's avatar
Roger Dingledine committed
697
int connection_read_to_buf(connection_t *conn) {
Roger Dingledine's avatar
Roger Dingledine committed
698
  int result;
Roger Dingledine's avatar
Roger Dingledine committed
699
  int at_most;
700

701
702
  /* how many bytes are we allowed to read? */
  at_most = connection_bucket_read_limit(conn);
703

Roger Dingledine's avatar
Roger Dingledine committed
704
  if(connection_speaks_cells(conn) && conn->state != OR_CONN_STATE_CONNECTING) {
705
706
    if(conn->state == OR_CONN_STATE_HANDSHAKING) {
      /* continue handshaking even if global token bucket is empty */
707
      return connection_tls_continue_handshake(conn);
708
    }
709

710
711
712
    log_fn(LOG_DEBUG,"%d: starting, inbuf_datalen %d (%d pending in tls object). at_most %d.",
           conn->s,(int)buf_datalen(conn->inbuf),tor_tls_get_pending_bytes(conn->tls), at_most);

713
    /* else open, or closing */
714
    result = read_to_buf_tls(conn->tls, at_most, conn->inbuf);
Roger Dingledine's avatar
Roger Dingledine committed
715
716
717
718

    switch(result) {
      case TOR_TLS_ERROR:
      case TOR_TLS_CLOSE:
719
720
        log_fn(LOG_INFO,"tls error. breaking (nickname %s, address %s).",
               conn->nickname ? conn->nickname : "not set", conn->address);
Roger Dingledine's avatar
Roger Dingledine committed
721
        return -1; /* XXX deal with close better */
722
      case TOR_TLS_WANTWRITE:
Roger Dingledine's avatar
Roger Dingledine committed
723
724
        connection_start_writing(conn);
        return 0;
725
      case TOR_TLS_WANTREAD: /* we're already reading */
Roger Dingledine's avatar
Roger Dingledine committed
726
      case TOR_TLS_DONE: /* no data read, so nothing to process */
727
728
        result = 0;
        break; /* so we call bucket_decrement below */
729
    }
730
  } else {
731
732
733
    result = read_to_buf(conn->s, at_most, conn->inbuf,
                         &conn->inbuf_reached_eof);

Roger Dingledine's avatar
Roger Dingledine committed
734
735
736
737
//  log(LOG_DEBUG,"connection_read_to_buf(): read_to_buf returned %d.",read_result);

    if(result < 0)
      return -1;
738
739
  }

740
  connection_bucket_decrement(conn, result);
Roger Dingledine's avatar
Roger Dingledine committed
741
  return 0;
Roger Dingledine's avatar
Roger Dingledine committed
742
743
}

744
/** A pass-through to fetch_from_buf. */
Roger Dingledine's avatar
Roger Dingledine committed
745
int connection_fetch_from_buf(char *string, int len, connection_t *conn) {
746
  return fetch_from_buf(string, len, conn->inbuf);
Roger Dingledine's avatar
Roger Dingledine committed
747
748
}

Nick Mathewson's avatar
Nick Mathewson committed
749
/** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
750
 * from its outbuf. */
751
752
753
754
int connection_wants_to_flush(connection_t *conn) {
  return conn->outbuf_flushlen;
}

755
756
757
758
/** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
 * connection_edge_consider_sending_sendme().
 */
759
760
761
762
int connection_outbuf_too_full(connection_t *conn) {
  return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE);
}

Nick Mathewson's avatar
Nick Mathewson committed
763
/** Try to flush more bytes onto conn-\>s.
764
765
766
767
768
 *
 * This function gets called either from conn_write() in main.c
 * when poll() has declared that conn wants to write, or below
 * from connection_write_to_buf() when an entire TLS record is ready.
 *
Nick Mathewson's avatar
Nick Mathewson committed
769
 * Update conn-\>timestamp_lastwritten to now, and call flush_buf
770
771
772
773
774
775
776
 * or flush_buf_tls appropriately. If it succeeds and there no more
 * more bytes on conn->outbuf, then call connection_finished_flushing
 * on it too.
 *
 * Mark the connection and return -1 if you want to close it, else
 * return 0.
 */
777
778
int connection_handle_write(connection_t *conn) {

Roger Dingledine's avatar
Roger Dingledine committed
779
  tor_assert(!connection_is_listener(conn));
780

781
  conn->timestamp_lastwritten = time(NULL);
782

783
784
785
  if (connection_speaks_cells(conn) &&
      conn->state != OR_CONN_STATE_CONNECTING) {
    if (conn->state == OR_CONN_STATE_HANDSHAKING) {
786
      connection_stop_writing(conn);
787
788
789
790
791
792
      if(connection_tls_continue_handshake(conn) < 0) {
        connection_close_immediate(conn); /* Don't flush; connection is dead. */
        connection_mark_for_close(conn, 0);
        return -1;
      }
      return 0;
793
    }
794

795
    /* else open, or closing */
796
    switch(flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen)) {
Roger Dingledine's avatar
Roger Dingledine committed
797
798
      case TOR_TLS_ERROR:
      case TOR_TLS_CLOSE:
799
        log_fn(LOG_INFO,"tls error. breaking.");
800
        connection_close_immediate(conn); /* Don't flush; connection is dead. */
801
        connection_mark_for_close(conn, 0);
Roger Dingledine's avatar
Roger Dingledine committed
802
        return -1; /* XXX deal with close better */
803
      case TOR_TLS_WANTWRITE:
804
        log_fn(LOG_DEBUG,"wanted write.");
Roger Dingledine's avatar
Roger Dingledine committed
805
806
        /* we're already writing */
        return 0;
807
      case TOR_TLS_WANTREAD:
Roger Dingledine's avatar
Roger Dingledine committed
808
        /* Make sure to avoid a loop if the receive buckets are empty. */
809
        log_fn(LOG_DEBUG,"wanted read.");
Roger Dingledine's avatar
Roger Dingledine committed
810
811
812
813
814
815
816
817
818
        if(!connection_is_reading(conn)) {
          connection_stop_writing(conn);
          conn->wants_to_write = 1;
          /* we'll start reading again when the next second arrives,
           * and then also start writing again.
           */
        }
        /* else no problem, we're already reading */
        return 0;
819
820
      /* case TOR_TLS_DONE:
       * for TOR_TLS_DONE, fall through to check if the flushlen
Roger Dingledine's avatar
Roger Dingledine committed
821
       * is empty, so we can stop writing.
Roger Dingledine's avatar
Roger Dingledine committed
822
       */
Roger Dingledine's avatar
Roger Dingledine committed
823
    }
824
  } else {
825
    if (flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen) < 0) {
826
      connection_close_immediate(conn); /* Don't flush; connection is dead. */
827
      connection_mark_for_close(conn, END_STREAM_REASON_MISC);
Roger Dingledine's avatar
Roger Dingledine committed
828
      return -1;
829
    }
Roger Dingledine's avatar
Roger Dingledine committed
830
    /* conns in CONNECTING state will fall through... */
831
832
  }

Roger Dingledine's avatar
Roger Dingledine committed
833
  if(!connection_wants_to_flush(conn)) /* it's done flushing */
834
835
    if(connection_finished_flushing(conn) < 0) { /* ...and get handled here. */
      /* already marked */
Roger Dingledine's avatar
Roger Dingledine committed
836
      return -1;
837
    }
Roger Dingledine's avatar
Roger Dingledine committed
838
839

  return 0;
840
841
}

842
843
844
845
/** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
 * outbuf, and ask it to start writing. If it's an OR conn, and an
 * entire TLS record is ready, then try to flush it now.
 */
846
void connection_write_to_buf(const char *string, int len, connection_t *conn) {
847

848
849
  if(!len || conn->marked_for_close)
    return;
850

851
  if(write_to_buf(string, len, conn->outbuf) < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
852
    log_fn(LOG_WARN,"write_to_buf failed. Closing connection (fd %d).", conn->s);
853
    connection_mark_for_close(conn,0);
854
    return;
855
  }
856
857

  connection_start_writing(conn);
858
#define MIN_TLS_FLUSHLEN 15872
859
860
861
862
/* openssl tls record size is 16383, this is close. The goal here is to
 * push data out as soon as we know there's enough for a tls record, so
 * during periods of high load we won't read the entire megabyte from
 * input before pushing any data out. */
863
864
  if(connection_speaks_cells(conn) &&
     conn->outbuf_flushlen < MIN_TLS_FLUSHLEN &&
865
866
867
868
869
     conn->outbuf_flushlen+len >= MIN_TLS_FLUSHLEN) {
    len -= (MIN_TLS_FLUSHLEN - conn->outbuf_flushlen);
    conn->outbuf_flushlen = MIN_TLS_FLUSHLEN;
    if(connection_handle_write(conn) < 0) {
      log_fn(LOG_WARN,"flushing failed.");
870
      return;
871
872
    }
  }
873
874
875
876
877
  if(len > 0) { /* if there's any left over */
    conn->outbuf_flushlen += len;
    connection_start_writing(conn);
    /* because connection_handle_write() above might have stopped writing */
  }
Roger Dingledine's avatar
Roger Dingledine committed
878
879
}

880
881
/** Return the conn to addr/port that has the most recent
 * timestamp_created, or NULL if no such conn exists. */
882
883
connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port) {
  int i, n;
884
  connection_t *conn, *best=NULL;
885
  connection_t **carray;
Roger Dingledine's avatar
Roger Dingledine committed
886

887
888
889
  get_connection_array(&carray,&n);
  for(i=0;i<n;i++) {
    conn = carray[i];
890
891
892
    if(conn->addr == addr && conn->port == port && !conn->marked_for_close &&
       (!best || best->timestamp_created < conn->timestamp_created))
      best = conn;
893
  }
894
  return best;
895
896
}

897
898
/** Find a connection to the router described by addr and port,
 * or alternately any router with the same identity key.
Nick Mathewson's avatar
Nick Mathewson committed
899
 * This connection <em>must</em> be in an "open" state.
900
901
902
903
 * If not, return NULL.
 */
/* XXX this twin thing is busted, now that we're rotating onion
 * keys. abandon/patch? */
904
905
906
907
908
909
910
911
912
connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port) {
  int i, n;
  connection_t *conn;
  routerinfo_t *router;
  connection_t **carray;

  /* first check if it's there exactly */
  conn = connection_exact_get_by_addr_port(addr,port);
  if(conn && connection_state_is_open(conn)) {
913
    log(LOG_DEBUG,"connection_twin_get_by_addr_port(): Found exact match.");
914
915
    return conn;
  }
Roger Dingledine's avatar
Roger Dingledine committed
916

917
  /* now check if any of the other open connections are a twin for this one */
Roger Dingledine's avatar
Roger Dingledine committed
918

919
920
921
  router = router_get_by_addr_port(addr,port);
  if(!router)
    return NULL;
Roger Dingledine's avatar
Roger Dingledine committed
922

923
924
925
  get_connection_array(&carray,&n);
  for(i=0;i<n;i++) {
    conn = carray[i];
Roger Dingledine's avatar
Roger Dingledine committed
926
    tor_assert(conn);
927
    if(connection_state_is_open(conn) &&
928
       !crypto_pk_cmp_keys(conn->identity_pkey, router->identity_pkey)) {
929
      log(LOG_DEBUG,"connection_twin_get_by_addr_port(): Found twin (%s).",conn->address);
930
931
932
933
934
935
      return conn;
    }
  }
  return NULL;
}

936
937
938
/** Return a connection of type <b>type</b> that is not marked for
 * close.
 */
939
940
941
942
connection_t *connection_get_by_type(int type) {
  int i, n;
  connection_t *conn;
  connection_t **carray;
Roger Dingledine's avatar
Roger Dingledine committed
943

944
945
946
947
948
949
950
951
952
  get_connection_array(&carray,&n);
  for(i=0;i<n;i++) {
    conn = carray[i];
    if(conn->type == type && !conn->marked_for_close)
      return conn;
  }
  return NULL;
}

953
954
955
/** Return a connection of type <b>type</b> that is in state <b>state</b>,
 * and that is not marked for close.
 */
956
957
958
959
connection_t *connection_get_by_type_state(int type, int state) {
  int i, n;
  connection_t *conn;
  connection_t **carray;
Roger Dingledine's avatar
Roger Dingledine committed
960

961
  get_connection_array(&carray,&n);
962
963
964
965
966
967
968
969
  for(i=0;i<n;i++) {
    conn = carray[i];
    if(conn->type == type && conn->state == state && !conn->marked_for_close)
      return conn;
  }
  return NULL;
}

970
971
972
973
/** Return the connection of type <b>type</b> that is in state
 * <b>state</b>, that was written to least recently, and that is not
 * marked for close.
 */
974
975
976
977
connection_t *connection_get_by_type_state_lastwritten(int type, int state) {
  int i, n;
  connection_t *conn, *best=NULL;
  connection_t **carray;
Roger Dingledine's avatar
Roger Dingledine committed
978

979
  get_connection_array(&carray,&n);
980
981
982
983
984
985
986
987
988
  for(i=0;i<n;i++) {
    conn = carray[i];
    if(conn->type == type && conn->state == state && !conn->marked_for_close)
      if(!best || conn->timestamp_lastwritten < best->timestamp_lastwritten)
        best = conn;
  }
  return best;
}

989
990
991
/** Return a connection of type <b>type</b> that has rendquery equal
 * to <b>rendquery</b>, and that is not marked for close.
 */
992
connection_t *connection_get_by_type_rendquery(int type, const char *rendquery) {
993
994
995
996
997
998
999
1000
  int i, n;
  connection_t *conn;
  connection_t **carray;

  get_connection_array(&carray,&n);
  for(i=0;i<n;i++) {
    conn = carray[i];
    if(conn->type == type &&