1 /* 2 * Copyright (c) 2009, 2010, 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 #ifdef AF_INET6 60 int domain = ipv6_available() ? AF_INET6 : AF_INET; 61 #else 62 int domain = AF_INET; 63 #endif 64 s = socket(domain, SOCK_STREAM, PROTO_SDP); 65 #elif defined(__linux__) 66 /** 67 * IPv6 not supported by SDP on Linux 68 */ 69 if (ipv6_available()) { 70 JNU_ThrowIOException(env, "IPv6 not supported"); 71 return -1; 72 } 73 s = socket(AF_INET_SDP, SOCK_STREAM, 0); 74 #else 75 /* not supported on other platforms at this time */ 76 s = -1; 77 errno = EPROTONOSUPPORT; 78 #endif 79 80 if (s < 0) 81 JNU_ThrowIOExceptionWithLastError(env, "socket"); 82 return s; 83 } 84 85 /** 86 * Creates a SDP socket, returning file descriptor referencing the socket. 87 */ 88 JNIEXPORT jint JNICALL 89 Java_sun_net_sdp_SdpSupport_create0(JNIEnv *env, jclass cls) 90 { 91 return create(env); 92 } 93 94 /** 95 * Converts an existing file descriptor, that references an unbound TCP socket, 96 * to SDP. 97 */ 98 JNIEXPORT void JNICALL 99 Java_sun_net_sdp_SdpSupport_convert0(JNIEnv *env, jclass cls, int fd) 100 { 101 int s = create(env); 102 if (s >= 0) { 103 socklen_t len; 104 int arg, res; 105 struct linger linger; 106 107 /* copy socket options that are relevant to SDP */ 108 len = sizeof(arg); 109 if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, &len) == 0) 110 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, len); 111 len = sizeof(arg); 112 if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, &len) == 0) 113 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, len); 114 len = sizeof(linger); 115 if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)&linger, &len) == 0) 116 setsockopt(s, SOL_SOCKET, SO_LINGER, (char*)&linger, len); 117 118 RESTARTABLE(dup2(s, fd), res); 119 if (res < 0) 120 JNU_ThrowIOExceptionWithLastError(env, "dup2"); 121 RESTARTABLE(close(s), res); 122 if (res < 0 && !(*env)->ExceptionOccurred(env)) 123 JNU_ThrowIOExceptionWithLastError(env, "close"); 124 } 125 }