47733
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 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 "jni_util.h"
|
|
33 |
|
|
34 |
/*
|
|
35 |
* Class: jdk_net_LinuxSocketOptions
|
|
36 |
* Method: setQuickAck
|
|
37 |
* Signature: (II)V
|
|
38 |
*/
|
|
39 |
JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setQuickAck0
|
|
40 |
(JNIEnv *env, jobject unused, jint fd, jboolean on) {
|
|
41 |
int optval;
|
|
42 |
int rv;
|
|
43 |
optval = (on ? 1 : 0);
|
|
44 |
rv = setsockopt(fd, SOL_SOCKET, TCP_QUICKACK, &optval, sizeof (optval));
|
|
45 |
if (rv < 0) {
|
|
46 |
if (errno == ENOPROTOOPT) {
|
|
47 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
|
|
48 |
"unsupported socket option");
|
|
49 |
} else {
|
|
50 |
JNU_ThrowByNameWithLastError(env, "java/net/SocketException",
|
|
51 |
"set option TCP_QUICKACK failed");
|
|
52 |
}
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
|
56 |
/*
|
|
57 |
* Class: jdk_net_LinuxSocketOptions
|
|
58 |
* Method: getQuickAck
|
|
59 |
* Signature: (I)Z;
|
|
60 |
*/
|
|
61 |
JNIEXPORT jboolean JNICALL Java_jdk_net_LinuxSocketOptions_getQuickAck0
|
|
62 |
(JNIEnv *env, jobject unused, jint fd) {
|
|
63 |
int on;
|
|
64 |
socklen_t sz = sizeof (on);
|
|
65 |
int rv = getsockopt(fd, SOL_SOCKET, TCP_QUICKACK, &on, &sz);
|
|
66 |
if (rv < 0) {
|
|
67 |
if (errno == ENOPROTOOPT) {
|
|
68 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
|
|
69 |
"unsupported socket option");
|
|
70 |
} else {
|
|
71 |
JNU_ThrowByNameWithLastError(env, "java/net/SocketException",
|
|
72 |
"get option TCP_QUICKACK failed");
|
|
73 |
}
|
|
74 |
}
|
|
75 |
return on != 0;
|
|
76 |
}
|
|
77 |
|
|
78 |
/*
|
|
79 |
* Class: jdk_net_LinuxSocketOptions
|
|
80 |
* Method: quickAckSupported
|
|
81 |
* Signature: ()Z
|
|
82 |
*/
|
|
83 |
JNIEXPORT jboolean JNICALL Java_jdk_net_LinuxSocketOptions_quickAckSupported0
|
|
84 |
(JNIEnv *env, jobject unused) {
|
|
85 |
int one = 1;
|
|
86 |
int rv, s;
|
|
87 |
s = socket(PF_INET, SOCK_STREAM, 0);
|
|
88 |
if (s < 0) {
|
|
89 |
return JNI_FALSE;
|
|
90 |
}
|
|
91 |
rv = setsockopt(s, SOL_SOCKET, TCP_QUICKACK, (void *) &one, sizeof (one));
|
|
92 |
if (rv != 0 && errno == ENOPROTOOPT) {
|
|
93 |
rv = JNI_FALSE;
|
|
94 |
} else {
|
|
95 |
rv = JNI_TRUE;
|
|
96 |
}
|
|
97 |
close(s);
|
|
98 |
return rv;
|
|
99 |
}
|