Path: blob/master/src/java.base/unix/native/libnet/SdpSupport.c
41119 views
/*1* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <sys/types.h>26#include <sys/socket.h>27#include <errno.h>2829#if defined(__linux__)30#if !defined(AF_INET_SDP)31#define AF_INET_SDP 2732#endif33#endif3435#include "jni.h"36#include "jni_util.h"37#include "net_util.h"3839#define RESTARTABLE(_cmd, _result) do { \40do { \41_result = _cmd; \42} while((_result == -1) && (errno == EINTR)); \43} while(0)444546/**47* Creates a SDP socket.48*/49static int create(JNIEnv* env)50{51int s;5253#if defined(__linux__)54/**55* IPv6 not supported by SDP on Linux56*/57if (ipv6_available()) {58JNU_ThrowIOException(env, "IPv6 not supported");59return -1;60}61s = socket(AF_INET_SDP, SOCK_STREAM, 0);62#else63/* not supported on other platforms at this time */64s = -1;65errno = EPROTONOSUPPORT;66#endif6768if (s < 0)69JNU_ThrowIOExceptionWithLastError(env, "socket");70return s;71}7273/**74* Creates a SDP socket, returning file descriptor referencing the socket.75*/76JNIEXPORT jint JNICALL77Java_sun_net_sdp_SdpSupport_create0(JNIEnv *env, jclass cls)78{79return create(env);80}8182/**83* Converts an existing file descriptor, that references an unbound TCP socket,84* to SDP.85*/86JNIEXPORT void JNICALL87Java_sun_net_sdp_SdpSupport_convert0(JNIEnv *env, jclass cls, int fd)88{89int s = create(env);90if (s >= 0) {91socklen_t len;92int arg, res;93struct linger linger;9495/* copy socket options that are relevant to SDP */96len = sizeof(arg);97if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, &len) == 0)98setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, len);99#ifdef SO_REUSEPORT100len = sizeof(arg);101if (getsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char*)&arg, &len) == 0)102setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (char*)&arg, len);103#endif104len = sizeof(arg);105if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, &len) == 0)106setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, len);107len = sizeof(linger);108if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)&linger, &len) == 0)109setsockopt(s, SOL_SOCKET, SO_LINGER, (char*)&linger, len);110111RESTARTABLE(dup2(s, fd), res);112if (res < 0)113JNU_ThrowIOExceptionWithLastError(env, "dup2");114res = close(s);115if (res < 0 && !(*env)->ExceptionOccurred(env))116JNU_ThrowIOExceptionWithLastError(env, "close");117}118}119120121