github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/networkutil/ip_version.go (about)

     1  // SPDX-License-Identifier: MPL-2.0
     2  /*
     3   * Copyright (C) 2024 The Noisy Sockets Authors.
     4   *
     5   * This Source Code Form is subject to the terms of the Mozilla Public
     6   * License, v. 2.0. If a copy of the MPL was not distributed with this
     7   * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     8   */
     9  
    10  package networkutil
    11  
    12  import (
    13  	"net/netip"
    14  )
    15  
    16  // HasIPv4 returns true if the list of addresses contains an IPv4 address.
    17  func HasIPv4(addrs []netip.Addr) bool {
    18  	for _, addr := range addrs {
    19  		if addr.Is4() {
    20  			return true
    21  		}
    22  	}
    23  
    24  	return false
    25  }
    26  
    27  // HasIPv6 returns true if the list of addresses contains an IPv6 address.
    28  func HasIPv6(addrs []netip.Addr) bool {
    29  	for _, addr := range addrs {
    30  		if addr.Is6() {
    31  			return true
    32  		}
    33  	}
    34  
    35  	return false
    36  }