or.h 73.3 KB
Newer Older
Roger Dingledine's avatar
Roger Dingledine committed
1
2
3
/* Copyright 2001 Matej Pfajfar.
 * Copyright 2001-2004 Roger Dingledine.
 * Copyright 2004 Roger Dingledine, Nick Mathewson. */
4
/* See LICENSE for licensing information */
Roger Dingledine's avatar
Roger Dingledine committed
5
6
/* $Id$ */

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

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

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

Roger Dingledine's avatar
Roger Dingledine committed
24
25
#include <stdio.h>
#include <stdlib.h>
Roger Dingledine's avatar
Roger Dingledine committed
26
#include <limits.h>
27
#ifdef HAVE_UNISTD_H
Roger Dingledine's avatar
Roger Dingledine committed
28
#include <unistd.h>
29
30
#endif
#ifdef HAVE_STRING_H
Roger Dingledine's avatar
Roger Dingledine committed
31
#include <string.h>
32
33
#endif
#ifdef HAVE_SIGNAL_H
Roger Dingledine's avatar
Roger Dingledine committed
34
#include <signal.h>
35
36
#endif
#ifdef HAVE_NETDB_H
Roger Dingledine's avatar
Roger Dingledine committed
37
#include <netdb.h>
38
39
#endif
#ifdef HAVE_CTYPE_H
Roger Dingledine's avatar
Roger Dingledine committed
40
#include <ctype.h>
41
#endif
42
#include "../common/torint.h"
43
44
45
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
46
47
48
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h> /* FreeBSD needs this to know what version it is */
#endif
49
50
51
#ifdef HAVE_SYS_LIMITS_H
#include <sys/limits.h>
#endif
52
#ifdef HAVE_MACHINE_LIMITS_H
53
54
#ifndef __FreeBSD__
  /* FreeBSD has a bug where it complains that this file is obsolete,
55
     and I should migrate to using sys/limits. It complains even when
56
     I include both. */
57
58
#include <machine/limits.h>
#endif
59
#endif
60
#ifdef HAVE_SYS_TYPES_H
Roger Dingledine's avatar
Roger Dingledine committed
61
#include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
62
#endif
Roger Dingledine's avatar
Roger Dingledine committed
63
64
65
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
66
#ifdef HAVE_SYS_FCNTL_H
Roger Dingledine's avatar
Roger Dingledine committed
67
#include <sys/fcntl.h>
68
69
70
71
72
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
Roger Dingledine's avatar
Roger Dingledine committed
73
#include <sys/ioctl.h>
74
75
#endif
#ifdef HAVE_SYS_SOCKET_H
Roger Dingledine's avatar
Roger Dingledine committed
76
#include <sys/socket.h>
77
78
#endif
#ifdef HAVE_SYS_TIME_H
Roger Dingledine's avatar
Roger Dingledine committed
79
#include <sys/time.h>
80
81
#endif
#ifdef HAVE_SYS_STAT_H
82
#include <sys/stat.h>
83
84
#endif
#ifdef HAVE_NETINET_IN_H
Roger Dingledine's avatar
Roger Dingledine committed
85
#include <netinet/in.h>
86
87
#endif
#ifdef HAVE_ARPA_INET_H
Roger Dingledine's avatar
Roger Dingledine committed
88
#include <arpa/inet.h>
89
90
#endif
#ifdef HAVE_ERRNO_H
Roger Dingledine's avatar
Roger Dingledine committed
91
#include <errno.h>
92
93
#endif
#ifdef HAVE_ASSERT_H
Roger Dingledine's avatar
Roger Dingledine committed
94
#include <assert.h>
95
96
#endif
#ifdef HAVE_TIME_H
97
#include <time.h>
98
#endif
99
100
101

#ifdef MS_WINDOWS
#if (_MSC_VER <= 1300)
102
#include <winsock.h>
103
#else
104
105
#include <winsock2.h>
#include <ws2tcpip.h>
106
#endif
107
108
#endif

109
#ifdef MS_WINDOWS
110
#include <io.h>
Roger Dingledine's avatar
Roger Dingledine committed
111
#include <process.h>
112
#include <direct.h>
113
#include <windows.h>
114
#define snprintf _snprintf
115
116
#endif

Nick Mathewson's avatar
Nick Mathewson committed
117
118
119
120
121
122
#ifdef HAVE_EVENT_H
#include <event.h>
#else
#error "Tor requires libevent to build."
#endif

123
#include "../common/crypto.h"
124
#include "../common/tortls.h"
Roger Dingledine's avatar
Roger Dingledine committed
125
#include "../common/log.h"
126
127
#include "../common/compat.h"
#include "../common/container.h"
128
#include "../common/util.h"
129
#include "../common/torgzip.h"
Roger Dingledine's avatar
Roger Dingledine committed
130

131
132
133
134
/** Define this if you want Tor to crash when any problem comes up,
 * so you can get a coredump and track things down. */
#undef TOR_FRAGILE

135
/** Upper bound on maximum simultaneous connections; can be lowered by
136
 * config file. */
137
#define MAXCONNECTIONS 15000
Roger Dingledine's avatar
Roger Dingledine committed
138

139
#define DEFAULT_BANDWIDTH_OP (1024 * 1000)
140
#define MAX_NICKNAME_LEN 19
141
/* Hex digest plus dollar sign. */
142
#define MAX_HEX_NICKNAME_LEN (HEX_DIGEST_LEN+1)
143
#define MAX_DIR_SIZE 500000
Roger Dingledine's avatar
Roger Dingledine committed
144

Roger Dingledine's avatar
Roger Dingledine committed
145
#ifdef TOR_PERF
Nick Mathewson's avatar
Nick Mathewson committed
146
/** How long do we keep DNS cache entries before purging them? */
Roger Dingledine's avatar
Roger Dingledine committed
147
148
#define MAX_DNS_ENTRY_AGE (150*60)
#else
149
#define MAX_DNS_ENTRY_AGE (15*60)
Roger Dingledine's avatar
Roger Dingledine committed
150
#endif
151

Nick Mathewson's avatar
Nick Mathewson committed
152
/** How often do we rotate onion keys? */
153
#define MIN_ONION_KEY_LIFETIME (7*24*60*60) /* once a week */
Nick Mathewson's avatar
Nick Mathewson committed
154
/** How often do we rotate TLS contexts? */
155
156
#define MAX_SSL_KEY_LIFETIME (120*60)

157
158
159
160
161
/** How old do we allow a router to get before removing it, either
 * from the descriptor list (for dirservers) or the router list (for others)?
 * In seconds. */
#define ROUTER_MAX_AGE (60*60*24)

162
163
164
165
typedef enum {
  CIRC_ID_TYPE_LOWER=0,
  CIRC_ID_TYPE_HIGHER=1
} circ_id_type_t;
166

167
#define _CONN_TYPE_MIN 3
Nick Mathewson's avatar
Nick Mathewson committed
168
/** Type for sockets listening for OR connections. */
Roger Dingledine's avatar
Roger Dingledine committed
169
#define CONN_TYPE_OR_LISTENER 3
170
/** Type for OR-to-OR or OP-to-OR connections. */
Roger Dingledine's avatar
Roger Dingledine committed
171
#define CONN_TYPE_OR 4
Nick Mathewson's avatar
Nick Mathewson committed
172
/** Type for connections from final OR to chosen destination. */
173
#define CONN_TYPE_EXIT 5
174
/** Type for sockets listening for SOCKS connections. */
175
#define CONN_TYPE_AP_LISTENER 6
176
/** Type for SOCKS connections to OP. */
177
#define CONN_TYPE_AP 7
178
/** Type for sockets listening for HTTP connections to the directory server. */
179
#define CONN_TYPE_DIR_LISTENER 8
180
/** Type for HTTP connections to the directory server. */
181
#define CONN_TYPE_DIR 9
182
/** Type for connections to local dnsworker processes. */
183
#define CONN_TYPE_DNSWORKER 10
184
/** Type for connections to local cpuworker processes. */
Roger Dingledine's avatar
Roger Dingledine committed
185
#define CONN_TYPE_CPUWORKER 11
186
187
188
189
190
/** Type for listenting for connections from user interface process */
#define CONN_TYPE_CONTROL_LISTENER 12
/** Type for connections from user interface process */
#define CONN_TYPE_CONTROL 13
#define _CONN_TYPE_MAX 13
Roger Dingledine's avatar
Roger Dingledine committed
191

192
193
#define CONN_IS_EDGE(x) ((x)->type == CONN_TYPE_EXIT || (x)->type == CONN_TYPE_AP)

194
/** State for any listener connection. */
Roger Dingledine's avatar
Roger Dingledine committed
195
196
#define LISTENER_STATE_READY 0

197
#define _DNSWORKER_STATE_MIN 1
198
/** State for a connection to a dnsworker process that's idle. */
199
#define DNSWORKER_STATE_IDLE 1
200
/** State for a connection to a dnsworker process that's resolving a hostname. */
201
202
#define DNSWORKER_STATE_BUSY 2
#define _DNSWORKER_STATE_MAX 2
203

204
#define _CPUWORKER_STATE_MIN 1
205
/** State for a connection to a cpuworker process that's idle. */
206
#define CPUWORKER_STATE_IDLE 1
Nick Mathewson's avatar
Nick Mathewson committed
207
/** State for a connection to a cpuworker process that's processing a
208
 * handshake. */
209
#define CPUWORKER_STATE_BUSY_ONION 2
Nick Mathewson's avatar
Nick Mathewson committed
210
#define _CPUWORKER_STATE_MAX 2
Roger Dingledine's avatar
Roger Dingledine committed
211
212
213

#define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION

214
#define _OR_CONN_STATE_MIN 1
215
/** State for a connection to an OR: waiting for connect() to finish. */
Nick Mathewson's avatar
Nick Mathewson committed
216
#define OR_CONN_STATE_CONNECTING 1
217
/** State for a connection to an OR: SSL is handshaking, not done yet. */
Nick Mathewson's avatar
Nick Mathewson committed
218
219
220
#define OR_CONN_STATE_HANDSHAKING 2
/** State for a connection to an OR: Ready to send/receive cells. */
#define OR_CONN_STATE_OPEN 3
221
222
223
#define _OR_CONN_STATE_MAX 3

#define _EXIT_CONN_STATE_MIN 1
224
/** State for an exit connection: waiting for response from dns farm. */
Nick Mathewson's avatar
Nick Mathewson committed
225
#define EXIT_CONN_STATE_RESOLVING 1
226
/** State for an exit connection: waiting for connect() to finish. */
Nick Mathewson's avatar
Nick Mathewson committed
227
#define EXIT_CONN_STATE_CONNECTING 2
228
/** State for an exit connection: open and ready to transmit data. */
229
#define EXIT_CONN_STATE_OPEN 3
230
/** State for an exit connection: waiting to be removed. */
Nick Mathewson's avatar
Nick Mathewson committed
231
#define EXIT_CONN_STATE_RESOLVEFAILED 4
232
#define _EXIT_CONN_STATE_MAX 4
Roger Dingledine's avatar
Roger Dingledine committed
233
#if 0
234
235
#define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
#define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
Roger Dingledine's avatar
Roger Dingledine committed
236
237
#endif

238
/* the AP state values must be disjoint from the EXIT state values */
239
#define _AP_CONN_STATE_MIN 5
240
/** State for a SOCKS connection: waiting for SOCKS request. */
241
#define AP_CONN_STATE_SOCKS_WAIT 5
Nick Mathewson's avatar
Nick Mathewson committed
242
/** State for a SOCKS connection: got a y.onion URL; waiting to receive
243
 * rendezvous rescriptor. */
244
#define AP_CONN_STATE_RENDDESC_WAIT 6
245
/** State for a SOCKS connection: waiting for a completed circuit. */
246
#define AP_CONN_STATE_CIRCUIT_WAIT 7
247
/** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
248
#define AP_CONN_STATE_CONNECT_WAIT 8
249
250
/** State for a SOCKS connection: send RESOLVE, waiting for RESOLVED. */
#define AP_CONN_STATE_RESOLVE_WAIT 9
251
/** State for a SOCKS connection: ready to send and receive. */
252
253
#define AP_CONN_STATE_OPEN 10
#define _AP_CONN_STATE_MAX 10
254

255
#define _DIR_CONN_STATE_MIN 1
256
/** State for connection to directory server: waiting for connect(). */
Roger Dingledine's avatar
Roger Dingledine committed
257
#define DIR_CONN_STATE_CONNECTING 1
258
/** State for connection to directory server: sending HTTP request. */
Roger Dingledine's avatar
Roger Dingledine committed
259
#define DIR_CONN_STATE_CLIENT_SENDING 2
260
/** State for connection to directory server: reading HTTP response. */
Roger Dingledine's avatar
Roger Dingledine committed
261
#define DIR_CONN_STATE_CLIENT_READING 3
262
/** State for connection at directory server: waiting for HTTP request. */
Roger Dingledine's avatar
Roger Dingledine committed
263
#define DIR_CONN_STATE_SERVER_COMMAND_WAIT 4
264
/** State for connection at directory server: sending HTTP response. */
Roger Dingledine's avatar
Roger Dingledine committed
265
266
267
#define DIR_CONN_STATE_SERVER_WRITING 5
#define _DIR_CONN_STATE_MAX 5

268
269
270
271
272
#define _CONTROL_CONN_STATE_MIN 1
#define CONTROL_CONN_STATE_OPEN 1
#define CONTROL_CONN_STATE_NEEDAUTH 2
#define _CONTROL_CONN_STATE_MAX 2

Roger Dingledine's avatar
Roger Dingledine committed
273
#define _DIR_PURPOSE_MIN 1
274
/** Purpose for connection to directory server: download a directory. */
Roger Dingledine's avatar
Roger Dingledine committed
275
#define DIR_PURPOSE_FETCH_DIR 1
276
277
278
/** Purpose for connection to directory server: download just the list
 * of running routers. */
#define DIR_PURPOSE_FETCH_RUNNING_LIST 2
Nick Mathewson's avatar
Nick Mathewson committed
279
280
/** Purpose for connection to directory server: download a rendezvous
 * descriptor. */
281
#define DIR_PURPOSE_FETCH_RENDDESC 3
Nick Mathewson's avatar
Nick Mathewson committed
282
283
/** Purpose for connection to directory server: set after a rendezvous
 * descriptor is downloaded. */
284
#define DIR_PURPOSE_HAS_FETCHED_RENDDESC 4
285
/** Purpose for connection to directory server: upload a server descriptor. */
286
#define DIR_PURPOSE_UPLOAD_DIR 5
Nick Mathewson's avatar
Nick Mathewson committed
287
/** Purpose for connection to directory server: upload a rendezvous
288
 * descriptor. */
289
#define DIR_PURPOSE_UPLOAD_RENDDESC 6
Nick Mathewson's avatar
Nick Mathewson committed
290
/** Purpose for connection at a directory server. */
291
292
#define DIR_PURPOSE_SERVER 7
#define _DIR_PURPOSE_MAX 7
293

294
295
296
297
298
#define _EXIT_PURPOSE_MIN 1
#define EXIT_PURPOSE_CONNECT 1
#define EXIT_PURPOSE_RESOLVE 2
#define _EXIT_PURPOSE_MAX 2

299
/** Circuit state: I'm the OP, still haven't done all my handshakes. */
Nick Mathewson's avatar
Nick Mathewson committed
300
#define CIRCUIT_STATE_BUILDING 0
301
/** Circuit state: Waiting to process the onionskin. */
Nick Mathewson's avatar
Nick Mathewson committed
302
#define CIRCUIT_STATE_ONIONSKIN_PENDING 1
303
/** Circuit state: I'd like to deliver a create, but my n_conn is still connecting. */
Nick Mathewson's avatar
Nick Mathewson committed
304
#define CIRCUIT_STATE_OR_WAIT 2
305
/** Circuit state: onionskin(s) processed, ready to send/receive cells. */
Nick Mathewson's avatar
Nick Mathewson committed
306
#define CIRCUIT_STATE_OPEN 3
Roger Dingledine's avatar
Roger Dingledine committed
307

308
#define _CIRCUIT_PURPOSE_MIN 1
309

310
/* these circuits were initiated elsewhere */
311
#define _CIRCUIT_PURPOSE_OR_MIN 1
Nick Mathewson's avatar
Nick Mathewson committed
312
313
/** OR-side circuit purpose: normal circuit, at OR. */
#define CIRCUIT_PURPOSE_OR 1
314
/** OR-side circuit purpose: At OR, from Bob, waiting for intro from Alices. */
Nick Mathewson's avatar
Nick Mathewson committed
315
#define CIRCUIT_PURPOSE_INTRO_POINT 2
316
/** OR-side circuit purpose: At OR, from Alice, waiting for Bob. */
Nick Mathewson's avatar
Nick Mathewson committed
317
#define CIRCUIT_PURPOSE_REND_POINT_WAITING 3
318
/** OR-side circuit purpose: At OR, both circuits have this purpose. */
Nick Mathewson's avatar
Nick Mathewson committed
319
#define CIRCUIT_PURPOSE_REND_ESTABLISHED 4
320
#define _CIRCUIT_PURPOSE_OR_MAX 4
321

322
/* these circuits originate at this node */
323
324
325
326

/* here's how circ client-side purposes work:
 *   normal circuits are C_GENERAL.
 *   circuits that are c_introducing are either on their way to
327
328
329
330
 *     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.
331
332
333
 *   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.
334
335
 *   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
336
337
 *     buildstate->pending_final_cpath then they're expecting a
 *     cell from bob, else they're not.
338
339
 *   circuits that are c_rend_ready_intro_acked are open, and
 *     some intro circ has sent its intro and received an ack.
340
341
342
 *   circuits that are c_rend_joined are open, have heard from
 *     bob, and are talking to him.
 */
Nick Mathewson's avatar
Nick Mathewson committed
343
344
/** Client-side circuit purpose: Normal circuit, with cpath. */
#define CIRCUIT_PURPOSE_C_GENERAL 5
345
/** Client-side circuit purpose: at Alice, connecting to intro point. */
Nick Mathewson's avatar
Nick Mathewson committed
346
#define CIRCUIT_PURPOSE_C_INTRODUCING 6
347
/** Client-side circuit purpose: at Alice, sent INTRODUCE1 to intro point, waiting for ACK/NAK. */
Nick Mathewson's avatar
Nick Mathewson committed
348
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT 7
349
/** Client-side circuit purpose: at Alice, introduced and acked, closing. */
Nick Mathewson's avatar
Nick Mathewson committed
350
#define CIRCUIT_PURPOSE_C_INTRODUCE_ACKED 8
351
/** Client-side circuit purpose: at Alice, waiting for ack. */
Nick Mathewson's avatar
Nick Mathewson committed
352
#define CIRCUIT_PURPOSE_C_ESTABLISH_REND 9
353
/** Client-side circuit purpose: at Alice, waiting for Bob. */
Nick Mathewson's avatar
Nick Mathewson committed
354
355
356
357
#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
358
/** Client-side circuit purpose: at Alice, rendezvous established. */
Nick Mathewson's avatar
Nick Mathewson committed
359
360
#define CIRCUIT_PURPOSE_C_REND_JOINED 12

361
/** Hidden-service-side circuit purpose: at Bob, waiting for introductions. */
Nick Mathewson's avatar
Nick Mathewson committed
362
363
#define CIRCUIT_PURPOSE_S_ESTABLISH_INTRO 13
/** Hidden-service-side circuit purpose: at Bob, successfully established
364
 * intro. */
Nick Mathewson's avatar
Nick Mathewson committed
365
#define CIRCUIT_PURPOSE_S_INTRO 14
366
/** Hidden-service-side circuit purpose: at Bob, connecting to rend point. */
Nick Mathewson's avatar
Nick Mathewson committed
367
#define CIRCUIT_PURPOSE_S_CONNECT_REND 15
368
/** Hidden-service-side circuit purpose: at Bob, rendezvous established. */
Nick Mathewson's avatar
Nick Mathewson committed
369
#define CIRCUIT_PURPOSE_S_REND_JOINED 16
370
#define _CIRCUIT_PURPOSE_MAX 16
371

Nick Mathewson's avatar
Nick Mathewson committed
372
373
/** True iff the circuit purpose <b>p</b> is for a circuit at the OP
 * that this OP has originated. */
374
375
376
#define CIRCUIT_PURPOSE_IS_ORIGIN(p) ((p)>_CIRCUIT_PURPOSE_OR_MAX)
#define CIRCUIT_IS_ORIGIN(c) (CIRCUIT_PURPOSE_IS_ORIGIN((c)->purpose))

377
378
379
380
381
#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
382
383
#define RELAY_COMMAND_EXTEND 6
#define RELAY_COMMAND_EXTENDED 7
384
385
#define RELAY_COMMAND_TRUNCATE 8
#define RELAY_COMMAND_TRUNCATED 9
386
#define RELAY_COMMAND_DROP 10
387
388
#define RELAY_COMMAND_RESOLVE 11
#define RELAY_COMMAND_RESOLVED 12
389

390
391
392
393
#define RELAY_COMMAND_ESTABLISH_INTRO 32
#define RELAY_COMMAND_ESTABLISH_RENDEZVOUS 33
#define RELAY_COMMAND_INTRODUCE1 34
#define RELAY_COMMAND_INTRODUCE2 35
394
395
396
397
398
#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
399

400
#define _MIN_END_STREAM_REASON 1
401
402
403
404
405
406
#define END_STREAM_REASON_MISC 1
#define END_STREAM_REASON_RESOLVEFAILED 2
#define END_STREAM_REASON_CONNECTFAILED 3
#define END_STREAM_REASON_EXITPOLICY 4
#define END_STREAM_REASON_DESTROY 5
#define END_STREAM_REASON_DONE 6
407
#define END_STREAM_REASON_TIMEOUT 7
408
409
#define END_STREAM_REASON_RESOURCELIMIT 8
#define _MAX_END_STREAM_REASON 8
410

411
412
413
414
415
#define RESOLVED_TYPE_IPV4 4
#define RESOLVED_TYPE_IPV6 6
#define RESOLVED_TYPE_ERROR_TRANSIENT 0xF0
#define RESOLVED_TYPE_ERROR 0xF1

Nick Mathewson's avatar
Nick Mathewson committed
416
/** Length of 'y' portion of 'y.onion' URL. */
417
418
#define REND_SERVICE_ID_LEN 16

419
420
421
#define CELL_DIRECTION_IN 1
#define CELL_DIRECTION_OUT 2

Roger Dingledine's avatar
Roger Dingledine committed
422
423
424
425
426
427
#ifdef TOR_PERF
#define CIRCWINDOW_START 10000
#define CIRCWINDOW_INCREMENT 1000
#define STREAMWINDOW_START 5000
#define STREAMWINDOW_INCREMENT 500
#else
428
429
#define CIRCWINDOW_START 1000
#define CIRCWINDOW_INCREMENT 100
430
431
#define STREAMWINDOW_START 500
#define STREAMWINDOW_INCREMENT 50
Roger Dingledine's avatar
Roger Dingledine committed
432
#endif
433

Roger Dingledine's avatar
Roger Dingledine committed
434
435
436
/* cell commands */
#define CELL_PADDING 0
#define CELL_CREATE 1
437
438
439
#define CELL_CREATED 2
#define CELL_RELAY 3
#define CELL_DESTROY 4
Roger Dingledine's avatar
Roger Dingledine committed
440

441
442
443
444
/* people behind fascist firewalls use only these ports */
#define REQUIRED_FIREWALL_DIRPORT 80
#define REQUIRED_FIREWALL_ORPORT 443

445
446
/* legal characters in a nickname */
#define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
447

448
449
450
/** Name to use in client TLS certificates if no nickname is given.*/
#define DEFAULT_CLIENT_NICKNAME "client"

451
452
#define SOCKS4_NETWORK_LEN 8

Roger Dingledine's avatar
Roger Dingledine committed
453
454
455
/*
 * Relay payload:
 *         Relay command           [1 byte]
456
457
 *         Recognized              [2 bytes]
 *         Stream ID               [2 bytes]
Roger Dingledine's avatar
Roger Dingledine committed
458
459
 *         Partial SHA-1           [4 bytes]
 *         Length                  [2 bytes]
460
 *         Relay payload           [498 bytes]
Roger Dingledine's avatar
Roger Dingledine committed
461
 */
462

Roger Dingledine's avatar
Roger Dingledine committed
463
464
465
#define CELL_PAYLOAD_SIZE 509
#define CELL_NETWORK_SIZE 512

466
467
468
#define RELAY_HEADER_SIZE (1+2+2+4+2)
#define RELAY_PAYLOAD_SIZE (CELL_PAYLOAD_SIZE-RELAY_HEADER_SIZE)

Nick Mathewson's avatar
Nick Mathewson committed
469
470
/** Parsed onion routing cell.  All communication from OP-to-OR, or from
 * OR-to-OR, is via cells. */
Roger Dingledine's avatar
Roger Dingledine committed
471
typedef struct {
Nick Mathewson's avatar
Nick Mathewson committed
472
473
  uint16_t circ_id; /**< Circuit which received the cell. */
  unsigned char command; /**< Type of the cell: one of PADDING, CREATE, RELAY,
474
475
                          * or DESTROY. */
  unsigned char payload[CELL_PAYLOAD_SIZE]; /**< Cell body. */
Roger Dingledine's avatar
Roger Dingledine committed
476
477
} cell_t;

Nick Mathewson's avatar
Nick Mathewson committed
478
/** Beginning of a RELAY cell payload. */
479
typedef struct {
Nick Mathewson's avatar
Nick Mathewson committed
480
481
482
483
484
  uint8_t command; /**< The end-to-end relay command. */
  uint16_t recognized; /**< Used to tell whether cell is for us. */
  uint16_t stream_id; /**< Which stream is this cell associated with? */
  char integrity[4]; /**< Used to tell whether cell is corrupted. */
  uint16_t length; /**< How long is the payload body? */
485
} relay_header_t;
Roger Dingledine's avatar
Roger Dingledine committed
486

487
typedef struct buf_t buf_t;
488
typedef struct socks_request_t socks_request_t;
489

490
#define CONNECTION_MAGIC 0x7C3C304Eu
Nick Mathewson's avatar
Nick Mathewson committed
491
/** Description of a connection to another host or process, and associated
492
 * data. */
Roger Dingledine's avatar
Roger Dingledine committed
493
struct connection_t {
Nick Mathewson's avatar
Nick Mathewson committed
494
  uint32_t magic; /**< For memory debugging: must equal CONNECTION_MAGIC. */
Roger Dingledine's avatar
Roger Dingledine committed
495

Nick Mathewson's avatar
Nick Mathewson committed
496
497
  uint8_t type; /**< What kind of connection is this? */
  uint8_t state; /**< Current state of this connection. */
498
499
  uint8_t purpose; /**< Only used for DIR types currently. */
  uint8_t wants_to_read; /**< Boolean: should we start reading again once
500
501
                          * the bandwidth throttler allows it?
                          */
502
  uint8_t wants_to_write; /**< Boolean: should we start writing again once
Roger Dingledine's avatar
Roger Dingledine committed
503
504
                           * the bandwidth throttler allows reads?
                           */
Roger Dingledine's avatar
Roger Dingledine committed
505
  int s; /**< Our socket; -1 if this connection is closed. */
506
507
508
  int poll_index; /* XXXX rename. */
  struct event *read_event; /**< libevent event structure. */
  struct event *write_event; /**< libevent event structure. */
509
  int marked_for_close; /**< Boolean: should we close this conn on the next
510
511
                         * iteration of the main loop?
                         */
512
513
  const char *marked_for_close_file; /**< For debugging: in which file were
                                      * we marked for close? */
Nick Mathewson's avatar
Nick Mathewson committed
514
515
516
  int hold_open_until_flushed; /**< Despite this connection's being marked
                                * for close, do we flush it before closing it?
                                */
Roger Dingledine's avatar
Roger Dingledine committed
517

518
  buf_t *inbuf; /**< Buffer holding data read over this connection. */
Roger Dingledine's avatar
Roger Dingledine committed
519
520
  int inbuf_reached_eof; /**< Boolean: did read() return 0 on this conn? */
  time_t timestamp_lastread; /**< When was the last time poll() said we could read? */
Roger Dingledine's avatar
Roger Dingledine committed
521

522
  buf_t *outbuf; /**< Buffer holding data to write over this connection. */
Roger Dingledine's avatar
Roger Dingledine committed
523
524
  size_t outbuf_flushlen; /**< How much data should we try to flush from the
                           * outbuf? */
Nick Mathewson's avatar
Nick Mathewson committed
525
  time_t timestamp_lastwritten; /**< When was the last time poll() said we could write? */
526

Roger Dingledine's avatar
Roger Dingledine committed
527
  time_t timestamp_created; /**< When was this connection_t created? */
Roger Dingledine's avatar
Roger Dingledine committed
528

Nick Mathewson's avatar
Nick Mathewson committed
529
530
  uint32_t addr; /**< IP of the other side of the connection; used to identify
                  * routers, along with port. */
531
  uint16_t port; /**< If non-zero, port  on the other end
532
                  * of the connection. */
Nick Mathewson's avatar
Nick Mathewson committed
533
  char *address; /**< FQDN (or IP) of the guy on the other end.
534
                  * strdup into this, because free_connection frees it.
535
                  */
536
  crypto_pk_env_t *identity_pkey; /**< Public RSA key for the other side's
537
                                   * signing key. */
538
  char identity_digest[DIGEST_LEN]; /**< Hash of identity_pkey */
Nick Mathewson's avatar
Nick Mathewson committed
539
  char *nickname; /**< Nickname of OR on other side (if any). */
540

541
542
543
  /** Nickname of planned exit node -- to be used with .exit support. */
  char *chosen_exit_name;

544
/* Used only by OR connections: */
Nick Mathewson's avatar
Nick Mathewson committed
545
546
547
548
  tor_tls *tls; /**< TLS connection state (OR only.) */
  uint16_t next_circ_id; /**< Which circ_id do we try to use next on
                          * this connection?  This is always in the
                          * range 0..1<<15-1. (OR only.)*/
Roger Dingledine's avatar
Roger Dingledine committed
549

550
  /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
551
  int bandwidth; /**< Connection bandwidth. (OPEN ORs only.) */
Roger Dingledine's avatar
Roger Dingledine committed
552
  int receiver_bucket; /**< When this hits 0, stop receiving. Every second we
553
                        * add 'bandwidth' to this, capping it at 10*bandwidth.
Nick Mathewson's avatar
Nick Mathewson committed
554
                        * (OPEN ORs only)
555
                        */
556
557
558
  circ_id_type_t circ_id_type; /**< When we send CREATE cells along this
                                * connection, which half of the space should
                                * we use? */
559

560
/* Used only by DIR and AP connections: */
Nick Mathewson's avatar
Nick Mathewson committed
561
562
  char rend_query[REND_SERVICE_ID_LEN+1]; /**< What rendezvous service are we
                                           * querying for? (DIR/AP only) */
563

564
/* Used only by edge connections: */
Roger Dingledine's avatar
Roger Dingledine committed
565
  size_t stream_size; /**< Used for debugging. */
566
  uint16_t stream_id;
Nick Mathewson's avatar
Nick Mathewson committed
567
568
  struct connection_t *next_stream; /**< Points to the next stream at this
                                     * edge, if any (Edge only). */
Roger Dingledine's avatar
Roger Dingledine committed
569
  struct crypt_path_t *cpath_layer; /**< A pointer to which node in the circ
Nick Mathewson's avatar
Nick Mathewson committed
570
571
572
573
574
575
                                     * this conn exits at. (Edge only.) */
  int package_window; /**< How many more relay cells can i send into the
                       * circuit? (Edge only.) */
  int deliver_window; /**< How many more relay cells can end at me? (Edge
                       * only.) */

576
577
  int done_sending; /**< For half-open connections; not used currently. */
  int done_receiving; /**< For half-open connections; not used currently. */
Roger Dingledine's avatar
Roger Dingledine committed
578
  char has_sent_end; /**< For debugging: set once we've set the stream end,
579
                        and check in circuit_about_to_close_connection(). */
Roger Dingledine's avatar
Roger Dingledine committed
580

581
  /* Used only by AP connections */
Nick Mathewson's avatar
Nick Mathewson committed
582
583
  socks_request_t *socks_request; /**< SOCKS structure describing request (AP
                                   * only.) */
584
585
586

  /* Used only by control connections */
  uint32_t event_mask;
587
588
589
};

typedef struct connection_t connection_t;
Roger Dingledine's avatar
Roger Dingledine committed
590

591
592
#define ADDR_POLICY_ACCEPT 1
#define ADDR_POLICY_REJECT 2
Roger Dingledine's avatar
Roger Dingledine committed
593

594
/** A linked list of policy rules */
595
typedef struct addr_policy_t {
596
  char policy_type; /**< One of ADDR_POLICY_ACCEPT or ADDR_POLICY_REJECT. */
597
598
  char *string; /**< String representation of this rule. */
  uint32_t addr; /**< Base address to accept or reject. */
599
600
  uint32_t msk; /**< Accept/reject all addresses <b>a</b> such that
                 * a &amp; msk == <b>addr</b> &amp; msk . */
601
602
  uint16_t prt_min; /**< Lowest port number to accept/reject. */
  uint16_t prt_max; /**< Highest port number to accept/reject. */
Nick Mathewson's avatar
Nick Mathewson committed
603

604
  struct addr_policy_t *next; /**< Next rule in list. */
605
} addr_policy_t;
Roger Dingledine's avatar
Roger Dingledine committed
606

Nick Mathewson's avatar
Nick Mathewson committed
607
/** Information about another onion router in the network. */
608
typedef struct {
Nick Mathewson's avatar
Nick Mathewson committed
609
610
  char *address; /**< Location of OR: either a hostname or an IP address. */
  char *nickname; /**< Human-readable OR name. */
Roger Dingledine's avatar
Roger Dingledine committed
611

Nick Mathewson's avatar
Nick Mathewson committed
612
  uint32_t addr; /**< IPv4 address of OR, in host order. */
613
614
  uint16_t or_port; /**< Port for OR-to-OR and OP-to-OR connections. */
  uint16_t dir_port; /**< Port for HTTP directory connections. */
615

Nick Mathewson's avatar
Nick Mathewson committed
616
617
  time_t published_on; /**< When was the information in this routerinfo_t
                        * published? */
Roger Dingledine's avatar
Roger Dingledine committed
618

619
620
  crypto_pk_env_t *onion_pkey; /**< Public RSA key for onions. */
  crypto_pk_env_t *identity_pkey;  /**< Public RSA key for signing. */
621
  char identity_digest[DIGEST_LEN]; /**< Digest of identity key */
622

Nick Mathewson's avatar
Nick Mathewson committed
623
  char *platform; /**< What software/operating system is this OR using? */
624

Roger Dingledine's avatar
Roger Dingledine committed
625
  /* link info */
Nick Mathewson's avatar
Nick Mathewson committed
626
627
628
  uint32_t bandwidthrate; /**< How many bytes does this OR add to its token
                           * bucket per second? */
  uint32_t bandwidthburst; /**< How large is this OR's token bucket? */
629
  /** How many bytes/s is this router known to handle? */
630
  uint32_t bandwidthcapacity;
631
  addr_policy_t *exit_policy; /**< What streams will this OR permit
Nick Mathewson's avatar
Nick Mathewson committed
632
                                      * to exit? */
633
  long uptime; /**< How many seconds the router claims to have been up */
634
635
  /* local info */
  int is_running; /**< As far as we know, is this OR currently running? */
636
637
  time_t status_set_at; /**< When did we last update is_running? */
  int is_verified; /**< Has a trusted dirserver validated this OR? */
638

639
640
  smartlist_t *declared_family; /**< Nicknames of router which this router
                                 * claims are its family. */
Roger Dingledine's avatar
Roger Dingledine committed
641
642
} routerinfo_t;

Nick Mathewson's avatar
Nick Mathewson committed
643
/** Contents of a directory of onion routers. */
644
typedef struct {
Nick Mathewson's avatar
Nick Mathewson committed
645
  /** List of routerinfo_t */
646
  smartlist_t *routers;
Nick Mathewson's avatar
Nick Mathewson committed
647
  /** Which versions of tor are recommended by this directory? */
648
  char *software_versions;
649
650
651
  /** When was the most recent directory that contributed to this list
   * published?
   */
652
  time_t published_on;
653
  time_t running_routers_updated_on;
654
655
  /** Which router is claimed to have signed it? */
  char *signing_router;
Roger Dingledine's avatar
Roger Dingledine committed
656
} routerlist_t;
657

Nick Mathewson's avatar
Nick Mathewson committed
658
/** Contents of a running-routers list */
659
typedef struct running_routers_t {
Nick Mathewson's avatar
Nick Mathewson committed
660
661
  time_t published_on; /**< When was the list marked as published? */
  /** Which ORs are on the list?  Entries may be prefixed with ! and $. */
662
  smartlist_t *running_routers;
663
  int is_running_routers_format; /**< Are we using the old entry format? */
664
665
} running_routers_t;

Nick Mathewson's avatar
Nick Mathewson committed
666
667
/** Holds accounting information for a single step in the layered encryption
 * performed by a circuit.  Used only at the client edge of a circuit. */
Roger Dingledine's avatar
Roger Dingledine committed
668
struct crypt_path_t {
669

670
  /* crypto environments */
Nick Mathewson's avatar
Nick Mathewson committed
671
672
  /** Encryption key and counter for cells heading towards the OR at this
   * step. */
673
  crypto_cipher_env_t *f_crypto;
Nick Mathewson's avatar
Nick Mathewson committed
674
675
  /** Encryption key and counter for cells heading back from the OR at this
   * step. */
676
  crypto_cipher_env_t *b_crypto;
677

Nick Mathewson's avatar
Nick Mathewson committed
678
  /** Digest state for cells heading towards the OR at this step. */
679
  crypto_digest_env_t *f_digest; /* for integrity checking */
Nick Mathewson's avatar
Nick Mathewson committed
680
  /** Digest state for cells heading away from the OR at this step. */
681
682
  crypto_digest_env_t *b_digest;

Nick Mathewson's avatar
Nick Mathewson committed
683
684
  /** Current state of Diffie-Hellman key negotiation with the OR at this
   * step. */
685
  crypto_dh_env_t *handshake_state;
Nick Mathewson's avatar
Nick Mathewson committed
686
  /** Negotiated key material shared with the OR at this step. */
687
  char handshake_digest[DIGEST_LEN];/* KH in tor-spec.txt */
688

Nick Mathewson's avatar
Nick Mathewson committed
689
  /** IP4 address of the OR at this step. */
690
  uint32_t addr;
Nick Mathewson's avatar
Nick Mathewson committed
691
  /** Port of the OR at this step. */
692
  uint16_t port;
693
694
  /** Identity key digest of the OR at this step. */
  char identity_digest[DIGEST_LEN];
695

Nick Mathewson's avatar
Nick Mathewson committed
696
697
698
699
700
  /** Is the circuit built to this step?  Must be one of:
   *    - CPATH_STATE_CLOSED (The circuit has not been extended to this step)
   *    - CPATH_STATE_AWAITING_KEYS (We have sent an EXTEND/CREATE to this step
   *      and not received an EXTENDED/CREATED)
   *    - CPATH_STATE_OPEN (The circuit has been extended to this step) */
701
  uint8_t state;
702
#define CPATH_STATE_CLOSED 0
703
#define CPATH_STATE_AWAITING_KEYS 1
704
#define CPATH_STATE_OPEN 2
Nick Mathewson's avatar
Nick Mathewson committed
705
706
707
708
  struct crypt_path_t *next; /**< Link to next crypt_path_t in the circuit.
                              * (The list is circular, so the last node
                              * links to the first.) */
  struct crypt_path_t *prev; /**< Link to previous crypt_path_t in the
709
                              * circuit. */
Nick Mathewson's avatar
Nick Mathewson committed
710
711
712
713
714

  int package_window; /**< How many bytes are we allowed to originate ending
                       * at this step? */
  int deliver_window; /**< How many bytes are we willing to deliver originating
                       * at this step? */
715
};
716

717
718
#define CPATH_KEY_MATERIAL_LEN (20*2+16*2)

719
#define DH_KEY_LEN DH_BYTES
720
721
722
723
#define ONIONSKIN_CHALLENGE_LEN (PKCS1_OAEP_PADDING_OVERHEAD+\
                                 CIPHER_KEY_LEN+\
                                 DH_KEY_LEN)
#define ONIONSKIN_REPLY_LEN (DH_KEY_LEN+DIGEST_LEN)
724
#define REND_COOKIE_LEN DIGEST_LEN
725

726
typedef struct crypt_path_t crypt_path_t;
727

Nick Mathewson's avatar
Nick Mathewson committed
728
/** Information used to build a circuit. */
Roger Dingledine's avatar
Roger Dingledine committed
729
typedef struct {
Roger Dingledine's avatar
Roger Dingledine committed
730
  /** Intended length of the final circuit. */
Roger Dingledine's avatar
Roger Dingledine committed
731
  int desired_path_len;
Roger Dingledine's avatar
Roger Dingledine committed
732
  /** Nickname of planned exit node. */
Nick Mathewson's avatar
Nick Mathewson committed
733
734
735
  char *chosen_exit_name;
  /** Identity of planned exit node. */
  char chosen_exit_digest[DIGEST_LEN];
736
737
738
739
  /** Whether every node in the circ must have adequate uptime. */
  int need_uptime;
  /** Whether every node in the circ must have adequate capacity. */
  int need_capacity;
740
741
  /** Whether the last hop was picked with exiting in mind. */
  int is_internal;
Roger Dingledine's avatar
Roger Dingledine committed
742
  /** The crypt_path_t to append after rendezvous: used for rendezvous. */
743
  struct crypt_path_t *pending_final_cpath;
Nick Mathewson's avatar
Nick Mathewson committed
744
  /** How many times has building a circuit for this task failed? */
745
  int failure_count;
Roger Dingledine's avatar
Roger Dingledine committed
746
} cpath_build_state_t;
747

748
#define CIRCUIT_MAGIC 0x35315243u
749
/** Struct for a path (circuit) through the onion routing network. */
Roger Dingledine's avatar
Roger Dingledine committed
750
struct circuit_t {
751
  uint32_t magic; /**< For memory debugging: must equal CIRCUIT_MAGIC. */
752

Nick Mathewson's avatar
Nick Mathewson committed
753
754
  int marked_for_close; /**< Should we close this circuit at the end of the
                         * main loop? */
755
756
  const char *marked_for_close_file; /**< For debugging: in which file was this
                                      * circuit marked for close? */
Nick Mathewson's avatar
Nick Mathewson committed
757

758
  /** The IPv4 address of the OR that is next in this circuit. */
Roger Dingledine's avatar
Roger Dingledine committed
759
  uint32_t n_addr;
760
  /** The port for the OR that is next in this circuit. */
Roger Dingledine's avatar
Roger Dingledine committed
761
  uint16_t n_port;
762
  /** The OR connection that is previous in this circuit. */
Roger Dingledine's avatar
Roger Dingledine committed
763
  connection_t *p_conn;
764
765
  /** The OR connection that is next in this circuit. */
  connection_t *n_conn;
766
767
  /** The identity hash of n_conn. */
  char n_conn_id_digest[DIGEST_LEN];
768
  /** Linked list of AP streams associated with this circuit. */
769
  connection_t *p_streams;
770
  /** Linked list of Exit streams associated with this circuit. */
771
  connection_t *n_streams;
772
773
  /** Linked list of Exit streams associated with this circuit that are
   * still being resolved. */
774
  connection_t *resolving_streams;
775
776
  /** The next stream_id that will be tried when we're attempting to
   * construct a new AP stream originating at this circuit. */
777
  uint16_t next_stream_id;
778
779
780
  /** How many relay data cells can we package (read from edge streams)
   * on this circuit before we receive a circuit-level sendme cell asking
   * for more? */
781
  int package_window;
782
783
784
785
  /** How many relay data cells will we deliver (write to edge streams)
   * on this circuit? When deliver_window gets low, we send some
   * circuit-level sendme cells to indicate that we're willing to accept
   * more. */
786
  int deliver_window;
Roger Dingledine's avatar
Roger Dingledine committed
787

788
789
790
  /** The circuit_id used in the previous (backward) hop of this circuit. */
  uint16_t p_circ_id;
  /** The circuit_id used in the next (forward) hop of this circuit. */
791
  uint16_t n_circ_id;
Roger Dingledine's avatar
Roger Dingledine committed
792

793
  /** The cipher used by intermediate hops for cells heading toward the
794
   * OP. */
795
796
  crypto_cipher_env_t *p_crypto;
  /** The cipher used by intermediate hops for cells heading away from
797
   * the OP. */
798
  crypto_cipher_env_t *n_crypto;
Roger Dingledine's avatar
Roger Dingledine committed
799

800
801
802
803
804
805
806
807
  /** The integrity-checking digest used by intermediate hops, for
   * cells packaged here and heading towards the OP.
   */
  crypto_digest_env_t *p_digest;
  /** The integrity-checking digest used by intermediate hops, for
   * cells packaged at the OP and arriving here.
   */
  crypto_digest_env_t *n_digest;
808

809
810
811
  /** Build state for this circuit. It includes the intended path
   * length, the chosen exit router, rendezvous information, etc.
   */
812
  cpath_build_state_t *build_state;
813
814
815
816
817
818
819
  /** The doubly-linked list of crypt_path_t entries, one per hop,
   * for this circuit. This includes ciphers for each hop,
   * integrity-checking digests for each hop, and package/delivery
   * windows for each hop.
   *
   * The cpath field is defined only when we are the circuit's origin.
   */