or.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* Copyright (c) 2002 Roger Dingledine. See LICENSE for licensing information */
  2. /* $Id$ */
  3. #ifndef __OR_H
  4. #define __OR_H
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <signal.h>
  10. #include <netdb.h>
  11. #include <ctype.h>
  12. #include <sys/poll.h>
  13. #include <sys/types.h>
  14. #include <sys/fcntl.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/socket.h>
  17. #include <sys/mman.h>
  18. #include <sys/stat.h>
  19. #include <sys/time.h>
  20. #include <netinet/in.h>
  21. #include <arpa/inet.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <openssl/err.h>
  25. #include <openssl/rsa.h>
  26. #include <openssl/pem.h>
  27. #include <openssl/evp.h>
  28. #include <openssl/rand.h>
  29. #include "../common/cell.h"
  30. #include "../common/config.h"
  31. #include "../common/key.h"
  32. #include "../common/log.h"
  33. #include "../common/onion.h"
  34. #include "../common/ss.h"
  35. #define MAXCONNECTIONS 200 /* upper bound on max connections.
  36. can be overridden by config file */
  37. #define MAX_BUF_SIZE (64*1024)
  38. #define ACI_TYPE_LOWER 0
  39. #define ACI_TYPE_HIGHER 1
  40. #define CONN_TYPE_OP_LISTENER 1
  41. #define CONN_TYPE_OP 2
  42. #define CONN_TYPE_OR_LISTENER 3
  43. #define CONN_TYPE_OR 4
  44. #define CONN_TYPE_APP 5
  45. #define LISTENER_STATE_READY 0
  46. #define OP_CONN_STATE_AWAITING_KEYS 0
  47. #define OP_CONN_STATE_OPEN 1
  48. #if 0
  49. #define OP_CONN_STATE_CLOSE 2 /* flushing the buffer, then will close */
  50. #define OP_CONN_STATE_CLOSE_WAIT 3 /* have sent a destroy, awaiting a confirmation */
  51. #endif
  52. #define OR_CONN_STATE_CLIENT_CONNECTING 0
  53. #define OR_CONN_STATE_CLIENT_SENDING_AUTH 1 /* sending address and info */
  54. #define OR_CONN_STATE_CLIENT_AUTH_WAIT 2 /* have sent address and info, waiting */
  55. #define OR_CONN_STATE_CLIENT_SENDING_NONCE 3 /* sending nonce, last piece of handshake */
  56. #define OR_CONN_STATE_SERVER_AUTH_WAIT 4 /* waiting for address and info */
  57. #define OR_CONN_STATE_SERVER_SENDING_AUTH 5 /* writing auth and nonce */
  58. #define OR_CONN_STATE_SERVER_NONCE_WAIT 6 /* waiting for confirmation of nonce */
  59. #define OR_CONN_STATE_OPEN 7 /* ready to send/receive cells. */
  60. #define APP_CONN_STATE_CONNECTING_WAIT 0 /* waiting for standard structure or dest info */
  61. #define APP_CONN_STATE_CONNECTING 1
  62. #define APP_CONN_STATE_OPEN 2
  63. #if 0
  64. #define APP_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  65. #define APP_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  66. #endif
  67. #define CIRCUIT_STATE_OPEN_WAIT 0 /* receiving/processing the onion */
  68. #define CIRCUIT_STATE_OPEN 1 /* onion processed, ready to send data along the connection */
  69. #define CIRCUIT_STATE_CLOSE_WAIT1 2 /* sent two "destroy" signals, waiting for acks */
  70. #define CIRCUIT_STATE_CLOSE_WAIT2 3 /* received one ack, waiting for one more
  71. (or if just one was sent, waiting for that one */
  72. //#define CIRCUIT_STATE_CLOSE 4 /* both acks received, connection is dead */ /* NOT USED */
  73. typedef uint16_t aci_t;
  74. typedef struct
  75. {
  76. /* Used by all types: */
  77. unsigned char type;
  78. int state;
  79. int s; /* our socket */
  80. int poll_index;
  81. int marked_for_close;
  82. char *inbuf;
  83. size_t inbuflen;
  84. size_t inbuf_datalen;
  85. int inbuf_reached_eof;
  86. char *outbuf;
  87. size_t outbuflen;
  88. size_t outbuf_datalen;
  89. /* used by OP and App: */
  90. uint16_t aci; /* anonymous connection identifier */
  91. /* used by OR and OP: */
  92. uint32_t bandwidth; /* connection bandwidth */
  93. int window_sent; /* how many cells can i still send? */
  94. int window_received; /* how many cells do i still expect to receive? */
  95. /* link encryption */
  96. unsigned char f_session_key[8];
  97. unsigned char b_session_key[8];
  98. unsigned char f_session_iv[8];
  99. unsigned char b_session_iv[8];
  100. EVP_CIPHER_CTX f_ctx;
  101. EVP_CIPHER_CTX b_ctx;
  102. // struct timeval lastsend; /* time of last transmission to the client */
  103. // struct timeval interval; /* transmission interval */
  104. uint32_t addr; /* these two uniquely identify a router */
  105. uint16_t port;
  106. /* used by app: */
  107. ss_t ss; /* standard structure */
  108. int ss_received; /* size of ss, received so far */
  109. /* used by OR, to keep state while connect()ing: Kludge. */
  110. RSA *prkey;
  111. struct sockaddr_in local;
  112. /* link info */
  113. uint32_t min;
  114. uint32_t max;
  115. char *address; /* strdup into this, gets free_connection frees it */
  116. RSA *pkey; /* public RSA key for the other side */
  117. char nonce[8];
  118. } connection_t;
  119. /* config stuff we know about the other ORs in the network */
  120. typedef struct
  121. {
  122. char *address;
  123. uint32_t addr;
  124. uint16_t port;
  125. RSA *pkey; /* public RSA key */
  126. /* link info */
  127. uint32_t min;
  128. uint32_t max;
  129. // struct timeval min_interval;
  130. /* time when last data was sent to that router */
  131. // struct timeval lastsend;
  132. /* socket */
  133. // int s;
  134. void *next;
  135. } routerinfo_t;
  136. /* per-anonymous-connection struct */
  137. typedef struct
  138. {
  139. #if 0
  140. uint32_t p_addr; /* all in network order */
  141. uint16_t p_port;
  142. #endif
  143. uint32_t n_addr;
  144. uint16_t n_port;
  145. connection_t *p_conn;
  146. connection_t *n_conn;
  147. aci_t p_aci; /* connection identifiers */
  148. aci_t n_aci;
  149. unsigned char p_f; /* crypto functions */
  150. unsigned char n_f;
  151. unsigned char p_key[128]; /* crypto keys */
  152. unsigned char n_key[128];
  153. unsigned char p_iv[16]; /* initialization vectors */
  154. unsigned char n_iv[16];
  155. EVP_CIPHER_CTX p_ctx; /* cipher context */
  156. EVP_CIPHER_CTX n_ctx;
  157. uint32_t expire; /* expiration time for the corresponding onion */
  158. int state;
  159. unsigned char *onion; /* stores the onion when state is CONN_STATE_OPEN_WAIT */
  160. uint32_t onionlen; /* total onion length */
  161. uint32_t recvlen; /* length of the onion so far */
  162. void *next;
  163. } circuit_t;
  164. /* all the function prototypes go here */
  165. /********************************* args.c ***************************/
  166. /* print help*/
  167. void print_usage();
  168. /* get command-line arguments */
  169. int getargs(int argc,char *argv[], char *args,char **conf_filename, int *loglevel);
  170. /********************************* buffers.c ***************************/
  171. int buf_new(char **pbuf, size_t *pbuflen, size_t *pbuf_datalen);
  172. int buf_free(char *buf);
  173. int read_to_buf(int s, char **pbuf, size_t *pbuflen, size_t *pbuf_datalen, int *preached_eof);
  174. /* grab from s, put onto buf, return how many bytes read */
  175. int flush_buf(int s, char **pbuf, size_t *pbuflen, size_t *pbuf_datalen);
  176. /* push from buf onto s
  177. * then memmove to front of buf
  178. * return -1 or how many bytes remain on the buf */
  179. int write_to_buf(char *string, size_t string_len,
  180. char **pbuf, size_t *pbuflen, size_t *pbuf_datalen);
  181. /* append string to buf (growing as needed, return -1 if "too big")
  182. * return total number of bytes on the buf
  183. */
  184. int fetch_from_buf(char *string, size_t string_len,
  185. char **pbuf, size_t *pbuflen, size_t *pbuf_datalen);
  186. /* if there is string_len bytes in buf, write them onto string,
  187. * * then memmove buf back (that is, remove them from buf) */
  188. /********************************* cell.c ***************************/
  189. int check_sane_cell(cell_t *cell);
  190. /********************************* circuit.c ***************************/
  191. void circuit_add(circuit_t *circ);
  192. void circuit_remove(circuit_t *circ);
  193. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn);
  194. /* internal */
  195. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);
  196. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn);
  197. circuit_t *circuit_get_by_conn(connection_t *conn);
  198. int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, connection_t *conn, int crypt_type);
  199. int circuit_crypt(circuit_t *circ, char *in, size_t inlen, char crypt_type);
  200. int circuit_init(circuit_t *circ, int aci_type);
  201. void circuit_free(circuit_t *circ);
  202. void circuit_close(circuit_t *circ);
  203. void circuit_about_to_close_connection(connection_t *conn);
  204. /* flush and send destroys for all circuits using conn */
  205. /********************************* command.c ***************************/
  206. void command_process_cell(cell_t *cell, connection_t *conn);
  207. void command_process_create_cell(cell_t *cell, connection_t *conn);
  208. void command_process_data_cell(cell_t *cell, connection_t *conn);
  209. void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  210. /********************************* config.c ***************************/
  211. /* loads the configuration file */
  212. int getconfig(char *filename, config_opt_t *options);
  213. /********************************* connection.c ***************************/
  214. connection_t *connection_new(int type);
  215. void connection_free(connection_t *conn);
  216. int connection_create_listener(RSA *prkey, struct sockaddr_in *local, int type);
  217. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state);
  218. /* start all connections that should be up but aren't */
  219. int retry_all_connections(routerinfo_t **router_array, int rarray_len,
  220. RSA *prkey, uint16_t or_port, uint16_t op_port);
  221. int connection_read_to_buf(connection_t *conn);
  222. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  223. int connection_flush_buf(connection_t *conn);
  224. int connection_write_to_buf(char *string, int len, connection_t *conn);
  225. int connection_send_destroy(aci_t aci, connection_t *conn);
  226. int connection_encrypt_cell_header(cell_t *cellp, connection_t *conn);
  227. int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn);
  228. int connection_process_inbuf(connection_t *conn);
  229. int connection_process_cell_from_inbuf(connection_t *conn);
  230. int connection_finished_flushing(connection_t *conn);
  231. /********************************* connection_or.c ***************************/
  232. int connection_or_process_inbuf(connection_t *conn);
  233. int connection_or_finished_flushing(connection_t *conn);
  234. connection_t *connection_or_new(void);
  235. connection_t *connection_or_listener_new(void);
  236. void conn_or_init_crypto(connection_t *conn);
  237. int or_handshake_client_process_auth(connection_t *conn);
  238. int or_handshake_client_send_auth(connection_t *conn);
  239. int or_handshake_server_process_auth(connection_t *conn);
  240. int or_handshake_server_process_nonce(connection_t *conn);
  241. int connect_to_router(routerinfo_t *router, RSA *prkey, struct sockaddr_in *local);
  242. int connection_or_create_listener(RSA *prkey, struct sockaddr_in *local);
  243. int connection_or_handle_listener_read(connection_t *conn);
  244. /********************************* connection_op.c ***************************/
  245. connection_t *connection_op_new(void);
  246. connection_t *connection_op_listener_new(void);
  247. int op_handshake_process_keys(connection_t *conn);
  248. int connection_op_process_inbuf(connection_t *conn);
  249. int connection_op_finished_flushing(connection_t *conn);
  250. int connection_op_create_listener(RSA *prkey, struct sockaddr_in *local);
  251. int connection_op_handle_listener_read(connection_t *conn);
  252. /********************************* connection_app.c ***************************/
  253. connection_t *connection_app_new(void);
  254. int connection_app_process_inbuf(connection_t *conn);
  255. int connection_app_package_inbuf(connection_t *conn);
  256. int connection_app_process_data_cell(cell_t *cell, connection_t *conn);
  257. int connection_app_finished_flushing(connection_t *conn);
  258. /********************************* main.c ***************************/
  259. int connection_add(connection_t *conn);
  260. int connection_remove(connection_t *conn);
  261. void connection_set_poll_socket(connection_t *conn);
  262. connection_t *connection_get_by_addr_port(uint32_t addr, uint16_t port);
  263. connection_t *connection_get_by_type(int type);
  264. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  265. void connection_watch_events(connection_t *conn, short events);
  266. void check_conn_read(int i);
  267. void check_conn_marked(int i);
  268. void check_conn_write(int i);
  269. void check_conn_hup(int i);
  270. int do_main_loop(void);
  271. int main(int argc, char *argv[]);
  272. /********************************* onion.c ***************************/
  273. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  274. uint32_t remote_addr, uint16_t remote_port);
  275. int process_onion(circuit_t *circ, connection_t *conn);
  276. /********************************* routers.c ***************************/
  277. routerinfo_t **getrouters(char *routerfile, size_t *listlenp);
  278. void delete_routerlist(routerinfo_t *list);
  279. /* create an NULL-terminated array of pointers pointing to elements of a router list */
  280. routerinfo_t **make_rarray(routerinfo_t* list, size_t *listlenp);
  281. #endif