1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
2 * Copyright (c) 2007-2013, The Tor Project, Inc. */
3 /* See LICENSE for licensing information */
7 * \brief Convert circuit, stream, and orconn error reasons to and/or from
8 * strings and errno values.
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. */
20 stream_end_reason_to_control_string(int reason
)
22 reason
&= END_STREAM_REASON_MASK
;
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";
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.
54 stream_end_reason_to_string(int reason
)
58 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
59 "End cell arrived with length 0. Should be at least 1.");
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";
76 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
77 "Reason for ending (%d) not recognized.",reason
);
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.
90 stream_end_reason_to_socks5_response(int reason
)
92 switch (reason
& END_STREAM_REASON_MASK
) {
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
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
;
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'). */
152 #define E_CASE(s) case s: case WSA ## s
153 #define S_CASE(s) case WSA ## s
155 #define E_CASE(s) case s
156 #define S_CASE(s) case s
159 /** Given an errno from a failed exit connection, return a reason code
160 * appropriate for use in a RELAY END cell. */
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
169 return END_STREAM_REASON_DONE
;
175 S_CASE(EPROTONOSUPPORT
):
176 S_CASE(EAFNOSUPPORT
):
178 return END_STREAM_REASON_INTERNAL
;
180 S_CASE(EHOSTUNREACH
):
183 return END_STREAM_REASON_NOROUTE
;
184 S_CASE(ECONNREFUSED
):
185 return END_STREAM_REASON_CONNECTREFUSED
;
187 return END_STREAM_REASON_CONNRESET
;
189 return END_STREAM_REASON_TIMEOUT
;
194 S_CASE(EADDRNOTAVAIL
):
196 return END_STREAM_REASON_RESOURCELIMIT
;
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. */
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
216 case END_OR_CONN_REASON_DONE
:
218 case END_OR_CONN_REASON_REFUSED
:
219 return "CONNECTREFUSED";
220 case END_OR_CONN_REASON_OR_IDENTITY
:
222 case END_OR_CONN_REASON_CONNRESET
:
223 return "CONNECTRESET";
224 case END_OR_CONN_REASON_TIMEOUT
:
226 case END_OR_CONN_REASON_NO_ROUTE
:
228 case END_OR_CONN_REASON_IO_ERROR
:
230 case END_OR_CONN_REASON_RESOURCE_LIMIT
:
231 return "RESOURCELIMIT";
232 case END_OR_CONN_REASON_MISC
:
234 case END_OR_CONN_REASON_PT_MISSING
:
239 log_warn(LD_BUG
, "Unrecognized or_conn reason code %d", r
);
244 /** Convert a TOR_TLS_* error code into an END_OR_CONN_* reason. */
246 tls_error_to_orconn_end_reason(int 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
:
263 return END_OR_CONN_REASON_DONE
;
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
)
276 return END_OR_CONN_REASON_DONE
;
280 S_CASE(EHOSTUNREACH
):
281 return END_OR_CONN_REASON_NO_ROUTE
;
282 S_CASE(ECONNREFUSED
):
283 return END_OR_CONN_REASON_REFUSED
;
285 return END_OR_CONN_REASON_CONNRESET
;
287 return END_OR_CONN_REASON_TIMEOUT
;
296 return END_OR_CONN_REASON_RESOURCE_LIMIT
;
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
309 circuit_end_reason_to_control_string(int reason
)
313 if (reason
>= 0 && reason
& END_CIRC_REASON_FLAG_REMOTE
) {
314 reason
&= ~END_CIRC_REASON_FLAG_REMOTE
;
319 case END_CIRC_AT_ORIGIN
:
320 /* This shouldn't get passed here; it's a catch-all reason. */
322 case END_CIRC_REASON_NONE
:
323 /* This shouldn't get passed here; it's a catch-all reason. */
325 case END_CIRC_REASON_TORPROTOCOL
:
326 return "TORPROTOCOL";
327 case END_CIRC_REASON_INTERNAL
:
329 case END_CIRC_REASON_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
:
343 case END_CIRC_REASON_TIMEOUT
:
345 case END_CIRC_REASON_DESTROYED
:
347 case END_CIRC_REASON_NOPATH
:
349 case END_CIRC_REASON_NOSUCHSERVICE
:
350 return "NOSUCHSERVICE";
351 case END_CIRC_REASON_MEASUREMENT_EXPIRED
:
352 return "MEASUREMENT_EXPIRED";
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
);
364 "Unrecognized reason code %d", reason
);
370 /** Return a string corresponding to a SOCKS4 reponse code. */
372 socks4_response_code_to_string(uint8_t code
)
376 return "connection accepted";
378 return "server rejected connection";
380 return "server cannot connect to identd on this client";
382 return "user id does not match identd";
384 return "invalid SOCKS 4 response code";
388 /** Return a string corresponding to a SOCKS5 reponse code. */
390 socks5_response_code_to_string(uint8_t code
)
394 return "connection accepted";
396 return "general SOCKS server failure";
398 return "connection not allowed by ruleset";
400 return "Network unreachable";
402 return "Host unreachable";
404 return "Connection refused";
406 return "TTL expired";
408 return "Command not supported";
410 return "Address type not supported";
412 return "unknown reason";
416 /** Return a string corresponding to a bandwidht_weight_rule_t */
418 bandwidth_weight_rule_to_string(bandwidth_weight_rule_t rule
)
423 return "no weighting";
424 case WEIGHT_FOR_EXIT
:
425 return "weight as exit";
427 return "weight as middle node";
428 case WEIGHT_FOR_GUARD
:
429 return "weight as guard";
431 return "weight as directory";
433 return "unknown rule";