LCOV - code coverage report
Current view: top level - util/net - fd_ip4.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 71 78 91.0 %
Date: 2026-06-19 09:21:35 Functions: 9 1784 0.5 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_util_net_fd_ip4_h
       2             : #define HEADER_fd_src_util_net_fd_ip4_h
       3             : 
       4             : #include "../bits/fd_bits.h"
       5             : 
       6             : /* FIXME: IP4 CRASH COURSE HERE */
       7             : 
       8             : #define FD_IP4_HDR_TOS_PREC_INTERNETCONTROL ((uchar)0xc0) /* This packet is should have Internet control type of service */
       9             : 
      10             : #define FD_IP4_HDR_FRAG_OFF_RF   ((ushort)0x8000) /* (in host byte order) Mask for the frag off reserved bit */
      11          15 : #define FD_IP4_HDR_FRAG_OFF_DF   ((ushort)0x4000) /* (in host byte order) Mask for the frag off don't frag bit */
      12             : #define FD_IP4_HDR_FRAG_OFF_MF   ((ushort)0x2000) /* (in host byte order) Mask for the frag off more frags bit */
      13             : #define FD_IP4_HDR_FRAG_OFF_MASK ((ushort)0x1fff) /* (in host byte order) Mask for the frag off offset bits */
      14             : 
      15             : #define FD_IP4_HDR_PROTOCOL_IP4  ((uchar) 0) /* The IP4 packet encapsulates an IP4  packet */
      16           0 : #define FD_IP4_HDR_PROTOCOL_ICMP ((uchar) 1) /* The IP4 packet encapsulates an ICMP packet */
      17             : #define FD_IP4_HDR_PROTOCOL_IGMP ((uchar) 2) /* The IP4 packet encapsulates an IGMP packet */
      18           0 : #define FD_IP4_HDR_PROTOCOL_TCP  ((uchar) 6) /* The IP4 packet encapsulates an TCP  packet */
      19   117154831 : #define FD_IP4_HDR_PROTOCOL_UDP  ((uchar)17) /* The IP4 packet encapsulates an UDP  packet */
      20          45 : #define FD_IP4_HDR_PROTOCOL_GRE  ((uchar)47) /* The IP4 packet encapsulates a GRE  packet */
      21             : 
      22             : #define FD_IP4_OPT_RA  ((uchar)148) /* This option is a router alert option */
      23             : #define FD_IP4_OPT_EOL ((uchar)0)   /* This is the end of the options list */
      24             : 
      25             : /* All of the below are in network byte order */
      26          39 : #define IP4_PRIVATE_RANGE1_START_NET FD_IP4_ADDR( 10,   0,   0,   0)
      27          33 : #define IP4_PRIVATE_RANGE1_END_NET   FD_IP4_ADDR( 10, 255, 255, 255)
      28          30 : #define IP4_PRIVATE_RANGE2_START_NET FD_IP4_ADDR(172,  16,   0,   0)
      29          18 : #define IP4_PRIVATE_RANGE2_END_NET   FD_IP4_ADDR(172,  31, 255, 255)
      30          21 : #define IP4_PRIVATE_RANGE3_START_NET FD_IP4_ADDR(192, 168,   0,   0)
      31           9 : #define IP4_PRIVATE_RANGE3_END_NET   FD_IP4_ADDR(192, 168, 255, 255)
      32             : 
      33          12 : #define IP4_LOOPBACK_START_NET       FD_IP4_ADDR(127,   0,   0,   0)
      34           3 : #define IP4_LOOPBACK_END_NET         FD_IP4_ADDR(127, 255, 255, 255)
      35             : 
      36             : union fd_ip4_hdr {
      37             :   struct {
      38             :     uchar  verihl;       /* 4 msb: IP version (==4), assumes little endian */
      39             :                          /* 4 lsb: Header length in words (>=5) */
      40             :     uchar  tos;          /* Type of service */
      41             :     ushort net_tot_len;  /* Frag size in bytes, incl ip hdr, net order */
      42             :     ushort net_id;       /* Frag id, unique from sender for long enough, net order */
      43             :     ushort net_frag_off; /* Frag off (dbl words)+status (top 3 bits), net order */
      44             :     uchar  ttl;          /* Frag time to live */
      45             :     uchar  protocol;     /* Type of payload */
      46             :     ushort check;        /* Header checksum ("invariant" order) */
      47             :     union __attribute__((packed)) {
      48             :       uchar saddr_c[4];  /* Address of sender, technically net order but all APIs below work with this directly */
      49             :       uint  saddr;
      50             :     };
      51             :     union __attribute__((packed)) {
      52             :       uchar daddr_c[4];  /* Address of destination, technically net order but all APIs below work with this directly */
      53             :       uint  daddr;
      54             :     };
      55             :     /* Up to 40 bytes of options here */
      56             :   };
      57             : };
      58             : 
      59             : typedef union fd_ip4_hdr fd_ip4_hdr_t;
      60             : 
      61             : /* FD_IP4_GET_VERSION obtains the version from the supplied fd_ip4_hdr */
      62             : 
      63     6670842 : #define FD_IP4_GET_VERSION(ip4) ((uchar)( ( (uint)(ip4).verihl >> 4u ) & 0x0fu ))
      64             : 
      65             : /* FD_IP4_SET_VERSION sets the version in the supplied fd_ip4_hdr */
      66             : 
      67             : #define FD_IP4_SET_VERSION(ip4,value) (ip4).verihl = ((uchar)( \
      68             :       ( (uint)(ip4).verihl & 0x0fu ) | ( ( (uint)(value) & 0x0fu ) << 4u ) ))
      69             : 
      70             : /* FD_IP4_GET_IHL retrieves the IHL field from the supplied fd_ip4_hdr */
      71             : 
      72     6670842 : #define FD_IP4_GET_IHL(ip4) ((uchar)( (uint)(ip4).verihl & 0x0fu ))
      73             : 
      74             : /* FD_IP4_GET_LEN retrieves and adjusts the IHL field from the supplied fd_ip4_hdr */
      75             : 
      76     6670842 : #define FD_IP4_GET_LEN(ip4) ( FD_IP4_GET_IHL(ip4) * 4u )
      77             : 
      78             : /* FD_IP4_SET_IHL sets the IHL field in the supplied fd_ip4_hdr */
      79             : 
      80             : #define FD_IP4_SET_IHL(ip4,value) (ip4).verihl = ((uchar)( \
      81             :       ( (uint)(ip4).verihl & 0xf0u ) | ( (uint)(value) & 0x0fu ) ))
      82             : 
      83             : /* FD_IP4_VERIHL combines the supplied IHL and VERSION into a single verihl fields */
      84             : 
      85   227638694 : #define FD_IP4_VERIHL(version,ihl) ((uchar)( ( ((uint)(version) & 0x0fu) << 4u ) | \
      86   227638694 :                                                ((uint)(ihl)     & 0x0fu) ))
      87             : 
      88             : /* FD_IP4_ADDR constructs an IP4 address from the 4-tuple x.y.z.w.
      89             :    Assumes x,y,z,w are all integers in [0,255]. */
      90             : 
      91   221580572 : #define FD_IP4_ADDR(x,y,z,w) (((uint)(x)) | (((uint)(y)) << 8) | (((uint)(z)) << 16) | (((uint)(w)) << 24))
      92             : 
      93             : /* FD_IP4_ADDR_FMT / FD_IP4_ADDR_FMT_ARGS are used to pretty print a
      94             :    ip4 address by a printf style formatter.  a must be safe against
      95             :    multiple evaluation.  Example usage:
      96             : 
      97             :      fd_ip4_hdr_t * hdr = ...;
      98             :      FD_LOG_NOTICE(( "DST MAC: " FD_IP4_ADDR_FMT, FD_IP4_ADDR_FMT_ARGS( hdr->daddr ) */
      99             : 
     100           0 : #define FD_IP4_ADDR_FMT         "%u.%u.%u.%u"
     101           0 : #define FD_IP4_ADDR_FMT_ARGS(a) ((a) & 255U),(((a)>>8) & 255U),(((a)>>16) & 255U),((a)>>24)
     102             : 
     103             : /* FIXME: CONSIDER AN OVERALL HEADER PRETTY PRINTER? */
     104             : 
     105             : FD_PROTOTYPES_BEGIN
     106             : 
     107             : /* fd_ip4_addr_is_{mcast,bcast,loopback} returns 1 if the ipaddr is
     108             :    {multicast (in [224-239].y.z.w), global broadcast
     109             :    (255.255.255.255), loopback (127.y.z.w)} and 0 otherwise.
     110             :    fd_ip4_hdr_net_frag_off_is_unfragmented returns 1 if the
     111             :    net_frag_off field of the ip4 header indicates the encapsulated
     112             :    packet is not fragmented (i.e. entirely containing the IP4 packet)
     113             :    and 0 otherwise (i.e. fragmented into multiple IP4 packets). */
     114             : 
     115          27 : FD_FN_CONST static inline int fd_ip4_addr_is_mcast( uint addr ) { return (((uchar)addr)>>4)==(uchar)0xe; }
     116           9 : FD_FN_CONST static inline int fd_ip4_addr_is_bcast( uint addr ) { return addr==~0U;                      }
     117          12 : FD_FN_CONST static inline int fd_ip4_addr_is_loopback( uint addr ) {
     118          12 :    return fd_uint_bswap( addr ) >= fd_uint_bswap( IP4_LOOPBACK_START_NET ) &&
     119          12 :           fd_uint_bswap( addr ) <= fd_uint_bswap( IP4_LOOPBACK_END_NET   );
     120          12 :  }
     121             : 
     122             : FD_FN_CONST static inline int
     123           0 : fd_ip4_hdr_net_frag_off_is_unfragmented( ushort net_frag_off ) { /* net order */
     124           0 :   return !(((uint)net_frag_off) & 0xff3fU); /* ff3f is fd_ushort_bswap( NET_IP_HDR_FRAG_OFF_MASK | NET_IP_HDR_FRAG_OFF_MF ) */
     125           0 : }
     126             : 
     127             : /* fd_ip4_hdr_check is used for hdr check field computation and
     128             :    validation.  hdr points to the first byte a memory region containing
     129             :    an ip4 header and any options that might follow it.  If the header
     130             :    has checksum (check==0), this returns the value to use for check.  If
     131             :    hdr has a checksum (check!=0), this returns 0 if hdr has a valid
     132             :    checksum (or non-zero if not).  This is mostly for use in cases where
     133             :    the overhead doesn't matter or when the hardware sending/receiving
     134             :    the packet doesn't do various checksum offload computations. */
     135             : 
     136             : FD_FN_PURE static inline ushort
     137          33 : fd_ip4_hdr_check( void const * vp_hdr ) {
     138          33 :   uchar * cp = (uchar*)vp_hdr;
     139             : 
     140          33 :   uint n = ( (*cp) & 0x0fu );
     141             : 
     142          33 :   ulong        c = 0UL;
     143         198 :   for( uint i=0U; i<n; i++ ) {
     144         165 :     uint u;
     145             : 
     146             :     /* the compiler elides the copy in practice */
     147         165 :     memcpy( &u, cp + i*4, 4 );
     148         165 :     c += (ulong)u;
     149         165 :   }
     150             : 
     151          33 :   c  = ( c>>32            ) +
     152          33 :        ((c>>16) & 0xffffUL) +
     153          33 :        ( c      & 0xffffUL);
     154          33 :   c  = ( c>>16            ) +
     155          33 :        ( c      & 0xffffUL);
     156          33 :   c += ( c>>16            );
     157             : 
     158          33 :   return (ushort)~c;
     159          33 : }
     160             : 
     161             : /* fd_ip4_hdr_check_fast is the same as the above but assumes that the
     162             :    header has no options (i.e. ihl==5) */
     163             : 
     164             : FD_FN_PURE static inline ushort
     165     6671028 : fd_ip4_hdr_check_fast( void const * vp_hdr ) {
     166     6671028 :   uchar * cp = (uchar*)vp_hdr;
     167             : 
     168     6671028 :   uint n = ( (*cp) & 0x0fu );
     169             : 
     170             :   /* branches aren't taken don't use branch table entries */
     171     6671028 :   if( FD_UNLIKELY( n != 5 ) ) return fd_ip4_hdr_check(vp_hdr);
     172             : 
     173             :   /* the compiler knows n here and completely unrolls the loop */
     174     6671028 :   ulong c = 0UL;
     175    40026168 :   for( uint i=0U; i<n; i++ ) {
     176    33355140 :     uint u;
     177             : 
     178             :     /* the compiler elides the copy in practice */
     179    33355140 :     memcpy( &u, cp + i*4, 4 );
     180    33355140 :     c += (ulong)u;
     181    33355140 :   }
     182             : 
     183     6671028 :   c  = ( c>>32            ) +
     184     6671028 :        ((c>>16) & 0xffffUL) +
     185     6671028 :        ( c      & 0xffffUL);
     186     6671028 :   c  = ( c>>16            ) +
     187     6671028 :        ( c      & 0xffffUL);
     188     6671028 :   c += ( c>>16            );
     189             : 
     190     6671028 :   return (ushort)~c;
     191     6671028 : }
     192             : 
     193             : /* fd_cstr_to_ip4_addr parses an IPv4 address matching format
     194             :    %u.%u.%u.%u  On success stores address to out and returns 1. On fail
     195             :    returns 0.  The given address is returned in network byte order such
     196             :    that "1.0.0.0" => 0x00000001. */
     197             : 
     198             : int
     199             : fd_cstr_to_ip4_addr( char const * s,
     200             :                      uint *       addr );
     201             : 
     202             : /* fd_ip4_addr_is_public checks if the given IPv4 address is a public address.
     203             :    assumed to be in net byte order.  */
     204             : 
     205             : FD_FN_CONST static inline int
     206          39 : fd_ip4_addr_is_public( uint addr ) {
     207          39 :   uint addr_host = fd_uint_bswap( addr );
     208          39 :   return !((addr_host >= fd_uint_bswap( IP4_PRIVATE_RANGE1_START_NET ) && addr_host <= fd_uint_bswap( IP4_PRIVATE_RANGE1_END_NET )) ||
     209          39 :            (addr_host >= fd_uint_bswap( IP4_PRIVATE_RANGE2_START_NET ) && addr_host <= fd_uint_bswap( IP4_PRIVATE_RANGE2_END_NET )) ||
     210          39 :            (addr_host >= fd_uint_bswap( IP4_PRIVATE_RANGE3_START_NET ) && addr_host <= fd_uint_bswap( IP4_PRIVATE_RANGE3_END_NET )) ||
     211          39 :            fd_ip4_addr_is_loopback( addr ));
     212          39 : }
     213             : 
     214             : /* fd_ip4_hdr_bswap reverses the endianness of all fields in the IPv4
     215             :    header. */
     216             : 
     217             : static inline void
     218    13341789 : fd_ip4_hdr_bswap( fd_ip4_hdr_t * hdr ) {
     219    13341789 :   hdr->net_tot_len  = (ushort)fd_ushort_bswap( hdr->net_tot_len  );
     220    13341789 :   hdr->net_id       = (ushort)fd_ushort_bswap( hdr->net_id       );
     221    13341789 :   hdr->net_frag_off = (ushort)fd_ushort_bswap( hdr->net_frag_off );
     222    13341789 :   hdr->check        = (ushort)fd_ushort_bswap( hdr->check        );
     223    13341789 : }
     224             : 
     225             : FD_PROTOTYPES_END
     226             : 
     227             : #endif /* HEADER_fd_src_util_net_fd_ip4_h */

Generated by: LCOV version 1.14