1 /* 2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 #include <sys/socket.h> 26 #include <string.h> 27 #include <errno.h> 28 #include <unistd.h> 29 30 #include <jni.h> 31 #include <netinet/tcp.h> 32 #include <netinet/in.h> 33 #include "jni_util.h" 34 #include "jdk_net_LinuxSocketOptions.h" 35 36 /* 37 * Class: jdk_net_LinuxSocketOptions 38 * Method: setQuickAck 39 * Signature: (II)V 40 */ 41 JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setQuickAck0 42 (JNIEnv *env, jobject unused, jint fd, jboolean on) { 43 int optval; 44 int rv; 45 optval = (on ? 1 : 0); 46 rv = setsockopt(fd, SOL_SOCKET, TCP_QUICKACK, &optval, sizeof (optval)); 47 if (rv < 0) { 48 if (errno == ENOPROTOOPT) { 49 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", 50 "unsupported socket option"); 51 } else { 52 JNU_ThrowByNameWithLastError(env, "java/net/SocketException", 53 "set option TCP_QUICKACK failed"); 54 } 55 } 56 } 57 58 /* 59 * Class: jdk_net_LinuxSocketOptions 60 * Method: getQuickAck 61 * Signature: (I)Z; 62 */ 63 JNIEXPORT jboolean JNICALL Java_jdk_net_LinuxSocketOptions_getQuickAck0 64 (JNIEnv *env, jobject unused, jint fd) { 65 int on; 66 socklen_t sz = sizeof (on); 67 int rv = getsockopt(fd, SOL_SOCKET, TCP_QUICKACK, &on, &sz); 68 if (rv < 0) { 69 if (errno == ENOPROTOOPT) { 70 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", 71 "unsupported socket option"); 72 } else { 73 JNU_ThrowByNameWithLastError(env, "java/net/SocketException", 74 "get option TCP_QUICKACK failed"); 75 } 76 } 77 return on != 0; 78 } 79 80 /* 81 * Class: jdk_net_LinuxSocketOptions 82 * Method: quickAckSupported 83 * Signature: ()Z 84 */ 85 JNIEXPORT jboolean JNICALL Java_jdk_net_LinuxSocketOptions_quickAckSupported0 86 (JNIEnv *env, jobject unused) { 87 int one = 1; 88 int rv, s; 89 s = socket(PF_INET, SOCK_STREAM, 0); 90 if (s < 0) { 91 return JNI_FALSE; 92 } 93 rv = setsockopt(s, SOL_SOCKET, TCP_QUICKACK, (void *) &one, sizeof (one)); 94 if (rv != 0 && errno == ENOPROTOOPT) { 95 rv = JNI_FALSE; 96 } else { 97 rv = JNI_TRUE; 98 } 99 close(s); 100 return rv; 101 } 102 103 static jint socketOptionSupported(jint sockopt) { 104 jint one = 1; 105 jint rv, s; 106 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 107 if (s < 0) { 108 return 0; 109 } 110 rv = setsockopt(s, SOL_TCP, sockopt, (void *) &one, sizeof (one)); 111 if (rv != 0 && errno == ENOPROTOOPT) { 112 rv = 0; 113 } else { 114 rv = 1; 115 } 116 close(s); 117 return rv; 118 } 119 120 static void handleError(JNIEnv *env, jint rv, const char *errmsg) { 121 if (rv < 0) { 122 if (errno == ENOPROTOOPT) { 123 JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", 124 "unsupported socket option"); 125 } else { 126 JNU_ThrowByNameWithLastError(env, "java/net/SocketException", errmsg); 127 } 128 } 129 } 130 131 /* 132 * Class: jdk_net_LinuxSocketOptions 133 * Method: keepAliveOptionsSupported0 134 * Signature: ()Z 135 */ 136 JNIEXPORT jboolean JNICALL Java_jdk_net_LinuxSocketOptions_keepAliveOptionsSupported0 137 (JNIEnv *env, jobject unused) { 138 return socketOptionSupported(TCP_KEEPIDLE) && socketOptionSupported(TCP_KEEPCNT) 139 && socketOptionSupported(TCP_KEEPINTVL); 140 } 141 142 /* 143 * Class: jdk_net_LinuxSocketOptions 144 * Method: setTcpkeepAliveProbes0 145 * Signature: (II)V 146 */ 147 JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpkeepAliveProbes0 148 (JNIEnv *env, jobject unused, jint fd, jint optval) { 149 jint rv = setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &optval, sizeof (optval)); 150 handleError(env, rv, "set option TCP_KEEPCNT failed"); 151 } 152 153 /* 154 * Class: jdk_net_LinuxSocketOptions 155 * Method: setTcpKeepAliveTime0 156 * Signature: (II)V 157 */ 158 JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpKeepAliveTime0 159 (JNIEnv *env, jobject unused, jint fd, jint optval) { 160 jint rv = setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &optval, sizeof (optval)); 161 handleError(env, rv, "set option TCP_KEEPIDLE failed"); 162 } 163 164 /* 165 * Class: jdk_net_LinuxSocketOptions 166 * Method: setTcpKeepAliveIntvl0 167 * Signature: (II)V 168 */ 169 JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpKeepAliveIntvl0 170 (JNIEnv *env, jobject unused, jint fd, jint optval) { 171 jint rv = setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &optval, sizeof (optval)); 172 handleError(env, rv, "set option TCP_KEEPINTVL failed"); 173 } 174 175 /* 176 * Class: jdk_net_LinuxSocketOptions 177 * Method: getTcpkeepAliveProbes0 178 * Signature: (I)I; 179 */ 180 JNIEXPORT jint JNICALL Java_jdk_net_LinuxSocketOptions_getTcpkeepAliveProbes0 181 (JNIEnv *env, jobject unused, jint fd) { 182 jint optval, rv; 183 socklen_t sz = sizeof (optval); 184 rv = getsockopt(fd, SOL_TCP, TCP_KEEPCNT, &optval, &sz); 185 handleError(env, rv, "get option TCP_KEEPCNT failed"); 186 return optval; 187 } 188 189 /* 190 * Class: jdk_net_LinuxSocketOptions 191 * Method: getTcpKeepAliveTime0 192 * Signature: (I)I; 193 */ 194 JNIEXPORT jint JNICALL Java_jdk_net_LinuxSocketOptions_getTcpKeepAliveTime0 195 (JNIEnv *env, jobject unused, jint fd) { 196 jint optval, rv; 197 socklen_t sz = sizeof (optval); 198 rv = getsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &optval, &sz); 199 handleError(env, rv, "get option TCP_KEEPIDLE failed"); 200 return optval; 201 } 202 203 /* 204 * Class: jdk_net_LinuxSocketOptions 205 * Method: getTcpKeepAliveIntvl0 206 * Signature: (I)I; 207 */ 208 JNIEXPORT jint JNICALL Java_jdk_net_LinuxSocketOptions_getTcpKeepAliveIntvl0 209 (JNIEnv *env, jobject unused, jint fd) { 210 jint optval, rv; 211 socklen_t sz = sizeof (optval); 212 rv = getsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &optval, &sz); 213 handleError(env, rv, "get option TCP_KEEPINTVL failed"); 214 return optval; 215 }