1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
//! Networking data types.
//!
//! There may a standard for embedded networking in the future, see
//! [rust-embedded issue 348] and [RFC 2832]
//!
//! This is mostly ripped from [`std::net`].
//!
//! [rust-embedded issue 348]: https://github.com/rust-embedded/wg/issues/348
//! [`std::net`]: https://doc.rust-lang.org/std/net/index.html
//! [RFC 2832]: https://github.com/rust-lang/rfcs/pull/2832
#[cfg(feature = "ip_in_core")]
pub use core::net::{Ipv4Addr, SocketAddrV4};
/// Ipv4Addr address struct.
///
/// Can be instantiated with [`Ipv4Addr::new`].
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Default)]
#[cfg(not(feature = "ip_in_core"))]
pub struct Ipv4Addr {
/// Octets of the [`Ipv4Addr`] address.
octets: [u8; 4],
}
#[cfg(not(feature = "ip_in_core"))]
impl Ipv4Addr {
/// Creates a new IPv4 address from four eight-bit octets.
///
/// The result will represent the IP address `a`.`b`.`c`.`d`.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
/// ```
#[allow(clippy::many_single_char_names)]
#[must_use]
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
Ipv4Addr {
octets: [a, b, c, d],
}
}
/// An IPv4 address with the address pointing to localhost: 127.0.0.1.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::LOCALHOST;
/// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1));
/// ```
pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1);
/// An IPv4 address representing an unspecified address: 0.0.0.0
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::UNSPECIFIED;
/// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0));
/// ```
pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0);
/// An IPv4 address representing the broadcast address: 255.255.255.255
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::BROADCAST;
/// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255));
/// ```
pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255);
/// Returns the four eight-bit integers that make up this address.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::new(127, 0, 0, 1);
/// assert_eq!(addr.octets(), [127, 0, 0, 1]);
/// ```
#[must_use]
#[inline]
pub const fn octets(&self) -> [u8; 4] {
self.octets
}
/// Returns [`true`] for the special 'unspecified' address (0.0.0.0).
///
/// This property is defined in _UNIX Network Programming, Second Edition_,
/// W. Richard Stevens, p. 891; see also [ip7].
///
/// [ip7]: http://man7.org/linux/man-pages/man7/ip.7.html
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true);
/// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false);
/// ```
#[must_use]
pub const fn is_unspecified(&self) -> bool {
self.octets[0] == 0 && self.octets[1] == 0 && self.octets[2] == 0 && self.octets[3] == 0
}
/// Returns [`true`] if this is a loopback address (127.0.0.0/8).
///
/// This property is defined by [IETF RFC 1122].
///
/// [IETF RFC 1122]: https://tools.ietf.org/html/rfc1122
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true);
/// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false);
/// ```
#[must_use]
pub const fn is_loopback(&self) -> bool {
self.octets[0] == 127
}
/// Returns [`true`] if this is a private address.
///
/// The private address ranges are defined in [IETF RFC 1918] and include:
///
/// - 10.0.0.0/8
/// - 172.16.0.0/12
/// - 192.168.0.0/16
///
/// [IETF RFC 1918]: https://tools.ietf.org/html/rfc1918
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true);
/// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true);
/// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true);
/// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true);
/// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false);
/// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true);
/// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false);
/// ```
#[allow(clippy::manual_range_contains)]
#[must_use]
pub const fn is_private(&self) -> bool {
match self.octets {
[10, ..] => true,
[172, b, ..] if b >= 16 && b <= 31 => true,
[192, 168, ..] => true,
_ => false,
}
}
/// Returns [`true`] if the address is link-local (169.254.0.0/16).
///
/// This property is defined by [IETF RFC 3927].
///
/// [IETF RFC 3927]: https://tools.ietf.org/html/rfc3927
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true);
/// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true);
/// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false);
/// ```
#[must_use]
pub const fn is_link_local(&self) -> bool {
matches!(self.octets, [169, 254, ..])
}
/// Returns [`true`] if this is a multicast address (224.0.0.0/4).
///
/// Multicast addresses have a most significant octet between 224 and 239,
/// and is defined by [IETF RFC 5771].
///
/// [IETF RFC 5771]: https://tools.ietf.org/html/rfc5771
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true);
/// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true);
/// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false);
/// ```
#[must_use]
pub const fn is_multicast(&self) -> bool {
self.octets[0] >= 224 && self.octets[0] <= 239
}
/// Returns [`true`] if this is a broadcast address (255.255.255.255).
///
/// A broadcast address has all octets set to 255 as defined in [IETF RFC 919].
///
/// [IETF RFC 919]: https://tools.ietf.org/html/rfc919
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true);
/// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false);
/// ```
#[must_use]
pub const fn is_broadcast(&self) -> bool {
u32::from_be_bytes(self.octets) == u32::from_be_bytes(Self::BROADCAST.octets)
}
/// Returns [`true`] if this address is in a range designated for documentation.
///
/// This is defined in [IETF RFC 5737]:
///
/// - 192.0.2.0/24 (TEST-NET-1)
/// - 198.51.100.0/24 (TEST-NET-2)
/// - 203.0.113.0/24 (TEST-NET-3)
///
/// [IETF RFC 5737]: https://tools.ietf.org/html/rfc5737
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true);
/// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true);
/// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true);
/// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false);
/// ```
#[allow(clippy::match_like_matches_macro)]
#[must_use]
pub const fn is_documentation(&self) -> bool {
match self.octets {
[192, 0, 2, _] => true,
[198, 51, 100, _] => true,
[203, 0, 113, _] => true,
_ => false,
}
}
}
#[cfg(not(feature = "ip_in_core"))]
impl ::core::fmt::Display for Ipv4Addr {
/// String formatter for [`Ipv4Addr`] addresses.
fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(
fmt,
"{}.{}.{}.{}",
self.octets[0], self.octets[1], self.octets[2], self.octets[3],
)
}
}
#[cfg(not(feature = "ip_in_core"))]
impl From<[u8; 4]> for Ipv4Addr {
/// Creates an `Ipv4Addr` from a four element byte array.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Ipv4Addr;
///
/// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]);
/// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr);
/// ```
fn from(octets: [u8; 4]) -> Self {
Ipv4Addr { octets }
}
}
#[cfg(all(feature = "defmt", not(feature = "ip_in_core")))]
impl defmt::Format for Ipv4Addr {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(
fmt,
"{}.{}.{}.{}",
self.octets[0],
self.octets[1],
self.octets[2],
self.octets[3]
)
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<std::net::Ipv4Addr> for Ipv4Addr {
fn from(ip: std::net::Ipv4Addr) -> Self {
Ipv4Addr::new(
ip.octets()[0],
ip.octets()[1],
ip.octets()[2],
ip.octets()[3],
)
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<&std::net::Ipv4Addr> for Ipv4Addr {
fn from(ip: &std::net::Ipv4Addr) -> Self {
Ipv4Addr::new(
ip.octets()[0],
ip.octets()[1],
ip.octets()[2],
ip.octets()[3],
)
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<Ipv4Addr> for std::net::Ipv4Addr {
fn from(ip: Ipv4Addr) -> Self {
std::net::Ipv4Addr::new(ip.octets[0], ip.octets[1], ip.octets[2], ip.octets[3])
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<&Ipv4Addr> for std::net::Ipv4Addr {
fn from(ip: &Ipv4Addr) -> Self {
std::net::Ipv4Addr::new(ip.octets[0], ip.octets[1], ip.octets[2], ip.octets[3])
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<Ipv4Addr> for std::net::IpAddr {
fn from(ip: Ipv4Addr) -> Self {
std::net::IpAddr::V4(ip.into())
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<&Ipv4Addr> for std::net::IpAddr {
fn from(ip: &Ipv4Addr) -> Self {
std::net::IpAddr::V4(ip.into())
}
}
/// EUI-48 MAC address struct.
///
/// Can be instantiated with [`Eui48Addr::new`].
///
/// This is an EUI-48 [MAC address] (previously called MAC-48).
///
/// [MAC address]: https://en.wikipedia.org/wiki/MAC_address
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash, Default)]
pub struct Eui48Addr {
/// Octets of the MAC address.
pub octets: [u8; 6],
}
impl Eui48Addr {
/// Creates a new EUI-48 MAC address from six eight-bit octets.
///
/// The result will represent the EUI-48 MAC address
/// `a`:`b`:`c`:`d`:`e`:`f`.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Eui48Addr;
///
/// let addr = Eui48Addr::new(0x00, 0x00, 0x5E, 0x00, 0x00, 0x00);
/// ```
#[allow(clippy::many_single_char_names)]
pub const fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> Eui48Addr {
Eui48Addr {
octets: [a, b, c, d, e, f],
}
}
/// An EUI-48 MAC address representing an unspecified address:
/// 00:00:00:00:00:00
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Eui48Addr;
///
/// let addr = Eui48Addr::UNSPECIFIED;
/// assert_eq!(addr, Eui48Addr::new(0x00, 0x00, 0x00, 0x00, 0x00, 0x00));
/// ```
pub const UNSPECIFIED: Self = Eui48Addr::new(0, 0, 0, 0, 0, 0);
}
impl ::core::fmt::Display for Eui48Addr {
/// String formatter for [`Eui48Addr`] addresses.
fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(
fmt,
"{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
self.octets[0],
self.octets[1],
self.octets[2],
self.octets[3],
self.octets[4],
self.octets[5],
)
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for Eui48Addr {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(
fmt,
"{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
self.octets[0],
self.octets[1],
self.octets[2],
self.octets[3],
self.octets[4],
self.octets[5],
)
}
}
impl From<[u8; 6]> for Eui48Addr {
/// Creates an `Eui48Addr` from a six element byte array.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::Eui48Addr;
///
/// let addr = Eui48Addr::from([0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC]);
/// assert_eq!(Eui48Addr::new(0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC), addr);
/// ```
fn from(octets: [u8; 6]) -> Self {
Eui48Addr { octets }
}
}
/// An IPv4 socket address.
///
/// This is mostly ripped from [`std::net::SocketAddrV4`].
///
/// IPv4 socket addresses consist of an [`IPv4` address] and a 16-bit port
/// number, as stated in [IETF RFC 793].
///
/// Can be instantiated with [`SocketAddrV4::new`].
///
/// # Examples
///
/// ```
/// use w5500_ll::net::{Ipv4Addr, SocketAddrV4};
///
/// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
///
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
/// assert_eq!(socket.port(), 8080);
/// ```
///
/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
/// [`IPv4` address]: Ipv4Addr
/// [`std::net::SocketAddrV4`]: https://doc.rust-lang.org/std/net/struct.SocketAddrV4.html
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash, Default)]
#[cfg(not(feature = "ip_in_core"))]
pub struct SocketAddrV4 {
ip: Ipv4Addr,
port: u16,
}
#[cfg(not(feature = "ip_in_core"))]
impl SocketAddrV4 {
/// Creates a new socket address from an [`IPv4` address] and a port number.
///
/// [`IPv4` address]: Ipv4Addr
///
/// # Examples
///
/// ```
/// use w5500_ll::net::{Ipv4Addr, SocketAddrV4};
///
/// let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
/// ```
#[must_use]
pub const fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
SocketAddrV4 { ip, port }
}
/// Returns the IP address associated with this socket address.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::{Ipv4Addr, SocketAddrV4};
///
/// let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
/// assert_eq!(addr.ip(), &Ipv4Addr::new(127, 0, 0, 1));
/// ```
#[must_use]
pub const fn ip(&self) -> &Ipv4Addr {
&self.ip
}
/// Changes the IP address associated with this socket address.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::{Ipv4Addr, SocketAddrV4};
///
/// let mut addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
/// addr.set_ip(Ipv4Addr::new(192, 168, 0, 1));
/// assert_eq!(addr.ip(), &Ipv4Addr::new(192, 168, 0, 1));
/// ```
pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
self.ip = new_ip
}
/// Returns the port number associated with this socket address.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::{Ipv4Addr, SocketAddrV4};
///
/// let addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
/// assert_eq!(addr.port(), 8080);
/// ```
#[must_use]
pub const fn port(&self) -> u16 {
self.port
}
/// Changes the port number associated with this socket address.
///
/// # Examples
///
/// ```
/// use w5500_ll::net::{Ipv4Addr, SocketAddrV4};
///
/// let mut addr = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080);
/// addr.set_port(4242);
/// assert_eq!(addr.port(), 4242);
/// ```
pub fn set_port(&mut self, new_port: u16) {
self.port = new_port
}
}
#[cfg(not(feature = "ip_in_core"))]
impl ::core::fmt::Display for SocketAddrV4 {
/// String formatter for [`SocketAddrV4`] addresses.
fn fmt(&self, fmt: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(fmt, "{}:{}", self.ip(), self.port())
}
}
#[cfg(all(feature = "defmt", not(feature = "ip_in_core")))]
impl defmt::Format for SocketAddrV4 {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "{:?}:{}", self.ip(), self.port())
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<std::net::SocketAddrV4> for SocketAddrV4 {
fn from(addr: std::net::SocketAddrV4) -> Self {
SocketAddrV4::new(addr.ip().into(), addr.port())
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<&std::net::SocketAddrV4> for SocketAddrV4 {
fn from(addr: &std::net::SocketAddrV4) -> Self {
SocketAddrV4::new(addr.ip().into(), addr.port())
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<SocketAddrV4> for std::net::SocketAddrV4 {
fn from(addr: SocketAddrV4) -> Self {
std::net::SocketAddrV4::new(addr.ip().into(), addr.port)
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<&SocketAddrV4> for std::net::SocketAddrV4 {
fn from(addr: &SocketAddrV4) -> Self {
std::net::SocketAddrV4::new(addr.ip().into(), addr.port)
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<&SocketAddrV4> for std::net::SocketAddr {
fn from(addr: &SocketAddrV4) -> Self {
std::net::SocketAddr::V4(addr.into())
}
}
#[cfg(all(feature = "std", not(feature = "ip_in_core")))]
impl From<SocketAddrV4> for std::net::SocketAddr {
fn from(addr: SocketAddrV4) -> Self {
std::net::SocketAddr::V4(addr.into())
}
}