Skip to content

Commit

Permalink
add ttl parameter on send command
Browse files Browse the repository at this point in the history
  • Loading branch information
ruckc committed Feb 8, 2019
1 parent d8f98ef commit c064250
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mctk"
version = "0.1.2"
version = "0.1.3"
authors = ["curtis@ruck.io"]
edition = "2018"
description = "multicast toolkit (mctk)"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ OPTIONS:
-i, --interval <milliseconds> Sets the sender's packet transmission interval [default: 1000]
-s, --packet-size <bytes> Sets the sender's packet size [default: 1000]
-p, --port <port> Sets the multicast port [default: 8201]
-t, --ttl <ttl> Sets the sender's time to live value [default: 2]
```
```bash
$ mctk help receive
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ pub fn join_multicast(addr: SocketAddr) -> io::Result<UdpSocket> {
Ok(socket.into_udp_socket())
}

pub fn new_sender() -> io::Result<Socket> {
pub fn new_sender(ttl: u8) -> io::Result<Socket> {
let socket = new_socket()?;

socket.set_multicast_if_v4(&Ipv4Addr::new(0, 0, 0, 0))?;

socket.set_multicast_ttl_v4(u32::from(ttl))?;

socket.bind(&SockAddr::from(SocketAddr::new(
Ipv4Addr::new(0, 0, 0, 0).into(),
0,
Expand All @@ -64,7 +66,7 @@ pub fn send_message(socket: &Socket, destination: SocketAddr, packet_size: u16,
let message = generate_message(packet_size, msgid);
socket
.send_to(message.as_bytes(), &SockAddr::from(destination))
.expect(&format!("could not send {} to {}", message, destination));
.expect("Unable to send message");
}

pub fn generate_message(size: u16, count: u32) -> String {
Expand Down
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ fn main() {
.help("Sets the sender's packet size")
.default_value("1000")
.takes_value(true),
)
.arg(
Arg::with_name("ttl")
.short("t")
.long("ttl")
.value_name("ttl")
.help("Sets the sender's time to live value")
.takes_value(true)
.default_value("2"),
),
)
.subcommand(
Expand Down Expand Up @@ -87,9 +96,10 @@ fn main() {
let sleep_interval: Duration =
Duration::from_millis(matches.value_of("interval").unwrap().parse().unwrap());
let packet_size: u16 = matches.value_of("packet-size").unwrap().parse().unwrap();
let ttl: u8 = matches.value_of("ttl").unwrap().parse().unwrap();
let destination_addr = SocketAddr::new(group, port);
println!("Running a sender to {}", destination_addr);
let socket: socket2::Socket = lib::new_sender().expect("could not create sender");
let socket: socket2::Socket = lib::new_sender(ttl).expect("could not create sender");
let mut count: u32 = 0;
while true {
count += 1;
Expand Down
2 changes: 1 addition & 1 deletion tests/multicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn test_multicast() {

let mut rcvbuf = [0u8; 65536];

let sender = mctk::new_sender().expect("unable to create sender");
let sender = mctk::new_sender(1).expect("unable to create sender");
let listener = mctk::join_multicast(sockaddr).expect("unable to create multicast listener");

mctk::send_message(&sender, sockaddr, 50, 17);
Expand Down

0 comments on commit c064250

Please sign in to comment.