Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/net/sdp/SdpSupport.c
32288 views
/*1* Copyright (c) 2009, 2010, 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(__solaris__)30#if !defined(PROTO_SDP)31#define PROTO_SDP 25732#endif33#elif defined(__linux__)34#if !defined(AF_INET_SDP)35#define AF_INET_SDP 2736#endif37#endif3839#include "jni.h"40#include "jni_util.h"41#include "net_util.h"4243#define RESTARTABLE(_cmd, _result) do { \44do { \45_result = _cmd; \46} while((_result == -1) && (errno == EINTR)); \47} while(0)484950/**51* Creates a SDP socket.52*/53static int create(JNIEnv* env)54{55int s;5657#if defined(__solaris__)58#ifdef AF_INET659int domain = ipv6_available() ? AF_INET6 : AF_INET;60#else61int domain = AF_INET;62#endif63s = socket(domain, SOCK_STREAM, PROTO_SDP);64#elif defined(__linux__)65/**66* IPv6 not supported by SDP on Linux67*/68if (ipv6_available()) {69JNU_ThrowIOException(env, "IPv6 not supported");70return -1;71}72s = socket(AF_INET_SDP, SOCK_STREAM, 0);73#else74/* not supported on other platforms at this time */75s = -1;76errno = EPROTONOSUPPORT;77#endif7879if (s < 0)80JNU_ThrowIOExceptionWithLastError(env, "socket");81return s;82}8384/**85* Creates a SDP socket, returning file descriptor referencing the socket.86*/87JNIEXPORT jint JNICALL88Java_sun_net_sdp_SdpSupport_create0(JNIEnv *env, jclass cls)89{90return create(env);91}9293/**94* Converts an existing file descriptor, that references an unbound TCP socket,95* to SDP.96*/97JNIEXPORT void JNICALL98Java_sun_net_sdp_SdpSupport_convert0(JNIEnv *env, jclass cls, int fd)99{100int s = create(env);101if (s >= 0) {102socklen_t len;103int arg, res;104struct linger linger;105106/* copy socket options that are relevant to SDP */107len = sizeof(arg);108if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, &len) == 0)109setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, len);110len = sizeof(arg);111if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, &len) == 0)112setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, len);113len = sizeof(linger);114if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)&linger, &len) == 0)115setsockopt(s, SOL_SOCKET, SO_LINGER, (char*)&linger, len);116117RESTARTABLE(dup2(s, fd), res);118if (res < 0)119JNU_ThrowIOExceptionWithLastError(env, "dup2");120RESTARTABLE(close(s), res);121}122}123124125