< prev index next >

src/java.base/unix/native/libnio/ch/Net.c

Print this page




  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 <poll.h>
  27 #include <sys/types.h>
  28 #include <sys/socket.h>
  29 #include <string.h>

  30 #include <netinet/in.h>
  31 #include <netinet/tcp.h>
  32 #include <limits.h>
  33 
  34 #include "jni.h"

  35 #include "jni_util.h"
  36 #include "jvm.h"
  37 #include "jlong.h"

  38 #include "sun_nio_ch_Net.h"
  39 #include "net_util.h"
  40 #include "net_util_md.h"
  41 #include "nio_util.h"
  42 #include "nio.h"
  43 
  44 #ifdef _AIX
  45 #include <stdlib.h>
  46 #include <sys/utsname.h>
  47 #endif
  48 
  49 /**
  50  * IP_MULTICAST_ALL supported since 2.6.31 but may not be available at
  51  * build time.
  52  */
  53 #ifdef __linux__
  54   #ifndef IP_MULTICAST_ALL
  55     #define IP_MULTICAST_ALL    49
  56   #endif
  57 #endif


 411 #endif /* _ALLBSD_SOURCE */
 412     }
 413     return NET_GetPortFromSockaddr(&sa);
 414 }
 415 
 416 JNIEXPORT jobject JNICALL
 417 Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
 418 {
 419     SOCKETADDRESS sa;
 420     socklen_t sa_len = sizeof(SOCKETADDRESS);
 421     int port;
 422     if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 423 #ifdef _ALLBSD_SOURCE
 424         /*
 425          * XXXBSD:
 426          * ECONNRESET is specific to the BSDs. We can not return an error,
 427          * as the calling Java code with raise a java.lang.Error with the expectation
 428          * that getsockname() will never fail. According to the Single UNIX Specification,
 429          * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
 430          */
 431         if (errno == ECONNRESET) {
 432             bzero(&sa.sa4, sizeof(sa));
 433             sa.sa4.sin_len  = sizeof(struct sockaddr_in);
 434             sa.sa4.sin_family = AF_INET;
 435             sa.sa4.sin_port = htonl(0);
 436             sa.sa4.sin_addr.s_addr = INADDR_ANY;
 437         } else {
 438             handleSocketError(env, errno);
 439             return NULL;
 440         }
 441 #else /* _ALLBSD_SOURCE */
 442         handleSocketError(env, errno);
 443         return NULL;
 444 #endif /* _ALLBSD_SOURCE */
 445     }
 446     return NET_SockaddrToInetAddress(env, &sa, &port);
 447 }
 448 
 449 JNIEXPORT jint JNICALL
 450 Java_sun_nio_ch_Net_remotePort(JNIEnv *env, jclass clazz, jobject fdo)
 451 {




  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 <poll.h>
  27 #include <sys/types.h>
  28 #include <sys/socket.h>
  29 #include <string.h>
  30 #include <stddef.h>
  31 #include <netinet/in.h>
  32 #include <netinet/tcp.h>
  33 #include <limits.h>
  34 
  35 #include "jni.h"
  36 #include "java_props.h"
  37 #include "jni_util.h"
  38 #include "jvm.h"
  39 #include "jlong.h"
  40 #include "java_net_InetAddress.h"
  41 #include "sun_nio_ch_Net.h"
  42 #include "net_util.h"
  43 #include "net_util_md.h"
  44 #include "nio_util.h"
  45 #include "nio.h"
  46 
  47 #ifdef _AIX
  48 #include <stdlib.h>
  49 #include <sys/utsname.h>
  50 #endif
  51 
  52 /**
  53  * IP_MULTICAST_ALL supported since 2.6.31 but may not be available at
  54  * build time.
  55  */
  56 #ifdef __linux__
  57   #ifndef IP_MULTICAST_ALL
  58     #define IP_MULTICAST_ALL    49
  59   #endif
  60 #endif


 414 #endif /* _ALLBSD_SOURCE */
 415     }
 416     return NET_GetPortFromSockaddr(&sa);
 417 }
 418 
 419 JNIEXPORT jobject JNICALL
 420 Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
 421 {
 422     SOCKETADDRESS sa;
 423     socklen_t sa_len = sizeof(SOCKETADDRESS);
 424     int port;
 425     if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) {
 426 #ifdef _ALLBSD_SOURCE
 427         /*
 428          * XXXBSD:
 429          * ECONNRESET is specific to the BSDs. We can not return an error,
 430          * as the calling Java code with raise a java.lang.Error with the expectation
 431          * that getsockname() will never fail. According to the Single UNIX Specification,
 432          * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
 433          */
 434         if (errno == ECONNRESET && sa.sa.sa_family != AF_UNIX) {
 435             bzero(&sa.sa4, sizeof(sa));
 436             sa.sa4.sin_len  = sizeof(struct sockaddr_in);
 437             sa.sa4.sin_family = AF_INET;
 438             sa.sa4.sin_port = htonl(0);
 439             sa.sa4.sin_addr.s_addr = INADDR_ANY;
 440         } else {
 441             handleSocketError(env, errno);
 442             return NULL;
 443         }
 444 #else /* _ALLBSD_SOURCE */
 445         handleSocketError(env, errno);
 446         return NULL;
 447 #endif /* _ALLBSD_SOURCE */
 448     }
 449     return NET_SockaddrToInetAddress(env, &sa, &port);
 450 }
 451 
 452 JNIEXPORT jint JNICALL
 453 Java_sun_nio_ch_Net_remotePort(JNIEnv *env, jclass clazz, jobject fdo)
 454 {


< prev index next >