blob: 716792385f451d8d5746d5777a86a4af7c3d1604 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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
38#include "nio_util.h"
39
40JNIEXPORT jint JNICALL
41Java_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 -2;
50 }
51 return convertReturnVal(env, result, JNI_TRUE);
52}
53
54
55JNIEXPORT jlong JNICALL
56Java_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 > 16) {
64 len = 16;
65 }
66
67 m.msg_name = NULL;
68 m.msg_namelen = 0;
69 m.msg_iov = iov;
70 m.msg_iovlen = len;
71#ifdef __solaris__
72 m.msg_accrights = NULL;
73 m.msg_accrightslen = 0;
74#endif
75
76#ifdef __linux__
77 m.msg_control = NULL;
78 m.msg_controllen = 0;
79#endif
80
81 result = recvmsg(fd, &m, 0);
82 if (result < 0 && errno == ECONNREFUSED) {
83 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
84 return -2;
85 }
86 return convertLongReturnVal(env, (jlong)result, JNI_TRUE);
87}
88
89JNIEXPORT jint JNICALL
90Java_sun_nio_ch_DatagramDispatcher_write0(JNIEnv *env, jclass clazz,
91 jobject fdo, jlong address, jint len)
92{
93 jint fd = fdval(env, fdo);
94 void *buf = (void *)jlong_to_ptr(address);
95 int result = send(fd, buf, len, 0);
96 if (result < 0 && errno == ECONNREFUSED) {
97 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
98 return -2;
99 }
100 return convertReturnVal(env, result, JNI_FALSE);
101}
102
103JNIEXPORT jlong JNICALL
104Java_sun_nio_ch_DatagramDispatcher_writev0(JNIEnv *env, jclass clazz,
105 jobject fdo, jlong address, jint len)
106{
107 jint fd = fdval(env, fdo);
108 struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
109 struct msghdr m;
110 ssize_t result = 0;
111 if (len > 16) {
112 len = 16;
113 }
114
115 m.msg_name = NULL;
116 m.msg_namelen = 0;
117 m.msg_iov = iov;
118 m.msg_iovlen = len;
119#ifdef __solaris__
120 m.msg_accrights = NULL;
121 m.msg_accrightslen = 0;
122#endif
123
124#ifdef __linux__
125 m.msg_control = NULL;
126 m.msg_controllen = 0;
127#endif
128
129 result = sendmsg(fd, &m, 0);
130 if (result < 0 && errno == ECONNREFUSED) {
131 JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException", 0);
132 return -2;
133 }
134 return convertLongReturnVal(env, (jlong)result, JNI_FALSE);
135}