1
1
// src/lib.rs
2
+ mod service;
3
+ use service:: { Service , NativeService , CService } ;
2
4
3
5
extern crate libc;
4
6
// extern crate trust_dns;
@@ -13,6 +15,29 @@ use std::fmt::Write; // For formatting strings
13
15
use std:: net:: Ipv4Addr ;
14
16
use dns_parser:: { Builder , QueryClass , QueryType , Packet } ;
15
17
18
+
19
+
20
+ #[ cfg( not( feature = "ffi" ) ) ]
21
+ fn build_info ( ) {
22
+ println ! ( "Default build" ) ;
23
+ }
24
+
25
+ #[ cfg( not( feature = "ffi" ) ) ]
26
+ fn create_service ( cb : fn ( & [ u8 ] ) ) -> Box < dyn Service > {
27
+ NativeService :: init ( cb)
28
+ }
29
+
30
+
31
+ #[ cfg( feature = "ffi" ) ]
32
+ fn build_info ( ) {
33
+ println ! ( "FFI build" ) ;
34
+ }
35
+
36
+ #[ cfg( feature = "ffi" ) ]
37
+ fn create_service ( cb : fn ( & [ u8 ] ) ) -> Box < dyn Service > {
38
+ CService :: init ( cb)
39
+ }
40
+
16
41
#[ repr( C ) ]
17
42
#[ derive( Debug , Clone , Copy ) ]
18
43
pub struct EspIp4Addr {
@@ -64,26 +89,6 @@ impl Clone for EspIpUnion {
64
89
// Manual implementation of Copy for the union
65
90
impl Copy for EspIpUnion { }
66
91
67
- // // Other structs remain the same
68
- // #[repr(C)]
69
- // #[derive(Debug, Clone, Copy)]
70
- // pub struct EspIp4Addr {
71
- // addr: u32,
72
- // }
73
- //
74
- // #[repr(C)]
75
- // #[derive(Debug, Clone, Copy)]
76
- // pub struct EspIp6Addr {
77
- // addr: [u32; 4],
78
- // zone: u8,
79
- // }
80
- //
81
- // #[repr(C)]
82
- // #[derive(Debug, Clone, Copy)]
83
- // pub struct EspIpAddr {
84
- // u_addr: EspIpUnion, // Union containing IPv4 or IPv6 address
85
- // addr_type: u8,
86
- // }
87
92
// Address type definitions
88
93
pub const ESP_IPADDR_TYPE_V4 : u8 = 0 ;
89
94
pub const ESP_IPADDR_TYPE_V6 : u8 = 6 ;
@@ -117,8 +122,6 @@ extern "C" {
117
122
) -> usize ;
118
123
fn _mdns_pcb_deinit ( tcpip_if : MdnsIf , ip_protocol : MdnsIpProtocol ) -> EspErr ;
119
124
fn set_callback ( callback : extern "C" fn ( * const u8 , usize ) ) ;
120
-
121
- // fn set_callback2();
122
125
}
123
126
124
127
extern "C" fn rust_callback ( data : * const u8 , len : usize )
@@ -127,92 +130,92 @@ extern "C" fn rust_callback(data: *const u8, len: usize)
127
130
unsafe {
128
131
// Ensure that the data pointer is valid
129
132
if !data. is_null ( ) {
130
- // Create a Vec<u8> from the raw pointer and length
131
- let data_vec = std:: slice:: from_raw_parts ( data, len) . to_vec ( ) ;
133
+ // Create a Vec<u8> from the raw pointer and length
134
+ let data_vec = std:: slice:: from_raw_parts ( data, len) . to_vec ( ) ;
132
135
133
- // Now call the safe parser function with the Vec<u8>
134
- parse_dns_response ( & data_vec) ; }
136
+ // Now call the safe parser function with the Vec<u8>
137
+ parse_dns_response ( & data_vec) . unwrap ( ) ;
138
+ }
135
139
}
136
140
}
137
141
138
- fn parse_dns_response ( data : & [ u8 ] ) {
139
- // Safe handling of the slice
140
- println ! ( "Parsing DNS response with length: {}" , data. len( ) ) ;
141
-
142
- parse_dns_response2 ( data) ;
143
- // Process the data (this will be safe, as `data` is a slice)
144
- // Example: You could convert the slice to a string, inspect it, or pass it to a DNS library
145
- }
146
-
147
- fn parse_dns_response2 ( data : & [ u8 ] ) -> Result < ( ) , String > {
142
+ fn parse_dns_response ( data : & [ u8 ] ) -> Result < ( ) , String > {
148
143
println ! ( "Parsing DNS response with length 2 : {}" , data. len( ) ) ;
149
- // use dns_parser::Packet;
150
144
let packet = Packet :: parse ( & data) . unwrap ( ) ;
151
145
for answer in packet. answers {
152
146
println ! ( "{:?}" , answer) ;
153
147
}
154
- // match Message::from_vec(data) {
155
- // Ok(msg) => {
156
- // // Successful parsing
157
- // println!("Parsed DNS message successfully.");
158
- // }
159
- // Err(e) => {
160
- // // Detailed error message
161
- // eprintln!("Error parsing DNS message: {}", e);
162
- // }
163
- // }
164
- // Parse the response message
165
- // let msg = Message::from_vec(data).map_err(|e| e.to_string())?;
166
- // println!("Type: {}", msg.op_code().to_string());
167
- // // Check if the message is a response (opcode is Response)
168
- // if msg.op_code() != trust_dns_client::op::OpCode::Status {
169
- // return Err("Not a response message".to_string());
170
- // }
171
- //
172
- // // Display the answer section (which should contain A record)
173
- // for answer in msg.answers() {
174
- // println!("Non-IP answer: {:?}", answer);
175
- // if let Some(ipv4_addr) = answer.rdata().to_ip_addr() {
176
- // println!("Resolved IP address: {}", ipv4_addr);
177
- // } else {
178
- // println!("Non-IP answer: {:?}", answer);
179
- // }
180
- // }
181
-
148
+ for question in packet. questions {
149
+ println ! ( "{:?}" , question) ;
150
+ }
182
151
Ok ( ( ) )
183
152
}
184
153
185
154
use std:: ffi:: CString ;
155
+ use std:: thread;
156
+ use std:: time:: Duration ;
157
+ use lazy_static:: lazy_static;
158
+ use std:: sync:: { Arc , Mutex } ;
186
159
160
+ lazy_static ! {
161
+ static ref SERVICE : Arc <Mutex <Option <Box <dyn Service >>>> = Arc :: new( Mutex :: new( None ) ) ;
162
+ }
163
+
164
+ fn read_cb ( vec : & [ u8 ] ) {
165
+ println ! ( "Received {:?}" , vec) ;
166
+ parse_dns_response ( vec) . unwrap ( ) ;
167
+ }
187
168
188
169
pub fn mdns_init ( ) {
170
+ build_info ( ) ;
171
+ let mut service_guard = SERVICE . lock ( ) . unwrap ( ) ;
172
+ if service_guard. is_none ( ) {
173
+ // Initialize the service only if it hasn't been initialized
174
+ * service_guard = Some ( create_service ( read_cb) ) ;
175
+ }
176
+ // let service: Box<dyn Service> = create_service(read_cb);
177
+ // service.action1();
178
+ // let packet: [u8; 34] = [
179
+ // 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x64, 0x61,
180
+ // 0x76, 0x69, 0x64, 0x2d, 0x77, 0x6f, 0x72, 0x6b, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00,
181
+ // 0x00, 0x01, 0x00, 0x01,
182
+ // ];
183
+ // service.send(packet.to_vec());
184
+ // thread::sleep(Duration::from_millis(500));
185
+ // service.deinit();
189
186
println ! ( "mdns_init called" ) ;
190
187
}
191
188
192
189
pub fn mdns_deinit ( ) {
190
+ let mut service_guard = SERVICE . lock ( ) . unwrap ( ) ;
191
+ if let Some ( service) = service_guard. take ( ) {
192
+ service. deinit ( ) ;
193
+ }
193
194
println ! ( "mdns_deinit called" ) ;
194
195
}
195
196
196
- pub fn create_mdns_query ( ) -> Vec < u8 > {
197
- let query_name = "david-work.local" ; // The domain you want to query
197
+ fn create_a_query ( name : & str ) -> Vec < u8 > {
198
198
let query_type = QueryType :: A ; // Type A query for IPv4 address
199
199
let query_class = QueryClass :: IN ; // Class IN (Internet)
200
200
201
201
// Create a new query with ID and recursion setting
202
- let mut builder = Builder :: new_query ( 12345 , true ) ;
202
+ let mut builder = Builder :: new_query ( 0x5555 , true ) ;
203
203
204
204
// Add the question for "david-work.local"
205
- builder. add_question ( query_name , false , query_type, query_class) ;
205
+ builder. add_question ( name , false , query_type, query_class) ;
206
206
207
207
// Build and return the query packet
208
208
builder. build ( ) . unwrap_or_else ( |x| x)
209
209
}
210
210
211
- pub fn mdns_query_host_rust ( name : & str ) {
212
- let c_name = CString :: new ( name) . expect ( "Failed to create CString" ) ;
213
- // unsafe {
214
- // mdns_query_host(c_name.as_ptr());
215
- // }
211
+ pub fn mdns_query ( name : & str ) {
212
+ let service_guard = SERVICE . lock ( ) . unwrap ( ) ;
213
+ if let Some ( service) = & * service_guard {
214
+ let packet = create_a_query ( name) ;
215
+ service. send ( packet) ;
216
+ } else {
217
+ println ! ( "Service not initialized" ) ;
218
+ }
216
219
}
217
220
218
221
pub fn mdns_pcb_init_rust ( tcpip_if : MdnsIf , ip_protocol : MdnsIpProtocol ) -> Result < ( ) , EspErr > {
@@ -229,24 +232,7 @@ pub fn mdns_pcb_init_rust(tcpip_if: MdnsIf, ip_protocol: MdnsIpProtocol) -> Resu
229
232
}
230
233
}
231
234
232
- pub fn mdns_udp_pcb_write_rust (
233
- tcpip_if : MdnsIf ,
234
- ip_protocol : MdnsIpProtocol ,
235
- ip : EspIpAddr ,
236
- port : u16 ,
237
- data : & [ u8 ] ,
238
- ) -> usize {
239
- unsafe {
240
- _mdns_udp_pcb_write (
241
- tcpip_if,
242
- ip_protocol,
243
- & ip as * const EspIpAddr ,
244
- port,
245
- data. as_ptr ( ) ,
246
- data. len ( ) ,
247
- )
248
- }
249
- }
235
+
250
236
251
237
pub fn mdns_pcb_deinit_rust ( tcpip_if : MdnsIf , ip_protocol : MdnsIpProtocol ) -> Result < ( ) , EspErr > {
252
238
let err = unsafe { _mdns_pcb_deinit ( tcpip_if, ip_protocol) } ;
0 commit comments