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
use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
pub const MAX_IPV4_DATAGRAM_SIZE: usize = 508;
pub const MAX_IPV6_DATAGRAM_SIZE: usize = 1212;
pub fn bind(host: IpAddr, port: Vec<u16>) -> io::Result<UdpSocket> {
let addr_candidates = super::make_addr_list(host, port);
UdpSocket::bind(&addr_candidates[..])
}
pub fn connect(addr: SocketAddr) -> io::Result<UdpSocket> {
let socket = if addr.is_ipv4() {
bind(
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
super::IANA_EPHEMERAL_PORT_RANGE.collect(),
)?
} else {
bind(
IpAddr::V6(Ipv6Addr::UNSPECIFIED),
super::IANA_EPHEMERAL_PORT_RANGE.collect(),
)?
};
#[cfg(not(target_os = "macos"))]
socket.connect(addr)?;
Ok(socket)
}
pub fn local() -> io::Result<UdpSocket> {
bind(
IpAddr::from(Ipv4Addr::LOCALHOST),
super::IANA_EPHEMERAL_PORT_RANGE.collect(),
)
}