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(__linux__) 31 #if !defined(AF_INET_SDP) 32 #define AF_INET_SDP 27 33 #endif 34 #endif 35 36 #include "jni.h" 37 #include "jni_util.h" 38 #include "net_util.h" 39 40 #define RESTARTABLE(_cmd, _result) do { \ 41 do { \ 42 _result = _cmd; \ 43 } while((_result == -1) && (errno == EINTR)); \ 44 } while(0) 45 46 47 /** 48 * Creates a SDP socket. 49 */ 50 static int create(JNIEnv* env) 51 { 52 int s; 53 54 #if defined(__linux__) 55 /** 56 * IPv6 not supported by SDP on Linux 57 */ 58 if (ipv6_available()) { 59 JNU_ThrowIOException(env, "IPv6 not supported"); 60 return -1; 61 } 62 s = socket(AF_INET_SDP, SOCK_STREAM, 0); 63 #else 64 /* not supported on other platforms at this time */ 65 s = -1; 66 errno = EPROTONOSUPPORT; 67 #endif 68 69 if (s < 0) 70 JNU_ThrowIOExceptionWithLastError(env, "socket"); 71 return s; 72 } 73 74 /** 75 * Creates a SDP socket, returning file descriptor referencing the socket. 76 */ 77 JNIEXPORT jint JNICALL 78 Java_sun_net_sdp_SdpSupport_create0(JNIEnv *env, jclass cls) 79 { 80 return create(env); 81 } 82 83 /** 84 * Converts an existing file descriptor, that references an unbound TCP socket, 85 * to SDP. 86 */ 87 JNIEXPORT void JNICALL 88 Java_sun_net_sdp_SdpSupport_convert0(JNIEnv *env, jclass cls, int fd) 89 { 90 int s = create(env); 91 if (s >= 0) { 92 socklen_t len; 93 int arg, res; 94 struct linger linger; 95 96 /* copy socket options that are relevant to SDP */ 97 len = sizeof(arg); 98 if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, &len) == 0) 99 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, len); 100 #ifdef SO_REUSEPORT 101 len = sizeof(arg); 102 if (getsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char*)&arg, &len) == 0) 103 setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (char*)&arg, len); 104 #endif 105 len = sizeof(arg); 106 if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, &len) == 0) 107 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, len); 108 len = sizeof(linger); 109 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)&linger, &len) == 0) 110 setsockopt(s, SOL_SOCKET, SO_LINGER, (char*)&linger, len); 111 112 RESTARTABLE(dup2(s, fd), res); 113 if (res < 0) 114 JNU_ThrowIOExceptionWithLastError(env, "dup2"); 115 res = close(s); 116 if (res < 0 && !(*env)->ExceptionOccurred(env)) 117 JNU_ThrowIOExceptionWithLastError(env, "close"); 118 } 119 }