< prev index next >

src/java.base/unix/native/libnet/PlainSocketImpl.c

Print this page
rev 14890 : 8158023: SocketExceptions contain too little information sometimes

*** 1,7 **** /* ! * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 474,484 **** "Address not available"); } else if ((errno == EISCONN) || (errno == EBADF)) { JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); } else { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "connect failed"); } return; } (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); --- 474,485 ---- "Address not available"); } else if ((errno == EISCONN) || (errno == EBADF)) { JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); } else { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "connect failed"); } return; } (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
*** 496,507 **** /* Now that we're a connected socket, let's extract the port number * that the system chose for us and store it in the Socket object. */ socklen_t slen = SOCKADDR_LEN; if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", ! "Error getting socket name"); } else { localport = NET_GetPortFromSockaddr((struct sockaddr *)&him); (*env)->SetIntField(env, this, psi_localportID, localport); } } --- 497,508 ---- /* Now that we're a connected socket, let's extract the port number * that the system chose for us and store it in the Socket object. */ socklen_t slen = SOCKADDR_LEN; if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); } else { localport = NET_GetPortFromSockaddr((struct sockaddr *)&him); (*env)->SetIntField(env, this, psi_localportID, localport); } }
*** 534,556 **** JNU_ThrowNullPointerException(env, "iaObj is null."); return; } /* bind */ ! if (NET_InetAddressToSockaddr(env, iaObj, localport, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) { ! return; } setDefaultScopeID(env, (struct sockaddr *)&him); if (NET_Bind(fd, (struct sockaddr *)&him, len) < 0) { if (errno == EADDRINUSE || errno == EADDRNOTAVAIL || errno == EPERM || errno == EACCES) { NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException", "Bind failed"); } else { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", ! "Bind failed"); } return; } /* set the address */ --- 535,558 ---- JNU_ThrowNullPointerException(env, "iaObj is null."); return; } /* bind */ ! if (NET_InetAddressToSockaddr(env, iaObj, localport, (struct sockaddr *)&him, ! &len, JNI_TRUE) != 0) { ! return; } setDefaultScopeID(env, (struct sockaddr *)&him); if (NET_Bind(fd, (struct sockaddr *)&him, len) < 0) { if (errno == EADDRINUSE || errno == EADDRNOTAVAIL || errno == EPERM || errno == EACCES) { NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "BindException", "Bind failed"); } else { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Bind failed"); } return; } /* set the address */
*** 561,572 **** socklen_t slen = sizeof(him); /* Now that we're a connected socket, let's extract the port number * that the system chose for us and store it in the Socket object. */ if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", ! "Error getting socket name"); return; } localport = NET_GetPortFromSockaddr((struct sockaddr *)&him); (*env)->SetIntField(env, this, psi_localportID, localport); } else { --- 563,574 ---- socklen_t slen = sizeof(him); /* Now that we're a connected socket, let's extract the port number * that the system chose for us and store it in the Socket object. */ if (getsockname(fd, (struct sockaddr *)&him, &slen) == -1) { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); return; } localport = NET_GetPortFromSockaddr((struct sockaddr *)&him); (*env)->SetIntField(env, this, psi_localportID, localport); } else {
*** 578,589 **** * Class: java_net_PlainSocketImpl * Method: socketListen * Signature: (I)V */ JNIEXPORT void JNICALL ! Java_java_net_PlainSocketImpl_socketListen (JNIEnv *env, jobject this, ! jint count) { /* this FileDescriptor fd field */ jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); /* fdObj's int fd field */ int fd; --- 580,591 ---- * Class: java_net_PlainSocketImpl * Method: socketListen * Signature: (I)V */ JNIEXPORT void JNICALL ! Java_java_net_PlainSocketImpl_socketListen(JNIEnv *env, jobject this, ! jint count) { /* this FileDescriptor fd field */ jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID); /* fdObj's int fd field */ int fd;
*** 602,613 **** */ if (count == 0x7fffffff) count -= 1; if (listen(fd, count) == -1) { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", ! "Listen failed"); } } /* * Class: java_net_PlainSocketImpl --- 604,615 ---- */ if (count == 0x7fffffff) count -= 1; if (listen(fd, count) == -1) { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Listen failed"); } } /* * Class: java_net_PlainSocketImpl
*** 682,692 **** if (errno == EBADF) { JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); } else if (errno == ENOMEM) { JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); } else { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed"); } return; } newfd = NET_Accept(fd, (struct sockaddr *)&him, &slen); --- 684,695 ---- if (errno == EBADF) { JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); } else if (errno == ENOMEM) { JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); } else { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Accept failed"); } return; } newfd = NET_Accept(fd, (struct sockaddr *)&him, &slen);
*** 725,735 **** errno = EBADF; } if (errno == EBADF) { JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); } else { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed"); } } return; } --- 728,739 ---- errno = EBADF; } if (errno == EBADF) { JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); } else { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Accept failed"); } } return; }
*** 779,790 **** /* NET_SocketAvailable returns 0 for failure, 1 for success */ if (NET_SocketAvailable(fd, &ret) == 0){ if (errno == ECONNRESET) { JNU_ThrowByName(env, "sun/net/ConnectionResetException", ""); } else { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", ! "ioctl FIONREAD failed"); } } return ret; } --- 783,794 ---- /* NET_SocketAvailable returns 0 for failure, 1 for success */ if (NET_SocketAvailable(fd, &ret) == 0){ if (errno == ECONNRESET) { JNU_ThrowByName(env, "sun/net/ConnectionResetException", ""); } else { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "ioctl FIONREAD failed"); } } return ret; }
*** 934,945 **** jio_snprintf(fullMsg, sizeof(fullMsg), "Invalid option or socket reset by remote peer"); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg); return; } #endif /* __solaris__ */ ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", ! "Error setting socket option"); } } /* * Class: java_net_PlainSocketImpl --- 938,949 ---- jio_snprintf(fullMsg, sizeof(fullMsg), "Invalid option or socket reset by remote peer"); JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg); return; } #endif /* __solaris__ */ ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Error setting socket option"); } } /* * Class: java_net_PlainSocketImpl
*** 979,990 **** jfieldID iaFieldID; len = SOCKADDR_LEN; if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", ! "Error getting socket name"); return -1; } iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); CHECK_NULL_RETURN(iaObj, -1); --- 983,994 ---- jfieldID iaFieldID; len = SOCKADDR_LEN; if (getsockname(fd, (struct sockaddr *)&him, &len) < 0) { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Error getting socket name"); return -1; } iaObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port); CHECK_NULL_RETURN(iaObj, -1);
*** 1012,1023 **** } else { optlen = sizeof(optval.i); } if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) { ! NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", ! "Error getting socket option"); return -1; } switch (cmd) { case java_net_SocketOptions_SO_LINGER: --- 1016,1027 ---- } else { optlen = sizeof(optval.i); } if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) { ! JNU_ThrowByNameWithMessageAndLastError ! (env, JNU_JAVANETPKG "SocketException", "Error getting socket option"); return -1; } switch (cmd) { case java_net_SocketOptions_SO_LINGER:
*** 1061,1068 **** } } n = NET_Send(fd, (char *)&d, 1, MSG_OOB); if (n == -1) { ! NET_ThrowByNameWithLastError(env, "java/io/IOException", "Write failed"); } } --- 1065,1072 ---- } } n = NET_Send(fd, (char *)&d, 1, MSG_OOB); if (n == -1) { ! JNU_ThrowIOExceptionWithLastError(env, "Write failed"); } }
< prev index next >