From e0074d8b74f9e62df913a3817fc4cecf7cac2c69 Mon Sep 17 00:00:00 2001 From: Jesper Jensen Date: Tue, 5 Oct 2021 23:07:06 +0200 Subject: Respond correctly no unknown query method --- src/proto.c | 15 +++++++++++++-- src/query.c | 1 + test/proto.c | 26 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/proto.c b/src/proto.c index 8a6f0e8..cde232b 100644 --- a/src/proto.c +++ b/src/proto.c @@ -485,8 +485,19 @@ int handle_packet(struct dht* dht, time_t now, enum commandType type, char* tran rc = handle_request(&dht->self, query, packet, packet_len, &cursor, end-cursor-1); if(rc == QUERY_EUNK) { - dbg("DISCARD: Unknown query method"); - return 0; + // The y key should have value e + *(cursor-4) = 'e'; + // The r key is called e for errors + *(cursor-1) = 'e'; + + // Now create the payload + rc = snprintf(cursor, end-cursor, "li204e14:Unknown Methode"); + if(rc < 0) + fatal("No space for response"); + cursor += rc; + assert(cursor < end); + + // Use the normal finalize flow } else if(rc != 0) fatal("Error handling request"); rc = snprintf(cursor, end-cursor, "e"); diff --git a/src/query.c b/src/query.c index 5645271..806c691 100644 --- a/src/query.c +++ b/src/query.c @@ -75,6 +75,7 @@ int handle_request(struct nodeid* self, const char* method, const char* packet, if(rc < 0) return QUERY_EBADQ; *response += rc; + }else if(strcmp(method, "get_peers") == 0) { } else { return QUERY_EUNK; } diff --git a/test/proto.c b/test/proto.c index 600602e..ae42e36 100644 --- a/test/proto.c +++ b/test/proto.c @@ -111,6 +111,32 @@ void test_ping() { TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:r1:rd2:id20:BBBBBBBBBBBBBBBBBBBBee", outbuff[0].payload, 47); } +void test_unknown_method() { + struct message outbuff[2] = {0}; + + struct dht dht; + dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}}; + { + struct message* message_cursor = outbuff; + proto_begin(&dht, time(NULL), &message_cursor, outbuff+2); + } + + struct sockaddr_in remote; + remote.sin_family = AF_INET; + inet_pton(AF_INET, "255.255.255.255", &remote.sin_addr.s_addr); + remote.sin_port = htons(6881); + + char buff[] = "d1:q4:fake1:t2:aa1:y1:qe"; + struct message* message_cursor = outbuff; + proto_run(&dht, buff, sizeof(buff), &remote, sizeof(remote), 0, &message_cursor, outbuff+2); + + // We should have sent a response + TEST_ASSERT_EQUAL_PTR(message_cursor, outbuff+1); + + TEST_ASSERT_EQUAL(42, outbuff[0].payload_len); + TEST_ASSERT_EQUAL_CHAR_ARRAY("d1:t2:aa1:y1:e1:eli204e14:Unknown Methodee", outbuff[0].payload, 42); +} + void test_note_times_out() { routing_flush(); struct message outbuff[2] = {0}; -- cgit v1.2.3