1 /*
2  * Copyright (c) 2001, 2011, 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 // Android-changed: Fuchsia: Point to correct location of header. http://b/119426171
27 // #include <sys/poll.h>
28 #if defined(__Fuchsia__)
29 #include <poll.h>
30 #else
31 #include <sys/poll.h>
32 #endif
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <string.h>
36 #include <netinet/in.h>
37 #include <netinet/tcp.h>
38 
39 #include "jni.h"
40 #include "jni_util.h"
41 #include "jvm.h"
42 #include "jlong.h"
43 #include "sun_nio_ch_Net.h"
44 #include "net_util.h"
45 #include "net_util_md.h"
46 #include "nio_util.h"
47 #include "nio.h"
48 #include "sun_nio_ch_PollArrayWrapper.h"
49 
50 #ifdef _AIX
51 #include <sys/utsname.h>
52 #endif
53 
54 /**
55  * IP_MULTICAST_ALL supported since 2.6.31 but may not be available at
56  * build time.
57  */
58 #ifdef __linux__
59   #ifndef IP_MULTICAST_ALL
60   #define IP_MULTICAST_ALL    49
61   #endif
62 #endif
63 
64 #include <nativehelper/JNIHelp.h>
65 
66 #define NATIVE_METHOD(className, functionName, signature) \
67 { #functionName, signature, (void*)(Java_sun_nio_ch_ ## className ## _ ## functionName) }
68 
69 #if defined(_ALLBSD_SOURCE) || defined(_AIX)
70 
71 #ifndef IP_BLOCK_SOURCE
72 
73 #if defined(_AIX)
74 
75 #define IP_BLOCK_SOURCE                 58   /* Block data from a given source to a given group */
76 #define IP_UNBLOCK_SOURCE               59   /* Unblock data from a given source to a given group */
77 #define IP_ADD_SOURCE_MEMBERSHIP        60   /* Join a source-specific group */
78 #define IP_DROP_SOURCE_MEMBERSHIP       61   /* Leave a source-specific group */
79 
80 #else
81 
82 #define IP_ADD_SOURCE_MEMBERSHIP        70   /* join a source-specific group */
83 #define IP_DROP_SOURCE_MEMBERSHIP       71   /* drop a single source */
84 #define IP_BLOCK_SOURCE                 72   /* block a source */
85 #define IP_UNBLOCK_SOURCE               73   /* unblock a source */
86 
87 #endif /* _AIX */
88 
89 #endif  /* IP_BLOCK_SOURCE */
90 
91 #ifndef MCAST_BLOCK_SOURCE
92 
93 #if defined(_AIX)
94 
95 #define MCAST_BLOCK_SOURCE              64
96 #define MCAST_UNBLOCK_SOURCE            65
97 #define MCAST_JOIN_SOURCE_GROUP         66
98 #define MCAST_LEAVE_SOURCE_GROUP        67
99 
100 #else
101 
102 #define MCAST_JOIN_SOURCE_GROUP         82   /* join a source-specific group */
103 #define MCAST_LEAVE_SOURCE_GROUP        83   /* leave a single source */
104 #define MCAST_BLOCK_SOURCE              84   /* block a source */
105 #define MCAST_UNBLOCK_SOURCE            85   /* unblock a source */
106 
107 #endif /* _AIX */
108 
109 #endif /* MCAST_BLOCK_SOURCE */
110 
111 #ifndef IPV6_ADD_MEMBERSHIP
112 
113 #define IPV6_ADD_MEMBERSHIP     IPV6_JOIN_GROUP
114 #define IPV6_DROP_MEMBERSHIP    IPV6_LEAVE_GROUP
115 
116 #endif /* IPV6_ADD_MEMBERSHIP */
117 
118 #if defined(_AIX)
119 
120 struct my_ip_mreq_source {
121         struct in_addr  imr_multiaddr;
122         struct in_addr  imr_sourceaddr;
123         struct in_addr  imr_interface;
124 };
125 
126 #else
127 
128 struct my_ip_mreq_source {
129         struct in_addr  imr_multiaddr;
130         struct in_addr  imr_interface;
131         struct in_addr  imr_sourceaddr;
132 };
133 
134 #endif /* _AIX */
135 
136 struct my_group_source_req {
137         uint32_t                gsr_interface;  /* interface index */
138         struct sockaddr_storage gsr_group;      /* group address */
139         struct sockaddr_storage gsr_source;     /* source address */
140 };
141 
142 #else   /* _ALLBSD_SOURCE */
143 
144 #define my_ip_mreq_source         ip_mreq_source
145 #define my_group_source_req       group_source_req
146 
147 #endif
148 
149 
150 #define COPY_INET6_ADDRESS(env, source, target) \
151     (*(env))->GetByteArrayRegion(env, source, 0, 16, target)
152 
153 /*
154  * Copy IPv6 group, interface index, and IPv6 source address
155  * into group_source_req structure.
156  */
157 #ifdef AF_INET6
initGroupSourceReq(JNIEnv * env,jbyteArray group,jint index,jbyteArray source,struct my_group_source_req * req)158 static void initGroupSourceReq(JNIEnv* env, jbyteArray group, jint index,
159                                jbyteArray source, struct my_group_source_req* req)
160 {
161     struct sockaddr_in6* sin6;
162 
163     req->gsr_interface = (uint32_t)index;
164 
165     sin6 = (struct sockaddr_in6*)&(req->gsr_group);
166     sin6->sin6_family = AF_INET6;
167     COPY_INET6_ADDRESS(env, group, (jbyte*)&(sin6->sin6_addr));
168 
169     sin6 = (struct sockaddr_in6*)&(req->gsr_source);
170     sin6->sin6_family = AF_INET6;
171     COPY_INET6_ADDRESS(env, source, (jbyte*)&(sin6->sin6_addr));
172 }
173 #endif
174 
175 #ifdef _AIX
176 
177 /*
178  * Checks whether or not "socket extensions for multicast source filters" is supported.
179  * Returns JNI_TRUE if it is supported, JNI_FALSE otherwise
180  */
isSourceFilterSupported()181 static jboolean isSourceFilterSupported(){
182     static jboolean alreadyChecked = JNI_FALSE;
183     static jboolean result = JNI_TRUE;
184     if (alreadyChecked != JNI_TRUE){
185         struct utsname uts;
186         memset(&uts, 0, sizeof(uts));
187         strcpy(uts.sysname, "?");
188         const int utsRes = uname(&uts);
189         int major = -1;
190         int minor = -1;
191         major = atoi(uts.version);
192         minor = atoi(uts.release);
193         if (strcmp(uts.sysname, "AIX") == 0) {
194             if (major < 6 || (major == 6 && minor < 1)) {// unsupported on aix < 6.1
195                 result = JNI_FALSE;
196             }
197         }
198         alreadyChecked = JNI_TRUE;
199     }
200     return result;
201 }
202 
203 #endif  /* _AIX */
204 
205 JNIEXPORT jboolean JNICALL
Java_sun_nio_ch_Net_isIPv6Available0(JNIEnv * env,jclass cl)206 Java_sun_nio_ch_Net_isIPv6Available0(JNIEnv* env, jclass cl)
207 {
208     return (ipv6_available()) ? JNI_TRUE : JNI_FALSE;
209 }
210 
211 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_isExclusiveBindAvailable(JNIEnv * env,jclass clazz)212 Java_sun_nio_ch_Net_isExclusiveBindAvailable(JNIEnv *env, jclass clazz) {
213     return -1;
214 }
215 
216 JNIEXPORT jboolean JNICALL
Java_sun_nio_ch_Net_canIPv6SocketJoinIPv4Group0(JNIEnv * env,jclass cl)217 Java_sun_nio_ch_Net_canIPv6SocketJoinIPv4Group0(JNIEnv* env, jclass cl)
218 {
219 #if defined(MACOSX) || defined(_AIX)
220     /* for now IPv6 sockets cannot join IPv4 multicast groups */
221     return JNI_FALSE;
222 #else
223     return JNI_TRUE;
224 #endif
225 }
226 
227 JNIEXPORT jboolean JNICALL
Java_sun_nio_ch_Net_canJoin6WithIPv4Group0(JNIEnv * env,jclass cl)228 Java_sun_nio_ch_Net_canJoin6WithIPv4Group0(JNIEnv* env, jclass cl)
229 {
230 #ifdef __solaris__
231     return JNI_TRUE;
232 #else
233     return JNI_FALSE;
234 #endif
235 }
236 
237 JNIEXPORT int JNICALL
Java_sun_nio_ch_Net_socket0(JNIEnv * env,jclass cl,jboolean preferIPv6,jboolean stream,jboolean reuse,jboolean ignored)238 Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
239                             jboolean stream, jboolean reuse, jboolean ignored)
240 {
241     int fd;
242     int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
243 #ifdef AF_INET6
244     int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET;
245 #else
246     int domain = AF_INET;
247 #endif
248 
249     fd = socket(domain, type, 0);
250     tagSocket(env, fd);
251     if (fd < 0) {
252         return handleSocketError(env, errno);
253     }
254 
255 #ifdef AF_INET6
256     /* Disable IPV6_V6ONLY to ensure dual-socket support */
257     if (domain == AF_INET6) {
258         int arg = 0;
259         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
260                        sizeof(int)) < 0) {
261             JNU_ThrowByNameWithLastError(env,
262                                          JNU_JAVANETPKG "SocketException",
263                                          "Unable to set IPV6_V6ONLY");
264             close(fd);
265             return -1;
266         }
267     }
268 #endif
269 
270     if (reuse) {
271         int arg = 1;
272         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
273                        sizeof(arg)) < 0) {
274             JNU_ThrowByNameWithLastError(env,
275                                          JNU_JAVANETPKG "SocketException",
276                                          "Unable to set SO_REUSEADDR");
277             close(fd);
278             return -1;
279         }
280     }
281 
282 #if defined(__linux__)
283     if (type == SOCK_DGRAM) {
284         int arg = 0;
285         int level = (domain == AF_INET6) ? IPPROTO_IPV6 : IPPROTO_IP;
286         if ((setsockopt(fd, level, IP_MULTICAST_ALL, (char*)&arg, sizeof(arg)) < 0) &&
287             (errno != ENOPROTOOPT)) {
288             JNU_ThrowByNameWithLastError(env,
289                                          JNU_JAVANETPKG "SocketException",
290                                          "Unable to set IP_MULTICAST_ALL");
291             close(fd);
292             return -1;
293         }
294     }
295 #endif
296 
297 #if defined(__linux__) && defined(AF_INET6)
298     /* By default, Linux uses the route default */
299     if (domain == AF_INET6 && type == SOCK_DGRAM) {
300         int arg = 1;
301         if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &arg,
302                        sizeof(arg)) < 0) {
303             JNU_ThrowByNameWithLastError(env,
304                                          JNU_JAVANETPKG "SocketException",
305                                          "Unable to set IPV6_MULTICAST_HOPS");
306             close(fd);
307             return -1;
308         }
309     }
310 #endif
311     return fd;
312 }
313 
314 JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_bind0(JNIEnv * env,jclass clazz,jobject fdo,jboolean preferIPv6,jboolean useExclBind,jobject iao,int port)315 Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean preferIPv6,
316                           jboolean useExclBind, jobject iao, int port)
317 {
318     SOCKADDR sa;
319     int sa_len = SOCKADDR_LEN;
320     int rv = 0;
321 
322     if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) {
323       return;
324     }
325 
326     rv = NET_Bind(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
327     if (rv != 0) {
328         handleSocketError(env, errno);
329     }
330 }
331 
332 JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_listen(JNIEnv * env,jclass cl,jobject fdo,jint backlog)333 Java_sun_nio_ch_Net_listen(JNIEnv *env, jclass cl, jobject fdo, jint backlog)
334 {
335     if (listen(fdval(env, fdo), backlog) < 0)
336         handleSocketError(env, errno);
337 }
338 
339 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_connect0(JNIEnv * env,jclass clazz,jboolean preferIPv6,jobject fdo,jobject iao,jint port)340 Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
341                              jobject fdo, jobject iao, jint port)
342 {
343     SOCKADDR sa;
344     int sa_len = SOCKADDR_LEN;
345     int rv;
346 
347     if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *) &sa,
348                                   &sa_len, preferIPv6) != 0)
349     {
350       return IOS_THROWN;
351     }
352 
353     rv = connect(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
354     if (rv != 0) {
355         if (errno == EINPROGRESS) {
356             return IOS_UNAVAILABLE;
357         } else if (errno == EINTR) {
358             return IOS_INTERRUPTED;
359         }
360         return handleSocketErrorWithDefault(env, errno, JNU_JAVANETPKG "ConnectException");
361     }
362     return 1;
363 }
364 
365 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_localPort(JNIEnv * env,jclass clazz,jobject fdo)366 Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
367 {
368     SOCKADDR sa;
369     socklen_t sa_len = SOCKADDR_LEN;
370     if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
371 #ifdef _ALLBSD_SOURCE
372         /*
373          * XXXBSD:
374          * ECONNRESET is specific to the BSDs. We can not return an error,
375          * as the calling Java code with raise a java.lang.Error given the expectation
376          * that getsockname() will never fail. According to the Single UNIX Specification,
377          * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
378          */
379         if (errno == ECONNRESET) {
380             struct sockaddr_in *sin;
381             sin = (struct sockaddr_in *) &sa;
382             bzero(sin, sizeof(*sin));
383             // BEGIN Android-changed: Fuchsia: sin_len is not a sockaddr_in member on Fuchsia.
384             // http://b/119497331
385             // sin->sin_len = sizeof(struct sockaddr_in);
386 #if !defined(__Fuchsia__)
387             sin->sin_len  = sizeof(struct sockaddr_in);
388 #endif
389             // END Android-changed: Fuchsia: sin_len is not a sockaddr_in member on Fuchsia.
390             sin->sin_family = AF_INET;
391             sin->sin_port = htonl(0);
392             sin->sin_addr.s_addr = INADDR_ANY;
393         } else {
394             handleSocketError(env, errno);
395             return -1;
396         }
397 #else /* _ALLBSD_SOURCE */
398         handleSocketError(env, errno);
399         return -1;
400 #endif /* _ALLBSD_SOURCE */
401     }
402     return NET_GetPortFromSockaddr((struct sockaddr *)&sa);
403 }
404 
405 JNIEXPORT jobject JNICALL
Java_sun_nio_ch_Net_localInetAddress(JNIEnv * env,jclass clazz,jobject fdo)406 Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
407 {
408     SOCKADDR sa;
409     socklen_t sa_len = SOCKADDR_LEN;
410     int port;
411     if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
412 #ifdef _ALLBSD_SOURCE
413         /*
414          * XXXBSD:
415          * ECONNRESET is specific to the BSDs. We can not return an error,
416          * as the calling Java code with raise a java.lang.Error with the expectation
417          * that getsockname() will never fail. According to the Single UNIX Specification,
418          * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
419          */
420         if (errno == ECONNRESET) {
421             struct sockaddr_in *sin;
422             sin = (struct sockaddr_in *) &sa;
423             bzero(sin, sizeof(*sin));
424             // BEGIN Android-changed: Fuchsia: sin_len is not a sockaddr_in member on Fuchsia.
425             // http://b/119497331
426             // sin->sin_len = sizeof(struct sockaddr_in);
427 #if !defined(__Fuchsia__)
428             sin->sin_len  = sizeof(struct sockaddr_in);
429 #endif
430             // END Android-changed: Fuchsia: sin_len is not a sockaddr_in member on Fuchsia.
431             sin->sin_family = AF_INET;
432             sin->sin_port = htonl(0);
433             sin->sin_addr.s_addr = INADDR_ANY;
434         } else {
435             handleSocketError(env, errno);
436             return NULL;
437         }
438 #else /* _ALLBSD_SOURCE */
439         handleSocketError(env, errno);
440         return NULL;
441 #endif /* _ALLBSD_SOURCE */
442     }
443     return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
444 }
445 
446 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_getIntOption0(JNIEnv * env,jclass clazz,jobject fdo,jboolean mayNeedConversion,jint level,jint opt)447 Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
448                                   jboolean mayNeedConversion, jint level, jint opt)
449 {
450     int result;
451     struct linger linger;
452     u_char carg;
453     void *arg;
454     socklen_t arglen;
455     int n;
456 
457     /* Option value is an int except for a few specific cases */
458 
459     arg = (void *)&result;
460     arglen = sizeof(result);
461 
462     if (level == IPPROTO_IP &&
463         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP)) {
464         arg = (void*)&carg;
465         arglen = sizeof(carg);
466     }
467 
468     if (level == SOL_SOCKET && opt == SO_LINGER) {
469         arg = (void *)&linger;
470         arglen = sizeof(linger);
471     }
472 
473     if (mayNeedConversion) {
474         n = NET_GetSockOpt(fdval(env, fdo), level, opt, arg, (int*)&arglen);
475     } else {
476         n = getsockopt(fdval(env, fdo), level, opt, arg, &arglen);
477     }
478     if (n < 0) {
479         JNU_ThrowByNameWithLastError(env,
480                                      JNU_JAVANETPKG "SocketException",
481                                      "sun.nio.ch.Net.getIntOption");
482         return -1;
483     }
484 
485     if (level == IPPROTO_IP &&
486         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP))
487     {
488         return (jint)carg;
489     }
490 
491     if (level == SOL_SOCKET && opt == SO_LINGER)
492         return linger.l_onoff ? (jint)linger.l_linger : (jint)-1;
493 
494     return (jint)result;
495 }
496 
497 JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_setIntOption0(JNIEnv * env,jclass clazz,jobject fdo,jboolean mayNeedConversion,jint level,jint opt,jint arg,jboolean isIPv6)498 Java_sun_nio_ch_Net_setIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
499                                   jboolean mayNeedConversion, jint level,
500                                   jint opt, jint arg, jboolean isIPv6)
501 {
502     int result;
503     struct linger linger;
504     u_char carg;
505     void *parg;
506     socklen_t arglen;
507     int n;
508 
509     /* Option value is an int except for a few specific cases */
510 
511     parg = (void*)&arg;
512     arglen = sizeof(arg);
513 
514     if (level == IPPROTO_IP &&
515         (opt == IP_MULTICAST_TTL || opt == IP_MULTICAST_LOOP)) {
516         parg = (void*)&carg;
517         arglen = sizeof(carg);
518         carg = (u_char)arg;
519     }
520 
521     if (level == SOL_SOCKET && opt == SO_LINGER) {
522         parg = (void *)&linger;
523         arglen = sizeof(linger);
524         if (arg >= 0) {
525             linger.l_onoff = 1;
526             linger.l_linger = arg;
527         } else {
528             linger.l_onoff = 0;
529             linger.l_linger = 0;
530         }
531     }
532 
533     if (mayNeedConversion) {
534         n = NET_SetSockOpt(fdval(env, fdo), level, opt, parg, arglen);
535     } else {
536         n = setsockopt(fdval(env, fdo), level, opt, parg, arglen);
537     }
538     if (n < 0) {
539         JNU_ThrowByNameWithLastError(env,
540                                      JNU_JAVANETPKG "SocketException",
541                                      "sun.nio.ch.Net.setIntOption");
542     }
543 #ifdef __linux__
544     if (level == IPPROTO_IPV6 && opt == IPV6_TCLASS && isIPv6) {
545         // set the V4 option also
546         setsockopt(fdval(env, fdo), IPPROTO_IP, IP_TOS, parg, arglen);
547     }
548 #endif
549 }
550 
551 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv * env,jobject this,jboolean join,jobject fdo,jint group,jint interf,jint source)552 Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv *env, jobject this, jboolean join, jobject fdo,
553                                 jint group, jint interf, jint source)
554 {
555     struct ip_mreq mreq;
556     struct my_ip_mreq_source mreq_source;
557     int opt, n, optlen;
558     void* optval;
559 
560     if (source == 0) {
561         mreq.imr_multiaddr.s_addr = htonl(group);
562         mreq.imr_interface.s_addr = htonl(interf);
563         opt = (join) ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
564         optval = (void*)&mreq;
565         optlen = sizeof(mreq);
566     } else {
567 #ifdef MACOSX
568         /* no IPv4 include-mode filtering for now */
569         return IOS_UNAVAILABLE;
570 #else
571 
572 #ifdef _AIX
573         /* check AIX for support of source filtering */
574         if (isSourceFilterSupported() != JNI_TRUE){
575             return IOS_UNAVAILABLE;
576         }
577 #endif
578 
579         mreq_source.imr_multiaddr.s_addr = htonl(group);
580         mreq_source.imr_sourceaddr.s_addr = htonl(source);
581         mreq_source.imr_interface.s_addr = htonl(interf);
582         opt = (join) ? IP_ADD_SOURCE_MEMBERSHIP : IP_DROP_SOURCE_MEMBERSHIP;
583         optval = (void*)&mreq_source;
584         optlen = sizeof(mreq_source);
585 #endif
586     }
587 
588     n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt, optval, optlen);
589     if (n < 0) {
590         if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
591             return IOS_UNAVAILABLE;
592         handleSocketError(env, errno);
593     }
594     return 0;
595 }
596 
597 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv * env,jobject this,jboolean block,jobject fdo,jint group,jint interf,jint source)598 Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, jobject fdo,
599                                     jint group, jint interf, jint source)
600 {
601 #ifdef MACOSX
602     /* no IPv4 exclude-mode filtering for now */
603     return IOS_UNAVAILABLE;
604 #else
605     struct my_ip_mreq_source mreq_source;
606     int n;
607     int opt = (block) ? IP_BLOCK_SOURCE : IP_UNBLOCK_SOURCE;
608 
609 #ifdef _AIX
610     /* check AIX for support of source filtering */
611     if (isSourceFilterSupported() != JNI_TRUE){
612         return IOS_UNAVAILABLE;
613     }
614 #endif
615         mreq_source.imr_multiaddr.s_addr = htonl(group);
616         mreq_source.imr_sourceaddr.s_addr = htonl(source);
617         mreq_source.imr_interface.s_addr = htonl(interf);
618 
619     n = setsockopt(fdval(env,fdo), IPPROTO_IP, opt,
620                    (void*)&mreq_source, sizeof(mreq_source));
621     if (n < 0) {
622         if (block && (errno == ENOPROTOOPT))
623             return IOS_UNAVAILABLE;
624         handleSocketError(env, errno);
625     }
626     return 0;
627 #endif
628 }
629 
630 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv * env,jobject this,jboolean join,jobject fdo,jbyteArray group,jint index,jbyteArray source)631 Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobject fdo,
632                                 jbyteArray group, jint index, jbyteArray source)
633 {
634 #ifdef AF_INET6
635     struct ipv6_mreq mreq6;
636     struct my_group_source_req req;
637     int opt, n, optlen;
638     void* optval;
639 
640     if (source == NULL) {
641         COPY_INET6_ADDRESS(env, group, (jbyte*)&(mreq6.ipv6mr_multiaddr));
642         mreq6.ipv6mr_interface = (int)index;
643         opt = (join) ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP;
644         optval = (void*)&mreq6;
645         optlen = sizeof(mreq6);
646     } else {
647 #ifdef MACOSX
648         /* no IPv6 include-mode filtering for now */
649         return IOS_UNAVAILABLE;
650 #else
651         initGroupSourceReq(env, group, index, source, &req);
652         opt = (join) ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP;
653         optval = (void*)&req;
654         optlen = sizeof(req);
655 #endif
656     }
657 
658     n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt, optval, optlen);
659     if (n < 0) {
660         if (join && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
661             return IOS_UNAVAILABLE;
662         handleSocketError(env, errno);
663     }
664     return 0;
665 #else
666     JNU_ThrowInternalError(env, "Should not get here");
667     return IOS_THROWN;
668 #endif  /* AF_INET6 */
669 }
670 
671 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv * env,jobject this,jboolean block,jobject fdo,jbyteArray group,jint index,jbyteArray source)672 Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, jobject fdo,
673                                     jbyteArray group, jint index, jbyteArray source)
674 {
675 #ifdef AF_INET6
676   #ifdef MACOSX
677     /* no IPv6 exclude-mode filtering for now */
678     return IOS_UNAVAILABLE;
679   #else
680     struct my_group_source_req req;
681     int n;
682     int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
683 
684     initGroupSourceReq(env, group, index, source, &req);
685 
686     n = setsockopt(fdval(env,fdo), IPPROTO_IPV6, opt,
687         (void*)&req, sizeof(req));
688     if (n < 0) {
689         if (block && (errno == ENOPROTOOPT || errno == EOPNOTSUPP))
690             return IOS_UNAVAILABLE;
691         handleSocketError(env, errno);
692     }
693     return 0;
694   #endif
695 #else
696     JNU_ThrowInternalError(env, "Should not get here");
697     return IOS_THROWN;
698 #endif
699 }
700 
701 JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_setInterface4(JNIEnv * env,jobject this,jobject fdo,jint interf)702 Java_sun_nio_ch_Net_setInterface4(JNIEnv* env, jobject this, jobject fdo, jint interf)
703 {
704     struct in_addr in;
705     socklen_t arglen = sizeof(struct in_addr);
706     int n;
707 
708     in.s_addr = htonl(interf);
709 
710     n = setsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF,
711                    (void*)&(in.s_addr), arglen);
712     if (n < 0) {
713         handleSocketError(env, errno);
714     }
715 }
716 
717 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_getInterface4(JNIEnv * env,jobject this,jobject fdo)718 Java_sun_nio_ch_Net_getInterface4(JNIEnv* env, jobject this, jobject fdo)
719 {
720     struct in_addr in;
721     socklen_t arglen = sizeof(struct in_addr);
722     int n;
723 
724     n = getsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF, (void*)&in, &arglen);
725     if (n < 0) {
726         handleSocketError(env, errno);
727         return -1;
728     }
729     return ntohl(in.s_addr);
730 }
731 
732 JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_setInterface6(JNIEnv * env,jobject this,jobject fdo,jint index)733 Java_sun_nio_ch_Net_setInterface6(JNIEnv* env, jobject this, jobject fdo, jint index)
734 {
735     int value = (jint)index;
736     socklen_t arglen = sizeof(value);
737     int n;
738 
739     n = setsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF,
740                    (void*)&(index), arglen);
741     if (n < 0) {
742         handleSocketError(env, errno);
743     }
744 }
745 
746 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_getInterface6(JNIEnv * env,jobject this,jobject fdo)747 Java_sun_nio_ch_Net_getInterface6(JNIEnv* env, jobject this, jobject fdo)
748 {
749     int index;
750     socklen_t arglen = sizeof(index);
751     int n;
752 
753     n = getsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF, (void*)&index, &arglen);
754     if (n < 0) {
755         handleSocketError(env, errno);
756         return -1;
757     }
758     return (jint)index;
759 }
760 
761 JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_shutdown(JNIEnv * env,jclass cl,jobject fdo,jint jhow)762 Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow)
763 {
764     int how = (jhow == sun_nio_ch_Net_SHUT_RD) ? SHUT_RD :
765         (jhow == sun_nio_ch_Net_SHUT_WR) ? SHUT_WR : SHUT_RDWR;
766     if ((shutdown(fdval(env, fdo), how) < 0) && (errno != ENOTCONN))
767         handleSocketError(env, errno);
768 }
769 
770 JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_poll(JNIEnv * env,jclass this,jobject fdo,jint events,jlong timeout)771 Java_sun_nio_ch_Net_poll(JNIEnv* env, jclass this, jobject fdo, jint events, jlong timeout)
772 {
773     struct pollfd pfd;
774     int rv;
775     pfd.fd = fdval(env, fdo);
776     pfd.events = events;
777     rv = poll(&pfd, 1, timeout);
778 
779     if (rv >= 0) {
780         return pfd.revents;
781     } else if (errno == EINTR) {
782         return IOS_INTERRUPTED;
783     } else {
784         handleSocketError(env, errno);
785         return IOS_THROWN;
786     }
787 }
788 
789 JNIEXPORT jshort JNICALL
Java_sun_nio_ch_Net_pollinValue(JNIEnv * env,jclass this)790 Java_sun_nio_ch_Net_pollinValue(JNIEnv *env, jclass this)
791 {
792     return (jshort)POLLIN;
793 }
794 
795 JNIEXPORT jshort JNICALL
Java_sun_nio_ch_Net_polloutValue(JNIEnv * env,jclass this)796 Java_sun_nio_ch_Net_polloutValue(JNIEnv *env, jclass this)
797 {
798     return (jshort)POLLOUT;
799 }
800 
801 JNIEXPORT jshort JNICALL
Java_sun_nio_ch_Net_pollerrValue(JNIEnv * env,jclass this)802 Java_sun_nio_ch_Net_pollerrValue(JNIEnv *env, jclass this)
803 {
804     return (jshort)POLLERR;
805 }
806 
807 JNIEXPORT jshort JNICALL
Java_sun_nio_ch_Net_pollhupValue(JNIEnv * env,jclass this)808 Java_sun_nio_ch_Net_pollhupValue(JNIEnv *env, jclass this)
809 {
810     return (jshort)POLLHUP;
811 }
812 
813 JNIEXPORT jshort JNICALL
Java_sun_nio_ch_Net_pollnvalValue(JNIEnv * env,jclass this)814 Java_sun_nio_ch_Net_pollnvalValue(JNIEnv *env, jclass this)
815 {
816     return (jshort)POLLNVAL;
817 }
818 
819 JNIEXPORT jshort JNICALL
Java_sun_nio_ch_Net_pollconnValue(JNIEnv * env,jclass this)820 Java_sun_nio_ch_Net_pollconnValue(JNIEnv *env, jclass this)
821 {
822     return (jshort)POLLOUT;
823 }
824 
825 
826 /* Declared in nio_util.h */
827 
828 jint
handleSocketErrorWithDefault(JNIEnv * env,jint errorValue,const char * defaultException)829 handleSocketErrorWithDefault(JNIEnv *env, jint errorValue, const char *defaultException)
830 {
831     const char *xn;
832     switch (errorValue) {
833         case EINPROGRESS:       /* Non-blocking connect */
834             return 0;
835 #ifdef EPROTO
836         case EPROTO:
837             xn = JNU_JAVANETPKG "ProtocolException";
838             break;
839 #endif
840         case ECONNREFUSED:
841             xn = JNU_JAVANETPKG "ConnectException";
842             break;
843         case ETIMEDOUT:
844             xn = JNU_JAVANETPKG "ConnectException";
845             break;
846         case EHOSTUNREACH:
847             xn = JNU_JAVANETPKG "NoRouteToHostException";
848             break;
849         case EADDRINUSE:  /* Fall through */
850         case EADDRNOTAVAIL:
851             xn = JNU_JAVANETPKG "BindException";
852             break;
853         default:
854             xn = defaultException;
855             break;
856     }
857     errno = errorValue;
858     JNU_ThrowByNameWithLastError(env, xn, "NioSocketError");
859     return IOS_THROWN;
860 }
861 
862 /* Declared in nio_util.h */
863 
864 jint
handleSocketError(JNIEnv * env,jint errorValue)865 handleSocketError(JNIEnv *env, jint errorValue) {
866     return handleSocketErrorWithDefault(env, errorValue,
867                                         JNU_JAVANETPKG "SocketException");
868 }
869 
870 
871 static JNINativeMethod gMethods[] = {
872   NATIVE_METHOD(Net, isIPv6Available0, "()Z"),
873   NATIVE_METHOD(Net, isExclusiveBindAvailable, "()I"),
874   NATIVE_METHOD(Net, canIPv6SocketJoinIPv4Group0, "()Z"),
875   NATIVE_METHOD(Net, canJoin6WithIPv4Group0, "()Z"),
876   NATIVE_METHOD(Net, socket0, "(ZZZZ)I"),
877   NATIVE_METHOD(Net, bind0, "(Ljava/io/FileDescriptor;ZZLjava/net/InetAddress;I)V"),
878   NATIVE_METHOD(Net, listen, "(Ljava/io/FileDescriptor;I)V"),
879   NATIVE_METHOD(Net, connect0, "(ZLjava/io/FileDescriptor;Ljava/net/InetAddress;I)I"),
880   NATIVE_METHOD(Net, shutdown, "(Ljava/io/FileDescriptor;I)V"),
881   NATIVE_METHOD(Net, localPort, "(Ljava/io/FileDescriptor;)I"),
882   NATIVE_METHOD(Net, localInetAddress, "(Ljava/io/FileDescriptor;)Ljava/net/InetAddress;"),
883   NATIVE_METHOD(Net, getIntOption0, "(Ljava/io/FileDescriptor;ZII)I"),
884   NATIVE_METHOD(Net, setIntOption0, "(Ljava/io/FileDescriptor;ZIIIZ)V"),
885   NATIVE_METHOD(Net, joinOrDrop4, "(ZLjava/io/FileDescriptor;III)I"),
886   NATIVE_METHOD(Net, blockOrUnblock4, "(ZLjava/io/FileDescriptor;III)I"),
887   NATIVE_METHOD(Net, joinOrDrop6, "(ZLjava/io/FileDescriptor;[BI[B)I"),
888   NATIVE_METHOD(Net, blockOrUnblock6, "(ZLjava/io/FileDescriptor;[BI[B)I"),
889   NATIVE_METHOD(Net, setInterface4, "(Ljava/io/FileDescriptor;I)V"),
890   NATIVE_METHOD(Net, getInterface4, "(Ljava/io/FileDescriptor;)I"),
891   NATIVE_METHOD(Net, setInterface6, "(Ljava/io/FileDescriptor;I)V"),
892   NATIVE_METHOD(Net, getInterface6, "(Ljava/io/FileDescriptor;)I"),
893   NATIVE_METHOD(Net, poll, "(Ljava/io/FileDescriptor;IJ)I"),
894   NATIVE_METHOD(Net, pollinValue, "()S"),
895   NATIVE_METHOD(Net, polloutValue, "()S"),
896   NATIVE_METHOD(Net, pollhupValue, "()S"),
897   NATIVE_METHOD(Net, pollerrValue, "()S"),
898   NATIVE_METHOD(Net, pollnvalValue, "()S"),
899   NATIVE_METHOD(Net, pollconnValue, "()S"),
900 };
901 
register_sun_nio_ch_Net(JNIEnv * env)902 void register_sun_nio_ch_Net(JNIEnv* env) {
903   jniRegisterNativeMethods(env, "sun/nio/ch/Net", gMethods, NELEM(gMethods));
904 }
905