| 75 | } |
| 76 | |
| 77 | fn create_inet_socket(addr: IpAddr) -> Result<net::UdpSocket> { |
| 78 | let domain = match addr { |
| 79 | IpAddr::V4(_) => libc::AF_INET, |
| 80 | IpAddr::V6(_) => libc::AF_INET6, |
| 81 | }; |
| 82 | |
| 83 | // SAFETY: we check the return value. |
| 84 | let sock = unsafe { libc::socket(domain, libc::SOCK_DGRAM, 0) }; |
| 85 | if sock < 0 { |
| 86 | return Err(Error::CreateSocket(IoError::last_os_error())); |
| 87 | } |
| 88 | |
| 89 | // SAFETY: nothing else will use or hold onto the raw sock fd. |
| 90 | Ok(unsafe { net::UdpSocket::from_raw_fd(sock) }) |
| 91 | } |
| 92 | |
| 93 | fn create_unix_socket() -> Result<net::UdpSocket> { |
| 94 | // SAFETY: we check the return value. |