mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
LookupServer: Split mDNS flags into separate field
This commit is contained in:
parent
233a9554a5
commit
6e70888315
6 changed files with 31 additions and 13 deletions
|
@ -9,12 +9,13 @@
|
||||||
|
|
||||||
namespace LookupServer {
|
namespace LookupServer {
|
||||||
|
|
||||||
DNSAnswer::DNSAnswer(const DNSName& name, u16 type, u16 class_code, u32 ttl, const String& record_data)
|
DNSAnswer::DNSAnswer(const DNSName& name, u16 type, u16 class_code, u32 ttl, const String& record_data, bool mdns_cache_flush)
|
||||||
: m_name(name)
|
: m_name(name)
|
||||||
, m_type(type)
|
, m_type(type)
|
||||||
, m_class_code(class_code)
|
, m_class_code(class_code)
|
||||||
, m_ttl(ttl)
|
, m_ttl(ttl)
|
||||||
, m_record_data(record_data)
|
, m_record_data(record_data)
|
||||||
|
, m_mdns_cache_flush(mdns_cache_flush)
|
||||||
{
|
{
|
||||||
auto now = time(nullptr);
|
auto now = time(nullptr);
|
||||||
m_expiration_time = now + m_ttl;
|
m_expiration_time = now + m_ttl;
|
||||||
|
|
|
@ -12,15 +12,19 @@
|
||||||
|
|
||||||
namespace LookupServer {
|
namespace LookupServer {
|
||||||
|
|
||||||
|
#define MDNS_CACHE_FLUSH 0x8000
|
||||||
|
|
||||||
class DNSAnswer {
|
class DNSAnswer {
|
||||||
public:
|
public:
|
||||||
DNSAnswer(const DNSName& name, u16 type, u16 class_code, u32 ttl, const String& record_data);
|
DNSAnswer(const DNSName& name, u16 type, u16 class_code, u32 ttl, const String& record_data, bool mdns_cache_flush);
|
||||||
|
|
||||||
const DNSName& name() const { return m_name; }
|
const DNSName& name() const { return m_name; }
|
||||||
u16 type() const { return m_type; }
|
u16 type() const { return m_type; }
|
||||||
u16 class_code() const { return m_class_code; }
|
u16 class_code() const { return m_class_code; }
|
||||||
|
u16 raw_class_code() const { return m_class_code | (m_mdns_cache_flush ? MDNS_CACHE_FLUSH : 0); }
|
||||||
u32 ttl() const { return m_ttl; }
|
u32 ttl() const { return m_ttl; }
|
||||||
const String& record_data() const { return m_record_data; }
|
const String& record_data() const { return m_record_data; }
|
||||||
|
bool mdns_cache_flush() const { return m_mdns_cache_flush; }
|
||||||
|
|
||||||
bool has_expired() const;
|
bool has_expired() const;
|
||||||
|
|
||||||
|
@ -31,6 +35,7 @@ private:
|
||||||
u32 m_ttl { 0 };
|
u32 m_ttl { 0 };
|
||||||
time_t m_expiration_time { 0 };
|
time_t m_expiration_time { 0 };
|
||||||
String m_record_data;
|
String m_record_data;
|
||||||
|
bool m_mdns_cache_flush { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,12 +56,12 @@ ByteBuffer DNSPacket::to_byte_buffer() const
|
||||||
for (auto& question : m_questions) {
|
for (auto& question : m_questions) {
|
||||||
stream << question.name();
|
stream << question.name();
|
||||||
stream << htons(question.record_type());
|
stream << htons(question.record_type());
|
||||||
stream << htons(question.class_code());
|
stream << htons(question.raw_class_code());
|
||||||
}
|
}
|
||||||
for (auto& answer : m_answers) {
|
for (auto& answer : m_answers) {
|
||||||
stream << answer.name();
|
stream << answer.name();
|
||||||
stream << htons(answer.type());
|
stream << htons(answer.type());
|
||||||
stream << htons(answer.class_code());
|
stream << htons(answer.raw_class_code());
|
||||||
stream << htonl(answer.ttl());
|
stream << htonl(answer.ttl());
|
||||||
if (answer.type() == T_PTR) {
|
if (answer.type() == T_PTR) {
|
||||||
DNSName name { answer.record_data() };
|
DNSName name { answer.record_data() };
|
||||||
|
@ -129,7 +129,9 @@ Optional<DNSPacket> DNSPacket::from_raw_packet(const u8* raw_data, size_t raw_si
|
||||||
NetworkOrdered<u16> class_code;
|
NetworkOrdered<u16> class_code;
|
||||||
};
|
};
|
||||||
auto& record_and_class = *(const RawDNSAnswerQuestion*)&raw_data[offset];
|
auto& record_and_class = *(const RawDNSAnswerQuestion*)&raw_data[offset];
|
||||||
packet.m_questions.empend(name, record_and_class.record_type, record_and_class.class_code);
|
u16 class_code = record_and_class.class_code & ~MDNS_WANTS_UNICAST_RESPONSE;
|
||||||
|
bool mdns_wants_unicast_response = record_and_class.class_code & MDNS_WANTS_UNICAST_RESPONSE;
|
||||||
|
packet.m_questions.empend(name, record_and_class.record_type, class_code, mdns_wants_unicast_response);
|
||||||
offset += 4;
|
offset += 4;
|
||||||
auto& question = packet.m_questions.last();
|
auto& question = packet.m_questions.last();
|
||||||
dbgln_if(LOOKUPSERVER_DEBUG, "Question #{}: name=_{}_, type={}, class={}", i, question.name(), question.record_type(), question.class_code());
|
dbgln_if(LOOKUPSERVER_DEBUG, "Question #{}: name=_{}_, type={}, class={}", i, question.name(), question.record_type(), question.class_code());
|
||||||
|
@ -154,7 +156,9 @@ Optional<DNSPacket> DNSPacket::from_raw_packet(const u8* raw_data, size_t raw_si
|
||||||
dbgln("data=(unimplemented record type {})", record.type());
|
dbgln("data=(unimplemented record type {})", record.type());
|
||||||
}
|
}
|
||||||
dbgln_if(LOOKUPSERVER_DEBUG, "Answer #{}: name=_{}_, type={}, ttl={}, length={}, data=_{}_", i, name, record.type(), record.ttl(), record.data_length(), data);
|
dbgln_if(LOOKUPSERVER_DEBUG, "Answer #{}: name=_{}_, type={}, ttl={}, length={}, data=_{}_", i, name, record.type(), record.ttl(), record.data_length(), data);
|
||||||
packet.m_answers.empend(name, record.type(), record.record_class(), record.ttl(), data);
|
u16 class_code = record.record_class() & ~MDNS_CACHE_FLUSH;
|
||||||
|
bool mdns_cache_flush = record.record_class() & MDNS_CACHE_FLUSH;
|
||||||
|
packet.m_answers.empend(name, record.type(), class_code, record.ttl(), data, mdns_cache_flush);
|
||||||
offset += record.data_length();
|
offset += record.data_length();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,23 +11,29 @@
|
||||||
|
|
||||||
namespace LookupServer {
|
namespace LookupServer {
|
||||||
|
|
||||||
|
#define MDNS_WANTS_UNICAST_RESPONSE 0x8000
|
||||||
|
|
||||||
class DNSQuestion {
|
class DNSQuestion {
|
||||||
public:
|
public:
|
||||||
DNSQuestion(const DNSName& name, u16 record_type, u16 class_code)
|
DNSQuestion(const DNSName& name, u16 record_type, u16 class_code, bool mdns_wants_unicast_response)
|
||||||
: m_name(name)
|
: m_name(name)
|
||||||
, m_record_type(record_type)
|
, m_record_type(record_type)
|
||||||
, m_class_code(class_code)
|
, m_class_code(class_code)
|
||||||
|
, m_mdns_wants_unicast_response(mdns_wants_unicast_response)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
u16 record_type() const { return m_record_type; }
|
u16 record_type() const { return m_record_type; }
|
||||||
u16 class_code() const { return m_class_code; }
|
u16 class_code() const { return m_class_code; }
|
||||||
|
u16 raw_class_code() const { return (u16)m_class_code | (m_mdns_wants_unicast_response ? MDNS_WANTS_UNICAST_RESPONSE : 0); }
|
||||||
const DNSName& name() const { return m_name; }
|
const DNSName& name() const { return m_name; }
|
||||||
|
bool mdns_wants_unicast_response() const { return m_mdns_wants_unicast_response; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DNSName m_name;
|
DNSName m_name;
|
||||||
u16 m_record_type { 0 };
|
u16 m_record_type { 0 };
|
||||||
u16 m_class_code { 0 };
|
u16 m_class_code { 0 };
|
||||||
|
bool m_mdns_wants_unicast_response { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ void LookupServer::load_etc_hosts()
|
||||||
m_etc_hosts.set(name, {});
|
m_etc_hosts.set(name, {});
|
||||||
it = m_etc_hosts.find(name);
|
it = m_etc_hosts.find(name);
|
||||||
}
|
}
|
||||||
it->value.empend(name, record_type, (u16)C_IN, static_ttl, data);
|
it->value.empend(name, record_type, (u16)C_IN, static_ttl, data, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
auto file = Core::File::construct("/etc/hosts");
|
auto file = Core::File::construct("/etc/hosts");
|
||||||
|
@ -123,7 +123,8 @@ Vector<DNSAnswer> LookupServer::lookup(const DNSName& name, unsigned short recor
|
||||||
answer.type(),
|
answer.type(),
|
||||||
answer.class_code(),
|
answer.class_code(),
|
||||||
answer.ttl(),
|
answer.ttl(),
|
||||||
answer.record_data()
|
answer.record_data(),
|
||||||
|
answer.mdns_cache_flush(),
|
||||||
};
|
};
|
||||||
answers.append(answer_with_original_case);
|
answers.append(answer_with_original_case);
|
||||||
};
|
};
|
||||||
|
@ -197,7 +198,7 @@ Vector<DNSAnswer> LookupServer::lookup(const DNSName& name, const String& namese
|
||||||
DNSName name_in_question = name;
|
DNSName name_in_question = name;
|
||||||
if (should_randomize_case == ShouldRandomizeCase::Yes)
|
if (should_randomize_case == ShouldRandomizeCase::Yes)
|
||||||
name_in_question.randomize_case();
|
name_in_question.randomize_case();
|
||||||
request.add_question({ name_in_question, record_type, C_IN });
|
request.add_question({ name_in_question, record_type, C_IN, false });
|
||||||
|
|
||||||
auto buffer = request.to_byte_buffer();
|
auto buffer = request.to_byte_buffer();
|
||||||
|
|
||||||
|
|
|
@ -88,9 +88,10 @@ void MulticastDNS::announce()
|
||||||
DNSAnswer answer {
|
DNSAnswer answer {
|
||||||
m_hostname,
|
m_hostname,
|
||||||
T_A,
|
T_A,
|
||||||
C_IN | 0x8000,
|
C_IN,
|
||||||
120,
|
120,
|
||||||
String { (const char*)&raw_addr, sizeof(raw_addr) }
|
String { (const char*)&raw_addr, sizeof(raw_addr) },
|
||||||
|
true,
|
||||||
};
|
};
|
||||||
response.add_answer(answer);
|
response.add_answer(answer);
|
||||||
}
|
}
|
||||||
|
@ -141,7 +142,7 @@ Vector<DNSAnswer> MulticastDNS::lookup(const DNSName& name, unsigned short recor
|
||||||
{
|
{
|
||||||
DNSPacket request;
|
DNSPacket request;
|
||||||
request.set_is_query();
|
request.set_is_query();
|
||||||
request.add_question({ name, record_type, C_IN });
|
request.add_question({ name, record_type, C_IN, false });
|
||||||
|
|
||||||
if (emit_packet(request) < 0) {
|
if (emit_packet(request) < 0) {
|
||||||
perror("failed to emit request packet");
|
perror("failed to emit request packet");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue