or.h 206 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.
Karsten Loesing's avatar
Karsten Loesing committed
4
 * Copyright (c) 2007-2009, The Tor Project, Inc. */
5
/* See LICENSE for licensing information */
Roger Dingledine's avatar
Roger Dingledine committed
6

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

12
13
#ifndef _TOR_OR_H
#define _TOR_OR_H
Roger Dingledine's avatar
Roger Dingledine committed
14

Nick Mathewson's avatar
Nick Mathewson committed
15
#include "orconfig.h"
16
17
18
19
20
21
22
23
24
25
26
27

#ifdef __COVERITY__
/* If we're building for a static analysis, turn on all the off-by-default
 * features. */
#ifndef INSTRUMENT_DOWNLOADS
#define INSTRUMENT_DOWNLOADS 1
#endif
#ifndef ENABLE_GEOIP_STATS
#define ENABLE_GEOIP_STATS 1
#endif
#endif

28
29
30
31
32
#ifdef MS_WINDOWS
#define WIN32_WINNT 0x400
#define _WIN32_WINNT 0x400
#define WIN32_LEAN_AND_MEAN
#endif
Nick Mathewson's avatar
Nick Mathewson committed
33

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

78
#ifdef MS_WINDOWS
79
#include <io.h>
Roger Dingledine's avatar
Roger Dingledine committed
80
#include <process.h>
81
#include <direct.h>
82
#include <windows.h>
83
#define snprintf _snprintf
84
85
#endif

86
87
88
89
90
91
92
#include "crypto.h"
#include "tortls.h"
#include "log.h"
#include "compat.h"
#include "container.h"
#include "util.h"
#include "torgzip.h"
93
#include "address.h"
94
#include "compat_libevent.h"
95

Nick Mathewson's avatar
Nick Mathewson committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* 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
113
114
115
/* Controller signals start at a high number so we don't
 * conflict with system-defined signals. */
#define SIGNEWNYM 129
116
#define SIGCLEARDNSCACHE 130
Nick Mathewson's avatar
Nick Mathewson committed
117

118
119
120
121
122
123
#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

124
/** Length of longest allowable configured nickname. */
125
#define MAX_NICKNAME_LEN 19
126
127
/** Length of a router identity encoded as a hexadecimal digest, plus
 * possible dollar sign. */
128
#define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
Roger Dingledine's avatar
Roger Dingledine committed
129
130
/** Maximum length of verbose router identifier: dollar sign, hex ID digest,
 * equal sign or tilde, nickname. */
131
132
#define MAX_VERBOSE_NICKNAME_LEN (1+HEX_DIGEST_LEN+1+MAX_NICKNAME_LEN)

133
/** Maximum size, in bytes, for resized buffers. */
134
#define MAX_BUF_SIZE ((1<<24)-1) /* 16MB-1 */
Roger Dingledine's avatar
Roger Dingledine committed
135
/** Maximum size, in bytes, for any directory object that we've downloaded. */
136
#define MAX_DIR_DL_SIZE MAX_BUF_SIZE
Roger Dingledine's avatar
Roger Dingledine committed
137

Nick Mathewson's avatar
Nick Mathewson committed
138
/** For HTTP parsing: Maximum number of bytes we'll accept in the headers
Roger Dingledine's avatar
Roger Dingledine committed
139
 * of an HTTP request or response. */
140
#define MAX_HEADERS_SIZE 50000
141
142
/** Maximum size, in bytes, for any directory object that we're accepting
 * as an upload. */
143
#define MAX_DIR_UL_SIZE MAX_BUF_SIZE
144

145
146
147
148
149
150
151
152
/** 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

153
154
/** How long do we keep DNS cache entries before purging them (regardless of
 * their TTL)? */
155
#define MAX_DNS_ENTRY_AGE (30*60)
156
157
/** How long do we cache/tell clients to cache DNS records when no TTL is
 * known? */
158
159
160
#define DEFAULT_DNS_TTL (30*60)
/** How long can a TTL be before we stop believing it? */
#define MAX_DNS_TTL (3*60*60)
161
162
/** How small can a TTL be before we stop believing it?  Provides rudimentary
 * pinning. */
163
#define MIN_DNS_TTL (60)
164

Nick Mathewson's avatar
Nick Mathewson committed
165
/** How often do we rotate onion keys? */
166
#define MIN_ONION_KEY_LIFETIME (7*24*60*60)
Nick Mathewson's avatar
Nick Mathewson committed
167
/** How often do we rotate TLS contexts? */
168
#define MAX_SSL_KEY_LIFETIME (2*60*60)
169

Roger Dingledine's avatar
Roger Dingledine committed
170
171
/** How old do we allow a router to get before removing it
 * from the router list? In seconds. */
172
173
174
175
#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)
176
177
/** How old do we let a saved descriptor get before force-removing it? */
#define OLD_ROUTER_DESC_MAX_AGE (60*60*24*5)
178

179
/** Possible rules for generating circuit IDs on an OR connection. */
180
typedef enum {
181
  CIRC_ID_TYPE_LOWER=0, /**< Pick from 0..1<<15-1. */
Roger Dingledine's avatar
Roger Dingledine committed
182
  CIRC_ID_TYPE_HIGHER=1, /**< Pick from 1<<15..1<<16-1. */
183
184
  /** The other side of a connection is an OP: never create circuits to it,
   * and let it use any circuit ID it wants. */
185
  CIRC_ID_TYPE_NEITHER=2
186
} circ_id_type_t;
187

188
#define _CONN_TYPE_MIN 3
Nick Mathewson's avatar
Nick Mathewson committed
189
/** Type for sockets listening for OR connections. */
Roger Dingledine's avatar
Roger Dingledine committed
190
#define CONN_TYPE_OR_LISTENER 3
191
192
/** 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
193
#define CONN_TYPE_OR 4
194
/** A TCP connection from an onion router to a stream's destination. */
195
#define CONN_TYPE_EXIT 5
196
/** Type for sockets listening for SOCKS connections. */
197
#define CONN_TYPE_AP_LISTENER 6
198
199
/** A SOCKS proxy connection from the user application to the onion
 * proxy. */
200
#define CONN_TYPE_AP 7
201
/** Type for sockets listening for HTTP connections to the directory server. */
202
#define CONN_TYPE_DIR_LISTENER 8
203
/** Type for HTTP connections to the directory server. */
204
#define CONN_TYPE_DIR 9
205
/** Connection from the main process to a CPU worker process. */
206
#define CONN_TYPE_CPUWORKER 10
Roger Dingledine's avatar
Roger Dingledine committed
207
/** Type for listening for connections from user interface process. */
208
#define CONN_TYPE_CONTROL_LISTENER 11
Roger Dingledine's avatar
Roger Dingledine committed
209
/** Type for connections from user interface process. */
210
#define CONN_TYPE_CONTROL 12
211
212
/** Type for sockets listening for transparent connections redirected by pf or
 * netfilter. */
213
#define CONN_TYPE_AP_TRANS_LISTENER 13
214
215
/** Type for sockets listening for transparent connections redirected by
 * natd. */
216
#define CONN_TYPE_AP_NATD_LISTENER 14
217
218
219
/** Type for sockets listening for DNS requests. */
#define CONN_TYPE_AP_DNS_LISTENER 15
#define _CONN_TYPE_MAX 15
220
221
/* !!!! If _CONN_TYPE_MAX is ever over 15, we must grow the type field in
 * connection_t. */
Roger Dingledine's avatar
Roger Dingledine committed
222

223
/** True iff <b>x</b> is an edge connection. */
224
225
#define CONN_IS_EDGE(x) \
  ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)
226

227
/** State for any listener connection. */
Roger Dingledine's avatar
Roger Dingledine committed
228
229
#define LISTENER_STATE_READY 0

230
#define _CPUWORKER_STATE_MIN 1
231
/** State for a connection to a cpuworker process that's idle. */
232
#define CPUWORKER_STATE_IDLE 1
Nick Mathewson's avatar
Nick Mathewson committed
233
/** State for a connection to a cpuworker process that's processing a
234
 * handshake. */
235
#define CPUWORKER_STATE_BUSY_ONION 2
Nick Mathewson's avatar
Nick Mathewson committed
236
#define _CPUWORKER_STATE_MAX 2
Roger Dingledine's avatar
Roger Dingledine committed
237
238
239

#define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION

240
#define _OR_CONN_STATE_MIN 1
241
/** State for a connection to an OR: waiting for connect() to finish. */
Nick Mathewson's avatar
Nick Mathewson committed
242
#define OR_CONN_STATE_CONNECTING 1
243
244
245
246
/** 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
247
248
/** State for a connection to an OR or client: SSL is handshaking, not done
 * yet. */
249
#define OR_CONN_STATE_TLS_HANDSHAKING 4
250
251
/** State for a connection to an OR: We're doing a second SSL handshake for
 * renegotiation purposes. */
252
#define OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING 5
253
254
/** State for a connection at an OR: We're waiting for the client to
 * renegotiate. */
255
#define OR_CONN_STATE_TLS_SERVER_RENEGOTIATING 6
256
/** State for a connection to an OR: We're done with our SSL handshake, but we
257
 * haven't yet negotiated link protocol versions and sent a netinfo cell.
258
 */
259
#define OR_CONN_STATE_OR_HANDSHAKING 7
Nick Mathewson's avatar
Nick Mathewson committed
260
/** State for a connection to an OR: Ready to send/receive cells. */
261
262
#define OR_CONN_STATE_OPEN 8
#define _OR_CONN_STATE_MAX 8
263
264

#define _EXIT_CONN_STATE_MIN 1
Nick Mathewson's avatar
Nick Mathewson committed
265
/** State for an exit connection: waiting for response from DNS farm. */
Nick Mathewson's avatar
Nick Mathewson committed
266
#define EXIT_CONN_STATE_RESOLVING 1
267
/** State for an exit connection: waiting for connect() to finish. */
Nick Mathewson's avatar
Nick Mathewson committed
268
#define EXIT_CONN_STATE_CONNECTING 2
269
/** State for an exit connection: open and ready to transmit data. */
270
#define EXIT_CONN_STATE_OPEN 3
271
/** State for an exit connection: waiting to be removed. */
Nick Mathewson's avatar
Nick Mathewson committed
272
#define EXIT_CONN_STATE_RESOLVEFAILED 4
273
#define _EXIT_CONN_STATE_MAX 4
Roger Dingledine's avatar
Roger Dingledine committed
274

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

298
299
/** True iff the AP_CONN_STATE_* value <b>s</b> means that the corresponding
 * edge connection is not attached to any circuit. */
300
301
302
#define AP_CONN_STATE_IS_UNATTACHED(s) \
  ((s) <= AP_CONN_STATE_CIRCUIT_WAIT || (s) == AP_CONN_STATE_NATD_WAIT)

303
#define _DIR_CONN_STATE_MIN 1
304
/** State for connection to directory server: waiting for connect(). */
Roger Dingledine's avatar
Roger Dingledine committed
305
#define DIR_CONN_STATE_CONNECTING 1
306
/** State for connection to directory server: sending HTTP request. */
Roger Dingledine's avatar
Roger Dingledine committed
307
#define DIR_CONN_STATE_CLIENT_SENDING 2
308
/** State for connection to directory server: reading HTTP response. */
Roger Dingledine's avatar
Roger Dingledine committed
309
#define DIR_CONN_STATE_CLIENT_READING 3
310
311
/** State for connection to directory server: happy and finished. */
#define DIR_CONN_STATE_CLIENT_FINISHED 4
312
/** State for connection at directory server: waiting for HTTP request. */
313
#define DIR_CONN_STATE_SERVER_COMMAND_WAIT 5
314
/** State for connection at directory server: sending HTTP response. */
315
316
#define DIR_CONN_STATE_SERVER_WRITING 6
#define _DIR_CONN_STATE_MAX 6
Roger Dingledine's avatar
Roger Dingledine committed
317

318
319
/** True iff the purpose of <b>conn</b> means that it's a server-side
 * directory connection. */
320
321
#define DIR_CONN_IS_SERVER(conn) ((conn)->purpose == DIR_PURPOSE_SERVER)

322
#define _CONTROL_CONN_STATE_MIN 1
323
/** State for a control connection: Authenticated and accepting v1 commands. */
324
#define CONTROL_CONN_STATE_OPEN 1
325
326
/** State for a control connection: Waiting for authentication; speaking
 * protocol v1. */
327
328
#define CONTROL_CONN_STATE_NEEDAUTH 2
#define _CONTROL_CONN_STATE_MAX 2
329

330
#define _DIR_PURPOSE_MIN 3
331
/** A connection to a directory server: download a rendezvous
Nick Mathewson's avatar
Nick Mathewson committed
332
 * descriptor. */
333
#define DIR_PURPOSE_FETCH_RENDDESC 3
334
/** A connection to a directory server: set after a rendezvous
Nick Mathewson's avatar
Nick Mathewson committed
335
 * descriptor is downloaded. */
336
#define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
337
338
/** A connection to a directory server: download one or more v2
 * network-status objects */
339
#define DIR_PURPOSE_FETCH_V2_NETWORKSTATUS 5
340
/** A connection to a directory server: download one or more server
341
342
 * descriptors. */
#define DIR_PURPOSE_FETCH_SERVERDESC 6
343
344
345
/** A connection to a directory server: download one or more extra-info
 * documents. */
#define DIR_PURPOSE_FETCH_EXTRAINFO 7
346
/** A connection to a directory server: upload a server descriptor. */
347
#define DIR_PURPOSE_UPLOAD_DIR 8
348
/** A connection to a directory server: upload a rendezvous
349
 * descriptor. */
350
#define DIR_PURPOSE_UPLOAD_RENDDESC 9
351
352
/** A connection to a directory server: upload a v3 networkstatus vote. */
#define DIR_PURPOSE_UPLOAD_VOTE 10
353
/** A connection to a directory server: upload a v3 consensus signature */
354
#define DIR_PURPOSE_UPLOAD_SIGNATURES 11
355
356
/** A connection to a directory server: download one or more v3 networkstatus
 * votes. */
357
#define DIR_PURPOSE_FETCH_STATUS_VOTE 12
358
359
/** A connection to a directory server: download a v3 detached signatures
 * object for a consensus. */
360
#define DIR_PURPOSE_FETCH_DETACHED_SIGNATURES 13
361
/** A connection to a directory server: download a v3 networkstatus
362
 * consensus. */
363
#define DIR_PURPOSE_FETCH_CONSENSUS 14
364
365
/** A connection to a directory server: download one or more directory
 * authority certificates. */
366
#define DIR_PURPOSE_FETCH_CERTIFICATE 15
367

Nick Mathewson's avatar
Nick Mathewson committed
368
/** Purpose for connection at a directory server. */
369
#define DIR_PURPOSE_SERVER 16
370
371
372
373
374
375
376
/** 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
377

378
379
/** True iff <b>p</b> is a purpose corresponding to uploading data to a
 * directory server. */
380
381
382
383
384
385
#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)

386
#define _EXIT_PURPOSE_MIN 1
387
/** This exit stream wants to do an ordinary connect. */
388
#define EXIT_PURPOSE_CONNECT 1
389
/** This exit stream wants to do a resolve (either normal or reverse). */
390
391
392
#define EXIT_PURPOSE_RESOLVE 2
#define _EXIT_PURPOSE_MAX 2

393
/* !!!! If any connection purpose is ever over 31, we must grow the type
394
395
 * field in connection_t. */

396
/** Circuit state: I'm the origin, still haven't done all my handshakes. */
Nick Mathewson's avatar
Nick Mathewson committed
397
#define CIRCUIT_STATE_BUILDING 0
398
/** Circuit state: Waiting to process the onionskin. */
Nick Mathewson's avatar
Nick Mathewson committed
399
#define CIRCUIT_STATE_ONIONSKIN_PENDING 1
400
401
/** Circuit state: I'd like to deliver a create, but my n_conn is still
 * connecting. */
Nick Mathewson's avatar
Nick Mathewson committed
402
#define CIRCUIT_STATE_OR_WAIT 2
403
/** Circuit state: onionskin(s) processed, ready to send/receive cells. */
Nick Mathewson's avatar
Nick Mathewson committed
404
#define CIRCUIT_STATE_OPEN 3
Roger Dingledine's avatar
Roger Dingledine committed
405

406
#define _CIRCUIT_PURPOSE_MIN 1
407

408
/* these circuits were initiated elsewhere */
409
#define _CIRCUIT_PURPOSE_OR_MIN 1
Nick Mathewson's avatar
Nick Mathewson committed
410
411
/** OR-side circuit purpose: normal circuit, at OR. */
#define CIRCUIT_PURPOSE_OR 1
412
/** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
Nick Mathewson's avatar
Nick Mathewson committed
413
#define CIRCUIT_PURPOSE_INTRO_POINT 2
414
/** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
Nick Mathewson's avatar
Nick Mathewson committed
415
#define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
416
/** OR-side circuit purpose: At OR, both circuits have this purpose. */
Nick Mathewson's avatar
Nick Mathewson committed
417
#define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
418
#define _CIRCUIT_PURPOSE_OR_MAX 4
419

420
/* these circuits originate at this node */
421
422
423
424

/* here's how circ client-side purposes work:
 *   normal circuits are C_GENERAL.
 *   circuits that are c_introducing are either on their way to
425
426
427
428
 *     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.
429
430
431
 *   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.
432
433
 *   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
434
435
 *     buildstate->pending_final_cpath then they're expecting a
 *     cell from bob, else they're not.
436
437
 *   circuits that are c_rend_ready_intro_acked are open, and
 *     some intro circ has sent its intro and received an ack.
438
439
440
 *   circuits that are c_rend_joined are open, have heard from
 *     bob, and are talking to him.
 */
Nick Mathewson's avatar
Nick Mathewson committed
441
442
/** Client-side circuit purpose: Normal circuit, with cpath. */
#define CIRCUIT_PURPOSE_C_GENERAL 5
443
/** Client-side circuit purpose: at Alice, connecting to intro point. */
Nick Mathewson's avatar
Nick Mathewson committed
444
#define CIRCUIT_PURPOSE_C_INTRODUCING 6
445
446
/** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point,
 * waiting for ACK/NAK. */
Nick Mathewson's avatar
Nick Mathewson committed
447
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
448
/** Client-side circuit purpose: at Alice, introduced and acked, closing. */
Nick Mathewson's avatar
Nick Mathewson committed
449
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
450
/** Client-side circuit purpose: at Alice, waiting for ack. */
Nick Mathewson's avatar
Nick Mathewson committed
451
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
452
/** Client-side circuit purpose: at Alice, waiting for Bob. */
Nick Mathewson's avatar
Nick Mathewson committed
453
454
455
456
#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
457
/** Client-side circuit purpose: at Alice, rendezvous established. */
Nick Mathewson's avatar
Nick Mathewson committed
458
459
#define CIRCUIT_PURPOSE_C_REND_JOINED 12

460
461
#define _CIRCUIT_PURPOSE_C_MAX 12

462
/** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
Nick Mathewson's avatar
Nick Mathewson committed
463
464
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
/** Hidden-service-side circuit purpose: at Bob, successfully established
465
 * intro. */
Nick Mathewson's avatar
Nick Mathewson committed
466
#define CIRCUIT_PURPOSE_S_INTRO 14
467
/** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
Nick Mathewson's avatar
Nick Mathewson committed
468
#define CIRCUIT_PURPOSE_S_CONNECT_REND 15
469
/** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
Nick Mathewson's avatar
Nick Mathewson committed
470
#define CIRCUIT_PURPOSE_S_REND_JOINED 16
471
472
/** A testing circuit; not meant to be used for actual traffic. */
#define CIRCUIT_PURPOSE_TESTING 17
473
474
475
/** A controller made this circuit and Tor should not use it. */
#define CIRCUIT_PURPOSE_CONTROLLER 18
#define _CIRCUIT_PURPOSE_MAX 18
476
477
478
/** 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
479

480
481
/** True iff the circuit purpose <b>p</b> is for a circuit that
 * originated at this node. */
482
#define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
483
484
/** True iff the circuit purpose <b>p</b> is for a circuit that originated
 * here to serve as a client.  (Hidden services don't count here.) */
485
#define CIRCUIT_PURPOSE_IS_CLIENT(p)  \
486
487
  ((p)> _CIRCUIT_PURPOSE_OR_MAX &&    \
   (p)<=_CIRCUIT_PURPOSE_C_MAX)
488
/** True iff the circuit_t <b>c</b> is actually an origin_circuit_t. */
489
490
#define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))

491
492
493
494
/** How many circuits do we want simultaneously in-progress to handle
 * a given stream? */
#define MIN_CIRCUITS_HANDLING_STREAM 2

495
496
/* These RELAY_COMMAND constants define values for relay cell commands, and
* must match those defined in tor-spec.txt. */
497
498
499
500
501
#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
502
503
#define RELAY_COMMAND_EXTEND 6
#define RELAY_COMMAND_EXTENDED 7
504
505
#define RELAY_COMMAND_TRUNCATE 8
#define RELAY_COMMAND_TRUNCATED 9
506
#define RELAY_COMMAND_DROP 10
507
508
#define RELAY_COMMAND_RESOLVE 11
#define RELAY_COMMAND_RESOLVED 12
509
#define RELAY_COMMAND_BEGIN_DIR 13
510

511
512
513
514
#define RELAY_COMMAND_ESTABLISH_INTRO 32
#define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
#define RELAY_COMMAND_INTRODUCE1 34
#define RELAY_COMMAND_INTRODUCE2 35
515
516
517
518
519
#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
520

521
/* Reasons why an OR connection is closed. */
522
523
524
525
526
527
528
529
530
#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
531

532
/* Reasons why we (or a remote OR) might close a stream. See tor-spec.txt for
533
 * documentation of these.  The values must match. */
534
535
#define END_STREAM_REASON_MISC 1
#define END_STREAM_REASON_RESOLVEFAILED 2
536
#define END_STREAM_REASON_CONNECTREFUSED 3
537
538
539
#define END_STREAM_REASON_EXITPOLICY 4
#define END_STREAM_REASON_DESTROY 5
#define END_STREAM_REASON_DONE 6
540
#define END_STREAM_REASON_TIMEOUT 7
541
/* 8 is unallocated for historical reasons. */
542
543
544
545
#define END_STREAM_REASON_HIBERNATING 9
#define END_STREAM_REASON_INTERNAL 10
#define END_STREAM_REASON_RESOURCELIMIT 11
#define END_STREAM_REASON_CONNRESET 12
546
#define END_STREAM_REASON_TORPROTOCOL 13
547
#define END_STREAM_REASON_NOTDIRECTORY 14
548
#define END_STREAM_REASON_ENTRYPOLICY 15
549

550
551
552
553
/* 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. */
554
/* XXXX 256 is no longer used; feel free to reuse it. */
555
556
/** 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. */
557
#define END_STREAM_REASON_CANT_ATTACH 257
558
559
/** We can't connect to any directories at all, so we killed our streams
 * before they can time out. */
560
#define END_STREAM_REASON_NET_UNREACHABLE 258
561
562
/** This is a SOCKS connection, and the client used (or misused) the SOCKS
 * protocol in a way we couldn't handle. */
563
#define END_STREAM_REASON_SOCKSPROTOCOL 259
564
565
/** This is a transparent proxy connection, but we can't extract the original
 * target address:port. */
566
#define END_STREAM_REASON_CANT_FETCH_ORIG_DEST 260
567
/** This is a connection on the NATD port, and the destination IP:Port was
Roger Dingledine's avatar
Roger Dingledine committed
568
 * either ill-formed or out-of-range. */
569
#define END_STREAM_REASON_INVALID_NATD_DEST 261
570

571
572
573
/** Bitwise-and this value with endreason to mask out all flags. */
#define END_STREAM_REASON_MASK 511

574
575
/** Bitwise-or this with the argument to control_event_stream_status
 * to indicate that the reason came from an END cell. */
576
577
578
579
#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
580
581
582
583
/** 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
584

585
586
587
588
589
590
591
/** 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
592
/* 'type' values to use in RESOLVED cells.  Specified in tor-spec.txt. */
593
#define RESOLVED_TYPE_HOSTNAME 0
594
595
596
597
598
#define RESOLVED_TYPE_IPV4 4
#define RESOLVED_TYPE_IPV6 6
#define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
#define RESOLVED_TYPE_ERROR 0xF1

599
600
601
/* 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. */
602
#define END_CIRC_REASON_NOPATH          -2
603
/** Catch-all "other" reason for closing origin circuits. */
604
#define END_CIRC_AT_ORIGIN              -1
605

606
607
/* Reasons why we (or a remote OR) might close a circuit. See tor-spec.txt for
 * documentation of these. */
608
609
610
611
612
613
614
615
616
617
#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
618
619
620
621
622
#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
623

Roger Dingledine's avatar
Roger Dingledine committed
624
625
626
/** 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. */
627
628
#define END_CIRC_REASON_FLAG_REMOTE     512

Nick Mathewson's avatar
Nick Mathewson committed
629
/** Length of 'y' portion of 'y.onion' URL. */
630
631
#define REND_SERVICE_ID_LEN_BASE32 16

632
633
634
/** Length of 'y.onion' including '.onion' URL. */
#define REND_SERVICE_ADDRESS_LEN (16+1+5)

635
636
/** Length of a binary-encoded rendezvous service ID. */
#define REND_SERVICE_ID_LEN 10
637

638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/** 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). */
653
#define REND_DESC_ID_V2_LEN_BASE32 32
654

655
656
657
658
659
660
661
662
/** 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

663
664
665
666
667
668
669
670
/** 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

671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/** 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)

688
689
690
691
692
/** 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
693
694
695
/** Maximum length of authorized client names for a hidden service. */
#define REND_CLIENTNAME_MAX_LEN 16

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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/** 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];
} 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)

739
740
741
742
743
/** Used to indicate which way a cell is going on a circuit. */
typedef enum {
  CELL_DIRECTION_IN=1, /**< The cell is moving towards the origin. */
  CELL_DIRECTION_OUT=2, /**< The cell is moving away from the origin. */
} cell_direction_t;
744

745
746
/** Initial value for both sides of a circuit transmission window when the
 * circuit is initialized.  Measured in cells. */
747
#define CIRCWINDOW_START 1000
748
/** Amount to increment a circuit window when we get a circuit SENDME. */
749
#define CIRCWINDOW_INCREMENT 100
750
751
/** Initial value on both sides of a stream transmission window when the
 * stream is initialized.  Measured in cells. */
752
#define STREAMWINDOW_START 500
753
/** Amount to increment a stream window when we get a stream SENDME. */
754
#define STREAMWINDOW_INCREMENT 50
755

756
/* Cell commands.  These values are defined in tor-spec.txt. */
Roger Dingledine's avatar
Roger Dingledine committed
757
758
#define CELL_PADDING 0
#define CELL_CREATE 1
759
760
761
#define CELL_CREATED 2
#define CELL_RELAY 3
#define CELL_DESTROY 4
762
763
#define CELL_CREATE_FAST 5
#define CELL_CREATED_FAST 6
764
765
#define CELL_VERSIONS 7
#define CELL_NETINFO 8
766
#define CELL_RELAY_EARLY 9
Roger Dingledine's avatar
Roger Dingledine committed
767

768
769
/** True iff the cell command <b>x</b> is one that implies a variable-length
 * cell. */
770
#define CELL_COMMAND_IS_VAR_LENGTH(x) ((x) == CELL_VERSIONS)
771

772
/** How long to test reachability before complaining to the user. */
773
#define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60)
774

775
/** Legal characters in a nickname. */
776
777
#define LEGAL_NICKNAME_CHARACTERS \
  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
778

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

783
/** Number of bytes in a SOCKS4 header. */
784
785
#define SOCKS4_NETWORK_LEN 8

Roger Dingledine's avatar
Roger Dingledine committed
786
787
788
/*
 * Relay payload:
 *         Relay command           [1 byte]
789
790
 *         Recognized              [2 bytes]
 *         Stream ID               [2 bytes]
Roger Dingledine's avatar
Roger Dingledine committed
791
792
 *         Partial SHA-1           [4 bytes]
 *         Length                  [2 bytes]
793
 *         Relay payload           [498 bytes]
Roger Dingledine's avatar
Roger Dingledine committed
794
 */
795

796
/** Number of bytes in a cell, minus cell header. */
Roger Dingledine's avatar
Roger Dingledine committed
797
#define CELL_PAYLOAD_SIZE 509
798
/** Number of bytes in a cell transmitted over the network. */
Roger Dingledine's avatar
Roger Dingledine committed
799
800
#define CELL_NETWORK_SIZE 512

801
/** Length of a header on a variable-length cell. */
802
803
#define VAR_CELL_HEADER_SIZE 5

804
805
/** Number of bytes in a relay cell's header (not including general cell
 * header). */
806
#define RELAY_HEADER_SIZE (1+2+2+4+2)
807
/** Largest number of bytes that can fit in a relay cell payload. */
808
809
#define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)

810
811
812
813
814
/** Identifies a circuit on an or_connection */
typedef uint16_t circid_t;
/** Identifies a stream on a circuit */
typedef uint16_t streamid_t;

815
816
/** Parsed onion routing cell.  All communication between nodes
 * is via cells. */
817
typedef struct cell_t {
818
  circid_t circ_id; /**< Circuit which received the cell. */
819
820
  uint8_t command; /**< Type of the cell: one of CELL_PADDING, CELL_CREATE,
                    * CELL_DESTROY, etc */
821
  char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
822
823
824
825
826
} cell_t;

/** Parsed variable-length onion routing cell. */
typedef struct var_cell_t {
  uint8_t command;
827
  circid_t circ_id;
828
829
830
  uint16_t payload_len;
  char payload[1];
} var_cell_t;
831

832
/** A cell as packed for writing to the network. */
833
typedef struct packed_cell_t {
Roger Dingledine's avatar
Roger Dingledine committed
834
  struct packed_cell_t *next; /**< Next cell queued on this circuit. */
835
  char body[CELL_NETWORK_SIZE]; /**< Cell as packed for network. */
836
} packed_cell_t;
837

838
839
/** A queue of cells on a circuit, waiting to be added to the
 * or_connection_t's outbuf. */
840
typedef struct cell_queue_t {
Roger Dingledine's avatar
Roger Dingledine committed
841
842
843
  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. */
844
} cell_queue_t;
Roger Dingledine's avatar
Roger Dingledine committed
845

Nick Mathewson's avatar
Nick Mathewson committed
846
/** Beginning of a RELAY cell payload. */
847
typedef struct {
Nick Mathewson's avatar
Nick Mathewson committed
848
849
  uint8_t command; /**< The end-to-end relay command. */
  uint16_t recognized; /**< Used to tell whether cell is for us. */
850
  streamid_t stream_id; /**< Which stream is this cell associated with? */
Nick Mathewson's avatar
Nick Mathewson committed
851
852
  char integrity[4]; /**< Used to tell whether cell is corrupted. */
  uint16_t length; /**< How long is the payload body? */
853
} relay_header_t;
Roger Dingledine's avatar
Roger Dingledine committed
854

855
typedef struct buf_t buf_t;
856
typedef struct socks_request_t socks_request_t;
857

858
859
/* Values for connection_t.magic: used to make sure that downcasts (casts from
* connection_t to foo_connection_t) are safe. */
860
861
862
863
864
#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
865

Nick Mathewson's avatar
Nick Mathewson committed
866
/** Description of a connection to another host or process, and associated
867
868
869
 * data.
 *
 * A connection is named based on what it's connected to -- an "OR
870
 * connection" has a Tor node on the other end, an "exit
871
872
873
874
875
876
877
878
879
880
881
882
883
 * 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.
 */
884
885
886
typedef struct connection_t {
  uint32_t magic; /**< For memory debugging: must equal one of
                   * *_CONNECTION_MAGIC. */
Roger Dingledine's avatar
Roger Dingledine committed
887

Nick Mathewson's avatar
Nick Mathewson committed
888
  uint8_t state; /**< Current state of this connection. */
889
890
  unsigned int type:4; /**< What kind of connection is this? */
  unsigned int purpose:5; /**< Only used for DIR and EXIT types currently. */
891
892
893
894

  /* The next fields are all one-bit booleans. Some are only applicable to
   * connection subtypes, but we hold them here anyway, to save space.
   */
895
896
897
898
899
900
  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
901
                                      * marked for close, do we flush it
Roger Dingledine's avatar
Roger Dingledine committed
902
                                      * before closing it? */
903
904
  unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
                                     * conn? */
905
906
907
  /** 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;
908

909
910
911
912
913
914
915
916
917
918
919
  /* 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
920
921
  /** True iff we've called connection_close_immediate() on this linked
   * connection. */
922
  unsigned int linked_conn_is_closed:1;
923

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

Nick Mathewson's avatar
Nick Mathewson committed
939
  /* XXXX_IP6 make this IPv6-capable */