src/solaris/native/java/net/PlainDatagramSocketImpl.c

Print this page




 937             (*env)->SetIntField(env, packet, dp_lengthID, n);
 938         }
 939 
 940     } while (retry);
 941 
 942     if (mallocedPacket) {
 943         free(fullPacket);
 944     }
 945 }
 946 
 947 /*
 948  * Class:     java_net_PlainDatagramSocketImpl
 949  * Method:    datagramSocketCreate
 950  * Signature: ()V
 951  */
 952 JNIEXPORT void JNICALL
 953 Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
 954                                                            jobject this) {
 955     jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
 956     int arg, fd, t = 1;

 957 #ifdef AF_INET6
 958     int domain = ipv6_available() ? AF_INET6 : AF_INET;
 959 #else
 960     int domain = AF_INET;
 961 #endif
 962 
 963     if (IS_NULL(fdObj)) {
 964         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 965                         "Socket closed");
 966         return;
 967     }
 968 
 969     if ((fd = JVM_Socket(domain, SOCK_DGRAM, 0)) == JVM_IO_ERR) {
 970         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
 971                        "Error creating socket");
 972         return;
 973     }
 974 
 975 #ifdef AF_INET6
 976     /* Disable IPV6_V6ONLY to ensure dual-socket support */
 977     if (domain == AF_INET6) {
 978         arg = 0;
 979         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
 980                        sizeof(int)) < 0) {
 981             NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
 982             close(fd);
 983             return;
 984         }
 985     }
 986 #endif /* AF_INET6 */
 987 
 988 #ifdef __APPLE__
 989     arg = 65507;
 990     if (JVM_SetSockOpt(fd, SOL_SOCKET, SO_SNDBUF,
 991                        (char *)&arg, sizeof(arg)) < 0) {
 992         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 993                         strerror(errno));
 994         return;
 995     }
 996     if (JVM_SetSockOpt(fd, SOL_SOCKET, SO_RCVBUF,
 997                        (char *)&arg, sizeof(arg)) < 0) {
 998         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 999                         strerror(errno));
1000         return;
1001     }
1002 #endif /* __APPLE__ */
1003 
1004      setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*) &t, sizeof(int));
1005 
1006 #if defined(__linux__)
1007      arg = 0;
1008      int level = (domain == AF_INET6) ? IPPROTO_IPV6 : IPPROTO_IP;
1009      if ((setsockopt(fd, level, IP_MULTICAST_ALL, (char*)&arg, sizeof(arg)) < 0) &&
1010          (errno != ENOPROTOOPT)) {
1011          JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
1012                          strerror(errno));

1013          close(fd);
1014          return;
1015      }
1016 #endif
1017 
1018 #if defined (__linux__) && defined (AF_INET6)
1019     /*
1020      * On Linux for IPv6 sockets we must set the hop limit
1021      * to 1 to be compatible with default TTL of 1 for IPv4 sockets.
1022      */
1023     if (domain == AF_INET6) {
1024         int ttl = 1;
1025         setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&ttl,
1026                    sizeof(ttl));
1027     }
1028 #endif /* __linux__ */
1029 
1030     (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
1031 }
1032 




 937             (*env)->SetIntField(env, packet, dp_lengthID, n);
 938         }
 939 
 940     } while (retry);
 941 
 942     if (mallocedPacket) {
 943         free(fullPacket);
 944     }
 945 }
 946 
 947 /*
 948  * Class:     java_net_PlainDatagramSocketImpl
 949  * Method:    datagramSocketCreate
 950  * Signature: ()V
 951  */
 952 JNIEXPORT void JNICALL
 953 Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(JNIEnv *env,
 954                                                            jobject this) {
 955     jobject fdObj = (*env)->GetObjectField(env, this, pdsi_fdID);
 956     int arg, fd, t = 1;
 957     char tmpbuf[1024];
 958 #ifdef AF_INET6
 959     int domain = ipv6_available() ? AF_INET6 : AF_INET;
 960 #else
 961     int domain = AF_INET;
 962 #endif
 963 
 964     if (IS_NULL(fdObj)) {
 965         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
 966                         "Socket closed");
 967         return;
 968     }
 969 
 970     if ((fd = JVM_Socket(domain, SOCK_DGRAM, 0)) == JVM_IO_ERR) {
 971         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
 972                        "Error creating socket");
 973         return;
 974     }
 975 
 976 #ifdef AF_INET6
 977     /* Disable IPV6_V6ONLY to ensure dual-socket support */
 978     if (domain == AF_INET6) {
 979         arg = 0;
 980         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
 981                        sizeof(int)) < 0) {
 982             NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
 983             close(fd);
 984             return;
 985         }
 986     }
 987 #endif /* AF_INET6 */
 988 
 989 #ifdef __APPLE__
 990     arg = 65507;
 991     if (JVM_SetSockOpt(fd, SOL_SOCKET, SO_SNDBUF,
 992                        (char *)&arg, sizeof(arg)) < 0) {
 993         getErrorString(errno, tmpbuf, sizeof(tmpbuf));
 994         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf);
 995         return;
 996     }
 997     if (JVM_SetSockOpt(fd, SOL_SOCKET, SO_RCVBUF,
 998                        (char *)&arg, sizeof(arg)) < 0) {
 999         getErrorString(errno, tmpbuf, sizeof(tmpbuf));
1000         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf);
1001         return;
1002     }
1003 #endif /* __APPLE__ */
1004 
1005      setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*) &t, sizeof(int));
1006 
1007 #if defined(__linux__)
1008      arg = 0;
1009      int level = (domain == AF_INET6) ? IPPROTO_IPV6 : IPPROTO_IP;
1010      if ((setsockopt(fd, level, IP_MULTICAST_ALL, (char*)&arg, sizeof(arg)) < 0) &&
1011            (errno != ENOPROTOOPT))
1012     {
1013         getErrorString(errno, tmpbuf, sizeof(tmpbuf));
1014         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", tmpbuf);
1015          close(fd);
1016          return;
1017      }
1018 #endif
1019 
1020 #if defined (__linux__) && defined (AF_INET6)
1021     /*
1022      * On Linux for IPv6 sockets we must set the hop limit
1023      * to 1 to be compatible with default TTL of 1 for IPv4 sockets.
1024      */
1025     if (domain == AF_INET6) {
1026         int ttl = 1;
1027         setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (char *)&ttl,
1028                    sizeof(ttl));
1029     }
1030 #endif /* __linux__ */
1031 
1032     (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
1033 }
1034