mostly working
parent
37be076642
commit
d4eaa5e1aa
Binary file not shown.
|
@ -1,66 +1,27 @@
|
||||||
# ninja log v5
|
# ninja log v5
|
||||||
2 323 1698190976428696830 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/string.cpp.o 9cf4bb76527ebff8
|
7 1281 1698258598109823604 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/string.cpp.o 2e3de5470fa27489
|
||||||
2 594 1698190976700701396 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/system.cpp.o 1adc5ac34f936d01
|
|
||||||
2 1649 1698214939939083215 CMakeFiles/insane_dns.dir/src/main.cpp.o e5cc6493a607c0c0
|
|
||||||
2 1338 1698190977440713822 libraries/BLT/CMakeFiles/BLT.dir/src/blt/profiling/profiler_v2.cpp.o 4b34d43980f81486
|
|
||||||
2 1234 1698190977336712076 libraries/BLT/CMakeFiles/BLT.dir/src/blt/profiling/profiler.cpp.o acd7791920b2ad2d
|
|
||||||
0 51 1698249238095921985 build.ninja 6eaded2198e1472a
|
|
||||||
2 404 1698190976508698172 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/format.cpp.o 924f830e8caafa45
|
|
||||||
3 1040 1698190977140708784 libraries/BLT/CMakeFiles/BLT.dir/src/blt/parse/argparse.cpp.o d9efc331e7323aad
|
|
||||||
630 837 1698211645218502034 libraries/BLT/libBLT.a 7458b833d4d035d7
|
|
||||||
2 1236 1698190977340712144 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o fe604df9ca9c4976
|
|
||||||
1649 1711 1698214940003084399 insane_dns 65fd02f0d52ca7a4
|
|
||||||
2 540 1698190976644700456 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/loader.cpp.o 9fde243767bd13f8
|
|
||||||
3 218 1698190976324695084 libraries/BLT/CMakeFiles/BLT.dir/src/blt/nbt/nbt_block.cpp.o ebfa2667e40ca05a
|
|
||||||
2 630 1698211645022498702 libraries/BLT/CMakeFiles/BLT.dir/src/blt/nbt/nbt.cpp.o abc003c343268699
|
|
||||||
2 313 1698211644706493328 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/filesystem.cpp.o 44105d2814a3e9f7
|
|
||||||
1 439 1698190976544698778 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/assert.cpp.o 7985d5c2c06fd22a
|
|
||||||
9 1267 1698249269912893790 libraries/BLT/CMakeFiles/BLT.dir/src/blt/nbt/nbt_block.cpp.o f9c9edddb799cd36
|
|
||||||
5 1734 1698249270376906050 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/filesystem.cpp.o 5171d83ac5d9e0dc
|
|
||||||
7 1918 1698249270564911008 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/string.cpp.o 2e3de5470fa27489
|
|
||||||
5 2141 1698249270784916812 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/format.cpp.o 9a4151eb8a13165e
|
|
||||||
4 2734 1698249271376932341 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/assert.cpp.o 2ae218d9777469fd
|
|
||||||
7 3066 1698249271708941021 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/system.cpp.o 85d21c49c2a9e587
|
7 3066 1698249271708941021 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/system.cpp.o 85d21c49c2a9e587
|
||||||
6 3078 1698249271720941335 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/loader.cpp.o 3bb9d28d25c10d3b
|
6 7601 1698262456928268539 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
||||||
8 3589 1698249272228954565 libraries/BLT/CMakeFiles/BLT.dir/src/blt/nbt/nbt.cpp.o 741b5851e86130d2
|
|
||||||
8 4299 1698249272936972914 libraries/BLT/CMakeFiles/BLT.dir/src/blt/profiling/profiler.cpp.o 66984105eaba6834
|
|
||||||
6 4555 1698249273192979506 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o 9f9b2e297e48c5fc
|
|
||||||
10 4728 1698249273360983822 libraries/BLT/CMakeFiles/BLT.dir/src/blt/parse/argparse.cpp.o 30865d3aadfb703
|
|
||||||
8 4855 1698249273492987213 libraries/BLT/CMakeFiles/BLT.dir/src/blt/profiling/profiler_v2.cpp.o 4f10bd0279063873
|
8 4855 1698249273492987213 libraries/BLT/CMakeFiles/BLT.dir/src/blt/profiling/profiler_v2.cpp.o 4f10bd0279063873
|
||||||
4855 5356 1698249273972999544 libraries/BLT/libBLT.a f5b601d9b774b003
|
8 4299 1698249272936972914 libraries/BLT/CMakeFiles/BLT.dir/src/blt/profiling/profiler.cpp.o 66984105eaba6834
|
||||||
4 7344 1698249275977050365 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
0 51 1698249238095921985 build.ninja 6eaded2198e1472a
|
||||||
7347 7539 1698249276181055475 insane_dns ff5ae500893d0be1
|
5 2141 1698249270784916812 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/format.cpp.o 9a4151eb8a13165e
|
||||||
6 6332 1698249537883728094 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
8 4211 1698258601029790203 libraries/BLT/CMakeFiles/BLT.dir/src/blt/parse/argparse.cpp.o 30865d3aadfb703
|
||||||
6332 6512 1698249538071717471 insane_dns ff5ae500893d0be1
|
4211 4615 1698258601425785673 libraries/BLT/libBLT.a f5b601d9b774b003
|
||||||
6 6314 1698249574381777062 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
6 4555 1698249273192979506 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/logging.cpp.o 9f9b2e297e48c5fc
|
||||||
6314 6494 1698249574569767564 insane_dns ff5ae500893d0be1
|
7602 7805 1698262457144266176 insane_dns ff5ae500893d0be1
|
||||||
6 7029 1698249827056586034 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
7 2209 1698258599037812988 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/loader.cpp.o 3bb9d28d25c10d3b
|
||||||
7029 7226 1698249827260580703 insane_dns ff5ae500893d0be1
|
9 1267 1698249269912893790 libraries/BLT/CMakeFiles/BLT.dir/src/blt/nbt/nbt_block.cpp.o f9c9edddb799cd36
|
||||||
6 6742 1698249850299992818 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
8 3589 1698249272228954565 libraries/BLT/CMakeFiles/BLT.dir/src/blt/nbt/nbt.cpp.o 741b5851e86130d2
|
||||||
6743 6931 1698249850495987939 insane_dns ff5ae500893d0be1
|
5 1734 1698249270376906050 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/filesystem.cpp.o 5171d83ac5d9e0dc
|
||||||
6 6821 1698250206165290821 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
6 1879 1698258598709816741 libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/assert.cpp.o 2ae218d9777469fd
|
||||||
6821 7006 1698250206361287883 insane_dns ff5ae500893d0be1
|
6 7584 1698262505119741436 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
||||||
6 6430 1698250305739854042 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
7584 7783 1698262505327739162 insane_dns ff5ae500893d0be1
|
||||||
6430 6612 1698250305931851366 insane_dns ff5ae500893d0be1
|
6 7676 1698262556199182765 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
||||||
6 6584 1698250326515566357 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
7676 7877 1698262556411180445 insane_dns ff5ae500893d0be1
|
||||||
6584 6765 1698250326703563769 insane_dns ff5ae500893d0be1
|
6 7686 1698262781216614131 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
||||||
6 6285 1698250344387321662 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
7686 7888 1698262781428610853 insane_dns ff5ae500893d0be1
|
||||||
6285 6468 1698250344575319100 insane_dns ff5ae500893d0be1
|
6 7588 1698262928722408382 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
||||||
6 6412 1698250629225987485 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
7588 7791 1698262928938405252 insane_dns ff5ae500893d0be1
|
||||||
6412 6560 1698250629381987307 insane_dns ff5ae500893d0be1
|
5 7595 1698262972765775585 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
||||||
6 6598 1698250688537890288 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
7595 7803 1698262972985772451 insane_dns ff5ae500893d0be1
|
||||||
6598 6802 1698250688749889838 insane_dns ff5ae500893d0be1
|
|
||||||
6 6326 1698250712265835606 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
|
||||||
6327 6508 1698250712453835139 insane_dns ff5ae500893d0be1
|
|
||||||
6 6699 1698252945195055615 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
|
||||||
6699 6913 1698252945419054797 insane_dns ff5ae500893d0be1
|
|
||||||
7 6575 1698253015654777760 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
|
||||||
6575 6755 1698253015842776966 insane_dns ff5ae500893d0be1
|
|
||||||
6 6770 1698253192529923549 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
|
||||||
6771 6955 1698253192721922518 insane_dns ff5ae500893d0be1
|
|
||||||
6 6664 1698253327185155757 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
|
||||||
6664 6846 1698253327373154627 insane_dns ff5ae500893d0be1
|
|
||||||
6 6659 1698253454672358671 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
|
||||||
6659 6842 1698253454860357454 insane_dns ff5ae500893d0be1
|
|
||||||
6 6394 1698253480540190047 CMakeFiles/insane_dns.dir/src/main.cpp.o 727da43cdbc82421
|
|
||||||
6394 6575 1698253480728188812 insane_dns ff5ae500893d0be1
|
|
||||||
|
|
Binary file not shown.
|
@ -1,3 +1,3 @@
|
||||||
Start testing: Oct 25 13:04 EDT
|
Start testing: Oct 25 15:42 EDT
|
||||||
----------------------------------------------------------
|
----------------------------------------------------------
|
||||||
End testing: Oct 25 13:04 EDT
|
End testing: Oct 25 15:42 EDT
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
43
src/ip.h
43
src/ip.h
|
@ -5,4 +5,47 @@
|
||||||
#ifndef INSANE_DNS_IP_H
|
#ifndef INSANE_DNS_IP_H
|
||||||
#define INSANE_DNS_IP_H
|
#define INSANE_DNS_IP_H
|
||||||
|
|
||||||
|
#include <blt/std/string.h>
|
||||||
|
#include <blt/std/assert.h>
|
||||||
|
#include <asio.hpp>
|
||||||
|
|
||||||
|
using asio::ip::udp;
|
||||||
|
using asio::ip::tcp;
|
||||||
|
|
||||||
|
struct IPAddress
|
||||||
|
{
|
||||||
|
unsigned char octets[4];
|
||||||
|
|
||||||
|
constexpr IPAddress(const std::string& str)
|
||||||
|
{
|
||||||
|
auto data = blt::string::split(str, '.');
|
||||||
|
BLT_ASSERT(data.size() == 4);
|
||||||
|
for (size_t i = 0; i < data.size(); i++)
|
||||||
|
data[i] = static_cast<unsigned char>(std::stoul(data[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr IPAddress(unsigned char oct[4])
|
||||||
|
{
|
||||||
|
// ugly sin
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
octets[i] = oct[i];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename IN, typename OUT>
|
||||||
|
void sendUDPMessage(const std::string& host, const IN& in, size_t in_size, OUT& out, size_t& out_size)
|
||||||
|
{
|
||||||
|
asio::io_context io_context;
|
||||||
|
udp::resolver resolver(io_context);
|
||||||
|
udp::endpoint receiver_endpoint(asio::ip::address::from_string(host), 53);
|
||||||
|
|
||||||
|
udp::socket socket(io_context);
|
||||||
|
socket.open(udp::v4());
|
||||||
|
|
||||||
|
socket.send_to(asio::const_buffers_1(in, in_size), receiver_endpoint);
|
||||||
|
|
||||||
|
udp::endpoint sender_endpoint;
|
||||||
|
out_size = socket.receive_from(asio::buffer(out), sender_endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
#endif //INSANE_DNS_IP_H
|
#endif //INSANE_DNS_IP_H
|
||||||
|
|
359
src/main.cpp
359
src/main.cpp
|
@ -13,9 +13,29 @@
|
||||||
#include <iomanip> // This might be necessary
|
#include <iomanip> // This might be necessary
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <unordered_set>
|
||||||
|
#include "ip.h"
|
||||||
|
|
||||||
using asio::ip::udp;
|
/**
|
||||||
using asio::ip::tcp;
|
* ----------------------------
|
||||||
|
* | CONFIG |
|
||||||
|
* ----------------------------
|
||||||
|
*/
|
||||||
|
// should we strictly match results? ie block *.wikipedia.org or just wikipedia.org?
|
||||||
|
static constexpr bool STRICT_MATCHING = false;
|
||||||
|
// true -> only match A records ; false -> match any named record (A, AAAA, CNAME)
|
||||||
|
static constexpr bool STRICT_FILTERING = false;
|
||||||
|
|
||||||
|
// replacement IP address. Make sure this is a 4 octet string seperated by .
|
||||||
|
static inline constexpr IPAddress REPLACEMENT_IP()
|
||||||
|
{
|
||||||
|
return {"139.57.100.6"};
|
||||||
|
}
|
||||||
|
|
||||||
|
static const std::unordered_set<std::string> DISALLOWED_DOMAINS{
|
||||||
|
"en.wikipedia.org",
|
||||||
|
"zombo.com"
|
||||||
|
};
|
||||||
|
|
||||||
// DNS data contains:
|
// DNS data contains:
|
||||||
// 2 bytes for transaction id
|
// 2 bytes for transaction id
|
||||||
|
@ -40,54 +60,6 @@ using asio::ip::tcp;
|
||||||
// 2 byte for length of data
|
// 2 byte for length of data
|
||||||
// (lengthy) byte for data
|
// (lengthy) byte for data
|
||||||
|
|
||||||
class send_buffer
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
mutable std::array<unsigned char, 65535> internal_data{};
|
|
||||||
mutable size_t write_index = 0;
|
|
||||||
public:
|
|
||||||
send_buffer() = default;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void write(const T& t) const
|
|
||||||
{
|
|
||||||
if constexpr (std::is_arithmetic_v<T>)
|
|
||||||
{
|
|
||||||
blt::mem::toBytes(t, &internal_data[write_index]);
|
|
||||||
write_index += sizeof(T);
|
|
||||||
} else if constexpr (std::is_same_v<T, std::string>)
|
|
||||||
{
|
|
||||||
std::memcpy(&t[0], t.size());
|
|
||||||
write('\0');
|
|
||||||
} else
|
|
||||||
static_assert("Data type not supported!");
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset() const
|
|
||||||
{
|
|
||||||
write_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] unsigned char operator[](size_t i){
|
|
||||||
return internal_data[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] unsigned char* data()
|
|
||||||
{
|
|
||||||
return internal_data.data();
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] size_t size() const
|
|
||||||
{
|
|
||||||
return write_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto buffer() const
|
|
||||||
{
|
|
||||||
return asio::buffer(internal_data.data(), size());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class byte_reader
|
class byte_reader
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
@ -127,12 +99,27 @@ class byte_reader
|
||||||
{
|
{
|
||||||
current_byte += s;
|
current_byte += s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline size_t last() const
|
||||||
|
{
|
||||||
|
return current_byte;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline unsigned char* from()
|
||||||
|
{
|
||||||
|
return &data[current_byte];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class send_buffer;
|
||||||
|
|
||||||
class question
|
class question
|
||||||
{
|
{
|
||||||
|
friend send_buffer;
|
||||||
private:
|
private:
|
||||||
std::string domain;
|
std::string domain;
|
||||||
|
uint16_t QTYPE;
|
||||||
|
uint16_t QCLASS;
|
||||||
public:
|
public:
|
||||||
explicit question(const byte_reader& reader)
|
explicit question(const byte_reader& reader)
|
||||||
{
|
{
|
||||||
|
@ -148,7 +135,9 @@ class question
|
||||||
domain += static_cast<char>(reader.next());
|
domain += static_cast<char>(reader.next());
|
||||||
}
|
}
|
||||||
// Skip QTYPE and QCLASS
|
// Skip QTYPE and QCLASS
|
||||||
reader.skip(4);
|
// but keep a copy of it for future writing
|
||||||
|
reader.to(QTYPE);
|
||||||
|
reader.to(QCLASS);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& operator()()
|
const std::string& operator()()
|
||||||
|
@ -159,6 +148,7 @@ class question
|
||||||
|
|
||||||
class answer
|
class answer
|
||||||
{
|
{
|
||||||
|
friend send_buffer;
|
||||||
private:
|
private:
|
||||||
uint16_t NAME = 0;
|
uint16_t NAME = 0;
|
||||||
uint16_t TYPE = 0;
|
uint16_t TYPE = 0;
|
||||||
|
@ -180,159 +170,186 @@ class answer
|
||||||
BLT_TRACE("%d, %d, %d, %d, %d", NAME, TYPE, CLASS, TTL, RDLENGTH);
|
BLT_TRACE("%d, %d, %d, %d, %d", NAME, TYPE, CLASS, TTL, RDLENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
// rule of 3
|
void substitute(const IPAddress& addr)
|
||||||
|
{
|
||||||
|
BLT_ASSERT(RDLENGTH == 4);
|
||||||
|
std::memcpy(RDATA, addr.octets, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// rule of 5
|
||||||
answer(const answer& answer) = delete;
|
answer(const answer& answer) = delete;
|
||||||
|
|
||||||
answer& operator=(const answer& answer) = delete;
|
answer& operator=(const answer& answer) = delete;
|
||||||
|
|
||||||
|
answer(answer&& move)
|
||||||
|
{
|
||||||
|
NAME = move.NAME;
|
||||||
|
TYPE = move.TYPE;
|
||||||
|
CLASS = move.CLASS;
|
||||||
|
TTL = move.TTL;
|
||||||
|
RDLENGTH = move.RDLENGTH;
|
||||||
|
RDATA = move.RDATA;
|
||||||
|
move.RDATA = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
answer& operator=(answer&& move)
|
||||||
|
{
|
||||||
|
NAME = move.NAME;
|
||||||
|
TYPE = move.TYPE;
|
||||||
|
CLASS = move.CLASS;
|
||||||
|
TTL = move.TTL;
|
||||||
|
RDLENGTH = move.RDLENGTH;
|
||||||
|
RDATA = move.RDATA;
|
||||||
|
move.RDATA = nullptr;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
~answer()
|
~answer()
|
||||||
{
|
{
|
||||||
delete[] RDATA;
|
delete[] RDATA;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename IN, typename OUT>
|
class send_buffer
|
||||||
void sendUDPMessage(const std::string& host, const IN& in, size_t in_size, OUT& out, size_t& out_size)
|
|
||||||
{
|
{
|
||||||
asio::io_context io_context;
|
|
||||||
udp::resolver resolver(io_context);
|
|
||||||
udp::endpoint receiver_endpoint(asio::ip::address::from_string(host), 53);
|
|
||||||
|
|
||||||
udp::socket socket(io_context);
|
|
||||||
socket.open(udp::v4());
|
|
||||||
|
|
||||||
socket.send_to(asio::const_buffers_1(in, in_size), receiver_endpoint);
|
|
||||||
|
|
||||||
udp::endpoint sender_endpoint;
|
|
||||||
out_size = socket.receive_from(asio::buffer(out), sender_endpoint);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct udp_server
|
|
||||||
{
|
|
||||||
explicit udp_server(asio::ip::udp::socket socket)
|
|
||||||
: local_socket(std::move(socket))
|
|
||||||
{
|
|
||||||
read();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handle_receive(asio::ip::udp::endpoint ep, std::size_t length)
|
mutable std::array<unsigned char, 65535> internal_data{};
|
||||||
|
mutable size_t write_index = 0;
|
||||||
|
public:
|
||||||
|
send_buffer() = default;
|
||||||
|
|
||||||
|
void write(unsigned char* data, size_t size) const
|
||||||
{
|
{
|
||||||
// get the number of questions
|
BLT_TRACE(size);
|
||||||
uint16_t questions; // yes I made this part of my library just for this :3
|
std::memcpy(&internal_data[write_index], data, size);
|
||||||
blt::mem::fromBytes(&recv_buf.data()[4], questions); // i hate little endian
|
write_index += size;
|
||||||
|
}
|
||||||
// the reader starts after the header to make things easier
|
|
||||||
byte_reader reader(recv_buf.data());
|
template<typename T>
|
||||||
|
void write(const T& t) const
|
||||||
BLT_INFO("Bytes received: %d with %d questions", length, questions);
|
{
|
||||||
|
if constexpr (std::is_arithmetic_v<T>)
|
||||||
// no one actually does multiple questions. trying to do it in dig is not easy
|
|
||||||
// and the standard isn't really designed for this (how do we handle if one question errors but the other doesn't? there is only
|
|
||||||
// one return code.)
|
|
||||||
for (uint16_t i = 0; i < questions; i++)
|
|
||||||
{
|
{
|
||||||
question q(reader);
|
blt::mem::toBytes(t, &internal_data[write_index]);
|
||||||
BLT_INFO("DOMAIN: %s", q().c_str());
|
write_index += sizeof(T);
|
||||||
}
|
} else if constexpr (std::is_same_v<T, std::string>)
|
||||||
|
{
|
||||||
//write(ep);
|
static_assert("No");
|
||||||
|
// std::memcpy(&internal_data[write_index], &t[0], t.length());
|
||||||
|
// write_index += t.length();
|
||||||
|
// write('\0');
|
||||||
|
} else if constexpr (std::is_same_v<T, answer>)
|
||||||
|
{
|
||||||
|
write(t.NAME);
|
||||||
|
write(t.TYPE);
|
||||||
|
write(t.CLASS);
|
||||||
|
write(t.TTL);
|
||||||
|
write(t.RDLENGTH);
|
||||||
|
std::memcpy(&internal_data[write_index], t.RDATA, t.RDLENGTH);
|
||||||
|
write_index += t.RDLENGTH;
|
||||||
|
} else if constexpr (std::is_same_v<T, question>)
|
||||||
|
{
|
||||||
|
auto labels = blt::string::split(t.domain, '.');
|
||||||
|
for (const auto& label : labels)
|
||||||
|
{
|
||||||
|
auto length = static_cast<uint8_t>(label.length());
|
||||||
|
write(length);
|
||||||
|
std::memcpy(&internal_data[write_index], &label[0], length);
|
||||||
|
write_index += length;
|
||||||
|
}
|
||||||
|
// write length of 0 to signal end of labels
|
||||||
|
write('\0');
|
||||||
|
write(t.QTYPE);
|
||||||
|
write(t.QCLASS);
|
||||||
|
} else
|
||||||
|
static_assert("Data type not supported!");
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_write()
|
void reset() const
|
||||||
{
|
{
|
||||||
|
write_index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void read()
|
[[nodiscard]] unsigned char operator[](size_t i)
|
||||||
{
|
{
|
||||||
local_socket.async_receive_from(asio::buffer(recv_buf), remote_connection, [this](asio::error_code ec, std::size_t length) {
|
return internal_data[i];
|
||||||
if (ec)
|
|
||||||
return;
|
|
||||||
this->handle_receive(remote_connection, length);
|
|
||||||
this->read();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void write(const asio::ip::udp::endpoint& remote)
|
[[nodiscard]] unsigned char* data()
|
||||||
{
|
{
|
||||||
std::thread th([&, this]() -> void {
|
return internal_data.data();
|
||||||
handle_write();
|
|
||||||
local_socket.async_send_to(send_buf.buffer(), remote, [this](asio::error_code ec, std::size_t length) {
|
|
||||||
if (ec)
|
|
||||||
return;
|
|
||||||
send_buf.reset();
|
|
||||||
this->read();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
asio::ip::udp::socket local_socket;
|
[[nodiscard]] size_t size() const
|
||||||
asio::ip::udp::endpoint remote_connection;
|
{
|
||||||
std::array<unsigned char, 65565> recv_buf{};
|
return write_index;
|
||||||
send_buffer send_buf{};
|
}
|
||||||
|
|
||||||
|
auto buffer() const
|
||||||
|
{
|
||||||
|
return asio::buffer(internal_data.data(), size());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// asio::io_context io_context;
|
|
||||||
//
|
|
||||||
// udp::socket socket(io_context, udp::endpoint(udp::v6(), 5555));
|
|
||||||
//
|
|
||||||
// while (true)
|
|
||||||
// {
|
|
||||||
// std::array<unsigned char, 65535> recv_buf{};
|
|
||||||
// std::array<unsigned char, 65535> mod_recv_buf{};
|
|
||||||
// udp::endpoint remote_endpoint;
|
|
||||||
// size_t bytes = socket.receive_from(asio::buffer(recv_buf), remote_endpoint);
|
|
||||||
//
|
|
||||||
// // get the number of questions
|
|
||||||
// uint16_t questions; // yes I made this part of my library just for this :3
|
|
||||||
// blt::mem::fromBytes(&recv_buf[4], questions); // i hate little endian
|
|
||||||
//
|
|
||||||
// // the reader starts after the header to make things easier
|
|
||||||
// byte_reader reader(recv_buf.data());
|
|
||||||
//
|
|
||||||
// BLT_INFO("Bytes received: %d with %d questions", bytes, questions);
|
|
||||||
//
|
|
||||||
// // no one actually does multiple questions. trying to do it in dig is not easy
|
|
||||||
// // and the standard isn't really designed for this (how do we handle if one question errors but the other doesn't? there is only
|
|
||||||
// // one return code.)
|
|
||||||
// for (uint16_t i = 0; i < questions; i++)
|
|
||||||
// {
|
|
||||||
// question q(reader);
|
|
||||||
// BLT_INFO("DOMAIN: %s", q().c_str());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// size_t out_bytes;
|
|
||||||
// sendUDPMessage("8.8.8.8", recv_buf.data(), bytes, mod_recv_buf, out_bytes);
|
|
||||||
//
|
|
||||||
// byte_reader reader2(mod_recv_buf.data());
|
|
||||||
// question q(reader2);
|
|
||||||
// answer a(reader2);
|
|
||||||
//
|
|
||||||
// asio::error_code ignored_error;
|
|
||||||
// socket.send_to(asio::const_buffers_1(mod_recv_buf.data(), out_bytes), remote_endpoint, 0, ignored_error);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// catch (std::exception& e)
|
|
||||||
// {
|
|
||||||
// std::cerr << e.what() << std::endl;
|
|
||||||
// }
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
asio::io_context io_context;
|
asio::io_context io_context;
|
||||||
|
|
||||||
asio::ip::udp::endpoint ep(asio::ip::udp::v6(), 5555); // also listens on ipv4
|
udp::socket socket(io_context, udp::endpoint(udp::v6(), 5555));
|
||||||
asio::ip::udp::socket sock(io_context, ep);
|
|
||||||
udp_server server(std::move(sock));
|
std::array<unsigned char, 65535> recv_buf{};
|
||||||
io_context.run();
|
std::array<unsigned char, 65535> mod_recv_buf{};
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
udp::endpoint remote_endpoint;
|
||||||
|
size_t bytes = socket.receive_from(asio::buffer(recv_buf), remote_endpoint);
|
||||||
|
|
||||||
|
// get the number of questions
|
||||||
|
uint16_t questions; // yes I made this part of my library just for this :3
|
||||||
|
blt::mem::fromBytes(&recv_buf[4], questions); // i hate little endian
|
||||||
|
|
||||||
|
BLT_INFO("Bytes received: %d with %d questions", bytes, questions);
|
||||||
|
|
||||||
|
// forward to google.
|
||||||
|
size_t out_bytes;
|
||||||
|
sendUDPMessage("8.8.8.8", recv_buf.data(), bytes, mod_recv_buf, out_bytes);
|
||||||
|
|
||||||
|
uint16_t num_of_answers;
|
||||||
|
blt::mem::fromBytes(&mod_recv_buf[6], num_of_answers);
|
||||||
|
|
||||||
|
byte_reader reader2(mod_recv_buf.data());
|
||||||
|
|
||||||
|
BLT_INFO("Bytes answered %d with %d answers", out_bytes, num_of_answers);
|
||||||
|
|
||||||
|
// no one actually does multiple questions. trying to do it in dig is not easy
|
||||||
|
// and the standard isn't really designed for this (how do we handle if one question errors but the other doesn't? there is only
|
||||||
|
// one return code.)
|
||||||
|
question q(reader2);
|
||||||
|
std::vector<answer> answers;
|
||||||
|
for (int i = 0; i < num_of_answers; i++)
|
||||||
|
{
|
||||||
|
answer a(reader2);
|
||||||
|
answers.push_back(std::move(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
BLT_INFO("DOMAIN: %s", q().c_str());
|
||||||
|
for (auto& a : answers)
|
||||||
|
a.substitute(REPLACEMENT_IP());
|
||||||
|
|
||||||
|
send_buffer send;
|
||||||
|
send.write(mod_recv_buf.data(), 12);
|
||||||
|
send.write(q);
|
||||||
|
for (const answer& a : answers)
|
||||||
|
send.write(a);
|
||||||
|
BLT_TRACE("%d - %d = %d", out_bytes, reader2.last(), out_bytes - reader2.last());
|
||||||
|
send.write(reader2.from(), out_bytes - reader2.last());
|
||||||
|
|
||||||
|
asio::error_code ignored_error;
|
||||||
|
socket.send_to(send.buffer(), remote_endpoint, 0, ignored_error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (std::exception& e)
|
catch (std::exception& e)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue