github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/network/network.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 network provides an interface for the standard library's network operations.
    11  // This is defined in a separate package to avoid issues with circular imports.
    12  package network
    13  
    14  import (
    15  	"context"
    16  	"io"
    17  	stdnet "net"
    18  )
    19  
    20  // Network is an interface that abstracts the standard library's network operations.
    21  type Network interface {
    22  	io.Closer
    23  	// Hostname returns the hostname of the local machine.
    24  	Hostname() (string, error)
    25  	// InterfaceAddrs returns a list of the network interfaces addresses.
    26  	InterfaceAddrs() ([]stdnet.Addr, error)
    27  	// LookupHost looks up the given host using the local resolver. It returns a slice of that host's addresses.
    28  	LookupHost(host string) ([]string, error)
    29  	// Dial connects to the address on the named network.
    30  	// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only).
    31  	Dial(network, address string) (stdnet.Conn, error)
    32  	// DialContext connects to the address on the named network using the provided context.
    33  	DialContext(ctx context.Context, network, address string) (stdnet.Conn, error)
    34  	// Listen listens for incoming connections on the network address.
    35  	// Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only).
    36  	// If the address is an empty string, Listen listens on all available addresses.
    37  	Listen(network, address string) (stdnet.Listener, error)
    38  	// ListenPacket listens for incoming packets addressed to the local address.
    39  	// Known networks are "udp", "udp4" (IPv4-only), "udp6" (IPv6-only).
    40  	// Caveat: The SetDeadline, SetReadDeadline, or SetWriteDeadline functions on the returned
    41  	// PacketConn may not work as expected (due to gVisor issues).
    42  	ListenPacket(network, address string) (stdnet.PacketConn, error)
    43  }