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

Print this page




  28 #include <ws2tcpip.h>
  29 #include <ctype.h>
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <malloc.h>
  33 #include <sys/types.h>
  34 
  35 #ifndef IPTOS_TOS_MASK
  36 #define IPTOS_TOS_MASK 0x1e
  37 #endif
  38 #ifndef IPTOS_PREC_MASK
  39 #define IPTOS_PREC_MASK 0xe0
  40 #endif
  41 
  42 #include "java_net_TwoStacksPlainDatagramSocketImpl.h"
  43 #include "java_net_SocketOptions.h"
  44 #include "java_net_NetworkInterface.h"
  45 
  46 #include "NetworkInterface.h"
  47 #include "jvm.h"

  48 #include "jni_util.h"
  49 #include "net_util.h"
  50 
  51 #define IN_CLASSD(i)    (((long)(i) & 0xf0000000) == 0xe0000000)
  52 #define IN_MULTICAST(i) IN_CLASSD(i)
  53 
  54 /************************************************************************
  55  * TwoStacksPlainDatagramSocketImpl
  56  */
  57 
  58 static jfieldID IO_fd_fdID;
  59 static jfieldID pdsi_trafficClassID;
  60 jfieldID pdsi_fdID;
  61 jfieldID pdsi_fd1ID;
  62 jfieldID pdsi_fduseID;
  63 jfieldID pdsi_lastfdID;
  64 jfieldID pdsi_timeoutID;
  65 
  66 jfieldID pdsi_localPortID;
  67 jfieldID pdsi_connected;


2151         (*env)->SetObjectField(env, ni, ni_addrsID, addrArray);
2152         return ni;
2153     }
2154     return NULL;
2155 }
2156 
2157 
2158 /*
2159  * Returns relevant info as a jint.
2160  *
2161  * Class:     java_net_TwoStacksPlainDatagramSocketImpl
2162  * Method:    socketGetOption
2163  * Signature: (I)Ljava/lang/Object;
2164  */
2165 JNIEXPORT jobject JNICALL
2166 Java_java_net_TwoStacksPlainDatagramSocketImpl_socketGetOption(JNIEnv *env, jobject this,
2167                                                       jint opt) {
2168 
2169     int fd=-1, fd1=-1;
2170     int level, optname, optlen;

2171     union {
2172         int i;
2173     } optval = {0};
2174     int ipv6_supported = ipv6_available();
2175 
2176     fd = getFD(env, this);
2177     if (ipv6_supported) {
2178         fd1 = getFD1(env, this);
2179     }
2180 
2181     if (fd < 0 && fd1 < 0) {
2182         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
2183                         "Socket closed");
2184         return NULL;
2185     }
2186 
2187     /*
2188      * Handle IP_MULTICAST_IF separately
2189      */
2190     if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||


2194 
2195     /*
2196      * Map the Java level socket option to the platform specific
2197      * level and option name.
2198      */
2199     if (NET_MapSocketOption(opt, &level, &optname)) {
2200         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
2201         return NULL;
2202     }
2203 
2204     if (fd == -1) {
2205         if (NET_MapSocketOptionV6(opt, &level, &optname)) {
2206             JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
2207             return NULL;
2208         }
2209         fd = fd1; /* must be IPv6 only */
2210     }
2211 
2212     optlen = sizeof(optval.i);
2213     if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
2214         char errmsg[255];
2215         sprintf(errmsg, "error getting socket option: %s\n", strerror(errno));


2216         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg);
2217         return NULL;
2218     }
2219 
2220     switch (opt) {
2221         case java_net_SocketOptions_SO_BROADCAST:
2222         case java_net_SocketOptions_SO_REUSEADDR:
2223             return createBoolean(env, optval.i);
2224 
2225         case java_net_SocketOptions_IP_MULTICAST_LOOP:
2226             /* getLoopbackMode() returns true if IP_MULTICAST_LOOP is disabled */
2227             return createBoolean(env, !optval.i);
2228 
2229         case java_net_SocketOptions_SO_SNDBUF:
2230         case java_net_SocketOptions_SO_RCVBUF:
2231         case java_net_SocketOptions_IP_TOS:
2232             return createInteger(env, optval.i);
2233 
2234         default :
2235             JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",




  28 #include <ws2tcpip.h>
  29 #include <ctype.h>
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <malloc.h>
  33 #include <sys/types.h>
  34 
  35 #ifndef IPTOS_TOS_MASK
  36 #define IPTOS_TOS_MASK 0x1e
  37 #endif
  38 #ifndef IPTOS_PREC_MASK
  39 #define IPTOS_PREC_MASK 0xe0
  40 #endif
  41 
  42 #include "java_net_TwoStacksPlainDatagramSocketImpl.h"
  43 #include "java_net_SocketOptions.h"
  44 #include "java_net_NetworkInterface.h"
  45 
  46 #include "NetworkInterface.h"
  47 #include "jvm.h"
  48 #include "jdk_strerror.h"
  49 #include "jni_util.h"
  50 #include "net_util.h"
  51 
  52 #define IN_CLASSD(i)    (((long)(i) & 0xf0000000) == 0xe0000000)
  53 #define IN_MULTICAST(i) IN_CLASSD(i)
  54 
  55 /************************************************************************
  56  * TwoStacksPlainDatagramSocketImpl
  57  */
  58 
  59 static jfieldID IO_fd_fdID;
  60 static jfieldID pdsi_trafficClassID;
  61 jfieldID pdsi_fdID;
  62 jfieldID pdsi_fd1ID;
  63 jfieldID pdsi_fduseID;
  64 jfieldID pdsi_lastfdID;
  65 jfieldID pdsi_timeoutID;
  66 
  67 jfieldID pdsi_localPortID;
  68 jfieldID pdsi_connected;


2152         (*env)->SetObjectField(env, ni, ni_addrsID, addrArray);
2153         return ni;
2154     }
2155     return NULL;
2156 }
2157 
2158 
2159 /*
2160  * Returns relevant info as a jint.
2161  *
2162  * Class:     java_net_TwoStacksPlainDatagramSocketImpl
2163  * Method:    socketGetOption
2164  * Signature: (I)Ljava/lang/Object;
2165  */
2166 JNIEXPORT jobject JNICALL
2167 Java_java_net_TwoStacksPlainDatagramSocketImpl_socketGetOption(JNIEnv *env, jobject this,
2168                                                       jint opt) {
2169 
2170     int fd=-1, fd1=-1;
2171     int level, optname, optlen;
2172     char buf[1024];
2173     union {
2174         int i;
2175     } optval = {0};
2176     int ipv6_supported = ipv6_available();
2177 
2178     fd = getFD(env, this);
2179     if (ipv6_supported) {
2180         fd1 = getFD1(env, this);
2181     }
2182 
2183     if (fd < 0 && fd1 < 0) {
2184         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
2185                         "Socket closed");
2186         return NULL;
2187     }
2188 
2189     /*
2190      * Handle IP_MULTICAST_IF separately
2191      */
2192     if (opt == java_net_SocketOptions_IP_MULTICAST_IF ||


2196 
2197     /*
2198      * Map the Java level socket option to the platform specific
2199      * level and option name.
2200      */
2201     if (NET_MapSocketOption(opt, &level, &optname)) {
2202         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
2203         return NULL;
2204     }
2205 
2206     if (fd == -1) {
2207         if (NET_MapSocketOptionV6(opt, &level, &optname)) {
2208             JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Invalid option");
2209             return NULL;
2210         }
2211         fd = fd1; /* must be IPv6 only */
2212     }
2213 
2214     optlen = sizeof(optval.i);
2215     if (NET_GetSockOpt(fd, level, optname, (void *)&optval, &optlen) < 0) {
2216         int size = 0;
2217         char errmsg[300];
2218         jdk_strerror(errno, buf, (size_t) 255);
2219         sprintf(errmsg, "error getting socket option: %s\n", buf);
2220         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", errmsg);
2221         return NULL;
2222     }
2223 
2224     switch (opt) {
2225         case java_net_SocketOptions_SO_BROADCAST:
2226         case java_net_SocketOptions_SO_REUSEADDR:
2227             return createBoolean(env, optval.i);
2228 
2229         case java_net_SocketOptions_IP_MULTICAST_LOOP:
2230             /* getLoopbackMode() returns true if IP_MULTICAST_LOOP is disabled */
2231             return createBoolean(env, !optval.i);
2232 
2233         case java_net_SocketOptions_SO_SNDBUF:
2234         case java_net_SocketOptions_SO_RCVBUF:
2235         case java_net_SocketOptions_IP_TOS:
2236             return createInteger(env, optval.i);
2237 
2238         default :
2239             JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",