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 | } |
120 | |