mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-04-21 14:29:10 +00:00
remove raw dns parse code
This commit is contained in:
parent
30d7217374
commit
5bb4bbf022
1 changed files with 0 additions and 140 deletions
140
src/virtdns.rs
140
src/virtdns.rs
|
@ -44,7 +44,6 @@ impl VirtualDns {
|
|||
VirtualDns::default()
|
||||
}
|
||||
|
||||
// /*
|
||||
pub fn receive_query(&mut self, data: &[u8]) -> Result<Vec<u8>> {
|
||||
use crate::dns;
|
||||
let message = dns::parse_data_to_dns_message(data, false)?;
|
||||
|
@ -53,105 +52,7 @@ impl VirtualDns {
|
|||
let message = dns::build_dns_response(message, &qname, ip, 5)?;
|
||||
Ok(message.to_vec()?)
|
||||
}
|
||||
// */
|
||||
/*
|
||||
pub fn receive_query(&mut self, data: &[u8]) -> Result<Vec<u8>> {
|
||||
#[derive(Eq, PartialEq, Debug)]
|
||||
#[allow(dead_code, clippy::upper_case_acronyms)]
|
||||
enum DnsRecordType {
|
||||
A = 1,
|
||||
AAAA = 28,
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Debug)]
|
||||
#[allow(dead_code)]
|
||||
enum DnsClass {
|
||||
IN = 1,
|
||||
}
|
||||
|
||||
if data.len() < 17 {
|
||||
return Err("Invalid DNS query".into());
|
||||
}
|
||||
// bit 1: Message is a query (0)
|
||||
// bits 2 - 5: Standard query opcode (0)
|
||||
// bit 6: Unused
|
||||
// bit 7: Message is not truncated (0)
|
||||
// bit 8: Recursion desired (1)
|
||||
let is_supported_query = (data[2] & 0b11111011) == 0b00000001;
|
||||
let num_queries = u16::from_be_bytes(data[4..6].try_into()?);
|
||||
if !is_supported_query || num_queries != 1 {
|
||||
return Err("Invalid DNS query".into());
|
||||
}
|
||||
|
||||
let (qname, offset) = VirtualDns::parse_qname(data, 12).ok_or("parse_qname")?;
|
||||
if offset + 3 >= data.len() {
|
||||
return Err("Invalid DNS query".into());
|
||||
}
|
||||
let qtype = u16::from_be_bytes(data[offset..offset + 2].try_into()?);
|
||||
let qclass = u16::from_be_bytes(data[offset + 2..offset + 4].try_into()?);
|
||||
|
||||
if qtype != DnsRecordType::A as u16 && qtype != DnsRecordType::AAAA as u16
|
||||
|| qclass != DnsClass::IN as u16
|
||||
{
|
||||
return Err("Invalid DNS query".into());
|
||||
}
|
||||
|
||||
if qtype == DnsRecordType::A as u16 {
|
||||
log::info!("DNS query: {}", qname);
|
||||
}
|
||||
|
||||
let mut response = Vec::<u8>::new();
|
||||
response.extend(&data[0..offset + 4]);
|
||||
response[2] |= 0x80; // Message is a response
|
||||
response[3] |= 0x80; // Recursion available
|
||||
|
||||
// Record count of the answer section:
|
||||
// We only send an answer record for A queries, assuming that IPv4 is supported everywhere.
|
||||
// This way, we do not have to handle two IP spaces for the virtual DNS feature.
|
||||
response[6] = 0;
|
||||
response[7] = if qtype == DnsRecordType::A as u16 {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
// Zero count of other sections:
|
||||
// authority section
|
||||
response[8] = 0;
|
||||
response[9] = 0;
|
||||
|
||||
const DNS_TTL: u8 = 30; // TTL in DNS replies in seconds
|
||||
|
||||
// additional section
|
||||
response[10] = 0;
|
||||
response[11] = 0;
|
||||
if qtype == DnsRecordType::A as u16 {
|
||||
if let Ok(ip) = self.allocate_ip(qname) {
|
||||
response.extend(&[
|
||||
0xc0, 0x0c, // Question name pointer
|
||||
0, 1, // Record type: A
|
||||
0, 1, // Class: IN
|
||||
0, 0, 0, DNS_TTL, // TTL
|
||||
0, 4, // Data length: 4 bytes
|
||||
]);
|
||||
match ip {
|
||||
IpAddr::V4(ip) => response.extend(ip.octets().as_ref()),
|
||||
IpAddr::V6(ip) => response.extend(ip.octets().as_ref()),
|
||||
};
|
||||
} else {
|
||||
log::error!("Virtual IP space for DNS exhausted");
|
||||
response[7] = 0; // No answers
|
||||
|
||||
// Set rcode to SERVFAIL
|
||||
response[3] &= 0xf0;
|
||||
response[3] |= 2;
|
||||
}
|
||||
} else {
|
||||
response[7] = 0; // No answers
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
// */
|
||||
fn increment_ip(addr: IpAddr) -> Result<IpAddr> {
|
||||
let mut ip_bytes = match addr as IpAddr {
|
||||
IpAddr::V4(ip) => Vec::<u8>::from(ip.octets()),
|
||||
|
@ -238,45 +139,4 @@ impl VirtualDns {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
/// Parse a non-root DNS qname at a specific offset and return the name along with its size.
|
||||
/// DNS packet parsing should be continued after the name.
|
||||
fn parse_qname(data: &[u8], mut offset: usize) -> Option<(String, usize)> {
|
||||
// Since we only parse qnames and qnames can't point anywhere,
|
||||
// we do not support pointers. (0xC0 is a bitmask for pointer detection.)
|
||||
let label_type = data[offset] & 0xC0;
|
||||
if label_type != 0x00 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut qname = String::from("");
|
||||
loop {
|
||||
if offset >= data.len() {
|
||||
return None;
|
||||
}
|
||||
let label_len = data[offset];
|
||||
if label_len == 0 {
|
||||
if qname.is_empty() {
|
||||
return None;
|
||||
}
|
||||
offset += 1;
|
||||
break;
|
||||
}
|
||||
if !qname.is_empty() {
|
||||
qname.push('.');
|
||||
}
|
||||
for _ in 0..label_len {
|
||||
offset += 1;
|
||||
if offset >= data.len() {
|
||||
return None;
|
||||
}
|
||||
qname.push(data[offset] as char);
|
||||
}
|
||||
offset += 1;
|
||||
}
|
||||
|
||||
Some((qname, offset))
|
||||
}
|
||||
// */
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue