forked from netty/netty
/
BsdSocket.java
234 lines (201 loc) · 9.35 KB
/
BsdSocket.java
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.kqueue;
import io.netty.channel.DefaultFileRegion;
import io.netty.channel.unix.IovArray;
import io.netty.channel.unix.PeerCredentials;
import io.netty.channel.unix.Socket;
import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import static io.netty.channel.kqueue.AcceptFilter.PLATFORM_UNSUPPORTED;
import static io.netty.channel.kqueue.Native.CONNECT_TCP_FASTOPEN;
import static io.netty.channel.unix.Errors.ERRNO_EINPROGRESS_NEGATIVE;
import static io.netty.channel.unix.Errors.ioResult;
import static io.netty.channel.unix.NativeInetAddress.ipv4MappedIpv6Address;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
/**
* A socket which provides access BSD native methods.
*/
final class BsdSocket extends Socket {
// These limits are just based on observations. I couldn't find anything in header files which formally
// define these limits.
private static final int APPLE_SND_LOW_AT_MAX = 1 << 17;
private static final int FREEBSD_SND_LOW_AT_MAX = 1 << 15;
static final int BSD_SND_LOW_AT_MAX = Math.min(APPLE_SND_LOW_AT_MAX, FREEBSD_SND_LOW_AT_MAX);
/**
* The `endpoints` structure passed to `connectx(2)` has an optional "source interface" field,
* which is the index of the network interface to use.
* According to `if_nametoindex(3)`, the value 0 is used when no interface is specified.
*/
private static final int UNSPECIFIED_SOURCE_INTERFACE = 0;
BsdSocket(int fd) {
super(fd);
}
void setAcceptFilter(AcceptFilter acceptFilter) throws IOException {
setAcceptFilter(intValue(), acceptFilter.filterName(), acceptFilter.filterArgs());
}
void setTcpNoPush(boolean tcpNoPush) throws IOException {
setTcpNoPush(intValue(), tcpNoPush ? 1 : 0);
}
void setSndLowAt(int lowAt) throws IOException {
setSndLowAt(intValue(), lowAt);
}
boolean isTcpNoPush() throws IOException {
return getTcpNoPush(intValue()) != 0;
}
int getSndLowAt() throws IOException {
return getSndLowAt(intValue());
}
AcceptFilter getAcceptFilter() throws IOException {
String[] result = getAcceptFilter(intValue());
return result == null ? PLATFORM_UNSUPPORTED : new AcceptFilter(result[0], result[1]);
}
PeerCredentials getPeerCredentials() throws IOException {
return getPeerCredentials(intValue());
}
long sendFile(DefaultFileRegion src, long baseOffset, long offset, long length) throws IOException {
// Open the file-region as it may be created via the lazy constructor. This is needed as we directly access
// the FileChannel field via JNI.
src.open();
long res = sendFile(intValue(), src, baseOffset, offset, length);
if (res >= 0) {
return res;
}
return ioResult("sendfile", (int) res);
}
/**
* Establish a connection to the given destination address, and send the given data to it.
*
* <strong>Note:</strong> This method relies on the {@code connectx(2)} system call, which is MacOS specific.
*
* @param source the source address we are connecting from.
* @param destination the destination address we are connecting to.
* @param data the data to copy to the kernel-side socket buffer.
* @param tcpFastOpen if {@code true}, set the flags needed to enable TCP FastOpen connecting.
* @return The number of bytes copied to the kernel-side socket buffer, or the number of bytes sent to the
* destination. This number is <em>negative</em> if connecting is left in an in-progress state,
* or <em>positive</em> if the connection was immediately established.
* @throws IOException if an IO error occurs, if the {@code data} is too big to send in one go,
* or if the system call is not supported on your platform.
*/
int connectx(InetSocketAddress source, InetSocketAddress destination, IovArray data, boolean tcpFastOpen)
throws IOException {
checkNotNull(destination, "Destination InetSocketAddress cannot be null.");
int flags = tcpFastOpen ? CONNECT_TCP_FASTOPEN : 0;
boolean sourceIPv6;
byte[] sourceAddress;
int sourceScopeId;
int sourcePort;
if (source == null) {
sourceIPv6 = false;
sourceAddress = null;
sourceScopeId = 0;
sourcePort = 0;
} else {
InetAddress sourceInetAddress = source.getAddress();
sourceIPv6 = sourceInetAddress instanceof Inet6Address;
if (sourceIPv6) {
sourceAddress = sourceInetAddress.getAddress();
sourceScopeId = ((Inet6Address) sourceInetAddress).getScopeId();
} else {
// convert to ipv4 mapped ipv6 address;
sourceScopeId = 0;
sourceAddress = ipv4MappedIpv6Address(sourceInetAddress.getAddress());
}
sourcePort = source.getPort();
}
InetAddress destinationInetAddress = destination.getAddress();
boolean destinationIPv6 = destinationInetAddress instanceof Inet6Address;
byte[] destinationAddress;
int destinationScopeId;
if (destinationIPv6) {
destinationAddress = destinationInetAddress.getAddress();
destinationScopeId = ((Inet6Address) destinationInetAddress).getScopeId();
} else {
// convert to ipv4 mapped ipv6 address;
destinationScopeId = 0;
destinationAddress = ipv4MappedIpv6Address(destinationInetAddress.getAddress());
}
int destinationPort = destination.getPort();
long iovAddress;
int iovCount;
int iovDataLength;
if (data == null || data.count() == 0) {
iovAddress = 0;
iovCount = 0;
iovDataLength = 0;
} else {
iovAddress = data.memoryAddress(0);
iovCount = data.count();
long size = data.size();
if (size > Integer.MAX_VALUE) {
throw new IOException("IovArray.size() too big: " + size + " bytes.");
}
iovDataLength = (int) size;
}
int result = connectx(intValue(),
UNSPECIFIED_SOURCE_INTERFACE, sourceIPv6, sourceAddress, sourceScopeId, sourcePort,
destinationIPv6, destinationAddress, destinationScopeId, destinationPort,
flags, iovAddress, iovCount, iovDataLength);
if (result == ERRNO_EINPROGRESS_NEGATIVE) {
// This is normal for non-blocking sockets.
// We'll know the connection has been established when the socket is selectable for writing.
// Tell the channel the data was written, so the outbound buffer can update its position.
return -iovDataLength;
}
if (result < 0) {
return ioResult("connectx", result);
}
return result;
}
public static BsdSocket newSocketStream() {
return new BsdSocket(newSocketStream0());
}
public static BsdSocket newSocketDgram() {
return new BsdSocket(newSocketDgram0());
}
public static BsdSocket newSocketDomain() {
return new BsdSocket(newSocketDomain0());
}
public static BsdSocket newSocketDomainDgram() {
return new BsdSocket(newSocketDomainDgram0());
}
private static native long sendFile(int socketFd, DefaultFileRegion src, long baseOffset,
long offset, long length) throws IOException;
/**
* @return If successful, zero or positive number of bytes transfered, otherwise negative errno.
*/
private static native int connectx(
int socketFd,
// sa_endpoints_t *endpoints:
int sourceInterface,
boolean sourceIPv6, byte[] sourceAddress, int sourceScopeId, int sourcePort,
boolean destinationIPv6, byte[] destinationAddress, int destinationScopeId, int destinationPort,
// sae_associd_t associd is reserved
int flags,
long iovAddress, int iovCount, int iovDataLength
// sae_connid_t *connid is reserved
);
private static native String[] getAcceptFilter(int fd) throws IOException;
private static native int getTcpNoPush(int fd) throws IOException;
private static native int getSndLowAt(int fd) throws IOException;
private static native PeerCredentials getPeerCredentials(int fd) throws IOException;
private static native void setAcceptFilter(int fd, String filterName, String filterArgs) throws IOException;
private static native void setTcpNoPush(int fd, int tcpNoPush) throws IOException;
private static native void setSndLowAt(int fd, int lowAt) throws IOException;
}