or.h 196 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-2008, The Tor Project, Inc. */
5
/* See LICENSE for licensing information */
Roger Dingledine's avatar
Roger Dingledine committed
6
7
/* $Id$ */

Nick Mathewson's avatar
Nick Mathewson committed
8
9
10
/**
 * \file or.h
 * \brief Master header file for Tor-specific functionality.
11
 **/
Nick Mathewson's avatar
Nick Mathewson committed
12

Roger Dingledine's avatar
Roger Dingledine committed
13
14
#ifndef __OR_H
#define __OR_H
15
#define OR_H_ID "$Id$"
Roger Dingledine's avatar
Roger Dingledine committed
16

Nick Mathewson's avatar
Nick Mathewson committed
17
#include "orconfig.h"
18
19
20
21
22
#ifdef MS_WINDOWS
#define WIN32_WINNT 0x400
#define _WIN32_WINNT 0x400
#define WIN32_LEAN_AND_MEAN
#endif
Nick Mathewson's avatar
Nick Mathewson committed
23

24
#ifdef HAVE_UNISTD_H
Roger Dingledine's avatar
Roger Dingledine committed
25
#include <unistd.h>
26
27
#endif
#ifdef HAVE_SIGNAL_H
Roger Dingledine's avatar
Roger Dingledine committed
28
#include <signal.h>
29
30
#endif
#ifdef HAVE_NETDB_H
Roger Dingledine's avatar
Roger Dingledine committed
31
#include <netdb.h>
32
#endif
33
34
35
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h> /* FreeBSD needs this to know what version it is */
#endif
36
#include "torint.h"
Roger Dingledine's avatar
Roger Dingledine committed
37
38
39
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
40
#ifdef HAVE_SYS_FCNTL_H
Roger Dingledine's avatar
Roger Dingledine committed
41
#include <sys/fcntl.h>
42
43
44
45
46
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
Roger Dingledine's avatar
Roger Dingledine committed
47
#include <sys/ioctl.h>
48
#endif
49
50
51
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
52
#ifdef HAVE_SYS_STAT_H
53
#include <sys/stat.h>
54
55
#endif
#ifdef HAVE_ARPA_INET_H
Roger Dingledine's avatar
Roger Dingledine committed
56
#include <arpa/inet.h>
57
58
#endif
#ifdef HAVE_ERRNO_H
Roger Dingledine's avatar
Roger Dingledine committed
59
#include <errno.h>
60
61
#endif
#ifdef HAVE_ASSERT_H
Roger Dingledine's avatar
Roger Dingledine committed
62
#include <assert.h>
63
64
#endif
#ifdef HAVE_TIME_H
65
#include <time.h>
66
#endif
67

68
#ifdef MS_WINDOWS
69
#include <io.h>
Roger Dingledine's avatar
Roger Dingledine committed
70
#include <process.h>
71
#include <direct.h>
72
#include <windows.h>
73
#define snprintf _snprintf
74
75
#endif

76
77
78
79
80
81
82
#include "crypto.h"
#include "tortls.h"
#include "log.h"
#include "compat.h"
#include "container.h"
#include "util.h"
#include "torgzip.h"
83
#include "address.h"
Roger Dingledine's avatar
Roger Dingledine committed
84

85
86
#include <event.h>

Nick Mathewson's avatar
Nick Mathewson committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* These signals are defined to help control_signal_act work.
 */
#ifndef SIGHUP
#define SIGHUP 1
#endif
#ifndef SIGINT
#define SIGINT 2
#endif
#ifndef SIGUSR1
#define SIGUSR1 10
#endif
#ifndef SIGUSR2
#define SIGUSR2 12
#endif
#ifndef SIGTERM
#define SIGTERM 15
#endif
104
105
106
/* Controller signals start at a high number so we don't
 * conflict with system-defined signals. */
#define SIGNEWNYM 129
107
#define SIGCLEARDNSCACHE 130
Nick Mathewson's avatar
Nick Mathewson committed
108

109
110
111
112
113
114
#if (SIZEOF_CELL_T != 0)
/* On Irix, stdlib.h defines a cell_t type, so we need to make sure
 * that our stuff always calls cell_t something different. */
#define cell_t tor_cell_t
#endif

115
/** Length of longest allowable configured nickname. */
116
#define MAX_NICKNAME_LEN 19
117
118
/** Length of a router identity encoded as a hexadecimal digest, plus
 * possible dollar sign. */
119
#define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
Roger Dingledine's avatar
Roger Dingledine committed
120
121
/** Maximum length of verbose router identifier: dollar sign, hex ID digest,
 * equal sign or tilde, nickname. */
122
123
#define MAX_VERBOSE_NICKNAME_LEN (1+HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN)

124
/** Maximum size, in bytes, for resized buffers. */
125
#define MAX_BUF_SIZE ((1<<24)-1) /* 16MB-1 */
Roger Dingledine's avatar
Roger Dingledine committed
126
/** Maximum size, in bytes, for any directory object that we've downloaded. */
127
#define MAX_DIR_DL_SIZE MAX_BUF_SIZE
Roger Dingledine's avatar
Roger Dingledine committed
128

129
/** For http parsing: Maximum number of bytes we'll accept in the headers
Roger Dingledine's avatar
Roger Dingledine committed
130
 * of an HTTP request or response. */
131
#define MAX_HEADERS_SIZE 50000
132
133
134
/** Maximum size, in bytes, for any directory object that we're accepting
 * as an upload. */
#define MAX_DIR_UL_SIZE 500000
135

136
137
138
139
140
141
142
143
/** Maximum size, in bytes, of a single router descriptor uploaded to us
 * as a directory authority. Caches and clients fetch whatever descriptors
 * the authorities tell them to fetch, and don't care about size. */
#define MAX_DESCRIPTOR_UPLOAD_SIZE 20000

/** Maximum size of a single extrainfo document, as above. */
#define MAX_EXTRAINFO_UPLOAD_SIZE 50000

144
145
/** How long do we keep DNS cache entries before purging them (regardless of
 * their TTL)? */
146
#define MAX_DNS_ENTRY_AGE (30*60)
147
148
/** How long do we cache/tell clients to cache DNS records when no TTL is
 * known? */
149
150
151
#define DEFAULT_DNS_TTL (30*60)
/** How long can a TTL be before we stop believing it? */
#define MAX_DNS_TTL (3*60*60)
152
153
/** How small can a TTL be before we stop believing it?  Provides rudimentary
 * pinning. */
154
#define MIN_DNS_TTL (60)
155

Nick Mathewson's avatar
Nick Mathewson committed
156
/** How often do we rotate onion keys? */
157
#define MIN_ONION_KEY_LIFETIME (7*24*60*60)
Nick Mathewson's avatar
Nick Mathewson committed
158
/** How often do we rotate TLS contexts? */
159
#define MAX_SSL_KEY_LIFETIME (2*60*60)
160

Roger Dingledine's avatar
Roger Dingledine committed
161
162
/** How old do we allow a router to get before removing it
 * from the router list? In seconds. */
163
164
165
166
#define ROUTER_MAX_AGE (60*60*48)
/** How old can a router get before we (as a server) will no longer
 * consider it live? In seconds. */
#define ROUTER_MAX_AGE_TO_PUBLISH (60*60*20)
167
168
/** How old do we let a saved descriptor get before force-removing it? */
#define OLD_ROUTER_DESC_MAX_AGE (60*60*24*5)
169

170
/** Possible rules for generating circuit IDs on an OR connection. */
171
typedef enum {
172
  CIRC_ID_TYPE_LOWER=0, /**< Pick from 0..1<<15-1. */
Roger Dingledine's avatar
Roger Dingledine committed
173
  CIRC_ID_TYPE_HIGHER=1, /**< Pick from 1<<15..1<<16-1. */
174
175
  /** The other side of a connection is an OP: never create circuits to it,
   * and let it use any circuit ID it wants. */
176
  CIRC_ID_TYPE_NEITHER=2
177
} circ_id_type_t;
178

179
#define _CONN_TYPE_MIN 3
Nick Mathewson's avatar
Nick Mathewson committed
180
/** Type for sockets listening for OR connections. */
Roger Dingledine's avatar
Roger Dingledine committed
181
#define CONN_TYPE_OR_LISTENER 3
182
183
/** A bidirectional TLS connection transmitting a sequence of cells.
 * May be from an OR to an OR, or from an OP to an OR. */
Roger Dingledine's avatar
Roger Dingledine committed
184
#define CONN_TYPE_OR 4
185
/** A TCP connection from an onion router to a stream's destination. */
186
#define CONN_TYPE_EXIT 5
187
/** Type for sockets listening for SOCKS connections. */
188
#define CONN_TYPE_AP_LISTENER 6
189
190
/** A SOCKS proxy connection from the user application to the onion
 * proxy. */
191
#define CONN_TYPE_AP 7
192
/** Type for sockets listening for HTTP connections to the directory server. */
193
#define CONN_TYPE_DIR_LISTENER 8
194
/** Type for HTTP connections to the directory server. */
195
#define CONN_TYPE_DIR 9
196
/** Connection from the main process to a CPU worker process. */
197
#define CONN_TYPE_CPUWORKER 10
Roger Dingledine's avatar
Roger Dingledine committed
198
/** Type for listening for connections from user interface process. */
199
#define CONN_TYPE_CONTROL_LISTENER 11
Roger Dingledine's avatar
Roger Dingledine committed
200
/** Type for connections from user interface process. */
201
#define CONN_TYPE_CONTROL 12
202
203
/** Type for sockets listening for transparent connections redirected by pf or
 * netfilter. */
204
#define CONN_TYPE_AP_TRANS_LISTENER 13
205
206
/** Type for sockets listening for transparent connections redirected by
 * natd. */
207
#define CONN_TYPE_AP_NATD_LISTENER 14
208
209
210
/** Type for sockets listening for DNS requests. */
#define CONN_TYPE_AP_DNS_LISTENER 15
#define _CONN_TYPE_MAX 15
211
212
/* !!!! If _CONN_TYPE_MAX is ever over 15, we must grow the type field in
 * connection_t. */
Roger Dingledine's avatar
Roger Dingledine committed
213

214
215
#define CONN_IS_EDGE(x) \
  ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
216

217
/** State for any listener connection. */
Roger Dingledine's avatar
Roger Dingledine committed
218
219
#define LISTENER_STATE_READY 0

220
#define _CPUWORKER_STATE_MIN 1
221
/** State for a connection to a cpuworker process that's idle. */
222
#define CPUWORKER_STATE_IDLE 1
Nick Mathewson's avatar
Nick Mathewson committed
223
/** State for a connection to a cpuworker process that's processing a
224
 * handshake. */
225
#define CPUWORKER_STATE_BUSY_ONION 2
Nick Mathewson's avatar
Nick Mathewson committed
226
#define _CPUWORKER_STATE_MAX 2
Roger Dingledine's avatar
Roger Dingledine committed
227
228
229

#define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION

230
#define _OR_CONN_STATE_MIN 1
231
/** State for a connection to an OR: waiting for connect() to finish. */
Nick Mathewson's avatar
Nick Mathewson committed
232
#define OR_CONN_STATE_CONNECTING 1
233
234
235
236
/** State for a connection to an OR: waiting for proxy command to flush. */
#define OR_CONN_STATE_PROXY_FLUSHING 2
/** State for a connection to an OR: waiting for proxy response. */
#define OR_CONN_STATE_PROXY_READING 3
237
238
/** State for a connection to an OR or client: SSL is handshaking, not done
 * yet. */
239
#define OR_CONN_STATE_TLS_HANDSHAKING 4
240
241
/** State for a connection to an OR: We're doing a second SSL handshake for
 * renegotiation purposes. */
242
#define OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING 5
243
244
/** State for a connection at an OR: We're waiting for the client to
 * renegotiate. */
245
#define OR_CONN_STATE_TLS_SERVER_RENEGOTIATING 6
246
/** State for a connection to an OR: We're done with our SSL handshake, but we
247
 * haven't yet negotiated link protocol versions and sent a netinfo cell.
248
 */
249
#define OR_CONN_STATE_OR_HANDSHAKING 7
Nick Mathewson's avatar
Nick Mathewson committed
250
/** State for a connection to an OR: Ready to send/receive cells. */
251
252
#define OR_CONN_STATE_OPEN 8
#define _OR_CONN_STATE_MAX 8
253
254

#define _EXIT_CONN_STATE_MIN 1
255
/** State for an exit connection: waiting for response from dns farm. */
Nick Mathewson's avatar
Nick Mathewson committed
256
#define EXIT_CONN_STATE_RESOLVING 1
257
/** State for an exit connection: waiting for connect() to finish. */
Nick Mathewson's avatar
Nick Mathewson committed
258
#define EXIT_CONN_STATE_CONNECTING 2
259
/** State for an exit connection: open and ready to transmit data. */
260
#define EXIT_CONN_STATE_OPEN 3
261
/** State for an exit connection: waiting to be removed. */
Nick Mathewson's avatar
Nick Mathewson committed
262
#define EXIT_CONN_STATE_RESOLVEFAILED 4
263
#define _EXIT_CONN_STATE_MAX 4
Roger Dingledine's avatar
Roger Dingledine committed
264

Roger Dingledine's avatar
Roger Dingledine committed
265
/* The AP state values must be disjoint from the EXIT state values. */
266
#define _AP_CONN_STATE_MIN 5
267
/** State for a SOCKS connection: waiting for SOCKS request. */
268
#define AP_CONN_STATE_SOCKS_WAIT 5
Nick Mathewson's avatar
Nick Mathewson committed
269
/** State for a SOCKS connection: got a y.onion URL; waiting to receive
Roger Dingledine's avatar
Roger Dingledine committed
270
 * rendezvous descriptor. */
271
#define AP_CONN_STATE_RENDDESC_WAIT 6
272
273
274
/** The controller will attach this connection to a circuit; it isn't our
 * job to do so. */
#define AP_CONN_STATE_CONTROLLER_WAIT 7
275
/** State for a SOCKS connection: waiting for a completed circuit. */
276
#define AP_CONN_STATE_CIRCUIT_WAIT 8
277
/** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
278
#define AP_CONN_STATE_CONNECT_WAIT 9
279
/** State for a SOCKS connection: sent RESOLVE, waiting for RESOLVED. */
280
#define AP_CONN_STATE_RESOLVE_WAIT 10
281
/** State for a SOCKS connection: ready to send and receive. */
282
#define AP_CONN_STATE_OPEN 11
283
284
285
286
/** State for a transparent natd connection: waiting for original
 * destination. */
#define AP_CONN_STATE_NATD_WAIT 12
#define _AP_CONN_STATE_MAX 12
287

288
289
/** True iff the AP_CONN_STATE_* value <b>s</b> means that the corresponding
 * edge connection is not attached to any circuit. */
290
291
292
#define AP_CONN_STATE_IS_UNATTACHED(s) \
  ((s) <= AP_CONN_STATE_CIRCUIT_WAIT || (s) == AP_CONN_STATE_NATD_WAIT)

293
#define _DIR_CONN_STATE_MIN 1
294
/** State for connection to directory server: waiting for connect(). */
Roger Dingledine's avatar
Roger Dingledine committed
295
#define DIR_CONN_STATE_CONNECTING 1
296
/** State for connection to directory server: sending HTTP request. */
Roger Dingledine's avatar
Roger Dingledine committed
297
#define DIR_CONN_STATE_CLIENT_SENDING 2
298
/** State for connection to directory server: reading HTTP response. */
Roger Dingledine's avatar
Roger Dingledine committed
299
#define DIR_CONN_STATE_CLIENT_READING 3
300
301
/** State for connection to directory server: happy and finished. */
#define DIR_CONN_STATE_CLIENT_FINISHED 4
302
/** State for connection at directory server: waiting for HTTP request. */
303
#define DIR_CONN_STATE_SERVER_COMMAND_WAIT 5
304
/** State for connection at directory server: sending HTTP response. */
305
306
#define DIR_CONN_STATE_SERVER_WRITING 6
#define _DIR_CONN_STATE_MAX 6
Roger Dingledine's avatar
Roger Dingledine committed
307

308
309
/** True iff the purpose of <b>conn</b> means that it's a server-side
 * directory connection. */
310
311
#define DIR_CONN_IS_SERVER(conn) ((conn)->purpose == DIR_PURPOSE_SERVER)

312
#define _CONTROL_CONN_STATE_MIN 1
313
/** State for a control connection: Authenticated and accepting v1 commands. */
314
#define CONTROL_CONN_STATE_OPEN 1
315
316
/** State for a control connection: Waiting for authentication; speaking
 * protocol v1. */
317
318
#define CONTROL_CONN_STATE_NEEDAUTH 2
#define _CONTROL_CONN_STATE_MAX 2
319

320
#define _DIR_PURPOSE_MIN 3
321
/** A connection to a directory server: download a rendezvous
Nick Mathewson's avatar
Nick Mathewson committed
322
 * descriptor. */
323
#define DIR_PURPOSE_FETCH_RENDDESC 3
324
/** A connection to a directory server: set after a rendezvous
Nick Mathewson's avatar
Nick Mathewson committed
325
 * descriptor is downloaded. */
326
#define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
327
328
/** A connection to a directory server: download one or more v2
 * network-status objects */
329
#define DIR_PURPOSE_FETCH_NETWORKSTATUS 5
330
/** A connection to a directory server: download one or more server
331
332
 * descriptors. */
#define DIR_PURPOSE_FETCH_SERVERDESC 6
333
334
335
/** A connection to a directory server: download one or more extra-info
 * documents. */
#define DIR_PURPOSE_FETCH_EXTRAINFO 7
336
/** A connection to a directory server: upload a server descriptor. */
337
#define DIR_PURPOSE_UPLOAD_DIR 8
338
/** A connection to a directory server: upload a rendezvous
339
 * descriptor. */
340
#define DIR_PURPOSE_UPLOAD_RENDDESC 9
341
342
/** A connection to a directory server: upload a v3 networkstatus vote. */
#define DIR_PURPOSE_UPLOAD_VOTE 10
343
/** A connection to a directory server: upload a v3 consensus signature */
344
#define DIR_PURPOSE_UPLOAD_SIGNATURES 11
345
/** A connection to a directory server: download one or more network-status
346
 * objects [XXX wtf, these are all the same comment] */
347
#define DIR_PURPOSE_FETCH_STATUS_VOTE 12
348
349
/** A connection to a directory server: download one or more network-status
 * objects */
350
#define DIR_PURPOSE_FETCH_DETACHED_SIGNATURES 13
351
352
/** A connection to a directory server: download one or more network-status
 * objects */
353
#define DIR_PURPOSE_FETCH_CONSENSUS 14
354
355
/** A connection to a directory server: download one or more network-status
 * objects */
356
#define DIR_PURPOSE_FETCH_CERTIFICATE 15
357

Nick Mathewson's avatar
Nick Mathewson committed
358
/** Purpose for connection at a directory server. */
359
#define DIR_PURPOSE_SERVER 16
360
361
362
363
364
365
366
/** A connection to a hidden service directory server: upload a v2 rendezvous
 * descriptor. */
#define DIR_PURPOSE_UPLOAD_RENDDESC_V2 17
/** A connection to a hidden service directory server: download a v2 rendezvous
 * descriptor. */
#define DIR_PURPOSE_FETCH_RENDDESC_V2 18
#define _DIR_PURPOSE_MAX 18
367

368
369
370
371
372
373
#define DIR_PURPOSE_IS_UPLOAD(p)                \
  ((p)==DIR_PURPOSE_UPLOAD_DIR ||               \
   (p)==DIR_PURPOSE_UPLOAD_RENDDESC ||          \
   (p)==DIR_PURPOSE_UPLOAD_VOTE ||              \
   (p)==DIR_PURPOSE_UPLOAD_SIGNATURES)

374
#define _EXIT_PURPOSE_MIN 1
375
/** This exit stream wants to do an ordinary connect. */
376
#define EXIT_PURPOSE_CONNECT 1
377
/** This exit stream wants to do a resolve (either normal or reverse). */
378
379
380
#define EXIT_PURPOSE_RESOLVE 2
#define _EXIT_PURPOSE_MAX 2

381
/* !!!! If any connection purpose is ever over over 31, we must grow the type
382
383
 * field in connection_t. */

384
/** Circuit state: I'm the origin, still haven't done all my handshakes. */
Nick Mathewson's avatar
Nick Mathewson committed
385
#define CIRCUIT_STATE_BUILDING 0
386
/** Circuit state: Waiting to process the onionskin. */
Nick Mathewson's avatar
Nick Mathewson committed
387
#define CIRCUIT_STATE_ONIONSKIN_PENDING 1
388
389
/** Circuit state: I'd like to deliver a create, but my n_conn is still
 * connecting. */
Nick Mathewson's avatar
Nick Mathewson committed
390
#define CIRCUIT_STATE_OR_WAIT 2
391
/** Circuit state: onionskin(s) processed, ready to send/receive cells. */
Nick Mathewson's avatar
Nick Mathewson committed
392
#define CIRCUIT_STATE_OPEN 3
Roger Dingledine's avatar
Roger Dingledine committed
393

394
#define _CIRCUIT_PURPOSE_MIN 1
395

396
/* these circuits were initiated elsewhere */
397
#define _CIRCUIT_PURPOSE_OR_MIN 1
Nick Mathewson's avatar
Nick Mathewson committed
398
399
/** OR-side circuit purpose: normal circuit, at OR. */
#define CIRCUIT_PURPOSE_OR 1
400
/** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
Nick Mathewson's avatar
Nick Mathewson committed
401
#define CIRCUIT_PURPOSE_INTRO_POINT 2
402
/** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
Nick Mathewson's avatar
Nick Mathewson committed
403
#define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
404
/** OR-side circuit purpose: At OR, both circuits have this purpose. */
Nick Mathewson's avatar
Nick Mathewson committed
405
#define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
406
#define _CIRCUIT_PURPOSE_OR_MAX 4
407

408
/* these circuits originate at this node */
409
410
411
412

/* here's how circ client-side purposes work:
 *   normal circuits are C_GENERAL.
 *   circuits that are c_introducing are either on their way to
413
414
415
416
 *     becoming open, or they are open and waiting for a
 *     suitable rendcirc before they send the intro.
 *   circuits that are c_introduce_ack_wait have sent the intro,
 *     but haven't gotten a response yet.
417
418
419
 *   circuits that are c_establish_rend are either on their way
 *     to becoming open, or they are open and have sent the
 *     establish_rendezvous cell but haven't received an ack.
420
421
 *   circuits that are c_rend_ready are open and have received a
 *     rend ack, but haven't heard from bob yet. if they have a
422
423
 *     buildstate->pending_final_cpath then they're expecting a
 *     cell from bob, else they're not.
424
425
 *   circuits that are c_rend_ready_intro_acked are open, and
 *     some intro circ has sent its intro and received an ack.
426
427
428
 *   circuits that are c_rend_joined are open, have heard from
 *     bob, and are talking to him.
 */
Nick Mathewson's avatar
Nick Mathewson committed
429
430
/** Client-side circuit purpose: Normal circuit, with cpath. */
#define CIRCUIT_PURPOSE_C_GENERAL 5
431
/** Client-side circuit purpose: at Alice, connecting to intro point. */
Nick Mathewson's avatar
Nick Mathewson committed
432
#define CIRCUIT_PURPOSE_C_INTRODUCING 6
433
434
/** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point,
 * waiting for ACK/NAK. */
Nick Mathewson's avatar
Nick Mathewson committed
435
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
436
/** Client-side circuit purpose: at Alice, introduced and acked, closing. */
Nick Mathewson's avatar
Nick Mathewson committed
437
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
438
/** Client-side circuit purpose: at Alice, waiting for ack. */
Nick Mathewson's avatar
Nick Mathewson committed
439
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
440
/** Client-side circuit purpose: at Alice, waiting for Bob. */
Nick Mathewson's avatar
Nick Mathewson committed
441
442
443
444
#define CIRCUIT_PURPOSE_C_REND_READY 10
/** Client-side circuit purpose: at Alice, waiting for Bob, INTRODUCE
 * has been acknowledged. */
#define CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED 11
445
/** Client-side circuit purpose: at Alice, rendezvous established. */
Nick Mathewson's avatar
Nick Mathewson committed
446
447
#define CIRCUIT_PURPOSE_C_REND_JOINED 12

448
/** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
Nick Mathewson's avatar
Nick Mathewson committed
449
450
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
/** Hidden-service-side circuit purpose: at Bob, successfully established
451
 * intro. */
Nick Mathewson's avatar
Nick Mathewson committed
452
#define CIRCUIT_PURPOSE_S_INTRO 14
453
/** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
Nick Mathewson's avatar
Nick Mathewson committed
454
#define CIRCUIT_PURPOSE_S_CONNECT_REND 15
455
/** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
Nick Mathewson's avatar
Nick Mathewson committed
456
#define CIRCUIT_PURPOSE_S_REND_JOINED 16
457
458
/** A testing circuit; not meant to be used for actual traffic. */
#define CIRCUIT_PURPOSE_TESTING 17
459
460
461
/** A controller made this circuit and Tor should not use it. */
#define CIRCUIT_PURPOSE_CONTROLLER 18
#define _CIRCUIT_PURPOSE_MAX 18
462
463
464
/** A catch-all for unrecognized purposes. Currently we don't expect
 * to make or see any circuits with this purpose. */
#define CIRCUIT_PURPOSE_UNKNOWN 255
465

466
467
/** True iff the circuit purpose <b>p</b> is for a circuit that
 * originated at this node. */
468
469
470
#define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
#define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))

471
472
473
474
/** How many circuits do we want simultaneously in-progress to handle
 * a given stream? */
#define MIN_CIRCUITS_HANDLING_STREAM 2

475
476
477
478
479
#define RELAY_COMMAND_BEGIN 1
#define RELAY_COMMAND_DATA 2
#define RELAY_COMMAND_END 3
#define RELAY_COMMAND_CONNECTED 4
#define RELAY_COMMAND_SENDME 5
480
481
#define RELAY_COMMAND_EXTEND 6
#define RELAY_COMMAND_EXTENDED 7
482
483
#define RELAY_COMMAND_TRUNCATE 8
#define RELAY_COMMAND_TRUNCATED 9
484
#define RELAY_COMMAND_DROP 10
485
486
#define RELAY_COMMAND_RESOLVE 11
#define RELAY_COMMAND_RESOLVED 12
487
#define RELAY_COMMAND_BEGIN_DIR 13
488

489
490
491
492
#define RELAY_COMMAND_ESTABLISH_INTRO 32
#define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
#define RELAY_COMMAND_INTRODUCE1 34
#define RELAY_COMMAND_INTRODUCE2 35
493
494
495
496
497
#define RELAY_COMMAND_RENDEZVOUS1 36
#define RELAY_COMMAND_RENDEZVOUS2 37
#define RELAY_COMMAND_INTRO_ESTABLISHED 38
#define RELAY_COMMAND_RENDEZVOUS_ESTABLISHED 39
#define RELAY_COMMAND_INTRODUCE_ACK 40
498

499
/* Reasons why an OR connection is closed */
500
501
502
503
504
505
506
507
508
#define END_OR_CONN_REASON_DONE           1
#define END_OR_CONN_REASON_REFUSED        2 /* connection refused */
#define END_OR_CONN_REASON_OR_IDENTITY    3
#define END_OR_CONN_REASON_CONNRESET      4 /* connection reset by peer */
#define END_OR_CONN_REASON_TIMEOUT        5
#define END_OR_CONN_REASON_NO_ROUTE       6 /* no route to host/net */
#define END_OR_CONN_REASON_IO_ERROR       7 /* read/write error */
#define END_OR_CONN_REASON_RESOURCE_LIMIT 8 /* sockets, buffers, etc */
#define END_OR_CONN_REASON_MISC           9
509

510
511
/* Reasons why we (or a remote OR) might close a stream. See tor-spec.txt for
 * documentation of these. */
512
513
#define END_STREAM_REASON_MISC 1
#define END_STREAM_REASON_RESOLVEFAILED 2
514
#define END_STREAM_REASON_CONNECTREFUSED 3
515
516
517
#define END_STREAM_REASON_EXITPOLICY 4
#define END_STREAM_REASON_DESTROY 5
#define END_STREAM_REASON_DONE 6
518
#define END_STREAM_REASON_TIMEOUT 7
519
/* 8 is unallocated for historical reasons. */
520
521
522
523
#define END_STREAM_REASON_HIBERNATING 9
#define END_STREAM_REASON_INTERNAL 10
#define END_STREAM_REASON_RESOURCELIMIT 11
#define END_STREAM_REASON_CONNRESET 12
524
#define END_STREAM_REASON_TORPROTOCOL 13
525
#define END_STREAM_REASON_NOTDIRECTORY 14
526
#define END_STREAM_REASON_ENTRYPOLICY 15
527

528
529
530
531
/* These high-numbered end reasons are not part of the official spec,
 * and are not intended to be put in relay end cells. They are here
 * to be more informative when sending back socks replies to the
 * application. */
532
/* XXXX 256 is no longer used; feel free to reuse it. */
533
534
/** We were unable to attach the connection to any circuit at all. */
/* XXXX the ways we use this one don't make a lot of sense. */
535
#define END_STREAM_REASON_CANT_ATTACH 257
536
537
/** We can't connect to any directories at all, so we killed our streams
 * before they can time out. */
538
#define END_STREAM_REASON_NET_UNREACHABLE 258
539
540
/** This is a SOCKS connection, and the client used (or misused) the SOCKS
 * protocol in a way we couldn't handle. */
541
#define END_STREAM_REASON_SOCKSPROTOCOL 259
542
543
/** This is a transparent proxy connection, but we can't extract the original
 * target address:port. */
544
#define END_STREAM_REASON_CANT_FETCH_ORIG_DEST 260
545
/** This is a connection on the NATD port, and the destination IP:Port was
Roger Dingledine's avatar
Roger Dingledine committed
546
 * either ill-formed or out-of-range. */
547
#define END_STREAM_REASON_INVALID_NATD_DEST 261
548

549
550
551
/** Bitwise-and this value with endreason to mask out all flags. */
#define END_STREAM_REASON_MASK 511

552
553
/** Bitwise-or this with the argument to control_event_stream_status
 * to indicate that the reason came from an END cell. */
554
555
556
557
#define END_STREAM_REASON_FLAG_REMOTE 512
/** Bitwise-or this with the argument to control_event_stream_status
 * to indicate that we already sent a CLOSED stream event. */
#define END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED 1024
558
559
560
561
/** Bitwise-or this with endreason to indicate that we already sent
 * a socks reply, and no further reply needs to be sent from
 * connection_mark_unattached_ap(). */
#define END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED 2048
562

563
564
565
566
567
568
569
/** Reason for remapping an AP connection's address: we have a cached
 * answer. */
#define REMAP_STREAM_SOURCE_CACHE 1
/** Reason for remapping an AP connection's address: the exit node told us an
 * answer. */
#define REMAP_STREAM_SOURCE_EXIT 2

Roger Dingledine's avatar
Roger Dingledine committed
570
/* 'type' values to use in RESOLVED cells.  Specified in tor-spec.txt. */
571
#define RESOLVED_TYPE_HOSTNAME 0
572
573
574
575
576
#define RESOLVED_TYPE_IPV4 4
#define RESOLVED_TYPE_IPV6 6
#define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
#define RESOLVED_TYPE_ERROR 0xF1

577
578
579
/* Negative reasons are internal: we never send them in a DESTROY or TRUNCATE
 * call; they only go to the controller for tracking  */
/** We couldn't build a path for this circuit. */
580
#define END_CIRC_REASON_NOPATH          -2
581
/** Catch-all "other" reason for closing origin circuits. */
582
#define END_CIRC_AT_ORIGIN              -1
583

584
585
/* Reasons why we (or a remote OR) might close a circuit. See tor-spec.txt for
 * documentation of these. */
586
587
588
589
590
591
592
593
594
595
#define _END_CIRC_REASON_MIN            0
#define END_CIRC_REASON_NONE            0
#define END_CIRC_REASON_TORPROTOCOL     1
#define END_CIRC_REASON_INTERNAL        2
#define END_CIRC_REASON_REQUESTED       3
#define END_CIRC_REASON_HIBERNATING     4
#define END_CIRC_REASON_RESOURCELIMIT   5
#define END_CIRC_REASON_CONNECTFAILED   6
#define END_CIRC_REASON_OR_IDENTITY     7
#define END_CIRC_REASON_OR_CONN_CLOSED  8
596
597
598
599
600
#define END_CIRC_REASON_FINISHED        9
#define END_CIRC_REASON_TIMEOUT         10
#define END_CIRC_REASON_DESTROYED       11
#define END_CIRC_REASON_NOSUCHSERVICE   12
#define _END_CIRC_REASON_MAX            12
601

Roger Dingledine's avatar
Roger Dingledine committed
602
603
604
/** Bitwise-OR this with the argument to circuit_mark_for_close() or
 * control_event_circuit_status() to indicate that the reason was
 * passed through from a destroy or truncate cell. */
605
606
#define END_CIRC_REASON_FLAG_REMOTE     512

Nick Mathewson's avatar
Nick Mathewson committed
607
/** Length of 'y' portion of 'y.onion' URL. */
608
609
#define REND_SERVICE_ID_LEN_BASE32 16

610
611
612
/** Length of 'y.onion' including '.onion' URL. */
#define REND_SERVICE_ADDRESS_LEN (16+1+5)

613
614
/** Length of a binary-encoded rendezvous service ID. */
#define REND_SERVICE_ID_LEN 10
615

616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/** Time period for which a v2 descriptor will be valid. */
#define REND_TIME_PERIOD_V2_DESC_VALIDITY (24*60*60)

/** Time period within which two sets of v2 descriptors will be uploaded in
 * parallel. */
#define REND_TIME_PERIOD_OVERLAPPING_V2_DESCS (60*60)

/** Number of non-consecutive replicas (i.e. distributed somewhere
 * in the ring) for a descriptor. */
#define REND_NUMBER_OF_NON_CONSECUTIVE_REPLICAS 2

/** Number of consecutive replicas for a descriptor. */
#define REND_NUMBER_OF_CONSECUTIVE_REPLICAS 3

/** Length of v2 descriptor ID (32 base32 chars = 160 bits). */
631
#define REND_DESC_ID_V2_LEN_BASE32 32
632

633
634
635
636
637
638
639
640
/** Length of the base32-encoded secret ID part of versioned hidden service
 * descriptors. */
#define REND_SECRET_ID_PART_LEN_BASE32 32

/** Length of the base32-encoded hash of an introduction point's
 * identity key. */
#define REND_INTRO_POINT_ID_LEN_BASE32 32

641
642
643
644
645
646
647
648
/** Length of the descriptor cookie that is used for client authorization
 * to hidden services. */
#define REND_DESC_COOKIE_LEN 16

/** Length of the base64-encoded descriptor cookie that is used for
 * exchanging client authorization between hidden service and client. */
#define REND_DESC_COOKIE_LEN_BASE64 22

649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/** Length of client identifier in encrypted introduction points for hidden
 * service authorization type 'basic'. */
#define REND_BASIC_AUTH_CLIENT_ID_LEN 4

/** Multiple of the number of clients to which the real number of clients
 * is padded with fake clients for hidden service authorization type
 * 'basic'. */
#define REND_BASIC_AUTH_CLIENT_MULTIPLE 16

/** Length of client entry consisting of client identifier and encrypted
 * session key for hidden service authorization type 'basic'. */
#define REND_BASIC_AUTH_CLIENT_ENTRY_LEN (REND_BASIC_AUTH_CLIENT_ID_LEN \
                                          + CIPHER_KEY_LEN)

/** Maximum size of v2 hidden service descriptors. */
#define REND_DESC_MAX_SIZE (20 * 1024)

666
667
668
669
670
/** Legal characters for use in authorized client names for a hidden
 * service. */
#define REND_LEGAL_CLIENTNAME_CHARACTERS \
  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-_"

Nick Mathewson's avatar
Nick Mathewson committed
671
672
673
/** Maximum length of authorized client names for a hidden service. */
#define REND_CLIENTNAME_MAX_LEN 16

674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/** Length of the rendezvous cookie that is used to connect circuits at the
 * rendezvous point. */
#define REND_COOKIE_LEN DIGEST_LEN

/** Client authorization type that a hidden service performs. */
typedef enum rend_auth_type_t {
  REND_NO_AUTH      = 0,
  REND_BASIC_AUTH   = 1,
  REND_STEALTH_AUTH = 2,
} rend_auth_type_t;

/** Client-side configuration of authorization for a hidden service. */
typedef struct rend_service_authorization_t {
  char descriptor_cookie[REND_DESC_COOKIE_LEN];
  char onion_address[REND_SERVICE_ADDRESS_LEN+1];
  rend_auth_type_t auth_type;
} rend_service_authorization_t;

/** Client- and server-side data that is used for hidden service connection
 * establishment. Not all fields contain data depending on where this struct
 * is used. */
typedef struct rend_data_t {
  /** Onion address (without the .onion part) that a client requests. */
  char onion_address[REND_SERVICE_ID_LEN_BASE32+1];

  /** (Optional) descriptor cookie that is used by a client. */
  char descriptor_cookie[REND_DESC_COOKIE_LEN];

  /** Authorization type for accessing a service used by a client. */
  rend_auth_type_t auth_type;

  /** Hash of the hidden service's PK used by a service. */
  char rend_pk_digest[DIGEST_LEN];

  /** Rendezvous cookie used by both, client and service. */
  char rend_cookie[REND_COOKIE_LEN];

  /** Rendezvous descriptor version that is used by a service. Used to
   * distinguish introduction and rendezvous points belonging to the same
   * rendezvous service ID, but different descriptor versions.
   */
  uint8_t rend_desc_version;
} rend_data_t;

/** Time interval for tracking possible replays of INTRODUCE2 cells.
 * Incoming cells with timestamps half of this interval in the past or
 * future are dropped immediately. */
#define REND_REPLAY_TIME_INTERVAL (60 * 60)

723
724
725
#define CELL_DIRECTION_IN 1
#define CELL_DIRECTION_OUT 2

726
727
/** Initial value for both sides of a circuit transmission window when the
 * circuit is initialized.  Measured in cells. */
728
#define CIRCWINDOW_START 1000
729
/** Amount to increment a circuit window when we get a circuit SENDME. */
730
#define CIRCWINDOW_INCREMENT 100
731
732
/** Initial value on both sides of a stream transmission window when the
 * stream is initialized.  Measured in cells. */
733
#define STREAMWINDOW_START 500
734
/** Amount to increment a stream window when we get a stream SENDME. */
735
#define STREAMWINDOW_INCREMENT 50
736

Roger Dingledine's avatar
Roger Dingledine committed
737
738
739
/* cell commands */
#define CELL_PADDING 0
#define CELL_CREATE 1
740
741
742
#define CELL_CREATED 2
#define CELL_RELAY 3
#define CELL_DESTROY 4
743
744
#define CELL_CREATE_FAST 5
#define CELL_CREATED_FAST 6
745
746
#define CELL_VERSIONS 7
#define CELL_NETINFO 8
747
#define CELL_RELAY_EARLY 9
Roger Dingledine's avatar
Roger Dingledine committed
748

749
#define CELL_COMMAND_IS_VAR_LENGTH(x) ((x) == CELL_VERSIONS)
750

751
/** How long to test reachability before complaining to the user. */
752
#define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60)
753

754
/** Legal characters in a nickname. */
755
756
#define LEGAL_NICKNAME_CHARACTERS \
  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
757

Roger Dingledine's avatar
Roger Dingledine committed
758
759
/** Name to use in client TLS certificates if no nickname is given. Once
 * Tor 0.1.2.x is obsolete, we can remove this. */
760
761
#define DEFAULT_CLIENT_NICKNAME "client"

762
/** Number of bytes in a SOCKS4 header. */
763
764
#define SOCKS4_NETWORK_LEN 8

Roger Dingledine's avatar
Roger Dingledine committed
765
766
767
/*
 * Relay payload:
 *         Relay command           [1 byte]
768
769
 *         Recognized              [2 bytes]
 *         Stream ID               [2 bytes]
Roger Dingledine's avatar
Roger Dingledine committed
770
771
 *         Partial SHA-1           [4 bytes]
 *         Length                  [2 bytes]
772
 *         Relay payload           [498 bytes]
Roger Dingledine's avatar
Roger Dingledine committed
773
 */
774

775
/** Number of bytes in a cell, minus cell header. */
Roger Dingledine's avatar
Roger Dingledine committed
776
#define CELL_PAYLOAD_SIZE 509
777
/** Number of bytes in a cell transmitted over the network. */
Roger Dingledine's avatar
Roger Dingledine committed
778
779
#define CELL_NETWORK_SIZE 512

780
781
#define VAR_CELL_HEADER_SIZE 5

782
783
/** Number of bytes in a relay cell's header (not including general cell
 * header). */
784
#define RELAY_HEADER_SIZE (1+2+2+4+2)
785
/** Largest number of bytes that can fit in a relay cell payload. */
786
787
#define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)

788
789
790
791
792
/** Identifies a circuit on an or_connection */
typedef uint16_t circid_t;
/** Identifies a stream on a circuit */
typedef uint16_t streamid_t;

793
794
/** Parsed onion routing cell.  All communication between nodes
 * is via cells. */
795
typedef struct cell_t {
796
  circid_t circ_id; /**< Circuit which received the cell. */
797
798
  uint8_t command; /**< Type of the cell: one of CELL_PADDING, CELL_CREATE,
                    * CELL_DESTROY, etc */
799
  char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
800
801
802
803
804
} cell_t;

/** Parsed variable-length onion routing cell. */
typedef struct var_cell_t {
  uint8_t command;
805
  circid_t circ_id;
806
807
808
  uint16_t payload_len;
  char payload[1];
} var_cell_t;
809

810
/** A cell as packed for writing to the network. */
811
typedef struct packed_cell_t {
Roger Dingledine's avatar
Roger Dingledine committed
812
  struct packed_cell_t *next; /**< Next cell queued on this circuit. */
813
  char body[CELL_NETWORK_SIZE]; /**< Cell as packed for network. */
814
} packed_cell_t;
815

816
817
/** A queue of cells on a circuit, waiting to be added to the
 * or_connection_t's outbuf. */
818
typedef struct cell_queue_t {
Roger Dingledine's avatar
Roger Dingledine committed
819
820
821
  packed_cell_t *head; /**< The first cell, or NULL if the queue is empty. */
  packed_cell_t *tail; /**< The last cell, or NULL if the queue is empty. */
  int n; /**< The number of cells in the queue. */
822
} cell_queue_t;
Roger Dingledine's avatar
Roger Dingledine committed
823

Nick Mathewson's avatar
Nick Mathewson committed
824
/** Beginning of a RELAY cell payload. */
825
typedef struct {
Nick Mathewson's avatar
Nick Mathewson committed
826
827
  uint8_t command; /**< The end-to-end relay command. */
  uint16_t recognized; /**< Used to tell whether cell is for us. */
828
  streamid_t stream_id; /**< Which stream is this cell associated with? */
Nick Mathewson's avatar
Nick Mathewson committed
829
830
  char integrity[4]; /**< Used to tell whether cell is corrupted. */
  uint16_t length; /**< How long is the payload body? */
831
} relay_header_t;
Roger Dingledine's avatar
Roger Dingledine committed
832

833
typedef struct buf_t buf_t;
834
typedef struct socks_request_t socks_request_t;
835

836
837
/* Values for connection_t.magic: used to make sure that downcasts (casts from
* connection_t to foo_connection_t) are safe. */
838
839
840
841
842
#define BASE_CONNECTION_MAGIC 0x7C3C304Eu
#define OR_CONNECTION_MAGIC 0x7D31FF03u
#define EDGE_CONNECTION_MAGIC 0xF0374013u
#define DIR_CONNECTION_MAGIC 0x9988ffeeu
#define CONTROL_CONNECTION_MAGIC 0x8abc765du
843

Nick Mathewson's avatar
Nick Mathewson committed
844
/** Description of a connection to another host or process, and associated
845
846
847
 * data.
 *
 * A connection is named based on what it's connected to -- an "OR
848
 * connection" has a Tor node on the other end, an "exit
849
850
851
852
853
854
855
856
857
858
859
860
861
 * connection" has a website or other server on the other end, and an
 * "AP connection" has an application proxy (and thus a user) on the
 * other end.
 *
 * Every connection has a type and a state.  Connections never change
 * their type, but can go through many state changes in their lifetime.
 *
 * Every connection has two associated input and output buffers.
 * Listeners don't use them.  For non-listener connections, incoming
 * data is appended to conn->inbuf, and outgoing data is taken from
 * conn->outbuf.  Connections differ primarily in the functions called
 * to fill and drain these buffers.
 */
862
863
864
typedef struct connection_t {
  uint32_t magic; /**< For memory debugging: must equal one of
                   * *_CONNECTION_MAGIC. */
Roger Dingledine's avatar
Roger Dingledine committed
865

Nick Mathewson's avatar
Nick Mathewson committed
866
  uint8_t state; /**< Current state of this connection. */
867
868
  unsigned int type:4; /**< What kind of connection is this? */
  unsigned int purpose:5; /**< Only used for DIR and EXIT types currently. */
869
870
871
872

  /* The next fields are all one-bit booleans. Some are only applicable to
   * connection subtypes, but we hold them here anyway, to save space.
   */
873
874
875
876
877
878
  unsigned int read_blocked_on_bw:1; /**< Boolean: should we start reading
                            * again once the bandwidth throttler allows it? */
  unsigned int write_blocked_on_bw:1; /**< Boolean: should we start writing
                             * again once the bandwidth throttler allows
                             * reads? */
  unsigned int hold_open_until_flushed:1; /**< Despite this connection's being
879
                                      * marked for close, do we flush it
Roger Dingledine's avatar
Roger Dingledine committed
880
                                      * before closing it? */
881
882
  unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
                                     * conn? */
883
  unsigned int edge_has_sent_end:1; /**< For debugging; only used on edge
884
                         * connections.  Set once we've set the stream end,
885
886
                         * and check in connection_about_to_close_connection().
                         */
887
  /** Edge connections only: true if we've blocked reading until the
888
889
   * circuit has fewer queued cells. */
  unsigned int edge_blocked_on_circ:1;
890
891
892
  /** Used for OR conns that shouldn't get any new circs attached to them,
   * because the connection is too old. */
  /* XXXX "obsolete" isn't really a good name here. */
893
  unsigned int or_is_obsolete:1;
894
  /** For AP connections only. If 1, and we fail to reach the chosen exit,
895
   * stop requiring it. */
896
  unsigned int chosen_exit_optional:1;
897
898
899
900
901
  /** For AP connections only. If non-zero, this exit node was picked as
   * a result of the TrackHostExit, and the value decrements every time
   * we fail to complete a circuit to our chosen exit -- if it reaches
   * zero, abandon the associated mapaddress. */
  unsigned int chosen_exit_retries:3;
902
903
904
  /** Set to 1 when we're inside connection_flushed_some to keep us from
   * calling connection_handle_write() recursively. */
  unsigned int in_flushed_some:1;
905

906
907
908
909
910
911
912
913
914
915
916
  /* For linked connections:
   */
  unsigned int linked:1; /**< True if there is, or has been, a linked_conn. */
  /** True iff we'd like to be notified about read events from the
   * linked conn. */
  unsigned int reading_from_linked_conn:1;
  /** True iff we're willing to write to the linked conn. */
  unsigned int writing_to_linked_conn:1;
  /** True iff we're currently able to read on the linked conn, and our
   * read_event should be made active with libevent. */
  unsigned int active_on_link:1;
Roger Dingledine's avatar
Roger Dingledine committed
917
918
  /** True iff we've called connection_close_immediate() on this linked
   * connection. */
919
  unsigned int linked_conn_is_closed:1;
920

921
  int s; /**< Our socket; -1 if this connection is closed, or has no
922
          * socket. */
923
  int conn_array_index; /**< Index into the global connection array. */
Roger Dingledine's avatar
Roger Dingledine committed
924
925
  struct event *read_event; /**< Libevent event structure. */
  struct event *write_event; /**< Libevent event structure. */
926
927
  buf_t *inbuf; /**< Buffer holding data read over this connection. */
  buf_t *outbuf; /**< Buffer holding data to write over this connection. */
Roger Dingledine's avatar
Roger Dingledine committed
928
929
  size_t outbuf_flushlen; /**< How much data should we try to flush from the
                           * outbuf? */
930
931
  time_t timestamp_lastread; /**< When was the last time libevent said we could
                              * read? */
932
  time_t timestamp_lastwritten; /**< When was the last time libevent said we
933
                                 * could write? */
Roger Dingledine's avatar
Roger Dingledine committed
934
  time_t timestamp_created; /**< When was this connection_t created? */
Roger Dingledine's avatar
Roger Dingledine committed
935

936
  /* XXXX_IP6 make this ipv6-capable */
937
938
939
  int socket_family; /**< Address family of this connection's socket.  Usually
                      * AF_INET, but it can also be AF_UNIX, or in the future
                      * AF_INET6 */
940
941
  tor_addr_t addr; /**< IP of the other side of the connection; used to
                    * identify routers, along with port. */
Roger Dingledine's avatar
Roger Dingledine committed
942
  uint16_t port; /**< If non-zero, port on the other end
943
                  * of the connection. */
944
945
946
  uint16_t marked_for_close; /**< Should we close this conn on the next
                              * iteration of the main loop? (If true, holds
                              * the line number where this connection was
Roger Dingledine's avatar
Roger Dingledine committed
947
                              * marked.) */
948
949
  const char *marked_for_close_file; /**< For debugging: in which file were
                                      * we marked for close? */