< prev index next >

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

Print this page




 873                 /* stuff the new Inetaddress in the packet */
 874                 (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
 875             } else {
 876                 /* only get the new port number */
 877                 port = NET_GetPortFromSockaddr(&rmtaddr);
 878             }
 879             /* and fill in the data, remote address/port and such */
 880             (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
 881                                        (jbyte *)fullPacket);
 882             (*env)->SetIntField(env, packet, dp_portID, port);
 883             (*env)->SetIntField(env, packet, dp_lengthID, n);
 884         }
 885 
 886     } while (retry);
 887 
 888     if (mallocedPacket) {
 889         free(fullPacket);
 890     }
 891 }
 892 








 893 /*
 894  * Class:     java_net_PlainDatagramSocketImpl
 895  * Method:    datagramSocketCreate
 896  * Signature: ()V
 897  */
 898 JNIEXPORT void JNICALL
 899 Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
 900                                                            jobject this) {
 901     jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
 902     int arg, fd, t = 1;
 903     char tmpbuf[1024];
 904     int domain = ipv6_available() ? AF_INET6 : AF_INET;
 905 
 906     if (IS_NULL(fdObj)) {
 907         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 908                         "Socket closed");
 909         return;
 910     }
 911 
 912     if ((fd = socket(domain, SOCK_DGRAM, 0)) == -1) {
 913         JNU_ThrowByNameWithMessageAndLastError
 914             (env, JNU_JAVANETPKG "SocketException", "Error creating socket");
 915         return;
 916     }
 917 
 918     /*
 919      * If IPv4 is available, disable IPV6_V6ONLY to ensure dual-socket support.
 920      */
 921     if (domain == AF_INET6 && ipv4_available()) {
 922         arg = 0;
 923         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
 924                        sizeof(int)) < 0) {
 925             NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
 926             close(fd);
 927             return;
 928         }
 929     }
 930 
 931 #ifdef __APPLE__
 932     arg = 65507;




 873                 /* stuff the new Inetaddress in the packet */
 874                 (*env)->SetObjectField(env, packet, dp_addressID, packetAddress);
 875             } else {
 876                 /* only get the new port number */
 877                 port = NET_GetPortFromSockaddr(&rmtaddr);
 878             }
 879             /* and fill in the data, remote address/port and such */
 880             (*env)->SetByteArrayRegion(env, packetBuffer, packetBufferOffset, n,
 881                                        (jbyte *)fullPacket);
 882             (*env)->SetIntField(env, packet, dp_portID, port);
 883             (*env)->SetIntField(env, packet, dp_lengthID, n);
 884         }
 885 
 886     } while (retry);
 887 
 888     if (mallocedPacket) {
 889         free(fullPacket);
 890     }
 891 }
 892 
 893 static int socket0(int domain, int type, int protocol) {
 894     if (domain == AF_INET) {
 895         errno = EAFNOSUPPORT;
 896         return -1;
 897     }
 898     return socket(domain, type, protocol);
 899 }
 900 
 901 /*
 902  * Class:     java_net_PlainDatagramSocketImpl
 903  * Method:    datagramSocketCreate
 904  * Signature: ()V
 905  */
 906 JNIEXPORT void JNICALL
 907 Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
 908                                                            jobject this) {
 909     jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
 910     int arg, fd, t = 1;
 911     char tmpbuf[1024];
 912     int domain = ipv6_available() ? AF_INET6 : AF_INET;
 913 
 914     if (IS_NULL(fdObj)) {
 915         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 916                         "Socket closed");
 917         return;
 918     }
 919 
 920     if ((fd = socket0(domain, SOCK_DGRAM, 0)) == -1) {
 921         JNU_ThrowByNameWithMessageAndLastError
 922             (env, JNU_JAVANETPKG "SocketException", "Error creating socket");
 923         return;
 924     }
 925 
 926     /*
 927      * If IPv4 is available, disable IPV6_V6ONLY to ensure dual-socket support.
 928      */
 929     if (domain == AF_INET6 && ipv4_available()) {
 930         arg = 0;
 931         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
 932                        sizeof(int)) < 0) {
 933             NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
 934             close(fd);
 935             return;
 936         }
 937     }
 938 
 939 #ifdef __APPLE__
 940     arg = 65507;


< prev index next >