1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
use crate::datastore::Command;
use crate::enums::AgentState;
use crate::HEADER_BYTES;
use log::{debug, trace};
use std::str::from_utf8;
use tokio::sync::{broadcast, mpsc};

pub fn vec_find(item: u8, search: &[u8]) -> Option<usize> {
    for (index, curr_byte) in search.iter().enumerate() {
        if &(item) == curr_byte {
            return Some(index);
        }
    }
    None
}

/// does the conversion from "example.com" to "7example3com" BUT DOES NOT DO THE TRAILING NULL BECAUSE REASONS
fn seven_dot_three_conversion(name: &[u8]) -> Vec<u8> {
    trace!("7.3 conversion for {name:?} {:?}", from_utf8(name));
    let mut result: Vec<u8> = vec![];

    // TODO: reimplement this with slices and stuff
    let mut next_dot: usize = match vec_find(46, name) {
        Some(value) => value,
        None => {
            // if there's no dots, then just push a length on the front and include the data. then bail
            result.push(name.len() as u8);
            result.extend(name);
            return result;
        }
    };
    let mut name_bytes: Vec<u8> = name.to_vec();
    let mut keep_looping = true;
    let mut current_position: usize = 0;
    // add the first segment length
    result.push(next_dot as u8);

    while keep_looping {
        if next_dot == current_position {
            name_bytes = name_bytes.to_vec()[current_position + 1..].into();
            next_dot = match vec_find(46, &name_bytes) {
                Some(value) => value,
                None => name_bytes.len(),
            };
            current_position = 0;
            // this should be the
            // debug!(". - {:?} ({:?})", next_dot, name_bytes);
            if next_dot != 0 {
                result.push(next_dot as u8);
                trace!("pushing next_dot {}", next_dot as u8);
            }
        } else {
            // we are processing bytes
            trace!("pushing {}", name_bytes[current_position]);
            result.push(name_bytes[current_position]);
            // debug!("{:?} {:?}", current_position, name_bytes.as_bytes()[current_position]);
            current_position += 1;
        }
        if current_position == name_bytes.len() {
            keep_looping = false;
        }
    }
    result
}

/// If you have a `name` and a `target` and want to see if you can find a chunk of the `target` that the `name` ends with, this is your function!
pub fn find_tail_match(name: &[u8], target: &Vec<u8>) -> usize {
    // #[cfg(debug)]
    // {
    let name_str = format!("{name:?}");
    let target_str = format!("{target:?}");
    let longest = match name_str.len() > target_str.len() {
        true => name_str.len(),
        false => target_str.len(),
    };
    trace!("find_tail_match(\n  name={name_str:>longest$}, \ntarget={target_str:>longest$}\n)",);
    // }
    let mut tail_index: usize = 0;
    for (i, _) in target.iter().enumerate() {
        trace!("Tail index={i}");
        let tail = &target[i..];
        if name.ends_with(tail) {
            trace!("Found a tail at index {i}");
            tail_index = i;
            break;
        } else {
            trace!("Didn't match: name / target \n{name:?}\n{:?}", &target[i..]);
        }
    }
    tail_index
}

/*
turn the NAME field into the bytes for a response

so example.com turns into

(7)example(3)com(0)

compress_target is the index of the octet in the response to point the response at
which should typically be the qname in the question bytes

compress_reference is the vec of bytes of the compression target, ie this is the kind of terrible thing you should do
name_as_bytes(
    "lol.example.com".as_bytes().to_vec(),
    Some(12),
    Some("example.com".as_bytes().to_vec())
)
*/
pub fn name_as_bytes(
    name: Vec<u8>,
    compress_target: Option<u16>,
    compress_reference: Option<&Vec<u8>>,
) -> Vec<u8> {
    trace!("################################");
    match from_utf8(&name) {
        Ok(nstr) => trace!("name_as_bytes name={nstr:?} compress_target={compress_target:?} compress_reference={compress_reference:?}"),
        Err(_) =>  trace!("failed to utf-8 name name_as_bytes name={name:?} compress_target={compress_target:?} compress_reference={compress_reference:?}"),
    };

    // if we're given a compression target and no reference just compress it and return
    if let (Some(target), None) = (compress_target, compress_reference) {
        trace!("we got a compress target ({target}) but no reference we're just going to compress");
        // we need the first two bits to be 1, to mark it as compressed
        // 4.1.4 RFC1035 - https://www.rfc-editor.org/rfc/rfc1035.html#section-4.1.4
        let result: Vec<u8> = (0b1100000000000000 | target).to_be_bytes().into();
        trace!("result of name_as_bytes {result:?}");
        return result;
    };

    let mut result: Vec<u8> = vec![];
    // if somehow it's a weird bare domain then we don't have to do much it
    if !name.contains(&46) {
        result.push(name.len() as u8);
        result.extend(&name);
        result.push(0); // null pad the name
        return result;
    }
    result = seven_dot_three_conversion(&name);

    if compress_target.is_none() {
        trace!("no compression target, adding the trailing null and returning!");
        result.push(0);
        return result;
    };

    if let Some(ct) = compress_reference {
        trace!("you gave me {ct:?} as a compression reference");

        if &name == ct {
            trace!("The thing we're converting is the same as the compression reference!");
            // return a pointer to the target_byte (probably the name in the header)
            if let Some(target) = compress_target {
                let result: u16 = 0b1100000000000000 | target;
                return result.to_be_bytes().to_vec();
            } else {
                panic!("you didn't give us a target, dude!")
            }
        }
        if name.ends_with(ct) {
            trace!("the name ends with the target! woo!");
            // Ok, we've gotten this far. We need to slice off the "front" of the string and return that.
            result = name.clone();
            result.truncate(name.len() - ct.len());
            trace!("The result is trimmed and now {:?}", from_utf8(&result));

            // do the 7.3 conversion
            result = seven_dot_three_conversion(&result);
            trace!("7.3converted: {:?}", from_utf8(&result));

            // then we need to return the pointer to the tail
            if let Some(target) = compress_target {
                let pointer_bytes: u16 = 0b1100000000000000 | target;
                result.extend(pointer_bytes.to_be_bytes());
            } else {
                #[cfg(debug)]
                panic!("No compression target and we totally could have compressed this.")
            }

            trace!("The result is trimmed and now {:?}", result);
        } else {
            // dropped into tail-finding mode where we're looking for a sub-string of the parent to target a compression pointer
            trace!("trying to find a sub-part of {ct:?} in {name:?}");

            let tail_index = find_tail_match(&name, ct);
            trace!("tail_index: {tail_index}");
            // if we get to here and the tail_index is 0 then we haven't set it - because we'd have caught the whole thing in the ends_with matcher earlier.
            if tail_index != 0 {
                trace!("Found a tail-match: {tail_index}");
                // slice the tail off the name
                let mut name_copy = name.to_vec();
                // the amount of the tail that matched to the name, ie abc, bc = 2, aab, bbb = 1
                let matched_length = ct.len() - tail_index;
                name_copy.truncate(name.len() - matched_length);
                trace!("sliced name down to {name_copy:?}");
                // put the pointer on there
                result = seven_dot_three_conversion(&name_copy);
                trace!("converted result to {result:?}");
                let pointer: u16 = 0b1100000000000000 | (HEADER_BYTES + tail_index + 1) as u16;
                result.extend(pointer.to_be_bytes());
            }
        }
    }
    trace!("Final result {result:?}");
    result
}

// lazy_static!{
//     static ref GOATNS_VERSION: DNSCharString = DNSCharString::from(format!("GoatNS {}", env!("CARGO_PKG_VERSION")).as_str());
// }

// lazy_static!{
//     static ref VERSION_RESPONSE: Vec<InternalResourceRecord> = vec![InternalResourceRecord::TXT {
//         class: RecordClass::Chaos,
//         ttl: 1,
//         txtdata: GOATNS_VERSION.to_owned(),
//     }];
// }

// pub fn reply_version(id: &u16, question: &Option<crate::Question>) -> Result<Reply, String> {
//     let mut reply = reply_builder(id.to_owned(), Rcode::NoError)?;
//     reply.question = question.to_owned();
//     reply.answers = VERSION_RESPONSE.clone();
//     reply.header.ancount = 1;
//     debug!("Version: {reply:?}");
//     debug!("Goatns version: {:?}", GOATNS_VERSION.to_owned());
//     Ok(reply)
// }

/// dumps the bytes out as if you were using some kind of fancy packet-dumper
pub fn hexdump(bytes: Vec<u8>) {
    for byte in bytes.chunks(2) {
        let byte0_alpha = match byte[0].is_ascii_alphanumeric() {
            true => from_utf8(byte[0..1].into()).expect("Failed to decode bytes"),
            false => " ",
        };
        match byte.len() {
            2 => {
                let byte1_alpha = match byte[1].is_ascii_alphanumeric() {
                    true => from_utf8(byte[1..2].into()).expect("Failed to decode bytes"),
                    false => " ",
                };

                debug!(
                    "{:02x} {:02x} {:#010b} {:#010b} {:3} {:3} {byte0_alpha} {byte1_alpha}",
                    byte[0], byte[1], byte[0], byte[1], byte[0], byte[1],
                );
            }
            _ => {
                debug!(
                    "{:02x}    {:#010b}    {:3} {byte0_alpha}",
                    byte[0], byte[0], byte[0],
                );
            }
        }
    }
}

/// turn a degrees/minutes/seconds format into unsigned 32-bit integer matching the format
/// required for a DNS LOC record
///
/// when positive = true, you're North or West
pub fn dms_to_u32(deg: u8, min: u8, sec: f32, positive: bool) -> u32 {
    let secsfrac = sec % 1f32;

    let dms_multiplied: u32 = (((((deg as u32 * 60) + min as u32) * 60) + sec as u32) * 1000)
        + (secsfrac * 1000.0) as u32;

    match positive {
        true => 2u32.pow(31) + dms_multiplied,
        false => 2u32.pow(31) - dms_multiplied,
    }
}

/// converts size/precision X * 10**Y(cm) to 0xXY
/// This code is ported from the C code in RFC1876 (Appendix A, precsize_aton)
#[allow(dead_code)]
pub fn loc_size_to_u8(input: f32) -> u8 {
    let mut mantissa: u8;

    let cmval = input * 100.0;

    let mut exponent = 0;
    for i in 0..10 {
        if (cmval as f64) < (10u64.pow(i + 1) as f64) {
            exponent = i;
            // eprintln!("CMVAL: {cmval} Exponent #{i} {}", (poweroften[i+1] as u64));
            break;
        }
    }
    // eprintln!("{:?}", ((cmval as f64) / (poweroften[exponent] as f64) ).ceil());
    mantissa = ((cmval as f64) / (10u64.pow(exponent) as f64)).ceil() as u8;
    if mantissa > 9u8 {
        mantissa = 9u8;
    }
    // eprintln!("mantissa: {mantissa}, exponent: {exponent}");
    // turn it into the magic ugly numbers
    let retval: u8 = (mantissa << 4) | (exponent as u8);
    retval
}

/// Get all the widgets for agent signalling
pub fn start_channels() -> (
    broadcast::Sender<AgentState>,
    mpsc::Sender<Command>,
    mpsc::Receiver<Command>,
) {
    let (agent_tx, _) = broadcast::channel(32);
    let datastore_sender: mpsc::Sender<Command>;
    let datastore_receiver: mpsc::Receiver<Command>;
    (datastore_sender, datastore_receiver) = mpsc::channel(crate::MAX_IN_FLIGHT);
    (agent_tx, datastore_sender, datastore_receiver)
}

/// Compares the TLD to the list of valid TLDs - usually from `allowed_tlds` in [crate::config::ConfigFile]
///```
/// use goatns::utils::check_valid_tld;
///
/// let valid_tlds = vec![];
/// let zone_name = "hello.example.goat";
/// assert_eq!(check_valid_tld(&zone_name, &valid_tlds), true);
///
/// let valid_tlds = vec!["goat".to_string()];
/// let zone_name = "hello.example.goat";
/// assert_eq!(check_valid_tld(&zone_name, &valid_tlds), true);
///
/// let valid_tlds = vec!["cheese".to_string()];
/// let zone_name = "hello.example.goat";
/// assert_eq!(check_valid_tld(&zone_name, &valid_tlds), false);
/// ```
pub fn check_valid_tld(zone_name: &str, allowed_tlds: &[String]) -> bool {
    if allowed_tlds.is_empty() {
        return true;
    }
    for tld in allowed_tlds.iter() {
        if zone_name.ends_with(&format!(".{tld}")) {
            return true;
        }
    }
    false
}