PageRenderTime 34ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/windows/native/sun/nio/ch/DatagramChannelImpl.c

https://github.com/ikeji/openjdk7-jdk
C | 247 lines | 181 code | 22 blank | 44 comment | 41 complexity | e3c2afbc7fc99cbddd499cdefeb2ccc7 MD5 | raw file
  1. /*
  2. * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. #include "jni.h"
  26. #include "jni_util.h"
  27. #include "jvm.h"
  28. #include "jlong.h"
  29. #include <io.h>
  30. #include "sun_nio_ch_DatagramChannelImpl.h"
  31. #include "nio.h"
  32. #include "nio_util.h"
  33. #include "net_util.h"
  34. #include <winsock2.h>
  35. static jfieldID isa_addrID; /* address in java.net.InetSocketAddress */
  36. static jfieldID isa_portID; /* port in java.net.InetSocketAddress */
  37. static jfieldID dci_senderID; /* sender in sun.nio.ch.DatagramChannelImpl */
  38. static jfieldID dci_senderAddrID; /* sender InetAddress in sun.nio.ch.DatagramChannelImpl */
  39. static jfieldID dci_senderPortID; /* sender port in sun.nio.ch.DatagramChannelImpl */
  40. static jclass isa_class; /* java.net.InetSocketAddress */
  41. static jmethodID isa_ctorID; /* java.net.InetSocketAddress(InetAddress, int) */
  42. JNIEXPORT void JNICALL
  43. Java_sun_nio_ch_DatagramChannelImpl_initIDs(JNIEnv *env, jclass clazz)
  44. {
  45. clazz = (*env)->FindClass(env, "java/net/InetSocketAddress");
  46. isa_class = (*env)->NewGlobalRef(env, clazz);
  47. isa_ctorID = (*env)->GetMethodID(env, clazz, "<init>",
  48. "(Ljava/net/InetAddress;I)V");
  49. isa_addrID = (*env)->GetFieldID(env, clazz, "addr",
  50. "Ljava/net/InetAddress;");
  51. isa_portID = (*env)->GetFieldID(env, clazz, "port", "I");
  52. clazz = (*env)->FindClass(env, "sun/nio/ch/DatagramChannelImpl");
  53. dci_senderID = (*env)->GetFieldID(env, clazz, "sender",
  54. "Ljava/net/SocketAddress;");
  55. dci_senderAddrID = (*env)->GetFieldID(env, clazz,
  56. "cachedSenderInetAddress",
  57. "Ljava/net/InetAddress;");
  58. dci_senderPortID = (*env)->GetFieldID(env, clazz,
  59. "cachedSenderPort", "I");
  60. }
  61. /*
  62. * This function "purges" all outstanding ICMP port unreachable packets
  63. * outstanding on a socket and returns JNI_TRUE if any ICMP messages
  64. * have been purged. The rational for purging is to emulate normal BSD
  65. * behaviour whereby receiving a "connection reset" status resets the
  66. * socket.
  67. */
  68. jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd)
  69. {
  70. jboolean got_icmp = JNI_FALSE;
  71. char buf[1];
  72. fd_set tbl;
  73. struct timeval t = { 0, 0 };
  74. SOCKETADDRESS sa;
  75. int addrlen = sizeof(sa);
  76. /*
  77. * Peek at the queue to see if there is an ICMP port unreachable. If there
  78. * is then receive it.
  79. */
  80. FD_ZERO(&tbl);
  81. FD_SET((u_int)fd, &tbl);
  82. while(1) {
  83. if (select(/*ignored*/fd+1, &tbl, 0, 0, &t) <= 0) {
  84. break;
  85. }
  86. if (recvfrom(fd, buf, 1, MSG_PEEK,
  87. (struct sockaddr *)&sa, &addrlen) != SOCKET_ERROR) {
  88. break;
  89. }
  90. if (WSAGetLastError() != WSAECONNRESET) {
  91. /* some other error - we don't care here */
  92. break;
  93. }
  94. recvfrom(fd, buf, 1, 0, (struct sockaddr *)&sa, &addrlen);
  95. got_icmp = JNI_TRUE;
  96. }
  97. return got_icmp;
  98. }
  99. JNIEXPORT void JNICALL
  100. Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
  101. jobject fdo)
  102. {
  103. jint fd = fdval(env, fdo);
  104. int rv = 0;
  105. SOCKETADDRESS sa;
  106. int sa_len = sizeof(sa);
  107. memset(&sa, 0, sa_len);
  108. rv = connect((SOCKET)fd, (struct sockaddr *)&sa, sa_len);
  109. if (rv == SOCKET_ERROR) {
  110. handleSocketError(env, WSAGetLastError());
  111. } else {
  112. /* Disable WSAECONNRESET errors as socket is no longer connected */
  113. BOOL enable = FALSE;
  114. DWORD bytesReturned = 0;
  115. WSAIoctl((SOCKET)fd, SIO_UDP_CONNRESET, &enable, sizeof(enable),
  116. NULL, 0, &bytesReturned, NULL, NULL);
  117. }
  118. }
  119. JNIEXPORT jint JNICALL
  120. Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jobject this,
  121. jobject fdo, jlong address,
  122. jint len, jboolean connected)
  123. {
  124. jint fd = fdval(env, fdo);
  125. void *buf = (void *)jlong_to_ptr(address);
  126. SOCKETADDRESS sa;
  127. int sa_len = sizeof(sa);
  128. BOOL retry = FALSE;
  129. jint n;
  130. jobject senderAddr;
  131. do {
  132. retry = FALSE;
  133. n = recvfrom((SOCKET)fd,
  134. (char *)buf,
  135. len,
  136. 0,
  137. (struct sockaddr *)&sa,
  138. &sa_len);
  139. if (n == SOCKET_ERROR) {
  140. int theErr = (jint)WSAGetLastError();
  141. if (theErr == WSAEMSGSIZE) {
  142. /* Spec says the rest of the data will be discarded... */
  143. n = len;
  144. } else if (theErr == WSAECONNRESET) {
  145. purgeOutstandingICMP(env, this, fd);
  146. if (connected == JNI_FALSE) {
  147. retry = TRUE;
  148. } else {
  149. JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
  150. return IOS_THROWN;
  151. }
  152. } else if (theErr == WSAEWOULDBLOCK) {
  153. return IOS_UNAVAILABLE;
  154. } else return handleSocketError(env, theErr);
  155. }
  156. } while (retry);
  157. /*
  158. * If the source address and port match the cached address
  159. * and port in DatagramChannelImpl then we don't need to
  160. * create InetAddress and InetSocketAddress objects.
  161. */
  162. senderAddr = (*env)->GetObjectField(env, this, dci_senderAddrID);
  163. if (senderAddr != NULL) {
  164. if (!NET_SockaddrEqualsInetAddress(env, (struct sockaddr *)&sa,
  165. senderAddr)) {
  166. senderAddr = NULL;
  167. } else {
  168. jint port = (*env)->GetIntField(env, this, dci_senderPortID);
  169. if (port != NET_GetPortFromSockaddr((struct sockaddr *)&sa)) {
  170. senderAddr = NULL;
  171. }
  172. }
  173. }
  174. if (senderAddr == NULL) {
  175. jobject isa = NULL;
  176. int port;
  177. jobject ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa,
  178. &port);
  179. if (ia != NULL) {
  180. isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
  181. }
  182. if (isa == NULL) {
  183. JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
  184. return IOS_THROWN;
  185. }
  186. // update cachedSenderInetAddress/cachedSenderPort
  187. (*env)->SetObjectField(env, this, dci_senderAddrID, ia);
  188. (*env)->SetIntField(env, this, dci_senderPortID,
  189. NET_GetPortFromSockaddr((struct sockaddr *)&sa));
  190. (*env)->SetObjectField(env, this, dci_senderID, isa);
  191. }
  192. return n;
  193. }
  194. JNIEXPORT jint JNICALL
  195. Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jobject this,
  196. jboolean preferIPv6, jobject fdo,
  197. jlong address, jint len, jobject dest)
  198. {
  199. jint fd = fdval(env, fdo);
  200. void *buf = (void *)jlong_to_ptr(address);
  201. SOCKETADDRESS sa;
  202. int sa_len;
  203. jint rv = 0;
  204. jobject destAddress = (*env)->GetObjectField(env, dest, isa_addrID);
  205. jint destPort = (*env)->GetIntField(env, dest, isa_portID);
  206. if (NET_InetAddressToSockaddr(env, destAddress, destPort,
  207. (struct sockaddr *)&sa,
  208. &sa_len, preferIPv6) != 0) {
  209. return IOS_THROWN;
  210. }
  211. rv = sendto((SOCKET)fd,
  212. buf,
  213. len,
  214. 0,
  215. (struct sockaddr *)&sa,
  216. sa_len);
  217. if (rv == SOCKET_ERROR) {
  218. int theErr = (jint)WSAGetLastError();
  219. if (theErr == WSAEWOULDBLOCK) {
  220. return IOS_UNAVAILABLE;
  221. }
  222. return handleSocketError(env, (jint)WSAGetLastError());
  223. }
  224. return rv;
  225. }