Ticket #25511: b25511-002.patch

File b25511-002.patch, 6.4 KB (added by neel, 16 months ago)

Patch (Revision 2)

  • src/common/util.c

    From c860b6c8ae754f9d09acefd680df37a517ad7c88 Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Thu, 5 Apr 2018 14:19:11 -0400
    Subject: [PATCH 1/2] Add format_local_iso_time_nospace()
    
    ---
     src/common/util.c | 9 +++++++++
     src/common/util.h | 1 +
     2 files changed, 10 insertions(+)
    
    diff --git a/src/common/util.c b/src/common/util.c
    index f63b12167..9003480b6 100644
    a b format_iso_time(char *buf, time_t t) 
    18211821  strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
    18221822}
    18231823
     1824/** As format_local_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
     1825 * embedding an internal space. */
     1826void
     1827format_local_iso_time_nospace(char *buf, time_t t)
     1828{
     1829  format_local_iso_time(buf, t);
     1830  buf[10] = 'T';
     1831}
     1832
    18241833/** As format_iso_time, but use the yyyy-mm-ddThh:mm:ss format to avoid
    18251834 * embedding an internal space. */
    18261835void
  • src/common/util.h

    diff --git a/src/common/util.h b/src/common/util.h
    index aeed8e823..7ec8e6728 100644
    a b int parse_rfc1123_time(const char *buf, time_t *t); 
    269269#define ISO_TIME_USEC_LEN (ISO_TIME_LEN+7)
    270270void format_local_iso_time(char *buf, time_t t);
    271271void format_iso_time(char *buf, time_t t);
     272void format_local_iso_time_nospace(char *buf, time_t t);
    272273void format_iso_time_nospace(char *buf, time_t t);
    273274void format_iso_time_nospace_usec(char *buf, const struct timeval *tv);
    274275int parse_iso_time_(const char *cp, time_t *t, int strict, int nospace);
  • new file changes/ticket25511

    -- 
    2.16.3
    
    
    From f585c23e6f896744c89453bd567be5c2db2b827d Mon Sep 17 00:00:00 2001
    From: Neel Chauhan <neel@neelc.org>
    Date: Thu, 5 Apr 2018 14:19:58 -0400
    Subject: [PATCH 2/2] Add GETINFO current-time/{local,utc} command to
     ControlPort (and regression test)
    
    ---
     changes/ticket25511        |  5 +++++
     src/or/control.c           | 28 ++++++++++++++++++++++++++++
     src/or/control.h           |  4 ++++
     src/test/test_controller.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
     4 files changed, 82 insertions(+)
     create mode 100644 changes/ticket25511
    
    diff --git a/changes/ticket25511 b/changes/ticket25511
    new file mode 100644
    index 000000000..0a24e265c
    - +  
     1  o Minor features (control port):
     2    - Introduce GETINFO "current-time/{local,utc}" to return the local
     3      and UTC times respectively in ISO format. This helps a controller
     4      like Tor Browser detect a time-related error. Closes ticket 25511.
     5      Patch by Neel Chauhan.
  • src/or/control.c

    diff --git a/src/or/control.c b/src/or/control.c
    index 5cac0e172..5cc9641f5 100644
    a b getinfo_helper_listeners(control_connection_t *control_conn, 
    19361936  return 0;
    19371937}
    19381938
     1939/** Implementation helper for GETINFO: answers requests for information about
     1940 * the current time in both local and UTF forms. */
     1941STATIC int
     1942getinfo_helper_current_time(control_connection_t *control_conn,
     1943                         const char *question,
     1944                         char **answer, const char **errmsg)
     1945{
     1946  (void)control_conn;
     1947  (void)errmsg;
     1948
     1949  struct timeval now;
     1950  tor_gettimeofday(&now);
     1951  char timebuf[ISO_TIME_LEN+1];
     1952
     1953  if (!strcmp(question, "current-time/local"))
     1954    format_local_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1955  else if (!strcmp(question, "current-time/utc"))
     1956    format_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1957  else
     1958    return 0;
     1959
     1960  *answer = tor_strdup(timebuf);
     1961  return 0;
     1962}
     1963
    19391964/** Implementation helper for GETINFO: knows the answers for questions about
    19401965 * directory information. */
    19411966STATIC int
    static const getinfo_item_t getinfo_items[] = { 
    30783103  DOC("config/defaults",
    30793104      "List of default values for configuration options. "
    30803105      "See also config/names"),
     3106  PREFIX("current-time/", current_time, "Current time."),
     3107  DOC("current-time/local", "Current time on the local system."),
     3108  DOC("current-time/utc", "Current UTC time."),
    30813109  PREFIX("downloads/networkstatus/", downloads,
    30823110         "Download statuses for networkstatus objects"),
    30833111  DOC("downloads/networkstatus/ns",
  • src/or/control.h

    diff --git a/src/or/control.h b/src/or/control.h
    index 28ffeaed8..62c53650a 100644
    a b STATIC int getinfo_helper_dir( 
    314314    control_connection_t *control_conn,
    315315    const char *question, char **answer,
    316316    const char **errmsg);
     317STATIC int getinfo_helper_current_time(
     318    control_connection_t *control_conn,
     319    const char *question, char **answer,
     320    const char **errmsg);
    317321
    318322#endif /* defined(CONTROL_PRIVATE) */
    319323
  • src/test/test_controller.c

    diff --git a/src/test/test_controller.c b/src/test/test_controller.c
    index 1c285bb3a..a3e8c0bc0 100644
    a b test_download_status_bridge(void *arg) 
    14701470  return;
    14711471}
    14721472
     1473static void
     1474test_current_time(void *arg)
     1475{
     1476  /* We just need one of these to pass, it doesn't matter what's in it */
     1477  control_connection_t dummy;
     1478  /* Get results out */
     1479  char *answer = NULL;
     1480  const char *errmsg = NULL;
     1481
     1482  /* We need these for storing the time. */
     1483  struct timeval now;
     1484  tor_gettimeofday(&now);
     1485  char timebuf[ISO_TIME_LEN+1];
     1486
     1487  (void)arg;
     1488
     1489  /* Case 1 - local time */
     1490  format_local_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1491  getinfo_helper_current_time(&dummy,
     1492                              "current-time/local",
     1493                              &answer, &errmsg);
     1494  tt_ptr_op(answer, OP_NE, NULL);
     1495  tt_ptr_op(errmsg, OP_EQ, NULL);
     1496  tt_str_op(answer, OP_EQ, timebuf);
     1497  tor_free(answer);
     1498  errmsg = NULL;
     1499
     1500  /* Case 2 - UTC time */
     1501  format_iso_time_nospace(timebuf, (time_t)now.tv_sec);
     1502  getinfo_helper_current_time(&dummy,
     1503                              "current-time/utc",
     1504                              &answer, &errmsg);
     1505  tt_ptr_op(answer, OP_NE, NULL);
     1506  tt_ptr_op(errmsg, OP_EQ, NULL);
     1507  tt_str_op(answer, OP_EQ, timebuf);
     1508  tor_free(answer);
     1509  errmsg = NULL;
     1510
     1511 done:
     1512  tor_free(answer);
     1513
     1514  return;
     1515}
     1516
    14731517struct testcase_t controller_tests[] = {
    14741518  { "add_onion_helper_keyarg_v2", test_add_onion_helper_keyarg_v2, 0,
    14751519    NULL, NULL },
    struct testcase_t controller_tests[] = { 
    14861530    NULL },
    14871531  { "download_status_desc", test_download_status_desc, 0, NULL, NULL },
    14881532  { "download_status_bridge", test_download_status_bridge, 0, NULL, NULL },
     1533  { "current_time", test_current_time, 0, NULL, NULL },
    14891534  END_OF_TESTCASES
    14901535};
    14911536