Attention is currently required from: Arne Schwabe, Frank Lichtenheld, Lev 
Stipakov, Ralf Lici.

Hello Arne Schwabe, Frank Lichtenheld, Ralf Lici,

I'd like you to reexamine a change. Please visit

    https://gerrit.openvpn.net/c/openvpn/+/1745?usp=email

to look at the new patch set (#22).


Change subject: probe: Add client probe reply parser
......................................................................

probe: Add client probe reply parser

Add probe_reply_find(), the client-side counterpart of
probe_request_find(): it scans a received P_CONTROL_V2 message for the
probe reply TLV, skipping other and future TLV types that are marked
optional. The reply's request_id names the probe it answers, with which
the client matches the reply to its probe.

Exercised by unit tests; the client probe path calls it in a follow-up.

Change-Id: If04ce09d4c353f5384c0c48f48f63e05869a373f
Signed-off-by: Lev Stipakov <[email protected]>
---
M src/openvpn/probe.c
M src/openvpn/probe.h
M tests/unit_tests/openvpn/test_pkt.c
M tests/unit_tests/openvpn/test_probe.c
M tests/unit_tests/openvpn/test_probe.h
5 files changed, 147 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/45/1745/22

diff --git a/src/openvpn/probe.c b/src/openvpn/probe.c
index b59c654..27de23a 100644
--- a/src/openvpn/probe.c
+++ b/src/openvpn/probe.c
@@ -102,6 +102,14 @@
 }

 bool
+probe_reply_find(struct buffer *payload, struct probe_reply *reply)
+{
+    struct buffer value;
+    return ctrl_msg_find_tlv(payload, TLV_TYPE_PROBE_REPLY, &value)
+           && probe_reply_read(&value, reply);
+}
+
+bool
 probe_timestamp_in_window(uint64_t probe_ts, uint64_t now, uint64_t 
window_secs)
 {
     /* both clocks are unsigned and either may be ahead, so subtract the
diff --git a/src/openvpn/probe.h b/src/openvpn/probe.h
index f836416..108ef58 100644
--- a/src/openvpn/probe.h
+++ b/src/openvpn/probe.h
@@ -153,6 +153,20 @@
 bool probe_request_find(struct buffer *payload, struct probe_request *req);

 /**
+ * Find the probe reply TLV in the payload of a received P_CONTROL_V2 message
+ * and parse it; the client-side counterpart of probe_request_find(), with the
+ * same handling of the other TLV types.
+ *
+ * The request_id of the reply names the probe it answers, which is how the
+ * client tells apart the replies to the probes it has outstanding.
+ *
+ * @param payload  the message payload, positioned at the first TLV
+ * @param reply    filled with the probe reply on success
+ * @return true if the payload holds exactly one well-formed probe reply
+ */
+bool probe_reply_find(struct buffer *payload, struct probe_reply *reply);
+
+/**
  * Is the timestamp of a probe request close enough to our own clock?
  *
  * This is how a server bounds the age of the requests it answers, and with it
diff --git a/tests/unit_tests/openvpn/test_pkt.c 
b/tests/unit_tests/openvpn/test_pkt.c
index aecac00..1958736 100644
--- a/tests/unit_tests/openvpn/test_pkt.c
+++ b/tests/unit_tests/openvpn/test_pkt.c
@@ -982,7 +982,12 @@
         cmocka_unit_test(test_probe_timestamp_in_window),
         cmocka_unit_test(test_probe_request_check_valid),
         cmocka_unit_test(test_probe_request_check_stale),
-        cmocka_unit_test(test_probe_request_check_no_request)
+        cmocka_unit_test(test_probe_request_check_no_request),
+        cmocka_unit_test(test_probe_reply_find),
+        cmocka_unit_test(test_probe_reply_find_skips_unknown),
+        cmocka_unit_test(test_probe_reply_find_rejects_unknown_mandatory),
+        cmocka_unit_test(test_probe_reply_find_missing),
+        cmocka_unit_test(test_probe_reply_find_request_tlv)
     };

     return cmocka_run_group_tests_name("pkt tests", tests, NULL, NULL);
diff --git a/tests/unit_tests/openvpn/test_probe.c 
b/tests/unit_tests/openvpn/test_probe.c
index 1c6d2c1..d1272117 100644
--- a/tests/unit_tests/openvpn/test_probe.c
+++ b/tests/unit_tests/openvpn/test_probe.c
@@ -599,3 +599,112 @@

     gc_free(&gc);
 }
+
+/* A payload of just a probe reply is found by the client scan, with all
+ * fields surviving. */
+void
+test_probe_reply_find(void **state)
+{
+    struct gc_arena gc = gc_new();
+    struct buffer buf = alloc_buf_gc(128, &gc);
+
+    const struct probe_reply in = {
+        .request_id = 0xdeadbeefcafef00dULL,
+        .priority = 5,
+        .weight = 50,
+        .max_latency_diff = 25,
+        .connect_lifetime = 120,
+        .flags = PROBE_REPLY_FLAG_RESEND_WKC,
+    };
+    assert_true(probe_reply_write(&buf, &in));
+
+    struct probe_reply out = { 0 };
+    assert_true(probe_reply_find(&buf, &out));
+    assert_true(out.request_id == in.request_id);
+    assert_int_equal(out.priority, in.priority);
+    assert_int_equal(out.weight, in.weight);
+    assert_int_equal(out.max_latency_diff, in.max_latency_diff);
+    assert_int_equal(out.connect_lifetime, in.connect_lifetime);
+    assert_int_equal(out.flags, in.flags);
+
+    gc_free(&gc);
+}
+
+/* A TLV of another type that is marked optional is skipped, so the reply is
+ * still found behind one. */
+void
+test_probe_reply_find_skips_unknown(void **state)
+{
+    struct gc_arena gc = gc_new();
+    struct buffer buf = alloc_buf_gc(128, &gc);
+
+    assert_true(ctrl_msg_tlv_write_header(&buf, 0x7ff, true, 4));
+    assert_true(buf_write_u32(&buf, 0xabad1dea));
+    const struct probe_reply in = { .priority = 7 };
+    assert_true(probe_reply_write(&buf, &in));
+
+    struct probe_reply out = { 0 };
+    assert_true(probe_reply_find(&buf, &out));
+    assert_int_equal(out.priority, 7);
+
+    gc_free(&gc);
+}
+
+/* As on the request side, a TLV we do not know that is not marked optional
+ * rejects the message whether it comes before or after the reply. */
+void
+test_probe_reply_find_rejects_unknown_mandatory(void **state)
+{
+    struct gc_arena gc = gc_new();
+    const struct probe_reply in = { .priority = 7 };
+
+    struct buffer before = alloc_buf_gc(128, &gc);
+    assert_true(ctrl_msg_tlv_write_header(&before, 0x7ff, false, 4));
+    assert_true(buf_write_u32(&before, 0xabad1dea));
+    assert_true(probe_reply_write(&before, &in));
+
+    struct buffer after = alloc_buf_gc(128, &gc);
+    assert_true(probe_reply_write(&after, &in));
+    assert_true(ctrl_msg_tlv_write_header(&after, 0x7ff, false, 4));
+    assert_true(buf_write_u32(&after, 0xabad1dea));
+
+    struct probe_reply out = { 0 };
+    assert_false(probe_reply_find(&before, &out));
+    assert_false(probe_reply_find(&after, &out));
+
+    gc_free(&gc);
+}
+
+/* A payload that holds no probe reply at all. */
+void
+test_probe_reply_find_missing(void **state)
+{
+    struct gc_arena gc = gc_new();
+    struct buffer buf = alloc_buf_gc(128, &gc);
+
+    assert_true(ctrl_msg_tlv_write_header(&buf, 0x7ff, true, 4));
+    assert_true(buf_write_u32(&buf, 0));
+
+    struct probe_reply out = { 0 };
+    assert_false(probe_reply_find(&buf, &out));
+
+    gc_free(&gc);
+}
+
+/* A probe request is not a probe reply: a client handed one must not take it
+ * for an answer. The request TLV is mandatory, so it rejects the message
+ * rather than being skipped. */
+void
+test_probe_reply_find_request_tlv(void **state)
+{
+    struct gc_arena gc = gc_new();
+    struct buffer buf = alloc_buf_gc(128, &gc);
+
+    const struct probe_request in = { .request_id = 42 };
+    assert_true(probe_request_write(&buf, &in));
+
+    struct probe_reply out = { 0 };
+    assert_false(probe_reply_find(&buf, &out));
+
+    gc_free(&gc);
+}
diff --git a/tests/unit_tests/openvpn/test_probe.h 
b/tests/unit_tests/openvpn/test_probe.h
index 941bf99..52864cc 100644
--- a/tests/unit_tests/openvpn/test_probe.h
+++ b/tests/unit_tests/openvpn/test_probe.h
@@ -69,4 +69,14 @@

 void test_probe_request_check_no_request(void **state);

+void test_probe_reply_find(void **state);
+
+void test_probe_reply_find_skips_unknown(void **state);
+
+void test_probe_reply_find_rejects_unknown_mandatory(void **state);
+
+void test_probe_reply_find_missing(void **state);
+
+void test_probe_reply_find_request_tlv(void **state);
+
 #endif

--
To view, visit https://gerrit.openvpn.net/c/openvpn/+/1745?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If04ce09d4c353f5384c0c48f48f63e05869a373f
Gerrit-Change-Number: 1745
Gerrit-PatchSet: 22
Gerrit-Owner: Lev Stipakov <[email protected]>
Gerrit-Reviewer: Arne Schwabe <[email protected]>
Gerrit-Reviewer: Frank Lichtenheld <[email protected]>
Gerrit-Reviewer: Ralf Lici <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: Ralf Lici <[email protected]>
Gerrit-Attention: Lev Stipakov <[email protected]>
Gerrit-Attention: Arne Schwabe <[email protected]>
Gerrit-Attention: Frank Lichtenheld <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to