diff --git a/src/core/Logger.cpp b/src/core/Logger.cpp index ee2b1ad..1a4cbd5 100644 --- a/src/core/Logger.cpp +++ b/src/core/Logger.cpp @@ -134,6 +134,16 @@ Logger::Sink& Logger::Sink::operator<<(const int msg) { return *this; } +Logger::Sink& Logger::Sink::operator<<(const unsigned msg) { + _parent._mutex.try_lock(); + if ( _level >= _parent._logLevel ) { + prefixLine(); + *(_parent._output) << msg; + cout << msg; + } + return *this; +} + Logger::Sink& Logger::Sink::operator<<(const long msg) { _parent._mutex.try_lock(); if ( _level >= _parent._logLevel ) { diff --git a/src/core/include/Logger.hpp b/src/core/include/Logger.hpp index 152da68..84eab39 100644 --- a/src/core/include/Logger.hpp +++ b/src/core/include/Logger.hpp @@ -50,6 +50,7 @@ class Logger { Sink& operator<<(const string& msg); Sink& operator<<(const char* msg); Sink& operator<<(const int msg); + Sink& operator<<(const unsigned msg); Sink& operator<<(const long msg); Sink& operator<<(const unsigned long msg); }; diff --git a/src/resolver/Message.cpp b/src/resolver/Message.cpp index f09dabf..cdf942a 100644 --- a/src/resolver/Message.cpp +++ b/src/resolver/Message.cpp @@ -4,18 +4,20 @@ namespace Plains { namespace Resolver { -unsigned Message::Header::getID() const { return ntohs(id); } -unsigned Message::Header::getRCode() const { return ntohs(rCode); }; -unsigned Message::Header::getQDCount() const { return ntohs(qdCount); }; -unsigned Message::Header::getANCount() const { return ntohs(anCount); }; -unsigned Message::Header::getNSCount() const { return ntohs(nsCount); }; -unsigned Message::Header::getARCount() const { return ntohs(arCount); }; -unsigned Message::Header::getOpCode() const { return ntohs(opCode); }; -bool Message::Header::getRD() const { return ntohs(rd); }; -bool Message::Header::getAA() const { return ntohs(aa); }; -bool Message::Header::getTC() const { return ntohs(tc); }; -bool Message::Header::getRA() const { return ntohs(ra); }; -bool Message::Header::getQR() const { return ntohs(qr); }; +unsigned Message::Header::getID() const { return ntohs(_id); } +unsigned Message::Header::getRCode() const { return ntohs(_rCode); } +unsigned Message::Header::getQDCount() const { return ntohs(_qdCount); } +unsigned Message::Header::getANCount() const { return ntohs(_anCount); } +unsigned Message::Header::getNSCount() const { return ntohs(_nsCount); } +unsigned Message::Header::getARCount() const { return ntohs(_arCount); } +unsigned Message::Header::getOpCode() const { return ntohs(_opCode); } +bool Message::Header::getRD() const { return ntohs(_rd); } +bool Message::Header::getAA() const { return ntohs(_aa); } +bool Message::Header::getTC() const { return ntohs(_tc); } +bool Message::Header::getRA() const { return ntohs(_ra); } +bool Message::Header::getQR() const { return ntohs(_qr); } + +void Message::Header::setQR(bool flag) { _qr = flag; } diff --git a/src/resolver/Server.cpp b/src/resolver/Server.cpp index 0b4a3f5..c122b59 100644 --- a/src/resolver/Server.cpp +++ b/src/resolver/Server.cpp @@ -126,8 +126,13 @@ void Server::run() { std::cout << std::endl << std::endl; */ Message* message = (Message*) &buffer; + _logger.warn << "ID: " << message->header.getID() << Logger::commit; - if ( sendto(udpServerSocket, msg, strlen(msg), 0, (struct sockaddr*)&clientAddress, clientAddressLength) < 0 ) { + // Change message to response type and send back for testing + message->header.setQR(Message::Header::QR::RESPONSE); + + //if ( sendto(udpServerSocket, msg, strlen(msg), 0, (struct sockaddr*)&clientAddress, clientAddressLength) < 0 ) { + if ( sendto(udpServerSocket, message, strlen(msg), 0, (struct sockaddr*)&clientAddress, clientAddressLength) < 0 ) { close(udpServerSocket); _logger.warn.ln("sending error..."); } diff --git a/src/resolver/include/Message.hpp b/src/resolver/include/Message.hpp index d5e9db9..2b78c4c 100644 --- a/src/resolver/include/Message.hpp +++ b/src/resolver/include/Message.hpp @@ -45,32 +45,34 @@ class Message bool getRA() const; bool getQR() const; + void setQR(bool flag); + 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. + 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 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 z : 3; //Zero: Three reserved bits set to zero. + unsigned _z : 3; //Zero: Three reserved bits set to zero. - unsigned rCode : 4; + unsigned _rCode : 4; - unsigned 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 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 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 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;