1 /*
2 * Copyright (c) 2002, 2012, 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
26 /*
27 */
28
29 #include "jni.h"
30 #include "jni_util.h"
31 #include "jvm.h"
32 #include "jlong.h"
33 #include "sun_nio_ch_DatagramDispatcher.h"
34 #include <sys/types.h>
35 #include <sys/uio.h>
36 #include <sys/socket.h>
37 #include <string.h>
38
39 #include "nio_util.h"
40 #include <limits.h>
41
42 JNIEXPORT jint JNICALL
43 Java_sun_nio_ch_DatagramDispatcher_read0(JNIEnv *env, jclass clazz,
44 jobject fdo, jlong address, jint len)
45 {
46 jint fd = fdval(env, fdo);
47 void *buf = (void *)jlong_to_ptr(address);
48 int result = recv(fd, buf, len, 0);
49 if (result < 0 && errno == ECONNREFUSED) {
50 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
51 return -2;
52 }
53 return convertReturnVal(env, result, JNI_TRUE);
54 }
55
56
57 JNIEXPORT jlong JNICALL
58 Java_sun_nio_ch_DatagramDispatcher_readv0(JNIEnv *env, jclass clazz,
59 jobject fdo, jlong address, jint len)
60 {
61 jint fd = fdval(env, fdo);
62 ssize_t result = 0;
63 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
64 struct msghdr m;
65 if (len > IOV_MAX) {
66 len = IOV_MAX;
67 }
68
69 // initialize the message
70 memset(&m, 0, sizeof(m));
71 m.msg_iov = iov;
72 m.msg_iovlen = len;
73
74 result = recvmsg(fd, &m, 0);
75 if (result < 0 && errno == ECONNREFUSED) {
76 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
77 return -2;
78 }
79 return convertLongReturnVal(env, (jlong)result, JNI_TRUE);
80 }
81
82 JNIEXPORT jint JNICALL
83 Java_sun_nio_ch_DatagramDispatcher_write0(JNIEnv *env, jclass clazz,
84 jobject fdo, jlong address, jint len)
85 {
86 jint fd = fdval(env, fdo);
87 void *buf = (void *)jlong_to_ptr(address);
88 int result = send(fd, buf, len, 0);
89 if (result < 0 && errno == ECONNREFUSED) {
90 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
91 return -2;
92 }
93 return convertReturnVal(env, result, JNI_FALSE);
94 }
95
96 JNIEXPORT jlong JNICALL
97 Java_sun_nio_ch_DatagramDispatcher_writev0(JNIEnv *env, jclass clazz,
98 jobject fdo, jlong address, jint len)
99 {
100 jint fd = fdval(env, fdo);
101 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
102 struct msghdr m;
103 ssize_t result = 0;
104 if (len > IOV_MAX) {
105 len = IOV_MAX;
106 }
107
108 // initialize the message
109 memset(&m, 0, sizeof(m));
110 m.msg_iov = iov;
111 m.msg_iovlen = len;
112
113 result = sendmsg(fd, &m, 0);
114 if (result < 0 && errno == ECONNREFUSED) {
115 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
116 return -2;
117 }
118 return convertLongReturnVal(env, (jlong)result, JNI_FALSE);
119 }
|
1 /*
2 * Copyright (c) 2002, 2020, 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
26 #include <sys/types.h>
27 #include <sys/uio.h>
28 #include <sys/socket.h>
29 #include <string.h>
30 #include <limits.h>
31
32 #include "jni.h"
33 #include "jni_util.h"
34 #include "jvm.h"
35 #include "jlong.h"
36 #include "nio.h"
37 #include "nio_util.h"
38 #include "sun_nio_ch_DatagramDispatcher.h"
39
40 JNIEXPORT jint JNICALL
41 Java_sun_nio_ch_DatagramDispatcher_read0(JNIEnv *env, jclass clazz,
42 jobject fdo, jlong address, jint len)
43 {
44 jint fd = fdval(env, fdo);
45 void *buf = (void *)jlong_to_ptr(address);
46 int result = recv(fd, buf, len, 0);
47 if (result < 0 && errno == ECONNREFUSED) {
48 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
49 return IOS_THROWN;
50 }
51 return convertReturnVal(env, result, JNI_TRUE);
52 }
53
54
55 JNIEXPORT jlong JNICALL
56 Java_sun_nio_ch_DatagramDispatcher_readv0(JNIEnv *env, jclass clazz,
57 jobject fdo, jlong address, jint len)
58 {
59 jint fd = fdval(env, fdo);
60 ssize_t result = 0;
61 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
62 struct msghdr m;
63 if (len > IOV_MAX) {
64 len = IOV_MAX;
65 }
66
67 // initialize the message
68 memset(&m, 0, sizeof(m));
69 m.msg_iov = iov;
70 m.msg_iovlen = len;
71
72 result = recvmsg(fd, &m, 0);
73 if (result < 0 && errno == ECONNREFUSED) {
74 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
75 return IOS_THROWN;
76 }
77 return convertLongReturnVal(env, (jlong)result, JNI_TRUE);
78 }
79
80 JNIEXPORT jint JNICALL
81 Java_sun_nio_ch_DatagramDispatcher_write0(JNIEnv *env, jclass clazz,
82 jobject fdo, jlong address, jint len)
83 {
84 jint fd = fdval(env, fdo);
85 void *buf = (void *)jlong_to_ptr(address);
86 int result = send(fd, buf, len, 0);
87 if (result < 0 && errno == ECONNREFUSED) {
88 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
89 return IOS_THROWN;
90 }
91 return convertReturnVal(env, result, JNI_FALSE);
92 }
93
94 JNIEXPORT jlong JNICALL
95 Java_sun_nio_ch_DatagramDispatcher_writev0(JNIEnv *env, jclass clazz,
96 jobject fdo, jlong address, jint len)
97 {
98 jint fd = fdval(env, fdo);
99 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
100 struct msghdr m;
101 ssize_t result = 0;
102 if (len > IOV_MAX) {
103 len = IOV_MAX;
104 }
105
106 // initialize the message
107 memset(&m, 0, sizeof(m));
108 m.msg_iov = iov;
109 m.msg_iovlen = len;
110
111 result = sendmsg(fd, &m, 0);
112 if (result < 0 && errno == ECONNREFUSED) {
113 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
114 return IOS_THROWN;
115 }
116 return convertLongReturnVal(env, (jlong)result, JNI_FALSE);
117 }
|