LCOV - code coverage report
Current view: top level - util/net - fd_ip6.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 38 0.0 %
Date: 2026-11-26 02:16:43 Functions: 0 1332 0.0 %

          Line data    Source code
       1             : #ifndef HEADER_fd_src_util_net_fd_ip6_h
       2             : #define HEADER_fd_src_util_net_fd_ip6_h
       3             : 
       4             : #include "../bits/fd_bits.h"
       5             : 
       6             : /* IPv6 address with an optional scope. */
       7             : 
       8             : struct fd_ip6_addr {
       9             :   uchar addr[ 16 ];
      10             :   uint  scope_id; /* 0 implies global */
      11             : };
      12             : 
      13             : typedef struct fd_ip6_addr fd_ip6_addr_t;
      14             : 
      15             : FD_PROTOTYPES_BEGIN
      16             : 
      17             : static inline void
      18             : fd_ip6_addr_ip4_mapped( uchar      ip6_addr[16],
      19           0 :                         uint const ip4_addr ) {
      20           0 :   memset( ip6_addr, 0, 10 );
      21           0 :   ip6_addr[ 10 ] = (uchar)0xff;
      22           0 :   ip6_addr[ 11 ] = (uchar)0xff;
      23           0 :   memcpy( ip6_addr+12, &ip4_addr, 4 );
      24           0 : }
      25             : 
      26             : static inline int
      27           0 : fd_ip6_addr_is_ip4_mapped( uchar const ip6_addr[16] ) {
      28           0 :   return (
      29           0 :     (ip6_addr[  0 ]==0x00) & (ip6_addr[  1 ]==0x00) &
      30           0 :     (ip6_addr[  2 ]==0x00) & (ip6_addr[  3 ]==0x00) &
      31           0 :     (ip6_addr[  4 ]==0x00) & (ip6_addr[  5 ]==0x00) &
      32           0 :     (ip6_addr[  6 ]==0x00) & (ip6_addr[  7 ]==0x00) &
      33           0 :     (ip6_addr[  8 ]==0x00) & (ip6_addr[  9 ]==0x00) &
      34           0 :     (ip6_addr[ 10 ]==0xff) & (ip6_addr[ 11 ]==0xff)
      35           0 :   );
      36           0 : }
      37             : 
      38             : static inline uint
      39           0 : fd_ip6_addr_to_ip4( uchar const ip6_addr[16] ) {
      40           0 :   uint ip4_addr;
      41           0 :   memcpy( &ip4_addr, ip6_addr+12, 4 );
      42           0 :   return ip4_addr;
      43           0 : }
      44             : 
      45             : /* fd_ip6_addr_is_unspecified returns 1 if the given address is the
      46             :    wildcard address (::), otherwise 0.  Binding a socket to the wildcard
      47             :    address listens on all addresses of all interfaces (both IPv6 and,
      48             :    unless IPV6_V6ONLY is set, IPv4). */
      49             : 
      50             : static inline int
      51           0 : fd_ip6_addr_is_unspecified( uchar const ip6_addr[16] ) {
      52           0 :   ulong hi, lo;
      53           0 :   memcpy( &hi, ip6_addr,   8 );
      54           0 :   memcpy( &lo, ip6_addr+8, 8 );
      55           0 :   return !(hi|lo);
      56           0 : }
      57             : 
      58             : /* fd_ip6_addr_is_loopback returns 1 if the given address is ::1 or an
      59             :    IPv4-mapped 127.0.0.0/8 address, otherwise 0. */
      60             : 
      61             : static inline int
      62           0 : fd_ip6_addr_is_loopback( uchar const ip6_addr[16] ) {
      63           0 :   static uchar const ip6_loopback[ 16 ] = { [15] = 0x01 };
      64           0 :   if( fd_ip6_addr_is_ip4_mapped( ip6_addr ) ) return ip6_addr[ 12 ]==127;
      65           0 :   return !memcmp( ip6_addr, ip6_loopback, 16UL );
      66           0 : }
      67             : 
      68             : /* fd_ip6_addr_is_scoped returns 1 if the given address has a scope
      69             :    narrower than global, i.e. a zone ID is required to identify which
      70             :    interface the address belongs to.  These are the link-local unicast
      71             :    addresses (fe80::/10) and the multicast addresses with a scope field
      72             :    below 'global' (ff00::/8 with the low nibble of byte 1 <0xe). */
      73             : 
      74             : static inline int
      75           0 : fd_ip6_addr_is_scoped( uchar const ip6_addr[16] ) {
      76           0 :   int link_local = (ip6_addr[0]==0xfe) & ((ip6_addr[1]&0xc0)==0x80);
      77           0 :   int multicast  = (ip6_addr[0]==0xff) & ((ip6_addr[1]&0x0f)< 0x0e);
      78           0 :   return link_local | multicast;
      79           0 : }
      80             : 
      81             : /* fd_cstr_to_ip6_addr parses an IPv6 address literal (RFC 4291), with
      82             :    an optional zone ID suffix (RFC 4007) naming the interface that the
      83             :    address belongs to.  The zone ID is either an interface name or a
      84             :    numeric interface index, e.g. "fe80::1%eth0" or "fe80::1%2".
      85             : 
      86             :    On success, stores the address to out and returns 1.  On failure,
      87             :    returns 0 and leaves out untouched.  Fails if a zone ID is given for
      88             :    an address that is not scoped, or names an interface that does not
      89             :    exist.
      90             : 
      91             :    Calls libc if_nametoindex (requires ioctl/Netlink access). */
      92             : 
      93             : int
      94             : fd_cstr_to_ip6_addr( char const *    s,
      95             :                      fd_ip6_addr_t * out );
      96             : 
      97             : /* fd_cstr_to_ip46_addr is like fd_cstr_to_ip6_addr, but additionally
      98             :    accepts an IPv4 address literal, which is stored as an IPv4-mapped
      99             :    IPv6 address (::ffff:a.b.c.d).  Note that the IPv4 wildcard address
     100             :    (0.0.0.0) maps to ::ffff:0.0.0.0.
     101             : 
     102             :    Calls libc if_nametoindex (requires ioctl/Netlink access).  */
     103             : 
     104             : int
     105             : fd_cstr_to_ip46_addr( char const *    s,
     106             :                       fd_ip6_addr_t * out );
     107             : 
     108             : /* FD_IP6_ADDR_CSTR_MAX is the buffer size required by
     109             :    fd_ip6_addr_cstr. */
     110             : 
     111             : #define FD_IP6_ADDR_CSTR_MAX (80UL)
     112             : 
     113             : /* fd_ip6_addr_cstr pretty prints an address for use as the host part of
     114             :    an URL, in the canonical text representation (RFC 5952).  IPv4-mapped
     115             :    addresses without a zone ID print as an IPv4 dotted quad (1.2.3.4),
     116             :    all others print bracketed, with a numeric zone ID suffix if the
     117             :    address is scoped (e.g. [fe80::1%2]).  Returns buf. */
     118             : 
     119             : char *
     120             : fd_ip6_addr_cstr( char                  buf[ static FD_IP6_ADDR_CSTR_MAX ],
     121             :                   fd_ip6_addr_t const * addr );
     122             : 
     123             : #define FD_IP6_ADDR_CSTR( name, addr ) \
     124           0 :   char name[ FD_IP6_ADDR_CSTR_MAX ]; fd_ip6_addr_cstr( name, (addr) )
     125             : 
     126             : FD_PROTOTYPES_END
     127             : 
     128             : #endif /* HEADER_fd_src_util_net_fd_ip6_h */

Generated by: LCOV version 1.14