zenoh_util/net/
mod.rs

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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//
// Copyright (c) 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
use std::net::{IpAddr, Ipv6Addr};

#[cfg(unix)]
use lazy_static::lazy_static;
#[cfg(unix)]
use pnet_datalink::NetworkInterface;
use tokio::net::{TcpSocket, UdpSocket};
use zenoh_core::zconfigurable;
#[cfg(unix)]
use zenoh_result::zerror;
use zenoh_result::{bail, ZResult};

zconfigurable! {
    static ref WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE: u32 = 8192;
    static ref WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES: u32 = 3;
}

#[cfg(unix)]
lazy_static! {
    static ref IFACES: Vec<NetworkInterface> = pnet_datalink::interfaces();
}

#[cfg(windows)]
unsafe fn get_adapters_addresses(af_spec: i32) -> ZResult<Vec<u8>> {
    use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

    let mut ret;
    let mut retries = 0;
    let mut size: u32 = *WINDOWS_GET_ADAPTERS_ADDRESSES_BUF_SIZE;
    let mut buffer: Vec<u8>;
    loop {
        buffer = Vec::with_capacity(size as usize);
        ret = winapi::um::iphlpapi::GetAdaptersAddresses(
            af_spec.try_into().unwrap(),
            0,
            std::ptr::null_mut(),
            buffer.as_mut_ptr() as *mut IP_ADAPTER_ADDRESSES_LH,
            &mut size,
        );
        if ret != winapi::shared::winerror::ERROR_BUFFER_OVERFLOW {
            break;
        }
        if retries >= *WINDOWS_GET_ADAPTERS_ADDRESSES_MAX_RETRIES {
            break;
        }
        retries += 1;
    }

    if ret != 0 {
        bail!("GetAdaptersAddresses returned {}", ret)
    }

    Ok(buffer)
}
pub fn get_interface(name: &str) -> ZResult<Option<IpAddr>> {
    #[cfg(unix)]
    {
        for iface in IFACES.iter() {
            if iface.name == name {
                for ifaddr in &iface.ips {
                    if ifaddr.is_ipv4() {
                        return Ok(Some(ifaddr.ip()));
                    }
                }
            }
            for ifaddr in &iface.ips {
                if ifaddr.ip().to_string() == name {
                    return Ok(Some(ifaddr.ip()));
                }
            }
        }
        Ok(None)
    }

    #[cfg(windows)]
    {
        unsafe {
            use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

            use crate::ffi;

            let buffer = get_adapters_addresses(winapi::shared::ws2def::AF_INET)?;

            let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
            while let Some(iface) = next_iface {
                if name == ffi::pstr_to_string(iface.AdapterName)
                    || name == ffi::pwstr_to_string(iface.FriendlyName)
                    || name == ffi::pwstr_to_string(iface.Description)
                {
                    let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref();
                    while let Some(ucast_addr) = next_ucast_addr {
                        if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) {
                            if ifaddr.is_ipv4() {
                                return Ok(Some(ifaddr.ip()));
                            }
                        }
                        next_ucast_addr = ucast_addr.Next.as_ref();
                    }
                }

                let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref();
                while let Some(ucast_addr) = next_ucast_addr {
                    if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) {
                        if ifaddr.ip().to_string() == name {
                            return Ok(Some(ifaddr.ip()));
                        }
                    }
                    next_ucast_addr = ucast_addr.Next.as_ref();
                }
                next_iface = iface.Next.as_ref();
            }
            Ok(None)
        }
    }
}

/// Get the network interface to bind the UDP sending port to when not specified by user
pub fn get_multicast_interfaces() -> Vec<IpAddr> {
    #[cfg(unix)]
    {
        IFACES
            .iter()
            .filter_map(|iface| {
                if iface.is_up() && iface.is_running() && iface.is_multicast() {
                    for ipaddr in &iface.ips {
                        if ipaddr.is_ipv4() {
                            return Some(ipaddr.ip());
                        }
                    }
                }
                None
            })
            .collect()
    }
    #[cfg(windows)]
    {
        // On windows, bind to [::], the system will select the default interface
        vec![IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)]
    }
}

pub fn get_local_addresses(interface: Option<&str>) -> ZResult<Vec<IpAddr>> {
    #[cfg(unix)]
    {
        Ok(IFACES
            .iter()
            .filter(|iface| {
                if let Some(interface) = interface.as_ref() {
                    if iface.name != *interface {
                        return false;
                    }
                }
                iface.is_up() && iface.is_running()
            })
            .flat_map(|iface| iface.ips.clone())
            .map(|ipnet| ipnet.ip())
            .collect())
    }

    #[cfg(windows)]
    {
        unsafe {
            use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

            use crate::ffi;

            let buffer = get_adapters_addresses(winapi::shared::ws2def::AF_UNSPEC)?;

            let mut result = vec![];
            let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
            while let Some(iface) = next_iface {
                if let Some(interface) = interface.as_ref() {
                    if ffi::pstr_to_string(iface.AdapterName) != *interface {
                        continue;
                    }
                }
                let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref();
                while let Some(ucast_addr) = next_ucast_addr {
                    if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) {
                        result.push(ifaddr.ip());
                    }
                    next_ucast_addr = ucast_addr.Next.as_ref();
                }
                next_iface = iface.Next.as_ref();
            }
            Ok(result)
        }
    }
}

/// Get the network interface to bind the UDP sending port to when not specified by user
pub fn get_unicast_addresses_of_multicast_interfaces() -> Vec<IpAddr> {
    #[cfg(unix)]
    {
        IFACES
            .iter()
            .filter(|iface| iface.is_up() && iface.is_running() && iface.is_multicast())
            .flat_map(|iface| {
                iface
                    .ips
                    .iter()
                    .filter(|ip| !ip.ip().is_multicast())
                    .map(|x| x.ip())
                    .collect::<Vec<IpAddr>>()
            })
            .collect()
    }
    #[cfg(windows)]
    {
        // On windows, bind to [::] or [::], the system will select the default interface
        vec![]
    }
}

pub fn get_unicast_addresses_of_interface(name: &str) -> ZResult<Vec<IpAddr>> {
    #[cfg(unix)]
    {
        match IFACES.iter().find(|iface| iface.name == name) {
            Some(iface) => {
                if !iface.is_up() {
                    bail!("Interface {name} is not up");
                }
                if !iface.is_running() {
                    bail!("Interface {name} is not running");
                }
                let addrs = iface
                    .ips
                    .iter()
                    .filter(|ip| !ip.ip().is_multicast())
                    .map(|x| x.ip())
                    .collect::<Vec<IpAddr>>();
                Ok(addrs)
            }
            None => bail!("Interface {name} not found"),
        }
    }

    #[cfg(windows)]
    {
        unsafe {
            use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

            use crate::ffi;

            let buffer = get_adapters_addresses(winapi::shared::ws2def::AF_INET)?;

            let mut addrs = vec![];
            let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
            while let Some(iface) = next_iface {
                if name == ffi::pstr_to_string(iface.AdapterName)
                    || name == ffi::pwstr_to_string(iface.FriendlyName)
                    || name == ffi::pwstr_to_string(iface.Description)
                {
                    let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref();
                    while let Some(ucast_addr) = next_ucast_addr {
                        if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) {
                            addrs.push(ifaddr.ip());
                        }
                        next_ucast_addr = ucast_addr.Next.as_ref();
                    }
                }
                next_iface = iface.Next.as_ref();
            }
            Ok(addrs)
        }
    }
}

pub fn get_index_of_interface(addr: IpAddr) -> ZResult<u32> {
    #[cfg(unix)]
    {
        IFACES
            .iter()
            .find(|iface| iface.ips.iter().any(|ipnet| ipnet.ip() == addr))
            .map(|iface| iface.index)
            .ok_or_else(|| zerror!("No interface found with address {addr}").into())
    }
    #[cfg(windows)]
    {
        unsafe {
            use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

            use crate::ffi;

            let buffer = get_adapters_addresses(winapi::shared::ws2def::AF_INET)?;

            let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
            while let Some(iface) = next_iface {
                let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref();
                while let Some(ucast_addr) = next_ucast_addr {
                    if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) {
                        if ifaddr.ip() == addr {
                            return Ok(iface.Ipv6IfIndex);
                        }
                    }
                    next_ucast_addr = ucast_addr.Next.as_ref();
                }
                next_iface = iface.Next.as_ref();
            }
            bail!("No interface found with address {addr}")
        }
    }
}

pub fn get_interface_names_by_addr(addr: IpAddr) -> ZResult<Vec<String>> {
    #[cfg(unix)]
    {
        if addr.is_unspecified() {
            Ok(IFACES
                .iter()
                .map(|iface| iface.name.clone())
                .collect::<Vec<String>>())
        } else {
            Ok(IFACES
                .iter()
                .filter(|iface| iface.ips.iter().any(|ipnet| ipnet.ip() == addr))
                .map(|iface| iface.name.clone())
                .collect::<Vec<String>>())
        }
    }
    #[cfg(windows)]
    {
        let mut result = vec![];
        unsafe {
            use winapi::um::iptypes::IP_ADAPTER_ADDRESSES_LH;

            use crate::ffi;

            let buffer = get_adapters_addresses(winapi::shared::ws2def::AF_UNSPEC)?;

            if addr.is_unspecified() {
                let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
                while let Some(iface) = next_iface {
                    result.push(ffi::pstr_to_string(iface.AdapterName));
                    next_iface = iface.Next.as_ref();
                }
            } else {
                let mut next_iface = (buffer.as_ptr() as *mut IP_ADAPTER_ADDRESSES_LH).as_ref();
                while let Some(iface) = next_iface {
                    let mut next_ucast_addr = iface.FirstUnicastAddress.as_ref();
                    while let Some(ucast_addr) = next_ucast_addr {
                        if let Ok(ifaddr) = ffi::win::sockaddr_to_addr(ucast_addr.Address) {
                            if ifaddr.ip() == addr {
                                result.push(ffi::pstr_to_string(iface.AdapterName));
                            }
                        }
                        next_ucast_addr = ucast_addr.Next.as_ref();
                    }
                    next_iface = iface.Next.as_ref();
                }
            }
        }
        Ok(result)
    }
}

pub fn get_ipv4_ipaddrs(interface: Option<&str>) -> Vec<IpAddr> {
    get_local_addresses(interface)
        .unwrap_or_else(|_| vec![])
        .drain(..)
        .filter_map(|x| match x {
            IpAddr::V4(a) => Some(a),
            IpAddr::V6(_) => None,
        })
        .filter(|x| !x.is_loopback() && !x.is_multicast())
        .map(IpAddr::V4)
        .collect()
}

pub fn get_ipv6_ipaddrs(interface: Option<&str>) -> Vec<IpAddr> {
    const fn is_unicast_link_local(addr: &Ipv6Addr) -> bool {
        (addr.segments()[0] & 0xffc0) == 0xfe80
    }

    let ipaddrs = get_local_addresses(interface).unwrap_or_else(|_| vec![]);

    // Get first all IPv4 addresses
    let ipv4_iter = ipaddrs
        .iter()
        .filter_map(|x| match x {
            IpAddr::V4(a) => Some(a),
            IpAddr::V6(_) => None,
        })
        .filter(|x| {
            !x.is_loopback() && !x.is_link_local() && !x.is_multicast() && !x.is_broadcast()
        });

    // Get next all IPv6 addresses
    let ipv6_iter = ipaddrs.iter().filter_map(|x| match x {
        IpAddr::V4(_) => None,
        IpAddr::V6(a) => Some(a),
    });

    // First match non-linklocal IPv6 addresses
    let nll_ipv6_addrs = ipv6_iter
        .clone()
        .filter(|x| !x.is_loopback() && !x.is_multicast() && !is_unicast_link_local(x))
        .map(|x| IpAddr::V6(*x));

    // Second match public IPv4 addresses
    let pub_ipv4_addrs = ipv4_iter
        .clone()
        .filter(|x| !x.is_private())
        .map(|x| IpAddr::V4(*x));

    // Third match linklocal IPv6 addresses
    let yll_ipv6_addrs = ipv6_iter
        .filter(|x| !x.is_loopback() && !x.is_multicast() && is_unicast_link_local(x))
        .map(|x| IpAddr::V6(*x));

    // Fourth match private IPv4 addresses
    let priv_ipv4_addrs = ipv4_iter
        .clone()
        .filter(|x| x.is_private())
        .map(|x| IpAddr::V4(*x));

    // Extend
    nll_ipv6_addrs
        .chain(pub_ipv4_addrs)
        .chain(yll_ipv6_addrs)
        .chain(priv_ipv4_addrs)
        .collect()
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn set_bind_to_device_tcp_socket(socket: &TcpSocket, iface: &str) -> ZResult<()> {
    socket.bind_device(Some(iface.as_bytes()))?;
    Ok(())
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn set_bind_to_device_udp_socket(socket: &UdpSocket, iface: &str) -> ZResult<()> {
    socket.bind_device(Some(iface.as_bytes()))?;
    Ok(())
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
pub fn set_bind_to_device_tcp_socket(socket: &TcpSocket, iface: &str) -> ZResult<()> {
    tracing::warn!("Binding the socket {socket:?} to the interface {iface} is not supported on macOS and Windows");
    Ok(())
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
pub fn set_bind_to_device_udp_socket(socket: &UdpSocket, iface: &str) -> ZResult<()> {
    tracing::warn!("Binding the socket {socket:?} to the interface {iface} is not supported on macOS and Windows");
    Ok(())
}