| 1 |
/* |
| 2 |
* Copyright 2016 The Netty Project |
| 3 |
* |
| 4 |
* The Netty Project licenses this file to you under the Apache License, |
| 5 |
* version 2.0 (the "License"); you may not use this file except in compliance |
| 6 |
* with the License. You may obtain a copy of the License at: |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
* License for the specific language governing permissions and limitations |
| 14 |
* under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
* Since glibc 2.8, the _GNU_SOURCE feature test macro must be defined |
| 19 |
* (before including any header files) in order to obtain the |
| 20 |
* definition of the ucred structure. See <a href=https://linux.die.net/man/7/unix> |
| 21 |
*/ |
| 22 |
#define _GNU_SOURCE |
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <netinet/in.h> |
| 28 |
#include <sys/sendfile.h> |
| 29 |
#include <linux/tcp.h> // TCP_NOTSENT_LOWAT is a linux specific define |
| 30 |
|
| 31 |
#include "netty_epoll_linuxsocket.h" |
| 32 |
#include "netty_unix_errors.h" |
| 33 |
#include "netty_unix_filedescriptor.h" |
| 34 |
#include "netty_unix_jni.h" |
| 35 |
#include "netty_unix_socket.h" |
| 36 |
#include "netty_unix_util.h" |
| 37 |
|
| 38 |
// TCP_FASTOPEN is defined in linux 3.7. We define this here so older kernels can compile. |
| 39 |
#ifndef TCP_FASTOPEN |
| 40 |
#define TCP_FASTOPEN 23 |
| 41 |
#endif |
| 42 |
|
| 43 |
// TCP_FASTOPEN_CONNECT is defined in linux 4.11. We define this here so older kernels can compile. |
| 44 |
#ifndef TCP_FASTOPEN_CONNECT |
| 45 |
#define TCP_FASTOPEN_CONNECT 30 |
| 46 |
#endif |
| 47 |
|
| 48 |
// TCP_NOTSENT_LOWAT is defined in linux 3.12. We define this here so older kernels can compile. |
| 49 |
#ifndef TCP_NOTSENT_LOWAT |
| 50 |
#define TCP_NOTSENT_LOWAT 25 |
| 51 |
#endif |
| 52 |
|
| 53 |
// SO_BUSY_POLL is defined in linux 3.11. We define this here so older kernels can compile. |
| 54 |
#ifndef SO_BUSY_POLL |
| 55 |
#define SO_BUSY_POLL 46 |
| 56 |
#endif |
| 57 |
|
| 58 |
static jclass peerCredentialsClass = NULL; |
| 59 |
static jmethodID peerCredentialsMethodId = NULL; |
| 60 |
|
| 61 |
static jfieldID fileChannelFieldId = NULL; |
| 62 |
static jfieldID transferredFieldId = NULL; |
| 63 |
static jfieldID fdFieldId = NULL; |
| 64 |
static jfieldID fileDescriptorFieldId = NULL; |
| 65 |
|
| 66 |
// JNI Registered Methods Begin |
| 67 |
static void netty_epoll_linuxsocket_setTimeToLive(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 68 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_TTL, &optval, sizeof(optval)); |
| 69 |
} |
| 70 |
|
| 71 |
static void netty_epoll_linuxsocket_setIpMulticastLoop(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jint optval) { |
| 72 |
if (ipv6 == JNI_TRUE) { |
| 73 |
u_int val = (u_int) optval; |
| 74 |
netty_unix_socket_setOption(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val, sizeof(val)); |
| 75 |
} else { |
| 76 |
u_char val = (u_char) optval; |
| 77 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_MULTICAST_LOOP, &val, sizeof(val)); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static void netty_epoll_linuxsocket_setInterface(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jbyteArray interfaceAddress, jint scopeId, jint interfaceIndex) { |
| 82 |
struct sockaddr_storage interfaceAddr; |
| 83 |
socklen_t interfaceAddrSize; |
| 84 |
struct sockaddr_in* interfaceIpAddr; |
| 85 |
|
| 86 |
memset(&interfaceAddr, 0, sizeof(interfaceAddr)); |
| 87 |
|
| 88 |
if (ipv6 == JNI_TRUE) { |
| 89 |
if (interfaceIndex == -1) { |
| 90 |
netty_unix_errors_throwIOException(env, "Unable to find network index"); |
| 91 |
return; |
| 92 |
} |
| 93 |
netty_unix_socket_setOption(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &interfaceIndex, sizeof(interfaceIndex)); |
| 94 |
} else { |
| 95 |
if (netty_unix_socket_initSockaddr(env, ipv6, interfaceAddress, scopeId, 0, &interfaceAddr, &interfaceAddrSize) == -1) { |
| 96 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr"); |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
interfaceIpAddr = (struct sockaddr_in*) &interfaceAddr; |
| 101 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_MULTICAST_IF, &interfaceIpAddr->sin_addr, sizeof(interfaceIpAddr->sin_addr)); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
static void netty_epoll_linuxsocket_setTcpCork(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 106 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval)); |
| 107 |
} |
| 108 |
|
| 109 |
static void netty_epoll_linuxsocket_setTcpQuickAck(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 110 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_QUICKACK, &optval, sizeof(optval)); |
| 111 |
} |
| 112 |
|
| 113 |
static void netty_epoll_linuxsocket_setTcpDeferAccept(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 114 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &optval, sizeof(optval)); |
| 115 |
} |
| 116 |
|
| 117 |
static void netty_epoll_linuxsocket_setTcpNotSentLowAt(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 118 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_NOTSENT_LOWAT, &optval, sizeof(optval)); |
| 119 |
} |
| 120 |
|
| 121 |
static void netty_epoll_linuxsocket_setTcpFastOpen(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 122 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_FASTOPEN, &optval, sizeof(optval)); |
| 123 |
} |
| 124 |
|
| 125 |
static void netty_epoll_linuxsocket_setTcpFastOpenConnect(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 126 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &optval, sizeof(optval)); |
| 127 |
} |
| 128 |
|
| 129 |
static void netty_epoll_linuxsocket_setTcpKeepIdle(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 130 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval)); |
| 131 |
} |
| 132 |
|
| 133 |
static void netty_epoll_linuxsocket_setTcpKeepIntvl(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 134 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval)); |
| 135 |
} |
| 136 |
|
| 137 |
static void netty_epoll_linuxsocket_setTcpKeepCnt(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 138 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval)); |
| 139 |
} |
| 140 |
|
| 141 |
static void netty_epoll_linuxsocket_setTcpUserTimeout(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 142 |
netty_unix_socket_setOption(env, fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &optval, sizeof(optval)); |
| 143 |
} |
| 144 |
|
| 145 |
static void netty_epoll_linuxsocket_setIpFreeBind(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 146 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_FREEBIND, &optval, sizeof(optval)); |
| 147 |
} |
| 148 |
|
| 149 |
static void netty_epoll_linuxsocket_setIpTransparent(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 150 |
netty_unix_socket_setOption(env, fd, SOL_IP, IP_TRANSPARENT, &optval, sizeof(optval)); |
| 151 |
} |
| 152 |
|
| 153 |
static void netty_epoll_linuxsocket_setIpRecvOrigDestAddr(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 154 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_RECVORIGDSTADDR, &optval, sizeof(optval)); |
| 155 |
} |
| 156 |
|
| 157 |
static void netty_epoll_linuxsocket_setSoBusyPoll(JNIEnv* env, jclass clazz, jint fd, jint optval) { |
| 158 |
netty_unix_socket_setOption(env, fd, SOL_SOCKET, SO_BUSY_POLL, &optval, sizeof(optval)); |
| 159 |
} |
| 160 |
|
| 161 |
static void netty_epoll_linuxsocket_joinGroup(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jbyteArray groupAddress, jbyteArray interfaceAddress, jint scopeId, jint interfaceIndex) { |
| 162 |
struct sockaddr_storage groupAddr; |
| 163 |
socklen_t groupAddrSize; |
| 164 |
struct sockaddr_storage interfaceAddr; |
| 165 |
socklen_t interfaceAddrSize; |
| 166 |
struct sockaddr_in* groupIpAddr; |
| 167 |
struct sockaddr_in* interfaceIpAddr; |
| 168 |
struct ip_mreq mreq; |
| 169 |
|
| 170 |
struct sockaddr_in6* groupIp6Addr; |
| 171 |
struct ipv6_mreq mreq6; |
| 172 |
|
| 173 |
memset(&groupAddr, 0, sizeof(groupAddr)); |
| 174 |
memset(&interfaceAddr, 0, sizeof(interfaceAddr)); |
| 175 |
|
| 176 |
if (netty_unix_socket_initSockaddr(env, ipv6, groupAddress, scopeId, 0, &groupAddr, &groupAddrSize) == -1) { |
| 177 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for groupAddress"); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
switch (groupAddr.ss_family) { |
| 182 |
case AF_INET: |
| 183 |
if (netty_unix_socket_initSockaddr(env, ipv6, interfaceAddress, scopeId, 0, &interfaceAddr, &interfaceAddrSize) == -1) { |
| 184 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for interfaceAddr"); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
interfaceIpAddr = (struct sockaddr_in*) &interfaceAddr; |
| 189 |
groupIpAddr = (struct sockaddr_in*) &groupAddr; |
| 190 |
|
| 191 |
memcpy(&mreq.imr_multiaddr, &groupIpAddr->sin_addr, sizeof(groupIpAddr->sin_addr)); |
| 192 |
memcpy(&mreq.imr_interface, &interfaceIpAddr->sin_addr, sizeof(interfaceIpAddr->sin_addr)); |
| 193 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 194 |
break; |
| 195 |
case AF_INET6: |
| 196 |
if (interfaceIndex == -1) { |
| 197 |
netty_unix_errors_throwIOException(env, "Unable to find network index"); |
| 198 |
return; |
| 199 |
} |
| 200 |
mreq6.ipv6mr_interface = interfaceIndex; |
| 201 |
|
| 202 |
groupIp6Addr = (struct sockaddr_in6*) &groupAddr; |
| 203 |
memcpy(&mreq6.ipv6mr_multiaddr, &groupIp6Addr->sin6_addr, sizeof(groupIp6Addr->sin6_addr)); |
| 204 |
netty_unix_socket_setOption(env, fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6, sizeof(mreq6)); |
| 205 |
break; |
| 206 |
default: |
| 207 |
netty_unix_errors_throwIOException(env, "Address family not supported"); |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
static void netty_epoll_linuxsocket_joinSsmGroup(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jbyteArray groupAddress, jbyteArray interfaceAddress, jint scopeId, jint interfaceIndex, jbyteArray sourceAddress) { |
| 213 |
struct sockaddr_storage groupAddr; |
| 214 |
socklen_t groupAddrSize; |
| 215 |
struct sockaddr_storage interfaceAddr; |
| 216 |
socklen_t interfaceAddrSize; |
| 217 |
struct sockaddr_storage sourceAddr; |
| 218 |
socklen_t sourceAddrSize; |
| 219 |
struct sockaddr_in* groupIpAddr; |
| 220 |
struct sockaddr_in* interfaceIpAddr; |
| 221 |
struct sockaddr_in* sourceIpAddr; |
| 222 |
struct ip_mreq_source mreq; |
| 223 |
|
| 224 |
struct group_source_req mreq6; |
| 225 |
|
| 226 |
memset(&groupAddr, 0, sizeof(groupAddr)); |
| 227 |
memset(&sourceAddr, 0, sizeof(sourceAddr)); |
| 228 |
memset(&interfaceAddr, 0, sizeof(interfaceAddr)); |
| 229 |
|
| 230 |
if (netty_unix_socket_initSockaddr(env, ipv6, groupAddress, scopeId, 0, &groupAddr, &groupAddrSize) == -1) { |
| 231 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for groupAddress"); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
if (netty_unix_socket_initSockaddr(env, ipv6, sourceAddress, scopeId, 0, &sourceAddr, &sourceAddrSize) == -1) { |
| 236 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for sourceAddress"); |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
switch (groupAddr.ss_family) { |
| 241 |
case AF_INET: |
| 242 |
if (netty_unix_socket_initSockaddr(env, ipv6, interfaceAddress, scopeId, 0, &interfaceAddr, &interfaceAddrSize) == -1) { |
| 243 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for interfaceAddress"); |
| 244 |
return; |
| 245 |
} |
| 246 |
interfaceIpAddr = (struct sockaddr_in*) &interfaceAddr; |
| 247 |
groupIpAddr = (struct sockaddr_in*) &groupAddr; |
| 248 |
sourceIpAddr = (struct sockaddr_in*) &sourceAddr; |
| 249 |
memcpy(&mreq.imr_multiaddr, &groupIpAddr->sin_addr, sizeof(groupIpAddr->sin_addr)); |
| 250 |
memcpy(&mreq.imr_interface, &interfaceIpAddr->sin_addr, sizeof(interfaceIpAddr->sin_addr)); |
| 251 |
memcpy(&mreq.imr_sourceaddr, &sourceIpAddr->sin_addr, sizeof(sourceIpAddr->sin_addr)); |
| 252 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 253 |
break; |
| 254 |
case AF_INET6: |
| 255 |
if (interfaceIndex == -1) { |
| 256 |
netty_unix_errors_throwIOException(env, "Unable to find network index"); |
| 257 |
return; |
| 258 |
} |
| 259 |
mreq6.gsr_group = groupAddr; |
| 260 |
mreq6.gsr_interface = interfaceIndex; |
| 261 |
mreq6.gsr_source = sourceAddr; |
| 262 |
netty_unix_socket_setOption(env, fd, IPPROTO_IPV6, MCAST_JOIN_SOURCE_GROUP, &mreq6, sizeof(mreq6)); |
| 263 |
break; |
| 264 |
default: |
| 265 |
netty_unix_errors_throwIOException(env, "Address family not supported"); |
| 266 |
break; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
static void netty_epoll_linuxsocket_leaveGroup(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jbyteArray groupAddress, jbyteArray interfaceAddress, jint scopeId, jint interfaceIndex) { |
| 271 |
struct sockaddr_storage groupAddr; |
| 272 |
socklen_t groupAddrSize; |
| 273 |
|
| 274 |
struct sockaddr_storage interfaceAddr; |
| 275 |
socklen_t interfaceAddrSize; |
| 276 |
struct sockaddr_in* groupIpAddr; |
| 277 |
struct sockaddr_in* interfaceIpAddr; |
| 278 |
struct ip_mreq mreq; |
| 279 |
|
| 280 |
struct sockaddr_in6* groupIp6Addr; |
| 281 |
struct ipv6_mreq mreq6; |
| 282 |
|
| 283 |
memset(&groupAddr, 0, sizeof(groupAddr)); |
| 284 |
memset(&interfaceAddr, 0, sizeof(interfaceAddr)); |
| 285 |
|
| 286 |
if (netty_unix_socket_initSockaddr(env, ipv6, groupAddress, scopeId, 0, &groupAddr, &groupAddrSize) == -1) { |
| 287 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for groupAddress"); |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
switch (groupAddr.ss_family) { |
| 292 |
case AF_INET: |
| 293 |
if (netty_unix_socket_initSockaddr(env, ipv6, interfaceAddress, scopeId, 0, &interfaceAddr, &interfaceAddrSize) == -1) { |
| 294 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for interfaceAddress"); |
| 295 |
return; |
| 296 |
} |
| 297 |
interfaceIpAddr = (struct sockaddr_in*) &interfaceAddr; |
| 298 |
groupIpAddr = (struct sockaddr_in*) &groupAddr; |
| 299 |
|
| 300 |
memcpy(&mreq.imr_multiaddr, &groupIpAddr->sin_addr, sizeof(groupIpAddr->sin_addr)); |
| 301 |
memcpy(&mreq.imr_interface, &interfaceIpAddr->sin_addr, sizeof(interfaceIpAddr->sin_addr)); |
| 302 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 303 |
break; |
| 304 |
case AF_INET6: |
| 305 |
if (interfaceIndex == -1) { |
| 306 |
netty_unix_errors_throwIOException(env, "Unable to find network index"); |
| 307 |
return; |
| 308 |
} |
| 309 |
mreq6.ipv6mr_interface = interfaceIndex; |
| 310 |
|
| 311 |
groupIp6Addr = (struct sockaddr_in6*) &groupAddr; |
| 312 |
memcpy(&mreq6.ipv6mr_multiaddr, &groupIp6Addr->sin6_addr, sizeof(groupIp6Addr->sin6_addr)); |
| 313 |
netty_unix_socket_setOption(env, fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq6, sizeof(mreq6)); |
| 314 |
break; |
| 315 |
default: |
| 316 |
netty_unix_errors_throwIOException(env, "Address family not supported"); |
| 317 |
break; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
static void netty_epoll_linuxsocket_leaveSsmGroup(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jbyteArray groupAddress, jbyteArray interfaceAddress, jint scopeId, jint interfaceIndex, jbyteArray sourceAddress) { |
| 322 |
struct sockaddr_storage groupAddr; |
| 323 |
socklen_t groupAddrSize; |
| 324 |
struct sockaddr_storage interfaceAddr; |
| 325 |
socklen_t interfaceAddrSize; |
| 326 |
struct sockaddr_storage sourceAddr; |
| 327 |
socklen_t sourceAddrSize; |
| 328 |
struct sockaddr_in* groupIpAddr; |
| 329 |
struct sockaddr_in* interfaceIpAddr; |
| 330 |
struct sockaddr_in* sourceIpAddr; |
| 331 |
|
| 332 |
struct ip_mreq_source mreq; |
| 333 |
struct group_source_req mreq6; |
| 334 |
|
| 335 |
memset(&groupAddr, 0, sizeof(groupAddr)); |
| 336 |
memset(&sourceAddr, 0, sizeof(sourceAddr)); |
| 337 |
memset(&interfaceAddr, 0, sizeof(interfaceAddr)); |
| 338 |
|
| 339 |
|
| 340 |
if (netty_unix_socket_initSockaddr(env, ipv6, groupAddress, scopeId, 0, &groupAddr, &groupAddrSize) == -1) { |
| 341 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for groupAddress"); |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
if (netty_unix_socket_initSockaddr(env, ipv6, sourceAddress, scopeId, 0, &sourceAddr, &sourceAddrSize) == -1) { |
| 346 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for sourceAddress"); |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
switch (groupAddr.ss_family) { |
| 351 |
case AF_INET: |
| 352 |
if (netty_unix_socket_initSockaddr(env, ipv6, interfaceAddress, scopeId, 0, &interfaceAddr, &interfaceAddrSize) == -1) { |
| 353 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr for interfaceAddress"); |
| 354 |
return; |
| 355 |
} |
| 356 |
interfaceIpAddr = (struct sockaddr_in*) &interfaceAddr; |
| 357 |
|
| 358 |
groupIpAddr = (struct sockaddr_in*) &groupAddr; |
| 359 |
sourceIpAddr = (struct sockaddr_in*) &sourceAddr; |
| 360 |
memcpy(&mreq.imr_multiaddr, &groupIpAddr->sin_addr, sizeof(groupIpAddr->sin_addr)); |
| 361 |
memcpy(&mreq.imr_interface, &interfaceIpAddr->sin_addr, sizeof(interfaceIpAddr->sin_addr)); |
| 362 |
memcpy(&mreq.imr_sourceaddr, &sourceIpAddr->sin_addr, sizeof(sourceIpAddr->sin_addr)); |
| 363 |
netty_unix_socket_setOption(env, fd, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, &mreq, sizeof(mreq)); |
| 364 |
break; |
| 365 |
case AF_INET6: |
| 366 |
if (interfaceIndex == -1) { |
| 367 |
netty_unix_errors_throwIOException(env, "Unable to find network index"); |
| 368 |
return; |
| 369 |
} |
| 370 |
|
| 371 |
mreq6.gsr_group = groupAddr; |
| 372 |
mreq6.gsr_interface = interfaceIndex; |
| 373 |
mreq6.gsr_source = sourceAddr; |
| 374 |
netty_unix_socket_setOption(env, fd, IPPROTO_IPV6, MCAST_LEAVE_SOURCE_GROUP, &mreq6, sizeof(mreq6)); |
| 375 |
break; |
| 376 |
default: |
| 377 |
netty_unix_errors_throwIOException(env, "Address family not supported"); |
| 378 |
break; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
static void netty_epoll_linuxsocket_setTcpMd5Sig(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jbyteArray address, jint scopeId, jbyteArray key) { |
| 383 |
struct sockaddr_storage addr; |
| 384 |
socklen_t addrSize; |
| 385 |
|
| 386 |
memset(&addr, 0, sizeof(addr)); |
| 387 |
|
| 388 |
if (netty_unix_socket_initSockaddr(env, ipv6, address, scopeId, 0, &addr, &addrSize) == -1) { |
| 389 |
netty_unix_errors_throwIOException(env, "Could not init sockaddr"); |
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
struct tcp_md5sig md5sig; |
| 394 |
memset(&md5sig, 0, sizeof(md5sig)); |
| 395 |
md5sig.tcpm_addr.ss_family = addr.ss_family; |
| 396 |
|
| 397 |
struct sockaddr_in* ipaddr; |
| 398 |
struct sockaddr_in6* ip6addr; |
| 399 |
|
| 400 |
switch (addr.ss_family) { |
| 401 |
case AF_INET: |
| 402 |
ipaddr = (struct sockaddr_in*) &addr; |
| 403 |
memcpy(&((struct sockaddr_in *) &md5sig.tcpm_addr)->sin_addr, &ipaddr->sin_addr, sizeof(ipaddr->sin_addr)); |
| 404 |
break; |
| 405 |
case AF_INET6: |
| 406 |
ip6addr = (struct sockaddr_in6*) &addr; |
| 407 |
memcpy(&((struct sockaddr_in6 *) &md5sig.tcpm_addr)->sin6_addr, &ip6addr->sin6_addr, sizeof(ip6addr->sin6_addr)); |
| 408 |
break; |
| 409 |
} |
| 410 |
|
| 411 |
if (key != NULL) { |
| 412 |
md5sig.tcpm_keylen = (*env)->GetArrayLength(env, key); |
| 413 |
(*env)->GetByteArrayRegion(env, key, 0, md5sig.tcpm_keylen, (void *) &md5sig.tcpm_key); |
| 414 |
if ((*env)->ExceptionCheck(env) == JNI_TRUE) { |
| 415 |
return; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
if (setsockopt(fd, IPPROTO_TCP, TCP_MD5SIG, &md5sig, sizeof(md5sig)) < 0) { |
| 420 |
netty_unix_errors_throwIOExceptionErrorNo(env, "setsockopt() failed: ", errno); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
static int netty_epoll_linuxsocket_getInterface(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6) { |
| 425 |
if (ipv6 == JNI_TRUE) { |
| 426 |
int optval; |
| 427 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &optval, sizeof(optval)) == -1) { |
| 428 |
return -1; |
| 429 |
} |
| 430 |
return optval; |
| 431 |
} else { |
| 432 |
struct in_addr optval; |
| 433 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_IP, IP_MULTICAST_IF, &optval, sizeof(optval)) == -1) { |
| 434 |
return -1; |
| 435 |
} |
| 436 |
|
| 437 |
return ntohl(optval.s_addr); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
static jint netty_epoll_linuxsocket_getTimeToLive(JNIEnv* env, jclass clazz, jint fd) { |
| 442 |
int optval; |
| 443 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_IP, IP_TTL, &optval, sizeof(optval)) == -1) { |
| 444 |
return -1; |
| 445 |
} |
| 446 |
return optval; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
static jint netty_epoll_linuxsocket_getIpMulticastLoop(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6) { |
| 451 |
if (ipv6 == JNI_TRUE) { |
| 452 |
u_int optval; |
| 453 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &optval, sizeof(optval)) == -1) { |
| 454 |
return -1; |
| 455 |
} |
| 456 |
return (jint) optval; |
| 457 |
} else { |
| 458 |
u_char optval; |
| 459 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_IP, IP_MULTICAST_LOOP, &optval, sizeof(optval)) == -1) { |
| 460 |
return -1; |
| 461 |
} |
| 462 |
return (jint) optval; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
static jint netty_epoll_linuxsocket_getTcpKeepIdle(JNIEnv* env, jclass clazz, jint fd) { |
| 467 |
int optval; |
| 468 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval)) == -1) { |
| 469 |
return -1; |
| 470 |
} |
| 471 |
return optval; |
| 472 |
} |
| 473 |
|
| 474 |
static jint netty_epoll_linuxsocket_getTcpKeepIntvl(JNIEnv* env, jclass clazz, jint fd) { |
| 475 |
int optval; |
| 476 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval)) == -1) { |
| 477 |
return -1; |
| 478 |
} |
| 479 |
return optval; |
| 480 |
} |
| 481 |
|
| 482 |
static jint netty_epoll_linuxsocket_getTcpKeepCnt(JNIEnv* env, jclass clazz, jint fd) { |
| 483 |
int optval; |
| 484 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval)) == -1) { |
| 485 |
return -1; |
| 486 |
} |
| 487 |
return optval; |
| 488 |
} |
| 489 |
|
| 490 |
static jint netty_epoll_linuxsocket_getTcpUserTimeout(JNIEnv* env, jclass clazz, jint fd) { |
| 491 |
int optval; |
| 492 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &optval, sizeof(optval)) == -1) { |
| 493 |
return -1; |
| 494 |
} |
| 495 |
return optval; |
| 496 |
} |
| 497 |
|
| 498 |
static jint netty_epoll_linuxsocket_isIpFreeBind(JNIEnv* env, jclass clazz, jint fd) { |
| 499 |
int optval; |
| 500 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_IP, IP_FREEBIND, &optval, sizeof(optval)) == -1) { |
| 501 |
return -1; |
| 502 |
} |
| 503 |
return optval; |
| 504 |
} |
| 505 |
|
| 506 |
static jint netty_epoll_linuxsocket_isIpTransparent(JNIEnv* env, jclass clazz, jint fd) { |
| 507 |
int optval; |
| 508 |
if (netty_unix_socket_getOption(env, fd, SOL_IP, IP_TRANSPARENT, &optval, sizeof(optval)) == -1) { |
| 509 |
return -1; |
| 510 |
} |
| 511 |
return optval; |
| 512 |
} |
| 513 |
|
| 514 |
static jint netty_epoll_linuxsocket_isIpRecvOrigDestAddr(JNIEnv* env, jclass clazz, jint fd) { |
| 515 |
int optval; |
| 516 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_IP, IP_RECVORIGDSTADDR, &optval, sizeof(optval)) == -1) { |
| 517 |
return -1; |
| 518 |
} |
| 519 |
return optval; |
| 520 |
} |
| 521 |
|
| 522 |
static void netty_epoll_linuxsocket_getTcpInfo(JNIEnv* env, jclass clazz, jint fd, jlongArray array) { |
| 523 |
struct tcp_info tcp_info; |
| 524 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_INFO, &tcp_info, sizeof(tcp_info)) == -1) { |
| 525 |
return; |
| 526 |
} |
| 527 |
jlong cArray[32]; |
| 528 |
// Expand to 64 bits, then cast away unsigned-ness. |
| 529 |
cArray[0] = (jlong) (uint64_t) tcp_info.tcpi_state; |
| 530 |
cArray[1] = (jlong) (uint64_t) tcp_info.tcpi_ca_state; |
| 531 |
cArray[2] = (jlong) (uint64_t) tcp_info.tcpi_retransmits; |
| 532 |
cArray[3] = (jlong) (uint64_t) tcp_info.tcpi_probes; |
| 533 |
cArray[4] = (jlong) (uint64_t) tcp_info.tcpi_backoff; |
| 534 |
cArray[5] = (jlong) (uint64_t) tcp_info.tcpi_options; |
| 535 |
cArray[6] = (jlong) (uint64_t) tcp_info.tcpi_snd_wscale; |
| 536 |
cArray[7] = (jlong) (uint64_t) tcp_info.tcpi_rcv_wscale; |
| 537 |
cArray[8] = (jlong) (uint64_t) tcp_info.tcpi_rto; |
| 538 |
cArray[9] = (jlong) (uint64_t) tcp_info.tcpi_ato; |
| 539 |
cArray[10] = (jlong) (uint64_t) tcp_info.tcpi_snd_mss; |
| 540 |
cArray[11] = (jlong) (uint64_t) tcp_info.tcpi_rcv_mss; |
| 541 |
cArray[12] = (jlong) (uint64_t) tcp_info.tcpi_unacked; |
| 542 |
cArray[13] = (jlong) (uint64_t) tcp_info.tcpi_sacked; |
| 543 |
cArray[14] = (jlong) (uint64_t) tcp_info.tcpi_lost; |
| 544 |
cArray[15] = (jlong) (uint64_t) tcp_info.tcpi_retrans; |
| 545 |
cArray[16] = (jlong) (uint64_t) tcp_info.tcpi_fackets; |
| 546 |
cArray[17] = (jlong) (uint64_t) tcp_info.tcpi_last_data_sent; |
| 547 |
cArray[18] = (jlong) (uint64_t) tcp_info.tcpi_last_ack_sent; |
| 548 |
cArray[19] = (jlong) (uint64_t) tcp_info.tcpi_last_data_recv; |
| 549 |
cArray[20] = (jlong) (uint64_t) tcp_info.tcpi_last_ack_recv; |
| 550 |
cArray[21] = (jlong) (uint64_t) tcp_info.tcpi_pmtu; |
| 551 |
cArray[22] = (jlong) (uint64_t) tcp_info.tcpi_rcv_ssthresh; |
| 552 |
cArray[23] = (jlong) (uint64_t) tcp_info.tcpi_rtt; |
| 553 |
cArray[24] = (jlong) (uint64_t) tcp_info.tcpi_rttvar; |
| 554 |
cArray[25] = (jlong) (uint64_t) tcp_info.tcpi_snd_ssthresh; |
| 555 |
cArray[26] = (jlong) (uint64_t) tcp_info.tcpi_snd_cwnd; |
| 556 |
cArray[27] = (jlong) (uint64_t) tcp_info.tcpi_advmss; |
| 557 |
cArray[28] = (jlong) (uint64_t) tcp_info.tcpi_reordering; |
| 558 |
cArray[29] = (jlong) (uint64_t) tcp_info.tcpi_rcv_rtt; |
| 559 |
cArray[30] = (jlong) (uint64_t) tcp_info.tcpi_rcv_space; |
| 560 |
cArray[31] = (jlong) (uint64_t) tcp_info.tcpi_total_retrans; |
| 561 |
|
| 562 |
(*env)->SetLongArrayRegion(env, array, 0, 32, cArray); |
| 563 |
} |
| 564 |
|
| 565 |
static jint netty_epoll_linuxsocket_isTcpCork(JNIEnv* env, jclass clazz, jint fd) { |
| 566 |
int optval; |
| 567 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_CORK, &optval, sizeof(optval)) == -1) { |
| 568 |
return -1; |
| 569 |
} |
| 570 |
return optval; |
| 571 |
} |
| 572 |
|
| 573 |
static jint netty_epoll_linuxsocket_getSoBusyPoll(JNIEnv* env, jclass clazz, jint fd) { |
| 574 |
int optval; |
| 575 |
if (netty_unix_socket_getOption(env, fd, SOL_SOCKET, SO_BUSY_POLL, &optval, sizeof(optval)) == -1) { |
| 576 |
return -1; |
| 577 |
} |
| 578 |
return optval; |
| 579 |
} |
| 580 |
|
| 581 |
static jint netty_epoll_linuxsocket_getTcpDeferAccept(JNIEnv* env, jclass clazz, jint fd) { |
| 582 |
int optval; |
| 583 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &optval, sizeof(optval)) == -1) { |
| 584 |
return -1; |
| 585 |
} |
| 586 |
return optval; |
| 587 |
} |
| 588 |
|
| 589 |
static jint netty_epoll_linuxsocket_isTcpQuickAck(JNIEnv* env, jclass clazz, jint fd) { |
| 590 |
int optval; |
| 591 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_QUICKACK, &optval, sizeof(optval)) == -1) { |
| 592 |
return -1; |
| 593 |
} |
| 594 |
return optval; |
| 595 |
} |
| 596 |
|
| 597 |
static jint netty_epoll_linuxsocket_isTcpFastOpenConnect(JNIEnv* env, jclass clazz, jint fd) { |
| 598 |
int optval; |
| 599 |
// We call netty_unix_socket_getOption0 directly so we can handle ENOPROTOOPT by ourself. |
| 600 |
if (netty_unix_socket_getOption0(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, &optval, sizeof(optval)) == -1) { |
| 601 |
if (errno == ENOPROTOOPT) { |
| 602 |
// Not supported by the system, so just return 0. |
| 603 |
return 0; |
| 604 |
} |
| 605 |
netty_unix_socket_getOptionHandleError(env, errno); |
| 606 |
return -1; |
| 607 |
} |
| 608 |
return optval; |
| 609 |
} |
| 610 |
|
| 611 |
static jint netty_epoll_linuxsocket_getTcpNotSentLowAt(JNIEnv* env, jclass clazz, jint fd) { |
| 612 |
int optval; |
| 613 |
if (netty_unix_socket_getOption(env, fd, IPPROTO_TCP, TCP_NOTSENT_LOWAT, &optval, sizeof(optval)) == -1) { |
| 614 |
return -1; |
| 615 |
} |
| 616 |
return optval; |
| 617 |
} |
| 618 |
|
| 619 |
static jobject netty_epoll_linuxsocket_getPeerCredentials(JNIEnv *env, jclass clazz, jint fd) { |
| 620 |
struct ucred credentials; |
| 621 |
if(netty_unix_socket_getOption(env,fd, SOL_SOCKET, SO_PEERCRED, &credentials, sizeof (credentials)) == -1) { |
| 622 |
return NULL; |
| 623 |
} |
| 624 |
jintArray gids = (*env)->NewIntArray(env, 1); |
| 625 |
(*env)->SetIntArrayRegion(env, gids, 0, 1, (jint*) &credentials.gid); |
| 626 |
return (*env)->NewObject(env, peerCredentialsClass, peerCredentialsMethodId, credentials.pid, credentials.uid, gids); |
| 627 |
} |
| 628 |
|
| 629 |
static jlong netty_epoll_linuxsocket_sendFile(JNIEnv* env, jclass clazz, jint fd, jobject fileRegion, jlong base_off, jlong off, jlong len) { |
| 630 |
jobject fileChannel = (*env)->GetObjectField(env, fileRegion, fileChannelFieldId); |
| 631 |
if (fileChannel == NULL) { |
| 632 |
netty_unix_errors_throwRuntimeException(env, "failed to get DefaultFileRegion.file"); |
| 633 |
return -1; |
| 634 |
} |
| 635 |
jobject fileDescriptor = (*env)->GetObjectField(env, fileChannel, fileDescriptorFieldId); |
| 636 |
if (fileDescriptor == NULL) { |
| 637 |
netty_unix_errors_throwRuntimeException(env, "failed to get FileChannelImpl.fd"); |
| 638 |
return -1; |
| 639 |
} |
| 640 |
jint srcFd = (*env)->GetIntField(env, fileDescriptor, fdFieldId); |
| 641 |
if (srcFd == -1) { |
| 642 |
netty_unix_errors_throwRuntimeException(env, "failed to get FileDescriptor.fd"); |
| 643 |
return -1; |
| 644 |
} |
| 645 |
ssize_t res; |
| 646 |
off_t offset = base_off + off; |
| 647 |
int err; |
| 648 |
do { |
| 649 |
res = sendfile(fd, srcFd, &offset, (size_t) len); |
| 650 |
} while (res == -1 && ((err = errno) == EINTR)); |
| 651 |
if (res < 0) { |
| 652 |
return -err; |
| 653 |
} |
| 654 |
if (res > 0) { |
| 655 |
// update the transferred field in DefaultFileRegion |
| 656 |
(*env)->SetLongField(env, fileRegion, transferredFieldId, off + res); |
| 657 |
} |
| 658 |
|
| 659 |
return res; |
| 660 |
} |
| 661 |
// JNI Registered Methods End |
| 662 |
|
| 663 |
// JNI Method Registration Table Begin |
| 664 |
static const JNINativeMethod fixed_method_table[] = { |
| 665 |
{ "setTimeToLive", "(II)V", (void *) netty_epoll_linuxsocket_setTimeToLive }, |
| 666 |
{ "getTimeToLive", "(I)I", (void *) netty_epoll_linuxsocket_getTimeToLive }, |
| 667 |
{ "setInterface", "(IZ[BII)V", (void *) netty_epoll_linuxsocket_setInterface }, |
| 668 |
{ "getInterface", "(IZ)I", (void *) netty_epoll_linuxsocket_getInterface }, |
| 669 |
{ "setIpMulticastLoop", "(IZI)V", (void * ) netty_epoll_linuxsocket_setIpMulticastLoop }, |
| 670 |
{ "getIpMulticastLoop", "(IZ)I", (void * ) netty_epoll_linuxsocket_getIpMulticastLoop }, |
| 671 |
{ "setTcpCork", "(II)V", (void *) netty_epoll_linuxsocket_setTcpCork }, |
| 672 |
{ "setSoBusyPoll", "(II)V", (void *) netty_epoll_linuxsocket_setSoBusyPoll }, |
| 673 |
{ "setTcpQuickAck", "(II)V", (void *) netty_epoll_linuxsocket_setTcpQuickAck }, |
| 674 |
{ "setTcpDeferAccept", "(II)V", (void *) netty_epoll_linuxsocket_setTcpDeferAccept }, |
| 675 |
{ "setTcpNotSentLowAt", "(II)V", (void *) netty_epoll_linuxsocket_setTcpNotSentLowAt }, |
| 676 |
{ "isTcpCork", "(I)I", (void *) netty_epoll_linuxsocket_isTcpCork }, |
| 677 |
{ "getSoBusyPoll", "(I)I", (void *) netty_epoll_linuxsocket_getSoBusyPoll }, |
| 678 |
{ "getTcpDeferAccept", "(I)I", (void *) netty_epoll_linuxsocket_getTcpDeferAccept }, |
| 679 |
{ "getTcpNotSentLowAt", "(I)I", (void *) netty_epoll_linuxsocket_getTcpNotSentLowAt }, |
| 680 |
{ "isTcpQuickAck", "(I)I", (void *) netty_epoll_linuxsocket_isTcpQuickAck }, |
| 681 |
{ "setTcpFastOpen", "(II)V", (void *) netty_epoll_linuxsocket_setTcpFastOpen }, |
| 682 |
{ "setTcpFastOpenConnect", "(II)V", (void *) netty_epoll_linuxsocket_setTcpFastOpenConnect }, |
| 683 |
{ "isTcpFastOpenConnect", "(I)I", (void *) netty_epoll_linuxsocket_isTcpFastOpenConnect }, |
| 684 |
{ "setTcpKeepIdle", "(II)V", (void *) netty_epoll_linuxsocket_setTcpKeepIdle }, |
| 685 |
{ "setTcpKeepIntvl", "(II)V", (void *) netty_epoll_linuxsocket_setTcpKeepIntvl }, |
| 686 |
{ "setTcpKeepCnt", "(II)V", (void *) netty_epoll_linuxsocket_setTcpKeepCnt }, |
| 687 |
{ "setTcpUserTimeout", "(II)V", (void *) netty_epoll_linuxsocket_setTcpUserTimeout }, |
| 688 |
{ "setIpFreeBind", "(II)V", (void *) netty_epoll_linuxsocket_setIpFreeBind }, |
| 689 |
{ "setIpTransparent", "(II)V", (void *) netty_epoll_linuxsocket_setIpTransparent }, |
| 690 |
{ "setIpRecvOrigDestAddr", "(II)V", (void *) netty_epoll_linuxsocket_setIpRecvOrigDestAddr }, |
| 691 |
{ "getTcpKeepIdle", "(I)I", (void *) netty_epoll_linuxsocket_getTcpKeepIdle }, |
| 692 |
{ "getTcpKeepIntvl", "(I)I", (void *) netty_epoll_linuxsocket_getTcpKeepIntvl }, |
| 693 |
{ "getTcpKeepCnt", "(I)I", (void *) netty_epoll_linuxsocket_getTcpKeepCnt }, |
| 694 |
{ "getTcpUserTimeout", "(I)I", (void *) netty_epoll_linuxsocket_getTcpUserTimeout }, |
| 695 |
{ "isIpFreeBind", "(I)I", (void *) netty_epoll_linuxsocket_isIpFreeBind }, |
| 696 |
{ "isIpTransparent", "(I)I", (void *) netty_epoll_linuxsocket_isIpTransparent }, |
| 697 |
{ "isIpRecvOrigDestAddr", "(I)I", (void *) netty_epoll_linuxsocket_isIpRecvOrigDestAddr }, |
| 698 |
{ "getTcpInfo", "(I[J)V", (void *) netty_epoll_linuxsocket_getTcpInfo }, |
| 699 |
{ "setTcpMd5Sig", "(IZ[BI[B)V", (void *) netty_epoll_linuxsocket_setTcpMd5Sig }, |
| 700 |
{ "joinGroup", "(IZ[B[BII)V", (void *) netty_epoll_linuxsocket_joinGroup }, |
| 701 |
{ "joinSsmGroup", "(IZ[B[BII[B)V", (void *) netty_epoll_linuxsocket_joinSsmGroup }, |
| 702 |
{ "leaveGroup", "(IZ[B[BII)V", (void *) netty_epoll_linuxsocket_leaveGroup }, |
| 703 |
{ "leaveSsmGroup", "(IZ[B[BII[B)V", (void *) netty_epoll_linuxsocket_leaveSsmGroup } |
| 704 |
// "sendFile" has a dynamic signature |
| 705 |
}; |
| 706 |
|
| 707 |
static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(fixed_method_table[0]); |
| 708 |
|
| 709 |
static jint dynamicMethodsTableSize() { |
| 710 |
return fixed_method_table_size + 2; // 2 is for the dynamic method signatures. |
| 711 |
} |
| 712 |
|
| 713 |
static JNINativeMethod* createDynamicMethodsTable(const char* packagePrefix) { |
| 714 |
char* dynamicTypeName = NULL; |
| 715 |
size_t size = sizeof(JNINativeMethod) * dynamicMethodsTableSize(); |
| 716 |
JNINativeMethod* dynamicMethods = malloc(size); |
| 717 |
if (dynamicMethods == NULL) { |
| 718 |
return NULL; |
| 719 |
} |
| 720 |
memset(dynamicMethods, 0, size); |
| 721 |
memcpy(dynamicMethods, fixed_method_table, sizeof(fixed_method_table)); |
| 722 |
|
| 723 |
JNINativeMethod* dynamicMethod = &dynamicMethods[fixed_method_table_size]; |
| 724 |
NETTY_PREPEND(packagePrefix, "io/netty/channel/unix/PeerCredentials;", dynamicTypeName, error); |
| 725 |
NETTY_PREPEND("(I)L", dynamicTypeName, dynamicMethod->signature, error); |
| 726 |
dynamicMethod->name = "getPeerCredentials"; |
| 727 |
dynamicMethod->fnPtr = (void *) netty_epoll_linuxsocket_getPeerCredentials; |
| 728 |
netty_unix_util_free_dynamic_name(&dynamicTypeName); |
| 729 |
|
| 730 |
++dynamicMethod; |
| 731 |
NETTY_PREPEND(packagePrefix, "io/netty/channel/DefaultFileRegion;JJJ)J", dynamicTypeName, error); |
| 732 |
NETTY_PREPEND("(IL", dynamicTypeName, dynamicMethod->signature, error); |
| 733 |
dynamicMethod->name = "sendFile"; |
| 734 |
dynamicMethod->fnPtr = (void *) netty_epoll_linuxsocket_sendFile; |
| 735 |
netty_unix_util_free_dynamic_name(&dynamicTypeName); |
| 736 |
return dynamicMethods; |
| 737 |
error: |
| 738 |
free(dynamicTypeName); |
| 739 |
netty_unix_util_free_dynamic_methods_table(dynamicMethods, fixed_method_table_size, dynamicMethodsTableSize()); |
| 740 |
return NULL; |
| 741 |
} |
| 742 |
|
| 743 |
// JNI Method Registration Table End |
| 744 |
|
| 745 |
jint netty_epoll_linuxsocket_JNI_OnLoad(JNIEnv* env, const char* packagePrefix) { |
| 746 |
int ret = JNI_ERR; |
| 747 |
char* nettyClassName = NULL; |
| 748 |
jclass fileRegionCls = NULL; |
| 749 |
jclass fileChannelCls = NULL; |
| 750 |
jclass fileDescriptorCls = NULL; |
| 751 |
// Register the methods which are not referenced by static member variables |
| 752 |
JNINativeMethod* dynamicMethods = createDynamicMethodsTable(packagePrefix); |
| 753 |
if (dynamicMethods == NULL) { |
| 754 |
goto done; |
| 755 |
} |
| 756 |
if (netty_unix_util_register_natives(env, |
| 757 |
packagePrefix, |
| 758 |
"io/netty/channel/epoll/LinuxSocket", |
| 759 |
dynamicMethods, |
| 760 |
dynamicMethodsTableSize()) != 0) { |
| 761 |
goto done; |
| 762 |
} |
| 763 |
|
| 764 |
NETTY_PREPEND(packagePrefix, "io/netty/channel/unix/PeerCredentials", nettyClassName, done); |
| 765 |
NETTY_LOAD_CLASS(env, peerCredentialsClass, nettyClassName, done); |
| 766 |
netty_unix_util_free_dynamic_name(&nettyClassName); |
| 767 |
|
| 768 |
NETTY_GET_METHOD(env, peerCredentialsClass, peerCredentialsMethodId, "<init>", "(II[I)V", done); |
| 769 |
|
| 770 |
NETTY_PREPEND(packagePrefix, "io/netty/channel/DefaultFileRegion", nettyClassName, done); |
| 771 |
NETTY_FIND_CLASS(env, fileRegionCls, nettyClassName, done); |
| 772 |
netty_unix_util_free_dynamic_name(&nettyClassName); |
| 773 |
|
| 774 |
NETTY_GET_FIELD(env, fileRegionCls, fileChannelFieldId, "file", "Ljava/nio/channels/FileChannel;", done); |
| 775 |
NETTY_GET_FIELD(env, fileRegionCls, transferredFieldId, "transferred", "J", done); |
| 776 |
|
| 777 |
NETTY_FIND_CLASS(env, fileChannelCls, "sun/nio/ch/FileChannelImpl", done); |
| 778 |
NETTY_GET_FIELD(env, fileChannelCls, fileDescriptorFieldId, "fd", "Ljava/io/FileDescriptor;", done); |
| 779 |
|
| 780 |
NETTY_FIND_CLASS(env, fileDescriptorCls, "java/io/FileDescriptor", done); |
| 781 |
NETTY_GET_FIELD(env, fileDescriptorCls, fdFieldId, "fd", "I", done); |
| 782 |
|
| 783 |
ret = NETTY_JNI_VERSION; |
| 784 |
done: |
| 785 |
netty_unix_util_free_dynamic_methods_table(dynamicMethods, fixed_method_table_size, dynamicMethodsTableSize()); |
| 786 |
free(nettyClassName); |
| 787 |
|
| 788 |
return ret; |
| 789 |
} |
| 790 |
|
| 791 |
void netty_epoll_linuxsocket_JNI_OnUnLoad(JNIEnv* env) { |
| 792 |
NETTY_UNLOAD_CLASS(env, peerCredentialsClass); |
| 793 |
} |