From 95c9fbd176a1a5bd55f04a53f06207f3fe633711 Mon Sep 17 00:00:00 2001 From: Sven Luethi Date: Sun, 18 Apr 2021 23:30:15 +0200 Subject: [PATCH] Overhauls Resolver Message class structure --- src/core/main.cpp | 5 +- src/resolver/Message.cpp | 4 ++ src/resolver/Server.cpp | 12 ++--- src/resolver/include/Message.hpp | 80 +++++++++++++++++++------------- 4 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/core/main.cpp b/src/core/main.cpp index 61f88df..1ecdc18 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -74,8 +74,9 @@ int main(int args, char **argv) { //Record testrec = ARecord(); // End of Testcode - eventBus.run(); logger.info.ln ("Plains successfully started."); + //shutdown(); + eventBus.run(); } catch (Exception& e) { logger.fatal << "Uncaught Exception: " << e.what() << Logger::commit; @@ -110,7 +111,7 @@ void signalHandler( int signalNumber ) { } void shutdown() { - globalLogger->warn << "Shutting down Plains..." << Logger::commit; + globalLogger->info << "Shutting down Plains..." << Logger::commit; globalResolver->~Server(); globalUIServer->~Server(); globalEventBus->~EventBus(); diff --git a/src/resolver/Message.cpp b/src/resolver/Message.cpp index c7f2780..275dbc8 100644 --- a/src/resolver/Message.cpp +++ b/src/resolver/Message.cpp @@ -1,8 +1,12 @@ #include "Message.hpp" +#include namespace Plains { namespace Resolver { +unsigned Message::Header::getID() const { + return ntohs(id); +} diff --git a/src/resolver/Server.cpp b/src/resolver/Server.cpp index 0fa2207..0b4a3f5 100644 --- a/src/resolver/Server.cpp +++ b/src/resolver/Server.cpp @@ -12,7 +12,6 @@ #include //Testing: -#include #include #include @@ -30,7 +29,7 @@ Server::Server(Logger& logger, EventBus& eventBus) _logger.debug.ln ("Starting Resolver..."); _logger.warn << "Testing some stuff..." << Logger::commit; - _logger.warn << "Message header overall size: " << sizeof(Message::Header) << Logger::commit; + _logger.warn << "Message::Header class overall size: " << sizeof(Message::Header) << Logger::commit; //_logger.debug << "Message::QR:RESPONSE Enum:" << Message::QR::RESPONSE << endl; /* @@ -99,7 +98,7 @@ void Server::run() { clientAddressLength = sizeof(clientAddress); // Create receive buffer - char buffer[65508] = { 0 }; // (2**16 = 65,536 − 8 byte UDP header − 20 byte IP header) + unsigned char buffer[65508] = { 0 }; // (2**16 = 65,536 − 8 byte UDP header − 20 byte IP header) // Create new UDP Socket if ((udpServerSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { @@ -114,18 +113,19 @@ void Server::run() { while(!_stop) { udpReadStatus = recvfrom(udpServerSocket, buffer, sizeof(buffer), 0, (struct sockaddr*)&clientAddress, &clientAddressLength); - if (udpServerSocket < 0) { + if (udpServerSocket < 0) { close(udpServerSocket); _logger.warn.ln("reading error..."); } - //std::cout.write(buffer, udpReadStatus); - //std::cout << "size: " << udpReadStatus << std::endl; + /* for(int i=0; i(buffer[i]) << " "; } std::cout << std::endl << std::endl; + */ + Message* message = (Message*) &buffer; if ( sendto(udpServerSocket, msg, strlen(msg), 0, (struct sockaddr*)&clientAddress, clientAddressLength) < 0 ) { close(udpServerSocket); diff --git a/src/resolver/include/Message.hpp b/src/resolver/include/Message.hpp index dbfb166..d5e9db9 100644 --- a/src/resolver/include/Message.hpp +++ b/src/resolver/include/Message.hpp @@ -7,59 +7,77 @@ namespace Resolver { class Message { public: - struct Header { -/* - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | ID | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - |QR| OpCode |AA|TC|RD|RA| Z | RCODE | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | QDCOUNT | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | ANCOUNT | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | NSCOUNT | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - | ARCOUNT | - +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ -*/ + Message(); + + public: //change to private after debugging + class Header { + public: + /* + 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | ID | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + |QR| OpCode |AA|TC|RD|RA| Z | RCODE | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | QDCOUNT | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | ANCOUNT | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | NSCOUNT | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + | ARCOUNT | + +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + */ struct QR { enum { QUERY, RESPONSE }; }; struct OpCode { enum { QUERY, IQUERY, STATUS, reserved, NOTIFY, UPDATE }; }; struct RCode { enum { NO_ERR, FORMAT_ERR, SERVER_FAIL, NAME_ERR, NOT_IMPL, REFUSED, YX_DOM, YX_RR_SET, NX_RR_SET, NOT_AUTH, NOT_ZONE }; }; + unsigned getID() const; + unsigned getRCode() const; + unsigned getQDCount() const; + unsigned getANCount() const; + unsigned getNSCount() const; + unsigned getARCount() const; + unsigned getOpCode() const; + bool getRD() const; + bool getAA() const; + bool getTC() const; + bool getRA() const; + bool getQR() const; - unsigned int id : 16; //Identifier: A 16-bit identification field generated by the device that creates the DNS query. It is copied by the server into the response, so it can be used by that device to match that query to the corresponding reply received from a DNS server. This is used in a manner similar to how the Identifier field is used in many of the ICMP message types. + private: + unsigned id : 16; //Identifier: A 16-bit identification field generated by the device that creates the DNS query. It is copied by the server into the response, so it can be used by that device to match that query to the corresponding reply received from a DNS server. This is used in a manner similar to how the Identifier field is used in many of the ICMP message types. - bool qr : 1; //Query/Response Flag: Differentiates between queries and responses. Set to 0 when the query is generated; changed to 1 when that query is changed to a response by a replying server. + bool qr : 1; //Query/Response Flag: Differentiates between queries and responses. Set to 0 when the query is generated; changed to 1 when that query is changed to a response by a replying server. - unsigned int opCode : 4; + unsigned opCode : 4; - bool aa : 1; //Authoritative Answer Flag: This bit is set to 1 in a response to indicate that the server that created the response is authoritative for the zone in which the domain name specified in the Question section is located. If it is 0, the response is non-authoritative. + bool aa : 1; //Authoritative Answer Flag: This bit is set to 1 in a response to indicate that the server that created the response is authoritative for the zone in which the domain name specified in the Question section is located. If it is 0, the response is non-authoritative. - bool tc : 1; //Truncation Flag: When set to 1, indicates that the message was truncated due to its length being longer than the maximum permitted for the type of transport mechanism used. TCP doesn't have a length limit for messages, while UDP messages are limited to 512 bytes, so this bit being sent usually is an indication that the message was sent using UDP and was too long to fit. The client may need to establish a TCP session to get the full message. On the other hand, if the portion truncated was part of the Additional section, it may choose not to bother. + bool tc : 1; //Truncation Flag: When set to 1, indicates that the message was truncated due to its length being longer than the maximum permitted for the type of transport mechanism used. TCP doesn't have a length limit for messages, while UDP messages are limited to 512 bytes, so this bit being sent usually is an indication that the message was sent using UDP and was too long to fit. The client may need to establish a TCP session to get the full message. On the other hand, if the portion truncated was part of the Additional section, it may choose not to bother. - bool rd : 1; //Recursion Desired: When set in a query, requests that the server receiving the query attempt to answer the query recursively, if the server supports recursive resolution. The value of this bit is not changed in the response. + bool rd : 1; //Recursion Desired: When set in a query, requests that the server receiving the query attempt to answer the query recursively, if the server supports recursive resolution. The value of this bit is not changed in the response. - bool ra : 1; //Recursion Available: Set to 1 or cleared to 0 in a response to indicate whether the server creating the response supports recursive queries. This can then be noted by the device that sent the query for future use. + bool ra : 1; //Recursion Available: Set to 1 or cleared to 0 in a response to indicate whether the server creating the response supports recursive queries. This can then be noted by the device that sent the query for future use. - unsigned int z : 3; //Zero: Three reserved bits set to zero. + unsigned z : 3; //Zero: Three reserved bits set to zero. - unsigned int rCode : 4; + unsigned rCode : 4; - unsigned int qdCount : 16; //Question Count: Specifies the number of questions in the Question section of the message. + unsigned qdCount : 16; //Question Count: Specifies the number of questions in the Question section of the message. - unsigned int anCount : 16; //Answer Record Count: Specifies the number of resource records in the Answer section of the message. + unsigned anCount : 16; //Answer Record Count: Specifies the number of resource records in the Answer section of the message. - unsigned int nsCount : 16; //Authority Record Count: Specifies the number of resource records in the Authority section of the message. (“NS” stands for “name server”, of course. J) + unsigned nsCount : 16; //Authority Record Count: Specifies the number of resource records in the Authority section of the message. (“NS” stands for “name server”, of course. J) - unsigned int arCount : 16; //Additional Record Count: Specifies the number of resource records in the Additional section of the message. - }; + unsigned arCount : 16; //Additional Record Count: Specifies the number of resource records in the Additional section of the message. + } header; struct Question {} question; struct Answer {} answer; struct Authority {} authority; struct Additional {} additional; + };