1 // Copyright 2014 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.
5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris windows
15 func sockaddr(family int, address string) (syscall.Sockaddr, error) {
18 a, err := net.ResolveIPAddr("ip4", address)
25 if a.IP = a.IP.To4(); a.IP == nil {
26 return nil, net.InvalidAddrError("non-ipv4 address")
28 sa := &syscall.SockaddrInet4{}
29 copy(sa.Addr[:], a.IP)
31 case syscall.AF_INET6:
32 a, err := net.ResolveIPAddr("ip6", address)
37 a.IP = net.IPv6unspecified
39 if a.IP.Equal(net.IPv4zero) {
40 a.IP = net.IPv6unspecified
42 if a.IP = a.IP.To16(); a.IP == nil || a.IP.To4() != nil {
43 return nil, net.InvalidAddrError("non-ipv6 address")
45 sa := &syscall.SockaddrInet6{ZoneId: zoneToUint32(a.Zone)}
46 copy(sa.Addr[:], a.IP)
49 return nil, net.InvalidAddrError("unexpected family")
53 func zoneToUint32(zone string) uint32 {
57 if ifi, err := net.InterfaceByName(zone); err == nil {
58 return uint32(ifi.Index)
60 n, err := strconv.Atoi(zone)
67 func last(s string, b byte) int {
69 for i--; i >= 0; i-- {