...
Source file
src/net/rawconn_unix_test.go
Documentation: net
1
2
3
4
5
6
7
8 package net
9
10 import (
11 "errors"
12 "syscall"
13 )
14
15 func readRawConn(c syscall.RawConn, b []byte) (int, error) {
16 var operr error
17 var n int
18 err := c.Read(func(s uintptr) bool {
19 n, operr = syscall.Read(int(s), b)
20 if operr == syscall.EAGAIN {
21 return false
22 }
23 return true
24 })
25 if err != nil {
26 return n, err
27 }
28 return n, operr
29 }
30
31 func writeRawConn(c syscall.RawConn, b []byte) error {
32 var operr error
33 err := c.Write(func(s uintptr) bool {
34 _, operr = syscall.Write(int(s), b)
35 if operr == syscall.EAGAIN {
36 return false
37 }
38 return true
39 })
40 if err != nil {
41 return err
42 }
43 return operr
44 }
45
46 func controlRawConn(c syscall.RawConn, addr Addr) error {
47 var operr error
48 fn := func(s uintptr) {
49 _, operr = syscall.GetsockoptInt(int(s), syscall.SOL_SOCKET, syscall.SO_REUSEADDR)
50 if operr != nil {
51 return
52 }
53 switch addr := addr.(type) {
54 case *TCPAddr:
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 if addr.IP.To16() != nil && addr.IP.To4() == nil {
76 operr = syscall.SetsockoptInt(int(s), syscall.IPPROTO_IPV6, syscall.IPV6_UNICAST_HOPS, 1)
77 } else if addr.IP.To16() == nil && addr.IP.To4() != nil {
78 operr = syscall.SetsockoptInt(int(s), syscall.IPPROTO_IP, syscall.IP_TTL, 1)
79 }
80 }
81 }
82 if err := c.Control(fn); err != nil {
83 return err
84 }
85 return operr
86 }
87
88 func controlOnConnSetup(network string, address string, c syscall.RawConn) error {
89 var operr error
90 var fn func(uintptr)
91 switch network {
92 case "tcp", "udp", "ip":
93 return errors.New("ambiguous network: " + network)
94 case "unix", "unixpacket", "unixgram":
95 fn = func(s uintptr) {
96 _, operr = syscall.GetsockoptInt(int(s), syscall.SOL_SOCKET, syscall.SO_ERROR)
97 }
98 default:
99 switch network[len(network)-1] {
100 case '4':
101 fn = func(s uintptr) {
102 operr = syscall.SetsockoptInt(int(s), syscall.IPPROTO_IP, syscall.IP_TTL, 1)
103 }
104 case '6':
105 fn = func(s uintptr) {
106 operr = syscall.SetsockoptInt(int(s), syscall.IPPROTO_IPV6, syscall.IPV6_UNICAST_HOPS, 1)
107 }
108 default:
109 return errors.New("unknown network: " + network)
110 }
111 }
112 if err := c.Control(fn); err != nil {
113 return err
114 }
115 return operr
116 }
117
View as plain text