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
//! NSE oops library wrapper
//!
//! Out-Of-Band (OOB) data processing support.
//! Based on Nmap's oops library.
use crate::brokered_stream::broker_send_all;
use crate::capabilities::NseCapabilityContext;
use crate::providers::{broker_tcp_connect, broker_tcp_receive, NseHostServices};
use mlua::{Lua, Result as LuaResult, Table};
use std::time::Duration;
/// Provider-backed oops registration.
///
/// `services` backs every TCP connect/send/receive path.
pub fn register_oops_library_with_services(
lua: &Lua,
capability_ctx: &NseCapabilityContext,
services: &NseHostServices,
) -> LuaResult<()> {
let globals = lua.globals();
let oops = lua.create_table()?;
oops.set(
"send",
lua.create_function({
let ctx = capability_ctx.clone();
let services = services.clone();
move |lua, (host, port, data): (String, u16, String)| {
let result = lua.create_table()?;
// The broker resolves `host` (authority-preserving); unresolvable
// or refused hosts keep the original `status = "error"` shape.
let (mut handle, _endpoint) = match broker_tcp_connect(
&ctx,
&services,
&host,
port,
Duration::from_secs(5),
"oops.send",
) {
Ok(pair) => pair,
Err(e) => {
result.set("status", "error")?;
result.set("error", e)?;
return Ok(result);
}
};
match broker_send_all(&ctx, handle.as_mut(), data.as_bytes(), "oops.send") {
Ok(_) => {
result.set("status", "ok")?;
result.set("bytes_sent", data.len())?;
}
Err(e) => {
result.set("status", "error")?;
result.set("error", e)?;
}
}
Ok(result)
}
})?,
)?;
oops.set(
"receive",
lua.create_function({
let ctx = capability_ctx.clone();
let services = services.clone();
move |lua, (host, port, timeout): (String, u16, Option<u32>)| {
let result = lua.create_table()?;
let timeout_dur = Duration::from_millis(timeout.unwrap_or(5000) as u64);
let (mut handle, _endpoint) = match broker_tcp_connect(
&ctx,
&services,
&host,
port,
Duration::from_secs(5),
"oops.receive",
) {
Ok(pair) => pair,
Err(e) => {
result.set("status", "error")?;
result.set("error", e)?;
return Ok(result);
}
};
if handle.set_timeouts(timeout_dur).is_err() {
tracing::warn!("Failed to set OOPS read timeout");
}
match broker_tcp_receive(&ctx, handle.as_mut(), 65536, "oops.receive") {
Ok(data) => {
result.set("status", "ok")?;
result.set("data", String::from_utf8_lossy(&data).to_string())?;
result.set("bytes_received", data.len())?;
}
Err(e) => {
// The broker surfaces timeouts as error strings (the
// `ErrorKind::TimedOut` distinction does not cross the
// provider boundary); match the timeout text to preserve
// the original `status = "timeout"` shape.
if e.contains("timed out") || e.contains("timeout") {
result.set("status", "timeout")?;
result.set("data", "")?;
} else {
result.set("status", "error")?;
result.set("error", e)?;
}
}
}
Ok(result)
}
})?,
)?;
oops.set(
"send_and_receive",
lua.create_function({
let ctx = capability_ctx.clone();
let services = services.clone();
move |lua, (host, port, data, timeout): (String, u16, String, Option<u32>)| {
let result = lua.create_table()?;
let timeout_dur = Duration::from_millis(timeout.unwrap_or(5000) as u64);
let (mut handle, _endpoint) = match broker_tcp_connect(
&ctx,
&services,
&host,
port,
Duration::from_secs(5),
"oops.send_and_receive",
) {
Ok(pair) => pair,
Err(e) => {
result.set("status", "error")?;
result.set("error", e)?;
return Ok(result);
}
};
// The provider exposes a single read/write timeout.
if handle.set_timeouts(timeout_dur).is_err() {
tracing::warn!("Failed to set OOPS timeout");
}
if let Err(e) = broker_send_all(
&ctx,
handle.as_mut(),
data.as_bytes(),
"oops.send_and_receive",
) {
result.set("status", "error")?;
result.set("error", e)?;
return Ok(result);
}
match broker_tcp_receive(&ctx, handle.as_mut(), 65536, "oops.send_and_receive") {
Ok(data) => {
result.set("status", "ok")?;
result.set("data", String::from_utf8_lossy(&data).to_string())?;
result.set("bytes_received", data.len())?;
}
Err(e) => {
if e.contains("timed out") || e.contains("timeout") {
result.set("status", "timeout")?;
result.set("data", "")?;
} else {
result.set("status", "error")?;
result.set("error", e)?;
}
}
}
Ok(result)
}
})?,
)?;
oops.set(
"new",
lua.create_function(|lua, (oob_type, data): (String, Option<String>)| {
let pkt = lua.create_table()?;
pkt.set("type", oob_type)?;
pkt.set("data", data.unwrap_or_default())?;
pkt.set(
"timestamp",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
)?;
Ok(pkt)
})?,
)?;
oops.set(
"set_data",
lua.create_function(|_lua, (pkt, data): (Table, String)| {
pkt.set("data", data)?;
Ok(pkt)
})?,
)?;
oops.set(
"get_data",
lua.create_function(|_lua, pkt: Table| pkt.get::<String>("data"))?,
)?;
oops.set(
"set_type",
lua.create_function(|_lua, (pkt, oob_type): (Table, String)| {
pkt.set("type", oob_type)?;
Ok(pkt)
})?,
)?;
oops.set(
"get_type",
lua.create_function(|_lua, pkt: Table| pkt.get::<String>("type"))?,
)?;
oops.set("version", lua.create_function(|_lua, _: ()| Ok("1.0.0"))?)?;
globals.set("oops", oops)?;
Ok(())
}