author | clanger |
Wed, 02 Nov 2016 11:52:51 +0100 | |
changeset 41834 | 99c4f72c0a91 |
parent 36115 | 0676e37a0b9c |
permissions | -rw-r--r-- |
6525 | 1 |
/* |
36115 | 2 |
* Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved. |
6525 | 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 |
||
26 |
#include <sys/types.h> |
|
27 |
#include <sys/socket.h> |
|
28 |
#include <errno.h> |
|
29 |
||
30 |
#if defined(__solaris__) |
|
31 |
#if !defined(PROTO_SDP) |
|
32 |
#define PROTO_SDP 257 |
|
33 |
#endif |
|
34 |
#elif defined(__linux__) |
|
35 |
#if !defined(AF_INET_SDP) |
|
36 |
#define AF_INET_SDP 27 |
|
37 |
#endif |
|
38 |
#endif |
|
39 |
||
40 |
#include "jni.h" |
|
41 |
#include "jni_util.h" |
|
42 |
#include "net_util.h" |
|
43 |
||
44 |
#define RESTARTABLE(_cmd, _result) do { \ |
|
45 |
do { \ |
|
46 |
_result = _cmd; \ |
|
47 |
} while((_result == -1) && (errno == EINTR)); \ |
|
48 |
} while(0) |
|
49 |
||
50 |
||
51 |
/** |
|
52 |
* Creates a SDP socket. |
|
53 |
*/ |
|
54 |
static int create(JNIEnv* env) |
|
55 |
{ |
|
56 |
int s; |
|
57 |
||
58 |
#if defined(__solaris__) |
|
59 |
int domain = ipv6_available() ? AF_INET6 : AF_INET; |
|
60 |
s = socket(domain, SOCK_STREAM, PROTO_SDP); |
|
61 |
#elif defined(__linux__) |
|
62 |
/** |
|
63 |
* IPv6 not supported by SDP on Linux |
|
64 |
*/ |
|
65 |
if (ipv6_available()) { |
|
66 |
JNU_ThrowIOException(env, "IPv6 not supported"); |
|
6850
56966b0a6a0d
6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents:
6525
diff
changeset
|
67 |
return -1; |
6525 | 68 |
} |
69 |
s = socket(AF_INET_SDP, SOCK_STREAM, 0); |
|
70 |
#else |
|
71 |
/* not supported on other platforms at this time */ |
|
72 |
s = -1; |
|
73 |
errno = EPROTONOSUPPORT; |
|
74 |
#endif |
|
75 |
||
76 |
if (s < 0) |
|
77 |
JNU_ThrowIOExceptionWithLastError(env, "socket"); |
|
78 |
return s; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Creates a SDP socket, returning file descriptor referencing the socket. |
|
83 |
*/ |
|
84 |
JNIEXPORT jint JNICALL |
|
85 |
Java_sun_net_sdp_SdpSupport_create0(JNIEnv *env, jclass cls) |
|
86 |
{ |
|
87 |
return create(env); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Converts an existing file descriptor, that references an unbound TCP socket, |
|
92 |
* to SDP. |
|
93 |
*/ |
|
94 |
JNIEXPORT void JNICALL |
|
95 |
Java_sun_net_sdp_SdpSupport_convert0(JNIEnv *env, jclass cls, int fd) |
|
96 |
{ |
|
97 |
int s = create(env); |
|
98 |
if (s >= 0) { |
|
99 |
socklen_t len; |
|
100 |
int arg, res; |
|
101 |
struct linger linger; |
|
102 |
||
103 |
/* copy socket options that are relevant to SDP */ |
|
104 |
len = sizeof(arg); |
|
105 |
if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, &len) == 0) |
|
106 |
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, len); |
|
36115 | 107 |
#ifdef SO_REUSEPORT |
108 |
len = sizeof(arg); |
|
109 |
if (getsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char*)&arg, &len) == 0) |
|
110 |
setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (char*)&arg, len); |
|
111 |
#endif |
|
6525 | 112 |
len = sizeof(arg); |
113 |
if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, &len) == 0) |
|
114 |
setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, len); |
|
115 |
len = sizeof(linger); |
|
116 |
if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)&linger, &len) == 0) |
|
117 |
setsockopt(s, SOL_SOCKET, SO_LINGER, (char*)&linger, len); |
|
118 |
||
119 |
RESTARTABLE(dup2(s, fd), res); |
|
120 |
if (res < 0) |
|
121 |
JNU_ThrowIOExceptionWithLastError(env, "dup2"); |
|
32512
89cd776e3a25
8080402: File Leak in jdk/src/java.base/share/classes/sun/net/sdp/SdpSupport.java
dfuchs
parents:
25859
diff
changeset
|
122 |
res = close(s); |
89cd776e3a25
8080402: File Leak in jdk/src/java.base/share/classes/sun/net/sdp/SdpSupport.java
dfuchs
parents:
25859
diff
changeset
|
123 |
if (res < 0 && !(*env)->ExceptionOccurred(env)) |
89cd776e3a25
8080402: File Leak in jdk/src/java.base/share/classes/sun/net/sdp/SdpSupport.java
dfuchs
parents:
25859
diff
changeset
|
124 |
JNU_ThrowIOExceptionWithLastError(env, "close"); |
6525 | 125 |
} |
126 |
} |