[tor-commits] [tor/master] Run Coccinelle for control.c refactor

dgoulet at torproject.org dgoulet at torproject.org
Fri May 3 14:57:34 UTC 2019


commit 983452e2215d27033ed91012a53c1b9ad3dd4a34
Author: Taylor Yu <catalyst at torproject.org>
Date:   Wed Apr 10 17:04:09 2019 -0500

    Run Coccinelle for control.c refactor
---
 src/feature/control/control.c         |   8 +-
 src/feature/control/control_auth.c    |  38 ++++----
 src/feature/control/control_cmd.c     | 166 ++++++++++++++++------------------
 src/feature/control/control_getinfo.c |  13 +--
 src/feature/control/control_proto.c   |   2 +-
 5 files changed, 106 insertions(+), 121 deletions(-)

diff --git a/src/feature/control/control.c b/src/feature/control/control.c
index 1f85bd3fc..436bf423c 100644
--- a/src/feature/control/control.c
+++ b/src/feature/control/control.c
@@ -401,7 +401,7 @@ connection_control_process_inbuf(control_connection_t *conn)
         return 0;
       else if (r == -1) {
         if (data_len + conn->incoming_cmd_cur_len > MAX_COMMAND_LINE_LENGTH) {
-          connection_write_str_to_buf("500 Line too long.\r\n", conn);
+          control_write_endreply(conn, 500, "Line too long.");
           connection_stop_reading(TO_CONN(conn));
           connection_mark_and_flush(TO_CONN(conn));
         }
@@ -455,20 +455,20 @@ connection_control_process_inbuf(control_connection_t *conn)
 
   /* Otherwise, Quit is always valid. */
   if (!strcasecmp(conn->current_cmd, "QUIT")) {
-    connection_write_str_to_buf("250 closing connection\r\n", conn);
+    control_write_endreply(conn, 250, "closing connection");
     connection_mark_and_flush(TO_CONN(conn));
     return 0;
   }
 
   if (conn->base_.state == CONTROL_CONN_STATE_NEEDAUTH &&
       !is_valid_initial_command(conn, conn->current_cmd)) {
-    connection_write_str_to_buf("514 Authentication required.\r\n", conn);
+    control_write_endreply(conn, 514, "Authentication required.");
     connection_mark_for_close(TO_CONN(conn));
     return 0;
   }
 
   if (data_len >= UINT32_MAX) {
-    connection_write_str_to_buf("500 A 4GB command? Nice try.\r\n", conn);
+    control_write_endreply(conn, 500, "A 4GB command? Nice try.");
     connection_mark_for_close(TO_CONN(conn));
     return 0;
   }
diff --git a/src/feature/control/control_auth.c b/src/feature/control/control_auth.c
index bdabdc989..51c8da1b9 100644
--- a/src/feature/control/control_auth.c
+++ b/src/feature/control/control_auth.c
@@ -141,27 +141,26 @@ handle_control_authchallenge(control_connection_t *conn,
   char server_nonce_encoded[(2*SAFECOOKIE_SERVER_NONCE_LEN) + 1];
 
   if (strcasecmp(smartlist_get(args->args, 0), "SAFECOOKIE")) {
-    connection_write_str_to_buf("513 AUTHCHALLENGE only supports SAFECOOKIE "
-                                "authentication\r\n", conn);
+    control_write_endreply(conn, 513,
+                           "AUTHCHALLENGE only supports SAFECOOKIE " "authentication");
     goto fail;
   }
   if (!authentication_cookie_is_set) {
-    connection_write_str_to_buf("515 Cookie authentication is disabled\r\n",
-                                conn);
+    control_write_endreply(conn, 515, "Cookie authentication is disabled");
     goto fail;
   }
   if (args->kwargs == NULL || args->kwargs->next != NULL) {
     /*    connection_write_str_to_buf("512 AUTHCHALLENGE requires exactly "
                                 "2 arguments.\r\n", conn);
     */
-    connection_printf_to_buf(conn,
-                             "512 AUTHCHALLENGE dislikes argument list %s\r\n",
+    control_printf_endreply(conn, 512,
+                             "AUTHCHALLENGE dislikes argument list %s",
                              escaped(args->raw_body));
     goto fail;
   }
   if (strcmp(args->kwargs->key, "")) {
-    connection_write_str_to_buf("512 AUTHCHALLENGE does not accept keyword "
-                                "arguments.\r\n", conn);
+    control_write_endreply(conn, 512,
+                           "AUTHCHALLENGE does not accept keyword " "arguments.");
     goto fail;
   }
 
@@ -177,8 +176,7 @@ handle_control_authchallenge(control_connection_t *conn,
     client_nonce = tor_malloc(client_nonce_len);
     if (base16_decode(client_nonce, client_nonce_len, hex_nonce,
                       strlen(hex_nonce)) != (int)client_nonce_len) {
-      connection_write_str_to_buf("513 Invalid base16 client nonce\r\n",
-                                  conn);
+      control_write_endreply(conn, 513, "Invalid base16 client nonce");
       tor_free(client_nonce);
       goto fail;
     }
@@ -223,9 +221,8 @@ handle_control_authchallenge(control_connection_t *conn,
   base16_encode(server_nonce_encoded, sizeof(server_nonce_encoded),
                 server_nonce, sizeof(server_nonce));
 
-  connection_printf_to_buf(conn,
-                           "250 AUTHCHALLENGE SERVERHASH=%s "
-                           "SERVERNONCE=%s\r\n",
+  control_printf_endreply(conn, 250,
+                           "AUTHCHALLENGE SERVERHASH=%s " "SERVERNONCE=%s",
                            server_hash_encoded,
                            server_nonce_encoded);
 
@@ -263,13 +260,12 @@ handle_control_authenticate(control_connection_t *conn,
     password = tor_strdup("");
     password_len = 0;
   } else if (args->kwargs->next) {
-    connection_write_str_to_buf(
-             "512 Too many arguments to AUTHENTICATE.\r\n", conn);
+    control_write_endreply(conn, 512, "Too many arguments to AUTHENTICATE.");
     connection_mark_for_close(TO_CONN(conn));
     return 0;
   } else if (strcmp(args->kwargs->key, "")) {
-    connection_write_str_to_buf(
-             "512 AUTHENTICATE does not accept keyword arguments.\r\n", conn);
+    control_write_endreply(conn, 512,
+                           "AUTHENTICATE does not accept keyword arguments.");
     connection_mark_for_close(TO_CONN(conn));
     return 0;
   } else if (strchr(args->raw_body, '\"')) {
@@ -282,10 +278,8 @@ handle_control_authenticate(control_connection_t *conn,
     password = tor_malloc(password_len+1);
     if (base16_decode(password, password_len+1, hex_passwd, strlen(hex_passwd))
                       != (int) password_len) {
-      connection_write_str_to_buf(
-            "551 Invalid hexadecimal encoding.  Maybe you tried a plain text "
-            "password?  If so, the standard requires that you put it in "
-            "double quotes.\r\n", conn);
+      control_write_endreply(conn, 551,
+                             "Invalid hexadecimal encoding.  Maybe you tried a plain text " "password?  If so, the standard requires that you put it in " "double quotes.");
       connection_mark_for_close(TO_CONN(conn));
       tor_free(password);
       return 0;
@@ -418,7 +412,7 @@ handle_control_authenticate(control_connection_t *conn,
 
  err:
   tor_free(password);
-  connection_printf_to_buf(conn, "515 Authentication failed: %s\r\n", errstr);
+  control_printf_endreply(conn, 515, "Authentication failed: %s", errstr);
   connection_mark_for_close(TO_CONN(conn));
   if (sl) { /* clean up */
     SMARTLIST_FOREACH(sl, char *, str, tor_free(str));
diff --git a/src/feature/control/control_cmd.c b/src/feature/control/control_cmd.c
index 5aa828ca5..ae4d6584c 100644
--- a/src/feature/control/control_cmd.c
+++ b/src/feature/control/control_cmd.c
@@ -319,11 +319,11 @@ handle_control_getconf(control_connection_t *conn,
 
   if ((len = smartlist_len(unrecognized))) {
     for (i=0; i < len-1; ++i)
-      connection_printf_to_buf(conn,
-                               "552-Unrecognized configuration key \"%s\"\r\n",
+      control_printf_midreply(conn, 552,
+                               "Unrecognized configuration key \"%s\"",
                                (char*)smartlist_get(unrecognized, i));
-    connection_printf_to_buf(conn,
-                             "552 Unrecognized configuration key \"%s\"\r\n",
+    control_printf_endreply(conn, 552,
+                             "Unrecognized configuration key \"%s\"",
                              (char*)smartlist_get(unrecognized, len-1));
   } else if ((len = smartlist_len(answers))) {
     char *tmp = smartlist_get(answers, len-1);
@@ -332,7 +332,7 @@ handle_control_getconf(control_connection_t *conn,
     msg = smartlist_join_strings(answers, "", 0, &msg_len);
     connection_buf_add(msg, msg_len, TO_CONN(conn));
   } else {
-    connection_write_str_to_buf("250 OK\r\n", conn);
+    send_control_done(conn);
   }
 
   SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
@@ -424,7 +424,7 @@ handle_control_setevents(control_connection_t *conn,
         }
 
         if (event_code == -1) {
-          connection_printf_to_buf(conn, "552 Unrecognized event \"%s\"\r\n",
+          control_printf_endreply(conn, 552, "Unrecognized event \"%s\"",
                                    ev);
           return 0;
         }
@@ -455,8 +455,8 @@ handle_control_saveconf(control_connection_t *conn,
   bool force = config_lines_contain_flag(args->kwargs, "FORCE");
   const or_options_t *options = get_options();
   if ((!force && options->IncludeUsed) || options_save_current() < 0) {
-    connection_write_str_to_buf(
-      "551 Unable to write configuration to disk.\r\n", conn);
+    control_write_endreply(conn, 551,
+                           "Unable to write configuration to disk.");
   } else {
     send_control_done(conn);
   }
@@ -489,7 +489,7 @@ handle_control_signal(control_connection_t *conn,
   }
 
   if (sig < 0)
-    connection_printf_to_buf(conn, "552 Unrecognized signal code \"%s\"\r\n",
+    control_printf_endreply(conn, 552, "Unrecognized signal code \"%s\"",
                              s);
   if (sig < 0)
     return 0;
@@ -776,7 +776,7 @@ handle_control_extendcircuit(control_connection_t *conn,
   if (purpose_line) {
     intended_purpose = circuit_purpose_from_string(purpose_line->value);
     if (intended_purpose == CIRCUIT_PURPOSE_UNKNOWN) {
-      connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n",
+      control_printf_endreply(conn, 552, "Unknown purpose \"%s\"",
                                purpose_line->value);
       goto done;
     }
@@ -787,22 +787,22 @@ handle_control_extendcircuit(control_connection_t *conn,
       // "EXTENDCIRCUIT 0" with no path.
       circ = circuit_launch(intended_purpose, CIRCLAUNCH_NEED_CAPACITY);
       if (!circ) {
-        connection_write_str_to_buf("551 Couldn't start circuit\r\n", conn);
+        control_write_endreply(conn, 551, "Couldn't start circuit");
       } else {
-        connection_printf_to_buf(conn, "250 EXTENDED %lu\r\n",
-                  (unsigned long)circ->global_identifier);
+        control_printf_endreply(conn, 250, "EXTENDED %lu",
+                                (unsigned long)circ->global_identifier);
       }
       goto done;
     }
   }
 
   if (!zero_circ && !(circ = get_circ(circ_id))) {
-    connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n", circ_id);
+    control_printf_endreply(conn, 552, "Unknown circuit \"%s\"", circ_id);
     goto done;
   }
 
   if (!path_str) {
-    connection_printf_to_buf(conn, "512 syntax error: path required.\r\n");
+    control_write_endreply(conn, 512, "syntax error: path required.");
     goto done;
   }
 
@@ -813,11 +813,11 @@ handle_control_extendcircuit(control_connection_t *conn,
   SMARTLIST_FOREACH_BEGIN(router_nicknames, const char *, n) {
     const node_t *node = node_get_by_nickname(n, 0);
     if (!node) {
-      connection_printf_to_buf(conn, "552 No such router \"%s\"\r\n", n);
+      control_printf_endreply(conn, 552, "No such router \"%s\"", n);
       goto done;
     }
     if (!node_has_preferred_descriptor(node, first_node)) {
-      connection_printf_to_buf(conn, "552 No descriptor for \"%s\"\r\n", n);
+      control_printf_endreply(conn, 552, "No descriptor for \"%s\"", n);
       goto done;
     }
     smartlist_add(nodes, (void*)node);
@@ -825,7 +825,7 @@ handle_control_extendcircuit(control_connection_t *conn,
   } SMARTLIST_FOREACH_END(n);
 
   if (!smartlist_len(nodes)) {
-    connection_write_str_to_buf("512 No router names provided\r\n", conn);
+    control_write_endreply(conn, 512, "No router names provided");
     goto done;
   }
 
@@ -863,7 +863,7 @@ handle_control_extendcircuit(control_connection_t *conn,
     int err_reason = 0;
     if ((err_reason = circuit_handle_first_hop(circ)) < 0) {
       circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
-      connection_write_str_to_buf("551 Couldn't start circuit\r\n", conn);
+      control_write_endreply(conn, 551, "Couldn't start circuit");
       goto done;
     }
   } else {
@@ -875,13 +875,13 @@ handle_control_extendcircuit(control_connection_t *conn,
         log_info(LD_CONTROL,
                  "send_next_onion_skin failed; circuit marked for closing.");
         circuit_mark_for_close(TO_CIRCUIT(circ), -err_reason);
-        connection_write_str_to_buf("551 Couldn't send onion skin\r\n", conn);
+        control_write_endreply(conn, 551, "Couldn't send onion skin");
         goto done;
       }
     }
   }
 
-  connection_printf_to_buf(conn, "250 EXTENDED %lu\r\n",
+  control_printf_endreply(conn, 250, "EXTENDED %lu",
                              (unsigned long)circ->global_identifier);
   if (zero_circ) /* send a 'launched' event, for completeness */
     circuit_event_status(circ, CIRC_EVENT_LAUNCHED, 0);
@@ -909,26 +909,26 @@ handle_control_setcircuitpurpose(control_connection_t *conn,
   const char *circ_id = smartlist_get(args->args,0);
 
   if (!(circ = get_circ(circ_id))) {
-    connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n", circ_id);
+    control_printf_endreply(conn, 552, "Unknown circuit \"%s\"", circ_id);
     goto done;
   }
 
   {
     const config_line_t *purp = config_line_find_case(args->kwargs, "PURPOSE");
     if (!purp) {
-      connection_write_str_to_buf("552 No purpose given\r\n", conn);
+      control_write_endreply(conn, 552, "No purpose given");
       goto done;
     }
     new_purpose = circuit_purpose_from_string(purp->value);
     if (new_purpose == CIRCUIT_PURPOSE_UNKNOWN) {
-      connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n",
+      control_printf_endreply(conn, 552, "Unknown purpose \"%s\"",
                                purp->value);
       goto done;
     }
   }
 
   circuit_change_purpose(TO_CIRCUIT(circ), new_purpose);
-  connection_write_str_to_buf("250 OK\r\n", conn);
+  send_control_done(conn);
 
  done:
   return 0;
@@ -959,17 +959,17 @@ handle_control_attachstream(control_connection_t *conn,
   const config_line_t *hoparg = config_line_find_case(args->kwargs, "HOP");
 
   if (!(ap_conn = get_stream(stream_id))) {
-    connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n", stream_id);
+    control_printf_endreply(conn, 552, "Unknown stream \"%s\"", stream_id);
     return 0;
   } else if (!zero_circ && !(circ = get_circ(circ_id))) {
-    connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n", circ_id);
+    control_printf_endreply(conn, 552, "Unknown circuit \"%s\"", circ_id);
     return 0;
   } else if (circ) {
     if (hoparg) {
       hop = (int) tor_parse_ulong(hoparg->value, 10, 0, INT_MAX,
                                   &hop_line_ok, NULL);
       if (!hop_line_ok) { /* broken hop line */
-        connection_printf_to_buf(conn, "552 Bad value hop=%s\r\n",
+        control_printf_endreply(conn, 552, "Bad value hop=%s",
                                  hoparg->value);
         return 0;
       }
@@ -979,9 +979,8 @@ handle_control_attachstream(control_connection_t *conn,
   if (ENTRY_TO_CONN(ap_conn)->state != AP_CONN_STATE_CONTROLLER_WAIT &&
       ENTRY_TO_CONN(ap_conn)->state != AP_CONN_STATE_CONNECT_WAIT &&
       ENTRY_TO_CONN(ap_conn)->state != AP_CONN_STATE_RESOLVE_WAIT) {
-    connection_write_str_to_buf(
-                    "555 Connection is not managed by controller.\r\n",
-                    conn);
+    control_write_endreply(conn, 555,
+                           "Connection is not managed by controller.");
     return 0;
   }
 
@@ -1000,15 +999,14 @@ handle_control_attachstream(control_connection_t *conn,
   }
 
   if (circ && (circ->base_.state != CIRCUIT_STATE_OPEN)) {
-    connection_write_str_to_buf(
-                    "551 Can't attach stream to non-open origin circuit\r\n",
-                    conn);
+    control_write_endreply(conn, 551,
+                           "Can't attach stream to non-open origin circuit");
     return 0;
   }
   /* Is this a single hop circuit? */
   if (circ && (circuit_get_cpath_len(circ)<2 || hop==1)) {
-    connection_write_str_to_buf(
-               "551 Can't attach stream to this one-hop circuit.\r\n", conn);
+    control_write_endreply(conn, 551,
+                           "Can't attach stream to this one-hop circuit.");
     return 0;
   }
 
@@ -1016,13 +1014,12 @@ handle_control_attachstream(control_connection_t *conn,
     /* find this hop in the circuit, and set cpath */
     cpath = circuit_get_cpath_hop(circ, hop);
     if (!cpath) {
-      connection_printf_to_buf(conn,
-                               "551 Circuit doesn't have %d hops.\r\n", hop);
+      control_printf_endreply(conn, 551, "Circuit doesn't have %d hops.", hop);
       return 0;
     }
   }
   if (connection_ap_handshake_rewrite_and_attach(ap_conn, circ, cpath) < 0) {
-    connection_write_str_to_buf("551 Unable to attach stream\r\n", conn);
+    control_write_endreply(conn, 551, "Unable to attach stream");
     return 0;
   }
   send_control_done(conn);
@@ -1054,7 +1051,7 @@ handle_control_postdescriptor(control_connection_t *conn,
   line = config_line_find_case(args->kwargs, "purpose");
   if (line) {
     purpose = router_purpose_from_string(line->value);
-    connection_printf_to_buf(conn, "552 Unknown purpose \"%s\"\r\n",
+    control_printf_endreply(conn, 552, "Unknown purpose \"%s\"",
                              line->value);
     goto done;
   }
@@ -1065,7 +1062,7 @@ handle_control_postdescriptor(control_connection_t *conn,
     else if (!strcasecmp(line->value, "yes"))
       cache = 1;
     else {
-      connection_printf_to_buf(conn, "552 Unknown cache request \"%s\"\r\n",
+      control_printf_endreply(conn, 552, "Unknown cache request \"%s\"",
                                line->value);
       goto done;
     }
@@ -1074,11 +1071,11 @@ handle_control_postdescriptor(control_connection_t *conn,
   switch (router_load_single_router(args->cmddata, purpose, cache, &msg)) {
   case -1:
     if (!msg) msg = "Could not parse descriptor";
-    connection_printf_to_buf(conn, "554 %s\r\n", msg);
+    control_write_endreply(conn, 554, msg);
     break;
   case 0:
     if (!msg) msg = "Descriptor not added";
-    connection_printf_to_buf(conn, "251 %s\r\n",msg);
+    control_write_endreply(conn, 251, msg);
     break;
   case 1:
     send_control_done(conn);
@@ -1107,7 +1104,7 @@ handle_control_redirectstream(control_connection_t *conn,
 
   if (!(ap_conn = get_stream(smartlist_get(args, 0)))
            || !ap_conn->socks_request) {
-    connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
+    control_printf_endreply(conn, 552, "Unknown stream \"%s\"",
                              (char*)smartlist_get(args, 0));
   } else {
     int ok = 1;
@@ -1116,7 +1113,7 @@ handle_control_redirectstream(control_connection_t *conn,
                                             10, 1, 65535, &ok, NULL);
     }
     if (!ok) {
-      connection_printf_to_buf(conn, "512 Cannot parse port \"%s\"\r\n",
+      control_printf_endreply(conn, 512, "Cannot parse port \"%s\"",
                                (char*)smartlist_get(args, 2));
     } else {
       new_addr = tor_strdup(smartlist_get(args, 1));
@@ -1155,13 +1152,13 @@ handle_control_closestream(control_connection_t *conn,
   tor_assert(smartlist_len(args) >= 2);
 
   if (!(ap_conn = get_stream(smartlist_get(args, 0))))
-    connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
+    control_printf_endreply(conn, 552, "Unknown stream \"%s\"",
                              (char*)smartlist_get(args, 0));
   else {
     reason = (uint8_t) tor_parse_ulong(smartlist_get(args,1), 10, 0, 255,
                                        &ok, NULL);
     if (!ok) {
-      connection_printf_to_buf(conn, "552 Unrecognized reason \"%s\"\r\n",
+      control_printf_endreply(conn, 552, "Unrecognized reason \"%s\"",
                                (char*)smartlist_get(args, 1));
       ap_conn = NULL;
     }
@@ -1192,7 +1189,7 @@ handle_control_closecircuit(control_connection_t *conn,
   origin_circuit_t *circ = NULL;
 
   if (!(circ=get_circ(circ_id))) {
-    connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n", circ_id);
+    control_printf_endreply(conn, 552, "Unknown circuit \"%s\"", circ_id);
     return 0;
   }
 
@@ -1277,7 +1274,7 @@ handle_control_protocolinfo(control_connection_t *conn,
       }
     });
   if (bad_arg) {
-    connection_printf_to_buf(conn, "513 No such version %s\r\n",
+    control_printf_endreply(conn, 513, "No such version %s",
                              escaped(bad_arg));
     /* Don't tolerate bad arguments when not authenticated. */
     if (!STATE_IS_OPEN(TO_CONN(conn)->state))
@@ -1342,7 +1339,7 @@ handle_control_usefeature(control_connection_t *conn,
       else if (!strcasecmp(arg, "EXTENDED_EVENTS"))
         ;
       else {
-        connection_printf_to_buf(conn, "552 Unrecognized feature \"%s\"\r\n",
+        control_printf_endreply(conn, 552, "Unrecognized feature \"%s\"",
                                  arg);
         bad = 1;
         break;
@@ -1426,7 +1423,7 @@ handle_control_hsfetch(control_connection_t *conn,
     version = HS_VERSION_THREE;
     hs_parse_address(hsaddress, &v3_pk, NULL, NULL);
   } else {
-    connection_printf_to_buf(conn, "513 Invalid argument \"%s\"\r\n",
+    control_printf_endreply(conn, 513, "Invalid argument \"%s\"",
                              arg1);
     goto done;
   }
@@ -1437,7 +1434,7 @@ handle_control_hsfetch(control_connection_t *conn,
 
       const node_t *node = node_get_by_hex_id(server, 0);
       if (!node) {
-        connection_printf_to_buf(conn, "552 Server \"%s\" not found\r\n",
+        control_printf_endreply(conn, 552, "Server \"%s\" not found",
                                  server);
         goto done;
       }
@@ -1456,7 +1453,7 @@ handle_control_hsfetch(control_connection_t *conn,
     rend_query = rend_data_client_create(hsaddress, desc_id, NULL,
                                          REND_NO_AUTH);
     if (rend_query == NULL) {
-      connection_printf_to_buf(conn, "551 Error creating the HS query\r\n");
+      control_write_endreply(conn, 551, "Error creating the HS query");
       goto done;
     }
   }
@@ -1464,7 +1461,7 @@ handle_control_hsfetch(control_connection_t *conn,
   /* Using a descriptor ID, we force the user to provide at least one
    * hsdir server using the SERVER= option. */
   if (desc_id && (!hsdirs || !smartlist_len(hsdirs))) {
-    connection_printf_to_buf(conn, "512 SERVER option is required\r\n");
+    control_write_endreply(conn, 512, "SERVER option is required");
     goto done;
   }
 
@@ -1516,7 +1513,7 @@ handle_control_hspost(control_connection_t *conn,
       const node_t *node = node_get_by_hex_id(server, 0);
 
       if (!node || !node->rs) {
-        connection_printf_to_buf(conn, "552 Server \"%s\" not found\r\n",
+        control_printf_endreply(conn, 552, "Server \"%s\" not found",
                                  server);
         goto done;
       }
@@ -1527,7 +1524,7 @@ handle_control_hspost(control_connection_t *conn,
     } else if (!strcasecmpstart(line->key, "HSADDRESS")) {
       const char *address = line->value;
       if (!hs_address_is_valid(address)) {
-        connection_printf_to_buf(conn, "512 Malformed onion address\r\n");
+        control_write_endreply(conn, 512, "Malformed onion address");
         goto done;
       }
       onion_address = address;
@@ -1539,7 +1536,7 @@ handle_control_hspost(control_connection_t *conn,
   /* Handle the v3 case. */
   if (onion_address) {
     if (hs_control_hspost_command(encoded_desc, onion_address, hs_dirs) < 0) {
-      connection_printf_to_buf(conn, "554 Invalid descriptor\r\n");
+      control_write_endreply(conn, 554, "Invalid descriptor");
     } else {
       send_control_done(conn);
     }
@@ -1579,7 +1576,7 @@ handle_control_hspost(control_connection_t *conn,
 
     rend_service_descriptor_free(parsed);
   } else {
-    connection_printf_to_buf(conn, "554 Invalid descriptor\r\n");
+    control_write_endreply(conn, 554, "Invalid descriptor");
   }
 
   tor_free(intro_content);
@@ -1685,7 +1682,7 @@ handle_control_add_onion(control_connection_t *conn,
       rend_service_port_config_t *cfg =
           rend_service_parse_port_config(arg->value, ",", NULL);
       if (!cfg) {
-        connection_printf_to_buf(conn, "512 Invalid VIRTPORT/TARGET\r\n");
+        control_write_endreply(conn, 512, "Invalid VIRTPORT/TARGET");
         goto out;
       }
       smartlist_add(port_cfgs, cfg);
@@ -1694,7 +1691,7 @@ handle_control_add_onion(control_connection_t *conn,
       int ok = 0;
       max_streams = (int)tor_parse_long(arg->value, 10, 0, 65535, &ok, NULL);
       if (!ok) {
-        connection_printf_to_buf(conn, "512 Invalid MaxStreams\r\n");
+        control_write_endreply(conn, 512, "Invalid MaxStreams");
         goto out;
       }
     } else if (!strcasecmp(arg->key, "Flags")) {
@@ -1722,7 +1719,7 @@ handle_control_add_onion(control_connection_t *conn,
 
       smartlist_split_string(flags, arg->value, ",", SPLIT_IGNORE_BLANK, 0);
       if (smartlist_len(flags) < 1) {
-        connection_printf_to_buf(conn, "512 Invalid 'Flags' argument\r\n");
+        control_write_endreply(conn, 512, "Invalid 'Flags' argument");
         bad = 1;
       }
       SMARTLIST_FOREACH_BEGIN(flags, const char *, flag)
@@ -1738,8 +1735,7 @@ handle_control_add_onion(control_connection_t *conn,
         } else if (!strcasecmp(flag, non_anonymous_flag)) {
           non_anonymous = 1;
         } else {
-          connection_printf_to_buf(conn,
-                                   "512 Invalid 'Flags' argument: %s\r\n",
+          control_printf_endreply(conn, 512, "Invalid 'Flags' argument: %s",
                                    escaped(flag));
           bad = 1;
           break;
@@ -1773,8 +1769,7 @@ handle_control_add_onion(control_connection_t *conn,
           }
         } SMARTLIST_FOREACH_END(ac);
         if (bad) {
-          connection_printf_to_buf(conn,
-                                   "512 Duplicate name in ClientAuth\r\n");
+          control_write_endreply(conn, 512, "Duplicate name in ClientAuth");
           rend_authorized_client_free(client);
           goto out;
         }
@@ -1792,19 +1787,19 @@ handle_control_add_onion(control_connection_t *conn,
     }
   }
   if (smartlist_len(port_cfgs) == 0) {
-    connection_printf_to_buf(conn, "512 Missing 'Port' argument\r\n");
+    control_write_endreply(conn, 512, "Missing 'Port' argument");
     goto out;
   } else if (auth_type == REND_NO_AUTH && auth_clients != NULL) {
-    connection_printf_to_buf(conn, "512 No auth type specified\r\n");
+    control_write_endreply(conn, 512, "No auth type specified");
     goto out;
   } else if (auth_type != REND_NO_AUTH && auth_clients == NULL) {
-    connection_printf_to_buf(conn, "512 No auth clients specified\r\n");
+    control_write_endreply(conn, 512, "No auth clients specified");
     goto out;
   } else if ((auth_type == REND_BASIC_AUTH &&
               smartlist_len(auth_clients) > 512) ||
              (auth_type == REND_STEALTH_AUTH &&
               smartlist_len(auth_clients) > 16)) {
-    connection_printf_to_buf(conn, "512 Too many auth clients\r\n");
+    control_write_endreply(conn, 512, "Too many auth clients");
     goto out;
   } else if (non_anonymous != rend_service_non_anonymous_mode_enabled(
                                                               get_options())) {
@@ -1815,8 +1810,8 @@ handle_control_add_onion(control_connection_t *conn,
      * 512 Tor is in non-anonymous hidden service mode
      * (I've deliberately written them out in full here to aid searchability.)
      */
-    connection_printf_to_buf(conn, "512 Tor is in %sanonymous hidden service "
-                             "mode\r\n",
+    control_printf_endreply(conn, 512,
+                             "Tor is in %sanonymous hidden service " "mode",
                              non_anonymous ? "" : "non-");
     goto out;
   }
@@ -1843,7 +1838,7 @@ handle_control_add_onion(control_connection_t *conn,
   /* Hidden service version 3 don't have client authentication support so if
    * ClientAuth was given, send back an error. */
   if (hs_version == HS_VERSION_THREE && auth_clients) {
-    connection_printf_to_buf(conn, "513 ClientAuth not supported\r\n");
+    control_write_endreply(conn, 513, "ClientAuth not supported");
     goto out;
   }
 
@@ -1873,10 +1868,10 @@ handle_control_add_onion(control_connection_t *conn,
     }
 
     tor_assert(service_id);
-    connection_printf_to_buf(conn, "250-ServiceID=%s\r\n", service_id);
+    control_printf_midreply(conn, 250, "ServiceID=%s", service_id);
     if (key_new_alg) {
       tor_assert(key_new_blob);
-      connection_printf_to_buf(conn, "250-PrivateKey=%s:%s\r\n",
+      control_printf_midreply(conn, 250, "PrivateKey=%s:%s",
                                key_new_alg, key_new_blob);
     }
     if (auth_created_clients) {
@@ -1891,24 +1886,24 @@ handle_control_add_onion(control_connection_t *conn,
       });
     }
 
-    connection_printf_to_buf(conn, "250 OK\r\n");
+    send_control_done(conn);
     break;
   }
   case RSAE_BADPRIVKEY:
-    connection_printf_to_buf(conn, "551 Failed to generate onion address\r\n");
+    control_write_endreply(conn, 551, "Failed to generate onion address");
     break;
   case RSAE_ADDREXISTS:
-    connection_printf_to_buf(conn, "550 Onion address collision\r\n");
+    control_write_endreply(conn, 550, "Onion address collision");
     break;
   case RSAE_BADVIRTPORT:
-    connection_printf_to_buf(conn, "512 Invalid VIRTPORT/TARGET\r\n");
+    control_write_endreply(conn, 512, "Invalid VIRTPORT/TARGET");
     break;
   case RSAE_BADAUTH:
-    connection_printf_to_buf(conn, "512 Invalid client authorization\r\n");
+    control_write_endreply(conn, 512, "Invalid client authorization");
     break;
   case RSAE_INTERNAL: /* FALLSTHROUGH */
   default:
-    connection_printf_to_buf(conn, "551 Failed to add Onion Service\r\n");
+    control_write_endreply(conn, 551, "Failed to add Onion Service");
   }
   if (key_new_blob) {
     memwipe(key_new_blob, 0, strlen(key_new_blob));
@@ -2150,7 +2145,7 @@ handle_control_del_onion(control_connection_t *conn,
   } else if (hs_address_is_valid(service_id)) {
     hs_version = HS_VERSION_THREE;
   } else {
-    connection_printf_to_buf(conn, "512 Malformed Onion Service id\r\n");
+    control_write_endreply(conn, 512, "Malformed Onion Service id");
     goto out;
   }
 
@@ -2174,7 +2169,7 @@ handle_control_del_onion(control_connection_t *conn,
     }
   }
   if (onion_services == NULL) {
-    connection_printf_to_buf(conn, "552 Unknown Onion Service id\r\n");
+    control_write_endreply(conn, 552, "Unknown Onion Service id");
   } else {
     int ret = -1;
     switch (hs_version) {
@@ -2226,7 +2221,7 @@ handle_control_obsolete(control_connection_t *conn,
   (void)args;
   char *command = tor_strdup(conn->current_cmd);
   tor_strupper(command);
-  connection_printf_to_buf(conn, "511 %s is obsolete.\r\n", command);
+  control_printf_endreply(conn, 511, "%s is obsolete.", command);
   tor_free(command);
   return 0;
 }
@@ -2359,8 +2354,7 @@ handle_single_control_command(const control_cmd_def_t *def,
                                        cmd_data_len, args,
                                        &err);
   if (!parsed_args) {
-    connection_printf_to_buf(conn,
-                             "512 Bad arguments to %s: %s\r\n",
+    control_printf_endreply(conn, 512, "Bad arguments to %s: %s",
                              conn->current_cmd, err?err:"");
     tor_free(err);
   } else {
@@ -2401,7 +2395,7 @@ handle_control_command(control_connection_t *conn,
     }
   }
 
-  connection_printf_to_buf(conn, "510 Unrecognized command \"%s\"\r\n",
+  control_printf_endreply(conn, 510, "Unrecognized command \"%s\"",
                            conn->current_cmd);
 
   return 0;
diff --git a/src/feature/control/control_getinfo.c b/src/feature/control/control_getinfo.c
index c10229bfb..3e31bb9e8 100644
--- a/src/feature/control/control_getinfo.c
+++ b/src/feature/control/control_getinfo.c
@@ -1608,7 +1608,7 @@ handle_control_getinfo(control_connection_t *conn,
     if (handle_getinfo_helper(conn, q, &ans, &errmsg) < 0) {
       if (!errmsg)
         errmsg = "Internal error";
-      connection_printf_to_buf(conn, "551 %s\r\n", errmsg);
+      control_write_endreply(conn, 551, errmsg);
       goto done;
     }
     if (!ans) {
@@ -1625,13 +1625,10 @@ handle_control_getinfo(control_connection_t *conn,
   if (smartlist_len(unrecognized)) {
     /* control-spec section 2.3, mid-reply '-' or end of reply ' ' */
     for (i=0; i < smartlist_len(unrecognized)-1; ++i)
-      connection_printf_to_buf(conn,
-                               "552-%s\r\n",
-                               (char *)smartlist_get(unrecognized, i));
-
-    connection_printf_to_buf(conn,
-                             "552 %s\r\n",
+      control_write_midreply(conn, 552,
                              (char *)smartlist_get(unrecognized, i));
+
+    control_write_endreply(conn, 552, (char *)smartlist_get(unrecognized, i));
     goto done;
   }
 
@@ -1645,7 +1642,7 @@ handle_control_getinfo(control_connection_t *conn,
       control_write_data(conn, v);
     }
   }
-  connection_write_str_to_buf("250 OK\r\n", conn);
+  send_control_done(conn);
 
  done:
   SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
diff --git a/src/feature/control/control_proto.c b/src/feature/control/control_proto.c
index 7bd237de9..1dd62da2b 100644
--- a/src/feature/control/control_proto.c
+++ b/src/feature/control/control_proto.c
@@ -166,7 +166,7 @@ read_escaped_data(const char *data, size_t len, char **out)
 void
 send_control_done(control_connection_t *conn)
 {
-  connection_write_str_to_buf("250 OK\r\n", conn);
+  control_write_endreply(conn, 250, "OK");
 }
 
 /** Write a reply to the control channel.





More information about the tor-commits mailing list