This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jean Pierre Dudey <[email protected]>
- Loading branch information
Showing
9 changed files
with
761 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ targetConfigs/ | |
setenv | ||
.DS_Store | ||
bin/ | ||
dist/tools/vaina/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "vaina" | ||
version = "0.1.0" | ||
authors = ["Locha Inc"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
clap = { version = "2", features = ["color"] } | ||
bytes = "0.5" | ||
snafu = "0.6" | ||
nix = "0.17" | ||
|
||
[patch.crates-io] | ||
nix = { git = "https://github.com/nix-rust/nix" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::ffi::OsString; | ||
use std::net::{Ipv6Addr, SocketAddr, UdpSocket}; | ||
use std::os::unix::io::FromRawFd; | ||
use std::str::FromStr; | ||
|
||
use nix::sys::socket::sockopt::{BindToDevice, Ipv6AddMembership}; | ||
use nix::sys::socket::{ | ||
bind, setsockopt, socket, AddressFamily, InetAddr, Ipv6Addr as NixIpv6Addr, | ||
Ipv6MembershipRequest, SockAddr, SockFlag, SockProtocol, SockType, | ||
}; | ||
|
||
use snafu::ResultExt; | ||
|
||
use crate::msg::Message; | ||
use crate::*; | ||
|
||
/// VAINA multicast address | ||
pub const VAINA_MCAST_ADDR: &str = "ff15::42"; | ||
pub const VAINA_PORT: u16 = 1337; | ||
|
||
#[derive(Debug)] | ||
pub struct VainaClient { | ||
seqno: u8, | ||
pending_acks: Vec<u8>, | ||
sock: UdpSocket, | ||
group: SocketAddr, | ||
} | ||
|
||
impl VainaClient { | ||
/// New `VainaClient` | ||
pub fn new(netif: &OsString) -> Result<VainaClient, Error> { | ||
let group = Ipv6Addr::from_str(VAINA_MCAST_ADDR).unwrap(); | ||
let stdaddr = SocketAddr::new(group.clone().into(), VAINA_PORT); | ||
let sockaddr = SockAddr::new_inet(InetAddr::from_std(&stdaddr)); | ||
let fd = socket( | ||
AddressFamily::Inet6, | ||
SockType::Datagram, | ||
SockFlag::empty(), | ||
SockProtocol::Udp, | ||
) | ||
.context(VainaSocket)?; | ||
|
||
setsockopt(fd, BindToDevice, netif).context(VainaSocket)?; | ||
bind(fd, &sockaddr).context(VainaSocket)?; | ||
|
||
let group_req = Ipv6MembershipRequest::new(NixIpv6Addr::from_std(&group)); | ||
setsockopt(fd, Ipv6AddMembership, &group_req).context(VainaSocket)?; | ||
|
||
Ok(VainaClient { | ||
seqno: 0u8, | ||
pending_acks: Vec::new(), | ||
sock: unsafe { UdpSocket::from_raw_fd(fd) }, | ||
group: stdaddr, | ||
}) | ||
} | ||
|
||
/// Craft a new sequence number | ||
pub fn craft_seqno(&mut self) -> u8 { | ||
let seqno = self.seqno; | ||
self.seqno += 1; | ||
seqno | ||
} | ||
|
||
/// Send a message | ||
pub fn send_message(&mut self, msg: &Message) -> Result<(), Error> { | ||
// Verify if well wait for an ACK! | ||
if !msg.is_ack() { | ||
self.pending_acks.push(msg.seqno()); | ||
} | ||
|
||
let bytes = msg.serialize(); | ||
|
||
self.sock | ||
.send_to(bytes.as_ref(), &self.group) | ||
.context(FailedSend)?; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.