From 6196d41c930a072fe327d515e7d7062ff63c124a Mon Sep 17 00:00:00 2001 From: Sofia Date: Thu, 28 Aug 2025 23:37:47 +0300 Subject: [PATCH] Add basic UDP message receiving --- .gitignore | 1 + Cargo.lock | 23 ++++++++++++++ Cargo.toml | 14 +++++++++ src/main.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..598ba0e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,23 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "libc" +version = "0.2.175" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" + +[[package]] +name = "panic-halt" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11" + +[[package]] +name = "udp-tests" +version = "0.1.0" +dependencies = [ + "libc", + "panic-halt", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ca79557 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "udp-tests" +version = "0.1.0" +edition = "2024" + +[dependencies] +libc = { version = "0.2.175", default-features = false } +panic-halt = "1.0.0" + +[profile.dev] +panic = "abort" + +[build] +rustflags = ["-C", "link-args=-lc"] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..25bf3dd --- /dev/null +++ b/src/main.rs @@ -0,0 +1,89 @@ +#![no_std] +#![no_main] + +use core::{ + alloc::{GlobalAlloc, Layout}, + ffi::CStr, + ptr::null_mut, +}; + +extern crate alloc; + +use alloc::vec::Vec; +use panic_halt as _; + +fn handle_error(code: i32, err: &'static core::ffi::CStr) { + unsafe { + if code == -1 { + libc::perror(err.as_ptr()); + libc::exit(libc::EXIT_FAILURE); + } + } +} + +struct MyAllocator; + +unsafe impl GlobalAlloc for MyAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + unsafe { libc::malloc(layout.size()) as *mut u8 } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + unsafe { libc::free(ptr as *mut libc::c_void) } + } +} + +#[global_allocator] +static GLOBAL: MyAllocator = MyAllocator; + +#[unsafe(no_mangle)] +pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize { + // Since we are passing a C string the final null character is mandatory + unsafe { + libc::printf(c"Hello world!\n".as_ptr()); + + let socket = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_UDP); + + let address = libc::sockaddr_in { + sin_family: libc::AF_INET as u16, + sin_port: libc::htons(8080), + sin_addr: libc::in_addr { + s_addr: libc::INADDR_ANY, + }, + sin_zero: [0, 0, 0, 0, 0, 0, 0, 0], + }; + + handle_error( + libc::bind( + socket, + &raw const address as *const libc::sockaddr, + core::mem::size_of::() as u32, + ), + c"bind", + ); + + let mut otus = Vec::with_capacity(10); + + loop { + let buf = libc::malloc(16) as *mut u8; + + let mut other = libc::malloc(core::mem::size_of::()); + let mut other_size = 0; + + libc::recvfrom( + socket, + buf as *mut libc::c_void, + 1, + 0, + &raw mut other as *mut libc::sockaddr, + &mut other_size, + ); + otus.push(*buf); + + libc::printf(c"Data: %s\n".as_ptr(), buf); + + libc::free(buf as *mut libc::c_void); + } + } + 0 +}