From eb56a23613638ca705f5be707cacc1b0989a2a76 Mon Sep 17 00:00:00 2001
From: David Timber <mieabby@gmail.com>
Date: Sat, 11 Sep 2021 11:11:40 +0800
Subject: Use prne_htbt_gen_msgid() to unify msg_id gen algo

* Add PRNE_HTBT_MSG_ID_NOTIFY for protocol error w/o associated msg_id
* Remove PRNE_HTBT_MSG_ID_MAX(wrong value)
---
 src/htbt.c             | 24 +++++++++++++-----------
 src/proone-hostinfod.c | 11 +++++++----
 src/protocol.h         |  4 ++--
 3 files changed, 22 insertions(+), 17 deletions(-)

(limited to 'src')

diff --git a/src/htbt.c b/src/htbt.c
index 971c7d4..1389c5f 100644
--- a/src/htbt.c
+++ b/src/htbt.c
@@ -826,11 +826,12 @@ static bool htbt_slv_recv_mh (
 	const bool mid,
 	pth_event_t ev)
 {
+	static const uint16_t ERR_MSGID = PRNE_HTBT_MSG_ID_NOTIFY;
 	bool ret = htbt_slv_recv_frame(
 		ctx,
 		mh,
 		(prne_htbt_dser_ft)prne_htbt_dser_msg_head,
-		corr_id,
+		&ERR_MSGID,
 		mid,
 		ev);
 
@@ -2196,25 +2197,26 @@ static bool htbt_main_slv_loopchk_f (void *ioctx) {
 	return ctx->parent->loop_flag;
 }
 
+static uint16_t htbt_main_gen_msgid (void *ctx) {
+	uint16_t ret = PRNE_HTBT_MSG_ID_MIN;
+	mbedtls_ctr_drbg_random(
+		(mbedtls_ctr_drbg_context*)ctx,
+		(unsigned char *)&ret,
+		sizeof(ret));
+	return ret;
+}
+
 static bool htbt_main_slv_setup_f (void *ioctx, pth_event_t ev) {
 	htbt_main_client_t *ctx = (htbt_main_client_t*)ioctx;
 	bool ret = true;
 	size_t actual;
 	prne_htbt_msg_head_t mh;
-	int f_ret;
 
 	prne_htbt_init_msg_head(&mh);
 
-	f_ret = mbedtls_ctr_drbg_random(
+	mh.id = prne_htbt_gen_msgid(
 		ctx->parent->param.ctr_drbg,
-		(unsigned char *)&mh.id,
-		sizeof(mh.id));
-	if (f_ret == 0) {
-		mh.id = (mh.id % PRNE_HTBT_MSG_ID_DELTA) + PRNE_HTBT_MSG_ID_MIN;
-	}
-	else {
-		mh.id = PRNE_HTBT_MSG_ID_MIN;
-	}
+		htbt_main_gen_msgid);
 	mh.is_rsp = false;
 	mh.op = PRNE_HTBT_OP_SOLICIT;
 	prne_htbt_ser_msg_head(NULL, 0, &actual, &mh);
diff --git a/src/proone-hostinfod.c b/src/proone-hostinfod.c
index ea6536b..97f4d09 100644
--- a/src/proone-hostinfod.c
+++ b/src/proone-hostinfod.c
@@ -1414,6 +1414,12 @@ END: // CATCH
 	return ret;
 }
 
+static uint16_t gen_msgid (void *ctx) {
+	uint16_t ret = PRNE_HTBT_MSG_ID_MIN;
+	prne_rnd((prne_rnd_t*)ctx, (uint8_t*)&ret, sizeof(ret));
+	return ret;
+}
+
 static int proc_client_stream (th_ctx_t *ctx, client_ctx_t *c) {
 	prne_htbt_msg_head_t mh;
 	prne_htbt_ser_rc_t src;
@@ -1444,10 +1450,7 @@ static int proc_client_stream (th_ctx_t *ctx, client_ctx_t *c) {
 		}
 
 		mh.op = PRNE_HTBT_OP_HOST_INFO;
-		prne_rnd(&ctx->rnd, (uint8_t*)&mh.id, sizeof(mh.id));
-		mh.id =
-			PRNE_HTBT_MSG_ID_MIN +
-			((mh.id % PRNE_HTBT_MSG_ID_MAX) - PRNE_HTBT_MSG_ID_MIN);
+		mh.id = prne_htbt_gen_msgid(&ctx->rnd, gen_msgid);
 
 		prne_htbt_ser_msg_head(NULL, 0, &actual, &mh);
 		if (prne_alloc_iobuf(c->ib + 1, actual)) {
diff --git a/src/protocol.h b/src/protocol.h
index 72f39ca..da500b3 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -344,8 +344,8 @@ typedef prne_htbt_ser_rc_t(*prne_htbt_dser_ft)(
 
 #define PRNE_HTBT_TLS_ALP			"prne-htbt"
 #define PRNE_HTBT_MSG_ID_MIN		1
-#define PRNE_HTBT_MSG_ID_MAX		INT16_MAX
-#define PRNE_HTBT_MSG_ID_DELTA		INT16_MAX
+#define PRNE_HTBT_MSG_ID_DELTA		(INT16_MAX - 1)
+#define PRNE_HTBT_MSG_ID_NOTIFY		INT16_MAX
 #define PRNE_HTBT_PROTO_PORT		64420
 // _POSIX_ARG_MAX equiv
 #define PRNE_HTBT_ARGS_MAX			255
-- 
cgit v1.2.3-70-g09d2