Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge in dnslib and fix setcap #103

Merged
merged 14 commits into from
Dec 7, 2018
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,18 @@ if(WIN32)
set(UTP_SRC ${UTP_SRC} libutp/libutp_inet_ntop.cpp)
endif()

set(DNSLIB_SRC
llarp/dns/message.cpp
llarp/dns/name.cpp
llarp/dns/question.cpp
llarp/dns/rr.cpp
llarp/dns/serialize.cpp
llarp/dns/server.cpp
)


set(LIB_SRC
${DNSLIB_SRC}
${UTP_SRC}
llarp/address_info.cpp
llarp/bencode.cpp
Expand Down Expand Up @@ -527,6 +537,7 @@ set(TEST_SRC
test/test_dns_unit.cpp
test/test_dnsc_unit.cpp
test/test_dnsd_unit.cpp
test/test_dnslib.cpp
test/test_llarp_queue.cpp
test/test_llarp_queue_manager.cpp
test/test_llarp_thread_pool.cpp
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ prefix = $(DESTDIR)/usr/local
CC ?= cc
CXX ?= c++

SETCAP ?= which setcap && setcap cap_net_bind_service,cap_net_admin=+eip
SETCAP ?= which setcap && setcap cap_net_admin,cap_net_bind_service=+eip

SHADOW_ROOT ?= $(HOME)/.shadow
SHADOW_BIN=$(SHADOW_ROOT)/bin/shadow
Expand Down
5 changes: 5 additions & 0 deletions include/llarp/dns/dns.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <llarp/dns/name.hpp>
#include <llarp/dns/rr.hpp>
#include <llarp/dns/serialize.hpp>
#include <llarp/dns/message.hpp>
#include <llarp/dns/server.hpp>
97 changes: 97 additions & 0 deletions include/llarp/dns/message.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#ifndef LLARP_DNS_MESSAGE_HPP
#define LLARP_DNS_MESSAGE_HPP
#include <llarp/dns/serialize.hpp>
#include <llarp/dns/rr.hpp>
#include <llarp/dns/question.hpp>

namespace llarp
{
namespace dns
{
using MsgID_t = uint16_t;
using Fields_t = uint16_t;
using Count_t = uint16_t;

struct MessageHeader : public Serialize
{
const static size_t Size = 12;

MessageHeader() = default;

MsgID_t id;
Fields_t fields;
Count_t qd_count;
Count_t an_count;
Count_t ns_count;
Count_t ar_count;

bool
Encode(llarp_buffer_t* buf) const override;

bool
Decode(llarp_buffer_t* buf) override;

bool
operator==(const MessageHeader& other) const
{
return id == other.id && fields == other.fields
&& qd_count == other.qd_count && an_count == other.an_count
&& ns_count == other.ns_count && ar_count == other.ar_count;
}
};

struct Message : public Serialize
{
Message(const MessageHeader& hdr);

Message(Message&& other);
Message(const Message& other);

void
UpdateHeader();

void
AddNXReply();

void
AddINReply(llarp::huint32_t addr);

void
AddAReply(std::string name);

bool
Encode(llarp_buffer_t* buf) const override;

bool
Decode(llarp_buffer_t* buf) override;

friend std::ostream&
operator<<(std::ostream& out, const Message& msg)
{
out << "[dns message id=" << std::hex << msg.hdr_id
<< " fields=" << msg.hdr_fields << " questions=[ ";
for(const auto& qd : msg.questions)
out << qd << ", ";
out << "] answers=[ ";
for(const auto& an : msg.answers)
out << an << ", ";
out << "] nameserver=[ ";
for(const auto& ns : msg.authorities)
out << ns << ", ";
out << "] additional=[ ";
for(const auto& ar : msg.additional)
out << ar << ", ";
return out << "]";
}

MsgID_t hdr_id;
Fields_t hdr_fields;
std::vector< Question > questions;
std::vector< ResourceRecord > answers;
std::vector< ResourceRecord > authorities;
std::vector< ResourceRecord > additional;
};
} // namespace dns
} // namespace llarp

#endif
28 changes: 28 additions & 0 deletions include/llarp/dns/name.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef LLARP_DNS_NAME_HPP
#define LLARP_DNS_NAME_HPP

#include <string>
#include <llarp/buffer.h>
#include <llarp/net_int.hpp>

namespace llarp
{
namespace dns
{
using Name_t = std::string;

/// decode name from buffer
bool
DecodeName(llarp_buffer_t* buf, Name_t& name);

/// encode name to buffer
bool
EncodeName(llarp_buffer_t* buf, const Name_t& name);

bool
DecodePTR(const Name_t& name, huint32_t& ip);

} // namespace dns
} // namespace llarp

#endif
4 changes: 4 additions & 0 deletions include/llarp/dns/query.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef LLARP_DNS_QUERY_HPP
#define LLARP_DNS_QUERY_HPP

#endif
46 changes: 46 additions & 0 deletions include/llarp/dns/question.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef LLARP_DNS_QUESTION_HPP
#define LLARP_DNS_QUESTION_HPP
#include <llarp/dns/serialize.hpp>
#include <llarp/dns/name.hpp>
#include <llarp/net_int.hpp>

namespace llarp
{
namespace dns
{
using QType_t = uint16_t;
using QClass_t = uint16_t;

struct Question : public Serialize
{
Question() = default;
Question(Question&& other);
Question(const Question& other);
bool
Encode(llarp_buffer_t* buf) const override;

bool
Decode(llarp_buffer_t* buf) override;

bool
operator==(const Question& other) const
{
return qname == other.qname && qtype == other.qtype
&& qclass == other.qclass;
}

friend std::ostream&
operator<<(std::ostream& out, const Question& q)
{
return out << "qname=" << q.qname << " qtype=" << (int)q.qtype
<< " qclass=" << (int)q.qclass;
}

Name_t qname;
QType_t qtype;
QClass_t qclass;
};
} // namespace dns
} // namespace llarp

#endif
47 changes: 47 additions & 0 deletions include/llarp/dns/rr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef LLARP_DNS_RR_HPP
#define LLARP_DNS_RR_HPP
#include <llarp/dns/name.hpp>
#include <llarp/dns/serialize.hpp>
#include <llarp/net_int.hpp>
#include <vector>
#include <memory>

namespace llarp
{
namespace dns
{
using RRClass_t = uint16_t;
using RRType_t = uint16_t;
using RR_RData_t = std::vector< byte_t >;
using RR_TTL_t = uint32_t;

struct ResourceRecord : public Serialize
{
ResourceRecord() = default;
ResourceRecord(const ResourceRecord& other);
ResourceRecord(ResourceRecord&& other);

bool
Encode(llarp_buffer_t* buf) const override;

bool
Decode(llarp_buffer_t* buf) override;

friend std::ostream&
operator<<(std::ostream& out, const ResourceRecord& rr)
{
return out << "[RR name=" << rr.rr_name << " type=" << rr.rr_type
<< " class=" << rr.rr_class << " ttl=" << rr.ttl
<< " rdata=[" << rr.rData.size() << " bytes]";
}

Name_t rr_name;
RRType_t rr_type;
RRClass_t rr_class;
RR_TTL_t ttl;
RR_RData_t rData;
};
} // namespace dns
} // namespace llarp

#endif
31 changes: 31 additions & 0 deletions include/llarp/dns/serialize.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef LLARP_DNS_SERIALIZE_HPP
#define LLARP_DNS_SERIALIZE_HPP
#include <llarp/buffer.h>
#include <vector>

namespace llarp
{
namespace dns
{
/// base type for serializable dns entities
struct Serialize
{
Copy link
Contributor

@sachaaaaa sachaaaaa Dec 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to add a virtual destructor for Serialize to compile this PR on my OSX box:

Suggested change
{
{
virtual
~Serialize() = default;

After that, this PR seems to fix my issue in #104 ! Yeay!

/// encode entity to buffer
virtual bool
Encode(llarp_buffer_t* buf) const = 0;

/// decode entity from buffer
virtual bool
Decode(llarp_buffer_t* buf) = 0;
};

bool
EncodeRData(llarp_buffer_t* buf, const std::vector< byte_t >& rdata);

bool
DecodeRData(llarp_buffer_t* buf, std::vector< byte_t >& rdata);

} // namespace dns
} // namespace llarp

#endif
Loading