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-2010, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
8 * \file connection_or.c
9 * \brief Functions to handle OR connections, TLS handshaking, and
10 * cells on the network.
15 #include "circuitbuild.h"
18 #include "connection.h"
19 #include "connection_or.h"
24 #include "routerlist.h"
26 static int connection_tls_finish_handshake(or_connection_t
*conn
);
27 static int connection_or_process_cells_from_inbuf(or_connection_t
*conn
);
28 static int connection_or_send_versions(or_connection_t
*conn
);
29 static int connection_init_or_handshake_state(or_connection_t
*conn
,
31 static int connection_or_check_valid_tls_handshake(or_connection_t
*conn
,
33 char *digest_rcvd_out
);
35 /**************************************************************/
37 /** Map from identity digest of connected OR or desired OR to a connection_t
38 * with that identity digest. If there is more than one such connection_t,
39 * they form a linked list, with next_with_same_id as the next pointer. */
40 static digestmap_t
*orconn_identity_map
= NULL
;
42 /** If conn is listed in orconn_identity_map, remove it, and clear
43 * conn->identity_digest. Otherwise do nothing. */
45 connection_or_remove_from_identity_map(or_connection_t
*conn
)
49 if (!orconn_identity_map
)
51 tmp
= digestmap_get(orconn_identity_map
, conn
->identity_digest
);
53 if (!tor_digest_is_zero(conn
->identity_digest
)) {
54 log_warn(LD_BUG
, "Didn't find connection '%s' on identity map when "
55 "trying to remove it.",
56 conn
->nickname
? conn
->nickname
: "NULL");
61 if (conn
->next_with_same_id
)
62 digestmap_set(orconn_identity_map
, conn
->identity_digest
,
63 conn
->next_with_same_id
);
65 digestmap_remove(orconn_identity_map
, conn
->identity_digest
);
67 while (tmp
->next_with_same_id
) {
68 if (tmp
->next_with_same_id
== conn
) {
69 tmp
->next_with_same_id
= conn
->next_with_same_id
;
72 tmp
= tmp
->next_with_same_id
;
75 memset(conn
->identity_digest
, 0, DIGEST_LEN
);
76 conn
->next_with_same_id
= NULL
;
79 /** Remove all entries from the identity-to-orconn map, and clear
80 * all identities in OR conns.*/
82 connection_or_clear_identity_map(void)
84 smartlist_t
*conns
= get_connection_array();
85 SMARTLIST_FOREACH(conns
, connection_t
*, conn
,
87 if (conn
->type
== CONN_TYPE_OR
) {
88 or_connection_t
*or_conn
= TO_OR_CONN(conn
);
89 memset(or_conn
->identity_digest
, 0, DIGEST_LEN
);
90 or_conn
->next_with_same_id
= NULL
;
94 digestmap_free(orconn_identity_map
, NULL
);
95 orconn_identity_map
= NULL
;
98 /** Change conn->identity_digest to digest, and add conn into
99 * orconn_digest_map. */
101 connection_or_set_identity_digest(or_connection_t
*conn
, const char *digest
)
103 or_connection_t
*tmp
;
107 if (!orconn_identity_map
)
108 orconn_identity_map
= digestmap_new();
109 if (!memcmp(conn
->identity_digest
, digest
, DIGEST_LEN
))
112 /* If the identity was set previously, remove the old mapping. */
113 if (! tor_digest_is_zero(conn
->identity_digest
))
114 connection_or_remove_from_identity_map(conn
);
116 memcpy(conn
->identity_digest
, digest
, DIGEST_LEN
);
118 /* If we're setting the ID to zero, don't add a mapping. */
119 if (tor_digest_is_zero(digest
))
122 tmp
= digestmap_set(orconn_identity_map
, digest
, conn
);
123 conn
->next_with_same_id
= tmp
;
126 /* Testing code to check for bugs in representation. */
127 for (; tmp
; tmp
= tmp
->next_with_same_id
) {
128 tor_assert(!memcmp(tmp
->identity_digest
, digest
, DIGEST_LEN
));
129 tor_assert(tmp
!= conn
);
134 /** Pack the cell_t host-order structure <b>src</b> into network-order
135 * in the buffer <b>dest</b>. See tor-spec.txt for details about the
138 * Note that this function doesn't touch <b>dst</b>-\>next: the caller
139 * should set it or clear it as appropriate.
142 cell_pack(packed_cell_t
*dst
, const cell_t
*src
)
144 char *dest
= dst
->body
;
145 *(uint16_t*)dest
= htons(src
->circ_id
);
146 *(uint8_t*)(dest
+2) = src
->command
;
147 memcpy(dest
+3, src
->payload
, CELL_PAYLOAD_SIZE
);
150 /** Unpack the network-order buffer <b>src</b> into a host-order
151 * cell_t structure <b>dest</b>.
154 cell_unpack(cell_t
*dest
, const char *src
)
156 dest
->circ_id
= ntohs(*(uint16_t*)(src
));
157 dest
->command
= *(uint8_t*)(src
+2);
158 memcpy(dest
->payload
, src
+3, CELL_PAYLOAD_SIZE
);
161 /** Write the header of <b>cell</b> into the first VAR_CELL_HEADER_SIZE
162 * bytes of <b>hdr_out</b>. */
164 var_cell_pack_header(const var_cell_t
*cell
, char *hdr_out
)
166 set_uint16(hdr_out
, htons(cell
->circ_id
));
167 set_uint8(hdr_out
+2, cell
->command
);
168 set_uint16(hdr_out
+3, htons(cell
->payload_len
));
171 /** Allocate and return a new var_cell_t with <b>payload_len</b> bytes of
174 var_cell_new(uint16_t payload_len
)
176 var_cell_t
*cell
= tor_malloc(sizeof(var_cell_t
)+payload_len
-1);
177 cell
->payload_len
= payload_len
;
183 /** Release all space held by <b>cell</b>. */
185 var_cell_free(var_cell_t
*cell
)
190 /** We've received an EOF from <b>conn</b>. Mark it for close and return. */
192 connection_or_reached_eof(or_connection_t
*conn
)
194 log_info(LD_OR
,"OR connection reached EOF. Closing.");
195 connection_mark_for_close(TO_CONN(conn
));
199 /** Handle any new bytes that have come in on connection <b>conn</b>.
200 * If conn is in 'open' state, hand it to
201 * connection_or_process_cells_from_inbuf()
205 connection_or_process_inbuf(or_connection_t
*conn
)
210 switch (conn
->_base
.state
) {
211 case OR_CONN_STATE_PROXY_HANDSHAKING
:
212 ret
= connection_read_proxy_handshake(TO_CONN(conn
));
214 /* start TLS after handshake completion, or deal with error */
216 tor_assert(TO_CONN(conn
)->proxy_state
== PROXY_CONNECTED
);
217 if (connection_tls_start_handshake(conn
, 0) < 0)
221 connection_mark_for_close(TO_CONN(conn
));
225 case OR_CONN_STATE_OPEN
:
226 case OR_CONN_STATE_OR_HANDSHAKING
:
227 return connection_or_process_cells_from_inbuf(conn
);
229 return 0; /* don't do anything */
233 /** When adding cells to an OR connection's outbuf, keep adding until the
234 * outbuf is at least this long, or we run out of cells. */
235 #define OR_CONN_HIGHWATER (32*1024)
237 /** Add cells to an OR connection's outbuf whenever the outbuf's data length
238 * drops below this size. */
239 #define OR_CONN_LOWWATER (16*1024)
241 /** Called whenever we have flushed some data on an or_conn: add more data
242 * from active circuits. */
244 connection_or_flushed_some(or_connection_t
*conn
)
246 size_t datalen
= buf_datalen(conn
->_base
.outbuf
);
247 /* If we're under the low water mark, add cells until we're just over the
248 * high water mark. */
249 if (datalen
< OR_CONN_LOWWATER
) {
250 ssize_t n
= (OR_CONN_HIGHWATER
- datalen
+ CELL_NETWORK_SIZE
-1)
252 time_t now
= approx_time();
253 while (conn
->active_circuits
&& n
> 0) {
255 flushed
= connection_or_flush_from_first_active_circuit(conn
, 1, now
);
262 /** Connection <b>conn</b> has finished writing and has no bytes left on
265 * Otherwise it's in state "open": stop writing and return.
267 * If <b>conn</b> is broken, mark it for close and return -1, else
271 connection_or_finished_flushing(or_connection_t
*conn
)
274 assert_connection_ok(TO_CONN(conn
),0);
276 switch (conn
->_base
.state
) {
277 case OR_CONN_STATE_PROXY_HANDSHAKING
:
278 case OR_CONN_STATE_OPEN
:
279 case OR_CONN_STATE_OR_HANDSHAKING
:
280 connection_stop_writing(TO_CONN(conn
));
283 log_err(LD_BUG
,"Called in unexpected state %d.", conn
->_base
.state
);
284 tor_fragile_assert();
290 /** Connected handler for OR connections: begin the TLS handshake.
293 connection_or_finished_connecting(or_connection_t
*or_conn
)
298 conn
= TO_CONN(or_conn
);
299 tor_assert(conn
->state
== OR_CONN_STATE_CONNECTING
);
301 log_debug(LD_HANDSHAKE
,"OR connect() to router at %s:%u finished.",
302 conn
->address
,conn
->port
);
303 control_event_bootstrap(BOOTSTRAP_STATUS_HANDSHAKE
, 0);
305 proxy_type
= PROXY_NONE
;
307 if (get_options()->HttpsProxy
)
308 proxy_type
= PROXY_CONNECT
;
309 else if (get_options()->Socks4Proxy
)
310 proxy_type
= PROXY_SOCKS4
;
311 else if (get_options()->Socks5Proxy
)
312 proxy_type
= PROXY_SOCKS5
;
314 if (proxy_type
!= PROXY_NONE
) {
315 /* start proxy handshake */
316 if (connection_proxy_connect(conn
, proxy_type
) < 0) {
317 connection_mark_for_close(conn
);
321 connection_start_reading(conn
);
322 conn
->state
= OR_CONN_STATE_PROXY_HANDSHAKING
;
326 if (connection_tls_start_handshake(or_conn
, 0) < 0) {
327 /* TLS handshaking error of some kind. */
328 connection_mark_for_close(conn
);
334 /** Return 1 if identity digest <b>id_digest</b> is known to be a
335 * currently or recently running relay. Otherwise return 0. */
337 connection_or_digest_is_known_relay(const char *id_digest
)
339 if (router_get_consensus_status_by_id(id_digest
))
340 return 1; /* It's in the consensus: "yes" */
341 if (router_get_by_digest(id_digest
))
342 return 1; /* Not in the consensus, but we have a descriptor for
343 * it. Probably it was in a recent consensus. "Yes". */
347 /** If we don't necessarily know the router we're connecting to, but we
348 * have an addr/port/id_digest, then fill in as much as we can. Start
349 * by checking to see if this describes a router we know. */
351 connection_or_init_conn_from_address(or_connection_t
*conn
,
352 const tor_addr_t
*addr
, uint16_t port
,
353 const char *id_digest
,
356 or_options_t
*options
= get_options();
357 int rate
, burst
; /* per-connection rate limiting params */
358 routerinfo_t
*r
= router_get_by_digest(id_digest
);
359 connection_or_set_identity_digest(conn
, id_digest
);
361 if (connection_or_digest_is_known_relay(id_digest
)) {
362 /* It's in the consensus, or we have a descriptor for it meaning it
363 * was probably in a recent consensus. It's a recognized relay:
364 * give it full bandwidth. */
365 rate
= (int)options
->BandwidthRate
;
366 burst
= (int)options
->BandwidthBurst
;
368 /* Not a recognized relay. Squeeze it down based on the suggested
369 * bandwidth parameters in the consensus, but allow local config
370 * options to override. */
371 rate
= options
->PerConnBWRate
? (int)options
->PerConnBWRate
:
372 (int)networkstatus_get_param(NULL
, "bwconnrate",
373 (int)options
->BandwidthRate
);
374 burst
= options
->PerConnBWBurst
? (int)options
->PerConnBWBurst
:
375 (int)networkstatus_get_param(NULL
, "bwconnburst",
376 (int)options
->BandwidthBurst
);
379 conn
->bandwidthrate
= rate
;
380 conn
->read_bucket
= conn
->write_bucket
= conn
->bandwidthburst
= burst
;
382 conn
->_base
.port
= port
;
383 tor_addr_copy(&conn
->_base
.addr
, addr
);
384 tor_addr_copy(&conn
->real_addr
, addr
);
386 /* XXXX proposal 118 will make this more complex. */
387 if (tor_addr_eq_ipv4h(&conn
->_base
.addr
, r
->addr
))
388 conn
->is_canonical
= 1;
390 /* Override the addr/port, so our log messages will make sense.
391 * This is dangerous, since if we ever try looking up a conn by
392 * its actual addr/port, we won't remember. Careful! */
393 /* XXXX arma: this is stupid, and it's the reason we need real_addr
394 * to track is_canonical properly. What requires it? */
395 /* XXXX <arma> i believe the reason we did this, originally, is because
396 * we wanted to log what OR a connection was to, and if we logged the
397 * right IP address and port 56244, that wouldn't be as helpful. now we
398 * log the "right" port too, so we know if it's moria1 or moria2.
400 tor_addr_from_ipv4h(&conn
->_base
.addr
, r
->addr
);
401 conn
->_base
.port
= r
->or_port
;
403 conn
->nickname
= tor_strdup(r
->nickname
);
404 tor_free(conn
->_base
.address
);
405 conn
->_base
.address
= tor_strdup(r
->address
);
408 /* If we're an authoritative directory server, we may know a
409 * nickname for this router. */
410 n
= dirserv_get_nickname_by_digest(id_digest
);
412 conn
->nickname
= tor_strdup(n
);
414 conn
->nickname
= tor_malloc(HEX_DIGEST_LEN
+2);
415 conn
->nickname
[0] = '$';
416 base16_encode(conn
->nickname
+1, HEX_DIGEST_LEN
+1,
417 conn
->identity_digest
, DIGEST_LEN
);
419 tor_free(conn
->_base
.address
);
420 conn
->_base
.address
= tor_dup_addr(addr
);
424 /** Return true iff <b>a</b> is "better" than <b>b</b> for new circuits.
426 * A more canonical connection is always better than a less canonical
427 * connection. That aside, a connection is better if it has circuits and the
428 * other does not, or if it was created more recently.
430 * Requires that both input connections are open; not is_bad_for_new_circs,
431 * and not impossibly non-canonical.
433 * If </b>forgive_new_connections</b> is true, then we do not call
434 * <b>a</b>better than <b>b</b> simply because b has no circuits,
435 * unless b is also relatively old.
438 connection_or_is_better(time_t now
,
439 const or_connection_t
*a
,
440 const or_connection_t
*b
,
441 int forgive_new_connections
)
444 /** Do not definitively deprecate a new connection with no circuits on it
445 * until this much time has passed. */
446 #define NEW_CONN_GRACE_PERIOD (15*60)
448 if (b
->is_canonical
&& !a
->is_canonical
)
449 return 0; /* A canonical connection is better than a non-canonical
450 * one, no matter how new it is or which has circuits. */
452 newer
= b
->_base
.timestamp_created
< a
->_base
.timestamp_created
;
455 /* We prefer canonical connections regardless of newness. */
456 (!b
->is_canonical
&& a
->is_canonical
) ||
457 /* If both have circuits we prefer the newer: */
458 (b
->n_circuits
&& a
->n_circuits
&& newer
) ||
459 /* If neither has circuits we prefer the newer: */
460 (!b
->n_circuits
&& !a
->n_circuits
&& newer
))
463 /* If one has no circuits and the other does... */
464 if (!b
->n_circuits
&& a
->n_circuits
) {
465 /* Then it's bad, unless it's in its grace period and we're forgiving. */
466 if (forgive_new_connections
&&
467 now
< b
->_base
.timestamp_created
+ NEW_CONN_GRACE_PERIOD
)
476 /** Return the OR connection we should use to extend a circuit to the router
477 * whose identity is <b>digest</b>, and whose address we believe (or have been
478 * told in an extend cell) is <b>target_addr</b>. If there is no good
479 * connection, set *<b>msg_out</b> to a message describing the connection's
480 * state and our next action, and set <b>launch_out</b> to a boolean for
481 * whether we should launch a new connection or not.
484 connection_or_get_for_extend(const char *digest
,
485 const tor_addr_t
*target_addr
,
486 const char **msg_out
,
489 or_connection_t
*conn
, *best
=NULL
;
490 int n_inprogress_goodaddr
= 0, n_old
= 0, n_noncanonical
= 0, n_possible
= 0;
491 time_t now
= approx_time();
494 tor_assert(launch_out
);
496 if (!orconn_identity_map
) {
497 *msg_out
= "Router not connected (nothing is). Connecting.";
502 conn
= digestmap_get(orconn_identity_map
, digest
);
504 for (; conn
; conn
= conn
->next_with_same_id
) {
505 tor_assert(conn
->_base
.magic
== OR_CONNECTION_MAGIC
);
506 tor_assert(conn
->_base
.type
== CONN_TYPE_OR
);
507 tor_assert(!memcmp(conn
->identity_digest
, digest
, DIGEST_LEN
));
508 if (conn
->_base
.marked_for_close
)
510 /* Never return a non-open connection. */
511 if (conn
->_base
.state
!= OR_CONN_STATE_OPEN
) {
512 /* If the address matches, don't launch a new connection for this
514 if (!tor_addr_compare(&conn
->real_addr
, target_addr
, CMP_EXACT
))
515 ++n_inprogress_goodaddr
;
518 /* Never return a connection that shouldn't be used for circs. */
519 if (conn
->is_bad_for_new_circs
) {
523 /* Never return a non-canonical connection using a recent link protocol
524 * if the address is not what we wanted.
526 * (For old link protocols, we can't rely on is_canonical getting
527 * set properly if we're talking to the right address, since we might
528 * have an out-of-date descriptor, and we will get no NETINFO cell to
529 * tell us about the right address.) */
530 if (!conn
->is_canonical
&& conn
->link_proto
>= 2 &&
531 tor_addr_compare(&conn
->real_addr
, target_addr
, CMP_EXACT
)) {
539 best
= conn
; /* If we have no 'best' so far, this one is good enough. */
543 if (connection_or_is_better(now
, conn
, best
, 0))
548 *msg_out
= "Connection is fine; using it.";
551 } else if (n_inprogress_goodaddr
) {
552 *msg_out
= "Connection in progress; waiting.";
555 } else if (n_old
|| n_noncanonical
) {
556 *msg_out
= "Connections all too old, or too non-canonical. "
557 " Launching a new one.";
561 *msg_out
= "Not connected. Connecting.";
567 /** How old do we let a connection to an OR get before deciding it's
568 * too old for new circuits? */
569 #define TIME_BEFORE_OR_CONN_IS_TOO_OLD (60*60*24*7)
571 /** Given the head of the linked list for all the or_connections with a given
572 * identity, set elements of that list as is_bad_for_new_circs() as
573 * appropriate. Helper for connection_or_set_bad_connections().
576 connection_or_group_set_badness(or_connection_t
*head
)
578 or_connection_t
*or_conn
= NULL
, *best
= NULL
;
579 int n_old
= 0, n_inprogress
= 0, n_canonical
= 0, n_other
= 0;
580 time_t now
= time(NULL
);
582 /* Pass 1: expire everything that's old, and see what the status of
583 * everything else is. */
584 for (or_conn
= head
; or_conn
; or_conn
= or_conn
->next_with_same_id
) {
585 if (or_conn
->_base
.marked_for_close
||
586 or_conn
->is_bad_for_new_circs
)
588 if (or_conn
->_base
.timestamp_created
+ TIME_BEFORE_OR_CONN_IS_TOO_OLD
591 "Marking OR conn to %s:%d as too old for new circuits "
592 "(fd %d, %d secs old).",
593 or_conn
->_base
.address
, or_conn
->_base
.port
, or_conn
->_base
.s
,
594 (int)(now
- or_conn
->_base
.timestamp_created
));
595 or_conn
->is_bad_for_new_circs
= 1;
598 if (or_conn
->is_bad_for_new_circs
) {
600 } else if (or_conn
->_base
.state
!= OR_CONN_STATE_OPEN
) {
602 } else if (or_conn
->is_canonical
) {
609 /* Pass 2: We know how about how good the best connection is.
610 * expire everything that's worse, and find the very best if we can. */
611 for (or_conn
= head
; or_conn
; or_conn
= or_conn
->next_with_same_id
) {
612 if (or_conn
->_base
.marked_for_close
||
613 or_conn
->is_bad_for_new_circs
)
614 continue; /* This one doesn't need to be marked bad. */
615 if (or_conn
->_base
.state
!= OR_CONN_STATE_OPEN
)
616 continue; /* Don't mark anything bad until we have seen what happens
617 * when the connection finishes. */
618 if (n_canonical
&& !or_conn
->is_canonical
) {
619 /* We have at least one open canonical connection to this router,
620 * and this one is open but not canonical. Mark it bad. */
622 "Marking OR conn to %s:%d as unsuitable for new circuits: "
623 "(fd %d, %d secs old). It is not canonical, and we have "
624 "another connection to that OR that is.",
625 or_conn
->_base
.address
, or_conn
->_base
.port
, or_conn
->_base
.s
,
626 (int)(now
- or_conn
->_base
.timestamp_created
));
627 or_conn
->is_bad_for_new_circs
= 1;
631 if (!best
|| connection_or_is_better(now
, or_conn
, best
, 0))
638 /* Pass 3: One connection to OR is best. If it's canonical, mark as bad
639 * every other open connection. If it's non-canonical, mark as bad
640 * every other open connection to the same address.
642 * XXXX This isn't optimal; if we have connections to an OR at multiple
643 * addresses, we'd like to pick the best _for each address_, and mark as
644 * bad every open connection that isn't best for its address. But this
645 * can only occur in cases where the other OR is old (so we have no
646 * canonical connection to it), or where all the connections to the OR are
647 * at noncanonical addresses and we have no good direct connection (which
648 * means we aren't at risk of attaching circuits to it anyway). As
649 * 0.1.2.x dies out, the first case will go away, and the second one is
650 * "mostly harmless", so a fix can wait until somebody is bored.
652 for (or_conn
= head
; or_conn
; or_conn
= or_conn
->next_with_same_id
) {
653 if (or_conn
->_base
.marked_for_close
||
654 or_conn
->is_bad_for_new_circs
||
655 or_conn
->_base
.state
!= OR_CONN_STATE_OPEN
)
657 if (or_conn
!= best
&& connection_or_is_better(now
, best
, or_conn
, 1)) {
658 /* This isn't the best conn, _and_ the best conn is better than it,
659 even when we're being forgiving. */
660 if (best
->is_canonical
) {
662 "Marking OR conn to %s:%d as unsuitable for new circuits: "
663 "(fd %d, %d secs old). We have a better canonical one "
664 "(fd %d; %d secs old).",
665 or_conn
->_base
.address
, or_conn
->_base
.port
, or_conn
->_base
.s
,
666 (int)(now
- or_conn
->_base
.timestamp_created
),
667 best
->_base
.s
, (int)(now
- best
->_base
.timestamp_created
));
668 or_conn
->is_bad_for_new_circs
= 1;
669 } else if (!tor_addr_compare(&or_conn
->real_addr
,
670 &best
->real_addr
, CMP_EXACT
)) {
672 "Marking OR conn to %s:%d as unsuitable for new circuits: "
673 "(fd %d, %d secs old). We have a better one with the "
674 "same address (fd %d; %d secs old).",
675 or_conn
->_base
.address
, or_conn
->_base
.port
, or_conn
->_base
.s
,
676 (int)(now
- or_conn
->_base
.timestamp_created
),
677 best
->_base
.s
, (int)(now
- best
->_base
.timestamp_created
));
678 or_conn
->is_bad_for_new_circs
= 1;
684 /** Go through all the OR connections, and set the is_bad_for_new_circs
686 * - all connections that are too old.
687 * - all open non-canonical connections for which a canonical connection
688 * exists to the same router.
689 * - all open canonical connections for which a 'better' canonical
690 * connection exists to the same router.
691 * - all open non-canonical connections for which a 'better' non-canonical
692 * connection exists to the same router at the same address.
694 * See connection_or_is_better() for our idea of what makes one OR connection
695 * better than another.
698 connection_or_set_bad_connections(void)
700 if (!orconn_identity_map
)
703 DIGESTMAP_FOREACH(orconn_identity_map
, identity
, or_connection_t
*, conn
) {
704 connection_or_group_set_badness(conn
);
705 } DIGESTMAP_FOREACH_END
;
708 /** <b>conn</b> is in the 'connecting' state, and it failed to complete
709 * a TCP connection. Send notifications appropriately.
711 * <b>reason</b> specifies the or_conn_end_reason for the failure;
712 * <b>msg</b> specifies the strerror-style error message.
715 connection_or_connect_failed(or_connection_t
*conn
,
716 int reason
, const char *msg
)
718 control_event_or_conn_status(conn
, OR_CONN_EVENT_FAILED
, reason
);
719 if (!authdir_mode_tests_reachability(get_options()))
720 control_event_bootstrap_problem(msg
, reason
);
723 /** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
724 * handshake with an OR with identity digest <b>id_digest</b>.
726 * If <b>id_digest</b> is me, do nothing. If we're already connected to it,
727 * return that connection. If the connect() is in progress, set the
728 * new conn's state to 'connecting' and return it. If connect() succeeds,
729 * call connection_tls_start_handshake() on it.
731 * This function is called from router_retry_connections(), for
732 * ORs connecting to ORs, and circuit_establish_circuit(), for
733 * OPs connecting to ORs.
735 * Return the launched conn, or NULL if it failed.
738 connection_or_connect(const tor_addr_t
*_addr
, uint16_t port
,
739 const char *id_digest
)
741 or_connection_t
*conn
;
742 or_options_t
*options
= get_options();
743 int socket_error
= 0;
748 tor_assert(id_digest
);
749 tor_addr_copy(&addr
, _addr
);
751 if (server_mode(options
) && router_digest_is_me(id_digest
)) {
752 log_info(LD_PROTOCOL
,"Client asked me to connect to myself. Refusing.");
756 conn
= or_connection_new(AF_INET
);
758 /* set up conn so it's got all the data we need to remember */
759 connection_or_init_conn_from_address(conn
, &addr
, port
, id_digest
, 1);
760 conn
->_base
.state
= OR_CONN_STATE_CONNECTING
;
761 control_event_or_conn_status(conn
, OR_CONN_EVENT_LAUNCHED
, 0);
763 /* use a proxy server if available */
764 if (options
->HttpsProxy
) {
766 tor_addr_copy(&addr
, &options
->HttpsProxyAddr
);
767 port
= options
->HttpsProxyPort
;
768 } else if (options
->Socks4Proxy
) {
770 tor_addr_copy(&addr
, &options
->Socks4ProxyAddr
);
771 port
= options
->Socks4ProxyPort
;
772 } else if (options
->Socks5Proxy
) {
774 tor_addr_copy(&addr
, &options
->Socks5ProxyAddr
);
775 port
= options
->Socks5ProxyPort
;
778 switch (connection_connect(TO_CONN(conn
), conn
->_base
.address
,
779 &addr
, port
, &socket_error
)) {
781 /* If the connection failed immediately, and we're using
782 * a proxy, our proxy is down. Don't blame the Tor server. */
784 entry_guard_register_connect_status(conn
->identity_digest
,
786 connection_or_connect_failed(conn
,
787 errno_to_orconn_end_reason(socket_error
),
788 tor_socket_strerror(socket_error
));
789 connection_free(TO_CONN(conn
));
792 connection_watch_events(TO_CONN(conn
), READ_EVENT
| WRITE_EVENT
);
793 /* writable indicates finish, readable indicates broken link,
794 error indicates broken link on windows */
796 /* case 1: fall through */
799 if (connection_or_finished_connecting(conn
) < 0) {
800 /* already marked for close */
806 /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
807 * we initiated the connection, else it's 1.
809 * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and
810 * pass <b>conn</b> to connection_tls_continue_handshake().
812 * Return -1 if <b>conn</b> is broken, else return 0.
815 connection_tls_start_handshake(or_connection_t
*conn
, int receiving
)
817 conn
->_base
.state
= OR_CONN_STATE_TLS_HANDSHAKING
;
818 conn
->tls
= tor_tls_new(conn
->_base
.s
, receiving
);
819 tor_tls_set_logged_address(conn
->tls
, // XXX client and relay?
820 escaped_safe_str(conn
->_base
.address
));
822 log_warn(LD_BUG
,"tor_tls_new failed. Closing.");
825 connection_start_reading(TO_CONN(conn
));
826 log_debug(LD_HANDSHAKE
,"starting TLS handshake on fd %d", conn
->_base
.s
);
827 note_crypto_pk_op(receiving
? TLS_HANDSHAKE_S
: TLS_HANDSHAKE_C
);
829 if (connection_tls_continue_handshake(conn
) < 0) {
835 /** Invoked on the server side from inside tor_tls_read() when the server
836 * gets a successful TLS renegotiation from the client. */
838 connection_or_tls_renegotiated_cb(tor_tls_t
*tls
, void *_conn
)
840 or_connection_t
*conn
= _conn
;
843 /* Don't invoke this again. */
844 tor_tls_set_renegotiate_callback(tls
, NULL
, NULL
);
845 tor_tls_block_renegotiation(tls
);
847 if (connection_tls_finish_handshake(conn
) < 0) {
848 /* XXXX_TLS double-check that it's ok to do this from inside read. */
849 /* XXXX_TLS double-check that this verifies certificates. */
850 connection_mark_for_close(TO_CONN(conn
));
854 /** Move forward with the tls handshake. If it finishes, hand
855 * <b>conn</b> to connection_tls_finish_handshake().
857 * Return -1 if <b>conn</b> is broken, else return 0.
860 connection_tls_continue_handshake(or_connection_t
*conn
)
863 check_no_tls_errors();
865 if (conn
->_base
.state
== OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING
) {
866 // log_notice(LD_OR, "Renegotiate with %p", conn->tls);
867 result
= tor_tls_renegotiate(conn
->tls
);
868 // log_notice(LD_OR, "Result: %d", result);
870 tor_assert(conn
->_base
.state
== OR_CONN_STATE_TLS_HANDSHAKING
);
871 // log_notice(LD_OR, "Continue handshake with %p", conn->tls);
872 result
= tor_tls_handshake(conn
->tls
);
873 // log_notice(LD_OR, "Result: %d", result);
876 CASE_TOR_TLS_ERROR_ANY
:
877 log_info(LD_OR
,"tls error [%s]. breaking connection.",
878 tor_tls_err_to_string(result
));
881 if (! tor_tls_used_v1_handshake(conn
->tls
)) {
882 if (!tor_tls_is_server(conn
->tls
)) {
883 if (conn
->_base
.state
== OR_CONN_STATE_TLS_HANDSHAKING
) {
884 // log_notice(LD_OR,"Done. state was TLS_HANDSHAKING.");
885 conn
->_base
.state
= OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING
;
888 // log_notice(LD_OR,"Done. state was %d.", conn->_base.state);
890 /* improved handshake, but not a client. */
891 tor_tls_set_renegotiate_callback(conn
->tls
,
892 connection_or_tls_renegotiated_cb
,
894 conn
->_base
.state
= OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
;
895 connection_stop_writing(TO_CONN(conn
));
896 connection_start_reading(TO_CONN(conn
));
900 return connection_tls_finish_handshake(conn
);
901 case TOR_TLS_WANTWRITE
:
902 connection_start_writing(TO_CONN(conn
));
903 log_debug(LD_OR
,"wanted write");
905 case TOR_TLS_WANTREAD
: /* handshaking conns are *always* reading */
906 log_debug(LD_OR
,"wanted read");
909 log_info(LD_OR
,"tls closed. breaking connection.");
915 /** Return 1 if we initiated this connection, or 0 if it started
916 * out as an incoming connection.
919 connection_or_nonopen_was_started_here(or_connection_t
*conn
)
921 tor_assert(conn
->_base
.type
== CONN_TYPE_OR
);
923 return 1; /* it's still in proxy states or something */
924 if (conn
->handshake_state
)
925 return conn
->handshake_state
->started_here
;
926 return !tor_tls_is_server(conn
->tls
);
929 /** <b>Conn</b> just completed its handshake. Return 0 if all is well, and
930 * return -1 if he is lying, broken, or otherwise something is wrong.
932 * If we initiated this connection (<b>started_here</b> is true), make sure
933 * the other side sent a correctly formed certificate. If I initiated the
934 * connection, make sure it's the right guy.
936 * Otherwise (if we _didn't_ initiate this connection), it's okay for
937 * the certificate to be weird or absent.
939 * If we return 0, and the certificate is as expected, write a hash of the
940 * identity key into <b>digest_rcvd_out</b>, which must have DIGEST_LEN
942 * If the certificate is invalid or missing on an incoming connection,
943 * we return 0 and set <b>digest_rcvd_out</b> to DIGEST_LEN NUL bytes.
944 * (If we return -1, the contents of this buffer are undefined.)
947 * 1) Set conn->circ_id_type according to tor-spec.txt.
948 * 2) If we're an authdirserver and we initiated the connection: drop all
949 * descriptors that claim to be on that IP/port but that aren't
950 * this guy; and note that this guy is reachable.
951 * 3) If this is a bridge and we didn't configure its identity
952 * fingerprint, remember the keyid we just learned.
955 connection_or_check_valid_tls_handshake(or_connection_t
*conn
,
957 char *digest_rcvd_out
)
959 crypto_pk_env_t
*identity_rcvd
=NULL
;
960 or_options_t
*options
= get_options();
961 int severity
= server_mode(options
) ? LOG_PROTOCOL_WARN
: LOG_WARN
;
962 const char *safe_address
=
963 started_here
? conn
->_base
.address
:
964 safe_str_client(conn
->_base
.address
);
965 const char *conn_type
= started_here
? "outgoing" : "incoming";
966 int has_cert
= 0, has_identity
=0;
968 check_no_tls_errors();
969 has_cert
= tor_tls_peer_has_cert(conn
->tls
);
970 if (started_here
&& !has_cert
) {
971 log_info(LD_HANDSHAKE
,"Tried connecting to router at %s:%d, but it didn't "
972 "send a cert! Closing.",
973 safe_address
, conn
->_base
.port
);
975 } else if (!has_cert
) {
976 log_debug(LD_HANDSHAKE
,"Got incoming connection with no certificate. "
979 check_no_tls_errors();
982 int v
= tor_tls_verify(started_here
?severity
:LOG_INFO
,
983 conn
->tls
, &identity_rcvd
);
984 if (started_here
&& v
<0) {
985 log_fn(severity
,LD_HANDSHAKE
,"Tried connecting to router at %s:%d: It"
986 " has a cert but it's invalid. Closing.",
987 safe_address
, conn
->_base
.port
);
990 log_info(LD_HANDSHAKE
,"Incoming connection gave us an invalid cert "
993 log_debug(LD_HANDSHAKE
,
994 "The certificate seems to be valid on %s connection "
995 "with %s:%d", conn_type
, safe_address
, conn
->_base
.port
);
997 check_no_tls_errors();
1000 if (identity_rcvd
) {
1002 crypto_pk_get_digest(identity_rcvd
, digest_rcvd_out
);
1003 if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd
)<0) {
1004 conn
->circ_id_type
= CIRC_ID_TYPE_LOWER
;
1006 conn
->circ_id_type
= CIRC_ID_TYPE_HIGHER
;
1008 crypto_free_pk_env(identity_rcvd
);
1010 memset(digest_rcvd_out
, 0, DIGEST_LEN
);
1011 conn
->circ_id_type
= CIRC_ID_TYPE_NEITHER
;
1014 if (started_here
&& tor_digest_is_zero(conn
->identity_digest
)) {
1015 connection_or_set_identity_digest(conn
, digest_rcvd_out
);
1016 tor_free(conn
->nickname
);
1017 conn
->nickname
= tor_malloc(HEX_DIGEST_LEN
+2);
1018 conn
->nickname
[0] = '$';
1019 base16_encode(conn
->nickname
+1, HEX_DIGEST_LEN
+1,
1020 conn
->identity_digest
, DIGEST_LEN
);
1021 log_info(LD_HANDSHAKE
, "Connected to router %s at %s:%d without knowing "
1022 "its key. Hoping for the best.",
1023 conn
->nickname
, conn
->_base
.address
, conn
->_base
.port
);
1024 /* if it's a bridge and we didn't know its identity fingerprint, now
1025 * we do -- remember it for future attempts. */
1026 learned_router_identity(&conn
->_base
.addr
, conn
->_base
.port
,
1031 int as_advertised
= 1;
1032 tor_assert(has_cert
);
1033 tor_assert(has_identity
);
1034 if (memcmp(digest_rcvd_out
, conn
->identity_digest
, DIGEST_LEN
)) {
1035 /* I was aiming for a particular digest. I didn't get it! */
1036 char seen
[HEX_DIGEST_LEN
+1];
1037 char expected
[HEX_DIGEST_LEN
+1];
1038 base16_encode(seen
, sizeof(seen
), digest_rcvd_out
, DIGEST_LEN
);
1039 base16_encode(expected
, sizeof(expected
), conn
->identity_digest
,
1041 log_fn(severity
, LD_HANDSHAKE
,
1042 "Tried connecting to router at %s:%d, but identity key was not "
1043 "as expected: wanted %s but got %s.",
1044 conn
->_base
.address
, conn
->_base
.port
, expected
, seen
);
1045 entry_guard_register_connect_status(conn
->identity_digest
, 0, 1,
1047 control_event_or_conn_status(conn
, OR_CONN_EVENT_FAILED
,
1048 END_OR_CONN_REASON_OR_IDENTITY
);
1049 if (!authdir_mode_tests_reachability(options
))
1050 control_event_bootstrap_problem("foo", END_OR_CONN_REASON_OR_IDENTITY
);
1053 if (authdir_mode_tests_reachability(options
)) {
1054 /* We initiated this connection to address:port. Drop all routers
1055 * with the same address:port and a different key.
1057 dirserv_orconn_tls_done(conn
->_base
.address
, conn
->_base
.port
,
1058 digest_rcvd_out
, as_advertised
);
1066 /** The tls handshake is finished.
1068 * Make sure we are happy with the person we just handshaked with.
1070 * If he initiated the connection, make sure he's not already connected,
1071 * then initialize conn from the information in router.
1073 * If all is successful, call circuit_n_conn_done() to handle events
1074 * that have been pending on the <tls handshake completion. Also set the
1075 * directory to be dirty (only matters if I'm an authdirserver).
1078 connection_tls_finish_handshake(or_connection_t
*conn
)
1080 char digest_rcvd
[DIGEST_LEN
];
1081 int started_here
= connection_or_nonopen_was_started_here(conn
);
1083 log_debug(LD_HANDSHAKE
,"tls handshake with %s done. verifying.",
1084 safe_str_client(conn
->_base
.address
));
1086 directory_set_dirty();
1088 if (connection_or_check_valid_tls_handshake(conn
, started_here
,
1092 circuit_build_times_network_is_live(&circ_times
);
1094 if (tor_tls_used_v1_handshake(conn
->tls
)) {
1095 conn
->link_proto
= 1;
1096 if (!started_here
) {
1097 connection_or_init_conn_from_address(conn
, &conn
->_base
.addr
,
1098 conn
->_base
.port
, digest_rcvd
, 0);
1100 tor_tls_block_renegotiation(conn
->tls
);
1101 return connection_or_set_state_open(conn
);
1103 conn
->_base
.state
= OR_CONN_STATE_OR_HANDSHAKING
;
1104 if (connection_init_or_handshake_state(conn
, started_here
) < 0)
1106 if (!started_here
) {
1107 connection_or_init_conn_from_address(conn
, &conn
->_base
.addr
,
1108 conn
->_base
.port
, digest_rcvd
, 0);
1110 return connection_or_send_versions(conn
);
1114 /** Allocate a new connection handshake state for the connection
1115 * <b>conn</b>. Return 0 on success, -1 on failure. */
1117 connection_init_or_handshake_state(or_connection_t
*conn
, int started_here
)
1119 or_handshake_state_t
*s
;
1120 s
= conn
->handshake_state
= tor_malloc_zero(sizeof(or_handshake_state_t
));
1121 s
->started_here
= started_here
? 1 : 0;
1125 /** Free all storage held by <b>state</b>. */
1127 or_handshake_state_free(or_handshake_state_t
*state
)
1131 memset(state
, 0xBE, sizeof(or_handshake_state_t
));
1135 /** Set <b>conn</b>'s state to OR_CONN_STATE_OPEN, and tell other subsystems
1136 * as appropriate. Called when we are done with all TLS and OR handshaking.
1139 connection_or_set_state_open(or_connection_t
*conn
)
1141 int started_here
= connection_or_nonopen_was_started_here(conn
);
1142 time_t now
= time(NULL
);
1143 conn
->_base
.state
= OR_CONN_STATE_OPEN
;
1144 control_event_or_conn_status(conn
, OR_CONN_EVENT_CONNECTED
, 0);
1147 circuit_build_times_network_is_live(&circ_times
);
1148 rep_hist_note_connect_succeeded(conn
->identity_digest
, now
);
1149 if (entry_guard_register_connect_status(conn
->identity_digest
,
1151 /* Close any circuits pending on this conn. We leave it in state
1152 * 'open' though, because it didn't actually *fail* -- we just
1153 * chose not to use it. (Otherwise
1154 * connection_about_to_close_connection() will call a big pile of
1155 * functions to indicate we shouldn't try it again.) */
1156 log_debug(LD_OR
, "New entry guard was reachable, but closing this "
1157 "connection so we can retry the earlier entry guards.");
1158 circuit_n_conn_done(conn
, 0);
1161 router_set_status(conn
->identity_digest
, 1);
1163 /* only report it to the geoip module if it's not a known router */
1164 if (!router_get_by_digest(conn
->identity_digest
)) {
1165 if (tor_addr_family(&TO_CONN(conn
)->addr
) == AF_INET
) {
1166 /*XXXX IP6 support ipv6 geoip.*/
1167 uint32_t a
= tor_addr_to_ipv4h(&TO_CONN(conn
)->addr
);
1168 geoip_note_client_seen(GEOIP_CLIENT_CONNECT
, a
, now
);
1173 or_handshake_state_free(conn
->handshake_state
);
1174 conn
->handshake_state
= NULL
;
1176 connection_start_reading(TO_CONN(conn
));
1177 circuit_n_conn_done(conn
, 1); /* send the pending creates, if any. */
1182 /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s outbuf.
1183 * For cells that use or affect a circuit, this should only be called by
1184 * connection_or_flush_from_first_active_circuit().
1187 connection_or_write_cell_to_buf(const cell_t
*cell
, or_connection_t
*conn
)
1189 packed_cell_t networkcell
;
1194 cell_pack(&networkcell
, cell
);
1196 connection_write_to_buf(networkcell
.body
, CELL_NETWORK_SIZE
, TO_CONN(conn
));
1198 if (cell
->command
!= CELL_PADDING
)
1199 conn
->timestamp_last_added_nonpadding
= approx_time();
1202 /** Pack a variable-length <b>cell</b> into wire-format, and write it onto
1203 * <b>conn</b>'s outbuf. Right now, this <em>DOES NOT</em> support cells that
1207 connection_or_write_var_cell_to_buf(const var_cell_t
*cell
,
1208 or_connection_t
*conn
)
1210 char hdr
[VAR_CELL_HEADER_SIZE
];
1213 var_cell_pack_header(cell
, hdr
);
1214 connection_write_to_buf(hdr
, sizeof(hdr
), TO_CONN(conn
));
1215 connection_write_to_buf(cell
->payload
, cell
->payload_len
, TO_CONN(conn
));
1216 if (cell
->command
!= CELL_PADDING
)
1217 conn
->timestamp_last_added_nonpadding
= approx_time();
1220 /** See whether there's a variable-length cell waiting on <b>conn</b>'s
1221 * inbuf. Return values as for fetch_var_cell_from_buf(). */
1223 connection_fetch_var_cell_from_buf(or_connection_t
*conn
, var_cell_t
**out
)
1225 return fetch_var_cell_from_buf(conn
->_base
.inbuf
, out
, conn
->link_proto
);
1228 /** Process cells from <b>conn</b>'s inbuf.
1230 * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
1231 * and hand it to command_process_cell().
1236 connection_or_process_cells_from_inbuf(or_connection_t
*conn
)
1238 var_cell_t
*var_cell
;
1242 "%d: starting, inbuf_datalen %d (%d pending in tls object).",
1243 conn
->_base
.s
,(int)buf_datalen(conn
->_base
.inbuf
),
1244 tor_tls_get_pending_bytes(conn
->tls
));
1245 if (connection_fetch_var_cell_from_buf(conn
, &var_cell
)) {
1247 return 0; /* not yet. */
1248 circuit_build_times_network_is_live(&circ_times
);
1249 command_process_var_cell(var_cell
, conn
);
1250 var_cell_free(var_cell
);
1252 char buf
[CELL_NETWORK_SIZE
];
1254 if (buf_datalen(conn
->_base
.inbuf
) < CELL_NETWORK_SIZE
) /* whole response
1256 return 0; /* not yet */
1258 circuit_build_times_network_is_live(&circ_times
);
1259 connection_fetch_from_buf(buf
, CELL_NETWORK_SIZE
, TO_CONN(conn
));
1261 /* retrieve cell info from buf (create the host-order struct from the
1262 * network-order string) */
1263 cell_unpack(&cell
, buf
);
1265 command_process_cell(&cell
, conn
);
1270 /** Write a destroy cell with circ ID <b>circ_id</b> and reason <b>reason</b>
1271 * onto OR connection <b>conn</b>. Don't perform range-checking on reason:
1272 * we may want to propagate reasons from other cells.
1277 connection_or_send_destroy(circid_t circ_id
, or_connection_t
*conn
, int reason
)
1283 memset(&cell
, 0, sizeof(cell_t
));
1284 cell
.circ_id
= circ_id
;
1285 cell
.command
= CELL_DESTROY
;
1286 cell
.payload
[0] = (uint8_t) reason
;
1287 log_debug(LD_OR
,"Sending destroy (circID %d).", circ_id
);
1289 /* XXXX It's possible that under some circumstances, we want the destroy
1290 * to take precedence over other data waiting on the circuit's cell queue.
1293 connection_or_write_cell_to_buf(&cell
, conn
);
1297 /** Array of recognized link protocol versions. */
1298 static const uint16_t or_protocol_versions
[] = { 1, 2 };
1299 /** Number of versions in <b>or_protocol_versions</b>. */
1300 static const int n_or_protocol_versions
=
1301 (int)( sizeof(or_protocol_versions
)/sizeof(uint16_t) );
1303 /** Return true iff <b>v</b> is a link protocol version that this Tor
1304 * implementation believes it can support. */
1306 is_or_protocol_version_known(uint16_t v
)
1309 for (i
= 0; i
< n_or_protocol_versions
; ++i
) {
1310 if (or_protocol_versions
[i
] == v
)
1316 /** Send a VERSIONS cell on <b>conn</b>, telling the other host about the
1317 * link protocol versions that this Tor can support. */
1319 connection_or_send_versions(or_connection_t
*conn
)
1323 tor_assert(conn
->handshake_state
&&
1324 !conn
->handshake_state
->sent_versions_at
);
1325 cell
= var_cell_new(n_or_protocol_versions
* 2);
1326 cell
->command
= CELL_VERSIONS
;
1327 for (i
= 0; i
< n_or_protocol_versions
; ++i
) {
1328 uint16_t v
= or_protocol_versions
[i
];
1329 set_uint16(cell
->payload
+(2*i
), htons(v
));
1332 connection_or_write_var_cell_to_buf(cell
, conn
);
1333 conn
->handshake_state
->sent_versions_at
= time(NULL
);
1335 var_cell_free(cell
);
1339 /** Send a NETINFO cell on <b>conn</b>, telling the other server what we know
1340 * about their address, our address, and the current time. */
1342 connection_or_send_netinfo(or_connection_t
*conn
)
1345 time_t now
= time(NULL
);
1350 memset(&cell
, 0, sizeof(cell_t
));
1351 cell
.command
= CELL_NETINFO
;
1354 set_uint32(cell
.payload
, htonl((uint32_t)now
));
1356 /* Their address. */
1357 out
= cell
.payload
+ 4;
1358 len
= append_address_to_payload(out
, &conn
->_base
.addr
);
1364 if ((me
= router_get_my_routerinfo())) {
1366 *out
++ = 1; /* only one address is supported. */
1368 tor_addr_from_ipv4h(&my_addr
, me
->addr
);
1369 len
= append_address_to_payload(out
, &my_addr
);
1377 connection_or_write_cell_to_buf(&cell
, conn
);