From 0261369e7b02d78624e0169550ee778974fef58d Mon Sep 17 00:00:00 2001 From: mattbk Date: Wed, 1 Oct 2025 14:42:52 -0500 Subject: [PATCH] Rough out db basics. --- .gitignore | 1 + Cargo.lock | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 48 +++++++++++++++++++++++++++-- 4 files changed, 134 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 87f1765..70d5dc5 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Cargo.lock b/Cargo.lock index 1bf7e66..41fd5f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 2733c35..5961110 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +rusqlite = "0.37.0" diff --git a/src/main.rs b/src/main.rs index ef24ca0..cfba167 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,13 @@ -// 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::bind("127.0.0.1:7878"); let socket: UdpSocket = match socket_result { Ok(s) => s, @@ -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)?; @@ -32,3 +36,41 @@ fn main() -> std::io::Result<()> { 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(()) +} + +