cdns_rs/a_sync/query_async_taps.rs
1/*-
2 * cdns-rs - a simple sync/async DNS query library
3 *
4 * Copyright (C) 2020 Aleksandr Morozov
5 *
6 * Copyright 2025 Aleksandr Morozov
7 *
8 * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
9 * the European Commission - subsequent versions of the EUPL (the "Licence").
10 *
11 * You may not use this work except in compliance with the Licence.
12 *
13 * You may obtain a copy of the Licence at:
14 *
15 * https://joinup.ec.europa.eu/software/page/eupl
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20 * Licence for the specific language governing permissions and limitations
21 * under the Licence.
22 */
23
24
25
26use super::{network::NetworkTapType, common::DnsRequestHeader};
27
28
29#[derive(Debug)]
30pub(crate) struct Tap
31{
32 /// A socket which should be used to send requests
33 tap: Box<NetworkTapType>,
34
35 /// A requests binded to this specific tap
36 qdnsr: Vec<DnsRequestHeader>
37}
38
39unsafe impl Send for Tap {}
40unsafe impl Sync for Tap {}
41
42impl Tap
43{
44 pub(crate)
45 fn new(tap: Box<NetworkTapType>, req: DnsRequestHeader) -> Self
46 {
47 return
48 Self
49 {
50 tap: tap,
51 qdnsr: vec![req]
52 };
53 }
54
55 pub(crate)
56 fn add(&mut self, req: DnsRequestHeader)
57 {
58 self.qdnsr.push(req);
59 }
60
61 pub(crate)
62 fn into_inner(self) -> (Box<NetworkTapType>, Vec<DnsRequestHeader>)
63 {
64 return (self.tap, self.qdnsr);
65 }
66
67 /*pub(crate)
68 fn inner_requests(self) -> Vec<DnsRequestHeader>
69 {
70 return self.qdnsr;
71 }*/
72}
73
74pub(crate) struct AsyncTaps
75{
76 taps: Vec<Tap>,
77}
78
79impl AsyncTaps
80{
81 pub(crate)
82 fn new_with_capacity(tap_n: usize) -> Self
83 {
84 return Self{ taps: Vec::with_capacity(tap_n)/*, reqs: HashMap::with_capacity(req_n)*/ };
85 }
86
87 pub(crate)
88 fn push(&mut self, tap: Tap)
89 {
90 self.taps.push(tap);
91 }
92
93 pub(crate)
94 fn push_to_last(&mut self, req: DnsRequestHeader)
95 {
96 let t = self.taps.last_mut().unwrap();
97
98 t.add(req);
99 }
100
101 pub(crate)
102 fn len(&self) -> usize
103 {
104 return self.taps.len();
105 }
106
107 pub(crate)
108 fn into_iter(self) -> std::vec::IntoIter<Tap>
109 {
110 return self.taps.into_iter();
111 }
112}
113
114