or.h 25.2 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>
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>
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
75
76
77
78
79
80
81
82
83
84
#endif
#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

85
#ifdef MS_WINDOWS
86
87
88
#include <io.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
89
#define snprintf _snprintf
90
91
#endif

92

93
#include "../common/crypto.h"
Roger Dingledine's avatar
Roger Dingledine committed
94
#include "../common/log.h"
95
#include "../common/util.h"
Roger Dingledine's avatar
Roger Dingledine committed
96

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

100
#define MAX_BUF_SIZE (640*1024)
101
#define DEFAULT_BANDWIDTH_OP (1024 * 1000)
Roger Dingledine's avatar
Roger Dingledine committed
102

103
104
105
#define HANDSHAKE_AS_OP 1
#define HANDSHAKE_AS_OR 2

Roger Dingledine's avatar
Roger Dingledine committed
106
107
#define ACI_TYPE_LOWER 0
#define ACI_TYPE_HIGHER 1
108
109
#define ACI_TYPE_BOTH 2

Roger Dingledine's avatar
Roger Dingledine committed
110
111
#define CONN_TYPE_OR_LISTENER 3
#define CONN_TYPE_OR 4
112
113
114
#define CONN_TYPE_EXIT 5
#define CONN_TYPE_AP_LISTENER 6
#define CONN_TYPE_AP 7
115
116
#define CONN_TYPE_DIR_LISTENER 8
#define CONN_TYPE_DIR 9
117
#define CONN_TYPE_DNSWORKER 10
Roger Dingledine's avatar
Roger Dingledine committed
118
119
120

#define LISTENER_STATE_READY 0

121
122
#define DNSWORKER_STATE_IDLE 0
#define DNSWORKER_STATE_BUSY 1
123

124
125
126
127
/* 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."
 */
128
//#define OR_CONN_STATE_OP_CONNECTING 0 /* an application proxy wants me to connect to this OR */
129
#define OR_CONN_STATE_OP_SENDING_KEYS 1
130
#define OR_CONN_STATE_CLIENT_CONNECTING 2 /* connecting to this OR */
131
132
133
134
135
136
137
138
#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. */

139
#define EXIT_CONN_STATE_RESOLVING 0 /* waiting for response from dns farm */
140
#define EXIT_CONN_STATE_CONNECTING 1 /* waiting for connect() to finish */
141
#define EXIT_CONN_STATE_OPEN 2
Roger Dingledine's avatar
Roger Dingledine committed
142
#if 0
143
144
#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
145
146
#endif

147
148
149
#define AP_CONN_STATE_SOCKS_WAIT 3
#define AP_CONN_STATE_OR_WAIT 4
#define AP_CONN_STATE_OPEN 5
150

151
152
153
154
155
156
#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

157
158
159
#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 */
160
161
162
163
#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
164
165
//#define CIRCUIT_STATE_CLOSE 4 /* both acks received, connection is dead */ /* NOT USED */

166
167
168
169
170
#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
171
172
#define RELAY_COMMAND_EXTEND 6
#define RELAY_COMMAND_EXTENDED 7
173
174
#define RELAY_COMMAND_TRUNCATE 8
#define RELAY_COMMAND_TRUNCATED 9
175

176
#define RELAY_HEADER_SIZE 8
177

178
#define RELAY_STATE_RESOLVING
179

180
/* default cipher function */
Roger Dingledine's avatar
Roger Dingledine committed
181
#define DEFAULT_CIPHER CRYPTO_CIPHER_AES_CTR
Nick Mathewson's avatar
src/or    
Nick Mathewson committed
182
183
184
185
186
187
/* Used to en/decrypt onion skins */
#define ONION_CIPHER      DEFAULT_CIPHER
/* Used to en/decrypt cells between ORs/OPs. */
#define CONNECTION_CIPHER DEFAULT_CIPHER
/* Used to en/decrypt RELAY cells */
#define CIRCUIT_CIPHER    DEFAULT_CIPHER
188

189
190
#define CELL_DIRECTION_IN 1
#define CELL_DIRECTION_OUT 2
191
192
#define EDGE_EXIT CONN_TYPE_EXIT
#define EDGE_AP CONN_TYPE_AP
193
#define CELL_DIRECTION(x) ((x) == EDGE_EXIT ? CELL_DIRECTION_IN : CELL_DIRECTION_OUT)
194
195
196
197

#define CIRCWINDOW_START 1000
#define CIRCWINDOW_INCREMENT 100

198
199
#define STREAMWINDOW_START 500
#define STREAMWINDOW_INCREMENT 50
200

Roger Dingledine's avatar
Roger Dingledine committed
201
202
203
/* cell commands */
#define CELL_PADDING 0
#define CELL_CREATE 1
204
205
206
#define CELL_CREATED 2
#define CELL_RELAY 3
#define CELL_DESTROY 4
Roger Dingledine's avatar
Roger Dingledine committed
207

208
209
#define CELL_PAYLOAD_SIZE 248
#define CELL_NETWORK_SIZE 256
Roger Dingledine's avatar
Roger Dingledine committed
210

211
212
213
214
215
216
/* 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
217
#define CONFIG_TYPE_BOOL    5
218
219
220
221
222
223
224
225
226
227
228
229

#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
230
231
typedef uint16_t aci_t;

Roger Dingledine's avatar
Roger Dingledine committed
232
/* cell definition */
233
typedef struct { 
Roger Dingledine's avatar
Roger Dingledine committed
234
235
  aci_t aci; /* Anonymous Connection Identifier */
  unsigned char command;
236
  unsigned char length; /* of payload if relay cell */
Roger Dingledine's avatar
Roger Dingledine committed
237
  uint32_t seq; /* sequence number */
238

239
  unsigned char payload[CELL_PAYLOAD_SIZE];
Roger Dingledine's avatar
Roger Dingledine committed
240
} cell_t;
241
242
#define CELL_RELAY_COMMAND(c)         (*(uint8_t*)((c).payload))
#define SET_CELL_RELAY_COMMAND(c,cmd) (*(uint8_t*)((c).payload) = (cmd))
243
244
245
246
#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
247

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#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;

263
struct connection_t { 
Roger Dingledine's avatar
Roger Dingledine committed
264
265
266

/* Used by all types: */

Roger Dingledine's avatar
Roger Dingledine committed
267
  uint8_t type;
Roger Dingledine's avatar
Roger Dingledine committed
268
  int state;
Roger Dingledine's avatar
Roger Dingledine committed
269
  uint8_t wants_to_read;
Roger Dingledine's avatar
Roger Dingledine committed
270
271
272
273
274
  int s; /* our socket */
  int poll_index;
  int marked_for_close;

  char *inbuf;
275
276
  int inbuflen;
  int inbuf_datalen;
Roger Dingledine's avatar
Roger Dingledine committed
277
  int inbuf_reached_eof;
278
  long timestamp_lastread;
Roger Dingledine's avatar
Roger Dingledine committed
279
280

  char *outbuf;
281
282
283
  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? */
284
285
286
  long timestamp_lastwritten;

  long timestamp_created;
Roger Dingledine's avatar
Roger Dingledine committed
287
288
289
290

/* used by OR and OP: */

  uint32_t bandwidth; /* connection bandwidth */
291
  int receiver_bucket; /* when this hits 0, stop receiving. Every second we
292
293
                        * add 'bandwidth' to this, capping it at 10*bandwidth.
                        */
294
  struct timeval send_timeval; /* for determining when to send the next cell */
Roger Dingledine's avatar
Roger Dingledine committed
295
296

  /* link encryption */
297
298
  crypto_cipher_env_t *f_crypto;
  crypto_cipher_env_t *b_crypto;
Roger Dingledine's avatar
Roger Dingledine committed
299
300
301
302

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

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

306
/* used by exit and ap: */
307
  char stream_id[STREAM_ID_SIZE];
308
  struct connection_t *next_stream;
309
  struct crypt_path_t *cpath_layer; /* a pointer to which node in the circ this conn exits at */
310
311
  int package_window;
  int deliver_window;
312
313
  int done_sending;
  int done_receiving;
314

315
/* Used by ap: */
316
317
  char socks_version; 
  char read_username;
318

319
/* Used by exit and ap: */
320
  char *dest_addr;
321
  uint16_t dest_port; /* host order */
322

323
/* Used by everyone */
324
  char *address; /* strdup into this, because free_connection frees it */
325
/* Used for cell connections */
326
  crypto_pk_env_t *pkey; /* public RSA key for the other side */
Roger Dingledine's avatar
Roger Dingledine committed
327

328
/* Used while negotiating OR/OR connections */
Roger Dingledine's avatar
Roger Dingledine committed
329
  char nonce[8];
330
331
332

/* Used by worker connections */
  int num_processed;
Roger Dingledine's avatar
Roger Dingledine committed
333
 
334
335
336
};

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

Roger Dingledine's avatar
Roger Dingledine committed
338
339
340
341
342
343
344
345
346
347
348
349
#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
350
/* config stuff we know about the other ORs in the network */
351
typedef struct {
Roger Dingledine's avatar
Roger Dingledine committed
352
353
  char *address;
 
354
  uint32_t addr; /* all host order */
355
356
357
  uint16_t or_port;
  uint16_t op_port;
  uint16_t ap_port;
358
  uint16_t dir_port;
Roger Dingledine's avatar
Roger Dingledine committed
359
 
360
  crypto_pk_env_t *pkey; /* public RSA key */
361
  crypto_pk_env_t *signing_pkey; /* May be null */
Roger Dingledine's avatar
Roger Dingledine committed
362
363
 
  /* link info */
364
  uint32_t bandwidth;
Roger Dingledine's avatar
Roger Dingledine committed
365
  struct exit_policy_t *exit_policy;
Roger Dingledine's avatar
Roger Dingledine committed
366
367
} routerinfo_t;

368
#define MAX_ROUTERS_IN_DIR 1024
369
370
371
typedef struct {
  routerinfo_t **routers;
  int n_routers;
372
  char *software_versions;
373
374
} directory_t;

375
struct crypt_path_t { 
376

377
378
379
  /* crypto environments */
  crypto_cipher_env_t *f_crypto;
  crypto_cipher_env_t *b_crypto;
380

381
382
383
384
385
  crypto_dh_env_t *handshake_state;

  uint32_t addr;
  uint16_t port;

386
387
  char state;
#define CPATH_STATE_CLOSED 0
388
#define CPATH_STATE_AWAITING_KEYS 1
389
#define CPATH_STATE_OPEN 2
390
391
392
  struct crypt_path_t *next;
  struct crypt_path_t *prev; /* doubly linked list */

393
394
  int package_window;
  int deliver_window;
395
};
396

397
398
#define DH_KEY_LEN CRYPTO_DH_SIZE
#define DH_ONIONSKIN_LEN DH_KEY_LEN+16
399

400
typedef struct crypt_path_t crypt_path_t;
401

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

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

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

419
  crypt_path_t *cpath;
420

421
  char onionskin[DH_ONIONSKIN_LEN]; /* for storage while onionskin pending */
422
423
  long timestamp_created;
  char dirty; /* whether this circuit has been used yet */
Roger Dingledine's avatar
Roger Dingledine committed
424
425
426

  int state;

427
428
429
//  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
430
431
432
433

  void *next;
} circuit_t;

434
435
436
437
438
struct onion_queue_t {
  circuit_t *circ;
  struct onion_queue_t *next;
};

439
typedef struct {
440
441
   char *LogLevel;
   char *RouterFile;
442
   char *SigningPrivateKeyFile;
443
   char *PrivateKeyFile;
444
   double CoinWeight;
445
   int Daemon;
446
447
   int ORPort;
   int APPort;
448
   int DirPort;
449
   int MaxConn;
450
   int OnionRouter;
451
   int TrafficShaping;
452
   int LinkPadding;
453
454
   int DirRebuildPeriod;
   int DirFetchPeriod;
455
   int KeepalivePeriod;
456
   int MaxOnionsPending;
457
   int NewCircuitPeriod;
458
   int TotalBandwidth;
459
   int NumCpus;
460
   int Role;
461
   int loglevel;
462
} or_options_t;
Roger Dingledine's avatar
Roger Dingledine committed
463
464
465
466
467

    /* all the function prototypes go here */

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

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

470
void buf_free(char *buf);
Roger Dingledine's avatar
Roger Dingledine committed
471

472
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
473
474
  /* grab from s, put onto buf, return how many bytes read */

475
int flush_buf(int s, char **buf, int *buflen, int *buf_flushlen, int *buf_datalen);
Roger Dingledine's avatar
Roger Dingledine committed
476
477
478
479
  /* push from buf onto s
   * then memmove to front of buf
   * return -1 or how many bytes remain on the buf */

480
481
int write_to_buf(char *string, int string_len,
                 char **buf, int *buflen, int *buf_datalen);
Roger Dingledine's avatar
Roger Dingledine committed
482
483
484
485
  /* append string to buf (growing as needed, return -1 if "too big")
   * return total number of bytes on the buf
   */

486

487
int fetch_from_buf(char *string, int string_len,
488
                   char **buf, int *buflen, int *buf_datalen);
489
490
491
492
493
494
495
496
497
498
  /* 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
499
500
501

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

Roger Dingledine's avatar
Roger Dingledine committed
502
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
503
504
505
506
507
508
509
510
511
512
513
514
515

/********************************* 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);
516
circuit_t *circuit_get_newest_ap(void);
517
circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *start, uint32_t naddr, uint16_t nport);
Roger Dingledine's avatar
Roger Dingledine committed
518

519
520
521
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,
522
                crypt_path_t **layer_hint, char *recognized, connection_t **conn);
523
int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn);
Roger Dingledine's avatar
Roger Dingledine committed
524

525
526
527
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);
528

Roger Dingledine's avatar
Roger Dingledine committed
529
void circuit_free(circuit_t *circ);
530
531
void circuit_free_cpath(crypt_path_t *cpath);
void circuit_free_cpath_node(crypt_path_t *victim);
Roger Dingledine's avatar
Roger Dingledine committed
532
533
534
535
536
537

void circuit_close(circuit_t *circ);

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

538
539
void circuit_dump_by_conn(connection_t *conn);

Roger Dingledine's avatar
Roger Dingledine committed
540
void circuit_expire_unused_circuits(void);
541
void circuit_launch_new(int failure_status);
542
int circuit_establish_circuit(void);
543
void circuit_n_conn_open(connection_t *or_conn);
544
545
546
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);
547
int circuit_truncated(circuit_t *circ, crypt_path_t *layer);
548

Roger Dingledine's avatar
Roger Dingledine committed
549
550
551
552
553
/********************************* command.c ***************************/

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

void command_process_create_cell(cell_t *cell, connection_t *conn);
554
void command_process_created_cell(cell_t *cell, connection_t *conn);
555
void command_process_sendme_cell(cell_t *cell, connection_t *conn);
556
void command_process_relay_cell(cell_t *cell, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
557
void command_process_destroy_cell(cell_t *cell, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
558
void command_process_connected_cell(cell_t *cell, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
559
560
561

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

562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* 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
579

580
581
/* return 0 if success, <0 if failure. */
int getconfig(int argc, char **argv, or_options_t *options);
582

Roger Dingledine's avatar
Roger Dingledine committed
583
584
/********************************* connection.c ***************************/

585
586
int tv_cmp(struct timeval *a, struct timeval *b);

Roger Dingledine's avatar
Roger Dingledine committed
587
588
589
590
connection_t *connection_new(int type);

void connection_free(connection_t *conn);

591
int connection_create_listener(struct sockaddr_in *bindaddr, int type);
Roger Dingledine's avatar
Roger Dingledine committed
592
593
594
595

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

/* start all connections that should be up but aren't */
596
int retry_all_connections(uint16_t or_listenport, uint16_t ap_listenport, uint16_t dir_listenport);
Roger Dingledine's avatar
Roger Dingledine committed
597
598
599
600
601

int connection_read_to_buf(connection_t *conn);

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

602
int connection_outbuf_too_full(connection_t *conn);
603
int connection_find_on_inbuf(char *string, int len, connection_t *conn);
604
int connection_wants_to_flush(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
605
606
607
int connection_flush_buf(connection_t *conn);

int connection_write_to_buf(char *string, int len, connection_t *conn);
608
609
610
611
612
613
614
void connection_send_cell(connection_t *conn);

int connection_receiver_bucket_should_increase(connection_t *conn);

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

615
#define connection_speaks_cells(conn) ((conn)->type == CONN_TYPE_OR)
616
int connection_is_listener(connection_t *conn);
617
618
int connection_state_is_open(connection_t *conn);

Roger Dingledine's avatar
Roger Dingledine committed
619
int connection_send_destroy(aci_t aci, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
620
int connection_send_connected(aci_t aci, connection_t *conn);
621
int connection_encrypt_cell(char *cellp, connection_t *conn);
Roger Dingledine's avatar
   
Roger Dingledine committed
622
int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
623
624

int connection_process_inbuf(connection_t *conn);
625
int connection_package_raw_inbuf(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
626
627
int connection_process_cell_from_inbuf(connection_t *conn);

628
int connection_consider_sending_sendme(connection_t *conn, int edge_type);
Roger Dingledine's avatar
Roger Dingledine committed
629
630
int connection_finished_flushing(connection_t *conn);

631
632
633
void cell_pack(char *dest, const cell_t *src);
void cell_unpack(cell_t *dest, const char *src);

634
/********************************* connection_ap.c ****************************/
Roger Dingledine's avatar
Roger Dingledine committed
635

636
int ap_handshake_process_socks(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
637

638
int ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ);
639

640
int ap_handshake_socks_reply(connection_t *conn, char result);
641

642
int connection_ap_create_listener(struct sockaddr_in *bindaddr);
643
644
645

int connection_ap_handle_listener_read(connection_t *conn);

646
647
648
/********************************* connection_edge.c ***************************/

int connection_edge_process_inbuf(connection_t *conn);
649
int connection_edge_send_command(connection_t *fromconn, circuit_t *circ, int relay_command);
650
int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ, connection_t *conn, int edge_type, crypt_path_t *layer_hint);
651
652
int connection_edge_finished_flushing(connection_t *conn);

653
654
/********************************* connection_exit.c ***************************/

Roger Dingledine's avatar
Roger Dingledine committed
655
int connection_exit_send_connected(connection_t *conn);
656
int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
657

658
int connection_exit_connect(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
659
660
661
662
663
664
665
666
667

/********************************* 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);

668
int connection_op_create_listener(struct sockaddr_in *bindaddr);
Roger Dingledine's avatar
Roger Dingledine committed
669
670
671

int connection_op_handle_listener_read(connection_t *conn);

672
/********************************* connection_or.c ***************************/
Roger Dingledine's avatar
Roger Dingledine committed
673

674
675
int connection_or_process_inbuf(connection_t *conn);
int connection_or_finished_flushing(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
676

677
connection_t *connection_or_connect(routerinfo_t *router);
678

679
int connection_or_create_listener(struct sockaddr_in *bindaddr);
680
int connection_or_handle_listener_read(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
681

682
683
684
685
/********************************* directory.c ***************************/

void directory_initiate_fetch(routerinfo_t *router);
int directory_send_command(connection_t *conn);
686
void directory_set_dirty(void);
687
688
689
690
691
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);
692
int connection_dir_create_listener(struct sockaddr_in *bindaddr);
693
694
int connection_dir_handle_listener_read(connection_t *conn);

695
696
/********************************* dns.c ***************************/

697
void dns_init(void);
698
699
int connection_dns_finished_flushing(connection_t *conn);
int connection_dns_process_inbuf(connection_t *conn);
700
void dns_cancel_pending_resolve(char *question, connection_t *onlyconn);
701
int dns_resolve(connection_t *exitconn);
702

Roger Dingledine's avatar
Roger Dingledine committed
703
704
/********************************* main.c ***************************/

705
706
707
708
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
709
710
711
712
int connection_add(connection_t *conn);
int connection_remove(connection_t *conn);
void connection_set_poll_socket(connection_t *conn);

713
714
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
715
716

connection_t *connection_get_by_type(int type);
717
connection_t *connection_get_by_type_state(int type, int state);
718
connection_t *connection_get_by_type_state_lastwritten(int type, int state);
Roger Dingledine's avatar
Roger Dingledine committed
719
720

void connection_watch_events(connection_t *conn, short events);
721
722
void connection_stop_reading(connection_t *conn);
void connection_start_reading(connection_t *conn);
723
724
void connection_stop_writing(connection_t *conn);
void connection_start_writing(connection_t *conn);
Roger Dingledine's avatar
Roger Dingledine committed
725

726
727
728
729
730
731
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); 
732

Roger Dingledine's avatar
Roger Dingledine committed
733
734
735
736
737
738
739
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);

740
741
742
743
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
744

745
746
747
748
749
750
751
/* 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
 */
752
unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *routelen);
753

754
crypt_path_t *onion_generate_cpath(routerinfo_t **firsthop);
755

756
757
int onion_skin_create(crypto_pk_env_t *router_key,
                      crypto_dh_env_t **handshake_state_out,
758
                      char *onion_skin_out); /* Must be DH_ONIONSKIN_LEN bytes long */
759

760
int onion_skin_server_handshake(char *onion_skin, /* DH_ONIONSKIN_LEN bytes long */
761
                                crypto_pk_env_t *private_key,
762
                                char *handshake_reply_out, /* DH_KEY_LEN bytes long */
763
764
765
766
                                char *key_out,
                                int key_out_len);

int onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
767
                             char *handshake_reply,/* Must be DH_KEY_LEN bytes long*/
768
769
770
                             char *key_out,
                             int key_out_len);

Roger Dingledine's avatar
Roger Dingledine committed
771
772
/********************************* routers.c ***************************/

773
774
int learn_my_address(struct sockaddr_in *me);
void router_retry_connections(void);
775
776
routerinfo_t *router_pick_directory_server(void);
routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
777
void router_get_directory(directory_t **pdirectory);
778
int router_is_me(uint32_t addr, uint16_t port);
779
void router_forget_router(uint32_t addr, uint16_t port);
780
int router_get_list_from_file(char *routerfile);
781
int router_resolve(routerinfo_t *router);
782
783
int router_resolve_directory(directory_t *dir);

784
/* Reads a list of known routers, unsigned. */
785
int router_get_list_from_string(char *s);
786
/* Exported for debugging */
787
int router_get_list_from_string_impl(char *s, directory_t **dest);
788
/* Reads a signed directory. */
789
int router_get_dir_from_string(char *s, crypto_pk_env_t *pkey);
790
/* Exported or debugging */
791
792
int router_get_dir_from_string_impl(char *s, directory_t **dest,
                                    crypto_pk_env_t *pkey);
793
routerinfo_t *router_get_entry_from_string(char **s);
794
int router_compare_to_exit_policy(connection_t *conn);
795
void routerinfo_free(routerinfo_t *router);
796

Roger Dingledine's avatar
Roger Dingledine committed
797
#endif
798
799
800
801
802
803
804
805

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