768
769 /**
770 * Enables SIO_LOOPBACK_FAST_PATH
771 */
772 JNIEXPORT jint JNICALL
773 NET_EnableFastTcpLoopback(int fd) {
774 int enabled = 1;
775 DWORD result_byte_count = -1;
776 int result = WSAIoctl(fd,
777 SIO_LOOPBACK_FAST_PATH,
778 &enabled,
779 sizeof(enabled),
780 NULL,
781 0,
782 &result_byte_count,
783 NULL,
784 NULL);
785 return result == SOCKET_ERROR ? WSAGetLastError() : 0;
786 }
787
788 /**
789 * See net_util.h for documentation
790 */
791 JNIEXPORT int JNICALL
792 NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port,
793 SOCKETADDRESS *sa, int *len,
794 jboolean v4MappedAddress)
795 {
796 jint family = getInetAddress_family(env, iaObj);
797 JNU_CHECK_EXCEPTION_RETURN(env, -1);
798 memset((char *)sa, 0, sizeof(SOCKETADDRESS));
799
800 if (ipv6_available() &&
801 !(family == java_net_InetAddress_IPv4 &&
802 v4MappedAddress == JNI_FALSE))
803 {
804 jbyte caddr[16];
805 jint address;
806 unsigned int scopeid = 0, cached_scope_id = 0;
807
|
768
769 /**
770 * Enables SIO_LOOPBACK_FAST_PATH
771 */
772 JNIEXPORT jint JNICALL
773 NET_EnableFastTcpLoopback(int fd) {
774 int enabled = 1;
775 DWORD result_byte_count = -1;
776 int result = WSAIoctl(fd,
777 SIO_LOOPBACK_FAST_PATH,
778 &enabled,
779 sizeof(enabled),
780 NULL,
781 0,
782 &result_byte_count,
783 NULL,
784 NULL);
785 return result == SOCKET_ERROR ? WSAGetLastError() : 0;
786 }
787
788 int
789 IsWindows10RS3OrGreater() {
790 OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0 };
791 DWORDLONG const cond_mask = VerSetConditionMask(
792 VerSetConditionMask(
793 VerSetConditionMask(
794 0, VER_MAJORVERSION, VER_GREATER_EQUAL),
795 VER_MINORVERSION, VER_GREATER_EQUAL),
796 VER_BUILDNUMBER, VER_GREATER_EQUAL);
797
798 osvi.dwMajorVersion = HIBYTE(_WIN32_WINNT_WIN10);
799 osvi.dwMinorVersion = LOBYTE(_WIN32_WINNT_WIN10);
800 osvi.dwBuildNumber = 16299; // RS3 (Redstone 3)
801
802 return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, cond_mask) != 0;
803 }
804
805 /**
806 * Shortens the default Windows socket
807 * connect timeout. Recommended for usage
808 * on the loopback adapter only.
809 */
810 JNIEXPORT jint JNICALL
811 NET_EnableFastTcpLoopbackConnect(int fd) {
812 TCP_INITIAL_RTO_PARAMETERS rto = {
813 TCP_INITIAL_RTO_UNSPECIFIED_RTT, // Use the default or overriden by the Administrator
814 1 // Minimum possible value before Windows 10 RS3
815 };
816
817 /**
818 * In Windows 10 RS3+ we can use the no retransmissions flag to
819 * completely remove the timeout delay, which is fixed to 500ms
820 * if Windows receives RST when the destination port is not open.
821 */
822 if (IsWindows10RS3OrGreater()) {
823 rto.MaxSynRetransmissions = TCP_INITIAL_RTO_NO_SYN_RETRANSMISSIONS;
824 }
825
826 DWORD result_byte_count = -1;
827 int result = WSAIoctl(fd, // descriptor identifying a socket
828 SIO_TCP_INITIAL_RTO, // dwIoControlCode
829 &rto, // pointer to TCP_INITIAL_RTO_PARAMETERS structure
830 sizeof(rto), // size, in bytes, of the input buffer
831 NULL, // pointer to output buffer
832 0, // size of output buffer
833 &result_byte_count, // number of bytes returned
834 NULL, // OVERLAPPED structure
835 NULL); // completion routine
836 return (result == SOCKET_ERROR) ? WSAGetLastError() : 0;
837 }
838
839 /**
840 * See net_util.h for documentation
841 */
842 JNIEXPORT int JNICALL
843 NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port,
844 SOCKETADDRESS *sa, int *len,
845 jboolean v4MappedAddress)
846 {
847 jint family = getInetAddress_family(env, iaObj);
848 JNU_CHECK_EXCEPTION_RETURN(env, -1);
849 memset((char *)sa, 0, sizeof(SOCKETADDRESS));
850
851 if (ipv6_available() &&
852 !(family == java_net_InetAddress_IPv4 &&
853 v4MappedAddress == JNI_FALSE))
854 {
855 jbyte caddr[16];
856 jint address;
857 unsigned int scopeid = 0, cached_scope_id = 0;
858
|