jdk/src/solaris/native/sun/net/spi/SdpProvider.c
changeset 6570 6fb864c9fe0b
parent 6569 eb3c76a898eb
parent 6563 7e0bcb97526a
child 6604 657f846b9848
equal deleted inserted replaced
6569:eb3c76a898eb 6570:6fb864c9fe0b
     1 /*
       
     2  * Copyright (c) 2009, 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 
       
    29 #if defined(__solaris__) && !defined(PROTO_SDP)
       
    30 #define PROTO_SDP       257
       
    31 #endif
       
    32 
       
    33 #include "jni.h"
       
    34 #include "jni_util.h"
       
    35 #include "net_util.h"
       
    36 
       
    37 #define RESTARTABLE(_cmd, _result) do { \
       
    38   do { \
       
    39     _result = _cmd; \
       
    40   } while((_result == -1) && (errno == EINTR)); \
       
    41 } while(0)
       
    42 
       
    43 JNIEXPORT void JNICALL
       
    44 Java_sun_net_spi_SdpProvider_convert(JNIEnv *env, jclass cls, jint fd)
       
    45 {
       
    46 #ifdef PROTO_SDP
       
    47 #ifdef AF_INET6
       
    48     int domain = ipv6_available() ? AF_INET6 : AF_INET;
       
    49 #else
       
    50     int domain = AF_INET;
       
    51 #endif
       
    52     int s = socket(domain, SOCK_STREAM, PROTO_SDP);
       
    53     if (s < 0) {
       
    54         JNU_ThrowIOExceptionWithLastError(env, "socket");
       
    55     } else {
       
    56         int arg, len, res;
       
    57         struct linger linger;
       
    58 
       
    59         /* copy socket options that are relevant to SDP */
       
    60         len = sizeof(arg);
       
    61         if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, &len) == 0)
       
    62             setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, len);
       
    63         len = sizeof(arg);
       
    64         if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, &len) == 0)
       
    65             setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, len);
       
    66         len = sizeof(linger);
       
    67         if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)&linger, &len) == 0)
       
    68             setsockopt(s, SOL_SOCKET, SO_LINGER, (char*)&linger, len);
       
    69 
       
    70         RESTARTABLE(dup2(s, fd), res);
       
    71         if (res < 0)
       
    72             JNU_ThrowIOExceptionWithLastError(env, "dup2");
       
    73         RESTARTABLE(close(s), res);
       
    74     }
       
    75 #else
       
    76     JNU_ThrowInternalError(env, "should not reach here");
       
    77 #endif
       
    78 }