or.h 205 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

13
14
#ifndef _TOR_OR_H
#define _TOR_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
23
24
25
26
27
28
29

#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

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

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

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

88
89
90
91
92
93
94
#include "crypto.h"
#include "tortls.h"
#include "log.h"
#include "compat.h"
#include "container.h"
#include "util.h"
#include "torgzip.h"
95
#include "address.h"
Roger Dingledine's avatar
Roger Dingledine committed
96

97
98
#include <event.h>

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

121
122
123
124
125
126
#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

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

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

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

148
149
150
151
152
153
154
155
/** 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

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

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

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

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

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

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

230
/** State for any listener connection. */
Roger Dingledine's avatar
Roger Dingledine committed
231
232
#define LISTENER_STATE_READY 0

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

#define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION

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

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

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

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

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

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

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

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

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

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

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

396
/* !!!! If any connection purpose is ever over over 31, we must grow the type
397
398
 * field in connection_t. */

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

409
#define _CIRCUIT_PURPOSE_MIN 1
410

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

423
/* these circuits originate at this node */
424
425
426
427

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

463
464
#define _CIRCUIT_PURPOSE_C_MAX 12

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

483
484
/** True iff the circuit purpose <b>p</b> is for a circuit that
 * originated at this node. */
485
#define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
486
487
/** 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.) */
488
489
490
#define CIRCUIT_PURPOSE_IS_CLIENT(p) \
  ((p)> _CIRCUIT_PURPOSE_OR_MAX &&    \
   (p)<=_CIRCUIT_PURPOSE_C_MAX)
491
/** True iff the circuit_t <b>c</b> is actually an origin_circuit_t. */
492
493
#define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))

494
495
496
497
/** How many circuits do we want simultaneously in-progress to handle
 * a given stream? */
#define MIN_CIRCUITS_HANDLING_STREAM 2

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

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

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

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

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

574
575
576
/** Bitwise-and this value with endreason to mask out all flags. */
#define END_STREAM_REASON_MASK 511

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

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

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

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

Roger Dingledine's avatar
Roger Dingledine committed
627
628
629
/** 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. */
630
631
#define END_CIRC_REASON_FLAG_REMOTE     512

Nick Mathewson's avatar
Nick Mathewson committed
632
/** Length of 'y' portion of 'y.onion' URL. */
633
634
#define REND_SERVICE_ID_LEN_BASE32 16

635
636
637
/** Length of 'y.onion' including '.onion' URL. */
#define REND_SERVICE_ADDRESS_LEN (16+1+5)

638
639
/** Length of a binary-encoded rendezvous service ID. */
#define REND_SERVICE_ID_LEN 10
640

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

658
659
660
661
662
663
664
665
/** 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

666
667
668
669
670
671
672
673
/** 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

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

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

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
739
740
741
742
743
744
745
746
747
/** 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)

748
749
750
751
752
/** 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;
753

754
755
/** Initial value for both sides of a circuit transmission window when the
 * circuit is initialized.  Measured in cells. */
756
#define CIRCWINDOW_START 1000
757
/** Amount to increment a circuit window when we get a circuit SENDME. */
758
#define CIRCWINDOW_INCREMENT 100
759
760
/** Initial value on both sides of a stream transmission window when the
 * stream is initialized.  Measured in cells. */
761
#define STREAMWINDOW_START 500
762
/** Amount to increment a stream window when we get a stream SENDME. */
763
#define STREAMWINDOW_INCREMENT 50
764

765
/* Cell commands.  These values are defined in tor-spec.txt. */
Roger Dingledine's avatar
Roger Dingledine committed
766
767
#define CELL_PADDING 0
#define CELL_CREATE 1
768
769
770
#define CELL_CREATED 2
#define CELL_RELAY 3
#define CELL_DESTROY 4
771
772
#define CELL_CREATE_FAST 5
#define CELL_CREATED_FAST 6
773
774
#define CELL_VERSIONS 7
#define CELL_NETINFO 8
775
#define CELL_RELAY_EARLY 9
Roger Dingledine's avatar
Roger Dingledine committed
776

777
778
/** True iff the cell command <b>x</b> is one that implies a variable-length
 * cell. */
779
#define CELL_COMMAND_IS_VAR_LENGTH(x) ((x) == CELL_VERSIONS)
780

781
/** How long to test reachability before complaining to the user. */
782
#define TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT (20*60)
783

784
/** Legal characters in a nickname. */
785
786
#define LEGAL_NICKNAME_CHARACTERS \
  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
787

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

792
/** Number of bytes in a SOCKS4 header. */
793
794
#define SOCKS4_NETWORK_LEN 8

Roger Dingledine's avatar
Roger Dingledine committed
795
796
797
/*
 * Relay payload:
 *         Relay command           [1 byte]
798
799
 *         Recognized              [2 bytes]
 *         Stream ID               [2 bytes]
Roger Dingledine's avatar
Roger Dingledine committed
800
801
 *         Partial SHA-1           [4 bytes]
 *         Length                  [2 bytes]
802
 *         Relay payload           [498 bytes]
Roger Dingledine's avatar
Roger Dingledine committed
803
 */
804

805
/** Number of bytes in a cell, minus cell header. */
Roger Dingledine's avatar
Roger Dingledine committed
806
#define CELL_PAYLOAD_SIZE 509
807
/** Number of bytes in a cell transmitted over the network. */
Roger Dingledine's avatar
Roger Dingledine committed
808
809
#define CELL_NETWORK_SIZE 512

810
/** Length of a header on a variable-length cell. */
811
812
#define VAR_CELL_HEADER_SIZE 5

813
814
/** Number of bytes in a relay cell's header (not including general cell
 * header). */
815
#define RELAY_HEADER_SIZE (1+2+2+4+2)
816
/** Largest number of bytes that can fit in a relay cell payload. */
817
818
#define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)

819
820
821
822
823
/** Identifies a circuit on an or_connection */
typedef uint16_t circid_t;
/** Identifies a stream on a circuit */
typedef uint16_t streamid_t;

824
825
/** Parsed onion routing cell.  All communication between nodes
 * is via cells. */
826
typedef struct cell_t {
827
  circid_t circ_id; /**< Circuit which received the cell. */
828
829
  uint8_t command; /**< Type of the cell: one of CELL_PADDING, CELL_CREATE,
                    * CELL_DESTROY, etc */
830
  char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
831
832
833
834
835
} cell_t;

/** Parsed variable-length onion routing cell. */
typedef struct var_cell_t {
  uint8_t command;
836
  circid_t circ_id;
837
838
839
  uint16_t payload_len;
  char payload[1];
} var_cell_t;
840

841
/** A cell as packed for writing to the network. */
842
typedef struct packed_cell_t {
Roger Dingledine's avatar
Roger Dingledine committed
843
  struct packed_cell_t *next; /**< Next cell queued on this circuit. */
844
  char body[CELL_NETWORK_SIZE]; /**< Cell as packed for network. */
845
} packed_cell_t;
846

847
848
/** A queue of cells on a circuit, waiting to be added to the
 * or_connection_t's outbuf. */
849
typedef struct cell_queue_t {
Roger Dingledine's avatar
Roger Dingledine committed
850
851
852
  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. */
853
} cell_queue_t;
Roger Dingledine's avatar
Roger Dingledine committed
854

Nick Mathewson's avatar
Nick Mathewson committed
855
/** Beginning of a RELAY cell payload. */
856
typedef struct {
Nick Mathewson's avatar
Nick Mathewson committed
857
858
  uint8_t command; /**< The end-to-end relay command. */
  uint16_t recognized; /**< Used to tell whether cell is for us. */
859
  streamid_t stream_id; /**< Which stream is this cell associated with? */
Nick Mathewson's avatar
Nick Mathewson committed
860
861
  char integrity[4]; /**< Used to tell whether cell is corrupted. */
  uint16_t length; /**< How long is the payload body? */
862
} relay_header_t;
Roger Dingledine's avatar
Roger Dingledine committed
863

864
typedef struct buf_t buf_t;
865
typedef struct socks_request_t socks_request_t;
866

867
868
/* Values for connection_t.magic: used to make sure that downcasts (casts from
* connection_t to foo_connection_t) are safe. */
869
870
871
872
873
#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
874

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

Nick Mathewson's avatar
Nick Mathewson committed
897
  uint8_t state; /**< Current state of this connection. */
898
899
  unsigned int type:4; /**< What kind of connection is this? */
  unsigned int purpose:5; /**< Only used for DIR and EXIT types currently. */
900
901
902
903

  /* The next fields are all one-bit booleans. Some are only applicable to
   * connection subtypes, but we hold them here anyway, to save space.
   */
904
905
906
907
908
909
  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
910
                                      * marked for close, do we flush it
Roger Dingledine's avatar
Roger Dingledine committed
911
                                      * before closing it? */
912
913
  unsigned int inbuf_reached_eof:1; /**< Boolean: did read() return 0 on this
                                     * conn? */
914
915
916
  /** 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;
917

918
919
920
921
922
923
924
925
926
927
928
  /* 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
929
930
  /** True iff we've called connection_close_immediate() on this linked
   * connection. */
931
  unsigned int linked_conn_is_closed:1;
932

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

Nick Mathewson's avatar