sockaddr_windows.go 1.93 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package sockaddr

import (
Steven Allen's avatar
Steven Allen committed
4
	"golang.org/x/sys/unix"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5 6 7
	"unsafe"
)

Steven Allen's avatar
Steven Allen committed
8
func sockaddrToAny(sa unix.Sockaddr) (*unix.RawSockaddrAny, Socklen, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	if sa == nil {
Steven Allen's avatar
Steven Allen committed
10
		return nil, 0, unix.EINVAL
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13
	switch sa := sa.(type) {
Steven Allen's avatar
Steven Allen committed
14
	case *unix.SockaddrInet4:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
		if sa.Port < 0 || sa.Port > 0xFFFF {
Steven Allen's avatar
Steven Allen committed
16
			return nil, 0, unix.EINVAL
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
		}
Steven Allen's avatar
Steven Allen committed
18 19
		var raw unix.RawSockaddrInet4
		raw.Family = unix.AF_INET
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21 22 23 24 25
		p := (*[2]byte)(unsafe.Pointer(&raw.Port))
		p[0] = byte(sa.Port >> 8)
		p[1] = byte(sa.Port)
		for i := 0; i < len(sa.Addr); i++ {
			raw.Addr[i] = sa.Addr[i]
		}
Steven Allen's avatar
Steven Allen committed
26
		return (*unix.RawSockaddrAny)(unsafe.Pointer(&raw)), int32(unsafe.Sizeof(raw)), nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27

Steven Allen's avatar
Steven Allen committed
28
	case *unix.SockaddrInet6:
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29
		if sa.Port < 0 || sa.Port > 0xFFFF {
Steven Allen's avatar
Steven Allen committed
30
			return nil, 0, unix.EINVAL
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
		}
Steven Allen's avatar
Steven Allen committed
32 33
		var raw unix.RawSockaddrInet6
		raw.Family = unix.AF_INET6
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35 36 37 38 39 40
		p := (*[2]byte)(unsafe.Pointer(&raw.Port))
		p[0] = byte(sa.Port >> 8)
		p[1] = byte(sa.Port)
		raw.Scope_id = sa.ZoneId
		for i := 0; i < len(sa.Addr); i++ {
			raw.Addr[i] = sa.Addr[i]
		}
Steven Allen's avatar
Steven Allen committed
41
		return (*unix.RawSockaddrAny)(unsafe.Pointer(&raw)), int32(unsafe.Sizeof(raw)), nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42

Steven Allen's avatar
Steven Allen committed
43 44
	case *unix.SockaddrUnix:
		return nil, 0, unix.EWINDOWS
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
	}
Steven Allen's avatar
Steven Allen committed
46
	return nil, 0, unix.EAFNOSUPPORT
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48
}

Steven Allen's avatar
Steven Allen committed
49
func anyToSockaddr(rsa *unix.RawSockaddrAny) (unix.Sockaddr, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50
	if rsa == nil {
Steven Allen's avatar
Steven Allen committed
51
		return nil, 0, unix.EINVAL
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52 53
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
	switch rsa.Addr.Family {
Steven Allen's avatar
Steven Allen committed
55 56
	case unix.AF_UNIX:
		return nil, unix.EWINDOWS
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57

Steven Allen's avatar
Steven Allen committed
58 59 60
	case unix.AF_INET:
		pp := (*unix.RawSockaddrInet4)(unsafe.Pointer(rsa))
		sa := new(unix.SockaddrInet4)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61 62 63 64 65 66
		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
		sa.Port = int(p[0])<<8 + int(p[1])
		for i := 0; i < len(sa.Addr); i++ {
			sa.Addr[i] = pp.Addr[i]
		}
		return sa, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67

Steven Allen's avatar
Steven Allen committed
68 69 70
	case unix.AF_INET6:
		pp := (*unix.RawSockaddrInet6)(unsafe.Pointer(rsa))
		sa := new(unix.SockaddrInet6)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71 72 73 74 75 76 77 78
		p := (*[2]byte)(unsafe.Pointer(&pp.Port))
		sa.Port = int(p[0])<<8 + int(p[1])
		sa.ZoneId = pp.Scope_id
		for i := 0; i < len(sa.Addr); i++ {
			sa.Addr[i] = pp.Addr[i]
		}
		return sa, nil
	}
Steven Allen's avatar
Steven Allen committed
79
	return nil, unix.EAFNOSUPPORT
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80
}