1 // Copyright 2017 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.
13 func readRawConn(c syscall.RawConn, b []byte) (int, error) {
16 err := c.Read(func(s uintptr) bool {
19 var buf syscall.WSABuf
21 buf.Len = uint32(len(b))
22 operr = syscall.WSARecv(syscall.Handle(s), &buf, 1, &read, &flags, nil, nil)
32 func writeRawConn(c syscall.RawConn, b []byte) error {
34 err := c.Write(func(s uintptr) bool {
36 var buf syscall.WSABuf
38 buf.Len = uint32(len(b))
39 operr = syscall.WSASend(syscall.Handle(s), &buf, 1, &written, 0, nil, nil)
48 func controlRawConn(c syscall.RawConn, addr Addr) error {
50 fn := func(s uintptr) {
52 l = int32(unsafe.Sizeof(v))
53 operr = syscall.Getsockopt(syscall.Handle(s), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, (*byte)(unsafe.Pointer(&v)), &l)
57 switch addr := addr.(type) {
59 // There's no guarantee that IP-level socket
60 // options work well with dual stack sockets.
61 // A simple solution would be to take a look
62 // at the bound address to the raw connection
63 // and to classify the address family of the
64 // underlying socket by the bound address:
66 // - When IP.To16() != nil and IP.To4() == nil,
67 // we can assume that the raw connection
68 // consists of an IPv6 socket using only
71 // - When IP.To16() == nil and IP.To4() != nil,
72 // the raw connection consists of an IPv4
73 // socket using only IPv4 addresses.
75 // - Otherwise, the raw connection is a dual
76 // stack socket, an IPv6 socket using IPv6
77 // addresses including IPv4-mapped or
78 // IPv4-embedded IPv6 addresses.
79 if addr.IP.To16() != nil && addr.IP.To4() == nil {
80 operr = syscall.SetsockoptInt(syscall.Handle(s), syscall.IPPROTO_IPV6, syscall.IPV6_UNICAST_HOPS, 1)
81 } else if addr.IP.To16() == nil && addr.IP.To4() != nil {
82 operr = syscall.SetsockoptInt(syscall.Handle(s), syscall.IPPROTO_IP, syscall.IP_TTL, 1)
86 if err := c.Control(fn); err != nil {
92 func controlOnConnSetup(network string, address string, c syscall.RawConn) error {
96 case "tcp", "udp", "ip":
97 return errors.New("ambiguous network: " + network)
99 switch network[len(network)-1] {
101 fn = func(s uintptr) {
102 operr = syscall.SetsockoptInt(syscall.Handle(s), syscall.IPPROTO_IP, syscall.IP_TTL, 1)
105 fn = func(s uintptr) {
106 operr = syscall.SetsockoptInt(syscall.Handle(s), syscall.IPPROTO_IPV6, syscall.IPV6_UNICAST_HOPS, 1)
109 return errors.New("unknown network: " + network)
112 if err := c.Control(fn); err != nil {