or.h 25.2 KB
Newer Older
1
/* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
2
/* See LICENSE for licensing information */
Roger Dingledine's avatar
Roger Dingledine committed
3
4
5
6
7
/* $Id$ */

#ifndef __OR_H
#define __OR_H

Nick Mathewson's avatar
Nick Mathewson committed
8
9
#include "orconfig.h"

Roger Dingledine's avatar
Roger Dingledine committed
10
11
#include <stdio.h>
#include <stdlib.h>
Roger Dingledine's avatar
Roger Dingledine committed
12
#include <limits.h>
13
#ifdef HAVE_UNISTD_H
Roger Dingledine's avatar
Roger Dingledine committed
14
#include <unistd.h>
15
16
#endif
#ifdef HAVE_STRING_H
Roger Dingledine's avatar
Roger Dingledine committed
17
#include <string.h>
18
19
#endif
#ifdef HAVE_SIGNAL_H
Roger Dingledine's avatar
Roger Dingledine committed
20
#include <signal.h>
21
22
#endif
#ifdef HAVE_NETDB_H
Roger Dingledine's avatar
Roger Dingledine committed
23
#include <netdb.h>
24
25
#endif
#ifdef HAVE_CTYPE_H
Roger Dingledine's avatar
Roger Dingledine committed
26
#include <ctype.h>
27
#endif
28
#include "../common/torint.h"
Nick Mathewson's avatar
Nick Mathewson committed
29
#ifdef HAVE_SYS_POLL_H
Roger Dingledine's avatar
Roger Dingledine committed
30
#include <sys/poll.h>
Nick Mathewson's avatar
Nick Mathewson committed
31
32
33
34
35
#elif HAVE_POLL_H
#include <poll.h>
#else
#include "../common/fakepoll.h"
#endif
36
#ifdef HAVE_SYS_TYPES_H
Roger Dingledine's avatar
Roger Dingledine committed
37
#include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
38
#endif
Roger Dingledine's avatar
Roger Dingledine committed
39
40
41
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
42
#ifdef HAVE_SYS_FCNTL_H
Roger Dingledine's avatar
Roger Dingledine committed
43
#include <sys/fcntl.h>
44
45
46
47
48
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
Roger Dingledine's avatar
Roger Dingledine committed
49
#include <sys/ioctl.h>
50
51
#endif
#ifdef HAVE_SYS_SOCKET_H
Roger Dingledine's avatar
Roger Dingledine committed
52
#include <sys/socket.h>
53
54
#endif
#ifdef HAVE_SYS_TIME_H
Roger Dingledine's avatar
Roger Dingledine committed
55
#include <sys/time.h>
56
57
#endif
#ifdef HAVE_SYS_STAT_H
58
#include <sys/stat.h>
59
60
#endif
#ifdef HAVE_NETINET_IN_H
Roger Dingledine's avatar
Roger Dingledine committed
61
#include <netinet/in.h>
62
63
#endif
#ifdef HAVE_ARPA_INET_H
Roger Dingledine's avatar
Roger Dingledine committed
64
#include <arpa/inet.h>
65
66
#endif
#ifdef HAVE_ERRNO_H
Roger Dingledine's avatar
Roger Dingledine committed
67
#include <errno.h>
68
69
#endif
#ifdef HAVE_ASSERT_H
Roger Dingledine's avatar
Roger Dingledine committed
70
#include <assert.h>
71
72
#endif
#ifdef HAVE_TIME_H
73
#include <time.h>
74
#endif
75
76
77
78
79
80
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
81
82
83
84
85
86
87
88
89
90
#ifdef HAVE_WINSOCK_H
#include <winsock.h>
#endif
#if _MSC_VER > 1300
#include <winsock2.h>
#include <ws2tcpip.h>
#elif defined(_MSC_VER)
#include <winsock.h>
#endif

91
#ifdef MS_WINDOWS
92
#include <io.h>
Roger Dingledine's avatar
Roger Dingledine committed
93
#include <process.h>
94
95
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
96
#define snprintf _snprintf
97
98
#endif

99
#include "../common/crypto.h"
100
#include "../common/tortls.h"
Roger Dingledine's avatar
Roger Dingledine committed
101
#include "../common/log.h"
102
#include "../common/util.h"
Roger Dingledine's avatar
Roger Dingledine committed
103

Roger Dingledine's avatar
   
Roger Dingledine committed
104
#define MAXCONNECTIONS 1000 /* upper bound on max connections.
105
                              can be lowered by config file */
Roger Dingledine's avatar
Roger Dingledine committed
106

107
#define DEFAULT_BANDWIDTH_OP (1024 * 1000)
108
#define MAX_NICKNAME_LEN 32
109
#define MAX_DIR_SIZE 50000 /* XXX, big enough? */
Roger Dingledine's avatar
Roger Dingledine committed
110

111
112
#define CIRC_ID_TYPE_LOWER 0
#define CIRC_ID_TYPE_HIGHER 1
113

114
#define _CONN_TYPE_MIN 3
Roger Dingledine's avatar
Roger Dingledine committed
115
116
#define CONN_TYPE_OR_LISTENER 3
#define CONN_TYPE_OR 4
117
118
119
#define CONN_TYPE_EXIT 5
#define CONN_TYPE_AP_LISTENER 6
#define CONN_TYPE_AP 7
120
121
#define CONN_TYPE_DIR_LISTENER 8
#define CONN_TYPE_DIR 9
122
#define CONN_TYPE_DNSWORKER 10
Roger Dingledine's avatar
Roger Dingledine committed
123
#define CONN_TYPE_CPUWORKER 11
124
#define _CONN_TYPE_MAX 11
Roger Dingledine's avatar
Roger Dingledine committed
125
126
127

#define LISTENER_STATE_READY 0

128
129
130
131
#define _DNSWORKER_STATE_MIN 1
#define DNSWORKER_STATE_IDLE 1
#define DNSWORKER_STATE_BUSY 2
#define _DNSWORKER_STATE_MAX 2
132

133
134
135
136
137
#define _CPUWORKER_STATE_MIN 1
#define CPUWORKER_STATE_IDLE 1
#define CPUWORKER_STATE_BUSY_ONION 2
#define CPUWORKER_STATE_BUSY_HANDSHAKE 3
#define _CPUWORKER_STATE_MAX 3
Roger Dingledine's avatar
Roger Dingledine committed
138
139
140

#define CPUWORKER_TASK_ONION CPUWORKER_STATE_BUSY_ONION

141
142
143
144
145
146
147
148
149
150
151
#define _OR_CONN_STATE_MIN 1
#define OR_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
#define OR_CONN_STATE_HANDSHAKING 2 /* SSL is handshaking, not done yet */
#define OR_CONN_STATE_OPEN 3 /* ready to send/receive cells. */
#define _OR_CONN_STATE_MAX 3

#define _EXIT_CONN_STATE_MIN 1
#define EXIT_CONN_STATE_RESOLVING 1 /* waiting for response from dns farm */
#define EXIT_CONN_STATE_CONNECTING 2 /* waiting for connect() to finish */
#define EXIT_CONN_STATE_OPEN 3
#define _EXIT_CONN_STATE_MAX 3
Roger Dingledine's avatar
Roger Dingledine committed
152
#if 0
153
154
#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
155
156
#endif

157
158
159
/* the AP state values must be disjoint from the EXIT state values */
#define _AP_CONN_STATE_MIN 4
#define AP_CONN_STATE_SOCKS_WAIT 4
160
#define AP_CONN_STATE_CIRCUIT_WAIT 5
161
162
163
164
165
166
167
168
169
170
171
172
173
#define AP_CONN_STATE_OPEN 6
#define _AP_CONN_STATE_MAX 6

#define _DIR_CONN_STATE_MIN 1
#define DIR_CONN_STATE_CONNECTING_FETCH 1
#define DIR_CONN_STATE_CONNECTING_UPLOAD 2
#define DIR_CONN_STATE_CLIENT_SENDING_FETCH 3
#define DIR_CONN_STATE_CLIENT_SENDING_UPLOAD 4
#define DIR_CONN_STATE_CLIENT_READING_FETCH 5
#define DIR_CONN_STATE_CLIENT_READING_UPLOAD 6
#define DIR_CONN_STATE_SERVER_COMMAND_WAIT 7
#define DIR_CONN_STATE_SERVER_WRITING 8
#define _DIR_CONN_STATE_MAX 8
174

175
#define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
Roger Dingledine's avatar
Roger Dingledine committed
176
#define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onionskin */
177
#define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
Roger Dingledine's avatar
Roger Dingledine committed
178
#define CIRCUIT_STATE_OPEN 3 /* onionskin(s) processed, ready to send/receive cells */
Roger Dingledine's avatar
Roger Dingledine committed
179

180
181
182
183
184
#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
185
186
#define RELAY_COMMAND_EXTEND 6
#define RELAY_COMMAND_EXTENDED 7
187
188
#define RELAY_COMMAND_TRUNCATE 8
#define RELAY_COMMAND_TRUNCATED 9
189
#define RELAY_COMMAND_DROP 10
190

191
#define RELAY_HEADER_SIZE 8
192

193
194
195
196
197
198
199
#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

200
/* default cipher function */
Roger Dingledine's avatar
Roger Dingledine committed
201
#define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
Nick Mathewson's avatar
src/or    
Nick Mathewson committed
202
203
204
205
/* Used to en/decrypt onion skins */
#define ONION_CIPHER      DEFAULT_CIPHER
/* Used to en/decrypt RELAY cells */
#define CIRCUIT_CIPHER    DEFAULT_CIPHER
206

207
208
#define CELL_DIRECTION_IN 1
#define CELL_DIRECTION_OUT 2
209
210
#define EDGE_EXIT CONN_TYPE_EXIT
#define EDGE_AP CONN_TYPE_AP
211
#define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
212
213
214
215

#define CIRCWINDOW_START 1000
#define CIRCWINDOW_INCREMENT 100

216
217
#define STREAMWINDOW_START 500
#define STREAMWINDOW_INCREMENT 50
218

Roger Dingledine's avatar
Roger Dingledine committed
219
220
221
/* cell commands */
#define CELL_PADDING 0
#define CELL_CREATE 1
222
223
224
#define CELL_CREATED 2
#define CELL_RELAY 3
#define CELL_DESTROY 4
Roger Dingledine's avatar
Roger Dingledine committed
225

226
227
#define CELL_PAYLOAD_SIZE 248
#define CELL_NETWORK_SIZE 256
Roger Dingledine's avatar
Roger Dingledine committed
228

229
230
/* legal characters in a filename */
#define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"
231
232
/* legal characters in a nickname */
#define LEGAL_NICKNAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
233

234
235
#define SOCKS4_NETWORK_LEN 8

236
typedef uint16_t circ_id_t;
Roger Dingledine's avatar
Roger Dingledine committed
237

Roger Dingledine's avatar
Roger Dingledine committed
238
/* cell definition */
239
typedef struct { 
240
  circ_id_t circ_id;
Roger Dingledine's avatar
Roger Dingledine committed
241
  unsigned char command;
242
  unsigned char length; /* of payload if relay cell */
Roger Dingledine's avatar
Roger Dingledine committed
243
  uint32_t seq; /* sequence number */
244

245
  unsigned char payload[CELL_PAYLOAD_SIZE];
Roger Dingledine's avatar
Roger Dingledine committed
246
} cell_t;
247
248
#define CELL_RELAY_COMMAND(c)         (*(uint8_t*)((c).payload))
#define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
249
250
#define STREAM_ID_SIZE 7
#define SET_CELL_STREAM_ID(c,id)      memcpy((c).payload+1,(id),STREAM_ID_SIZE)
251
#define CELL_RELAY_COMMAND_END_REASON(c) (*(uint8_t)((c).payload+1))
252
253

#define ZERO_STREAM "\0\0\0\0\0\0\0\0"
Roger Dingledine's avatar
Roger Dingledine committed
254

255
typedef struct buf_t buf_t;
256
typedef struct socks_request_t socks_request_t;
257

258
struct connection_t { 
Roger Dingledine's avatar
Roger Dingledine committed
259

Roger Dingledine's avatar
Roger Dingledine committed
260
  uint8_t type;
261
262
263
264
  uint8_t state;
  uint8_t wants_to_read; /* should we start reading again once
                          * the bandwidth throttler allows it?
                          */
Roger Dingledine's avatar
Roger Dingledine committed
265
266
267
  uint8_t wants_to_write; /* should we start writing again once
                           * the bandwidth throttler allows reads?
                           */
Roger Dingledine's avatar
Roger Dingledine committed
268
  int s; /* our socket */
269
270
271
272
  int poll_index; /* index of this conn into the poll_array */
  int marked_for_close; /* should we close this conn on the next
                         * iteration of the main loop?
                         */
Roger Dingledine's avatar
Roger Dingledine committed
273

274
  buf_t *inbuf;
275
  int inbuf_reached_eof; /* did read() return 0 on this conn? */
Roger Dingledine's avatar
Roger Dingledine committed
276
  time_t timestamp_lastread; /* when was the last time poll() said we could read? */
Roger Dingledine's avatar
Roger Dingledine committed
277

278
  buf_t *outbuf;
279
  int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
Roger Dingledine's avatar
Roger Dingledine committed
280
  time_t timestamp_lastwritten; /* when was the last time poll() said we could write? */
281

Roger Dingledine's avatar
Roger Dingledine committed
282
  time_t timestamp_created; /* when was this connection_t created? */
Roger Dingledine's avatar
Roger Dingledine committed
283

284
285
286
287
288
289
  uint32_t addr; /* these two uniquely identify a router. Both in host order. */
  uint16_t port; /* if non-zero, they identify the guy on the other end
                  * of the connection. */
  char *address; /* FQDN (or IP) of the guy on the other end.
                  * strdup into this, because free_connection frees it
                  */
290
291
292
  crypto_pk_env_t *onion_pkey; /* public RSA key for the other side's onions */
  crypto_pk_env_t *link_pkey; /* public RSA key for the other side's TLS */
  crypto_pk_env_t *identity_pkey; /* public RSA key for the other side's signing */
293
  char *nickname;
294
295

/* Used only by OR connections: */
296
  tor_tls *tls;
297
298
  circ_id_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.*/
Roger Dingledine's avatar
Roger Dingledine committed
299

300
301
302
303
304
305
  /* bandwidth and receiver_bucket only used by ORs in OPEN state: */
  uint32_t bandwidth; /* connection bandwidth. */
  int receiver_bucket; /* when this hits 0, stop receiving. Every second we
                        * add 'bandwidth' to this, capping it at 10*bandwidth.
                        */

306
/* Used only by edge connections: */
307
  char stream_id[STREAM_ID_SIZE];
308
  struct connection_t *next_stream; /* points to the next stream at this edge, if any */
309
  struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
310
311
  int package_window; /* how many more relay cells can i send into the circuit? */
  int deliver_window; /* how many more relay cells can end at me? */
312

313
314
  int done_sending; /* for half-open connections; not used currently */
  int done_receiving;
315
316
  char has_sent_end; /* for debugging: set once we've set the stream end,
                        and check in circuit_about_to_close_connection() */
317
318
319
  
  /* Used only by AP connections */
  socks_request_t *socks_request;
320
321
322
};

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

Roger Dingledine's avatar
Roger Dingledine committed
324
325
326
327
328
329
#define EXIT_POLICY_ACCEPT 1
#define EXIT_POLICY_REJECT 2

struct exit_policy_t {
  char policy_type;
  char *string;
330
331
332
  uint32_t addr;
  uint32_t msk;
  uint16_t prt;
Roger Dingledine's avatar
Roger Dingledine committed
333
334
335
336

  struct exit_policy_t *next;
};

Roger Dingledine's avatar
Roger Dingledine committed
337
/* config stuff we know about the other ORs in the network */
338
typedef struct {
Roger Dingledine's avatar
Roger Dingledine committed
339
  char *address;
340
  char *nickname;
Roger Dingledine's avatar
Roger Dingledine committed
341
 
342
  uint32_t addr; /* all host order */
343
  uint16_t or_port;
344
  uint16_t socks_port;
345
  uint16_t dir_port;
346
347

  time_t published_on;
Roger Dingledine's avatar
Roger Dingledine committed
348
 
349
350
351
  crypto_pk_env_t *onion_pkey; /* public RSA key for onions */
  crypto_pk_env_t *link_pkey;  /* public RSA key for TLS */
  crypto_pk_env_t *identity_pkey;  /* public RSA key for signing */
Roger Dingledine's avatar
Roger Dingledine committed
352
 
353
354
  int is_running;

Roger Dingledine's avatar
Roger Dingledine committed
355
  /* link info */
356
  uint32_t bandwidth;
Roger Dingledine's avatar
Roger Dingledine committed
357
  struct exit_policy_t *exit_policy;
Roger Dingledine's avatar
Roger Dingledine committed
358
359
} routerinfo_t;

360
#define MAX_ROUTERS_IN_DIR 1024
361
362
363
typedef struct {
  routerinfo_t **routers;
  int n_routers;
364
  char *software_versions;
365
  time_t published_on;
Roger Dingledine's avatar
Roger Dingledine committed
366
} routerlist_t;
367

368
struct crypt_path_t { 
369

370
371
372
  /* crypto environments */
  crypto_cipher_env_t *f_crypto;
  crypto_cipher_env_t *b_crypto;
373

374
375
376
377
378
  crypto_dh_env_t *handshake_state;

  uint32_t addr;
  uint16_t port;

379
  uint8_t state;
380
#define CPATH_STATE_CLOSED 0
381
#define CPATH_STATE_AWAITING_KEYS 1
382
#define CPATH_STATE_OPEN 2
383
384
385
  struct crypt_path_t *next;
  struct crypt_path_t *prev; /* doubly linked list */

386
387
  int package_window;
  int deliver_window;
388
};
389

390
391
#define DH_KEY_LEN CRYPTO_DH_SIZE
#define DH_ONIONSKIN_LEN DH_KEY_LEN+16
392

393
typedef struct crypt_path_t crypt_path_t;
394

Roger Dingledine's avatar
Roger Dingledine committed
395
396
397
398
typedef struct {
  int desired_path_len;
  char *chosen_exit; /* nickname of planned exit node */
} cpath_build_state_t;
399

400
/* struct for a path (circuit) through the network */
Roger Dingledine's avatar
Roger Dingledine committed
401
struct circuit_t {
Roger Dingledine's avatar
Roger Dingledine committed
402
403
404
  uint32_t n_addr;
  uint16_t n_port;
  connection_t *p_conn;
405
406
407
  connection_t *n_conn; /* for the OR conn, if there is one */
  connection_t *p_streams;
  connection_t *n_streams;
408
409
  int package_window;
  int deliver_window;
Roger Dingledine's avatar
Roger Dingledine committed
410

411
412
  circ_id_t p_circ_id; /* circuit identifiers */
  circ_id_t n_circ_id;
Roger Dingledine's avatar
Roger Dingledine committed
413

414
  crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
415
  crypto_cipher_env_t *n_crypto;
Roger Dingledine's avatar
Roger Dingledine committed
416

417
  cpath_build_state_t *build_state;
418
  crypt_path_t *cpath;
419

420
  char onionskin[DH_ONIONSKIN_LEN]; /* for storage while onionskin pending */
Roger Dingledine's avatar
Roger Dingledine committed
421
422
  time_t timestamp_created;
  time_t timestamp_dirty; /* when the circuit was first used, or 0 if clean */
Roger Dingledine's avatar
Roger Dingledine committed
423

424
  uint8_t state;
Roger Dingledine's avatar
Roger Dingledine committed
425
426

  void *next;
Roger Dingledine's avatar
Roger Dingledine committed
427
428
429
};

typedef struct circuit_t circuit_t;
Roger Dingledine's avatar
Roger Dingledine committed
430

431
typedef struct {
432
433
434
435
436
437
438
439
  char *LogLevel;
  char *LogFile;
  char *DebugLogFile;
  char *DataDirectory;
  char *RouterFile;
  char *Nickname;
  char *Address;
  char *PidFile;
440
441
  char *ExitNodes;
  char *EntryNodes;
442
443
444
445
  char *ExitPolicy;
  char *SocksBindAddress;
  char *ORBindAddress;
  char *DirBindAddress;
446
  char *RecommendedVersions;
447
448
  char *User;
  char *Group;
449
  double PathlenCoinWeight;
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  int ORPort;
  int SocksPort;
  int DirPort;
  int MaxConn;
  int TrafficShaping;
  int LinkPadding;
  int IgnoreVersion;
  int RunAsDaemon;
  int DirRebuildPeriod;
  int DirFetchPostPeriod;
  int KeepalivePeriod;
  int MaxOnionsPending;
  int NewCircuitPeriod;
  int TotalBandwidth;
  int NumCpus;
  int loglevel;
466
} or_options_t;
Roger Dingledine's avatar
Roger Dingledine committed
467

468
469
470
471
472
473
474
#define MAX_SOCKS_REPLY_LEN 256
/* Not 256; addresses must fit in a begin cell. */
#define MAX_SOCKS_ADDR_LEN 200
struct socks_request_t {
  char socks_version;
  int replylen;
  char reply[MAX_SOCKS_REPLY_LEN];
Roger Dingledine's avatar
Roger Dingledine committed
475
  char address[MAX_SOCKS_ADDR_LEN];
476
477
478
  uint16_t port;
};

479
/* all the function prototypes go here */
Roger Dingledine's avatar
Roger Dingledine committed
480
481
482

/********************************* buffers.c ***************************/

483
484
int find_on_inbuf(char *string, int string_len, buf_t *buf);

485
486
487
488
489
490
491
buf_t *buf_new();
buf_t *buf_new_with_capacity(size_t size);
void buf_free(buf_t *buf);

size_t buf_datalen(const buf_t *buf);
size_t buf_capacity(const buf_t *buf);
const char *_buf_peek_raw_buffer(const buf_t *buf);
Roger Dingledine's avatar
Roger Dingledine committed
492

493
494
int read_to_buf(int s, int at_most, buf_t *buf, int *reached_eof);
int read_to_buf_tls(tor_tls *tls, int at_most, buf_t *buf);
Roger Dingledine's avatar
Roger Dingledine committed
495

496
497
int flush_buf(int s, buf_t *buf, int *buf_flushlen);
int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen);
Roger Dingledine's avatar
Roger Dingledine committed
498

499
int write_to_buf(const char *string, int string_len, buf_t *buf);
500
501
int fetch_from_buf(char *string, int string_len, buf_t *buf);
int fetch_from_buf_http(buf_t *buf,
502
503
                        char *headers_out, int max_headerlen,
                        char *body_out, int max_bodylen);
504
int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
Roger Dingledine's avatar
Roger Dingledine committed
505
506
507
508
509

/********************************* circuit.c ***************************/

void circuit_add(circuit_t *circ);
void circuit_remove(circuit_t *circ);
510
circuit_t *circuit_new(circ_id_t p_circ_id, connection_t *p_conn);
Roger Dingledine's avatar
Roger Dingledine committed
511
void circuit_free(circuit_t *circ);
512
void circuit_free_cpath(crypt_path_t *cpath);
Roger Dingledine's avatar
Roger Dingledine committed
513

514
circuit_t *circuit_get_by_circ_id_conn(circ_id_t circ_id, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
515
circuit_t *circuit_get_by_conn(connection_t *conn);
516
circuit_t *circuit_get_newest(connection_t *conn, int must_be_open);
Roger Dingledine's avatar
Roger Dingledine committed
517

518
519
520
void circuit_expire_building(void);
int circuit_count_building(void);

521
522
523
int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
                               int cell_direction, crypt_path_t *layer_hint);
int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
524
                crypt_path_t **layer_hint, char *recognized, connection_t **conn);
525
int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn);
Roger Dingledine's avatar
Roger Dingledine committed
526

527
528
529
void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint);
530

Roger Dingledine's avatar
Roger Dingledine committed
531
532
533
void circuit_close(circuit_t *circ);
void circuit_about_to_close_connection(connection_t *conn);

Nick Mathewson's avatar
Nick Mathewson committed
534
void circuit_dump_by_conn(connection_t *conn, int severity);
535

Roger Dingledine's avatar
Roger Dingledine committed
536
void circuit_expire_unused_circuits(void);
537
538
539
int circuit_launch_new(void);
void circuit_increment_failure_count(void);
void circuit_reset_failure_count(void);
540
int circuit_establish_circuit(void);
541
void circuit_n_conn_open(connection_t *or_conn);
542
543
544
int circuit_send_next_onion_skin(circuit_t *circ);
int circuit_extend(cell_t *cell, circuit_t *circ);
int circuit_finish_handshake(circuit_t *circ, char *reply);
545
int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
546

547
548
549
void assert_cpath_ok(const crypt_path_t *c);
void assert_cpath_layer_ok(const crypt_path_t *c);
void assert_circuit_ok(const circuit_t *c);
550

551
552
553
extern unsigned long stats_n_relay_cells_relayed;
extern unsigned long stats_n_relay_cells_delivered;

Roger Dingledine's avatar
Roger Dingledine committed
554
555
556
557
/********************************* command.c ***************************/

void command_process_cell(cell_t *cell, connection_t *conn);

558
559
560
561
562
563
extern unsigned long stats_n_padding_cells_processed;
extern unsigned long stats_n_create_cells_processed;
extern unsigned long stats_n_created_cells_processed;
extern unsigned long stats_n_relay_cells_processed;
extern unsigned long stats_n_destroy_cells_processed;

Roger Dingledine's avatar
Roger Dingledine committed
564
565
/********************************* config.c ***************************/

566
int getconfig(int argc, char **argv, or_options_t *options);
567

Roger Dingledine's avatar
Roger Dingledine committed
568
569
570
571
572
/********************************* connection.c ***************************/

connection_t *connection_new(int type);
void connection_free(connection_t *conn);

573
int connection_create_listener(char *bindaddress, uint16_t bindport, int type);
574

575
int connection_connect(connection_t *conn, char *address, uint32_t addr, uint16_t port);
576
int retry_all_connections(void);
Roger Dingledine's avatar
Roger Dingledine committed
577

578
int connection_handle_read(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
579
580
581
int connection_read_to_buf(connection_t *conn);

int connection_fetch_from_buf(char *string, int len, connection_t *conn);
582
int connection_find_on_inbuf(char *string, int len, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
583

584
int connection_wants_to_flush(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
585
int connection_outbuf_too_full(connection_t *conn);
586
int connection_handle_write(connection_t *conn);
587
void connection_write_to_buf(const char *string, int len, connection_t *conn);
588

589
590
591
592
593
594
595
connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);

connection_t *connection_get_by_type(int type);
connection_t *connection_get_by_type_state(int type, int state);
connection_t *connection_get_by_type_state_lastwritten(int type, int state);

596
597
int connection_receiver_bucket_should_increase(connection_t *conn);

598
#define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
Nick Mathewson's avatar
Nick Mathewson committed
599
600
601
602
#define connection_has_pending_tls_data(conn) \
  ((conn)->type == CONN_TYPE_OR && \
   (conn)->state == OR_CONN_STATE_OPEN && \
   tor_tls_get_pending_bytes(conn->tls))
603
int connection_is_listener(connection_t *conn);
604
605
int connection_state_is_open(connection_t *conn);

606
int connection_send_destroy(circ_id_t circ_id, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
607
608
609
610

int connection_process_inbuf(connection_t *conn);
int connection_finished_flushing(connection_t *conn);

611
612
void assert_connection_ok(connection_t *conn, time_t now);

613
614
615
/********************************* connection_edge.c ***************************/

int connection_edge_process_inbuf(connection_t *conn);
616
void connection_edge_end(connection_t *conn, char reason, crypt_path_t *cpath_layer);
617

618
619
int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command,
                                 void *payload, int payload_len, crypt_path_t *cpath_layer);
620

Roger Dingledine's avatar
Roger Dingledine committed
621
622
int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn,
                                       int edge_type, crypt_path_t *layer_hint);
623
624
int connection_edge_finished_flushing(connection_t *conn);

Nick Mathewson's avatar
Nick Mathewson committed
625
int connection_edge_package_raw_inbuf(connection_t *conn);
626

627
void connection_exit_connect(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
628

629
630
void connection_ap_attach_pending(void);

631
632
633
634
635
extern uint64_t stats_n_data_cells_packaged;
extern uint64_t stats_n_data_bytes_packaged;
extern uint64_t stats_n_data_cells_received;
extern uint64_t stats_n_data_bytes_received;

Roger Dingledine's avatar
Roger Dingledine committed
636
void client_dns_init(void);
637
void client_dns_clean(void);
Roger Dingledine's avatar
Roger Dingledine committed
638

639
/********************************* connection_or.c ***************************/
Roger Dingledine's avatar
Roger Dingledine committed
640

641
642
int connection_or_process_inbuf(connection_t *conn);
int connection_or_finished_flushing(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
643

644
void connection_or_init_conn_from_router(connection_t *conn, routerinfo_t *router);
645
connection_t *connection_or_connect(routerinfo_t *router);
646

647
648
649
int connection_tls_start_handshake(connection_t *conn, int receiving);
int connection_tls_continue_handshake(connection_t *conn);

Nick Mathewson's avatar
Nick Mathewson committed
650
void connection_or_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
651

Roger Dingledine's avatar
Roger Dingledine committed
652
653
654
655
656
657
658
659
660
/********************************* cpuworker.c *****************************/

void cpu_init(void);
int connection_cpu_finished_flushing(connection_t *conn);
int connection_cpu_process_inbuf(connection_t *conn);
int cpuworker_main(void *data);
int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
                        void *task);

661
662
/********************************* directory.c ***************************/

663
void directory_initiate_command(routerinfo_t *router, int command);
664
665
666
int connection_dir_process_inbuf(connection_t *conn);
int connection_dir_finished_flushing(connection_t *conn);

667
668
/********************************* dns.c ***************************/

669
void dns_init(void);
670
671
int connection_dns_finished_flushing(connection_t *conn);
int connection_dns_process_inbuf(connection_t *conn);
672
void dns_cancel_pending_resolve(char *question, connection_t *onlyconn);
673
int dns_resolve(connection_t *exitconn);
674

Roger Dingledine's avatar
Roger Dingledine committed
675
676
677
678
679
680
/********************************* main.c ***************************/

int connection_add(connection_t *conn);
int connection_remove(connection_t *conn);
void connection_set_poll_socket(connection_t *conn);

Nick Mathewson's avatar
Nick Mathewson committed
681
682
void get_connection_array(connection_t ***array, int *n);

Roger Dingledine's avatar
Roger Dingledine committed
683
void connection_watch_events(connection_t *conn, short events);
Roger Dingledine's avatar
Roger Dingledine committed
684
int connection_is_reading(connection_t *conn);
685
686
void connection_stop_reading(connection_t *conn);
void connection_start_reading(connection_t *conn);
687
688
void connection_stop_writing(connection_t *conn);
void connection_start_writing(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
689
690
691
692
693

int main(int argc, char *argv[]);

/********************************* onion.c ***************************/

694
int decide_circ_id_type(char *local_nick, char *remote_nick);
Roger Dingledine's avatar
Roger Dingledine committed
695

696
int onion_pending_add(circuit_t *circ);
Roger Dingledine's avatar
Roger Dingledine committed
697
circuit_t *onion_next_task(void);
698
void onion_pending_remove(circuit_t *circ);
Roger Dingledine's avatar
Roger Dingledine committed
699

700
int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys);
Roger Dingledine's avatar
Roger Dingledine committed
701

702
703
char **parse_nickname_list(char *start, int *num);

704
705
int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state, 
                       routerinfo_t **router_out);
706

707
708
int onion_skin_create(crypto_pk_env_t *router_key,
                      crypto_dh_env_t **handshake_state_out,
709
                      char *onion_skin_out); /* Must be DH_ONIONSKIN_LEN bytes long */
710

711
int onion_skin_server_handshake(char *onion_skin, /* DH_ONIONSKIN_LEN bytes long */
712
                                crypto_pk_env_t *private_key,
713
                                char *handshake_reply_out, /* DH_KEY_LEN bytes long */
714
715
716
717
                                char *key_out,
                                int key_out_len);

int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
718
                             char *handshake_reply,/* Must be DH_KEY_LEN bytes long*/
719
720
721
                             char *key_out,
                             int key_out_len);

722
cpath_build_state_t *onion_new_cpath_build_state(void);
723

724
725
726
727
728
729
730
731
/********************************* router.c ***************************/

void set_onion_key(crypto_pk_env_t *k);
crypto_pk_env_t *get_onion_key(void);
void set_identity_key(crypto_pk_env_t *k);
crypto_pk_env_t *get_identity_key(void);
crypto_pk_env_t *get_link_key(void);
int init_keys(void);
Roger Dingledine's avatar
Roger Dingledine committed
732

733
void router_retry_connections(void);
734
735
736
737
738
739
740
741
742
void router_upload_desc_to_dirservers(void);
int router_compare_to_my_exit_policy(connection_t *conn);
const char *router_get_my_descriptor(void);
int router_rebuild_descriptor(void);
int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
                                 crypto_pk_env_t *ident_key);

/********************************* routerlist.c ***************************/

743
routerinfo_t *router_pick_directory_server(void);
744
routerinfo_t *router_pick_randomly_from_running(void);
745
routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
746
routerinfo_t *router_get_by_link_pk(crypto_pk_env_t *pk);
747
routerinfo_t *router_get_by_nickname(char *nickname);
748
749
void router_get_routerlist(routerlist_t **prouterlist);
void routerinfo_free(routerinfo_t *router);
750
void router_mark_as_down(char *nickname);
751
int router_set_routerlist_from_file(char *routerfile);
Nick Mathewson's avatar
Nick Mathewson committed
752
int router_get_dir_hash(char *s, char *digest);
753
754
755
756
int router_get_router_hash(char *s, char *digest);
int router_set_routerlist_from_directory(char *s, crypto_pk_env_t *pkey);
routerinfo_t *router_get_entry_from_string(char**s);
int router_add_exit_policy_from_string(routerinfo_t *router, char *s);
757
758
int router_supports_exit_address(uint32_t addr, uint16_t port,
                                 routerinfo_t *router);
759
760
int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
                                       struct exit_policy_t *policy);
761
762
int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port);
int router_exit_policy_rejects_all(routerinfo_t *router);
763

764
/********************************* dirserv.c ***************************/
Nick Mathewson's avatar
Nick Mathewson committed
765
int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk);
766
767
768
769
770
771
772
773
774
775
776
777
int dirserv_parse_fingerprint_file(const char *fname);
int dirserv_router_fingerprint_is_known(const routerinfo_t *router);
void dirserv_free_fingerprint_list();
int dirserv_add_descriptor(const char **desc);
int dirserv_init_from_directory_string(const char *dir);
void dirserv_free_descriptors();
int dirserv_dump_directory_to_string(char *s, int maxlen,
                                     crypto_pk_env_t *private_key);
void directory_set_dirty();
size_t dirserv_get_directory(const char **cp);


Roger Dingledine's avatar
Roger Dingledine committed
778
#endif
779
780
781
782
783
784
785
786

/*
  Local Variables:
  mode:c
  indent-tabs-mode:nil
  c-basic-offset:2
  End:
*/