control.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Copyright 2004 Nick Mathewson */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. #define CONTROL_CMD_ERROR 0x0000
  6. #define CONTROL_CMD_DONE 0x0001
  7. #define CONTROL_CMD_SETCONF 0x0002
  8. #define CONTROL_CMD_GETCONF 0x0003
  9. #define CONTROL_CMD_CONFVALUE 0x0004
  10. #define CONTROL_CMD_SETEVENTS 0x0005
  11. #define CONTROL_CMD_EVENT 0x0006
  12. #define CONTROL_CMD_AUTHENTICATE 0x0007
  13. #define _CONTROL_CMD_MAX_RECOGNIZED 0x0007
  14. #define ERR_UNSPECIFIED 0x0000
  15. #define ERR_UNRECOGNIZED_TYPE 0x0001
  16. #define ERR_UNRECOGNIZED_CONFIG_KEY 0x0002
  17. #define ERR_INVALID_CONFIG_VALUE 0x0003
  18. #define ERR_UNRECOGNIZED_EVENT_CODE 0x0004
  19. #define ERR_UNAUTHORIZED_USER 0x0005
  20. #define ERR_FAILED_AUTHENTICATION 0x0006
  21. #define _EVENT_MIN 0x0001
  22. #define EVENT_CIRCUIT_STATUS 0x0001
  23. #define EVENT_STREAM_STATUS 0x0002
  24. #define EVENT_OR_CONN_STATUS 0x0003
  25. #define EVENT_BANDWIDTH_USED 0x0004
  26. #define EVENT_WARNING 0x0005
  27. #define _EVENT_MAX 0x0005
  28. #define EVENT_IS_INTERESTING(e) (global_event_mask & (1<<(e)))
  29. static const char *CONTROL_COMMANDS[] = {
  30. "error",
  31. "done",
  32. "setconf",
  33. "getconf",
  34. "confvalue",
  35. "setevents",
  36. "events",
  37. "authenticate",
  38. };
  39. static uint32_t global_event_mask = 0;
  40. static void update_global_event_mask(void);
  41. static void send_control_message(connection_t *conn, uint16_t type,
  42. uint16_t len, const char *body);
  43. static void send_control_done(connection_t *conn);
  44. static void send_control_error(connection_t *conn, uint16_t error,
  45. const char *message);
  46. static void send_control_event(uint16_t event, uint16_t len, const char *body);
  47. static int handle_control_setconf(connection_t *conn, uint16_t len,
  48. const char *body);
  49. static int handle_control_getconf(connection_t *conn, uint16_t len,
  50. const char *body);
  51. static int handle_control_setevents(connection_t *conn, uint16_t len,
  52. const char *body);
  53. static int handle_control_authenticate(connection_t *conn, uint16_t len,
  54. const char *body);
  55. static INLINE const char *
  56. control_cmd_to_string(uint16_t cmd)
  57. {
  58. return (cmd<=_CONTROL_CMD_MAX_RECOGNIZED) ? CONTROL_COMMANDS[cmd] : "Unknown";
  59. }
  60. static void update_global_event_mask(void)
  61. {
  62. connection_t **conns;
  63. int n_conns, i;
  64. global_event_mask = 0;
  65. get_connection_array(&conns, &n_conns);
  66. for (i = 0; i < n_conns; ++i) {
  67. if (conns[i]->type == CONN_TYPE_CONTROL &&
  68. conns[i]->state == CONTROL_CONN_STATE_OPEN) {
  69. global_event_mask |= conns[i]->event_mask;
  70. }
  71. }
  72. }
  73. static void
  74. send_control_message(connection_t *conn, uint16_t type, uint16_t len,
  75. const char *body)
  76. {
  77. char buf[4];
  78. tor_assert(conn);
  79. tor_assert(len || !body);
  80. tor_assert(type <= _CONTROL_CMD_MAX_RECOGNIZED);
  81. set_uint32(buf, htons(len));
  82. set_uint32(buf+2, htons(type));
  83. connection_write_to_buf(buf, 4, conn);
  84. if (len)
  85. connection_write_to_buf(body, len, conn);
  86. }
  87. static void
  88. send_control_done(connection_t *conn)
  89. {
  90. send_control_message(conn, CONTROL_CMD_DONE, 0, NULL);
  91. }
  92. static void
  93. send_control_error(connection_t *conn, uint16_t error, const char *message)
  94. {
  95. char buf[256];
  96. size_t len;
  97. set_uint16(buf, htons(error));
  98. len = strlen(message);
  99. tor_assert(len < (256-2));
  100. memcpy(buf+2, message, len);
  101. send_control_message(conn, CONTROL_CMD_ERROR, len+2, buf);
  102. }
  103. static void
  104. send_control_event(uint16_t event, uint16_t len, const char *body)
  105. {
  106. connection_t **conns;
  107. int n_conns, i;
  108. get_connection_array(&conns, &n_conns);
  109. for (i = 0; i < n_conns; ++i) {
  110. if (conns[i]->type == CONN_TYPE_CONTROL &&
  111. conns[i]->state == CONTROL_CONN_STATE_OPEN &&
  112. conns[i]->event_mask & (1<<event)) {
  113. send_control_message(conns[i], CONTROL_CMD_EVENT, len, body);
  114. }
  115. }
  116. }
  117. static int
  118. handle_control_setconf(connection_t *conn, uint16_t len,
  119. const char *body)
  120. {
  121. /* XXXX009 NM */
  122. return 0;
  123. }
  124. static int handle_control_getconf(connection_t *conn, uint16_t len,
  125. const char *body)
  126. {
  127. /* XXXX009 NM */
  128. return 0;
  129. }
  130. static int handle_control_setevents(connection_t *conn, uint16_t len,
  131. const char *body)
  132. {
  133. uint16_t event_code;
  134. uint32_t event_mask = 0;
  135. if (len % 2) {
  136. send_control_error(conn, ERR_UNSPECIFIED,
  137. "Odd number of bytes in setevents message");
  138. return 0;
  139. }
  140. for (; len; len -= 2, body += 2) {
  141. event_code = ntohs(get_uint16(body));
  142. if (event_code < _EVENT_MIN || event_code > _EVENT_MAX) {
  143. send_control_error(conn, ERR_UNRECOGNIZED_EVENT_CODE,
  144. "Unrecognized event code");
  145. return 0;
  146. }
  147. event_mask |= (1 << event_code);
  148. }
  149. conn->event_mask = event_mask;
  150. update_global_event_mask();
  151. send_control_done(conn);
  152. return 0;
  153. }
  154. static int handle_control_authenticate(connection_t *conn, uint16_t len,
  155. const char *body)
  156. {
  157. if (0/* XXXX009 NM */) {
  158. send_control_done(conn);
  159. conn->state = CONTROL_CONN_STATE_OPEN;
  160. } else {
  161. send_control_error(conn, ERR_FAILED_AUTHENTICATION,"Authentication failed");
  162. }
  163. return 0;
  164. }
  165. int connection_control_finished_flushing(connection_t *conn) {
  166. tor_assert(conn);
  167. tor_assert(conn->type == CONN_TYPE_CONTROL);
  168. connection_stop_writing(conn);
  169. return 0;
  170. }
  171. int connection_control_process_inbuf(connection_t *conn) {
  172. uint16_t body_len, command_type;
  173. char *body;
  174. tor_assert(conn);
  175. tor_assert(conn->type == CONN_TYPE_CONTROL);
  176. again:
  177. switch(fetch_from_buf_control(conn->inbuf, &body_len, &command_type, &body))
  178. {
  179. case -1:
  180. log_fn(LOG_WARN, "Error in control command. Failing.");
  181. return -1;
  182. case 0:
  183. /* Control command not all here yet. Wait. */
  184. return 0;
  185. case 1:
  186. /* We got a command. Process it. */
  187. break;
  188. default:
  189. tor_assert(0);
  190. }
  191. /* We got a command. If we need authentication, only authentication
  192. * commands will be considered. */
  193. if (conn->state == CONTROL_CONN_STATE_NEEDAUTH &&
  194. command_type != CONTROL_CMD_AUTHENTICATE) {
  195. log_fn(LOG_WARN, "Rejecting '%s' command; authentication needed.",
  196. control_cmd_to_string(command_type));
  197. send_control_error(conn, ERR_UNAUTHORIZED_USER, "Authentication required");
  198. tor_free(body);
  199. goto again;
  200. }
  201. switch(command_type)
  202. {
  203. case CONTROL_CMD_SETCONF:
  204. if (handle_control_setconf(conn, body_len, body))
  205. return -1;
  206. break;
  207. case CONTROL_CMD_GETCONF:
  208. if (handle_control_getconf(conn, body_len, body))
  209. return -1;
  210. break;
  211. case CONTROL_CMD_SETEVENTS:
  212. if (handle_control_setevents(conn, body_len, body))
  213. return -1;
  214. break;
  215. case CONTROL_CMD_AUTHENTICATE:
  216. if (handle_control_authenticate(conn, body_len, body))
  217. return -1;
  218. break;
  219. case CONTROL_CMD_ERROR:
  220. case CONTROL_CMD_DONE:
  221. case CONTROL_CMD_CONFVALUE:
  222. case CONTROL_CMD_EVENT:
  223. log_fn(LOG_WARN, "Received client-only '%s' command; ignoring.",
  224. control_cmd_to_string(command_type));
  225. send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
  226. "Command type only valid from server to tor client");
  227. break;
  228. default:
  229. log_fn(LOG_WARN, "Received unrecognized command type %d; ignoring.",
  230. (int)command_type);
  231. send_control_error(conn, ERR_UNRECOGNIZED_TYPE,
  232. "Unrecognized command type");
  233. break;
  234. }
  235. tor_free(body);
  236. goto again; /* There might be more data. */
  237. }
  238. int control_event_circuit_status(circuit_t *circ)
  239. {
  240. if (!EVENT_IS_INTERESTING(EVENT_CIRCUIT_STATUS))
  241. return 0;
  242. /* XXXXX009 NM */
  243. return 0;
  244. }
  245. int control_event_stream_status(connection_t *conn)
  246. {
  247. tor_assert(conn->type == CONN_TYPE_AP);
  248. if (!EVENT_IS_INTERESTING(EVENT_STREAM_STATUS))
  249. return 0;
  250. /* XXXXX009 NM */
  251. return 0;
  252. }
  253. int control_event_or_conn_status(connection_t *conn)
  254. {
  255. tor_assert(conn->type == CONN_TYPE_OR);
  256. if (!EVENT_IS_INTERESTING(EVENT_OR_CONN_STATUS))
  257. return 0;
  258. /* XXXXX009 NM */
  259. return 0;
  260. }
  261. int control_event_bandwidth_used(uint32_t n_read, uint32_t n_written)
  262. {
  263. char buf[8];
  264. if (!EVENT_IS_INTERESTING(EVENT_BANDWIDTH_USED))
  265. return 0;
  266. set_uint32(buf, htonl(n_read));
  267. set_uint32(buf+4, htonl(n_read));
  268. send_control_event(EVENT_BANDWIDTH_USED, 8, buf);
  269. return 0;
  270. }
  271. int control_event_warning(const char *msg)
  272. {
  273. size_t len;
  274. if (!EVENT_IS_INTERESTING(EVENT_WARNING))
  275. return 0;
  276. len = strlen(msg);
  277. send_control_event(EVENT_WARNING, len+1, msg);
  278. return 0;
  279. }
  280. /*
  281. Local Variabls:
  282. mode:c
  283. indent-tabs-mode:nil
  284. c-basic-offset:2
  285. End:
  286. */