1 | /* |
2 | * Copyright (c) 2009, 2016, 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 | |
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" ); |
67 | return -1; |
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); |
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 |
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" ); |
122 | res = close(s); |
123 | if (res < 0 && !(*env)->ExceptionOccurred(env)) |
124 | JNU_ThrowIOExceptionWithLastError(env, "close" ); |
125 | } |
126 | } |
127 | |