[or-cvs] refactor so connection_write_to_buf() never fails

Roger Dingledine arma at seul.org
Sat Oct 4 02:38:20 UTC 2003


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/home2/arma/work/onion/cvs/src/or

Modified Files:
	circuit.c connection.c connection_edge.c connection_or.c 
	cpuworker.c directory.c dns.c main.c onion.c or.h 
Log Message:
refactor so connection_write_to_buf() never fails


Index: circuit.c
===================================================================
RCS file: /home/or/cvsroot/src/or/circuit.c,v
retrieving revision 1.72
retrieving revision 1.73
diff -u -d -r1.72 -r1.73
--- circuit.c	2 Oct 2003 20:00:38 -0000	1.72
+++ circuit.c	4 Oct 2003 02:38:17 -0000	1.73
@@ -267,7 +267,8 @@
   }
 
   log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
-  return connection_write_cell_to_buf(cell, conn);
+  connection_write_cell_to_buf(cell, conn);
+  return 0;
 }
 
 int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
@@ -737,9 +738,7 @@
       return -1;
     }
 
-    if(connection_write_cell_to_buf(&cell, circ->n_conn) < 0) {
-      return -1;
-    }
+    connection_write_cell_to_buf(&cell, circ->n_conn);
 
     circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
     circ->state = CIRCUIT_STATE_BUILDING;
@@ -843,10 +842,7 @@
 
   memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6, DH_ONIONSKIN_LEN);
 
-  if(connection_write_cell_to_buf(&newcell, circ->n_conn) < 0) {
-    return -1;
-  }
-
+  connection_write_cell_to_buf(&newcell, circ->n_conn);
   return 0;
 }
 

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.113
retrieving revision 1.114
diff -u -d -r1.113 -r1.114
--- connection.c	30 Sep 2003 21:27:16 -0000	1.113
+++ connection.c	4 Oct 2003 02:38:17 -0000	1.114
@@ -489,13 +489,10 @@
   return 0;
 }
 
-int connection_write_to_buf(const char *string, int len, connection_t *conn) {
-
-  if(!len)
-    return 0;
+void connection_write_to_buf(const char *string, int len, connection_t *conn) {
 
-  if(conn->marked_for_close)
-    return 0;
+  if(!len || conn->marked_for_close)
+    return;
 
   if( (!connection_speaks_cells(conn)) ||
       (!connection_state_is_open(conn)) ||
@@ -506,7 +503,10 @@
     conn->outbuf_flushlen += len;
   }
 
-  return write_to_buf(string, len, conn->outbuf);
+  if(write_to_buf(string, len, conn->outbuf) < 0) {
+    log_fn(LOG_WARNING,"write_to_buf failed. Closing connection (fd %d).", conn->s);
+    conn->marked_for_close = 1;
+  }
 }
 
 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port) {
@@ -653,7 +653,8 @@
   cell.aci = aci;
   cell.command = CELL_DESTROY;
   log_fn(LOG_INFO,"Sending destroy (aci %d).",aci);
-  return connection_write_cell_to_buf(&cell, conn);
+  connection_write_cell_to_buf(&cell, conn);
+  return 0;
 }
 
 int connection_process_inbuf(connection_t *conn) {

Index: connection_edge.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_edge.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- connection_edge.c	4 Oct 2003 01:37:01 -0000	1.30
+++ connection_edge.c	4 Oct 2003 02:38:17 -0000	1.31
@@ -171,13 +171,9 @@
 
 //      printf("New text for buf (%d bytes): '%s'", cell->length - RELAY_HEADER_SIZE, cell->payload + RELAY_HEADER_SIZE);
       stats_n_data_bytes_received += (cell->length - RELAY_HEADER_SIZE);
-      if(connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
-                                 cell->length - RELAY_HEADER_SIZE, conn) < 0) {
-/*ENDCLOSE*/    conn->marked_for_close = 1;
-        return 0;
-      }
-      if(connection_consider_sending_sendme(conn, edge_type) < 0)
-/*ENDCLOSE*/    conn->marked_for_close = 1;
+      connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
+                             cell->length - RELAY_HEADER_SIZE, conn);
+      connection_consider_sending_sendme(conn, edge_type);
       return 0;
     case RELAY_COMMAND_END:
       if(!conn) {
@@ -302,7 +298,8 @@
     case AP_CONN_STATE_OPEN:
     case EXIT_CONN_STATE_OPEN:
       connection_stop_writing(conn);
-      return connection_consider_sending_sendme(conn, conn->type);
+      connection_consider_sending_sendme(conn, conn->type);
+      return 0;
     case AP_CONN_STATE_SOCKS_WAIT:
       connection_stop_writing(conn);
       return 0;
@@ -403,18 +400,18 @@
   goto repeat_connection_package_raw_inbuf;
 }
 
-int connection_consider_sending_sendme(connection_t *conn, int edge_type) {
+void connection_consider_sending_sendme(connection_t *conn, int edge_type) {
   circuit_t *circ;
   cell_t cell;
  
   if(connection_outbuf_too_full(conn))
-    return 0;
+    return;
  
   circ = circuit_get_by_conn(conn);
   if(!circ) {
     /* this can legitimately happen if the destroy has already arrived and torn down the circuit */
     log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
-    return 0;
+    return;
   }
  
   memset(&cell, 0, sizeof(cell_t));
@@ -434,11 +431,9 @@
     if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION(edge_type), conn->cpath_layer) < 0) {
       log_fn(LOG_WARNING,"circuit_deliver_relay_cell failed. Closing.");
       circuit_close(circ);
-      return 0;
+      return;
     }
   }
- 
-  return 0;
 }
 
 static int connection_ap_handshake_process_socks(connection_t *conn) {

Index: connection_or.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_or.c,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -d -r1.61 -r1.62
--- connection_or.c	1 Oct 2003 01:49:53 -0000	1.61
+++ connection_or.c	4 Oct 2003 02:38:17 -0000	1.62
@@ -248,13 +248,13 @@
 
 /* ********************************** */
 
-int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn) {
+void connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn) {
   char networkcell[CELL_NETWORK_SIZE];
   char *n = networkcell;
 
   cell_pack(n, cellp);
  
-  return connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
+  connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
 }
 
 /* if there's a whole cell there, pull it off and process it. */

Index: cpuworker.c
===================================================================
RCS file: /home/or/cvsroot/src/or/cpuworker.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- cpuworker.c	27 Sep 2003 21:09:56 -0000	1.9
+++ cpuworker.c	4 Oct 2003 02:38:17 -0000	1.10
@@ -267,13 +267,9 @@
     cpuworker->state = CPUWORKER_STATE_BUSY_ONION;
     num_cpuworkers_busy++;
 
-    if(connection_write_to_buf(&question_type, 1, cpuworker) < 0 ||
-       connection_write_to_buf(tag, sizeof(tag), cpuworker) < 0 ||
-       connection_write_to_buf(circ->onionskin, DH_ONIONSKIN_LEN, cpuworker) < 0) {
-      log_fn(LOG_WARNING,"Write failed. Closing worker and failing circ.");
-      cpuworker->marked_for_close = 1;
-      return -1;
-    }
+    connection_write_to_buf(&question_type, 1, cpuworker);
+    connection_write_to_buf(tag, sizeof(tag), cpuworker);
+    connection_write_to_buf(circ->onionskin, DH_ONIONSKIN_LEN, cpuworker);
   }
   return 0;    
 }

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/src/or/directory.c,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -d -r1.41 -r1.42
--- directory.c	30 Sep 2003 21:27:16 -0000	1.41
+++ directory.c	4 Oct 2003 02:38:17 -0000	1.42
@@ -90,10 +90,7 @@
 
   switch(command) {
     case DIR_CONN_STATE_CONNECTING_FETCH:
-      if(connection_write_to_buf(fetchstring, strlen(fetchstring), conn) < 0) {
-        log_fn(LOG_WARNING,"Couldn't write fetch to buffer.");
-        return -1;
-      }
+      connection_write_to_buf(fetchstring, strlen(fetchstring), conn);
       conn->state = DIR_CONN_STATE_CLIENT_SENDING_FETCH;
       break;
     case DIR_CONN_STATE_CONNECTING_UPLOAD:
@@ -104,10 +101,7 @@
       }
       snprintf(tmp, sizeof(tmp), "POST / HTTP/1.0\r\nContent-Length: %d\r\n\r\n%s",
                strlen(s), s);
-      if(connection_write_to_buf(tmp, strlen(tmp), conn) < 0) {
-        log_fn(LOG_WARNING,"Couldn't write post/descriptor to buffer.");
-        return -1;
-      }
+      connection_write_to_buf(tmp, strlen(tmp), conn);
       conn->state = DIR_CONN_STATE_CLIENT_SENDING_UPLOAD;
       break;
   }
@@ -200,11 +194,8 @@
     }
 
     log_fn(LOG_DEBUG,"Dumping directory to client."); 
-    if((connection_write_to_buf(answerstring, strlen(answerstring), conn) < 0) ||
-       (connection_write_to_buf(cp, dlen, conn) < 0)) {
-      log_fn(LOG_WARNING,"Failed to write answerstring+directory to outbuf.");
-      return -1;
-    }
+    connection_write_to_buf(answerstring, strlen(answerstring), conn);
+    connection_write_to_buf(cp, dlen, conn);
     conn->state = DIR_CONN_STATE_SERVER_WRITING;
     return 0;
   }
@@ -218,10 +209,7 @@
       return -1; /* XXX should write an http failed code */
     }
     dirserv_get_directory(&cp); /* rebuild and write to disk */
-    if(connection_write_to_buf(answerstring, strlen(answerstring), conn) < 0) {
-      log_fn(LOG_WARNING,"Failed to write answerstring to outbuf.");
-      return -1;
-    }
+    connection_write_to_buf(answerstring, strlen(answerstring), conn);
     conn->state = DIR_CONN_STATE_SERVER_WRITING;
     return 0;
   }

Index: dns.c
===================================================================
RCS file: /home/or/cvsroot/src/or/dns.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -d -r1.29 -r1.30
--- dns.c	27 Sep 2003 21:09:56 -0000	1.29
+++ dns.c	4 Oct 2003 02:38:17 -0000	1.30
@@ -170,14 +170,8 @@
   num_dnsworkers_busy++;
 
   len = strlen(dnsconn->address);
-  /* FFFF we should have it retry if the first worker bombs out */
-  if(connection_write_to_buf(&len, 1, dnsconn) < 0 ||
-     connection_write_to_buf(dnsconn->address, len, dnsconn) < 0) {
-    log_fn(LOG_WARNING,"Write failed. Closing worker and failing resolve.");
-    dnsconn->marked_for_close = 1;
-    dns_cancel_pending_resolve(exitconn->address, NULL);
-    return -1;
-  }
+  connection_write_to_buf(&len, 1, dnsconn);
+  connection_write_to_buf(dnsconn->address, len, dnsconn);
 
 //  log_fn(LOG_DEBUG,"submitted '%s'", exitconn->address);
   return 0;

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.123
retrieving revision 1.124
diff -u -d -r1.123 -r1.124
--- main.c	4 Oct 2003 01:37:01 -0000	1.123
+++ main.c	4 Oct 2003 02:38:17 -0000	1.124
@@ -345,8 +345,7 @@
               conn->address, conn->port);
           memset(&cell,0,sizeof(cell_t));
           cell.command = CELL_PADDING;
-          if(connection_write_cell_to_buf(&cell, conn) < 0)
-            conn->marked_for_close = 1;
+          connection_write_cell_to_buf(&cell, conn);
         }
       }
     }

Index: onion.c
===================================================================
RCS file: /home/or/cvsroot/src/or/onion.c,v
retrieving revision 1.69
retrieving revision 1.70
diff -u -d -r1.69 -r1.70
--- onion.c	1 Oct 2003 01:49:53 -0000	1.69
+++ onion.c	4 Oct 2003 02:38:17 -0000	1.70
@@ -151,9 +151,7 @@
     return -1;
   }
 
-  if(connection_write_cell_to_buf(&cell, circ->p_conn) < 0) {
-    return -1;
-  }
+  connection_write_cell_to_buf(&cell, circ->p_conn);
   log_fn(LOG_DEBUG,"Finished sending 'created' cell.");
 
   return 0;

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.155
retrieving revision 1.156
diff -u -d -r1.155 -r1.156
--- or.h	4 Oct 2003 01:37:01 -0000	1.155
+++ or.h	4 Oct 2003 02:38:18 -0000	1.156
@@ -550,7 +550,7 @@
 int connection_outbuf_too_full(connection_t *conn);
 int connection_flush_buf(connection_t *conn);
 int connection_handle_write(connection_t *conn);
-int connection_write_to_buf(const char *string, int len, connection_t *conn);
+void connection_write_to_buf(const char *string, int len, connection_t *conn);
 
 connection_t *connection_twin_get_by_addr_port(uint32_t addr, uint16_t port);
 connection_t *connection_exact_get_by_addr_port(uint32_t addr, uint16_t port);
@@ -581,7 +581,7 @@
 int connection_edge_finished_flushing(connection_t *conn);
 
 int connection_package_raw_inbuf(connection_t *conn);
-int connection_consider_sending_sendme(connection_t *conn, int edge_type);
+void connection_consider_sending_sendme(connection_t *conn, int edge_type);
 
 int connection_exit_connect(connection_t *conn);
 
@@ -601,7 +601,7 @@
 int connection_tls_start_handshake(connection_t *conn, int receiving);
 int connection_tls_continue_handshake(connection_t *conn);
 
-int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
+void connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn);
 int connection_process_cell_from_inbuf(connection_t *conn);
 
 /********************************* cpuworker.c *****************************/



More information about the tor-commits mailing list