forked from lesismal/nbio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_unix.go
151 lines (124 loc) · 3.04 KB
/
net_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// +build linux darwin netbsd freebsd openbsd dragonfly
package nbio
import (
"errors"
"net"
"syscall"
)
func sockaddrToAddr(sa syscall.Sockaddr) net.Addr {
var a net.Addr
switch sa := sa.(type) {
case *syscall.SockaddrInet4:
a = &net.TCPAddr{
IP: append([]byte{}, sa.Addr[:]...),
Port: sa.Port,
}
case *syscall.SockaddrInet6:
var zone string
if sa.ZoneId != 0 {
if ifi, err := net.InterfaceByIndex(int(sa.ZoneId)); err == nil {
zone = ifi.Name
}
}
a = &net.TCPAddr{
IP: append([]byte{}, sa.Addr[:]...),
Port: sa.Port,
Zone: zone,
}
case *syscall.SockaddrUnix:
a = &net.UnixAddr{Net: "unix", Name: sa.Name}
}
return a
}
func getSockaddr(proto, addr string) (sa syscall.Sockaddr, soType int, err error) {
var tcp *net.TCPAddr
tcp, err = net.ResolveTCPAddr(proto, addr)
if err != nil && tcp.IP != nil {
return nil, -1, err
}
tcpVersion, err := determineTCPProto(proto, tcp)
if err != nil {
return nil, -1, err
}
switch tcpVersion {
case "tcp":
return &syscall.SockaddrInet4{Port: tcp.Port}, syscall.AF_INET, nil
case "tcp4":
sa := &syscall.SockaddrInet4{Port: tcp.Port}
if tcp.IP != nil {
copy(sa.Addr[:], tcp.IP[12:16])
}
return sa, syscall.AF_INET, nil
case "tcp6":
sa := &syscall.SockaddrInet6{Port: tcp.Port}
if tcp.IP != nil {
copy(sa.Addr[:], tcp.IP)
}
if tcp.Zone != "" {
iface, err := net.InterfaceByName(tcp.Zone)
if err != nil {
return nil, -1, err
}
sa.ZoneId = uint32(iface.Index)
}
return sa, syscall.AF_INET6, nil
}
return nil, -1, errors.New("unsupported protocol")
}
func determineTCPProto(proto string, ip *net.TCPAddr) (string, error) {
if ip.IP.To4() != nil {
return "tcp4", nil
}
if ip.IP.To16() != nil {
return "tcp6", nil
}
switch proto {
case "tcp", "tcp4", "tcp6":
return proto, nil
}
return "", errors.New("unsupported protocol")
}
func listen(network, address string, backlogNum int64) (int, error) {
var (
err error
soType, fd int
sockaddr syscall.Sockaddr
)
if sockaddr, soType, err = getSockaddr(network, address); err != nil {
return -1, err
}
syscall.ForkLock.RLock()
defer syscall.ForkLock.RUnlock()
if fd, err = syscall.Socket(soType, syscall.SOCK_STREAM, syscall.IPPROTO_TCP); err != nil {
return -1, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
syscall.Close(fd)
return -1, err
}
socketOptReusePort := 0x0F
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, socketOptReusePort, 1); err != nil {
socketOptReusePort = 0x200
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, socketOptReusePort, 1); err != nil {
syscall.Close(fd)
return -1, err
}
}
if err = syscall.Bind(fd, sockaddr); err != nil {
syscall.Close(fd)
return -1, err
}
n := int(backlogNum)
if backlogNum <= 0 {
n = syscall.SOMAXCONN
}
if err = syscall.Listen(fd, n); err != nil {
syscall.Close(fd)
return -1, err
}
if err = syscall.SetNonblock(fd, true); err != nil {
syscall.Close(fd)
return -1, err
}
return fd, nil
}