In routerlist_assert_ok(), check r2 before taking &(r2->cache_info)
[tor.git] / src / or / reasons.c
blob750e89bbe78d4234471e7f242b942e9f68cd18f9
1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2013, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
5 /**
6 * \file reasons.c
7 * \brief Convert circuit, stream, and orconn error reasons to and/or from
8 * strings and errno values.
9 **/
11 #include "or.h"
12 #include "config.h"
13 #include "reasons.h"
15 /***************************** Edge (stream) reasons **********************/
17 /** Convert the reason for ending a stream <b>reason</b> into the format used
18 * in STREAM events. Return NULL if the reason is unrecognized. */
19 const char *
20 stream_end_reason_to_control_string(int reason)
22 reason &= END_STREAM_REASON_MASK;
23 switch (reason) {
24 case END_STREAM_REASON_MISC: return "MISC";
25 case END_STREAM_REASON_RESOLVEFAILED: return "RESOLVEFAILED";
26 case END_STREAM_REASON_CONNECTREFUSED: return "CONNECTREFUSED";
27 case END_STREAM_REASON_EXITPOLICY: return "EXITPOLICY";
28 case END_STREAM_REASON_DESTROY: return "DESTROY";
29 case END_STREAM_REASON_DONE: return "DONE";
30 case END_STREAM_REASON_TIMEOUT: return "TIMEOUT";
31 case END_STREAM_REASON_NOROUTE: return "NOROUTE";
32 case END_STREAM_REASON_HIBERNATING: return "HIBERNATING";
33 case END_STREAM_REASON_INTERNAL: return "INTERNAL";
34 case END_STREAM_REASON_RESOURCELIMIT: return "RESOURCELIMIT";
35 case END_STREAM_REASON_CONNRESET: return "CONNRESET";
36 case END_STREAM_REASON_TORPROTOCOL: return "TORPROTOCOL";
37 case END_STREAM_REASON_NOTDIRECTORY: return "NOTDIRECTORY";
39 case END_STREAM_REASON_CANT_ATTACH: return "CANT_ATTACH";
40 case END_STREAM_REASON_NET_UNREACHABLE: return "NET_UNREACHABLE";
41 case END_STREAM_REASON_SOCKSPROTOCOL: return "SOCKS_PROTOCOL";
43 case END_STREAM_REASON_PRIVATE_ADDR: return "PRIVATE_ADDR";
45 default: return NULL;
49 /** Translate <b>reason</b>, which came from a relay 'end' cell,
50 * into a static const string describing why the stream is closing.
51 * <b>reason</b> is -1 if no reason was provided.
53 const char *
54 stream_end_reason_to_string(int reason)
56 switch (reason) {
57 case -1:
58 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
59 "End cell arrived with length 0. Should be at least 1.");
60 return "MALFORMED";
61 case END_STREAM_REASON_MISC: return "misc error";
62 case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
63 case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
64 case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
65 case END_STREAM_REASON_DESTROY: return "destroyed";
66 case END_STREAM_REASON_DONE: return "closed normally";
67 case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
68 case END_STREAM_REASON_NOROUTE: return "no route to host";
69 case END_STREAM_REASON_HIBERNATING: return "server is hibernating";
70 case END_STREAM_REASON_INTERNAL: return "internal error at server";
71 case END_STREAM_REASON_RESOURCELIMIT: return "server out of resources";
72 case END_STREAM_REASON_CONNRESET: return "connection reset";
73 case END_STREAM_REASON_TORPROTOCOL: return "Tor protocol error";
74 case END_STREAM_REASON_NOTDIRECTORY: return "not a directory";
75 default:
76 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
77 "Reason for ending (%d) not recognized.",reason);
78 return "unknown";
82 /** Translate <b>reason</b> (as from a relay 'end' cell) into an
83 * appropriate SOCKS5 reply code.
85 * A reason of 0 means that we're not actually expecting to send
86 * this code back to the socks client; we just call it 'succeeded'
87 * to keep things simple.
89 socks5_reply_status_t
90 stream_end_reason_to_socks5_response(int reason)
92 switch (reason & END_STREAM_REASON_MASK) {
93 case 0:
94 return SOCKS5_SUCCEEDED;
95 case END_STREAM_REASON_MISC:
96 return SOCKS5_GENERAL_ERROR;
97 case END_STREAM_REASON_RESOLVEFAILED:
98 return SOCKS5_HOST_UNREACHABLE;
99 case END_STREAM_REASON_CONNECTREFUSED:
100 return SOCKS5_CONNECTION_REFUSED;
101 case END_STREAM_REASON_ENTRYPOLICY:
102 return SOCKS5_NOT_ALLOWED;
103 case END_STREAM_REASON_EXITPOLICY:
104 return SOCKS5_NOT_ALLOWED;
105 case END_STREAM_REASON_DESTROY:
106 return SOCKS5_GENERAL_ERROR;
107 case END_STREAM_REASON_DONE:
108 /* Note that 'DONE' usually indicates a successful close from the other
109 * side of the stream... but if we receive it before a connected cell --
110 * that is, before we have sent a SOCKS reply -- that means that the
111 * other side of the circuit closed the connection before telling us it
112 * was complete. */
113 return SOCKS5_CONNECTION_REFUSED;
114 case END_STREAM_REASON_TIMEOUT:
115 return SOCKS5_TTL_EXPIRED;
116 case END_STREAM_REASON_NOROUTE:
117 return SOCKS5_HOST_UNREACHABLE;
118 case END_STREAM_REASON_RESOURCELIMIT:
119 return SOCKS5_GENERAL_ERROR;
120 case END_STREAM_REASON_HIBERNATING:
121 return SOCKS5_GENERAL_ERROR;
122 case END_STREAM_REASON_INTERNAL:
123 return SOCKS5_GENERAL_ERROR;
124 case END_STREAM_REASON_CONNRESET:
125 return SOCKS5_CONNECTION_REFUSED;
126 case END_STREAM_REASON_TORPROTOCOL:
127 return SOCKS5_GENERAL_ERROR;
129 case END_STREAM_REASON_CANT_ATTACH:
130 return SOCKS5_GENERAL_ERROR;
131 case END_STREAM_REASON_NET_UNREACHABLE:
132 return SOCKS5_NET_UNREACHABLE;
133 case END_STREAM_REASON_SOCKSPROTOCOL:
134 return SOCKS5_GENERAL_ERROR;
135 case END_STREAM_REASON_PRIVATE_ADDR:
136 return SOCKS5_GENERAL_ERROR;
138 default:
139 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
140 "Reason for ending (%d) not recognized; "
141 "sending generic socks error.", reason);
142 return SOCKS5_GENERAL_ERROR;
146 /* We need to use a few macros to deal with the fact that Windows
147 * decided that their sockets interface should be a permakludge.
148 * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
149 * version, and S_CASE is for errors where windows has only a WSAEFOO
150 * version. (The E is for 'error', the S is for 'socket'). */
151 #ifdef _WIN32
152 #define E_CASE(s) case s: case WSA ## s
153 #define S_CASE(s) case WSA ## s
154 #else
155 #define E_CASE(s) case s
156 #define S_CASE(s) case s
157 #endif
159 /** Given an errno from a failed exit connection, return a reason code
160 * appropriate for use in a RELAY END cell. */
161 uint8_t
162 errno_to_stream_end_reason(int e)
164 /* To add new errors here, find out if they exist on Windows, and if a WSA*
165 * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
166 * appropriate. */
167 switch (e) {
168 case EPIPE:
169 return END_STREAM_REASON_DONE;
170 E_CASE(EBADF):
171 E_CASE(EFAULT):
172 E_CASE(EINVAL):
173 S_CASE(EISCONN):
174 S_CASE(ENOTSOCK):
175 S_CASE(EPROTONOSUPPORT):
176 S_CASE(EAFNOSUPPORT):
177 S_CASE(ENOTCONN):
178 return END_STREAM_REASON_INTERNAL;
179 S_CASE(ENETUNREACH):
180 S_CASE(EHOSTUNREACH):
181 E_CASE(EACCES):
182 case EPERM:
183 return END_STREAM_REASON_NOROUTE;
184 S_CASE(ECONNREFUSED):
185 return END_STREAM_REASON_CONNECTREFUSED;
186 S_CASE(ECONNRESET):
187 return END_STREAM_REASON_CONNRESET;
188 S_CASE(ETIMEDOUT):
189 return END_STREAM_REASON_TIMEOUT;
190 S_CASE(ENOBUFS):
191 case ENOMEM:
192 case ENFILE:
193 S_CASE(EADDRINUSE):
194 S_CASE(EADDRNOTAVAIL):
195 E_CASE(EMFILE):
196 return END_STREAM_REASON_RESOURCELIMIT;
197 default:
198 log_info(LD_EXIT, "Didn't recognize errno %d (%s); telling the client "
199 "that we are ending a stream for 'misc' reason.",
200 e, tor_socket_strerror(e));
201 return END_STREAM_REASON_MISC;
205 /***************************** ORConn reasons *****************************/
207 /** Convert the reason for ending an OR connection <b>r</b> into the format
208 * used in ORCONN events. Return "UNKNOWN" if the reason is unrecognized. */
209 const char *
210 orconn_end_reason_to_control_string(int r)
212 /* To add new errors here, find out if they exist on Windows, and if a WSA*
213 * equivalent exists on windows. Add a case, an S_CASE, or an E_CASE as
214 * appropriate. */
215 switch (r) {
216 case END_OR_CONN_REASON_DONE:
217 return "DONE";
218 case END_OR_CONN_REASON_REFUSED:
219 return "CONNECTREFUSED";
220 case END_OR_CONN_REASON_OR_IDENTITY:
221 return "IDENTITY";
222 case END_OR_CONN_REASON_CONNRESET:
223 return "CONNECTRESET";
224 case END_OR_CONN_REASON_TIMEOUT:
225 return "TIMEOUT";
226 case END_OR_CONN_REASON_NO_ROUTE:
227 return "NOROUTE";
228 case END_OR_CONN_REASON_IO_ERROR:
229 return "IOERROR";
230 case END_OR_CONN_REASON_RESOURCE_LIMIT:
231 return "RESOURCELIMIT";
232 case END_OR_CONN_REASON_MISC:
233 return "MISC";
234 case END_OR_CONN_REASON_PT_MISSING:
235 return "PT_MISSING";
236 case 0:
237 return "";
238 default:
239 log_warn(LD_BUG, "Unrecognized or_conn reason code %d", r);
240 return "UNKNOWN";
244 /** Convert a TOR_TLS_* error code into an END_OR_CONN_* reason. */
246 tls_error_to_orconn_end_reason(int e)
248 switch (e) {
249 case TOR_TLS_ERROR_IO:
250 return END_OR_CONN_REASON_IO_ERROR;
251 case TOR_TLS_ERROR_CONNREFUSED:
252 return END_OR_CONN_REASON_REFUSED;
253 case TOR_TLS_ERROR_CONNRESET:
254 return END_OR_CONN_REASON_CONNRESET;
255 case TOR_TLS_ERROR_NO_ROUTE:
256 return END_OR_CONN_REASON_NO_ROUTE;
257 case TOR_TLS_ERROR_TIMEOUT:
258 return END_OR_CONN_REASON_TIMEOUT;
259 case TOR_TLS_WANTREAD:
260 case TOR_TLS_WANTWRITE:
261 case TOR_TLS_CLOSE:
262 case TOR_TLS_DONE:
263 return END_OR_CONN_REASON_DONE;
264 default:
265 return END_OR_CONN_REASON_MISC;
269 /** Given an errno from a failed ORConn connection, return a reason code
270 * appropriate for use in the controller orconn events. */
272 errno_to_orconn_end_reason(int e)
274 switch (e) {
275 case EPIPE:
276 return END_OR_CONN_REASON_DONE;
277 S_CASE(ENOTCONN):
278 S_CASE(ENETUNREACH):
279 S_CASE(ENETDOWN):
280 S_CASE(EHOSTUNREACH):
281 return END_OR_CONN_REASON_NO_ROUTE;
282 S_CASE(ECONNREFUSED):
283 return END_OR_CONN_REASON_REFUSED;
284 S_CASE(ECONNRESET):
285 return END_OR_CONN_REASON_CONNRESET;
286 S_CASE(ETIMEDOUT):
287 return END_OR_CONN_REASON_TIMEOUT;
288 S_CASE(ENOBUFS):
289 case ENOMEM:
290 case ENFILE:
291 E_CASE(EMFILE):
292 E_CASE(EACCES):
293 E_CASE(EBADF):
294 E_CASE(EFAULT):
295 E_CASE(EINVAL):
296 return END_OR_CONN_REASON_RESOURCE_LIMIT;
297 default:
298 log_info(LD_OR, "Didn't recognize errno %d (%s).",
299 e, tor_socket_strerror(e));
300 return END_OR_CONN_REASON_MISC;
304 /***************************** Circuit reasons *****************************/
306 /** Convert a numeric reason for destroying a circuit into a string for a
307 * CIRCUIT event. */
308 const char *
309 circuit_end_reason_to_control_string(int reason)
311 int is_remote = 0;
313 if (reason >= 0 && reason & END_CIRC_REASON_FLAG_REMOTE) {
314 reason &= ~END_CIRC_REASON_FLAG_REMOTE;
315 is_remote = 1;
318 switch (reason) {
319 case END_CIRC_AT_ORIGIN:
320 /* This shouldn't get passed here; it's a catch-all reason. */
321 return "ORIGIN";
322 case END_CIRC_REASON_NONE:
323 /* This shouldn't get passed here; it's a catch-all reason. */
324 return "NONE";
325 case END_CIRC_REASON_TORPROTOCOL:
326 return "TORPROTOCOL";
327 case END_CIRC_REASON_INTERNAL:
328 return "INTERNAL";
329 case END_CIRC_REASON_REQUESTED:
330 return "REQUESTED";
331 case END_CIRC_REASON_HIBERNATING:
332 return "HIBERNATING";
333 case END_CIRC_REASON_RESOURCELIMIT:
334 return "RESOURCELIMIT";
335 case END_CIRC_REASON_CONNECTFAILED:
336 return "CONNECTFAILED";
337 case END_CIRC_REASON_OR_IDENTITY:
338 return "OR_IDENTITY";
339 case END_CIRC_REASON_CHANNEL_CLOSED:
340 return "CHANNEL_CLOSED";
341 case END_CIRC_REASON_FINISHED:
342 return "FINISHED";
343 case END_CIRC_REASON_TIMEOUT:
344 return "TIMEOUT";
345 case END_CIRC_REASON_DESTROYED:
346 return "DESTROYED";
347 case END_CIRC_REASON_NOPATH:
348 return "NOPATH";
349 case END_CIRC_REASON_NOSUCHSERVICE:
350 return "NOSUCHSERVICE";
351 case END_CIRC_REASON_MEASUREMENT_EXPIRED:
352 return "MEASUREMENT_EXPIRED";
353 default:
354 if (is_remote) {
356 * If it's remote, it's not a bug *here*, so don't use LD_BUG, but
357 * do note that the someone we're talking to is speaking the Tor
358 * protocol with a weird accent.
360 log_warn(LD_PROTOCOL,
361 "Remote server sent bogus reason code %d", reason);
362 } else {
363 log_warn(LD_BUG,
364 "Unrecognized reason code %d", reason);
366 return NULL;
370 /** Return a string corresponding to a SOCKS4 reponse code. */
371 const char *
372 socks4_response_code_to_string(uint8_t code)
374 switch (code) {
375 case 0x5a:
376 return "connection accepted";
377 case 0x5b:
378 return "server rejected connection";
379 case 0x5c:
380 return "server cannot connect to identd on this client";
381 case 0x5d:
382 return "user id does not match identd";
383 default:
384 return "invalid SOCKS 4 response code";
388 /** Return a string corresponding to a SOCKS5 reponse code. */
389 const char *
390 socks5_response_code_to_string(uint8_t code)
392 switch (code) {
393 case 0x00:
394 return "connection accepted";
395 case 0x01:
396 return "general SOCKS server failure";
397 case 0x02:
398 return "connection not allowed by ruleset";
399 case 0x03:
400 return "Network unreachable";
401 case 0x04:
402 return "Host unreachable";
403 case 0x05:
404 return "Connection refused";
405 case 0x06:
406 return "TTL expired";
407 case 0x07:
408 return "Command not supported";
409 case 0x08:
410 return "Address type not supported";
411 default:
412 return "unknown reason";
416 /** Return a string corresponding to a bandwidht_weight_rule_t */
417 const char *
418 bandwidth_weight_rule_to_string(bandwidth_weight_rule_t rule)
420 switch (rule)
422 case NO_WEIGHTING:
423 return "no weighting";
424 case WEIGHT_FOR_EXIT:
425 return "weight as exit";
426 case WEIGHT_FOR_MID:
427 return "weight as middle node";
428 case WEIGHT_FOR_GUARD:
429 return "weight as guard";
430 case WEIGHT_FOR_DIR:
431 return "weight as directory";
432 default:
433 return "unknown rule";