1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5//go:build unix || js || wasip1 || windows
6
7package net
8
9import (
10	"syscall"
11)
12
13// A sockaddr represents a TCP, UDP, IP or Unix network endpoint
14// address that can be converted into a syscall.Sockaddr.
15type sockaddr interface {
16	Addr
17
18	// family returns the platform-dependent address family
19	// identifier.
20	family() int
21
22	// isWildcard reports whether the address is a wildcard
23	// address.
24	isWildcard() bool
25
26	// sockaddr returns the address converted into a syscall
27	// sockaddr type that implements syscall.Sockaddr
28	// interface. It returns a nil interface when the address is
29	// nil.
30	sockaddr(family int) (syscall.Sockaddr, error)
31
32	// toLocal maps the zero address to a local system address (127.0.0.1 or ::1)
33	toLocal(net string) sockaddr
34}
35
36func (fd *netFD) addrFunc() func(syscall.Sockaddr) Addr {
37	switch fd.family {
38	case syscall.AF_INET, syscall.AF_INET6:
39		switch fd.sotype {
40		case syscall.SOCK_STREAM:
41			return sockaddrToTCP
42		case syscall.SOCK_DGRAM:
43			return sockaddrToUDP
44		case syscall.SOCK_RAW:
45			return sockaddrToIP
46		}
47	case syscall.AF_UNIX:
48		switch fd.sotype {
49		case syscall.SOCK_STREAM:
50			return sockaddrToUnix
51		case syscall.SOCK_DGRAM:
52			return sockaddrToUnixgram
53		case syscall.SOCK_SEQPACKET:
54			return sockaddrToUnixpacket
55		}
56	}
57	return func(syscall.Sockaddr) Addr { return nil }
58}
59