github.com/kubearmor/cilium@v1.6.12/pkg/proxy/ipsock_posix.go (about) 1 // Copyright 2009 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 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows 6 7 package proxy 8 9 import ( 10 "net" 11 "syscall" 12 ) 13 14 func ipToSockaddr(family int, ip net.IP, port int, zone string) (syscall.Sockaddr, error) { 15 switch family { 16 case syscall.AF_INET: 17 if len(ip) == 0 { 18 ip = net.IPv4zero 19 } 20 ip4 := ip.To4() 21 if ip4 == nil { 22 return nil, &net.AddrError{Err: "non-IPv4 address", Addr: ip.String()} 23 } 24 sa := &syscall.SockaddrInet4{Port: port} 25 copy(sa.Addr[:], ip4) 26 return sa, nil 27 case syscall.AF_INET6: 28 // In general, an IP wildcard address, which is either 29 // "0.0.0.0" or "::", means the entire IP addressing 30 // space. For some historical reason, it is used to 31 // specify "any available address" on some operations 32 // of IP node. 33 // 34 // When the IP node supports IPv4-mapped IPv6 address, 35 // we allow an listener to listen to the wildcard 36 // address of both IP addressing spaces by specifying 37 // IPv6 wildcard address. 38 if len(ip) == 0 || ip.Equal(net.IPv4zero) { 39 ip = net.IPv6zero 40 } 41 // We accept any IPv6 address including IPv4-mapped 42 // IPv6 address. 43 ip6 := ip.To16() 44 if ip6 == nil { 45 return nil, &net.AddrError{Err: "non-IPv6 address", Addr: ip.String()} 46 } 47 sa := &syscall.SockaddrInet6{Port: port, ZoneId: 0} 48 copy(sa.Addr[:], ip6) 49 return sa, nil 50 } 51 return nil, &net.AddrError{Err: "invalid address family", Addr: ip.String()} 52 }