Skip to content

Commit

Permalink
LibDNS: Add IPC encoder/decoder for the DNSAnswer class
Browse files Browse the repository at this point in the history
  • Loading branch information
tomuta authored and linusg committed Apr 15, 2022
1 parent f3af825 commit a3a1fe8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Userland/Libraries/LibDNS/DNSAnswer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "DNSAnswer.h"
#include <AK/Stream.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <time.h>

namespace DNS {
Expand Down Expand Up @@ -95,3 +97,30 @@ ErrorOr<void> AK::Formatter<DNS::DNSRecordClass>::format(AK::FormatBuilder& buil
TRY(builder.put_u64((u16)value));
return {};
}

namespace IPC {

bool encode(Encoder& encoder, DNS::DNSAnswer const& answer)
{
encoder << answer.name().as_string() << (u16)answer.type() << (u16)answer.class_code() << answer.ttl() << answer.record_data() << answer.mdns_cache_flush();
return true;
}

ErrorOr<void> decode(Decoder& decoder, DNS::DNSAnswer& answer)
{
String name;
TRY(decoder.decode(name));
u16 record_type, class_code;
TRY(decoder.decode(record_type));
TRY(decoder.decode(class_code));
u32 ttl;
TRY(decoder.decode(ttl));
String record_data;
TRY(decoder.decode(record_data));
bool cache_flush;
TRY(decoder.decode(cache_flush));
answer = { { name }, (DNS::DNSRecordType)record_type, (DNS::DNSRecordClass)class_code, ttl, record_data, cache_flush };
return {};
}

}
8 changes: 8 additions & 0 deletions Userland/Libraries/LibDNS/DNSAnswer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <AK/String.h>
#include <AK/Traits.h>
#include <AK/Types.h>
#include <LibIPC/Forward.h>

namespace DNS {

Expand Down Expand Up @@ -90,3 +91,10 @@ struct AK::Formatter<DNS::DNSRecordClass> : StandardFormatter {

ErrorOr<void> format(AK::FormatBuilder&, DNS::DNSRecordClass);
};

namespace IPC {

bool encode(Encoder&, DNS::DNSAnswer const&);
ErrorOr<void> decode(Decoder&, DNS::DNSAnswer&);

}

0 comments on commit a3a1fe8

Please sign in to comment.