Make changes to latest bridge-stats fixes as suggested by Nick.
[tor/rransom.git] / src / or / connection_or.c
blob60c5c7fcad9dda831139517bf9ef9a2554fde4a3
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2009, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file connection_or.c
9 * \brief Functions to handle OR connections, TLS handshaking, and
10 * cells on the network.
11 **/
13 #include "or.h"
15 static int connection_tls_finish_handshake(or_connection_t *conn);
16 static int connection_or_process_cells_from_inbuf(or_connection_t *conn);
17 static int connection_or_send_versions(or_connection_t *conn);
18 static int connection_init_or_handshake_state(or_connection_t *conn,
19 int started_here);
20 static int connection_or_check_valid_tls_handshake(or_connection_t *conn,
21 int started_here,
22 char *digest_rcvd_out);
24 /**************************************************************/
26 /** Map from identity digest of connected OR or desired OR to a connection_t
27 * with that identity digest. If there is more than one such connection_t,
28 * they form a linked list, with next_with_same_id as the next pointer. */
29 static digestmap_t *orconn_identity_map = NULL;
31 /** If conn is listed in orconn_identity_map, remove it, and clear
32 * conn->identity_digest. Otherwise do nothing. */
33 void
34 connection_or_remove_from_identity_map(or_connection_t *conn)
36 or_connection_t *tmp;
37 tor_assert(conn);
38 if (!orconn_identity_map)
39 return;
40 tmp = digestmap_get(orconn_identity_map, conn->identity_digest);
41 if (!tmp) {
42 if (!tor_digest_is_zero(conn->identity_digest)) {
43 log_warn(LD_BUG, "Didn't find connection '%s' on identity map when "
44 "trying to remove it.",
45 conn->nickname ? conn->nickname : "NULL");
47 return;
49 if (conn == tmp) {
50 if (conn->next_with_same_id)
51 digestmap_set(orconn_identity_map, conn->identity_digest,
52 conn->next_with_same_id);
53 else
54 digestmap_remove(orconn_identity_map, conn->identity_digest);
55 } else {
56 while (tmp->next_with_same_id) {
57 if (tmp->next_with_same_id == conn) {
58 tmp->next_with_same_id = conn->next_with_same_id;
59 break;
61 tmp = tmp->next_with_same_id;
64 memset(conn->identity_digest, 0, DIGEST_LEN);
65 conn->next_with_same_id = NULL;
68 /** Remove all entries from the identity-to-orconn map, and clear
69 * all identities in OR conns.*/
70 void
71 connection_or_clear_identity_map(void)
73 smartlist_t *conns = get_connection_array();
74 SMARTLIST_FOREACH(conns, connection_t *, conn,
76 if (conn->type == CONN_TYPE_OR) {
77 or_connection_t *or_conn = TO_OR_CONN(conn);
78 memset(or_conn->identity_digest, 0, DIGEST_LEN);
79 or_conn->next_with_same_id = NULL;
81 });
83 digestmap_free(orconn_identity_map, NULL);
84 orconn_identity_map = NULL;
87 /** Change conn->identity_digest to digest, and add conn into
88 * orconn_digest_map. */
89 static void
90 connection_or_set_identity_digest(or_connection_t *conn, const char *digest)
92 or_connection_t *tmp;
93 tor_assert(conn);
94 tor_assert(digest);
96 if (!orconn_identity_map)
97 orconn_identity_map = digestmap_new();
98 if (!memcmp(conn->identity_digest, digest, DIGEST_LEN))
99 return;
101 /* If the identity was set previously, remove the old mapping. */
102 if (! tor_digest_is_zero(conn->identity_digest))
103 connection_or_remove_from_identity_map(conn);
105 memcpy(conn->identity_digest, digest, DIGEST_LEN);
107 /* If we're setting the ID to zero, don't add a mapping. */
108 if (tor_digest_is_zero(digest))
109 return;
111 tmp = digestmap_set(orconn_identity_map, digest, conn);
112 conn->next_with_same_id = tmp;
114 #if 1
115 /* Testing code to check for bugs in representation. */
116 for (; tmp; tmp = tmp->next_with_same_id) {
117 tor_assert(!memcmp(tmp->identity_digest, digest, DIGEST_LEN));
118 tor_assert(tmp != conn);
120 #endif
123 /** Pack the cell_t host-order structure <b>src</b> into network-order
124 * in the buffer <b>dest</b>. See tor-spec.txt for details about the
125 * wire format.
127 * Note that this function doesn't touch <b>dst</b>-\>next: the caller
128 * should set it or clear it as appropriate.
130 void
131 cell_pack(packed_cell_t *dst, const cell_t *src)
133 char *dest = dst->body;
134 *(uint16_t*)dest = htons(src->circ_id);
135 *(uint8_t*)(dest+2) = src->command;
136 memcpy(dest+3, src->payload, CELL_PAYLOAD_SIZE);
139 /** Unpack the network-order buffer <b>src</b> into a host-order
140 * cell_t structure <b>dest</b>.
142 static void
143 cell_unpack(cell_t *dest, const char *src)
145 dest->circ_id = ntohs(*(uint16_t*)(src));
146 dest->command = *(uint8_t*)(src+2);
147 memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
150 /** Write the header of <b>cell</b> into the first VAR_CELL_HEADER_SIZE
151 * bytes of <b>hdr_out</b>. */
152 void
153 var_cell_pack_header(const var_cell_t *cell, char *hdr_out)
155 set_uint16(hdr_out, htons(cell->circ_id));
156 set_uint8(hdr_out+2, cell->command);
157 set_uint16(hdr_out+3, htons(cell->payload_len));
160 /** Allocate and return a new var_cell_t with <b>payload_len</b> bytes of
161 * payload space. */
162 var_cell_t *
163 var_cell_new(uint16_t payload_len)
165 var_cell_t *cell = tor_malloc(sizeof(var_cell_t)+payload_len-1);
166 cell->payload_len = payload_len;
167 cell->command = 0;
168 cell->circ_id = 0;
169 return cell;
172 /** Release all space held by <b>cell</b>. */
173 void
174 var_cell_free(var_cell_t *cell)
176 tor_free(cell);
179 /** We've received an EOF from <b>conn</b>. Mark it for close and return. */
181 connection_or_reached_eof(or_connection_t *conn)
183 log_info(LD_OR,"OR connection reached EOF. Closing.");
184 connection_mark_for_close(TO_CONN(conn));
185 return 0;
188 /** Handle any new bytes that have come in on connection <b>conn</b>.
189 * If conn is in 'open' state, hand it to
190 * connection_or_process_cells_from_inbuf()
191 * (else do nothing).
194 connection_or_process_inbuf(or_connection_t *conn)
196 int ret;
197 tor_assert(conn);
199 switch (conn->_base.state) {
200 case OR_CONN_STATE_PROXY_HANDSHAKING:
201 ret = connection_read_proxy_handshake(TO_CONN(conn));
203 /* start TLS after handshake completion, or deal with error */
204 if (ret == 1) {
205 tor_assert(TO_CONN(conn)->proxy_state == PROXY_CONNECTED);
206 if (connection_tls_start_handshake(conn, 0) < 0)
207 ret = -1;
209 if (ret < 0) {
210 connection_mark_for_close(TO_CONN(conn));
213 return ret;
214 case OR_CONN_STATE_OPEN:
215 case OR_CONN_STATE_OR_HANDSHAKING:
216 return connection_or_process_cells_from_inbuf(conn);
217 default:
218 return 0; /* don't do anything */
222 /** When adding cells to an OR connection's outbuf, keep adding until the
223 * outbuf is at least this long, or we run out of cells. */
224 #define OR_CONN_HIGHWATER (32*1024)
226 /** Add cells to an OR connection's outbuf whenever the outbuf's data length
227 * drops below this size. */
228 #define OR_CONN_LOWWATER (16*1024)
230 /** Called whenever we have flushed some data on an or_conn: add more data
231 * from active circuits. */
233 connection_or_flushed_some(or_connection_t *conn)
235 size_t datalen = buf_datalen(conn->_base.outbuf);
236 /* If we're under the low water mark, add cells until we're just over the
237 * high water mark. */
238 if (datalen < OR_CONN_LOWWATER) {
239 ssize_t n = (OR_CONN_HIGHWATER - datalen + CELL_NETWORK_SIZE-1)
240 / CELL_NETWORK_SIZE;
241 time_t now = approx_time();
242 while (conn->active_circuits && n > 0) {
243 int flushed;
244 flushed = connection_or_flush_from_first_active_circuit(conn, 1, now);
245 n -= flushed;
248 return 0;
251 /** Connection <b>conn</b> has finished writing and has no bytes left on
252 * its outbuf.
254 * Otherwise it's in state "open": stop writing and return.
256 * If <b>conn</b> is broken, mark it for close and return -1, else
257 * return 0.
260 connection_or_finished_flushing(or_connection_t *conn)
262 tor_assert(conn);
263 assert_connection_ok(TO_CONN(conn),0);
265 switch (conn->_base.state) {
266 case OR_CONN_STATE_PROXY_HANDSHAKING:
267 case OR_CONN_STATE_OPEN:
268 case OR_CONN_STATE_OR_HANDSHAKING:
269 connection_stop_writing(TO_CONN(conn));
270 break;
271 default:
272 log_err(LD_BUG,"Called in unexpected state %d.", conn->_base.state);
273 tor_fragile_assert();
274 return -1;
276 return 0;
279 /** Connected handler for OR connections: begin the TLS handshake.
282 connection_or_finished_connecting(or_connection_t *or_conn)
284 int proxy_type;
285 connection_t *conn;
286 tor_assert(or_conn);
287 conn = TO_CONN(or_conn);
288 tor_assert(conn->state == OR_CONN_STATE_CONNECTING);
290 log_debug(LD_HANDSHAKE,"OR connect() to router at %s:%u finished.",
291 conn->address,conn->port);
292 control_event_bootstrap(BOOTSTRAP_STATUS_HANDSHAKE, 0);
294 proxy_type = PROXY_NONE;
296 if (get_options()->HttpsProxy)
297 proxy_type = PROXY_CONNECT;
298 else if (get_options()->Socks4Proxy)
299 proxy_type = PROXY_SOCKS4;
300 else if (get_options()->Socks5Proxy)
301 proxy_type = PROXY_SOCKS5;
303 if (proxy_type != PROXY_NONE) {
304 /* start proxy handshake */
305 if (connection_proxy_connect(conn, proxy_type) < 0) {
306 connection_mark_for_close(conn);
307 return -1;
310 connection_start_reading(conn);
311 conn->state = OR_CONN_STATE_PROXY_HANDSHAKING;
312 return 0;
315 if (connection_tls_start_handshake(or_conn, 0) < 0) {
316 /* TLS handshaking error of some kind. */
317 connection_mark_for_close(conn);
318 return -1;
320 return 0;
323 /** If we don't necessarily know the router we're connecting to, but we
324 * have an addr/port/id_digest, then fill in as much as we can. Start
325 * by checking to see if this describes a router we know. */
326 static void
327 connection_or_init_conn_from_address(or_connection_t *conn,
328 const tor_addr_t *addr, uint16_t port,
329 const char *id_digest,
330 int started_here)
332 or_options_t *options = get_options();
333 routerinfo_t *r = router_get_by_digest(id_digest);
334 conn->bandwidthrate = (int)options->BandwidthRate;
335 conn->read_bucket = conn->bandwidthburst = (int)options->BandwidthBurst;
336 connection_or_set_identity_digest(conn, id_digest);
338 conn->_base.port = port;
339 tor_addr_copy(&conn->_base.addr, addr);
340 tor_addr_copy(&conn->real_addr, addr);
341 if (r) {
342 /* XXXX proposal 118 will make this more complex. */
343 if (tor_addr_eq_ipv4h(&conn->_base.addr, r->addr))
344 conn->is_canonical = 1;
345 if (!started_here) {
346 /* Override the addr/port, so our log messages will make sense.
347 * This is dangerous, since if we ever try looking up a conn by
348 * its actual addr/port, we won't remember. Careful! */
349 /* XXXX arma: this is stupid, and it's the reason we need real_addr
350 * to track is_canonical properly. What requires it? */
351 /* XXXX <arma> i believe the reason we did this, originally, is because
352 * we wanted to log what OR a connection was to, and if we logged the
353 * right IP address and port 56244, that wouldn't be as helpful. now we
354 * log the "right" port too, so we know if it's moria1 or moria2.
356 tor_addr_from_ipv4h(&conn->_base.addr, r->addr);
357 conn->_base.port = r->or_port;
359 conn->nickname = tor_strdup(r->nickname);
360 tor_free(conn->_base.address);
361 conn->_base.address = tor_strdup(r->address);
362 } else {
363 const char *n;
364 /* If we're an authoritative directory server, we may know a
365 * nickname for this router. */
366 n = dirserv_get_nickname_by_digest(id_digest);
367 if (n) {
368 conn->nickname = tor_strdup(n);
369 } else {
370 conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
371 conn->nickname[0] = '$';
372 base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
373 conn->identity_digest, DIGEST_LEN);
375 tor_free(conn->_base.address);
376 conn->_base.address = tor_dup_addr(addr);
380 /** Return true iff <b>a</b> is "better" than <b>b</b> for new circuits.
382 * A more canonical connection is always better than a less canonical
383 * connection. That aside, a connection is better if it has circuits and the
384 * other does not, or if it was created more recently.
386 * Requires that both input connections are open; not is_bad_for_new_circs,
387 * and not impossibly non-canonical.
389 * If </b>forgive_new_connections</b> is true, then we do not call
390 * <b>a</b>better than <b>b</b> simply because b has no circuits,
391 * unless b is also relatively old.
393 static int
394 connection_or_is_better(time_t now,
395 const or_connection_t *a,
396 const or_connection_t *b,
397 int forgive_new_connections)
399 int newer;
400 /** Do not definitively deprecate a new connection with no circuits on it
401 * until this much time has passed. */
402 #define NEW_CONN_GRACE_PERIOD (15*60)
404 if (b->is_canonical && !a->is_canonical)
405 return 0; /* A canonical connection is better than a non-canonical
406 * one, no matter how new it is or which has circuits. */
408 newer = b->_base.timestamp_created < a->_base.timestamp_created;
410 if (
411 /* We prefer canonical connections regardless of newness. */
412 (!b->is_canonical && a->is_canonical) ||
413 /* If both have circuits we prefer the newer: */
414 (b->n_circuits && a->n_circuits && newer) ||
415 /* If neither has circuits we prefer the newer: */
416 (!b->n_circuits && !a->n_circuits && newer))
417 return 1;
419 /* If one has no circuits and the other does... */
420 if (!b->n_circuits && a->n_circuits) {
421 /* Then it's bad, unless it's in its grace period and we're forgiving. */
422 if (forgive_new_connections &&
423 now < b->_base.timestamp_created + NEW_CONN_GRACE_PERIOD)
424 return 0;
425 else
426 return 1;
429 return 0;
432 /** Return the OR connection we should use to extend a circuit to the router
433 * whose identity is <b>digest</b>, and whose address we believe (or have been
434 * told in an extend cell) is <b>target_addr</b>. If there is no good
435 * connection, set *<b>msg_out</b> to a message describing the connection's
436 * state and our next action, and set <b>launch_out</b> to a boolean for
437 * whether we should launch a new connection or not.
439 or_connection_t *
440 connection_or_get_for_extend(const char *digest,
441 const tor_addr_t *target_addr,
442 const char **msg_out,
443 int *launch_out)
445 or_connection_t *conn, *best=NULL;
446 int n_inprogress_goodaddr = 0, n_old = 0, n_noncanonical = 0, n_possible = 0;
447 time_t now = approx_time();
449 tor_assert(msg_out);
450 tor_assert(launch_out);
452 if (!orconn_identity_map) {
453 *msg_out = "Router not connected (nothing is). Connecting.";
454 *launch_out = 1;
455 return NULL;
458 conn = digestmap_get(orconn_identity_map, digest);
460 for (; conn; conn = conn->next_with_same_id) {
461 tor_assert(conn->_base.magic == OR_CONNECTION_MAGIC);
462 tor_assert(conn->_base.type == CONN_TYPE_OR);
463 tor_assert(!memcmp(conn->identity_digest, digest, DIGEST_LEN));
464 if (conn->_base.marked_for_close)
465 continue;
466 /* Never return a non-open connection. */
467 if (conn->_base.state != OR_CONN_STATE_OPEN) {
468 /* If the address matches, don't launch a new connection for this
469 * circuit. */
470 if (!tor_addr_compare(&conn->real_addr, target_addr, CMP_EXACT))
471 ++n_inprogress_goodaddr;
472 continue;
474 /* Never return a connection that shouldn't be used for circs. */
475 if (conn->is_bad_for_new_circs) {
476 ++n_old;
477 continue;
479 /* Never return a non-canonical connection using a recent link protocol
480 * if the address is not what we wanted.
482 * (For old link protocols, we can't rely on is_canonical getting
483 * set properly if we're talking to the right address, since we might
484 * have an out-of-date descriptor, and we will get no NETINFO cell to
485 * tell us about the right address.) */
486 if (!conn->is_canonical && conn->link_proto >= 2 &&
487 tor_addr_compare(&conn->real_addr, target_addr, CMP_EXACT)) {
488 ++n_noncanonical;
489 continue;
492 ++n_possible;
494 if (!best) {
495 best = conn; /* If we have no 'best' so far, this one is good enough. */
496 continue;
499 if (connection_or_is_better(now, conn, best, 0))
500 best = conn;
503 if (best) {
504 *msg_out = "Connection is fine; using it.";
505 *launch_out = 0;
506 return best;
507 } else if (n_inprogress_goodaddr) {
508 *msg_out = "Connection in progress; waiting.";
509 *launch_out = 0;
510 return NULL;
511 } else if (n_old || n_noncanonical) {
512 *msg_out = "Connections all too old, or too non-canonical. "
513 " Launching a new one.";
514 *launch_out = 1;
515 return NULL;
516 } else {
517 *msg_out = "Not connected. Connecting.";
518 *launch_out = 1;
519 return NULL;
523 /** How old do we let a connection to an OR get before deciding it's
524 * too old for new circuits? */
525 #define TIME_BEFORE_OR_CONN_IS_TOO_OLD (60*60*24*7)
527 /** Given the head of the linked list for all the or_connections with a given
528 * identity, set elements of that list as is_bad_for_new_circs() as
529 * appropriate. Helper for connection_or_set_bad_connections().
531 static void
532 connection_or_group_set_badness(or_connection_t *head)
534 or_connection_t *or_conn = NULL, *best = NULL;
535 int n_old = 0, n_inprogress = 0, n_canonical = 0, n_other = 0;
536 time_t now = time(NULL);
538 /* Pass 1: expire everything that's old, and see what the status of
539 * everything else is. */
540 for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
541 if (or_conn->_base.marked_for_close ||
542 or_conn->is_bad_for_new_circs)
543 continue;
544 if (or_conn->_base.timestamp_created + TIME_BEFORE_OR_CONN_IS_TOO_OLD
545 < now) {
546 log_info(LD_OR,
547 "Marking OR conn to %s:%d as too old for new circuits "
548 "(fd %d, %d secs old).",
549 or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
550 (int)(now - or_conn->_base.timestamp_created));
551 or_conn->is_bad_for_new_circs = 1;
554 if (or_conn->is_bad_for_new_circs) {
555 ++n_old;
556 } else if (or_conn->_base.state != OR_CONN_STATE_OPEN) {
557 ++n_inprogress;
558 } else if (or_conn->is_canonical) {
559 ++n_canonical;
560 } else {
561 ++n_other;
565 /* Pass 2: We know how about how good the best connection is.
566 * expire everything that's worse, and find the very best if we can. */
567 for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
568 if (or_conn->_base.marked_for_close ||
569 or_conn->is_bad_for_new_circs)
570 continue; /* This one doesn't need to be marked bad. */
571 if (or_conn->_base.state != OR_CONN_STATE_OPEN)
572 continue; /* Don't mark anything bad until we have seen what happens
573 * when the connection finishes. */
574 if (n_canonical && !or_conn->is_canonical) {
575 /* We have at least one open canonical connection to this router,
576 * and this one is open but not canonical. Mark it bad. */
577 log_info(LD_OR,
578 "Marking OR conn to %s:%d as too old for new circuits: "
579 "(fd %d, %d secs old). It is not canonical, and we have "
580 "another connection to that OR that is.",
581 or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
582 (int)(now - or_conn->_base.timestamp_created));
583 or_conn->is_bad_for_new_circs = 1;
584 continue;
587 if (!best || connection_or_is_better(now, or_conn, best, 0))
588 best = or_conn;
591 if (!best)
592 return;
594 /* Pass 3: One connection to OR is best. If it's canonical, mark as bad
595 * every other open connection. If it's non-canonical, mark as bad
596 * every other open connection to the same address.
598 * XXXX This isn't optimal; if we have connections to an OR at multiple
599 * addresses, we'd like to pick the best _for each address_, and mark as
600 * bad every open connection that isn't best for its address. But this
601 * can only occur in cases where the other OR is old (so we have no
602 * canonical connection to it), or where all the connections to the OR are
603 * at noncanonical addresses and we have no good direct connection (which
604 * means we aren't at risk of attaching circuits to it anyway). As
605 * 0.1.2.x dies out, the first case will go away, and the second one is
606 * "mostly harmless", so a fix can wait until somebody is bored.
608 for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
609 if (or_conn->_base.marked_for_close ||
610 or_conn->is_bad_for_new_circs ||
611 or_conn->_base.state != OR_CONN_STATE_OPEN)
612 continue;
613 if (or_conn != best && connection_or_is_better(now, best, or_conn, 1)) {
614 /* This isn't the best conn, _and_ the best conn is better than it,
615 even when we're being forgiving. */
616 if (best->is_canonical) {
617 log_info(LD_OR,
618 "Marking OR conn to %s:%d as too old for new circuits: "
619 "(fd %d, %d secs old). We have a better canonical one "
620 "(fd %d; %d secs old).",
621 or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
622 (int)(now - or_conn->_base.timestamp_created),
623 best->_base.s, (int)(now - best->_base.timestamp_created));
624 or_conn->is_bad_for_new_circs = 1;
625 } else if (!tor_addr_compare(&or_conn->real_addr,
626 &best->real_addr, CMP_EXACT)) {
627 log_info(LD_OR,
628 "Marking OR conn to %s:%d as too old for new circuits: "
629 "(fd %d, %d secs old). We have a better one "
630 "(fd %d; %d secs old).",
631 or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
632 (int)(now - or_conn->_base.timestamp_created),
633 best->_base.s, (int)(now - best->_base.timestamp_created));
634 or_conn->is_bad_for_new_circs = 1;
640 /** Go through all the OR connections, and set the is_bad_for_new_circs
641 * flag on:
642 * - all connections that are too old.
643 * - all open non-canonical connections for which a canonical connection
644 * exists to the same router.
645 * - all open canonical connections for which a 'better' canonical
646 * connection exists to the same router.
647 * - all open non-canonical connections for which a 'better' non-canonical
648 * connection exists to the same router at the same address.
650 * See connection_or_is_better() for our idea of what makes one OR connection
651 * better than another.
653 void
654 connection_or_set_bad_connections(void)
656 if (!orconn_identity_map)
657 return;
659 DIGESTMAP_FOREACH(orconn_identity_map, identity, or_connection_t *, conn) {
660 connection_or_group_set_badness(conn);
661 } DIGESTMAP_FOREACH_END;
664 /** <b>conn</b> is in the 'connecting' state, and it failed to complete
665 * a TCP connection. Send notifications appropriately.
667 * <b>reason</b> specifies the or_conn_end_reason for the failure;
668 * <b>msg</b> specifies the strerror-style error message.
670 void
671 connection_or_connect_failed(or_connection_t *conn,
672 int reason, const char *msg)
674 control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED, reason);
675 if (!authdir_mode_tests_reachability(get_options()))
676 control_event_bootstrap_problem(msg, reason);
679 /** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
680 * handshake with an OR with identity digest <b>id_digest</b>.
682 * If <b>id_digest</b> is me, do nothing. If we're already connected to it,
683 * return that connection. If the connect() is in progress, set the
684 * new conn's state to 'connecting' and return it. If connect() succeeds,
685 * call connection_tls_start_handshake() on it.
687 * This function is called from router_retry_connections(), for
688 * ORs connecting to ORs, and circuit_establish_circuit(), for
689 * OPs connecting to ORs.
691 * Return the launched conn, or NULL if it failed.
693 or_connection_t *
694 connection_or_connect(const tor_addr_t *_addr, uint16_t port,
695 const char *id_digest)
697 or_connection_t *conn;
698 or_options_t *options = get_options();
699 int socket_error = 0;
700 int using_proxy = 0;
701 tor_addr_t addr;
703 tor_assert(_addr);
704 tor_assert(id_digest);
705 tor_addr_copy(&addr, _addr);
707 if (server_mode(options) && router_digest_is_me(id_digest)) {
708 log_info(LD_PROTOCOL,"Client asked me to connect to myself. Refusing.");
709 return NULL;
712 conn = or_connection_new(AF_INET);
714 /* set up conn so it's got all the data we need to remember */
715 connection_or_init_conn_from_address(conn, &addr, port, id_digest, 1);
716 conn->_base.state = OR_CONN_STATE_CONNECTING;
717 control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED, 0);
719 /* use a proxy server if available */
720 if (options->HttpsProxy) {
721 using_proxy = 1;
722 tor_addr_copy(&addr, &options->HttpsProxyAddr);
723 port = options->HttpsProxyPort;
724 } else if (options->Socks4Proxy) {
725 using_proxy = 1;
726 tor_addr_copy(&addr, &options->Socks4ProxyAddr);
727 port = options->Socks4ProxyPort;
728 } else if (options->Socks5Proxy) {
729 using_proxy = 1;
730 tor_addr_copy(&addr, &options->Socks5ProxyAddr);
731 port = options->Socks5ProxyPort;
734 switch (connection_connect(TO_CONN(conn), conn->_base.address,
735 &addr, port, &socket_error)) {
736 case -1:
737 /* If the connection failed immediately, and we're using
738 * a proxy, our proxy is down. Don't blame the Tor server. */
739 if (!using_proxy)
740 entry_guard_register_connect_status(conn->identity_digest,
741 0, 1, time(NULL));
742 connection_or_connect_failed(conn,
743 errno_to_orconn_end_reason(socket_error),
744 tor_socket_strerror(socket_error));
745 connection_free(TO_CONN(conn));
746 return NULL;
747 case 0:
748 connection_watch_events(TO_CONN(conn), READ_EVENT | WRITE_EVENT);
749 /* writable indicates finish, readable indicates broken link,
750 error indicates broken link on windows */
751 return conn;
752 /* case 1: fall through */
755 if (connection_or_finished_connecting(conn) < 0) {
756 /* already marked for close */
757 return NULL;
759 return conn;
762 /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
763 * we initiated the connection, else it's 1.
765 * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and
766 * pass <b>conn</b> to connection_tls_continue_handshake().
768 * Return -1 if <b>conn</b> is broken, else return 0.
771 connection_tls_start_handshake(or_connection_t *conn, int receiving)
773 conn->_base.state = OR_CONN_STATE_TLS_HANDSHAKING;
774 conn->tls = tor_tls_new(conn->_base.s, receiving);
775 tor_tls_set_logged_address(conn->tls, // XXX client and relay?
776 escaped_safe_str(conn->_base.address));
777 if (!conn->tls) {
778 log_warn(LD_BUG,"tor_tls_new failed. Closing.");
779 return -1;
781 connection_start_reading(TO_CONN(conn));
782 log_debug(LD_HANDSHAKE,"starting TLS handshake on fd %d", conn->_base.s);
783 note_crypto_pk_op(receiving ? TLS_HANDSHAKE_S : TLS_HANDSHAKE_C);
785 if (connection_tls_continue_handshake(conn) < 0) {
786 return -1;
788 return 0;
791 /** Invoked on the server side from inside tor_tls_read() when the server
792 * gets a successful TLS renegotiation from the client. */
793 static void
794 connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn)
796 or_connection_t *conn = _conn;
797 (void)tls;
799 /* Don't invoke this again. */
800 tor_tls_set_renegotiate_callback(tls, NULL, NULL);
801 tor_tls_block_renegotiation(tls);
803 if (connection_tls_finish_handshake(conn) < 0) {
804 /* XXXX_TLS double-check that it's ok to do this from inside read. */
805 /* XXXX_TLS double-check that this verifies certificates. */
806 connection_mark_for_close(TO_CONN(conn));
810 /** Move forward with the tls handshake. If it finishes, hand
811 * <b>conn</b> to connection_tls_finish_handshake().
813 * Return -1 if <b>conn</b> is broken, else return 0.
816 connection_tls_continue_handshake(or_connection_t *conn)
818 int result;
819 check_no_tls_errors();
820 again:
821 if (conn->_base.state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
822 // log_notice(LD_OR, "Renegotiate with %p", conn->tls);
823 result = tor_tls_renegotiate(conn->tls);
824 // log_notice(LD_OR, "Result: %d", result);
825 } else {
826 tor_assert(conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING);
827 // log_notice(LD_OR, "Continue handshake with %p", conn->tls);
828 result = tor_tls_handshake(conn->tls);
829 // log_notice(LD_OR, "Result: %d", result);
831 switch (result) {
832 CASE_TOR_TLS_ERROR_ANY:
833 log_info(LD_OR,"tls error [%s]. breaking connection.",
834 tor_tls_err_to_string(result));
835 return -1;
836 case TOR_TLS_DONE:
837 if (! tor_tls_used_v1_handshake(conn->tls)) {
838 if (!tor_tls_is_server(conn->tls)) {
839 if (conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING) {
840 // log_notice(LD_OR,"Done. state was TLS_HANDSHAKING.");
841 conn->_base.state = OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING;
842 goto again;
844 // log_notice(LD_OR,"Done. state was %d.", conn->_base.state);
845 } else {
846 /* improved handshake, but not a client. */
847 tor_tls_set_renegotiate_callback(conn->tls,
848 connection_or_tls_renegotiated_cb,
849 conn);
850 conn->_base.state = OR_CONN_STATE_TLS_SERVER_RENEGOTIATING;
851 connection_stop_writing(TO_CONN(conn));
852 connection_start_reading(TO_CONN(conn));
853 return 0;
856 return connection_tls_finish_handshake(conn);
857 case TOR_TLS_WANTWRITE:
858 connection_start_writing(TO_CONN(conn));
859 log_debug(LD_OR,"wanted write");
860 return 0;
861 case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
862 log_debug(LD_OR,"wanted read");
863 return 0;
864 case TOR_TLS_CLOSE:
865 log_info(LD_OR,"tls closed. breaking connection.");
866 return -1;
868 return 0;
871 /** Return 1 if we initiated this connection, or 0 if it started
872 * out as an incoming connection.
875 connection_or_nonopen_was_started_here(or_connection_t *conn)
877 tor_assert(conn->_base.type == CONN_TYPE_OR);
878 if (!conn->tls)
879 return 1; /* it's still in proxy states or something */
880 if (conn->handshake_state)
881 return conn->handshake_state->started_here;
882 return !tor_tls_is_server(conn->tls);
885 /** <b>Conn</b> just completed its handshake. Return 0 if all is well, and
886 * return -1 if he is lying, broken, or otherwise something is wrong.
888 * If we initiated this connection (<b>started_here</b> is true), make sure
889 * the other side sent sent a correctly formed certificate. If I initiated the
890 * connection, make sure it's the right guy.
892 * Otherwise (if we _didn't_ initiate this connection), it's okay for
893 * the certificate to be weird or absent.
895 * If we return 0, and the certificate is as expected, write a hash of the
896 * identity key into digest_rcvd, which must have DIGEST_LEN space in it. (If
897 * we return -1 this buffer is undefined.) If the certificate is invalid
898 * or missing on an incoming connection, we return 0 and set digest_rcvd to
899 * DIGEST_LEN 0 bytes.
901 * As side effects,
902 * 1) Set conn->circ_id_type according to tor-spec.txt.
903 * 2) If we're an authdirserver and we initiated the connection: drop all
904 * descriptors that claim to be on that IP/port but that aren't
905 * this guy; and note that this guy is reachable.
907 static int
908 connection_or_check_valid_tls_handshake(or_connection_t *conn,
909 int started_here,
910 char *digest_rcvd_out)
912 crypto_pk_env_t *identity_rcvd=NULL;
913 or_options_t *options = get_options();
914 int severity = server_mode(options) ? LOG_PROTOCOL_WARN : LOG_WARN;
915 const char *safe_address =
916 started_here ? conn->_base.address :
917 safe_str_client(conn->_base.address);
918 const char *conn_type = started_here ? "outgoing" : "incoming";
919 int has_cert = 0, has_identity=0;
921 check_no_tls_errors();
922 has_cert = tor_tls_peer_has_cert(conn->tls);
923 if (started_here && !has_cert) {
924 log_info(LD_HANDSHAKE,"Tried connecting to router at %s:%d, but it didn't "
925 "send a cert! Closing.",
926 safe_address, conn->_base.port);
927 return -1;
928 } else if (!has_cert) {
929 log_debug(LD_HANDSHAKE,"Got incoming connection with no certificate. "
930 "That's ok.");
932 check_no_tls_errors();
934 if (has_cert) {
935 int v = tor_tls_verify(started_here?severity:LOG_INFO,
936 conn->tls, &identity_rcvd);
937 if (started_here && v<0) {
938 log_fn(severity,LD_HANDSHAKE,"Tried connecting to router at %s:%d: It"
939 " has a cert but it's invalid. Closing.",
940 safe_address, conn->_base.port);
941 return -1;
942 } else if (v<0) {
943 log_info(LD_HANDSHAKE,"Incoming connection gave us an invalid cert "
944 "chain; ignoring.");
945 } else {
946 log_debug(LD_HANDSHAKE,
947 "The certificate seems to be valid on %s connection "
948 "with %s:%d", conn_type, safe_address, conn->_base.port);
950 check_no_tls_errors();
953 if (identity_rcvd) {
954 has_identity = 1;
955 crypto_pk_get_digest(identity_rcvd, digest_rcvd_out);
956 if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
957 conn->circ_id_type = CIRC_ID_TYPE_LOWER;
958 } else {
959 conn->circ_id_type = CIRC_ID_TYPE_HIGHER;
961 crypto_free_pk_env(identity_rcvd);
962 } else {
963 memset(digest_rcvd_out, 0, DIGEST_LEN);
964 conn->circ_id_type = CIRC_ID_TYPE_NEITHER;
967 if (started_here && tor_digest_is_zero(conn->identity_digest)) {
968 connection_or_set_identity_digest(conn, digest_rcvd_out);
969 tor_free(conn->nickname);
970 conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
971 conn->nickname[0] = '$';
972 base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
973 conn->identity_digest, DIGEST_LEN);
974 log_info(LD_HANDSHAKE, "Connected to router %s at %s:%d without knowing "
975 "its key. Hoping for the best.",
976 conn->nickname, conn->_base.address, conn->_base.port);
979 if (started_here) {
980 int as_advertised = 1;
981 tor_assert(has_cert);
982 tor_assert(has_identity);
983 if (memcmp(digest_rcvd_out, conn->identity_digest, DIGEST_LEN)) {
984 /* I was aiming for a particular digest. I didn't get it! */
985 char seen[HEX_DIGEST_LEN+1];
986 char expected[HEX_DIGEST_LEN+1];
987 base16_encode(seen, sizeof(seen), digest_rcvd_out, DIGEST_LEN);
988 base16_encode(expected, sizeof(expected), conn->identity_digest,
989 DIGEST_LEN);
990 log_fn(severity, LD_HANDSHAKE,
991 "Tried connecting to router at %s:%d, but identity key was not "
992 "as expected: wanted %s but got %s.",
993 conn->_base.address, conn->_base.port, expected, seen);
994 entry_guard_register_connect_status(conn->identity_digest, 0, 1,
995 time(NULL));
996 control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED,
997 END_OR_CONN_REASON_OR_IDENTITY);
998 if (!authdir_mode_tests_reachability(options))
999 control_event_bootstrap_problem("foo", END_OR_CONN_REASON_OR_IDENTITY);
1000 as_advertised = 0;
1002 if (authdir_mode_tests_reachability(options)) {
1003 /* We initiated this connection to address:port. Drop all routers
1004 * with the same address:port and a different key.
1006 dirserv_orconn_tls_done(conn->_base.address, conn->_base.port,
1007 digest_rcvd_out, as_advertised);
1009 if (!as_advertised)
1010 return -1;
1012 return 0;
1015 /** The tls handshake is finished.
1017 * Make sure we are happy with the person we just handshaked with.
1019 * If he initiated the connection, make sure he's not already connected,
1020 * then initialize conn from the information in router.
1022 * If all is successful, call circuit_n_conn_done() to handle events
1023 * that have been pending on the <tls handshake completion. Also set the
1024 * directory to be dirty (only matters if I'm an authdirserver).
1026 static int
1027 connection_tls_finish_handshake(or_connection_t *conn)
1029 char digest_rcvd[DIGEST_LEN];
1030 int started_here = connection_or_nonopen_was_started_here(conn);
1032 log_debug(LD_HANDSHAKE,"tls handshake with %s done. verifying.",
1033 safe_str_client(conn->_base.address));
1035 directory_set_dirty();
1037 if (connection_or_check_valid_tls_handshake(conn, started_here,
1038 digest_rcvd) < 0)
1039 return -1;
1041 circuit_build_times_network_is_live(&circ_times);
1043 if (tor_tls_used_v1_handshake(conn->tls)) {
1044 conn->link_proto = 1;
1045 if (!started_here) {
1046 connection_or_init_conn_from_address(conn, &conn->_base.addr,
1047 conn->_base.port, digest_rcvd, 0);
1049 tor_tls_block_renegotiation(conn->tls);
1050 return connection_or_set_state_open(conn);
1051 } else {
1052 conn->_base.state = OR_CONN_STATE_OR_HANDSHAKING;
1053 if (connection_init_or_handshake_state(conn, started_here) < 0)
1054 return -1;
1055 if (!started_here) {
1056 connection_or_init_conn_from_address(conn, &conn->_base.addr,
1057 conn->_base.port, digest_rcvd, 0);
1059 return connection_or_send_versions(conn);
1063 /** Allocate a new connection handshake state for the connection
1064 * <b>conn</b>. Return 0 on success, -1 on failure. */
1065 static int
1066 connection_init_or_handshake_state(or_connection_t *conn, int started_here)
1068 or_handshake_state_t *s;
1069 s = conn->handshake_state = tor_malloc_zero(sizeof(or_handshake_state_t));
1070 s->started_here = started_here ? 1 : 0;
1071 return 0;
1074 /** Free all storage held by <b>state</b>. */
1075 void
1076 or_handshake_state_free(or_handshake_state_t *state)
1078 if (!state)
1079 return;
1080 memset(state, 0xBE, sizeof(or_handshake_state_t));
1081 tor_free(state);
1084 /** Set <b>conn</b>'s state to OR_CONN_STATE_OPEN, and tell other subsystems
1085 * as appropriate. Called when we are done with all TLS and OR handshaking.
1088 connection_or_set_state_open(or_connection_t *conn)
1090 int started_here = connection_or_nonopen_was_started_here(conn);
1091 time_t now = time(NULL);
1092 conn->_base.state = OR_CONN_STATE_OPEN;
1093 control_event_or_conn_status(conn, OR_CONN_EVENT_CONNECTED, 0);
1095 if (started_here) {
1096 circuit_build_times_network_is_live(&circ_times);
1097 rep_hist_note_connect_succeeded(conn->identity_digest, now);
1098 if (entry_guard_register_connect_status(conn->identity_digest,
1099 1, 0, now) < 0) {
1100 /* Close any circuits pending on this conn. We leave it in state
1101 * 'open' though, because it didn't actually *fail* -- we just
1102 * chose not to use it. (Otherwise
1103 * connection_about_to_close_connection() will call a big pile of
1104 * functions to indicate we shouldn't try it again.) */
1105 log_debug(LD_OR, "New entry guard was reachable, but closing this "
1106 "connection so we can retry the earlier entry guards.");
1107 circuit_n_conn_done(conn, 0);
1108 return -1;
1110 router_set_status(conn->identity_digest, 1);
1111 } else {
1112 /* only report it to the geoip module if it's not a known router */
1113 if (!router_get_by_digest(conn->identity_digest)) {
1114 if (tor_addr_family(&TO_CONN(conn)->addr) == AF_INET) {
1115 /*XXXX IP6 support ipv6 geoip.*/
1116 uint32_t a = tor_addr_to_ipv4h(&TO_CONN(conn)->addr);
1117 geoip_note_client_seen(GEOIP_CLIENT_CONNECT, a, now);
1122 or_handshake_state_free(conn->handshake_state);
1123 conn->handshake_state = NULL;
1125 connection_start_reading(TO_CONN(conn));
1126 circuit_n_conn_done(conn, 1); /* send the pending creates, if any. */
1128 return 0;
1131 /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s outbuf.
1132 * For cells that use or affect a circuit, this should only be called by
1133 * connection_or_flush_from_first_active_circuit().
1135 void
1136 connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
1138 packed_cell_t networkcell;
1140 tor_assert(cell);
1141 tor_assert(conn);
1143 cell_pack(&networkcell, cell);
1145 connection_write_to_buf(networkcell.body, CELL_NETWORK_SIZE, TO_CONN(conn));
1147 if (cell->command != CELL_PADDING)
1148 conn->timestamp_last_added_nonpadding = approx_time();
1151 /** Pack a variable-length <b>cell</b> into wire-format, and write it onto
1152 * <b>conn</b>'s outbuf. Right now, this <em>DOES NOT</em> support cells that
1153 * affect a circuit.
1155 void
1156 connection_or_write_var_cell_to_buf(const var_cell_t *cell,
1157 or_connection_t *conn)
1159 char hdr[VAR_CELL_HEADER_SIZE];
1160 tor_assert(cell);
1161 tor_assert(conn);
1162 var_cell_pack_header(cell, hdr);
1163 connection_write_to_buf(hdr, sizeof(hdr), TO_CONN(conn));
1164 connection_write_to_buf(cell->payload, cell->payload_len, TO_CONN(conn));
1165 if (cell->command != CELL_PADDING)
1166 conn->timestamp_last_added_nonpadding = approx_time();
1169 /** See whether there's a variable-length cell waiting on <b>conn</b>'s
1170 * inbuf. Return values as for fetch_var_cell_from_buf(). */
1171 static int
1172 connection_fetch_var_cell_from_buf(or_connection_t *conn, var_cell_t **out)
1174 return fetch_var_cell_from_buf(conn->_base.inbuf, out, conn->link_proto);
1177 /** Process cells from <b>conn</b>'s inbuf.
1179 * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
1180 * and hand it to command_process_cell().
1182 * Always return 0.
1184 static int
1185 connection_or_process_cells_from_inbuf(or_connection_t *conn)
1187 var_cell_t *var_cell;
1189 while (1) {
1190 log_debug(LD_OR,
1191 "%d: starting, inbuf_datalen %d (%d pending in tls object).",
1192 conn->_base.s,(int)buf_datalen(conn->_base.inbuf),
1193 tor_tls_get_pending_bytes(conn->tls));
1194 if (connection_fetch_var_cell_from_buf(conn, &var_cell)) {
1195 if (!var_cell)
1196 return 0; /* not yet. */
1197 circuit_build_times_network_is_live(&circ_times);
1198 command_process_var_cell(var_cell, conn);
1199 var_cell_free(var_cell);
1200 } else {
1201 char buf[CELL_NETWORK_SIZE];
1202 cell_t cell;
1203 if (buf_datalen(conn->_base.inbuf) < CELL_NETWORK_SIZE) /* whole response
1204 available? */
1205 return 0; /* not yet */
1207 circuit_build_times_network_is_live(&circ_times);
1208 connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, TO_CONN(conn));
1210 /* retrieve cell info from buf (create the host-order struct from the
1211 * network-order string) */
1212 cell_unpack(&cell, buf);
1214 command_process_cell(&cell, conn);
1219 /** Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
1220 * onto OR connection <b>conn</b>. Don't perform range-checking on reason:
1221 * we may want to propagate reasons from other cells.
1223 * Return 0.
1226 connection_or_send_destroy(circid_t circ_id, or_connection_t *conn, int reason)
1228 cell_t cell;
1230 tor_assert(conn);
1232 memset(&cell, 0, sizeof(cell_t));
1233 cell.circ_id = circ_id;
1234 cell.command = CELL_DESTROY;
1235 cell.payload[0] = (uint8_t) reason;
1236 log_debug(LD_OR,"Sending destroy (circID %d).", circ_id);
1238 /* XXXX It's possible that under some circumstances, we want the destroy
1239 * to take precedence over other data waiting on the circuit's cell queue.
1242 connection_or_write_cell_to_buf(&cell, conn);
1243 return 0;
1246 /** Array of recognized link protocol versions. */
1247 static const uint16_t or_protocol_versions[] = { 1, 2 };
1248 /** Number of versions in <b>or_protocol_versions</b>. */
1249 static const int n_or_protocol_versions =
1250 (int)( sizeof(or_protocol_versions)/sizeof(uint16_t) );
1252 /** Return true iff <b>v</b> is a link protocol version that this Tor
1253 * implementation believes it can support. */
1255 is_or_protocol_version_known(uint16_t v)
1257 int i;
1258 for (i = 0; i < n_or_protocol_versions; ++i) {
1259 if (or_protocol_versions[i] == v)
1260 return 1;
1262 return 0;
1265 /** Send a VERSIONS cell on <b>conn</b>, telling the other host about the
1266 * link protocol versions that this Tor can support. */
1267 static int
1268 connection_or_send_versions(or_connection_t *conn)
1270 var_cell_t *cell;
1271 int i;
1272 tor_assert(conn->handshake_state &&
1273 !conn->handshake_state->sent_versions_at);
1274 cell = var_cell_new(n_or_protocol_versions * 2);
1275 cell->command = CELL_VERSIONS;
1276 for (i = 0; i < n_or_protocol_versions; ++i) {
1277 uint16_t v = or_protocol_versions[i];
1278 set_uint16(cell->payload+(2*i), htons(v));
1281 connection_or_write_var_cell_to_buf(cell, conn);
1282 conn->handshake_state->sent_versions_at = time(NULL);
1284 var_cell_free(cell);
1285 return 0;
1288 /** Send a NETINFO cell on <b>conn</b>, telling the other server what we know
1289 * about their address, our address, and the current time. */
1291 connection_or_send_netinfo(or_connection_t *conn)
1293 cell_t cell;
1294 time_t now = time(NULL);
1295 routerinfo_t *me;
1296 int len;
1297 char *out;
1299 memset(&cell, 0, sizeof(cell_t));
1300 cell.command = CELL_NETINFO;
1302 /* Timestamp. */
1303 set_uint32(cell.payload, htonl((uint32_t)now));
1305 /* Their address. */
1306 out = cell.payload + 4;
1307 len = append_address_to_payload(out, &conn->_base.addr);
1308 if (len<0)
1309 return -1;
1310 out += len;
1312 /* My address. */
1313 if ((me = router_get_my_routerinfo())) {
1314 tor_addr_t my_addr;
1315 *out++ = 1; /* only one address is supported. */
1317 tor_addr_from_ipv4h(&my_addr, me->addr);
1318 len = append_address_to_payload(out, &my_addr);
1319 if (len < 0)
1320 return -1;
1321 out += len;
1322 } else {
1323 *out++ = 0;
1326 connection_or_write_cell_to_buf(&cell, conn);
1328 return 0;