or.h 24.8 KB
Newer Older
1
2
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* 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>
Roger Dingledine's avatar
Roger Dingledine committed
13
14
15
16
17
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <netdb.h>
#include <ctype.h>
18
19
20
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
Nick Mathewson's avatar
Nick Mathewson committed
21
#ifdef HAVE_SYS_POLL_H
Roger Dingledine's avatar
Roger Dingledine committed
22
#include <sys/poll.h>
Nick Mathewson's avatar
Nick Mathewson committed
23
24
25
26
27
#elif HAVE_POLL_H
#include <poll.h>
#else
#include "../common/fakepoll.h"
#endif
Roger Dingledine's avatar
Roger Dingledine committed
28
29
30
31
32
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
33
#include <sys/stat.h>
Roger Dingledine's avatar
Roger Dingledine committed
34
35
36
37
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <assert.h>
38
#include <time.h>
39

40
#include "../common/crypto.h"
Roger Dingledine's avatar
Roger Dingledine committed
41
42
#include "../common/log.h"
#include "../common/ss.h"
43
#include "../common/util.h"
Roger Dingledine's avatar
Roger Dingledine committed
44

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

48
#define MAX_BUF_SIZE (640*1024)
49
#define DEFAULT_BANDWIDTH_OP (1024 * 1000)
Roger Dingledine's avatar
Roger Dingledine committed
50

51
52
53
#define HANDSHAKE_AS_OP 1
#define HANDSHAKE_AS_OR 2

Roger Dingledine's avatar
Roger Dingledine committed
54
55
#define ACI_TYPE_LOWER 0
#define ACI_TYPE_HIGHER 1
56
57
#define ACI_TYPE_BOTH 2

Roger Dingledine's avatar
Roger Dingledine committed
58
59
#define CONN_TYPE_OR_LISTENER 3
#define CONN_TYPE_OR 4
60
61
62
#define CONN_TYPE_EXIT 5
#define CONN_TYPE_AP_LISTENER 6
#define CONN_TYPE_AP 7
63
64
#define CONN_TYPE_DIR_LISTENER 8
#define CONN_TYPE_DIR 9
65
#define CONN_TYPE_DNSWORKER 10
Roger Dingledine's avatar
Roger Dingledine committed
66
67
68

#define LISTENER_STATE_READY 0

69
70
#define DNSWORKER_STATE_IDLE 0
#define DNSWORKER_STATE_BUSY 1
71

72
73
74
75
/* how to read these states:
 * foo_CONN_STATE_bar_baz:
 * "I am acting as a bar, currently in stage baz of talking with a foo."
 */
76
//#define OR_CONN_STATE_OP_CONNECTING 0 /* an application proxy wants me to connect to this OR */
77
#define OR_CONN_STATE_OP_SENDING_KEYS 1
78
#define OR_CONN_STATE_CLIENT_CONNECTING 2 /* connecting to this OR */
79
80
81
82
83
84
85
86
#define OR_CONN_STATE_CLIENT_SENDING_AUTH 3 /* sending address and info */
#define OR_CONN_STATE_CLIENT_AUTH_WAIT 4 /* have sent address and info, waiting */
#define OR_CONN_STATE_CLIENT_SENDING_NONCE 5 /* sending nonce, last piece of handshake */
#define OR_CONN_STATE_SERVER_AUTH_WAIT 6 /* waiting for address and info */
#define OR_CONN_STATE_SERVER_SENDING_AUTH 7 /* writing auth and nonce */
#define OR_CONN_STATE_SERVER_NONCE_WAIT 8 /* waiting for confirmation of nonce */
#define OR_CONN_STATE_OPEN 9 /* ready to send/receive cells. */

87
#define EXIT_CONN_STATE_RESOLVING 0 /* waiting for response from dns farm */
88
#define EXIT_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
89
#define EXIT_CONN_STATE_OPEN 2
Roger Dingledine's avatar
Roger Dingledine committed
90
#if 0
91
92
#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
93
94
#endif

95
96
97
#define AP_CONN_STATE_SOCKS_WAIT 3
#define AP_CONN_STATE_OR_WAIT 4
#define AP_CONN_STATE_OPEN 5
98

99
100
101
102
103
104
#define DIR_CONN_STATE_CONNECTING 0
#define DIR_CONN_STATE_SENDING_COMMAND 1
#define DIR_CONN_STATE_READING 2
#define DIR_CONN_STATE_COMMAND_WAIT 3
#define DIR_CONN_STATE_WRITING 4

105
106
107
#define CIRCUIT_STATE_BUILDING 0 /* I'm the OP, still haven't done all my handshakes */
#define CIRCUIT_STATE_ONIONSKIN_PENDING 1 /* waiting to process the onion */
#define CIRCUIT_STATE_OR_WAIT 2 /* I'm the OP, my firsthop is still connecting */
108
109
110
111
#define CIRCUIT_STATE_OPEN 3 /* onion processed, ready to send data along the connection */
//#define CIRCUIT_STATE_CLOSE_WAIT1 4 /* sent two "destroy" signals, waiting for acks */
//#define CIRCUIT_STATE_CLOSE_WAIT2 5 /* received one ack, waiting for one more 
//                                       (or if just one was sent, waiting for that one */
Roger Dingledine's avatar
Roger Dingledine committed
112
113
//#define CIRCUIT_STATE_CLOSE 4 /* both acks received, connection is dead */ /* NOT USED */

114
115
116
117
118
#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
119
120
#define RELAY_COMMAND_EXTEND 6
#define RELAY_COMMAND_EXTENDED 7
121
122
#define RELAY_COMMAND_TRUNCATE 8
#define RELAY_COMMAND_TRUNCATED 9
123

124
#define RELAY_HEADER_SIZE 8
125

126
#define RELAY_STATE_RESOLVING
127

128
/* default cipher function */
129
#define DEFAULT_CIPHER CRYPTO_CIPHER_3DES
130

131
132
#define CELL_DIRECTION_IN 1
#define CELL_DIRECTION_OUT 2
133
134
#define EDGE_EXIT CONN_TYPE_EXIT
#define EDGE_AP CONN_TYPE_AP
135
#define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
136
137
138
139

#define CIRCWINDOW_START 1000
#define CIRCWINDOW_INCREMENT 100

140
141
#define STREAMWINDOW_START 500
#define STREAMWINDOW_INCREMENT 50
142

Roger Dingledine's avatar
Roger Dingledine committed
143
144
145
/* cell commands */
#define CELL_PADDING 0
#define CELL_CREATE 1
146
147
148
#define CELL_CREATED 2
#define CELL_RELAY 3
#define CELL_DESTROY 4
Roger Dingledine's avatar
Roger Dingledine committed
149

150
151
#define CELL_PAYLOAD_SIZE 248
#define CELL_NETWORK_SIZE 256
Roger Dingledine's avatar
Roger Dingledine committed
152

153
154
155
156
157
158
/* enumeration of types which option values can take */
#define CONFIG_TYPE_STRING  0
#define CONFIG_TYPE_CHAR    1
#define CONFIG_TYPE_INT     2
#define CONFIG_TYPE_LONG    3
#define CONFIG_TYPE_DOUBLE  4
159
#define CONFIG_TYPE_BOOL    5
160
161
162
163
164
165
166
167
168
169
170
171

#define CONFIG_LINE_MAXLEN 1024

/* legal characters in a filename */
#define CONFIG_LEGAL_FILENAME_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/"

struct config_line {
  char *key;
  char *value;
  struct config_line *next;
};

Roger Dingledine's avatar
Roger Dingledine committed
172
173
typedef uint16_t aci_t;

Roger Dingledine's avatar
Roger Dingledine committed
174
/* cell definition */
175
typedef struct { 
Roger Dingledine's avatar
Roger Dingledine committed
176
177
  aci_t aci; /* Anonymous Connection Identifier */
  unsigned char command;
178
  unsigned char length; /* of payload if relay cell */
Roger Dingledine's avatar
Roger Dingledine committed
179
  uint32_t seq; /* sequence number */
180

181
  unsigned char payload[CELL_PAYLOAD_SIZE];
Roger Dingledine's avatar
Roger Dingledine committed
182
} cell_t;
183
184
#define CELL_RELAY_COMMAND(c)         (*(uint8_t*)((c).payload))
#define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
185
186
187
188
#define STREAM_ID_SIZE 7
#define SET_CELL_STREAM_ID(c,id)      memcpy((c).payload+1,(id),STREAM_ID_SIZE)

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

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#define SOCKS4_REQUEST_GRANTED          90
#define SOCKS4_REQUEST_REJECT           91
#define SOCKS4_REQUEST_IDENT_FAILED     92
#define SOCKS4_REQUEST_IDENT_CONFLICT   93

/* structure of a socks client operation */
typedef struct {
   unsigned char version;     /* socks version number */
   unsigned char command;     /* command code */
   unsigned char destport[2]; /* destination port, network order */
   unsigned char destip[4];   /* destination address */
   /* userid follows, terminated by a NULL */
   /* dest host follows, terminated by a NULL */
} socks4_t;

205
struct connection_t { 
Roger Dingledine's avatar
Roger Dingledine committed
206
207
208
209
210
211
212
213
214
215

/* Used by all types: */

  unsigned char type;
  int state;
  int s; /* our socket */
  int poll_index;
  int marked_for_close;

  char *inbuf;
216
217
  int inbuflen;
  int inbuf_datalen;
Roger Dingledine's avatar
Roger Dingledine committed
218
  int inbuf_reached_eof;
219
  long timestamp_lastread;
Roger Dingledine's avatar
Roger Dingledine committed
220
221

  char *outbuf;
222
223
224
  int outbuflen; /* how many bytes are allocated for the outbuf? */
  int outbuf_flushlen; /* how much data should we try to flush from the outbuf? */
  int outbuf_datalen; /* how much data is there total on the outbuf? */
225
226
227
  long timestamp_lastwritten;

  long timestamp_created;
Roger Dingledine's avatar
Roger Dingledine committed
228
229
230
231

/* used by OR and OP: */

  uint32_t bandwidth; /* connection bandwidth */
232
  int receiver_bucket; /* when this hits 0, stop receiving. Every second we
233
234
                        * add 'bandwidth' to this, capping it at 10*bandwidth.
                        */
235
  struct timeval send_timeval; /* for determining when to send the next cell */
Roger Dingledine's avatar
Roger Dingledine committed
236
237

  /* link encryption */
238
239
  crypto_cipher_env_t *f_crypto;
  crypto_cipher_env_t *b_crypto;
Roger Dingledine's avatar
Roger Dingledine committed
240
241
242
243

//  struct timeval lastsend; /* time of last transmission to the client */
//  struct timeval interval; /* transmission interval */

244
  uint32_t addr; /* these two uniquely identify a router. Both in host order. */
Roger Dingledine's avatar
Roger Dingledine committed
245
246
  uint16_t port;

247
/* used by exit and ap: */
248
  char stream_id[STREAM_ID_SIZE];
249
  struct connection_t *next_stream;
250
  struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
251
252
  int package_window;
  int deliver_window;
253
254
  int done_sending;
  int done_receiving;
255

256
/* Used by ap: */
257
258
  char socks_version; 
  char read_username;
259

260
/* Used by exit and ap: */
261
  char *dest_addr;
262
  uint16_t dest_port; /* host order */
263

264
/* Used by everyone */
265
  char *address; /* strdup into this, because free_connection frees it */
266
/* Used for cell connections */
267
  crypto_pk_env_t *pkey; /* public RSA key for the other side */
Roger Dingledine's avatar
Roger Dingledine committed
268

269
/* Used while negotiating OR/OR connections */
Roger Dingledine's avatar
Roger Dingledine committed
270
  char nonce[8];
271
272
273

/* Used by worker connections */
  int num_processed;
Roger Dingledine's avatar
Roger Dingledine committed
274
 
275
276
277
};

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

Roger Dingledine's avatar
Roger Dingledine committed
279
280
281
282
283
284
285
286
287
288
289
290
#define EXIT_POLICY_ACCEPT 1
#define EXIT_POLICY_REJECT 2

struct exit_policy_t {
  char policy_type;
  char *string;
  char *address;
  char *port;

  struct exit_policy_t *next;
};

Roger Dingledine's avatar
Roger Dingledine committed
291
/* config stuff we know about the other ORs in the network */
292
typedef struct {
Roger Dingledine's avatar
Roger Dingledine committed
293
294
  char *address;
 
295
  uint32_t addr; /* all host order */
296
297
298
  uint16_t or_port;
  uint16_t op_port;
  uint16_t ap_port;
299
  uint16_t dir_port;
Roger Dingledine's avatar
Roger Dingledine committed
300
 
301
  crypto_pk_env_t *pkey; /* public RSA key */
302
  crypto_pk_env_t *signing_pkey; /* May be null */
Roger Dingledine's avatar
Roger Dingledine committed
303
304
 
  /* link info */
305
  uint32_t bandwidth;
Roger Dingledine's avatar
Roger Dingledine committed
306
  struct exit_policy_t *exit_policy;
Roger Dingledine's avatar
Roger Dingledine committed
307
308
} routerinfo_t;

309
#define MAX_ROUTERS_IN_DIR 1024
310
311
312
typedef struct {
  routerinfo_t **routers;
  int n_routers;
313
  char *software_versions;
314
315
} directory_t;

316
struct crypt_path_t { 
317

318
319
320
  /* crypto environments */
  crypto_cipher_env_t *f_crypto;
  crypto_cipher_env_t *b_crypto;
321

322
323
324
325
326
  crypto_dh_env_t *handshake_state;

  uint32_t addr;
  uint16_t port;

327
328
  char state;
#define CPATH_STATE_CLOSED 0
329
#define CPATH_STATE_AWAITING_KEYS 1
330
#define CPATH_STATE_OPEN 2
331
332
333
  struct crypt_path_t *next;
  struct crypt_path_t *prev; /* doubly linked list */

334
335
  int package_window;
  int deliver_window;
336
};
337

338
339
#define DH_KEY_LEN CRYPTO_DH_SIZE
#define DH_ONIONSKIN_LEN DH_KEY_LEN+16
340

341
typedef struct crypt_path_t crypt_path_t;
342

343
/* struct for a path (circuit) through the network */
344
typedef struct {
Roger Dingledine's avatar
Roger Dingledine committed
345
346
347
  uint32_t n_addr;
  uint16_t n_port;
  connection_t *p_conn;
348
349
350
  connection_t *n_conn; /* for the OR conn, if there is one */
  connection_t *p_streams;
  connection_t *n_streams;
351
352
  int package_window;
  int deliver_window;
Roger Dingledine's avatar
Roger Dingledine committed
353
354
355
356

  aci_t p_aci; /* connection identifiers */
  aci_t n_aci;

357
  crypto_cipher_env_t *p_crypto; /* used only for intermediate hops */
358
  crypto_cipher_env_t *n_crypto;
Roger Dingledine's avatar
Roger Dingledine committed
359

360
  crypt_path_t *cpath;
361

362
  char onionskin[DH_ONIONSKIN_LEN]; /* for storage while onionskin pending */
363
364
  long timestamp_created;
  char dirty; /* whether this circuit has been used yet */
Roger Dingledine's avatar
Roger Dingledine committed
365
366
367

  int state;

368
369
370
//  unsigned char *onion; /* stores the onion when state is CONN_STATE_OPEN_WAIT */
//  uint32_t onionlen; /* total onion length */
//  uint32_t recvlen; /* length of the onion so far */
Roger Dingledine's avatar
Roger Dingledine committed
371
372
373
374

  void *next;
} circuit_t;

375
376
377
378
379
struct onion_queue_t {
  circuit_t *circ;
  struct onion_queue_t *next;
};

380
typedef struct {
381
382
   char *LogLevel;
   char *RouterFile;
383
   char *SigningPrivateKeyFile;
384
   char *PrivateKeyFile;
385
   double CoinWeight;
386
   int Daemon;
387
388
   int ORPort;
   int APPort;
389
   int DirPort;
390
   int MaxConn;
391
   int OnionRouter;
392
   int TrafficShaping;
393
   int LinkPadding;
394
395
   int DirRebuildPeriod;
   int DirFetchPeriod;
396
   int KeepalivePeriod;
397
   int MaxOnionsPending;
398
   int NewCircuitPeriod;
399
   int Role;
400
   int loglevel;
401
} or_options_t;
Roger Dingledine's avatar
Roger Dingledine committed
402
403
404
405
406

    /* all the function prototypes go here */

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

407
int buf_new(char **buf, int *buflen, int *buf_datalen);
Roger Dingledine's avatar
Roger Dingledine committed
408

409
void buf_free(char *buf);
Roger Dingledine's avatar
Roger Dingledine committed
410

411
int read_to_buf(int s, int at_most, char **buf, int *buflen, int *buf_datalen, int *reached_eof);
Roger Dingledine's avatar
Roger Dingledine committed
412
413
  /* grab from s, put onto buf, return how many bytes read */

414
int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen);
Roger Dingledine's avatar
Roger Dingledine committed
415
416
417
418
  /* push from buf onto s
   * then memmove to front of buf
   * return -1 or how many bytes remain on the buf */

419
420
int write_to_buf(char *string, int string_len,
                 char **buf, int *buflen, int *buf_datalen);
Roger Dingledine's avatar
Roger Dingledine committed
421
422
423
424
  /* append string to buf (growing as needed, return -1 if "too big")
   * return total number of bytes on the buf
   */

425

426
int fetch_from_buf(char *string, int string_len,
427
                   char **buf, int *buflen, int *buf_datalen);
428
429
430
431
432
433
434
435
436
437
  /* if there is string_len bytes in buf, write them onto string,
   * then memmove buf back (that is, remove them from buf)
   */

int find_on_inbuf(char *string, int string_len,
                  char *buf, int buf_datalen);
  /* find first instance of needle 'string' on haystack 'buf'. return how
   * many bytes from the beginning of buf to the end of string.
   * If it's not there, return -1.
   */
Roger Dingledine's avatar
Roger Dingledine committed
438
439
440

/********************************* cell.c ***************************/

Roger Dingledine's avatar
Roger Dingledine committed
441
int pack_create(uint16_t aci, unsigned char *onion, uint32_t onionlen, unsigned char **cellbuf, unsigned int *cellbuflen);
Roger Dingledine's avatar
Roger Dingledine committed
442
443
444
445
446
447
448
449
450
451
452
453
454

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

void circuit_add(circuit_t *circ);
void circuit_remove(circuit_t *circ);

circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn);

/* internal */
aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);

circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn);
circuit_t *circuit_get_by_conn(connection_t *conn);
455
circuit_t *circuit_get_newest_ap(void);
456
circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *start, uint32_t naddr, uint16_t nport);
Roger Dingledine's avatar
Roger Dingledine committed
457

458
459
460
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,
461
                crypt_path_t **layer_hint, char *recognized, connection_t **conn);
462
int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn);
Roger Dingledine's avatar
Roger Dingledine committed
463

464
465
466
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);
467

Roger Dingledine's avatar
Roger Dingledine committed
468
void circuit_free(circuit_t *circ);
469
470
void circuit_free_cpath(crypt_path_t *cpath);
void circuit_free_cpath_node(crypt_path_t *victim);
Roger Dingledine's avatar
Roger Dingledine committed
471
472
473
474
475
476

void circuit_close(circuit_t *circ);

void circuit_about_to_close_connection(connection_t *conn);
  /* flush and send destroys for all circuits using conn */

477
478
void circuit_dump_by_conn(connection_t *conn);

Roger Dingledine's avatar
Roger Dingledine committed
479
void circuit_expire_unused_circuits(void);
480
void circuit_launch_new(int failure_status);
481
int circuit_establish_circuit(void);
482
void circuit_n_conn_open(connection_t *or_conn);
483
484
485
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);
486
int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
487

Roger Dingledine's avatar
Roger Dingledine committed
488
489
490
491
492
/********************************* command.c ***************************/

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

void command_process_create_cell(cell_t *cell, connection_t *conn);
493
void command_process_created_cell(cell_t *cell, connection_t *conn);
494
void command_process_sendme_cell(cell_t *cell, connection_t *conn);
495
void command_process_relay_cell(cell_t *cell, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
496
void command_process_destroy_cell(cell_t *cell, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
497
void command_process_connected_cell(cell_t *cell, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
498
499
500

/********************************* config.c ***************************/

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
const char *basename(const char *filename);

/* open configuration file for reading */
FILE *config_open(const unsigned char *filename);

/* close configuration file */
int config_close(FILE *f);

struct config_line *config_get_commandlines(int argc, char **argv);

/* parse the config file and strdup into key/value strings. Return list.
 *  *  * Warn and ignore mangled lines. */
struct config_line *config_get_lines(FILE *f);

void config_free_lines(struct config_line *front);

int config_compare(struct config_line *c, char *key, int type, void *arg);

void config_assign(or_options_t *options, struct config_line *list);
Roger Dingledine's avatar
Roger Dingledine committed
520

521
522
/* return 0 if success, <0 if failure. */
int getconfig(int argc, char **argv, or_options_t *options);
523

Roger Dingledine's avatar
Roger Dingledine committed
524
525
/********************************* connection.c ***************************/

526
527
int tv_cmp(struct timeval *a, struct timeval *b);

Roger Dingledine's avatar
Roger Dingledine committed
528
529
530
531
connection_t *connection_new(int type);

void connection_free(connection_t *conn);

532
int connection_create_listener(struct sockaddr_in *bindaddr, int type);
Roger Dingledine's avatar
Roger Dingledine committed
533
534
535
536

int connection_handle_listener_read(connection_t *conn, int new_type, int new_state);

/* start all connections that should be up but aren't */
537
int retry_all_connections(uint16_t or_listenport, uint16_t ap_listenport, uint16_t dir_listenport);
Roger Dingledine's avatar
Roger Dingledine committed
538
539
540
541
542

int connection_read_to_buf(connection_t *conn);

int connection_fetch_from_buf(char *string, int len, connection_t *conn);

543
int connection_outbuf_too_full(connection_t *conn);
544
int connection_find_on_inbuf(char *string, int len, connection_t *conn);
545
int connection_wants_to_flush(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
546
547
548
int connection_flush_buf(connection_t *conn);

int connection_write_to_buf(char *string, int len, connection_t *conn);
549
550
551
552
553
554
555
556
void connection_send_cell(connection_t *conn);

int connection_receiver_bucket_should_increase(connection_t *conn);
void connection_increment_receiver_bucket (connection_t *conn);

void connection_increment_send_timeval(connection_t *conn);
void connection_init_timeval(connection_t *conn);

557
#define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
558
int connection_is_listener(connection_t *conn);
559
560
int connection_state_is_open(connection_t *conn);

Roger Dingledine's avatar
Roger Dingledine committed
561
int connection_send_destroy(aci_t aci, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
562
int connection_send_connected(aci_t aci, connection_t *conn);
563
int connection_encrypt_cell(char *cellp, connection_t *conn);
Roger Dingledine's avatar
   
Roger Dingledine committed
564
int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
565
566

int connection_process_inbuf(connection_t *conn);
567
int connection_package_raw_inbuf(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
568
569
int connection_process_cell_from_inbuf(connection_t *conn);

570
int connection_consider_sending_sendme(connection_t *conn, int edge_type);
Roger Dingledine's avatar
Roger Dingledine committed
571
572
int connection_finished_flushing(connection_t *conn);

573
574
575
void cell_pack(char *dest, const cell_t *src);
void cell_unpack(cell_t *dest, const char *src);

576
/********************************* connection_ap.c ****************************/
Roger Dingledine's avatar
Roger Dingledine committed
577

578
int ap_handshake_process_socks(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
579

580
int ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
581

582
int ap_handshake_socks_reply(connection_t *conn, char result);
583

584
int connection_ap_create_listener(struct sockaddr_in *bindaddr);
585
586
587

int connection_ap_handle_listener_read(connection_t *conn);

588
589
590
/********************************* connection_edge.c ***************************/

int connection_edge_process_inbuf(connection_t *conn);
591
int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command);
592
int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn, int edge_type, crypt_path_t *layer_hint);
593
594
int connection_edge_finished_flushing(connection_t *conn);

595
596
/********************************* connection_exit.c ***************************/

Roger Dingledine's avatar
Roger Dingledine committed
597
int connection_exit_send_connected(connection_t *conn);
598
int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
599

600
int connection_exit_connect(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
601
602
603
604
605
606
607
608
609

/********************************* connection_op.c ***************************/

int op_handshake_process_keys(connection_t *conn);

int connection_op_process_inbuf(connection_t *conn);

int connection_op_finished_flushing(connection_t *conn);

610
int connection_op_create_listener(struct sockaddr_in *bindaddr);
Roger Dingledine's avatar
Roger Dingledine committed
611
612
613

int connection_op_handle_listener_read(connection_t *conn);

614
/********************************* connection_or.c ***************************/
Roger Dingledine's avatar
Roger Dingledine committed
615

616
617
int connection_or_process_inbuf(connection_t *conn);
int connection_or_finished_flushing(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
618

Roger Dingledine's avatar
Roger Dingledine committed
619
void connection_or_set_open(connection_t *conn);
620
621
622
623
624
625
626
627
628
629
630
void conn_or_init_crypto(connection_t *conn);

int or_handshake_op_send_keys(connection_t *conn);
int or_handshake_op_finished_sending_keys(connection_t *conn);

int or_handshake_client_process_auth(connection_t *conn);
int or_handshake_client_send_auth(connection_t *conn);

int or_handshake_server_process_auth(connection_t *conn);
int or_handshake_server_process_nonce(connection_t *conn);

631
connection_t *connection_or_connect(routerinfo_t *router);
632

633
int connection_or_create_listener(struct sockaddr_in *bindaddr);
634
int connection_or_handle_listener_read(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
635

636
637
638
639
/********************************* directory.c ***************************/

void directory_initiate_fetch(routerinfo_t *router);
int directory_send_command(connection_t *conn);
640
void directory_set_dirty(void);
641
642
643
644
645
void directory_rebuild(void);
int connection_dir_process_inbuf(connection_t *conn);
int directory_handle_command(connection_t *conn);
int directory_handle_reading(connection_t *conn);
int connection_dir_finished_flushing(connection_t *conn);
646
int connection_dir_create_listener(struct sockaddr_in *bindaddr);
647
648
int connection_dir_handle_listener_read(connection_t *conn);

649
650
651
652
/********************************* dns.c ***************************/

int connection_dns_finished_flushing(connection_t *conn);
int connection_dns_process_inbuf(connection_t *conn);
653
void dns_init(void);
654
int dns_resolve(connection_t *exitconn);
655
656
int dns_master_start(void);

Roger Dingledine's avatar
Roger Dingledine committed
657
658
/********************************* main.c ***************************/

659
660
661
662
void set_privatekey(crypto_pk_env_t *k);
crypto_pk_env_t *get_privatekey(void);
void set_signing_privatekey(crypto_pk_env_t *k);
crypto_pk_env_t *get_signing_privatekey(void);
Roger Dingledine's avatar
Roger Dingledine committed
663
664
665
666
int connection_add(connection_t *conn);
int connection_remove(connection_t *conn);
void connection_set_poll_socket(connection_t *conn);

667
668
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);
Roger Dingledine's avatar
Roger Dingledine committed
669
670

connection_t *connection_get_by_type(int type);
671
connection_t *connection_get_by_type_state(int type, int state);
Roger Dingledine's avatar
Roger Dingledine committed
672
673

void connection_watch_events(connection_t *conn, short events);
674
675
void connection_stop_reading(connection_t *conn);
void connection_start_reading(connection_t *conn);
676
677
void connection_stop_writing(connection_t *conn);
void connection_start_writing(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
678
679
680
681
682

void check_conn_read(int i);
void check_conn_marked(int i);
void check_conn_write(int i);

683
int prepare_for_poll(int *timeout);
Roger Dingledine's avatar
Roger Dingledine committed
684
685
686

int do_main_loop(void);

687
void dumpstats(void);
688
689
690
691
692
693
int dump_signed_directory_to_string(char *s, int maxlen, 
                                    crypto_pk_env_t *private_key);
/* Exported for debugging */
int dump_signed_directory_to_string_impl(char *s, int maxlen, 
                                         directory_t *dir, 
                                         crypto_pk_env_t *private_key); 
694

Roger Dingledine's avatar
Roger Dingledine committed
695
696
697
698
699
700
701
int main(int argc, char *argv[]);

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

int decide_aci_type(uint32_t local_addr, uint16_t local_port,
                    uint32_t remote_addr, uint16_t remote_port);

702
703
704
705
int onion_pending_add(circuit_t *circ);
int onion_pending_check(void);
void onion_pending_process_one(void);
void onion_pending_remove(circuit_t *circ);
Roger Dingledine's avatar
Roger Dingledine committed
706

707
708
709
710
711
712
713
/* uses a weighted coin with weight cw to choose a route length */
int chooselen(double cw);

/* returns an array of pointers to routent that define a new route through the OR network
 * int cw is the coin weight to use when choosing the route 
 * order of routers is from last to first
 */
714
unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *routelen);
715

716
crypt_path_t *onion_generate_cpath(routerinfo_t **firsthop);
717

718
719
int onion_skin_create(crypto_pk_env_t *router_key,
                      crypto_dh_env_t **handshake_state_out,
720
                      char *onion_skin_out); /* Must be DH_ONIONSKIN_LEN bytes long */
721

722
int onion_skin_server_handshake(char *onion_skin, /* DH_ONIONSKIN_LEN bytes long */
723
                                crypto_pk_env_t *private_key,
724
                                char *handshake_reply_out, /* DH_KEY_LEN bytes long */
725
726
727
728
                                char *key_out,
                                int key_out_len);

int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
729
                             char *handshake_reply,/* Must be DH_KEY_LEN bytes long*/
730
731
732
                             char *key_out,
                             int key_out_len);

Roger Dingledine's avatar
Roger Dingledine committed
733
734
/********************************* routers.c ***************************/

735
736
int learn_my_address(struct sockaddr_in *me);
void router_retry_connections(void);
737
738
routerinfo_t *router_pick_directory_server(void);
routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
739
void router_get_directory(directory_t **pdirectory);
740
int router_is_me(uint32_t addr, uint16_t port);
741
void router_forget_router(uint32_t addr, uint16_t port);
742
int router_get_list_from_file(char *routerfile);
743
int router_resolve(routerinfo_t *router);
744
745
int router_resolve_directory(directory_t *dir);

746
/* Reads a list of known routers, unsigned. */
747
int router_get_list_from_string(char *s);
748
/* Exported for debugging */
749
int router_get_list_from_string_impl(char *s, directory_t **dest);
750
/* Reads a signed directory. */
751
int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey);
752
/* Exported or debugging */
753
754
int router_get_dir_from_string_impl(char *s, directory_t **dest,
                                    crypto_pk_env_t *pkey);
755
routerinfo_t *router_get_entry_from_string(char **s);
756
int router_compare_to_exit_policy(connection_t *conn);
757
void routerinfo_free(routerinfo_t *router);
758

Roger Dingledine's avatar
Roger Dingledine committed
759
#endif
760
761
762
763
764
765
766
767

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