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

Bumped to futures 0.3 / tokio 0.2 and rust 2018 edition #3

Closed
wants to merge 8 commits into from
Closed
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
26 changes: 14 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
name = "libmdns"
version = "0.2.4"
authors = ["Will Stott <[email protected]>"]

description = "mDNS Responder library for building discoverable LAN services in Rust"
repository = "https://github.com/librespot-org/libmdns"
readme = "README.md"
license = "MIT"
edition = "2018"

[dependencies]
byteorder = "1.2"
futures = "0.1"
get_if_addrs = "0.5"
hostname = "0.2"
log = "0.4"
multimap = "0.4"
net2 = "0.2"
rand = "0.5"
tokio-core = "0.1"
quick-error = "1.2"
byteorder = "1.3.4"
futures = "0.3.4"
get_if_addrs = "0.5.3"
hostname = "0.3.1"
log = "0.4.8"
multimap = "0.8.0"
net2 = "0.2.33"
rand = "0.7.3"
tokio = { version = "0.2.13", features = ["udp","stream","io-driver","io-std","macros","signal"] }
quick-error = "1.2.3"
pin-project = "0.4.8"


[dev-dependencies]
env_logger = "0.5"
env_logger = "0.7.1"
14 changes: 7 additions & 7 deletions examples/register.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
extern crate env_logger;
extern crate libmdns;
use env_logger;
use libmdns;

pub fn main() {
#[tokio::main]
pub async fn main() {
env_logger::init();

let responder = libmdns::Responder::new().unwrap();
let responder = libmdns::Responder::builder().use_v6(false).build().unwrap();

let _svc = responder.register(
"_http._tcp".to_owned(),
"libmdns Web Server".to_owned(),
80,
&["path=/"],
);
Comment on lines +8 to 15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to add more options, what do you think about a Builder for each service? Then extending the options for each published Service instead.


loop {
::std::thread::sleep(::std::time::Duration::from_secs(10));
}
tokio::signal::ctrl_c().await.unwrap();
}
7 changes: 7 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
merge_derives=true
merge_imports=true
normalize_comments=true
reorder_impl_items=true
reorder_modules=true
use_try_shorthand=true
use_field_init_shorthand=true
128 changes: 76 additions & 52 deletions src/dns_parser/builder.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::marker::PhantomData;

use byteorder::{ByteOrder, BigEndian, WriteBytesExt};
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};

use super::{Opcode, ResponseCode, Header, Name, RRData, QueryType, QueryClass};
use super::{Header, Name, Opcode, QueryClass, QueryType, RRData, ResponseCode};

pub enum Questions {}
pub enum Answers {}
#[allow(dead_code)] pub enum Nameservers {}
#[allow(dead_code)]
pub enum Nameservers {}
pub enum Additional {}

pub trait MoveTo<T> { }
impl <T> MoveTo<T> for T {}
pub trait MoveTo<T> {}
impl<T> MoveTo<T> for T {}

impl MoveTo<Answers> for Questions {}

Expand Down Expand Up @@ -40,7 +41,7 @@ impl Builder<Questions> {
pub fn new_query(id: u16, recursion: bool) -> Builder<Questions> {
let mut buf = Vec::with_capacity(512);
let head = Header {
id: id,
id,
query: true,
opcode: Opcode::StandardQuery,
authoritative: false,
Expand All @@ -55,16 +56,20 @@ impl Builder<Questions> {
};
buf.extend([0u8; 12].iter());
head.write(&mut buf[..12]);
Builder { buf: buf, max_size: Some(512), _state: PhantomData }
Builder {
buf,
max_size: Some(512),
_state: PhantomData,
}
}

pub fn new_response(id: u16, recursion: bool, authoritative: bool) -> Builder<Questions> {
let mut buf = Vec::with_capacity(512);
let head = Header {
id: id,
id,
query: false,
opcode: Opcode::StandardQuery,
authoritative: authoritative,
authoritative,
truncated: false,
recursion_desired: recursion,
recursion_available: false,
Expand All @@ -76,14 +81,16 @@ impl Builder<Questions> {
};
buf.extend([0u8; 12].iter());
head.write(&mut buf[..12]);
Builder { buf: buf, max_size: Some(512), _state: PhantomData }
Builder {
buf,
max_size: Some(512),
_state: PhantomData,
}
}
}

impl <T> Builder<T> {
fn write_rr(&mut self, name: &Name,
cls: QueryClass, ttl: u32, data: &RRData) {

impl<T> Builder<T> {
fn write_rr(&mut self, name: &Name, cls: QueryClass, ttl: u32, data: &RRData) {
name.write_to(&mut self.buf).unwrap();
self.buf.write_u16::<BigEndian>(data.typ() as u16).unwrap();
self.buf.write_u16::<BigEndian>(cls as u16).unwrap();
Expand All @@ -96,7 +103,10 @@ impl <T> Builder<T> {
data.write_to(&mut self.buf).unwrap();
let data_size = self.buf.len() - data_offset;

BigEndian::write_u16(&mut self.buf[size_offset..size_offset+2], data_size as u16);
BigEndian::write_u16(
&mut self.buf[size_offset..size_offset + 2],
data_size as u16,
);
}

/// Returns the final packet
Expand All @@ -113,7 +123,7 @@ impl <T> Builder<T> {
/// appropriate.
// TODO(tailhook) does the truncation make sense for TCP, and how
// to treat it for EDNS0?
pub fn build(mut self) -> Result<Vec<u8>,Vec<u8>> {
pub fn build(mut self) -> Result<Vec<u8>, Vec<u8>> {
// TODO(tailhook) optimize labels
match self.max_size {
Some(max_size) if self.buf.len() > max_size => {
Expand All @@ -124,97 +134,111 @@ impl <T> Builder<T> {
}
}

pub fn move_to<U>(self) -> Builder<U> where T: MoveTo<U> {
Builder { buf: self.buf, max_size: self.max_size, _state: PhantomData }
pub fn move_to<U>(self) -> Builder<U>
where
T: MoveTo<U>,
{
Builder {
buf: self.buf,
max_size: self.max_size,
_state: PhantomData,
}
}

pub fn set_max_size(&mut self, max_size: Option<usize>) {
self.max_size = max_size;
}

pub fn is_empty(&self) -> bool {
Header::question_count(&self.buf) == 0 &&
Header::answer_count(&self.buf) == 0 &&
Header::nameserver_count(&self.buf) == 0 &&
Header::additional_count(&self.buf) == 0
Header::question_count(&self.buf) == 0
&& Header::answer_count(&self.buf) == 0
&& Header::nameserver_count(&self.buf) == 0
&& Header::additional_count(&self.buf) == 0
}
}

impl <T: MoveTo<Questions>> Builder<T> {
impl<T: MoveTo<Questions>> Builder<T> {
/// Adds a question to the packet
///
/// # Panics
///
/// * There are already 65535 questions in the buffer.
#[allow(dead_code)]
pub fn add_question(self, qname: &Name,
qtype: QueryType, qclass: QueryClass)
-> Builder<Questions>
{
pub fn add_question(
self,
qname: &Name,
qtype: QueryType,
qclass: QueryClass,
) -> Builder<Questions> {
let mut builder = self.move_to::<Questions>();

qname.write_to(&mut builder.buf).unwrap();
builder.buf.write_u16::<BigEndian>(qtype as u16).unwrap();
builder.buf.write_u16::<BigEndian>(qclass as u16).unwrap();
Header::inc_questions(&mut builder.buf)
.expect("Too many questions");
Header::inc_questions(&mut builder.buf).expect("Too many questions");
builder
}
}

impl <T: MoveTo<Answers>> Builder<T> {
pub fn add_answer(self, name: &Name,
cls: QueryClass, ttl: u32, data: &RRData)
-> Builder<Answers>
{
impl<T: MoveTo<Answers>> Builder<T> {
pub fn add_answer(
self,
name: &Name,
cls: QueryClass,
ttl: u32,
data: &RRData,
) -> Builder<Answers> {
let mut builder = self.move_to::<Answers>();

builder.write_rr(name, cls, ttl, data);
Header::inc_answers(&mut builder.buf)
.expect("Too many answers");
Header::inc_answers(&mut builder.buf).expect("Too many answers");

builder
}
}

impl <T: MoveTo<Nameservers>> Builder<T> {
impl<T: MoveTo<Nameservers>> Builder<T> {
#[allow(dead_code)]
pub fn add_nameserver(self, name: &Name,
cls: QueryClass, ttl: u32, data: &RRData)
-> Builder<Nameservers>
{
pub fn add_nameserver(
self,
name: &Name,
cls: QueryClass,
ttl: u32,
data: &RRData,
) -> Builder<Nameservers> {
let mut builder = self.move_to::<Nameservers>();

builder.write_rr(name, cls, ttl, data);
Header::inc_nameservers(&mut builder.buf)
.expect("Too many nameservers");
Header::inc_nameservers(&mut builder.buf).expect("Too many nameservers");

builder
}
}

impl Builder<Additional> {
#[allow(dead_code)]
pub fn add_additional(self, name: &Name,
cls: QueryClass, ttl: u32, data: &RRData)
-> Builder<Additional>
{
pub fn add_additional(
self,
name: &Name,
cls: QueryClass,
ttl: u32,
data: &RRData,
) -> Builder<Additional> {
let mut builder = self.move_to::<Additional>();

builder.write_rr(name, cls, ttl, data);
Header::inc_nameservers(&mut builder.buf)
.expect("Too many additional answers");
Header::inc_nameservers(&mut builder.buf).expect("Too many additional answers");

builder
}
}

#[cfg(test)]
mod test {
use super::QueryType as QT;
use super::QueryClass as QC;
use super::Name;
use super::Builder;
use super::Name;
use super::QueryClass as QC;
use super::QueryType as QT;

#[test]
fn build_query() {
Expand Down
Loading