@@ -33,7 +33,27 @@ pub fn udp<L: ToSocketAddrs, R: ToSocketAddrs>(local: L, remote: R) -> io::Resul
33
33
UdpSender :: connect ( local, remote)
34
34
}
35
35
36
+ /// Create a UDP sender that broadcast messages to the well-known port (514).
37
+ ///
38
+ /// See also [RFC-3164] §2 Transport Layer Protocol.
39
+ ///
40
+ /// [RFC-3164]: https://datatracker.ietf.org/doc/html/rfc3164#section-2
41
+ pub fn broadcast_well_known ( ) -> io:: Result < UdpSender > {
42
+ broadcast ( 514 )
43
+ }
44
+
45
+ /// Create a UDP sender that broadcast messages to the given port.
46
+ pub fn broadcast ( port : u16 ) -> io:: Result < UdpSender > {
47
+ let socket = UdpSocket :: bind ( "0.0.0.0:0" ) ?;
48
+ socket. set_broadcast ( true ) ?;
49
+ socket. connect ( format ! ( "255.255.255.255:{port}" ) ) ?;
50
+ Ok ( UdpSender :: new ( socket) )
51
+ }
52
+
36
53
/// A syslog sender that sends messages to a UDP socket.
54
+ ///
55
+ /// Users can obtain a `UdpSender` by calling [`udp_well_known`], [`udp`], [`broadcast_well_known`],
56
+ /// or [`broadcast`].
37
57
#[ derive( Debug ) ]
38
58
pub struct UdpSender {
39
59
socket : UdpSocket ,
@@ -45,10 +65,18 @@ impl UdpSender {
45
65
pub fn connect < L : ToSocketAddrs , R : ToSocketAddrs > ( local : L , remote : R ) -> io:: Result < Self > {
46
66
let socket = UdpSocket :: bind ( local) ?;
47
67
socket. connect ( remote) ?;
48
- Ok ( Self {
68
+ Ok ( Self :: new ( socket) )
69
+ }
70
+
71
+ /// Create a new UDP sender with the given socket.
72
+ ///
73
+ /// This is useful when users want to configure the socket in fine-grained. Note that the
74
+ /// passed `socket` MUST be connected to the remote address.
75
+ pub fn new ( socket : UdpSocket ) -> Self {
76
+ Self {
49
77
socket,
50
78
context : SyslogContext :: default ( ) ,
51
- } )
79
+ }
52
80
}
53
81
54
82
/// Set the context when formatting Syslog message.
0 commit comments