Cactus/net/slp/
mod.rs

1//! The module accountable for making the Server List Ping (SLP) protocol.
2
3// TODO: Encoding/Decoding of VarInts and VarLongs into a separate module, maybe the module packet
4
5// TODO: As discussed in the main.rs file, we might want to start with running the SLP into a
6// separate thread for the sake of simplicity, for now. Then we'll need to dig into concurrency
7// rather than pure parallelism.
8
9// TODO: Add logging.
10
11pub mod ip_logger;
12
13use log::debug;
14
15use super::packet::{PacketBuilder, PacketError};
16use crate::consts;
17use crate::packet::Packet;
18
19/// The response for a Status Request packet.
20pub fn status_response() -> Result<Packet, PacketError> {
21    let json_response = consts::protocol::status_response_json();
22
23    PacketBuilder::new().append_string(json_response).build(
24        0x00,
25        Some("Status Response (Clientbound) (Status)".to_string()),
26    )
27}
28
29/// The response for a Ping Request packet.
30pub fn ping_response(ping_request_packet: Packet) -> Result<Packet, PacketError> {
31    debug!("Ping packet is: {ping_request_packet}");
32    let payload: &[u8] = ping_request_packet.get_payload();
33    debug!(
34        "Ping packet payload is: {payload:?} and len is {}",
35        payload.len()
36    );
37    if payload.len() == 8 {
38        // Send back the same timestamp as what we received
39        PacketBuilder::new().append_bytes(&payload[0..8]).build(
40            0x01,
41            Some("Ping Response (Clientbound) (Status)".to_string()),
42        )
43    } else {
44        Err(PacketError::PayloadDecodeError(
45            "failed to decode timestamp (Long) in the Ping Request packet".to_string(),
46        ))
47    }
48}