327 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
328 "ICMP Port Unreachable");
329 if (packetBufferLen > MAX_BUFFER_LEN)
330 free(fullPacket);
331 return -1;
332 } else if (timeout) {
333 /* Adjust timeout */
334 jlong newTime = JVM_CurrentTimeMillis(env, 0);
335 timeout -= (jint)(newTime - prevTime);
336 if (timeout <= 0) {
337 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
338 "Receive timed out");
339 if (packetBufferLen > MAX_BUFFER_LEN)
340 free(fullPacket);
341 return -1;
342 }
343 prevTime = newTime;
344 }
345 retry = TRUE;
346 }
347 } while (retry);
348
349 port = (int) ntohs ((u_short) GET_PORT((SOCKETADDRESS *)&sa));
350
351 /* truncate the data if the packet's length is too small */
352 if (rv > packetBufferLen) {
353 rv = packetBufferLen;
354 }
355 if (rv < 0) {
356 if (WSAGetLastError() == WSAEMSGSIZE) {
357 /* it is because the buffer is too small. It's UDP, it's
358 * unreliable, it's all good. discard the rest of the
359 * data..
360 */
361 rv = packetBufferLen;
362 } else {
363 /* failure */
364 (*env)->SetIntField(env, dpObj, dp_lengthID, 0);
365 }
366 }
439 length = MAX_PACKET_LEN;
440 }
441 fullPacket = (char *)malloc(length);
442 if (!fullPacket) {
443 JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
444 return;
445 }
446 } else {
447 fullPacket = &(BUF[0]);
448 }
449
450 (*env)->GetByteArrayRegion(env, data, offset, length,
451 (jbyte *)fullPacket);
452 rv = sendto(fd, fullPacket, length, 0, sap, sa_len);
453 if (rv == SOCKET_ERROR) {
454 if (rv == -1) {
455 NET_ThrowNew(env, WSAGetLastError(), "Datagram send failed");
456 }
457 }
458
459 if (length > MAX_BUFFER_LEN) {
460 free(fullPacket);
461 }
462 }
463
464 /*
465 * Class: java_net_DualStackPlainDatagramSocketImpl
466 * Method: socketSetIntOption
467 * Signature: (III)V
468 */
469 JNIEXPORT void JNICALL
470 Java_java_net_DualStackPlainDatagramSocketImpl_socketSetIntOption
471 (JNIEnv *env, jclass clazz, jint fd, jint cmd, jint value)
472 {
473 int level = 0, opt = 0;
474
475 if (NET_MapSocketOption(cmd, &level, &opt) < 0) {
476 JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
477 return;
478 }
|
327 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
328 "ICMP Port Unreachable");
329 if (packetBufferLen > MAX_BUFFER_LEN)
330 free(fullPacket);
331 return -1;
332 } else if (timeout) {
333 /* Adjust timeout */
334 jlong newTime = JVM_CurrentTimeMillis(env, 0);
335 timeout -= (jint)(newTime - prevTime);
336 if (timeout <= 0) {
337 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
338 "Receive timed out");
339 if (packetBufferLen > MAX_BUFFER_LEN)
340 free(fullPacket);
341 return -1;
342 }
343 prevTime = newTime;
344 }
345 retry = TRUE;
346 }
347 if (!peek && rv > 0) {
348 JVM_callNetworkReadBytes(env, rv);
349 }
350 } while (retry);
351
352 port = (int) ntohs ((u_short) GET_PORT((SOCKETADDRESS *)&sa));
353
354 /* truncate the data if the packet's length is too small */
355 if (rv > packetBufferLen) {
356 rv = packetBufferLen;
357 }
358 if (rv < 0) {
359 if (WSAGetLastError() == WSAEMSGSIZE) {
360 /* it is because the buffer is too small. It's UDP, it's
361 * unreliable, it's all good. discard the rest of the
362 * data..
363 */
364 rv = packetBufferLen;
365 } else {
366 /* failure */
367 (*env)->SetIntField(env, dpObj, dp_lengthID, 0);
368 }
369 }
442 length = MAX_PACKET_LEN;
443 }
444 fullPacket = (char *)malloc(length);
445 if (!fullPacket) {
446 JNU_ThrowOutOfMemoryError(env, "Native heap allocation failed");
447 return;
448 }
449 } else {
450 fullPacket = &(BUF[0]);
451 }
452
453 (*env)->GetByteArrayRegion(env, data, offset, length,
454 (jbyte *)fullPacket);
455 rv = sendto(fd, fullPacket, length, 0, sap, sa_len);
456 if (rv == SOCKET_ERROR) {
457 if (rv == -1) {
458 NET_ThrowNew(env, WSAGetLastError(), "Datagram send failed");
459 }
460 }
461
462 if (rv > 0) {
463 JVM_callFileWriteBytes(env, rv);
464 }
465
466 if (length > MAX_BUFFER_LEN) {
467 free(fullPacket);
468 }
469 }
470
471 /*
472 * Class: java_net_DualStackPlainDatagramSocketImpl
473 * Method: socketSetIntOption
474 * Signature: (III)V
475 */
476 JNIEXPORT void JNICALL
477 Java_java_net_DualStackPlainDatagramSocketImpl_socketSetIntOption
478 (JNIEnv *env, jclass clazz, jint fd, jint cmd, jint value)
479 {
480 int level = 0, opt = 0;
481
482 if (NET_MapSocketOption(cmd, &level, &opt) < 0) {
483 JNU_ThrowByName(env, "java/net/SocketException", "Invalid option");
484 return;
485 }
|