@@ -344,6 +344,29 @@ impl UdpSocket {
344344 }
345345
346346 match dst {
347+ SocketAddr :: V4 ( dst) if dst. ip ( ) . is_broadcast ( ) => {
348+ let host = world. current_host ( ) ;
349+ match host. udp . is_broadcast_enabled ( src. port ( ) ) {
350+ true => world
351+ . hosts
352+ . iter ( )
353+ . filter ( |( _, host) | host. udp . is_port_assigned ( dst. port ( ) ) )
354+ . map ( |( addr, _) | SocketAddr :: new ( * addr, dst. port ( ) ) )
355+ . collect :: < Vec < _ > > ( )
356+ . into_iter ( )
357+ . try_for_each ( |dst| match dst {
358+ dst if src. ip ( ) == dst. ip ( ) => {
359+ send_loopback ( src, dst, Protocol :: Udp ( packet. clone ( ) ) ) ;
360+ Ok ( ( ) )
361+ }
362+ dst => world. send_message ( src, dst, Protocol :: Udp ( packet. clone ( ) ) ) ,
363+ } ) ,
364+ false => Err ( Error :: new (
365+ ErrorKind :: PermissionDenied ,
366+ "Broadcast is not enabled" ,
367+ ) ) ,
368+ }
369+ }
347370 dst if dst. ip ( ) . is_multicast ( ) => world
348371 . multicast_groups
349372 . destination_addresses ( dst)
@@ -357,12 +380,38 @@ impl UdpSocket {
357380 Ok ( ( ) )
358381 }
359382 dst => world. send_message ( src, dst, Protocol :: Udp ( packet. clone ( ) ) ) ,
360- } ) ?,
361- dst if is_same ( src, dst) => send_loopback ( src, dst, Protocol :: Udp ( packet) ) ,
362- _ => world. send_message ( src, dst, Protocol :: Udp ( packet) ) ?,
383+ } ) ,
384+ dst if is_same ( src, dst) => {
385+ send_loopback ( src, dst, Protocol :: Udp ( packet) ) ;
386+ Ok ( ( ) )
387+ }
388+ _ => world. send_message ( src, dst, Protocol :: Udp ( packet) ) ,
363389 }
390+ }
364391
365- Ok ( ( ) )
392+ /// Gets the value of the `SO_BROADCAST` option for this socket.
393+ ///
394+ /// For more information about this option, see [`set_broadcast`].
395+ ///
396+ /// [`set_broadcast`]: method@Self::set_broadcast
397+ pub fn broadcast ( & self ) -> io:: Result < bool > {
398+ let local_port = self . local_addr . port ( ) ;
399+ World :: current ( |world| Ok ( world. current_host ( ) . udp . is_broadcast_enabled ( local_port) ) )
400+ }
401+
402+ /// Sets the value of the `SO_BROADCAST` option for this socket.
403+ ///
404+ /// When enabled, this socket is allowed to send packets to a broadcast
405+ /// address.
406+ pub fn set_broadcast ( & self , on : bool ) -> io:: Result < ( ) > {
407+ let local_port = match self . local_addr {
408+ SocketAddr :: V4 ( addr) => addr. port ( ) ,
409+ _ => return Ok ( ( ) ) ,
410+ } ;
411+ World :: current ( |world| {
412+ world. current_host_mut ( ) . udp . set_broadcast ( local_port, on) ;
413+ Ok ( ( ) )
414+ } )
366415 }
367416
368417 /// Gets the value of the `IP_MULTICAST_LOOP` option for this socket.
0 commit comments