1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use actix::prelude::*;
use futures::prelude::*;
use tokio::prelude::*;

use codec::MdnsCodec;
use errors::{Error, ErrorKind, Result};
use futures::sync::oneshot;
use service::{ServiceInstance, ServicesDescription};
use socket2::{Domain, Protocol, Socket, Type};

use std::{
    collections::HashMap,
    net::{Ipv4Addr, SocketAddrV4},
};

use actix::AsyncContext;
use codec::ParsedPacket;
use continuous::{
    ContinuousInstancesList, ForeignMdnsQueryInfo, NewInstance, ReceivedMdnsInstance, Subscribe,
    Subscription,
};
use futures::sync::mpsc;
use gu_actix::FlattenFuture;
use service::{ServiceDescription, Services};
use std::{
    collections::HashSet,
    net::SocketAddr::{self, V4},
    time::Duration,
};
use tokio::{
    net::{UdpFramed, UdpSocket},
    reactor::Handle,
};

/// Actor resolving mDNS services names into list of IPs
#[derive(Debug, Default)]
pub struct MdnsActor<T: MdnsConnection> {
    /// Interior, indirect responder sink
    sender: Option<mpsc::Sender<((ServicesDescription, u16), SocketAddr)>>,
    data: Box<T>,
}

pub trait MdnsConnection: 'static + Default + Sized {
    fn port() -> u16;

    fn unicast_query() -> bool;

    fn handle_packet(&mut self, packet: ParsedPacket, src: SocketAddr);
}

pub type OneShotResponse<T> = ActorResponse<MdnsActor<T>, HashSet<ServiceInstance>, Error>;
pub type ContinuousResponse<T> = ActorResponse<MdnsActor<T>, Subscription, Error>;

#[derive(Debug, Default)]
pub struct OneShot {
    /// Next id for mDNS query
    next_id: u16,
    /// Services for given id
    map: HashMap<u16, Services>,
}

#[derive(Default)]
pub struct Continuous {
    /// Services for given id
    map: HashMap<String, Addr<ContinuousInstancesList>>,
}

impl MdnsConnection for OneShot {
    fn port() -> u16 {
        0
    }

    fn unicast_query() -> bool {
        true
    }

    fn handle_packet(&mut self, packet: ParsedPacket, src: SocketAddr) {
        if let Some(services) = self.map.get_mut(&packet.id) {
            for mut service in packet.instances {
                match src {
                    V4(sock) => service.addrs_v4 = biggest_mask_ipv4(&service.addrs_v4, sock.ip()),
                    _ => (),
                }

                services.add_instance(service);
            }
        }
    }
}

impl MdnsConnection for Continuous {
    fn port() -> u16 {
        5353
    }

    fn unicast_query() -> bool {
        false
    }

    fn handle_packet(&mut self, packet: ParsedPacket, _src: SocketAddr) {
        for name in packet.questions {
            self.map
                .get(&name)
                .map(|list| list.do_send(ForeignMdnsQueryInfo));
        }

        for instance in packet.instances {
            self.map
                .get(&instance.name)
                .map(|list| list.do_send(ReceivedMdnsInstance::new(instance)));
        }
    }
}

impl<T: MdnsConnection> MdnsActor<T> {
    pub fn new() -> Self {
        MdnsActor::default()
    }

    fn create_mdns_socket() -> Result<UdpSocket> {
        let socket = Socket::new(Domain::ipv4(), Type::dgram(), Some(Protocol::udp()))?;

        let multicast_ip = Ipv4Addr::new(224, 0, 0, 251);
        let any_ip = Ipv4Addr::new(0, 0, 0, 0);

        let socket_address = SocketAddrV4::new(any_ip, T::port());

        socket.set_reuse_address(true)?;
        socket.set_multicast_loop_v4(true)?;
        socket.join_multicast_v4(&multicast_ip, &any_ip)?;
        socket.bind(&socket_address.into())?;

        UdpSocket::from_std(socket.into_udp_socket(), &Handle::current()).map_err(Error::from)
    }
}

pub fn send_mdns_query(
    sender: Option<mpsc::Sender<((ServicesDescription, u16), SocketAddr)>>,
    services: ServicesDescription,
    id: u16,
) -> impl Future<Item = (), Error = Error> {
    let multicast_ip = Ipv4Addr::new(224, 0, 0, 251);
    let addr = SocketAddrV4::new(multicast_ip, 5353).into();

    let message = ((services, id), addr);

    match sender {
        Some(a) => future::Either::A(
            a.send(message)
                .and_then(|sender| Ok(sender.flush()))
                .and_then(|_| Ok(()))
                .map_err(|e| {
                    error!("{}", e);
                    ErrorKind::FutureSendError.into()
                }),
        ),
        None => future::Either::B(future::err(ErrorKind::ActorNotInitialized.into())),
    }
}

impl MdnsActor<OneShot> {
    fn retrieve_services(&mut self, id: u16) -> Result<HashSet<ServiceInstance>> {
        self.data
            .map
            .remove(&id)
            .ok_or(ErrorKind::MissingKey.into())
            .and_then(|a| Ok(a.collect()))
    }

    fn build_response<F>(
        &mut self,
        fut: F,
        _ctx: &mut Context<Self>,
        id: u16,
    ) -> OneShotResponse<OneShot>
    where
        F: Future<Item = (), Error = Error> + 'static,
    {
        let (tx, rx) = oneshot::channel();

        ActorResponse::async(fut.into_actor(self).and_then(move |_r, act, ctx| {
            ctx.run_later(Duration::from_secs(1), move |act, _ctx| {
                let _ = tx
                    .send(act.retrieve_services(id))
                    .and_then(|_a| Ok(()))
                    .map_err(|e| error!("{:?}", e));
            });
            rx.flatten_fut().into_actor(act)
        }))
    }
}

fn zeros(s1: &Ipv4Addr, s2: &Ipv4Addr) -> u8 {
    let oct1 = s1.octets();
    let oct2 = s2.octets();
    let mut zeros = 0;

    for pos in 0..3 {
        let add = (oct1[pos] ^ oct2[pos]).leading_zeros() as u8;
        zeros += add;

        if add != 8 {
            break;
        }
    }

    zeros
}

fn biggest_mask_ipv4(vec: &Vec<Ipv4Addr>, src: &Ipv4Addr) -> Vec<Ipv4Addr> {
    let mut best_instance = None;
    let mut best_score = 33;

    for ip in vec.iter() {
        if best_instance == None {
            let zeros = zeros(src, ip);
            if zeros < best_score {
                best_instance = Some(ip);
                best_score = zeros;
            }
        }
    }

    vec![*best_instance.unwrap_or(src)]
}

struct PacketPair {
    pub packet: ParsedPacket,
    pub socket: SocketAddr,
}

impl Message for PacketPair {
    type Result = ();
}

impl<T: MdnsConnection> Handler<PacketPair> for MdnsActor<T> {
    type Result = ();

    fn handle(&mut self, msg: PacketPair, _ctx: &mut Context<MdnsActor<T>>) -> () {
        T::handle_packet(&mut self.data, msg.packet, msg.socket)
    }
}

impl<T: MdnsConnection> Actor for MdnsActor<T> {
    type Context = Context<Self>;

    /// Creates stream handler for incoming mDNS packets
    fn started(&mut self, ctx: &mut Self::Context) {
        use futures::Stream;

        let socket = Self::create_mdns_socket().expect("Creation of mDNS socket failed");
        let (sink, stream) = UdpFramed::new(socket, MdnsCodec(T::unicast_query())).split();

        ctx.add_message_stream(
            stream
                .map(|(packet, socket)| PacketPair { packet, socket })
                .map_err(|_| ()),
        );

        let (tx, rx) = mpsc::channel(16);
        ctx.spawn(
            rx.map_err(|_| ErrorKind::UninitializedChannelReceiver)
                .forward(sink)
                .map_err(|e| error!("{:?}", e))
                .and_then(|_| Ok(()))
                .into_actor(self),
        );

        self.sender = Some(tx);
    }
}

impl<T: MdnsConnection> Supervised for MdnsActor<T> {}

impl<T: MdnsConnection> SystemService for MdnsActor<T> {}

impl Handler<ServicesDescription> for MdnsActor<OneShot> {
    type Result = OneShotResponse<OneShot>;

    fn handle(
        &mut self,
        msg: ServicesDescription,
        ctx: &mut Self::Context,
    ) -> OneShotResponse<OneShot> {
        let id = self.data.next_id;
        self.data.next_id = id.wrapping_add(1);

        self.data.map.insert(id, Services::from(&msg));
        let future = send_mdns_query(self.sender.clone(), msg, id);

        self.build_response(future, ctx, id)
    }
}

pub struct SubscribeInstance {
    pub service: ServiceDescription,
    pub rec: Recipient<NewInstance>,
}

impl Message for SubscribeInstance {
    type Result = Result<Subscription>;
}

impl Handler<SubscribeInstance> for MdnsActor<Continuous> {
    type Result = ContinuousResponse<Continuous>;

    fn handle(
        &mut self,
        msg: SubscribeInstance,
        _ctx: &mut Self::Context,
    ) -> ContinuousResponse<Continuous> {
        use std::collections::hash_map::Entry;
        let list = ContinuousInstancesList::new(msg.service.clone(), self.sender.clone().unwrap());

        let res = match self.data.map.entry(msg.service.to_string()) {
            Entry::Vacant(a) => {
                let service = list.start();
                a.insert(service.clone());
                service.send(Subscribe { rec: msg.rec })
            }
            Entry::Occupied(ref mut b) => {
                if b.get().connected() {
                    b.get_mut().send(Subscribe { rec: msg.rec })
                } else {
                    let service = list.start();
                    b.insert(service.clone());
                    service.send(Subscribe { rec: msg.rec })
                }
            }
        };

        ActorResponse::async(res.map_err(|_| ErrorKind::Mailbox.into()).into_actor(self))
    }
}

#[cfg(test)]
mod tests {
    use actor::{MdnsActor, OneShot};

    #[test]
    fn create_mdns_socket() {
        let socket = MdnsActor::<OneShot>::create_mdns_socket();

        assert!(socket.is_ok());
    }
}