1use crate::inet::{
5 ip,
6 ipv6::{IpV6Address, SocketAddressV6},
7 unspecified::Unspecified,
8 ExplicitCongestionNotification,
9};
10use core::{fmt, mem::size_of, net};
11use s2n_codec::zerocopy::U16;
12
13const IPV4_LEN: usize = 32 / 8;
16
17define_inet_type!(
18 pub struct IpV4Address {
19 octets: [u8; IPV4_LEN],
20 }
21);
22
23impl IpV4Address {
24 pub const UNSPECIFIED: Self = Self {
26 octets: [0; IPV4_LEN],
27 };
28
29 #[inline]
57 pub const fn unicast_scope(self) -> Option<ip::UnicastScope> {
58 use ip::UnicastScope::*;
59
60 match self.octets {
65 [0, 0, 0, 0] => None,
76
77 [0, _, _, _] => None,
83
84 [127, _, _, _] => Some(Loopback),
92
93 [10, _, _, _] => Some(Private),
101 [172, 16..=31, _, _] => Some(Private),
102 [192, 168, _, _] => Some(Private),
103
104 [100, 64..=127, _, _] => {
107 Some(Private)
112 }
113
114 [169, 254, _, _] => Some(LinkLocal),
118
119 [192, 0, 0, 9] => Some(Global),
133
134 [192, 0, 0, 10] => Some(Global),
148
149 [192, 0, 0, _] => None,
153
154 [198, 18..=19, _, _] => None,
159
160 [192, 0, 2, _] => None,
165 [198, 51, 100, _] => None,
166 [203, 0, 113, _] => None,
167
168 [233, 252, 0, _] => None,
173
174 [240..=255, _, _, _] => None,
183
184 _ => Some(Global),
186 }
187 }
188
189 #[inline]
191 pub const fn to_ipv6_mapped(self) -> IpV6Address {
192 let mut addr = [0; size_of::<IpV6Address>()];
195 let [a, b, c, d] = self.octets;
196 addr[10] = 0xFF;
197 addr[11] = 0xFF;
198 addr[12] = a;
199 addr[13] = b;
200 addr[14] = c;
201 addr[15] = d;
202 IpV6Address { octets: addr }
203 }
204
205 #[inline]
206 pub fn with_port(self, port: u16) -> SocketAddressV4 {
207 SocketAddressV4 {
208 ip: self,
209 port: port.into(),
210 }
211 }
212}
213
214impl fmt::Debug for IpV4Address {
215 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
216 write!(fmt, "IPv4Address({self})")
217 }
218}
219
220impl fmt::Display for IpV4Address {
221 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
222 let octets = &self.octets;
223 write!(
224 fmt,
225 "{}.{}.{}.{}",
226 octets[0], octets[1], octets[2], octets[3]
227 )
228 }
229}
230
231impl Unspecified for IpV4Address {
232 #[inline]
233 fn is_unspecified(&self) -> bool {
234 Self::UNSPECIFIED.eq(self)
235 }
236}
237
238test_inet_snapshot!(ipv4, ipv4_snapshot_test, IpV4Address);
239
240define_inet_type!(
241 pub struct SocketAddressV4 {
242 ip: IpV4Address,
243 port: U16,
244 }
245);
246
247impl SocketAddressV4 {
248 pub const UNSPECIFIED: Self = Self {
249 ip: IpV4Address::UNSPECIFIED,
250 port: U16::ZERO,
251 };
252
253 #[inline]
254 pub const fn ip(&self) -> &IpV4Address {
255 &self.ip
256 }
257
258 #[inline]
259 pub fn port(self) -> u16 {
260 self.port.into()
261 }
262
263 #[inline]
264 pub fn set_port(&mut self, port: u16) {
265 self.port.set(port)
266 }
267
268 #[inline]
269 pub const fn unicast_scope(&self) -> Option<ip::UnicastScope> {
270 self.ip.unicast_scope()
271 }
272
273 #[inline]
275 pub const fn to_ipv6_mapped(self) -> SocketAddressV6 {
276 let ip = self.ip().to_ipv6_mapped();
277 let port = self.port;
278 SocketAddressV6 { ip, port }
279 }
280}
281
282impl fmt::Debug for SocketAddressV4 {
283 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
284 write!(fmt, "SocketAddressV4({self})")
285 }
286}
287
288impl fmt::Display for SocketAddressV4 {
289 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
290 write!(fmt, "{}:{}", self.ip, self.port)
291 }
292}
293
294impl Unspecified for SocketAddressV4 {
295 #[inline]
296 fn is_unspecified(&self) -> bool {
297 Self::UNSPECIFIED.eq(self)
298 }
299}
300
301impl From<net::Ipv4Addr> for IpV4Address {
302 fn from(address: net::Ipv4Addr) -> Self {
303 (&address).into()
304 }
305}
306
307impl From<&net::Ipv4Addr> for IpV4Address {
308 fn from(address: &net::Ipv4Addr) -> Self {
309 address.octets().into()
310 }
311}
312
313impl From<IpV4Address> for net::Ipv4Addr {
314 fn from(address: IpV4Address) -> Self {
315 address.octets.into()
316 }
317}
318
319impl From<net::SocketAddrV4> for SocketAddressV4 {
320 fn from(address: net::SocketAddrV4) -> Self {
321 let ip = address.ip().into();
322 let port = address.port().into();
323 Self { ip, port }
324 }
325}
326
327impl From<(net::Ipv4Addr, u16)> for SocketAddressV4 {
328 fn from((ip, port): (net::Ipv4Addr, u16)) -> Self {
329 Self::new(ip, port)
330 }
331}
332
333impl From<SocketAddressV4> for net::SocketAddrV4 {
334 fn from(address: SocketAddressV4) -> Self {
335 let ip = address.ip.into();
336 let port = address.port.into();
337 Self::new(ip, port)
338 }
339}
340
341impl From<&SocketAddressV4> for net::SocketAddrV4 {
342 fn from(address: &SocketAddressV4) -> Self {
343 let ip = address.ip.into();
344 let port = address.port.into();
345 Self::new(ip, port)
346 }
347}
348
349impl From<SocketAddressV4> for net::SocketAddr {
350 fn from(address: SocketAddressV4) -> Self {
351 let addr: net::SocketAddrV4 = address.into();
352 addr.into()
353 }
354}
355
356impl From<&SocketAddressV4> for net::SocketAddr {
357 fn from(address: &SocketAddressV4) -> Self {
358 let addr: net::SocketAddrV4 = address.into();
359 addr.into()
360 }
361}
362
363test_inet_snapshot!(socket_v4, socket_v4_snapshot_test, SocketAddressV4);
364
365impl From<[u8; IPV4_LEN]> for IpV4Address {
366 #[inline]
367 fn from(octets: [u8; IPV4_LEN]) -> Self {
368 Self { octets }
369 }
370}
371
372impl From<IpV4Address> for [u8; IPV4_LEN] {
373 #[inline]
374 fn from(address: IpV4Address) -> Self {
375 address.octets
376 }
377}
378
379define_inet_type!(
402 pub struct Header {
403 vihl: Vihl,
404 tos: Tos,
405 total_len: U16,
406 id: U16,
407 flag_fragment: FlagFragment,
408 ttl: u8,
409 protocol: ip::Protocol,
410 checksum: U16,
411 source: IpV4Address,
412 destination: IpV4Address,
413 }
414);
415
416impl fmt::Debug for Header {
417 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
418 f.debug_struct("ipv4::Header")
419 .field("version", &self.vihl.version())
420 .field("header_len", &self.vihl.header_len())
421 .field("dscp", &self.tos.dscp())
422 .field("ecn", &self.tos.ecn())
423 .field("total_len", &self.total_len)
424 .field("id", &format_args!("0x{:04x}", self.id.get()))
425 .field("flags (reserved)", &self.flag_fragment.reserved())
426 .field(
427 "flags (don't fragment)",
428 &self.flag_fragment.dont_fragment(),
429 )
430 .field(
431 "flags (more fragments)",
432 &self.flag_fragment.more_fragments(),
433 )
434 .field("fragment_offset", &self.flag_fragment.fragment_offset())
435 .field("ttl", &self.ttl)
436 .field("protocol", &self.protocol)
437 .field("checksum", &format_args!("0x{:04x}", self.checksum.get()))
438 .field("source", &self.source)
439 .field("destination", &self.destination)
440 .finish()
441 }
442}
443
444impl Header {
445 #[inline]
447 pub fn swap(&mut self) {
448 core::mem::swap(&mut self.source, &mut self.destination)
449 }
450
451 #[inline]
452 pub const fn vihl(&self) -> &Vihl {
453 &self.vihl
454 }
455
456 #[inline]
457 pub fn vihl_mut(&mut self) -> &mut Vihl {
458 &mut self.vihl
459 }
460
461 #[inline]
462 pub const fn tos(&self) -> &Tos {
463 &self.tos
464 }
465
466 #[inline]
467 pub fn tos_mut(&mut self) -> &mut Tos {
468 &mut self.tos
469 }
470
471 #[inline]
472 pub const fn total_len(&self) -> &U16 {
473 &self.total_len
474 }
475
476 #[inline]
477 pub fn total_len_mut(&mut self) -> &mut U16 {
478 &mut self.total_len
479 }
480
481 #[inline]
482 pub const fn id(&self) -> &U16 {
483 &self.id
484 }
485
486 #[inline]
487 pub fn id_mut(&mut self) -> &mut U16 {
488 &mut self.id
489 }
490
491 #[inline]
492 pub const fn flag_fragment(&self) -> &FlagFragment {
493 &self.flag_fragment
494 }
495
496 #[inline]
497 pub fn flag_fragment_mut(&mut self) -> &mut FlagFragment {
498 &mut self.flag_fragment
499 }
500
501 #[inline]
502 pub const fn ttl(&self) -> &u8 {
503 &self.ttl
504 }
505
506 #[inline]
507 pub fn ttl_mut(&mut self) -> &mut u8 {
508 &mut self.ttl
509 }
510
511 #[inline]
512 pub const fn protocol(&self) -> &ip::Protocol {
513 &self.protocol
514 }
515
516 #[inline]
517 pub fn protocol_mut(&mut self) -> &mut ip::Protocol {
518 &mut self.protocol
519 }
520
521 #[inline]
522 pub const fn checksum(&self) -> &U16 {
523 &self.checksum
524 }
525
526 #[inline]
527 pub fn checksum_mut(&mut self) -> &mut U16 {
528 &mut self.checksum
529 }
530
531 #[inline]
532 pub const fn source(&self) -> &IpV4Address {
533 &self.source
534 }
535
536 #[inline]
537 pub fn source_mut(&mut self) -> &mut IpV4Address {
538 &mut self.source
539 }
540
541 #[inline]
542 pub const fn destination(&self) -> &IpV4Address {
543 &self.destination
544 }
545
546 #[inline]
547 pub fn destination_mut(&mut self) -> &mut IpV4Address {
548 &mut self.destination
549 }
550
551 #[inline]
552 pub fn update_checksum(&mut self) {
553 use core::hash::Hasher;
554
555 self.checksum.set(0);
556
557 let bytes = self.as_bytes();
558
559 let mut checksum = crate::inet::checksum::Checksum::generic();
560
561 checksum.write(bytes);
562
563 self.checksum.set_be(checksum.finish_be());
564 }
565}
566
567define_inet_type!(
572 pub struct Vihl {
573 value: u8,
574 }
575);
576
577impl fmt::Debug for Vihl {
578 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
579 f.debug_struct("Vihl")
580 .field("version", &self.version())
581 .field("header_len", &self.header_len())
582 .finish()
583 }
584}
585
586impl Vihl {
587 #[inline]
588 pub fn version(&self) -> u8 {
589 self.value >> 4
590 }
591
592 #[inline]
593 pub fn set_version(&mut self, value: u8) -> &mut Self {
594 self.value = (value << 4) | (self.value & 0x0F);
595 self
596 }
597
598 #[inline]
599 pub fn header_len(&self) -> u8 {
600 self.value & 0x0F
601 }
602
603 #[inline]
604 pub fn set_header_len(&mut self, value: u8) -> &mut Self {
605 self.value = (self.value & 0xF0) | (value & 0x0F);
606 self
607 }
608}
609
610define_inet_type!(
615 pub struct Tos {
616 value: u8,
617 }
618);
619
620impl Tos {
621 #[inline]
623 pub fn dscp(&self) -> u8 {
624 self.value >> 2
625 }
626
627 #[inline]
628 pub fn set_dscp(&mut self, value: u8) -> &mut Self {
629 self.value = (value << 2) | (self.value & 0b11);
630 self
631 }
632
633 #[inline]
634 pub fn ecn(&self) -> ExplicitCongestionNotification {
635 ExplicitCongestionNotification::new(self.value & 0b11)
636 }
637
638 #[inline]
639 pub fn set_ecn(&mut self, ecn: ExplicitCongestionNotification) -> &mut Self {
640 self.value = (self.value & !0b11) | ecn as u8;
641 self
642 }
643}
644
645impl fmt::Debug for Tos {
646 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
647 f.debug_struct("ipv4::Tos")
648 .field("dscp", &self.dscp())
649 .field("ecn", &self.ecn())
650 .finish()
651 }
652}
653
654define_inet_type!(
659 pub struct FlagFragment {
660 value: U16,
661 }
662);
663
664impl FlagFragment {
665 const FRAGMENT_MASK: u16 = 0b0001_1111_1111_1111;
666
667 #[inline]
668 pub fn reserved(&self) -> bool {
669 self.get(1 << 15)
670 }
671
672 pub fn set_reserved(&mut self, enabled: bool) -> &mut Self {
673 self.set(1 << 15, enabled)
674 }
675
676 #[inline]
677 pub fn dont_fragment(&self) -> bool {
678 self.get(1 << 14)
679 }
680
681 #[inline]
682 pub fn set_dont_fragment(&mut self, enabled: bool) -> &mut Self {
683 self.set(1 << 14, enabled)
684 }
685
686 #[inline]
687 pub fn more_fragments(&self) -> bool {
688 self.get(1 << 13)
689 }
690
691 #[inline]
692 pub fn set_more_fragments(&mut self, enabled: bool) -> &mut Self {
693 self.set(1 << 13, enabled)
694 }
695
696 #[inline]
697 pub fn fragment_offset(&self) -> u16 {
698 self.value.get() & Self::FRAGMENT_MASK
699 }
700
701 #[inline]
702 pub fn set_fragment_offset(&mut self, offset: u16) -> &mut Self {
703 self.value
704 .set(self.value.get() & !Self::FRAGMENT_MASK | offset & Self::FRAGMENT_MASK);
705 self
706 }
707
708 #[inline]
709 fn get(&self, mask: u16) -> bool {
710 self.value.get() & mask == mask
711 }
712
713 #[inline]
714 fn set(&mut self, mask: u16, enabled: bool) -> &mut Self {
715 let value = self.value.get();
716 let value = if enabled { value | mask } else { value & !mask };
717 self.value.set(value);
718 self
719 }
720}
721
722impl fmt::Debug for FlagFragment {
723 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
724 f.debug_struct("ipv4::FlagFragment")
725 .field("reserved", &self.reserved())
726 .field("dont_fragment", &self.dont_fragment())
727 .field("more_fragments", &self.more_fragments())
728 .field("fragment_offset", &self.fragment_offset())
729 .finish()
730 }
731}
732
733#[cfg(any(test, feature = "std"))]
734mod std_conversion {
735 use super::*;
736 use std::net;
737
738 impl net::ToSocketAddrs for SocketAddressV4 {
739 type Iter = std::iter::Once<net::SocketAddr>;
740
741 fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
742 let ip = self.ip.into();
743 let port = self.port.into();
744 let addr = net::SocketAddrV4::new(ip, port);
745 Ok(std::iter::once(addr.into()))
746 }
747 }
748}
749
750#[cfg(test)]
751mod tests {
752 use super::*;
753 use bolero::{check, generator::*};
754 use s2n_codec::{DecoderBuffer, DecoderBufferMut};
755
756 #[test]
758 #[cfg_attr(kani, kani::proof, kani::unwind(5), kani::solver(kissat))]
759 fn scope_test() {
760 let g = produce::<[u8; 4]>().map_gen(IpV4Address::from);
761 check!().with_generator(g).cloned().for_each(|subject| {
762 use ip::UnicastScope::*;
763
764 let expected = std::net::Ipv4Addr::from(subject);
765
766 let network = ip_network::Ipv4Network::from(expected);
770
771 match subject.unicast_scope() {
772 Some(Global) => {
773 if subject.octets == [192, 0, 0, 9] || subject.octets == [192, 0, 0, 10] {
777 return;
778 }
779
780 assert!(network.is_global());
781 }
782 Some(Private) => {
783 assert!(expected.is_private() || network.is_shared_address_space());
784 }
785 Some(Loopback) => {
786 assert!(expected.is_loopback());
787 }
788 Some(LinkLocal) => {
789 assert!(expected.is_link_local());
790 }
791 None => {
792 assert!(
793 expected.is_broadcast()
794 || expected.is_multicast()
795 || expected.is_documentation()
796 || network.is_benchmarking()
797 || network.is_ietf_protocol_assignments()
798 || network.is_reserved()
799 || network.is_local_identification()
800 || network.is_unspecified()
801 );
802 }
803 }
804 })
805 }
806
807 #[test]
808 #[cfg_attr(miri, ignore)]
809 fn snapshot_test() {
810 let mut buffer = vec![0u8; core::mem::size_of::<Header>()];
811 for (idx, byte) in buffer.iter_mut().enumerate() {
812 *byte = idx as u8;
813 }
814 let decoder = DecoderBuffer::new(&buffer);
815 let (header, _) = decoder.decode::<&Header>().unwrap();
816 insta::assert_debug_snapshot!("snapshot_test", header);
817
818 buffer.fill(255);
819 let decoder = DecoderBuffer::new(&buffer);
820 let (header, _) = decoder.decode::<&Header>().unwrap();
821 insta::assert_debug_snapshot!("snapshot_filled_test", header);
822 }
823
824 #[test]
825 #[cfg_attr(kani, kani::proof, kani::unwind(5), kani::solver(kissat))]
826 fn header_getter_setter_test() {
827 check!().with_type::<Header>().for_each(|expected| {
828 let mut buffer = [255u8; core::mem::size_of::<Header>()];
829 let decoder = DecoderBufferMut::new(&mut buffer);
830 let (header, _) = decoder.decode::<&mut Header>().unwrap();
831 {
832 header
834 .vihl_mut()
835 .set_version(expected.vihl().version())
836 .set_header_len(expected.vihl().header_len());
837 header
838 .tos_mut()
839 .set_dscp(expected.tos().dscp())
840 .set_ecn(expected.tos().ecn());
841 header.id_mut().set(expected.id().get());
842 header.total_len_mut().set(expected.total_len().get());
843 header
844 .flag_fragment_mut()
845 .set_reserved(expected.flag_fragment().reserved())
846 .set_dont_fragment(expected.flag_fragment().dont_fragment())
847 .set_more_fragments(expected.flag_fragment().more_fragments())
848 .set_fragment_offset(expected.flag_fragment().fragment_offset());
849 *header.ttl_mut() = *expected.ttl();
850 *header.protocol_mut() = *expected.protocol();
851 header.checksum_mut().set(expected.checksum().get());
852 *header.source_mut() = *expected.source();
853 *header.destination_mut() = *expected.destination();
854 }
855
856 let decoder = DecoderBuffer::new(&buffer);
857 let (actual, _) = decoder.decode::<&Header>().unwrap();
858 {
859 assert_eq!(expected, actual);
861 assert_eq!(expected.vihl(), actual.vihl());
862 assert_eq!(expected.vihl().version(), actual.vihl().version());
863 assert_eq!(expected.vihl().header_len(), actual.vihl().header_len());
864 assert_eq!(expected.tos(), actual.tos());
865 assert_eq!(expected.tos().dscp(), actual.tos().dscp());
866 assert_eq!(expected.tos().ecn(), actual.tos().ecn());
867 assert_eq!(expected.id(), actual.id());
868 assert_eq!(expected.total_len(), actual.total_len());
869 assert_eq!(expected.flag_fragment(), actual.flag_fragment());
870 assert_eq!(
871 expected.flag_fragment().reserved(),
872 actual.flag_fragment().reserved()
873 );
874 assert_eq!(
875 expected.flag_fragment().dont_fragment(),
876 actual.flag_fragment().dont_fragment()
877 );
878 assert_eq!(
879 expected.flag_fragment().more_fragments(),
880 actual.flag_fragment().more_fragments()
881 );
882 assert_eq!(expected.ttl(), actual.ttl());
883 assert_eq!(expected.protocol(), actual.protocol());
884 assert_eq!(expected.checksum(), actual.checksum());
885 assert_eq!(expected.source(), actual.source());
886 assert_eq!(expected.destination(), actual.destination());
887 }
888 })
889 }
890
891 #[test]
892 fn header_round_trip_test() {
893 check!().for_each(|buffer| {
894 s2n_codec::assert_codec_round_trip_bytes!(Header, buffer);
895 });
896 }
897}