guess 0.1.5

High-performance zero-copy network protocol detection from first bytes.
Documentation
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* src/detector.rs */
use crate::{
	DetectionError, DetectionResult, DetectionStatus, Protocol, ProtocolInfo, ProtocolVersion,
};
use core::marker::PhantomData;

/// Marker for TCP transport.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Tcp;
/// Marker for UDP transport.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Udp;
/// Marker for unknown or mixed transport.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Unknown;

/// Protocol detector with type-state for transport layer.
#[derive(Debug, Clone)]
pub struct ProtocolDetector<Transport = Unknown> {
	/// Enabled protocols for this detector.
	#[allow(dead_code)]
	pub(crate) enabled: ProtocolSet,
	/// Optional custom priority order for detection.
	#[cfg(feature = "std")]
	pub(crate) priority_order: Option<Vec<Protocol>>,
	/// Maximum bytes to inspect.
	pub(crate) max_inspect_bytes: usize,
	/// Version constraints for detection.
	#[allow(dead_code)]
	pub(crate) expected_versions: ProtocolVersionSet,
	/// Transport type marker.
	pub(crate) _transport: PhantomData<Transport>,
}

/// A set of enabled protocols.
#[derive(Default, Clone, Copy, Debug)]
pub(crate) struct ProtocolSet {
	/// HTTP enabled.
	#[cfg(feature = "http")]
	pub http: bool,
	/// IMAP enabled.
	#[cfg(feature = "imap")]
	pub imap: bool,
	/// TLS enabled.
	#[cfg(feature = "tls")]
	pub tls: bool,
	/// SSH enabled.
	#[cfg(feature = "ssh")]
	pub ssh: bool,
	/// DNS enabled.
	#[cfg(feature = "dns")]
	pub dns: bool,
	/// FTP enabled.
	#[cfg(feature = "ftp")]
	pub ftp: bool,
	/// DHCP enabled.
	#[cfg(feature = "dhcp")]
	pub dhcp: bool,
	/// NTP enabled.
	#[cfg(feature = "ntp")]
	pub ntp: bool,
	/// QUIC enabled.
	#[cfg(feature = "quic")]
	pub quic: bool,
	/// `MySQL` enabled.
	#[cfg(feature = "mysql")]
	pub mysql: bool,
	/// `PostgreSQL` enabled.
	#[cfg(feature = "postgres")]
	pub postgres: bool,
	/// Redis enabled.
	#[cfg(feature = "redis")]
	pub redis: bool,
	/// MQTT enabled.
	#[cfg(feature = "mqtt")]
	pub mqtt: bool,
	/// SMTP enabled.
	#[cfg(feature = "smtp")]
	pub smtp: bool,
	/// POP3 enabled.
	#[cfg(feature = "pop3")]
	pub pop3: bool,
	/// SMB enabled.
	#[cfg(feature = "smb")]
	pub smb: bool,
	/// SIP enabled.
	#[cfg(feature = "sip")]
	pub sip: bool,
	/// RTSP enabled.
	#[cfg(feature = "rtsp")]
	pub rtsp: bool,
	/// STUN enabled.
	#[cfg(feature = "stun")]
	pub stun: bool,
}

/// A set of expected protocol versions.
#[derive(Default, Clone, Debug)]
pub(crate) struct ProtocolVersionSet {
	/// Expected HTTP version.
	#[cfg(feature = "http")]
	pub http: Option<&'static str>,
	/// Expected TLS version.
	#[cfg(feature = "tls")]
	pub tls: Option<&'static str>,
	/// Expected SSH version.
	#[cfg(feature = "ssh")]
	pub ssh: Option<&'static str>,
	/// Expected Redis version.
	#[cfg(feature = "redis")]
	pub redis: Option<u8>,
}

impl<Transport> ProtocolDetector<Transport> {
	/// Detects the protocol and returns its information.
	///
	/// # Errors
	///
	/// Returns `InsufficientData` if more bytes are needed to confirm a protocol.
	#[allow(unused_variables, unused_mut)]
	pub fn detect_info<'a>(&self, data: &'a [u8]) -> DetectionResult<Option<ProtocolInfo<'a>>> {
		let limit = data.len().min(self.max_inspect_bytes);
		let data = &data[..limit];

		let mut any_incomplete = false;

		#[cfg(feature = "std")]
		if let Some(order) = &self.priority_order {
			for protocol in order {
				match self.check_protocol(*protocol, data) {
					(DetectionStatus::Match, version) => {
						return Ok(Some(ProtocolInfo {
							protocol: *protocol,
							version,
						}));
					}
					(DetectionStatus::Incomplete, _) => any_incomplete = true,
					(DetectionStatus::NoMatch, _) => {}
				}
			}
			return if any_incomplete {
				Err(DetectionError::InsufficientData)
			} else {
				Ok(None)
			};
		}

		// Default detection logic (no Vec allocation)
		#[cfg(feature = "ssh")]
		if self.enabled.ssh {
			match self.check_protocol(Protocol::Ssh, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Ssh,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "sip")]
		if self.enabled.sip {
			match self.check_protocol(Protocol::Sip, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Sip,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "rtsp")]
		if self.enabled.rtsp {
			match self.check_protocol(Protocol::Rtsp, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Rtsp,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "imap")]
		if self.enabled.imap {
			match self.check_protocol(Protocol::Imap, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Imap,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "tls")]
		if self.enabled.tls {
			match self.check_protocol(Protocol::Tls, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Tls,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "http")]
		if self.enabled.http {
			match self.check_protocol(Protocol::Http, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Http,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "dns")]
		if self.enabled.dns {
			match self.check_protocol(Protocol::Dns, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Dns,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "ftp")]
		if self.enabled.ftp {
			match self.check_protocol(Protocol::Ftp, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Ftp,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "dhcp")]
		if self.enabled.dhcp {
			match self.check_protocol(Protocol::Dhcp, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Dhcp,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "ntp")]
		if self.enabled.ntp {
			match self.check_protocol(Protocol::Ntp, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Ntp,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "quic")]
		if self.enabled.quic {
			match self.check_protocol(Protocol::Quic, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Quic,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "mysql")]
		if self.enabled.mysql {
			match self.check_protocol(Protocol::Mysql, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Mysql,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "postgres")]
		if self.enabled.postgres {
			match self.check_protocol(Protocol::Postgres, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Postgres,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "redis")]
		if self.enabled.redis {
			match self.check_protocol(Protocol::Redis, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Redis,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "mqtt")]
		if self.enabled.mqtt {
			match self.check_protocol(Protocol::Mqtt, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Mqtt,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "smtp")]
		if self.enabled.smtp {
			match self.check_protocol(Protocol::Smtp, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Smtp,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "pop3")]
		if self.enabled.pop3 {
			match self.check_protocol(Protocol::Pop3, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Pop3,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "smb")]
		if self.enabled.smb {
			match self.check_protocol(Protocol::Smb, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Smb,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}
		#[cfg(feature = "stun")]
		if self.enabled.stun {
			match self.check_protocol(Protocol::Stun, data) {
				(DetectionStatus::Match, version) => {
					return Ok(Some(ProtocolInfo {
						protocol: Protocol::Stun,
						version,
					}));
				}
				(DetectionStatus::Incomplete, _) => any_incomplete = true,
				(DetectionStatus::NoMatch, _) => {}
			}
		}

		if any_incomplete {
			Err(DetectionError::InsufficientData)
		} else {
			Ok(None)
		}
	}

	/// Internal helper to check a single protocol with version constraints.
	#[allow(dead_code, clippy::collapsible_if, clippy::unused_self)]
	fn check_protocol<'a>(
		&self,
		protocol: Protocol,
		data: &'a [u8],
	) -> (DetectionStatus, ProtocolVersion<'a>) {
		let (status, version) = protocol.probe_info(data);
		if status == DetectionStatus::Match {
			match (protocol, &version) {
				#[cfg(feature = "http")]
				(Protocol::Http, ProtocolVersion::Http(v)) => {
					if let Some(expected) = self.expected_versions.http {
						if *v != expected {
							return (DetectionStatus::NoMatch, ProtocolVersion::Unknown);
						}
					}
				}
				#[cfg(feature = "tls")]
				(Protocol::Tls, ProtocolVersion::Tls(v)) => {
					if let Some(expected) = self.expected_versions.tls {
						if *v != expected {
							return (DetectionStatus::NoMatch, ProtocolVersion::Unknown);
						}
					}
				}
				#[cfg(feature = "ssh")]
				(Protocol::Ssh, ProtocolVersion::Ssh(v)) => {
					if let Some(expected) = self.expected_versions.ssh {
						if *v != expected {
							return (DetectionStatus::NoMatch, ProtocolVersion::Unknown);
						}
					}
				}
				#[cfg(feature = "redis")]
				(Protocol::Redis, ProtocolVersion::Redis(v)) => {
					if let Some(expected) = self.expected_versions.redis {
						if *v != expected {
							return (DetectionStatus::NoMatch, ProtocolVersion::Unknown);
						}
					}
				}
				_ => {}
			}
		}
		(status, version)
	}

	/// Backwards compatible detect method.
	///
	/// # Errors
	///
	/// Returns `InsufficientData` if more bytes are needed.
	pub fn detect(&self, data: &[u8]) -> DetectionResult<Option<Protocol>> {
		self
			.detect_info(data)
			.map(|opt| opt.map(|info| info.protocol))
	}

	/// Internal constructor for custom chains.
	#[cfg(feature = "std")]
	#[allow(unused_mut)]
	pub(crate) fn with_order(order: Vec<Protocol>, max_inspect_bytes: usize) -> Self {
		let mut enabled = ProtocolSet::default();
		for p in &order {
			match p {
				#[cfg(feature = "http")]
				Protocol::Http => enabled.http = true,
				#[cfg(feature = "tls")]
				Protocol::Tls => enabled.tls = true,
				#[cfg(feature = "ssh")]
				Protocol::Ssh => enabled.ssh = true,
				#[cfg(feature = "dns")]
				Protocol::Dns => enabled.dns = true,
				#[cfg(feature = "quic")]
				Protocol::Quic => enabled.quic = true,
				#[cfg(feature = "mysql")]
				Protocol::Mysql => enabled.mysql = true,
				#[cfg(feature = "postgres")]
				Protocol::Postgres => enabled.postgres = true,
				#[cfg(feature = "redis")]
				Protocol::Redis => enabled.redis = true,
				#[cfg(feature = "mqtt")]
				Protocol::Mqtt => enabled.mqtt = true,
				#[cfg(feature = "smtp")]
				Protocol::Smtp => enabled.smtp = true,
				#[cfg(feature = "pop3")]
				Protocol::Pop3 => enabled.pop3 = true,
				#[cfg(feature = "imap")]
				Protocol::Imap => enabled.imap = true,
				#[cfg(feature = "ftp")]
				Protocol::Ftp => enabled.ftp = true,
				#[cfg(feature = "smb")]
				Protocol::Smb => enabled.smb = true,
				#[cfg(feature = "stun")]
				Protocol::Stun => enabled.stun = true,
				#[cfg(feature = "sip")]
				Protocol::Sip => enabled.sip = true,
				#[cfg(feature = "rtsp")]
				Protocol::Rtsp => enabled.rtsp = true,
				#[cfg(feature = "dhcp")]
				Protocol::Dhcp => enabled.dhcp = true,
				#[cfg(feature = "ntp")]
				Protocol::Ntp => enabled.ntp = true,
				#[allow(unreachable_patterns)]
				_ => {}
			}
		}
		Self {
			enabled,
			priority_order: Some(order),
			max_inspect_bytes,
			expected_versions: ProtocolVersionSet::default(),
			_transport: PhantomData,
		}
	}
}

impl ProtocolDetector<Unknown> {
	/// Creates a new builder.
	#[must_use]
	pub fn builder() -> crate::ProtocolDetectorBuilder<Unknown> {
		crate::ProtocolDetectorBuilder::new()
	}

	/// Creates a new custom chain builder to define a specific detection order.
	#[cfg(feature = "std")]
	#[must_use]
	pub fn chain() -> crate::ProtocolChainBuilder {
		crate::ProtocolChainBuilder::new()
	}
}

impl ProtocolDetector<Tcp> {}

impl ProtocolDetector<Udp> {}