1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 use std::fmt::Formatter; 15 use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr}; 16 use std::os::windows::io::{AsRawSocket, RawSocket}; 17 use std::{fmt, io, net}; 18 19 use crate::source::Fd; 20 use crate::sys::windows::udp::UdpSock; 21 use crate::sys::NetState; 22 use crate::{Interest, Selector, Source, Token}; 23 24 /// A UDP socket. 25 pub struct UdpSocket { 26 pub(crate) inner: net::UdpSocket, 27 /// State is None if the socket has not been Registered. 28 pub(crate) state: NetState, 29 } 30 31 impl UdpSocket { 32 /// Binds a new UdpSocket to the specific address. 33 /// 34 /// # Examples 35 /// 36 /// ```no_run 37 /// use ylong_io::UdpSocket; 38 /// 39 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 40 /// let sender = UdpSocket::bind(sender_addr).expect("UdpSocket bind fail!"); 41 /// ``` bind(addr: SocketAddr) -> io::Result<UdpSocket>42 pub fn bind(addr: SocketAddr) -> io::Result<UdpSocket> { 43 let sock = UdpSock::new_socket(addr)?; 44 sock.bind(addr).map(UdpSocket::from_std) 45 } 46 47 /// Connects peer address to get ConnectedUdpSocket. 48 /// 49 /// # Examples 50 /// 51 /// ```no_run 52 /// use ylong_io::UdpSocket; 53 /// 54 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 55 /// let receiver_addr = "127.0.0.1:8082".parse().unwrap(); 56 /// 57 /// let sender = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 58 /// let connected_sender = sender 59 /// .connect(receiver_addr) 60 /// .expect("Connect Socket Failed!"); 61 /// ``` connect(self, addr: SocketAddr) -> io::Result<ConnectedUdpSocket>62 pub fn connect(self, addr: SocketAddr) -> io::Result<ConnectedUdpSocket> { 63 let socket = ConnectedUdpSocket::from_std(self); 64 socket.inner.connect(addr)?; 65 Ok(socket) 66 } 67 68 /// Convert net::UdpSocket to ylong_io::UdpSocket from_std(socket: net::UdpSocket) -> UdpSocket69 pub fn from_std(socket: net::UdpSocket) -> UdpSocket { 70 UdpSocket { 71 inner: socket, 72 state: NetState::new(), 73 } 74 } 75 76 /// Returns the socket address that this socket was created from. 77 /// 78 /// # Examples 79 /// 80 /// ```no_run 81 /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; 82 /// 83 /// use ylong_io::UdpSocket; 84 /// 85 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 86 /// let socket = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 87 /// assert_eq!( 88 /// socket.local_addr().unwrap(), 89 /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8081)) 90 /// ); 91 /// ``` local_addr(&self) -> io::Result<SocketAddr>92 pub fn local_addr(&self) -> io::Result<SocketAddr> { 93 self.inner.local_addr() 94 } 95 96 /// Sends data on the socket to the given address. On success, returns the 97 /// number of bytes written. 98 /// 99 /// # Examples 100 /// 101 /// ```no_run 102 /// use ylong_io::UdpSocket; 103 /// 104 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 105 /// let receiver_addr = "127.0.0.1:8082".parse().unwrap(); 106 /// 107 /// let socket = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 108 /// socket 109 /// .send_to(&[0; 10], receiver_addr) 110 /// .expect("Send Socket Failed!"); 111 /// ``` send_to(&self, buf: &[u8], target: SocketAddr) -> io::Result<usize>112 pub fn send_to(&self, buf: &[u8], target: SocketAddr) -> io::Result<usize> { 113 self.state 114 .try_io(|inner| inner.send_to(buf, target), &self.inner) 115 } 116 117 /// Receives a single datagram message on the socket. On success, returns 118 /// the number of bytes read and the origin. 119 /// 120 /// # Examples 121 /// 122 /// ```no_run 123 /// use ylong_io::UdpSocket; 124 /// 125 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 126 /// 127 /// let socket = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 128 /// let mut buf = [0; 10]; 129 /// let (number_of_bytes, src_addr) = socket.recv_from(&mut buf).expect("Didn't receive data"); 130 /// let filled_buf = &mut buf[..number_of_bytes]; 131 /// ``` recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)>132 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { 133 self.state.try_io(|inner| inner.recv_from(buf), &self.inner) 134 } 135 136 /// Receives single datagram on the socket from the remote address to which 137 /// it is connected, without removing the message from input queue. On 138 /// success, returns the number of bytes peeked. 139 /// 140 /// # Examples 141 /// 142 /// ```no_run 143 /// use ylong_io::UdpSocket; 144 /// 145 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 146 /// 147 /// let socket = UdpSocket::bind(sender_addr).expect("couldn't bind to address"); 148 /// let mut buf = [0; 10]; 149 /// let (number_of_bytes, src_addr) = socket.peek_from(&mut buf).expect("Didn't receive data"); 150 /// let filled_buf = &mut buf[..number_of_bytes]; 151 /// ``` peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)>152 pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> { 153 self.state.try_io(|inner| inner.peek_from(buf), &self.inner) 154 } 155 156 /// Sets the value of the SO_BROADCAST option for this socket. 157 /// 158 /// # Examples 159 /// 160 /// ```no_run 161 /// use ylong_io::UdpSocket; 162 /// 163 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 164 /// 165 /// let socket = UdpSocket::bind(sender_addr).expect("couldn't bind to address"); 166 /// socket 167 /// .set_broadcast(false) 168 /// .expect("set_broadcast call failed"); 169 /// ``` set_broadcast(&self, on: bool) -> io::Result<()>170 pub fn set_broadcast(&self, on: bool) -> io::Result<()> { 171 self.inner.set_broadcast(on) 172 } 173 174 /// Gets the value of the SO_BROADCAST option for this socket. 175 /// 176 /// # Examples 177 /// 178 /// ```no_run 179 /// use ylong_io::UdpSocket; 180 /// 181 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 182 /// 183 /// let socket = UdpSocket::bind(sender_addr).expect("couldn't bind to address"); 184 /// socket 185 /// .set_broadcast(false) 186 /// .expect("set_broadcast call failed"); 187 /// assert_eq!(socket.broadcast().unwrap(), false); 188 /// ``` broadcast(&self) -> io::Result<bool>189 pub fn broadcast(&self) -> io::Result<bool> { 190 self.inner.broadcast() 191 } 192 193 /// Sets the value for the IP_TTL option on this socket. 194 /// 195 /// # Examples 196 /// 197 /// ```no_run 198 /// use ylong_io::UdpSocket; 199 /// 200 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 201 /// let socket = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 202 /// socket.set_ttl(100).expect("set_ttl call failed"); 203 /// ``` set_ttl(&self, ttl: u32) -> io::Result<()>204 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> { 205 self.inner.set_ttl(ttl) 206 } 207 208 /// Sets the value for the IP_TTL option on this socket. 209 /// 210 /// # Examples 211 /// 212 /// ```no_run 213 /// use ylong_io::UdpSocket; 214 /// 215 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 216 /// let socket = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 217 /// socket.set_ttl(100).expect("set_ttl call failed"); 218 /// assert_eq!(socket.ttl().unwrap(), 100); 219 /// ``` ttl(&self) -> io::Result<u32>220 pub fn ttl(&self) -> io::Result<u32> { 221 self.inner.ttl() 222 } 223 224 /// Gets the value of the SO_ERROR option on this socket. 225 /// This will retrieve the stored error in the underlying socket, clearing 226 /// the field in the process. This can be useful for checking errors between 227 /// calls. 228 /// 229 /// # Examples 230 /// 231 /// ```no_run 232 /// use ylong_io::UdpSocket; 233 /// 234 /// let addr = "127.0.0.1:34254".parse().unwrap(); 235 /// let socket = UdpSocket::bind(addr).expect("couldn't bind to address"); 236 /// match socket.take_error() { 237 /// Ok(Some(error)) => println!("UdpSocket error: {error:?}"), 238 /// Ok(None) => println!("No error"), 239 /// Err(error) => println!("UdpSocket.take_error failed: {error:?}"), 240 /// } 241 /// ``` take_error(&self) -> io::Result<Option<io::Error>>242 pub fn take_error(&self) -> io::Result<Option<io::Error>> { 243 self.inner.take_error() 244 } 245 246 /// Gets the value of the IP_MULTICAST_LOOP option for this socket. multicast_loop_v4(&self) -> io::Result<bool>247 pub fn multicast_loop_v4(&self) -> io::Result<bool> { 248 self.inner.multicast_loop_v4() 249 } 250 251 /// Sets the value of the IP_MULTICAST_LOOP option for this socket. 252 /// If enabled, multicast packets will be looped back to the local socket. 253 /// Note that this might not have any effect on IPv6 sockets. set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()>254 pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> { 255 self.inner.set_multicast_loop_v4(multicast_loop_v4) 256 } 257 258 /// Gets the value of the IP_MULTICAST_TTL option for this socket. multicast_ttl_v4(&self) -> io::Result<u32>259 pub fn multicast_ttl_v4(&self) -> io::Result<u32> { 260 self.inner.multicast_ttl_v4() 261 } 262 263 /// Sets the value of the IP_MULTICAST_TTL option for this socket. 264 /// Indicates the time-to-live value of outgoing multicast packets for this 265 /// socket. The default value is 1 which means that multicast packets don't 266 /// leave the local network unless explicitly requested. Note that this 267 /// might not have any effect on IPv6 sockets. set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()>268 pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> { 269 self.inner.set_multicast_ttl_v4(multicast_ttl_v4) 270 } 271 272 /// Gets the value of the IPV6_MULTICAST_LOOP option for this socket. multicast_loop_v6(&self) -> io::Result<bool>273 pub fn multicast_loop_v6(&self) -> io::Result<bool> { 274 self.inner.multicast_loop_v6() 275 } 276 277 /// Sets the value of the IPV6_MULTICAST_LOOP option for this socket. 278 /// Controls whether this socket sees the multicast packets it sends itself. 279 /// Note that this might not have any affect on IPv4 sockets. set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()>280 pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> { 281 self.inner.set_multicast_loop_v6(multicast_loop_v6) 282 } 283 284 /// Executes an operation of the IP_ADD_MEMBERSHIP type. join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()>285 pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> { 286 self.inner.join_multicast_v4(multiaddr, interface) 287 } 288 289 /// Executes an operation of the IPV6_ADD_MEMBERSHIP type. join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()>290 pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> { 291 self.inner.join_multicast_v6(multiaddr, interface) 292 } 293 294 /// Executes an operation of the IP_DROP_MEMBERSHIP type. leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()>295 pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> { 296 self.inner.leave_multicast_v4(multiaddr, interface) 297 } 298 299 /// Executes an operation of the IPV6_DROP_MEMBERSHIP type. leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()>300 pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> { 301 self.inner.leave_multicast_v6(multiaddr, interface) 302 } 303 } 304 305 /// UdpSocket which has been connected. 306 pub struct ConnectedUdpSocket { 307 pub(crate) inner: net::UdpSocket, 308 /// State is None if the socket has not been Registered. 309 pub(crate) state: NetState, 310 } 311 312 impl ConnectedUdpSocket { 313 /// Convert net::UdpSocket to ylong_io::ConnectedUdpSocket from_std(socket: UdpSocket) -> ConnectedUdpSocket314 pub fn from_std(socket: UdpSocket) -> ConnectedUdpSocket { 315 ConnectedUdpSocket { 316 inner: socket.inner, 317 state: socket.state, 318 } 319 } 320 321 /// Returns the socket address that this socket was created from. 322 /// 323 /// # Examples 324 /// 325 /// ```no_run 326 /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; 327 /// 328 /// use ylong_io::UdpSocket; 329 /// 330 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 331 /// let receiver_addr = "127.0.0.1:8082".parse().unwrap(); 332 /// 333 /// let sender = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 334 /// let connected_sender = sender 335 /// .connect(receiver_addr) 336 /// .expect("Connect Socket Failed!"); 337 /// 338 /// assert_eq!( 339 /// connected_sender.local_addr().unwrap(), 340 /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8081)) 341 /// ); 342 /// ``` local_addr(&self) -> io::Result<SocketAddr>343 pub fn local_addr(&self) -> io::Result<SocketAddr> { 344 self.inner.local_addr() 345 } 346 347 /// Returns the socket address of the remote peer to which the socket is 348 /// connected. 349 /// 350 /// # Examples 351 /// 352 /// ```no_run 353 /// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; 354 /// 355 /// use ylong_io::UdpSocket; 356 /// 357 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 358 /// let receiver_addr = "127.0.0.1:8082".parse().unwrap(); 359 /// 360 /// let sender = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 361 /// let connected_sender = sender 362 /// .connect(receiver_addr) 363 /// .expect("Connect Socket Failed!"); 364 /// 365 /// assert_eq!( 366 /// connected_sender.peer_addr().unwrap(), 367 /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8082)) 368 /// ); 369 /// ``` peer_addr(&self) -> io::Result<SocketAddr>370 pub fn peer_addr(&self) -> io::Result<SocketAddr> { 371 self.inner.peer_addr() 372 } 373 374 /// Sets the value for the IP_TTL option on this socket. 375 /// 376 /// # Examples 377 /// 378 /// ```no_run 379 /// use ylong_io::UdpSocket; 380 /// 381 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 382 /// let socket = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 383 /// let receiver_addr = "127.0.0.1:8081".parse().unwrap(); 384 /// let connect_socket = socket 385 /// .connect(receiver_addr) 386 /// .expect("Connect Socket Failed!"); 387 /// connect_socket.set_ttl(100).expect("set_ttl call failed"); 388 /// ``` set_ttl(&self, ttl: u32) -> io::Result<()>389 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> { 390 self.inner.set_ttl(ttl) 391 } 392 393 /// Sets the value for the IP_TTL option on this socket. 394 /// 395 /// # Examples 396 /// 397 /// ```no_run 398 /// use ylong_io::UdpSocket; 399 /// 400 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 401 /// let socket = UdpSocket::bind(sender_addr).expect("Bind Socket Failed!"); 402 /// let receiver_addr = "127.0.0.1:8081".parse().unwrap(); 403 /// let connect_socket = socket 404 /// .connect(receiver_addr) 405 /// .expect("Connect Socket Failed!"); 406 /// connect_socket.set_ttl(100).expect("set_ttl call failed"); 407 /// assert_eq!(connect_socket.ttl().unwrap(), 100); 408 /// ``` ttl(&self) -> io::Result<u32>409 pub fn ttl(&self) -> io::Result<u32> { 410 self.inner.ttl() 411 } 412 413 /// Sends data on the socket to the remote address to which it is connected. 414 /// 415 /// # Examples 416 /// 417 /// ```no_run 418 /// use ylong_io::UdpSocket; 419 /// 420 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 421 /// let receiver_addr = "127.0.0.1:8082".parse().unwrap(); 422 /// 423 /// let socket = UdpSocket::bind(sender_addr).expect("couldn't bind to address"); 424 /// let connected_sender = socket 425 /// .connect(receiver_addr) 426 /// .expect("connect function failed"); 427 /// connected_sender 428 /// .send(&[0, 1, 2]) 429 /// .expect("couldn't send message"); 430 /// ``` send(&self, buf: &[u8]) -> io::Result<usize>431 pub fn send(&self, buf: &[u8]) -> io::Result<usize> { 432 self.state.try_io(|inner| inner.send(buf), &self.inner) 433 } 434 435 /// Receives a single datagram message on the socket from the remote address 436 /// to which it is connected. On success, returns the number of bytes read. 437 /// 438 /// # Examples 439 /// 440 /// ```no_run 441 /// use ylong_io::UdpSocket; 442 /// 443 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 444 /// let receiver_addr = "127.0.0.1:8082".parse().unwrap(); 445 /// 446 /// let socket = UdpSocket::bind(sender_addr).expect("couldn't bind to address"); 447 /// let connected_sender = socket 448 /// .connect(receiver_addr) 449 /// .expect("connect function failed"); 450 /// let mut buf = [0; 10]; 451 /// match connected_sender.recv(&mut buf) { 452 /// Ok(received) => println!("received {} bytes {:?}", received, &buf[..received]), 453 /// Err(e) => println!("recv function failed: {:?}", e), 454 /// } 455 /// ``` recv(&self, buf: &mut [u8]) -> io::Result<usize>456 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> { 457 self.state.try_io(|inner| inner.recv(buf), &self.inner) 458 } 459 460 /// Receives single datagram on the socket from the remote address to which 461 /// it is connected, without removing the message from input queue. On 462 /// success, returns the number of bytes peeked. 463 /// 464 /// # Examples 465 /// 466 /// ```no_run 467 /// use ylong_io::UdpSocket; 468 /// 469 /// let sender_addr = "127.0.0.1:8081".parse().unwrap(); 470 /// let receiver_addr = "127.0.0.1:8082".parse().unwrap(); 471 /// 472 /// let socket = UdpSocket::bind(sender_addr).expect("couldn't bind to address"); 473 /// let connect_socket = socket 474 /// .connect(receiver_addr) 475 /// .expect("connect function failed"); 476 /// let mut buf = [0; 10]; 477 /// match connect_socket.peek(&mut buf) { 478 /// Ok(received) => println!("received {received} bytes"), 479 /// Err(e) => println!("peek function failed: {e:?}"), 480 /// } 481 /// ``` peek(&self, buf: &mut [u8]) -> io::Result<usize>482 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> { 483 self.state.try_io(|inner| inner.peek(buf), &self.inner) 484 } 485 486 /// Sets the value of the `SO_BROADCAST` option for this socket. 487 /// When enabled, this socket is allowed to send packets to a broadcast 488 /// address. 489 /// 490 /// # Examples 491 /// 492 /// ```rust 493 /// use std::io; 494 /// 495 /// use ylong_io::UdpSocket; 496 /// 497 /// async fn io_func() -> io::Result<()> { 498 /// let local_addr = "127.0.0.1:8080".parse().unwrap(); 499 /// let receiver_addr = "127.0.0.1:8081".parse().unwrap(); 500 /// let socket = UdpSocket::bind(local_addr)?; 501 /// let connect_socket = socket 502 /// .connect(receiver_addr) 503 /// .expect("connect function failed"); 504 /// if connect_socket.broadcast()? == false { 505 /// connect_socket.set_broadcast(true)?; 506 /// } 507 /// assert_eq!(connect_socket.broadcast()?, true); 508 /// Ok(()) 509 /// } 510 /// ``` set_broadcast(&self, on: bool) -> io::Result<()>511 pub fn set_broadcast(&self, on: bool) -> io::Result<()> { 512 self.inner.set_broadcast(on) 513 } 514 515 /// Gets the value of the `SO_BROADCAST` option for this socket. 516 /// 517 /// # Examples 518 /// 519 /// ```rust 520 /// use std::io; 521 /// 522 /// use ylong_io::UdpSocket; 523 /// 524 /// async fn io_func() -> io::Result<()> { 525 /// let local_addr = "127.0.0.1:8080".parse().unwrap(); 526 /// let receiver_addr = "127.0.0.1:8081".parse().unwrap(); 527 /// let socket = UdpSocket::bind(local_addr)?; 528 /// let connect_socket = socket 529 /// .connect(receiver_addr) 530 /// .expect("connect function failed"); 531 /// assert_eq!(connect_socket.broadcast()?, false); 532 /// Ok(()) 533 /// } 534 /// ``` broadcast(&self) -> io::Result<bool>535 pub fn broadcast(&self) -> io::Result<bool> { 536 self.inner.broadcast() 537 } 538 539 /// Gets the value of the IP_MULTICAST_LOOP option for this socket. multicast_loop_v4(&self) -> io::Result<bool>540 pub fn multicast_loop_v4(&self) -> io::Result<bool> { 541 self.inner.multicast_loop_v4() 542 } 543 544 /// Sets the value of the IP_MULTICAST_LOOP option for this socket. 545 /// If enabled, multicast packets will be looped back to the local socket. 546 /// Note that this might not have any effect on IPv6 sockets. set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()>547 pub fn set_multicast_loop_v4(&self, multicast_loop_v4: bool) -> io::Result<()> { 548 self.inner.set_multicast_loop_v4(multicast_loop_v4) 549 } 550 551 /// Gets the value of the IP_MULTICAST_TTL option for this socket. multicast_ttl_v4(&self) -> io::Result<u32>552 pub fn multicast_ttl_v4(&self) -> io::Result<u32> { 553 self.inner.multicast_ttl_v4() 554 } 555 556 /// Sets the value of the IP_MULTICAST_TTL option for this socket. 557 /// Indicates the time-to-live value of outgoing multicast packets for this 558 /// socket. The default value is 1 which means that multicast packets don't 559 /// leave the local network unless explicitly requested. Note that this 560 /// might not have any effect on IPv6 sockets. set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()>561 pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> { 562 self.inner.set_multicast_ttl_v4(multicast_ttl_v4) 563 } 564 565 /// Gets the value of the IPV6_MULTICAST_LOOP option for this socket. multicast_loop_v6(&self) -> io::Result<bool>566 pub fn multicast_loop_v6(&self) -> io::Result<bool> { 567 self.inner.multicast_loop_v6() 568 } 569 570 /// Sets the value of the IPV6_MULTICAST_LOOP option for this socket. 571 /// Controls whether this socket sees the multicast packets it sends itself. 572 /// Note that this might not have any affect on IPv4 sockets. set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()>573 pub fn set_multicast_loop_v6(&self, multicast_loop_v6: bool) -> io::Result<()> { 574 self.inner.set_multicast_loop_v6(multicast_loop_v6) 575 } 576 577 /// Executes an operation of the IP_ADD_MEMBERSHIP type. join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()>578 pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> { 579 self.inner.join_multicast_v4(multiaddr, interface) 580 } 581 582 /// Executes an operation of the IPV6_ADD_MEMBERSHIP type. join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()>583 pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> { 584 self.inner.join_multicast_v6(multiaddr, interface) 585 } 586 587 /// Executes an operation of the IP_DROP_MEMBERSHIP type. leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()>588 pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> io::Result<()> { 589 self.inner.leave_multicast_v4(multiaddr, interface) 590 } 591 592 /// Executes an operation of the IPV6_DROP_MEMBERSHIP type. leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()>593 pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> io::Result<()> { 594 self.inner.leave_multicast_v6(multiaddr, interface) 595 } 596 597 /// Gets the value of the SO_ERROR option on this socket. 598 /// This will retrieve the stored error in the underlying socket, clearing 599 /// the field in the process. This can be useful for checking errors between 600 /// calls. 601 /// 602 /// # Examples 603 /// 604 /// ```no_run 605 /// use ylong_io::UdpSocket; 606 /// 607 /// let addr = "127.0.0.1:34253".parse().unwrap(); 608 /// let receiver_addr = "127.0.0.1:34254".parse().unwrap(); 609 /// let socket = UdpSocket::bind(addr).expect("couldn't bind to address"); 610 /// let connected_sender = socket 611 /// .connect(receiver_addr) 612 /// .expect("connect function failed"); 613 /// match connected_sender.take_error() { 614 /// Ok(Some(error)) => println!("ConnectedUdpSocket error: {error:?}"), 615 /// Ok(None) => println!("No error"), 616 /// Err(error) => println!("ConnectedUdpSocket.take_error failed: {error:?}"), 617 /// } 618 /// ``` take_error(&self) -> io::Result<Option<io::Error>>619 pub fn take_error(&self) -> io::Result<Option<io::Error>> { 620 self.inner.take_error() 621 } 622 } 623 624 impl fmt::Debug for UdpSocket { fmt(&self, f: &mut Formatter<'_>) -> fmt::Result625 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 626 self.inner.fmt(f) 627 } 628 } 629 630 impl fmt::Debug for ConnectedUdpSocket { fmt(&self, f: &mut Formatter<'_>) -> fmt::Result631 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { 632 self.inner.fmt(f) 633 } 634 } 635 636 impl Source for UdpSocket { register( &mut self, selector: &Selector, token: Token, interests: Interest, ) -> io::Result<()>637 fn register( 638 &mut self, 639 selector: &Selector, 640 token: Token, 641 interests: Interest, 642 ) -> io::Result<()> { 643 self.state 644 .register(selector, token, interests, self.get_fd()) 645 } 646 deregister(&mut self, _selector: &Selector) -> io::Result<()>647 fn deregister(&mut self, _selector: &Selector) -> io::Result<()> { 648 self.state.deregister() 649 } 650 get_fd(&self) -> Fd651 fn get_fd(&self) -> Fd { 652 self.inner.as_raw_socket() 653 } 654 } 655 656 impl Source for ConnectedUdpSocket { register( &mut self, selector: &Selector, token: Token, interests: Interest, ) -> io::Result<()>657 fn register( 658 &mut self, 659 selector: &Selector, 660 token: Token, 661 interests: Interest, 662 ) -> io::Result<()> { 663 self.state 664 .register(selector, token, interests, self.get_fd()) 665 } 666 deregister(&mut self, _selector: &Selector) -> io::Result<()>667 fn deregister(&mut self, _selector: &Selector) -> io::Result<()> { 668 self.state.deregister() 669 } 670 get_fd(&self) -> Fd671 fn get_fd(&self) -> Fd { 672 self.inner.as_raw_socket() 673 } 674 } 675 676 impl AsRawSocket for UdpSocket { as_raw_socket(&self) -> RawSocket677 fn as_raw_socket(&self) -> RawSocket { 678 self.inner.as_raw_socket() 679 } 680 } 681 682 impl AsRawSocket for ConnectedUdpSocket { as_raw_socket(&self) -> RawSocket683 fn as_raw_socket(&self) -> RawSocket { 684 self.inner.as_raw_socket() 685 } 686 } 687