diff options
-rwxr-xr-x | contrib/tor-resolve.py | 16 | ||||
-rw-r--r-- | src/or/circuituse.c | 3 | ||||
-rw-r--r-- | src/or/connection_edge.c | 18 | ||||
-rw-r--r-- | src/or/control.c | 3 | ||||
-rw-r--r-- | src/or/relay.c | 3 |
5 files changed, 28 insertions, 15 deletions
diff --git a/contrib/tor-resolve.py b/contrib/tor-resolve.py index 3557c2aa96..919bc876cc 100755 --- a/contrib/tor-resolve.py +++ b/contrib/tor-resolve.py @@ -36,7 +36,9 @@ def socks5ResolveRequest(hostname, atype=0x03, command=0xF0): version = 5 rsv = 0 port = 0 - reqheader = struct.pack("!BBBBB",version, command, rsv, atype, len(hostname)) + reqheader = struct.pack("!BBBB",version, command, rsv, atype) + if atype == 0x03: + reqheader += struct.pack("!B", len(hostname)) portstr = struct.pack("!H",port) return "%s%s%s"%(reqheader,hostname,portstr) @@ -62,8 +64,11 @@ def socks5ParseResponse(r): # not really the right way to format IPv6 return "IPv6: %s"%(":".join([hex(ord(c)) for c in addr])) else: - nul = r.index('\0',4) - return r[4:nul] + hlen, = struct.unpack("!B", r[4]) + expected_len = 5 + hlen + 2 + if len(r) < expected_len: + return None + return r[5:-2] def socks5ResolvePTRRequest(hostname): return socks5ResolveRequest(socket.inet_aton(hostname), @@ -105,7 +110,6 @@ def resolve(hostname, sockshost, socksport, socksver=4, reverse=0): if socksver == 5: s.send(socks5Hello()) socks5ParseHello(s.recv(2)) - print len(fmt(hostname)), len(hostname) s.send(fmt(hostname)) answer = s.recv(6) result = parse(answer) @@ -127,7 +131,7 @@ if __name__ == '__main__': sys.exit(0) socksver = 4 reverse = 0 - while sys.argv[1] == '-': + while sys.argv[1][0] == '-': if sys.argv[1] in ("-4", "-5"): socksver = int(sys.argv[1][1]) del sys.argv[1] @@ -138,7 +142,7 @@ if __name__ == '__main__': break if len(sys.argv) >= 4: - print "Syntax: resolve.py [-4|-5] hostname [sockshost:socksport]" + print "Syntax: resolve.py [-x] [-4|-5] hostname [sockshost:socksport]" sys.exit(0) if len(sys.argv) == 3: sh,sp = parseHostAndPort(sys.argv[2]) diff --git a/src/or/circuituse.c b/src/or/circuituse.c index 74e6895946..310e8278fb 100644 --- a/src/or/circuituse.c +++ b/src/or/circuituse.c @@ -947,7 +947,8 @@ circuit_get_open_circ_or_launch(edge_connection_t *conn, tor_assert(conn); tor_assert(circp); tor_assert(conn->_base.state == AP_CONN_STATE_CIRCUIT_WAIT); - is_resolve = conn->socks_request->command == SOCKS_COMMAND_RESOLVE; + is_resolve = (conn->socks_request->command == SOCKS_COMMAND_RESOLVE || + conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR); need_uptime = smartlist_string_num_isin(get_options()->LongLivedPorts, conn->socks_request->port); diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c index 8d6c9273ba..9ab672ff07 100644 --- a/src/or/connection_edge.c +++ b/src/or/connection_edge.c @@ -1120,7 +1120,7 @@ connection_ap_handshake_rewrite_and_attach(edge_connection_t *conn, return -1; } - if (socks->command == SOCKS_COMMAND_RESOLVE) { // resolve_ptr XXXX NM + if (socks->command == SOCKS_COMMAND_RESOLVE) { uint32_t answer; struct in_addr in; /* Reply to resolves immediately if we can. */ @@ -1141,7 +1141,7 @@ connection_ap_handshake_rewrite_and_attach(edge_connection_t *conn, return 0; } rep_hist_note_used_resolve(time(NULL)); /* help predict this next time */ - } else { /* socks->command == SOCKS_COMMAND_CONNECT */ + } else if (socks->command == SOCKS_COMMAND_CONNECT) { if (socks->port == 0) { log_notice(LD_APP,"Application asked to connect to port 0. Refusing."); connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL); @@ -1166,6 +1166,12 @@ connection_ap_handshake_rewrite_and_attach(edge_connection_t *conn, /* help predict this next time */ rep_hist_note_used_port(socks->port, time(NULL)); + } else if (socks->command == SOCKS_COMMAND_RESOLVE_PTR) { + // XXXX NM Do anything here? + + rep_hist_note_used_resolve(time(NULL)); /* help predict this next time */ + } else { + tor_fragile_assert(); } conn->_base.state = AP_CONN_STATE_CIRCUIT_WAIT; if ((circ && @@ -1701,10 +1707,10 @@ connection_ap_handshake_socks_resolved(edge_connection_t *conn, buf[1] = SOCKS5_SUCCEEDED; buf[2] = 0; /* reserved */ buf[3] = 0x03; /* Domainname address type */ - memcpy(buf+4, answer, answer_len); /* address */ - buf[4+answer_len] = '\0'; - set_uint16(buf+4+answer_len+1, 0); /* port == 0. */ - replylen = 4+answer_len+1+2; + buf[4] = (char)answer_len; + memcpy(buf+5, answer, answer_len); /* address */ + set_uint16(buf+5+answer_len, 0); /* port == 0. */ + replylen = 5+answer_len+2; } else { buf[1] = SOCKS5_HOST_UNREACHABLE; memset(buf+2, 0, 8); diff --git a/src/or/control.c b/src/or/control.c index 58eb9b0f41..e260f15347 100644 --- a/src/or/control.c +++ b/src/or/control.c @@ -1435,7 +1435,8 @@ handle_getinfo_helper(const char *question, char **answer) case AP_CONN_STATE_CONTROLLER_WAIT: case AP_CONN_STATE_CIRCUIT_WAIT: if (conn->socks_request && - conn->socks_request->command == SOCKS_COMMAND_RESOLVE) + (conn->socks_request->command == SOCKS_COMMAND_RESOLVE || + conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR)) state = "NEWRESOLVE"; else state = "NEW"; diff --git a/src/or/relay.c b/src/or/relay.c index 3e9690fbc9..8d9ce04fb1 100644 --- a/src/or/relay.c +++ b/src/or/relay.c @@ -865,7 +865,8 @@ connection_edge_process_relay_cell_not_open( "Dropping."); return 0; } - tor_assert(conn->socks_request->command == SOCKS_COMMAND_RESOLVE); + tor_assert(conn->socks_request->command == SOCKS_COMMAND_RESOLVE || + conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR); answer_len = cell->payload[RELAY_HEADER_SIZE+1]; if (rh->length < 2 || answer_len+2>rh->length) { log_warn(LD_PROTOCOL, "Dropping malformed 'resolved' cell"); |