github.com/haraldrudell/parl@v0.4.176/pnet/network.go (about)

     1  /*
     2  © 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pnet
     7  
     8  import (
     9  	"net"
    10  
    11  	"github.com/haraldrudell/parl/perrors"
    12  	"github.com/haraldrudell/parl/sets"
    13  )
    14  
    15  // the type of Network
    16  //   - NetworkTCP NetworkTCP4 NetworkTCP6
    17  //   - NetworkUDP NetworkUDP4 NetworkUDP6
    18  //   - NetworkIP NetworkIP4 NetworkIP6
    19  //   - NetworkUnix NetworkUnixGram NetworkUnixPacket
    20  type Network string
    21  
    22  const (
    23  	NetworkDefault    Network = ""           // default
    24  	NetworkTCP        Network = "tcp"        // tcp IPv4 or IPv6
    25  	NetworkTCP4       Network = "tcp4"       // net network tcp ipv4
    26  	NetworkTCP6       Network = "tcp6"       // tcp IPv6
    27  	NetworkUDP        Network = "udp"        // udp is udp IPv4 or IPv6
    28  	NetworkUDP4       Network = "udp4"       // udp4 is udp IPv4
    29  	NetworkUDP6       Network = "udp6"       // udp6 is udp IPv6
    30  	NetworkIP         Network = "ip"         // ip is IP protocol IPv4 or IPv6 addressing
    31  	NetworkIP4        Network = "ip4"        // ip4 is IP protocol IPv4
    32  	NetworkIP6        Network = "ip6"        // ip6 is IP protocol IPv6
    33  	NetworkUnix       Network = "unix"       // unix is tcp or udp over Unix socket
    34  	NetworkUnixGram   Network = "unixgram"   // unixgram is udp over Unix socket
    35  	NetworkUnixPacket Network = "unixpacket" // unixpacket is tcp over Unix socket
    36  )
    37  
    38  // ParseNetwork checks if network is valid
    39  //   - tcp tcp4 tcp6 udp udp4 udp6 ip ip4 ip6 unix unixgram unixpacket
    40  func ParseNetwork(network string) (n Network, err error) {
    41  	n = Network(network)
    42  	if !n.IsValid() {
    43  		err = perrors.Errorf("ParseNetwork: %w", net.UnknownNetworkError(network))
    44  	}
    45  	return
    46  }
    47  
    48  var networkSet = sets.NewSet[Network]([]sets.SetElementFull[Network]{
    49  	{ValueV: NetworkTCP, Name: string(NetworkTCP), Full: "tcp IPV4 or IPv6"},
    50  	{ValueV: NetworkTCP4, Name: string(NetworkTCP4), Full: "tcp IPv4"},
    51  	{ValueV: NetworkTCP6, Name: string(NetworkTCP6), Full: "tcp IPv6"},
    52  	{ValueV: NetworkUDP, Name: string(NetworkUDP), Full: "udp IPv4 or IPv6"},
    53  	{ValueV: NetworkUDP4, Name: string(NetworkUDP4), Full: "udp IPv4"},
    54  	{ValueV: NetworkUDP6, Name: string(NetworkUDP6), Full: "udp IPv6"},
    55  	{ValueV: NetworkIP, Name: string(NetworkIP), Full: "IP protocol IPv4 or IPv6 addressing"},
    56  	{ValueV: NetworkIP4, Name: string(NetworkIP4), Full: "IP protocol IPv4"},
    57  	{ValueV: NetworkIP6, Name: string(NetworkIP6), Full: "IP protocol IPv6"},
    58  	{ValueV: NetworkUnix, Name: string(NetworkUnix), Full: "tcp or udp over Unix socket"},
    59  	{ValueV: NetworkUnixGram, Name: string(NetworkUnixGram), Full: "udp over Unix socket"},
    60  	{ValueV: NetworkUnixPacket, Name: string(NetworkUnixPacket), Full: "tcp over Unix socket"},
    61  })
    62  
    63  func (t Network) String() (s string) { return networkSet.StringT(t) }
    64  
    65  func (t Network) IsValid() (isValid bool) { return networkSet.IsValid(t) }