Skip to content

Commit ff0fd8e

Browse files
tisonkunBBArikL
andauthored
feat: impl BroadcastSender (#8)
Signed-off-by: tison <[email protected]> Co-authored-by: Arik <[email protected]>
1 parent 236ceca commit ff0fd8e

File tree

7 files changed

+72
-2
lines changed

7 files changed

+72
-2
lines changed

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ doc-scrape-examples = true
6565
name = "udp_sender"
6666
path = "examples/udp_sender.rs"
6767

68+
[[example]]
69+
doc-scrape-examples = true
70+
name = "broadcast_sender"
71+
path = "examples/broadcast_sender.rs"
72+
6873
[[example]]
6974
doc-scrape-examples = true
7075
name = "unix_sender"

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Client library written in Rust to send messages to a Syslog server. Support impl
2424
* RFC-5424 Formatter: [The Syslog Protocol](https://datatracker.ietf.org/doc/html/rfc5424)
2525
* `UdpSender`: [RFC 5426 - Transmission of Syslog Messages over UDP](https://datatracker.ietf.org/doc/html/rfc5426)
2626
* `TcpSender`: [RFC 6587 - Transmission of Syslog Messages over TCP](https://datatracker.ietf.org/doc/html/rfc6587)
27+
* `TlsSender`: [RFC 5425 - Transport Layer Security (TLS) Transport Mapping for Syslog](https://datatracker.ietf.org/doc/html/rfc5425)
28+
* This implementation is based on [`native-tls`](https://crates.io/crates/native-tls) and requires features `native-tls` turned on.
2729
* (unix only) Unix domain socket sender (datagram or stream)
2830

2931
## Getting Started

examples/broadcast_sender.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2024 FastLabs Developers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use fasyslog::Severity;
16+
17+
fn main() {
18+
let mut sender = fasyslog::sender::broadcast_well_known().unwrap();
19+
let mut generator = names::Generator::default();
20+
for _ in 0..100 {
21+
let name = generator.next().unwrap();
22+
let message = format!("Broadcast {name} to everyone!");
23+
sender
24+
.send_rfc5424(Severity::ERROR, None::<String>, Vec::new(), message)
25+
.unwrap();
26+
}
27+
}

src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
//! * [RFC-5424 Formatter]: [The Syslog Protocol](https://datatracker.ietf.org/doc/html/rfc5424)
2525
//! * [`UdpSender`]: [RFC 5426 - Transmission of Syslog Messages over UDP](https://datatracker.ietf.org/doc/html/rfc5426)
2626
//! * [`TcpSender`]: [RFC 6587 - Transmission of Syslog Messages over TCP](https://datatracker.ietf.org/doc/html/rfc6587)
27+
//! * [`TlsSender`]: [RFC 5425 - Transport Layer Security (TLS) Transport Mapping for Syslog](https://datatracker.ietf.org/doc/html/rfc5425)
28+
//! * This implementation is based on [`native-tls`](https://crates.io/crates/native-tls) and
29+
//! requires features `native-tls` turned on.
2730
//! * (unix only) Unix domain socket sender (datagram or stream)
2831
//!
2932
//! [RFC-3164 Formatter]: format::RFC3164Formatter
3033
//! [RFC-5424 Formatter]: format::RFC5424Formatter
3134
//! [`UdpSender`]: sender::UdpSender
3235
//! [`TcpSender`]: sender::TcpSender
36+
//! [`TlsSender`]: sender::TlsSender
3337
//!
3438
//! # Example
3539
//!

src/sender/native_tls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ pub fn tls_with<A: ToSocketAddrs, S: AsRef<str>>(
5151
}
5252

5353
/// A syslog sender that sends messages to a TCP socket over TLS.
54+
///
55+
/// Users can obtain a `TlsSender` by calling [`tls_well_known`], [`tls`], or [`tls_with`].
5456
#[derive(Debug)]
5557
pub struct TlsSender {
5658
writer: BufWriter<TlsStream<TcpStream>>,

src/sender/tcp.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub fn tcp<A: ToSocketAddrs>(addr: A) -> io::Result<TcpSender> {
3737
}
3838

3939
/// A syslog sender that sends messages to a TCP socket.
40+
///
41+
/// Users can obtain a `TcpSender` by calling [`tcp_well_known`] or [`tcp`].
4042
#[derive(Debug)]
4143
pub struct TcpSender {
4244
writer: BufWriter<TcpStream>,

src/sender/udp.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,27 @@ pub fn udp<L: ToSocketAddrs, R: ToSocketAddrs>(local: L, remote: R) -> io::Resul
3333
UdpSender::connect(local, remote)
3434
}
3535

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+
3653
/// 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`].
3757
#[derive(Debug)]
3858
pub struct UdpSender {
3959
socket: UdpSocket,
@@ -45,10 +65,18 @@ impl UdpSender {
4565
pub fn connect<L: ToSocketAddrs, R: ToSocketAddrs>(local: L, remote: R) -> io::Result<Self> {
4666
let socket = UdpSocket::bind(local)?;
4767
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 {
4977
socket,
5078
context: SyslogContext::default(),
51-
})
79+
}
5280
}
5381

5482
/// Set the context when formatting Syslog message.

0 commit comments

Comments
 (0)