Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create unix socket and qauld-ctl binary #543

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
# binaries
"clients/cli",
"clients/qauld",
"clients/qauld-ctl",

# libp2p modules
"libp2p_modules/qaul_info",
Expand Down
8 changes: 8 additions & 0 deletions rust/clients/qauld-ctl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "qauld-ctl"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
20 changes: 20 additions & 0 deletions rust/clients/qauld-ctl/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::os::unix::net::UnixStream;
use std::io::{Read, Write};
mod socket;
fn main() {
socket::run_socket();
let mut stream = UnixStream::connect("/tmp/qauld.sock").expect("Unable to connect to Unix socket");
let request = "some request".as_bytes();
match stream.write(request) {
Ok(_) => {
let mut buffer = [0; 1024];
match stream.read(&mut buffer) {
Ok(_) => {
// handle the response
}
Err(e) => println!("Error reading from socket: {}", e),
}
}
Err(e) => println!("Error writing to socket: {}", e),
}
}
31 changes: 31 additions & 0 deletions rust/clients/qauld-ctl/src/socket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::os::unix::net::UnixListener;
use std::os::unix::net::UnixStream;
use std::io::{Read, Write};
use std::fs::{self, Permissions};
use std::os::unix::fs::PermissionsExt;

pub fn run_socket() {

let socket_dir = "/tmp/";
let socket_file = "/tmp/qauld.sock";
// 0700 (read, write, execute for owner only)
fs::set_permissions(socket_dir, Permissions::from_mode(0o700));
// 0600 (read, write for owner only)
fs::set_permissions(socket_file, Permissions::from_mode(0o600));

let listener = UnixListener::bind(socket_file).expect("Unable to bind to Unix socket");
for stream in listener.incoming() {
match stream {
Ok(mut stream) => {
let mut buffer = [0; 1024];
match stream.read(&mut buffer) {
Ok(_) => {
// handle the request
}
Err(e) => println!("Error reading from socket: {}", e),
}
}
Err(e) => println!("Error accepting incoming connection: {}", e),
}
}
}