Compare commits
4 Commits
970456a39b
...
c228129e21
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c228129e21 | ||
![]() |
0261369e7b | ||
![]() |
0420e9994b | ||
![]() |
c6ee82e338 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
*.db
|
||||
|
87
Cargo.lock
generated
87
Cargo.lock
generated
@@ -2,6 +2,93 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2261d10cca569e4643e526d8dc2e62e433cc8aba21ab764233731f8d369bf394"
|
||||
|
||||
[[package]]
|
||||
name = "fallible-iterator"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
|
||||
|
||||
[[package]]
|
||||
name = "fallible-streaming-iterator"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
|
||||
dependencies = [
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashlink"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1"
|
||||
dependencies = [
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libsqlite3-sys"
|
||||
version = "0.35.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "133c182a6a2c87864fe97778797e46c7e999672690dc9fa3ee8e241aa4a9c13f"
|
||||
dependencies = [
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "my_udp_server"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rusqlite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
||||
|
||||
[[package]]
|
||||
name = "rusqlite"
|
||||
version = "0.37.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "165ca6e57b20e1351573e3729b958bc62f0e48025386970b6e4d29e7a7e71f3f"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"fallible-iterator",
|
||||
"fallible-streaming-iterator",
|
||||
"hashlink",
|
||||
"libsqlite3-sys",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.15.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||
|
@@ -4,3 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
rusqlite = "0.37.0"
|
||||
|
@@ -2,4 +2,7 @@
|
||||
(working name)
|
||||
|
||||
Experimental server to catch UDP packets from a
|
||||
[modified mwtchahrd](https://gitea.farpn.net/w1cdn/mwtchahrd) and do something with them.
|
||||
[modified mwtchahrd](https://gitea.farpn.net/w1cdn/mwtchahrd) and do something with them.
|
||||
|
||||
Initially based on the example here:
|
||||
[Building UDP Clients and Servers](https://notes.kodekloud.com/docs/Rust-Programming/Network-Programming-and-File-Handling/Building-UDP-Clients-and-Servers)
|
53
src/main.rs
53
src/main.rs
@@ -1,10 +1,15 @@
|
||||
// UDP server
|
||||
use std::net::UdpSocket;
|
||||
use std::net::UdpSocket; // UDP server
|
||||
use rusqlite::{params, Connection, Result}; // Database operations and result handling
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
|
||||
let _ = create_database();
|
||||
let _ = insert_packet("foo", 1);
|
||||
|
||||
let socket_result: Result<UdpSocket, std::io::Error> = UdpSocket::bind("127.0.0.1:7878");
|
||||
let mut socket: UdpSocket = match socket_result {
|
||||
let socket: UdpSocket = match socket_result {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
println!("Failed to bind socket: {}", e);
|
||||
@@ -17,7 +22,6 @@ fn main() -> std::io::Result<()> {
|
||||
// Buffer for incoming data (512 bytes)
|
||||
let mut buffer: [u8; 512] = [0; 512];
|
||||
|
||||
|
||||
loop {
|
||||
// Receive data from the client
|
||||
let (bytes_received, src_addr) = socket.recv_from(&mut buffer)?;
|
||||
@@ -27,9 +31,48 @@ fn main() -> std::io::Result<()> {
|
||||
src_addr,
|
||||
String::from_utf8_lossy(&buffer[..bytes_received])
|
||||
);
|
||||
|
||||
|
||||
let _ = insert_packet(&String::from_utf8_lossy(&buffer[..bytes_received]), 1);
|
||||
|
||||
// Echo the data back to the client
|
||||
socket.send_to(&buffer[..bytes_received], src_addr)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Database functions
|
||||
fn create_database() -> Result<()> {
|
||||
// Connect to SQLite database (creates the file if it doesn't exist)
|
||||
let conn = Connection::open("pkt_database.db")?;
|
||||
|
||||
// Create a table
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS packets (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
raw TEXT NOT NULL,
|
||||
age INTEGER NOT NULL,
|
||||
date INTEGER NOT NULL
|
||||
)",
|
||||
[], // No parameters needed
|
||||
)?;
|
||||
|
||||
println!("Database and table created successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_packet(raw: &str, age: i32) -> Result<()> {
|
||||
let conn = Connection::open("pkt_database.db")?;
|
||||
|
||||
let ds = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
|
||||
|
||||
// Insert
|
||||
conn.execute(
|
||||
"INSERT INTO packets (raw, age, date) VALUES (?1, ?2, ?3)",
|
||||
params![raw, age, ds], // Bind parameters
|
||||
)?;
|
||||
|
||||
println!("Row inserted successfully.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user