simple_ssdp/
lib.rs

1use std::convert::Into;
2use std::net::IpAddr;
3use std::net::Ipv4Addr;
4use std::net::Ipv6Addr;
5
6pub mod client;
7mod http_helper;
8pub mod service;
9
10#[cfg(test)]
11mod http_helper_test;
12mod socket_helper;
13
14#[derive(PartialEq)]
15/// The Multicast Address in use
16///
17/// This uses officially assigned addresses by IANA
18pub enum MulticastAddr {
19    /// The IPv4 Multicast address: `239.255.255.250`
20    ///
21    /// This is referred to as `site-local`
22    V4,
23
24    /// The IPv6 Multicast address: `ff02::c`
25    ///
26    /// This is referred to as `link-local`
27    V6LinkLocal,
28
29    /// The IPv6 Multicast address: `ff05::c`
30    ///
31    /// This is referred to as `site-local`
32    V6SiteLocal,
33
34    /// This is used only for test purposes or a local setup
35    /// 
36    /// Multicast is not redirected to the originating host, so we cannot run a Service and a Client on the same device
37    Loopback,
38}
39
40impl MulticastAddr {
41    /// Returns an [IpAddr] for the given enum value
42    pub fn get_ip(&self) -> IpAddr {
43        match self {
44            MulticastAddr::V4 => self.get_v4().unwrap().into(),
45            MulticastAddr::V6LinkLocal => self.get_v6().unwrap().into(),
46            MulticastAddr::V6SiteLocal => self.get_v6().unwrap().into(),
47            MulticastAddr::Loopback => self.get_v4().unwrap().into(),
48        }
49    }
50
51    /// Returns `true` if the enum value is IPv4, `false` if it's IPv6
52    pub fn is_v4(&self) -> bool {
53        match self {
54            MulticastAddr::V4 => true,
55            MulticastAddr::V6LinkLocal => false,
56            MulticastAddr::V6SiteLocal => false,
57            MulticastAddr::Loopback => true,
58        }
59    }
60
61    /// Returns the IPv4 addr if enum is an IPv4 addr
62    pub fn get_v4(&self) -> Option<Ipv4Addr> {
63        match self {
64            MulticastAddr::V4 => Some(Ipv4Addr::new(239, 255, 255, 250)),
65            MulticastAddr::V6LinkLocal => None,
66            MulticastAddr::V6SiteLocal => None,
67            MulticastAddr::Loopback => Some(Ipv4Addr::new(127, 0, 0, 1)),
68        }
69    }
70
71    /// Returns the IPv6 addr if enum is an IPv6 addr
72    pub fn get_v6(&self) -> Option<Ipv6Addr> {
73        match self {
74            MulticastAddr::V4 => None,
75            MulticastAddr::V6LinkLocal => Some(Ipv6Addr::new(0xFF02, 0, 0, 0, 0, 0, 0, 0xC)),
76            MulticastAddr::V6SiteLocal => Some(Ipv6Addr::new(0xFF05, 0, 0, 0, 0, 0, 0, 0xC)),
77            MulticastAddr::Loopback => None,
78        }
79    }
80}
81
82/// Port assigned by IANA for SSDP
83pub(crate) static SSDP_PORT: u16 = 1900;