< prev index next >

src/java.base/windows/native/libnet/net_util_md.c

Print this page




 735             bound_port = GET_PORT (&oaddr);
 736             SET_PORT (b->addr, bound_port);
 737             rv = NET_WinBind(fd, b->addr, sizeof(SOCKETADDRESS), exclBind);
 738 
 739             if (rv != SOCKET_ERROR) {
 740                 if (family == AF_INET) {
 741                     b->ipv4_fd = fd;
 742                     b->ipv6_fd = ofd;
 743                 } else {
 744                     b->ipv4_fd = ofd;
 745                     b->ipv6_fd = fd;
 746                 }
 747                 return 0;
 748             }
 749         }
 750         CLOSE_SOCKETS_AND_RETURN;
 751     }
 752     return 0;
 753 }
 754 































 755 /**
 756  * Enables SIO_LOOPBACK_FAST_PATH
 757  */
 758 JNIEXPORT jint JNICALL
 759 NET_EnableFastTcpLoopback(int fd) {
 760     int enabled = 1;
 761     DWORD result_byte_count = -1;
 762     int result = WSAIoctl(fd,
 763                           SIO_LOOPBACK_FAST_PATH,
 764                           &enabled,
 765                           sizeof(enabled),
 766                           NULL,
 767                           0,
 768                           &result_byte_count,
 769                           NULL,
 770                           NULL);
 771     return result == SOCKET_ERROR ? WSAGetLastError() : 0;
 772 }
 773 
 774 /**
 775  * See net_util.h for documentation
 776  */
 777 JNIEXPORT int JNICALL
 778 NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port,
 779                           SOCKETADDRESS *sa, int *len,
 780                           jboolean v4MappedAddress)
 781 {
 782     jint family = getInetAddress_family(env, iaObj);
 783     JNU_CHECK_EXCEPTION_RETURN(env, -1);
 784     memset((char *)sa, 0, sizeof(SOCKETADDRESS));
 785 
 786     if (ipv6_available() &&
 787         !(family == java_net_InetAddress_IPv4 &&
 788           v4MappedAddress == JNI_FALSE))
 789     {
 790         jbyte caddr[16];
 791         jint address;
 792         unsigned int scopeid = 0;
 793 
 794         if (family == java_net_InetAddress_IPv4) {
 795             // convert to IPv4-mapped address
 796             memset((char *)caddr, 0, 16);
 797             address = getInetAddress_addr(env, iaObj);
 798             JNU_CHECK_EXCEPTION_RETURN(env, -1);
 799             if (address == INADDR_ANY) {
 800                 /* we would always prefer IPv6 wildcard address
 801                  * caddr[10] = 0xff;
 802                  * caddr[11] = 0xff; */
 803             } else {
 804                 caddr[10] = 0xff;
 805                 caddr[11] = 0xff;
 806                 caddr[12] = ((address >> 24) & 0xff);
 807                 caddr[13] = ((address >> 16) & 0xff);
 808                 caddr[14] = ((address >> 8) & 0xff);
 809                 caddr[15] = (address & 0xff);
 810             }
 811         } else {
 812             getInet6Address_ipaddress(env, iaObj, (char *)caddr);
 813             scopeid = getInet6Address_scopeid(env, iaObj);

 814         }
 815         sa->sa6.sin6_port = (u_short)htons((u_short)port);
 816         memcpy((void *)&sa->sa6.sin6_addr, caddr, sizeof(struct in6_addr));
 817         sa->sa6.sin6_family = AF_INET6;
 818         sa->sa6.sin6_scope_id = scopeid;







 819         if (len != NULL) {
 820             *len = sizeof(struct sockaddr_in6);
 821         }
 822     } else {
 823         jint address;
 824         if (family != java_net_InetAddress_IPv4) {
 825             JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
 826             return -1;
 827         }
 828         address = getInetAddress_addr(env, iaObj);
 829         JNU_CHECK_EXCEPTION_RETURN(env, -1);
 830         sa->sa4.sin_port = htons((short)port);
 831         sa->sa4.sin_addr.s_addr = (u_long)htonl(address);
 832         sa->sa4.sin_family = AF_INET;
 833         if (len != NULL) {
 834             *len = sizeof(struct sockaddr_in);
 835         }
 836     }
 837     return 0;
 838 }




 735             bound_port = GET_PORT (&oaddr);
 736             SET_PORT (b->addr, bound_port);
 737             rv = NET_WinBind(fd, b->addr, sizeof(SOCKETADDRESS), exclBind);
 738 
 739             if (rv != SOCKET_ERROR) {
 740                 if (family == AF_INET) {
 741                     b->ipv4_fd = fd;
 742                     b->ipv6_fd = ofd;
 743                 } else {
 744                     b->ipv4_fd = ofd;
 745                     b->ipv6_fd = fd;
 746                 }
 747                 return 0;
 748             }
 749         }
 750         CLOSE_SOCKETS_AND_RETURN;
 751     }
 752     return 0;
 753 }
 754 
 755 /*
 756  * Determine the default interface for an IPv6 address.
 757  *
 758  * Returns :-
 759  *      0 if error
 760  *      > 0 interface index to use
 761  */
 762 jint getDefaultIPv6Interface(JNIEnv *env, struct sockaddr_in6 *target_addr)
 763 {
 764     int ret;
 765     DWORD b;
 766     struct sockaddr_in6 route;
 767     SOCKET fd = socket(AF_INET6, SOCK_STREAM, 0);
 768     if (fd == INVALID_SOCKET) {
 769         return 0;
 770     }
 771 
 772     ret = WSAIoctl(fd, SIO_ROUTING_INTERFACE_QUERY,
 773                    (void *)target_addr, sizeof(struct sockaddr_in6),
 774                    (void *)&route, sizeof(struct sockaddr_in6),
 775                    &b, 0, 0);
 776     if (ret == SOCKET_ERROR) {
 777         // error
 778         closesocket(fd);
 779         return 0;
 780     } else {
 781         closesocket(fd);
 782         return route.sin6_scope_id;
 783     }
 784 }
 785 
 786 /**
 787  * Enables SIO_LOOPBACK_FAST_PATH
 788  */
 789 JNIEXPORT jint JNICALL
 790 NET_EnableFastTcpLoopback(int fd) {
 791     int enabled = 1;
 792     DWORD result_byte_count = -1;
 793     int result = WSAIoctl(fd,
 794                           SIO_LOOPBACK_FAST_PATH,
 795                           &enabled,
 796                           sizeof(enabled),
 797                           NULL,
 798                           0,
 799                           &result_byte_count,
 800                           NULL,
 801                           NULL);
 802     return result == SOCKET_ERROR ? WSAGetLastError() : 0;
 803 }
 804 
 805 /**
 806  * See net_util.h for documentation
 807  */
 808 JNIEXPORT int JNICALL
 809 NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port,
 810                           SOCKETADDRESS *sa, int *len,
 811                           jboolean v4MappedAddress)
 812 {
 813     jint family = getInetAddress_family(env, iaObj);
 814     JNU_CHECK_EXCEPTION_RETURN(env, -1);
 815     memset((char *)sa, 0, sizeof(SOCKETADDRESS));
 816 
 817     if (ipv6_available() &&
 818         !(family == java_net_InetAddress_IPv4 &&
 819           v4MappedAddress == JNI_FALSE))
 820     {
 821         jbyte caddr[16];
 822         jint address;
 823         unsigned int scopeid = 0, cached_scope_id = 0;
 824 
 825         if (family == java_net_InetAddress_IPv4) {
 826             // convert to IPv4-mapped address
 827             memset((char *)caddr, 0, 16);
 828             address = getInetAddress_addr(env, iaObj);
 829             JNU_CHECK_EXCEPTION_RETURN(env, -1);
 830             if (address == INADDR_ANY) {
 831                 /* we would always prefer IPv6 wildcard address
 832                  * caddr[10] = 0xff;
 833                  * caddr[11] = 0xff; */
 834             } else {
 835                 caddr[10] = 0xff;
 836                 caddr[11] = 0xff;
 837                 caddr[12] = ((address >> 24) & 0xff);
 838                 caddr[13] = ((address >> 16) & 0xff);
 839                 caddr[14] = ((address >> 8) & 0xff);
 840                 caddr[15] = (address & 0xff);
 841             }
 842         } else {
 843             getInet6Address_ipaddress(env, iaObj, (char *)caddr);
 844             scopeid = getInet6Address_scopeid(env, iaObj);
 845             cached_scope_id = (unsigned int)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID);
 846         }
 847         sa->sa6.sin6_port = (u_short)htons((u_short)port);
 848         memcpy((void *)&sa->sa6.sin6_addr, caddr, sizeof(struct in6_addr));
 849         sa->sa6.sin6_family = AF_INET6;
 850         if ((family == java_net_InetAddress_IPv6) &&
 851             IN6_IS_ADDR_LINKLOCAL(&sa->sa6.sin6_addr) &&
 852             (!scopeid && !cached_scope_id))
 853         {
 854             cached_scope_id = getDefaultIPv6Interface(env, &sa->sa6);
 855             (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
 856         }
 857         sa->sa6.sin6_scope_id = scopeid == 0 ? cached_scope_id : scopeid;
 858         if (len != NULL) {
 859             *len = sizeof(struct sockaddr_in6);
 860         }
 861     } else {
 862         jint address;
 863         if (family != java_net_InetAddress_IPv4) {
 864             JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
 865             return -1;
 866         }
 867         address = getInetAddress_addr(env, iaObj);
 868         JNU_CHECK_EXCEPTION_RETURN(env, -1);
 869         sa->sa4.sin_port = htons((short)port);
 870         sa->sa4.sin_addr.s_addr = (u_long)htonl(address);
 871         sa->sa4.sin_family = AF_INET;
 872         if (len != NULL) {
 873             *len = sizeof(struct sockaddr_in);
 874         }
 875     }
 876     return 0;
 877 }


< prev index next >