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"
Roger Dingledine's avatar
Roger Dingledine committed
94

95
96
#include <event.h>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION

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

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

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

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

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

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

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

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

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

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

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

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

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

407
#define _CIRCUIT_PURPOSE_MIN 1
408

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

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

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

461
462
#define _CIRCUIT_PURPOSE_C_MAX 12

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

746
747
748
749
750
/** 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;
751

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

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

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

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

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

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

790
/** Number of bytes in a SOCKS4 header. */
791
792
#define SOCKS4_NETWORK_LEN 8

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

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

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

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

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

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

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

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

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

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

862
typedef struct buf_t buf_t;
863
typedef struct socks_request_t socks_request_t;
864

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

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

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

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

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

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