1 /*
2  * Copyright (c) 1997, 2018, 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 #include <dlfcn.h>
26 #include <errno.h>
27 #include <net/if.h>
28 #include <netinet/tcp.h> // defines TCP_NODELAY
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/time.h>
33 
34 #if defined(__linux__)
35 #include <arpa/inet.h>
36 #include <net/route.h>
37 #include <sys/utsname.h>
38 #endif
39 
40 #if defined(__solaris__)
41 #include <inet/nd.h>
42 #include <limits.h>
43 #include <stropts.h>
44 #include <sys/filio.h>
45 #include <sys/sockio.h>
46 #endif
47 
48 #if defined(MACOSX)
49 #include <sys/sysctl.h>
50 #endif
51 
52 #ifdef __OpenBSD__
53 #include <sys/socketvar.h>
54 #endif
55 
56 #include "jvm.h"
57 #include "net_util.h"
58 
59 #include "java_net_SocketOptions.h"
60 #include "java_net_InetAddress.h"
61 
62 #if defined(__linux__) && !defined(IPV6_FLOWINFO_SEND)
63 #define IPV6_FLOWINFO_SEND      33
64 #endif
65 
66 #if defined(__solaris__) && !defined(MAXINT)
67 #define MAXINT INT_MAX
68 #endif
69 
70 /*
71  * EXCLBIND socket options only on Solaris
72  */
73 #if defined(__solaris__) && !defined(TCP_EXCLBIND)
74 #define TCP_EXCLBIND            0x21
75 #endif
76 #if defined(__solaris__) && !defined(UDP_EXCLBIND)
77 #define UDP_EXCLBIND            0x0101
78 #endif
79 
setDefaultScopeID(JNIEnv * env,struct sockaddr * him)80 void setDefaultScopeID(JNIEnv *env, struct sockaddr *him)
81 {
82 #ifdef _ALLBSD_SOURCE
83     static jclass ni_class = NULL;
84     static jfieldID ni_defaultIndexID;
85     if (ni_class == NULL) {
86         jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
87         CHECK_NULL(c);
88         c = (*env)->NewGlobalRef(env, c);
89         CHECK_NULL(c);
90         ni_defaultIndexID = (*env)->GetStaticFieldID(env, c, "defaultIndex", "I");
91         CHECK_NULL(ni_defaultIndexID);
92         ni_class = c;
93     }
94     int defaultIndex;
95     struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)him;
96     if (sin6->sin6_family == AF_INET6 && (sin6->sin6_scope_id == 0) &&
97         (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
98          IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
99         defaultIndex = (*env)->GetStaticIntField(env, ni_class,
100                                                  ni_defaultIndexID);
101         sin6->sin6_scope_id = defaultIndex;
102     }
103 #endif
104 }
105 
getDefaultScopeID(JNIEnv * env)106 int getDefaultScopeID(JNIEnv *env) {
107     int defaultIndex = 0;
108     static jclass ni_class = NULL;
109     static jfieldID ni_defaultIndexID;
110     if (ni_class == NULL) {
111         jclass c = (*env)->FindClass(env, "java/net/NetworkInterface");
112         CHECK_NULL_RETURN(c, 0);
113         c = (*env)->NewGlobalRef(env, c);
114         CHECK_NULL_RETURN(c, 0);
115         ni_defaultIndexID = (*env)->GetStaticFieldID(env, c, "defaultIndex", "I");
116         CHECK_NULL_RETURN(ni_defaultIndexID, 0);
117         ni_class = c;
118     }
119     defaultIndex = (*env)->GetStaticIntField(env, ni_class,
120                                              ni_defaultIndexID);
121     return defaultIndex;
122 }
123 
124 #define RESTARTABLE(_cmd, _result) do { \
125     do { \
126         _result = _cmd; \
127     } while((_result == -1) && (errno == EINTR)); \
128 } while(0)
129 
NET_SocketAvailable(int s,jint * pbytes)130 int NET_SocketAvailable(int s, jint *pbytes) {
131     int result;
132     RESTARTABLE(ioctl(s, FIONREAD, pbytes), result);
133     // note: ioctl can return 0 when successful, NET_SocketAvailable
134     // is expected to return 0 on failure and 1 on success.
135     return (result == -1) ? 0 : 1;
136 }
137 
138 #ifdef __solaris__
139 static int init_tcp_max_buf, init_udp_max_buf;
140 static int tcp_max_buf;
141 static int udp_max_buf;
142 static int useExclBind = 0;
143 
144 /*
145  * Get the specified parameter from the specified driver. The value
146  * of the parameter is assumed to be an 'int'. If the parameter
147  * cannot be obtained return -1
148  */
net_getParam(char * driver,char * param)149 int net_getParam(char *driver, char *param)
150 {
151     struct strioctl stri;
152     char buf [64];
153     int s;
154     int value;
155 
156     s = open (driver, O_RDWR);
157     if (s < 0) {
158         return -1;
159     }
160     strncpy (buf, param, sizeof(buf));
161     stri.ic_cmd = ND_GET;
162     stri.ic_timout = 0;
163     stri.ic_dp = buf;
164     stri.ic_len = sizeof(buf);
165     if (ioctl (s, I_STR, &stri) < 0) {
166         value = -1;
167     } else {
168         value = atoi(buf);
169     }
170     close (s);
171     return value;
172 }
173 
174 /*
175  * Iterative way to find the max value that SO_SNDBUF or SO_RCVBUF
176  * for Solaris versions that do not support the ioctl() in net_getParam().
177  * Ugly, but only called once (for each sotype).
178  *
179  * As an optimization, we make a guess using the default values for Solaris
180  * assuming they haven't been modified with ndd.
181  */
182 
183 #define MAX_TCP_GUESS 1024 * 1024
184 #define MAX_UDP_GUESS 2 * 1024 * 1024
185 
186 #define FAIL_IF_NOT_ENOBUFS if (errno != ENOBUFS) return -1
187 
findMaxBuf(int fd,int opt,int sotype)188 static int findMaxBuf(int fd, int opt, int sotype) {
189     int a = 0;
190     int b = MAXINT;
191     int initial_guess;
192     int limit = -1;
193 
194     if (sotype == SOCK_DGRAM) {
195         initial_guess = MAX_UDP_GUESS;
196     } else {
197         initial_guess = MAX_TCP_GUESS;
198     }
199 
200     if (setsockopt(fd, SOL_SOCKET, opt, &initial_guess, sizeof(int)) == 0) {
201         initial_guess++;
202         if (setsockopt(fd, SOL_SOCKET, opt, &initial_guess,sizeof(int)) < 0) {
203             FAIL_IF_NOT_ENOBUFS;
204             return initial_guess - 1;
205         }
206         a = initial_guess;
207     } else {
208         FAIL_IF_NOT_ENOBUFS;
209         b = initial_guess - 1;
210     }
211     do {
212         int mid = a + (b-a)/2;
213         if (setsockopt(fd, SOL_SOCKET, opt, &mid, sizeof(int)) == 0) {
214             limit = mid;
215             a = mid + 1;
216         } else {
217             FAIL_IF_NOT_ENOBUFS;
218             b = mid - 1;
219         }
220     } while (b >= a);
221 
222     return limit;
223 }
224 #endif
225 
226 #ifdef __linux__
227 static int vinit = 0;
228 static int kernelV24 = 0;
229 static int vinit24 = 0;
230 
kernelIsV24()231 int kernelIsV24 () {
232     if (!vinit24) {
233         struct utsname sysinfo;
234         if (uname(&sysinfo) == 0) {
235             sysinfo.release[3] = '\0';
236             if (strcmp(sysinfo.release, "2.4") == 0) {
237                 kernelV24 = JNI_TRUE;
238             }
239         }
240         vinit24 = 1;
241     }
242     return kernelV24;
243 }
244 #endif
245 
246 void
NET_ThrowByNameWithLastError(JNIEnv * env,const char * name,const char * defaultDetail)247 NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
248                    const char *defaultDetail) {
249     JNU_ThrowByNameWithMessageAndLastError(env, name, defaultDetail);
250 }
251 
252 void
NET_ThrowCurrent(JNIEnv * env,char * msg)253 NET_ThrowCurrent(JNIEnv *env, char *msg) {
254     NET_ThrowNew(env, errno, msg);
255 }
256 
257 void
NET_ThrowNew(JNIEnv * env,int errorNumber,char * msg)258 NET_ThrowNew(JNIEnv *env, int errorNumber, char *msg) {
259     char fullMsg[512];
260     if (!msg) {
261         msg = "no further information";
262     }
263     switch(errorNumber) {
264     case EBADF:
265         jio_snprintf(fullMsg, sizeof(fullMsg), "socket closed: %s", msg);
266         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", fullMsg);
267         break;
268     case EINTR:
269         JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", msg);
270         break;
271     default:
272         errno = errorNumber;
273         JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", msg);
274         break;
275     }
276 }
277 
278 
279 jfieldID
NET_GetFileDescriptorID(JNIEnv * env)280 NET_GetFileDescriptorID(JNIEnv *env)
281 {
282     jclass cls = (*env)->FindClass(env, "java/io/FileDescriptor");
283     CHECK_NULL_RETURN(cls, NULL);
284     return (*env)->GetFieldID(env, cls, "fd", "I");
285 }
286 
287 #if defined(DONT_ENABLE_IPV6) || defined(__DragonFly__)
IPv6_supported()288 jint  IPv6_supported()
289 {
290     return JNI_FALSE;
291 }
292 
293 #else /* !DONT_ENABLE_IPV6 */
294 
IPv6_supported()295 jint  IPv6_supported()
296 {
297     int fd;
298     void *ipv6_fn;
299     SOCKETADDRESS sa;
300     socklen_t sa_len = sizeof(SOCKETADDRESS);
301 
302     fd = socket(AF_INET6, SOCK_STREAM, 0) ;
303     if (fd < 0) {
304         /*
305          *  TODO: We really cant tell since it may be an unrelated error
306          *  for now we will assume that AF_INET6 is not available
307          */
308         return JNI_FALSE;
309     }
310 
311     /*
312      * If fd 0 is a socket it means we may have been launched from inetd or
313      * xinetd. If it's a socket then check the family - if it's an
314      * IPv4 socket then we need to disable IPv6.
315      */
316     if (getsockname(0, &sa.sa, &sa_len) == 0) {
317         if (sa.sa.sa_family == AF_INET) {
318             close(fd);
319             return JNI_FALSE;
320         }
321     }
322 
323     /**
324      * Linux - check if any interface has an IPv6 address.
325      * Don't need to parse the line - we just need an indication.
326      */
327 #ifdef __linux__
328     {
329         FILE *fP = fopen("/proc/net/if_inet6", "r");
330         char buf[255];
331         char *bufP;
332 
333         if (fP == NULL) {
334             close(fd);
335             return JNI_FALSE;
336         }
337         bufP = fgets(buf, sizeof(buf), fP);
338         fclose(fP);
339         if (bufP == NULL) {
340             close(fd);
341             return JNI_FALSE;
342         }
343     }
344 #endif
345 
346     /**
347      * On Solaris 8 it's possible to create INET6 sockets even
348      * though IPv6 is not enabled on all interfaces. Thus we
349      * query the number of IPv6 addresses to verify that IPv6
350      * has been configured on at least one interface.
351      *
352      * On Linux it doesn't matter - if IPv6 is built-in the
353      * kernel then IPv6 addresses will be bound automatically
354      * to all interfaces.
355      */
356 #ifdef __solaris__
357 
358 #ifdef SIOCGLIFNUM
359     {
360         struct lifnum numifs;
361 
362         numifs.lifn_family = AF_INET6;
363         numifs.lifn_flags = 0;
364         if (ioctl(fd, SIOCGLIFNUM, (char *)&numifs) < 0) {
365             /**
366              * SIOCGLIFNUM failed - assume IPv6 not configured
367              */
368             close(fd);
369             return JNI_FALSE;
370         }
371         /**
372          * If no IPv6 addresses then return false. If count > 0
373          * it's possible that all IPv6 addresses are "down" but
374          * that's okay as they may be brought "up" while the
375          * VM is running.
376          */
377         if (numifs.lifn_count == 0) {
378             close(fd);
379             return JNI_FALSE;
380         }
381     }
382 #else
383     /* SIOCGLIFNUM not defined in build environment ??? */
384     close(fd);
385     return JNI_FALSE;
386 #endif
387 
388 #endif /* __solaris */
389 
390     /*
391      *  OK we may have the stack available in the kernel,
392      *  we should also check if the APIs are available.
393      */
394     ipv6_fn = JVM_FindLibraryEntry(RTLD_DEFAULT, "inet_pton");
395     close(fd);
396     if (ipv6_fn == NULL ) {
397         return JNI_FALSE;
398     } else {
399         return JNI_TRUE;
400     }
401 }
402 #endif /* DONT_ENABLE_IPV6 */
403 
reuseport_supported()404 jint reuseport_supported()
405 {
406     /* Do a simple dummy call, and try to figure out from that */
407     int one = 1;
408     int rv, s;
409     s = socket(PF_INET, SOCK_STREAM, 0);
410     if (s < 0) {
411         return JNI_FALSE;
412     }
413     rv = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (void *)&one, sizeof(one));
414     if (rv != 0) {
415         rv = JNI_FALSE;
416     } else {
417         rv = JNI_TRUE;
418     }
419     close(s);
420     return rv;
421 }
422 
NET_ThrowUnknownHostExceptionWithGaiError(JNIEnv * env,const char * hostname,int gai_error)423 void NET_ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
424                                                const char* hostname,
425                                                int gai_error)
426 {
427     int size;
428     char *buf;
429     const char *format = "%s: %s";
430     const char *error_string = gai_strerror(gai_error);
431     if (error_string == NULL)
432         error_string = "unknown error";
433 
434     size = strlen(format) + strlen(hostname) + strlen(error_string) + 2;
435     buf = (char *) malloc(size);
436     if (buf) {
437         jstring s;
438         sprintf(buf, format, hostname, error_string);
439         s = JNU_NewStringPlatform(env, buf);
440         if (s != NULL) {
441             jobject x = JNU_NewObjectByName(env,
442                                             "java/net/UnknownHostException",
443                                             "(Ljava/lang/String;)V", s);
444             if (x != NULL)
445                 (*env)->Throw(env, x);
446         }
447         free(buf);
448     }
449 }
450 
451 #if defined(__linux__)
452 
453 /* following code creates a list of addresses from the kernel
454  * routing table that are routed via the loopback address.
455  * We check all destination addresses against this table
456  * and override the scope_id field to use the relevant value for "lo"
457  * in order to work-around the Linux bug that prevents packets destined
458  * for certain local addresses from being sent via a physical interface.
459  */
460 
461 struct loopback_route {
462     struct in6_addr addr; /* destination address */
463     int plen; /* prefix length */
464 };
465 
466 static struct loopback_route *loRoutes = 0;
467 static int nRoutes = 0; /* number of routes */
468 static int loRoutes_size = 16; /* initial size */
469 static int lo_scope_id = 0;
470 
471 static void initLoopbackRoutes();
472 
printAddr(struct in6_addr * addr)473 void printAddr (struct in6_addr *addr) {
474     int i;
475     for (i=0; i<16; i++) {
476         printf ("%02x", addr->s6_addr[i]);
477     }
478     printf ("\n");
479 }
480 
needsLoopbackRoute(struct in6_addr * dest_addr)481 static jboolean needsLoopbackRoute (struct in6_addr* dest_addr) {
482     int byte_count;
483     int extra_bits, i;
484     struct loopback_route *ptr;
485 
486     if (loRoutes == 0) {
487         initLoopbackRoutes();
488     }
489 
490     for (ptr = loRoutes, i=0; i<nRoutes; i++, ptr++) {
491         struct in6_addr *target_addr=&ptr->addr;
492         int dest_plen = ptr->plen;
493         byte_count = dest_plen >> 3;
494         extra_bits = dest_plen & 0x3;
495 
496         if (byte_count > 0) {
497             if (memcmp(target_addr, dest_addr, byte_count)) {
498                 continue;  /* no match */
499             }
500         }
501 
502         if (extra_bits > 0) {
503             unsigned char c1 = ((unsigned char *)target_addr)[byte_count];
504             unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count];
505             unsigned char mask = 0xff << (8 - extra_bits);
506             if ((c1 & mask) != (c2 & mask)) {
507                 continue;
508             }
509         }
510         return JNI_TRUE;
511     }
512     return JNI_FALSE;
513 }
514 
515 
initLoopbackRoutes()516 static void initLoopbackRoutes() {
517     FILE *f;
518     char srcp[8][5];
519     char hopp[8][5];
520     int dest_plen, src_plen, use, refcnt, metric;
521     unsigned long flags;
522     char dest_str[40];
523     struct in6_addr dest_addr;
524     char device[16];
525     struct loopback_route *loRoutesTemp;
526 
527     if (loRoutes != 0) {
528         free (loRoutes);
529     }
530     loRoutes = calloc (loRoutes_size, sizeof(struct loopback_route));
531     if (loRoutes == 0) {
532         return;
533     }
534     /*
535      * Scan /proc/net/ipv6_route looking for a matching
536      * route.
537      */
538     if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
539         return ;
540     }
541     while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
542                      "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
543                      "%4s%4s%4s%4s%4s%4s%4s%4s "
544                      "%08x %08x %08x %08lx %8s",
545                      dest_str, &dest_str[5], &dest_str[10], &dest_str[15],
546                      &dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35],
547                      &dest_plen,
548                      srcp[0], srcp[1], srcp[2], srcp[3],
549                      srcp[4], srcp[5], srcp[6], srcp[7],
550                      &src_plen,
551                      hopp[0], hopp[1], hopp[2], hopp[3],
552                      hopp[4], hopp[5], hopp[6], hopp[7],
553                      &metric, &use, &refcnt, &flags, device) == 31) {
554 
555         /*
556          * Some routes should be ignored
557          */
558         if ( (dest_plen < 0 || dest_plen > 128)  ||
559              (src_plen != 0) ||
560              (flags & (RTF_POLICY | RTF_FLOW)) ||
561              ((flags & RTF_REJECT) && dest_plen == 0) ) {
562             continue;
563         }
564 
565         /*
566          * Convert the destination address
567          */
568         dest_str[4] = ':';
569         dest_str[9] = ':';
570         dest_str[14] = ':';
571         dest_str[19] = ':';
572         dest_str[24] = ':';
573         dest_str[29] = ':';
574         dest_str[34] = ':';
575         dest_str[39] = '\0';
576 
577         if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) {
578             /* not an Ipv6 address */
579             continue;
580         }
581         if (strcmp(device, "lo") != 0) {
582             /* Not a loopback route */
583             continue;
584         } else {
585             if (nRoutes == loRoutes_size) {
586                 loRoutesTemp = realloc (loRoutes, loRoutes_size *
587                                         sizeof (struct loopback_route) * 2);
588 
589                 if (loRoutesTemp == 0) {
590                     free(loRoutes);
591                     loRoutes = NULL;
592                     nRoutes = 0;
593                     fclose (f);
594                     return;
595                 }
596                 loRoutes=loRoutesTemp;
597                 loRoutes_size *= 2;
598             }
599             memcpy (&loRoutes[nRoutes].addr,&dest_addr,sizeof(struct in6_addr));
600             loRoutes[nRoutes].plen = dest_plen;
601             nRoutes ++;
602         }
603     }
604 
605     fclose (f);
606     {
607         /* now find the scope_id for "lo" */
608 
609         char devname[21];
610         char addr6p[8][5];
611         int plen, scope, dad_status, if_idx;
612 
613         if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
614             while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
615                       addr6p[0], addr6p[1], addr6p[2], addr6p[3],
616                       addr6p[4], addr6p[5], addr6p[6], addr6p[7],
617                   &if_idx, &plen, &scope, &dad_status, devname) == 13) {
618 
619                 if (strcmp(devname, "lo") == 0) {
620                     /*
621                      * Found - so just return the index
622                      */
623                     fclose(f);
624                     lo_scope_id = if_idx;
625                     return;
626                 }
627             }
628             fclose(f);
629         }
630     }
631 }
632 
633 /*
634  * Following is used for binding to local addresses. Equivalent
635  * to code above, for bind().
636  */
637 
638 struct localinterface {
639     int index;
640     char localaddr [16];
641 };
642 
643 static struct localinterface *localifs = 0;
644 static int localifsSize = 0;    /* size of array */
645 static int nifs = 0;            /* number of entries used in array */
646 
647 /* not thread safe: make sure called once from one thread */
648 
initLocalIfs()649 static void initLocalIfs () {
650     FILE *f;
651     unsigned char staddr [16];
652     char ifname [33];
653     struct localinterface *lif=0;
654     int index, x1, x2, x3;
655     unsigned int u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,ua,ub,uc,ud,ue,uf;
656 
657     if ((f = fopen("/proc/net/if_inet6", "r")) == NULL) {
658         return ;
659     }
660     while (fscanf (f, "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x "
661                 "%d %x %x %x %32s",&u0,&u1,&u2,&u3,&u4,&u5,&u6,&u7,
662                 &u8,&u9,&ua,&ub,&uc,&ud,&ue,&uf,
663                 &index, &x1, &x2, &x3, ifname) == 21) {
664         staddr[0] = (unsigned char)u0;
665         staddr[1] = (unsigned char)u1;
666         staddr[2] = (unsigned char)u2;
667         staddr[3] = (unsigned char)u3;
668         staddr[4] = (unsigned char)u4;
669         staddr[5] = (unsigned char)u5;
670         staddr[6] = (unsigned char)u6;
671         staddr[7] = (unsigned char)u7;
672         staddr[8] = (unsigned char)u8;
673         staddr[9] = (unsigned char)u9;
674         staddr[10] = (unsigned char)ua;
675         staddr[11] = (unsigned char)ub;
676         staddr[12] = (unsigned char)uc;
677         staddr[13] = (unsigned char)ud;
678         staddr[14] = (unsigned char)ue;
679         staddr[15] = (unsigned char)uf;
680         nifs ++;
681         if (nifs > localifsSize) {
682             localifs = (struct localinterface *) realloc (
683                         localifs, sizeof (struct localinterface)* (localifsSize+5));
684             if (localifs == 0) {
685                 nifs = 0;
686                 fclose (f);
687                 return;
688             }
689             lif = localifs + localifsSize;
690             localifsSize += 5;
691         } else {
692             lif ++;
693         }
694         memcpy (lif->localaddr, staddr, 16);
695         lif->index = index;
696     }
697     fclose (f);
698 }
699 
700 /* return the scope_id (interface index) of the
701  * interface corresponding to the given address
702  * returns 0 if no match found
703  */
704 
getLocalScopeID(char * addr)705 static int getLocalScopeID (char *addr) {
706     struct localinterface *lif;
707     int i;
708     if (localifs == 0) {
709         initLocalIfs();
710     }
711     for (i=0, lif=localifs; i<nifs; i++, lif++) {
712         if (memcmp (addr, lif->localaddr, 16) == 0) {
713             return lif->index;
714         }
715     }
716     return 0;
717 }
718 
platformInit()719 void platformInit () {
720     initLoopbackRoutes();
721     initLocalIfs();
722 }
723 
724 #elif defined(_AIX)
725 
726 /* Initialize stubs for blocking I/O workarounds (see src/solaris/native/java/net/linux_close.c) */
727 extern void aix_close_init();
728 
platformInit()729 void platformInit () {
730     aix_close_init();
731 }
732 
733 #else
734 
platformInit()735 void platformInit () {}
736 
737 #endif
738 
parseExclusiveBindProperty(JNIEnv * env)739 void parseExclusiveBindProperty(JNIEnv *env) {
740 #ifdef __solaris__
741     jstring s, flagSet;
742     jclass iCls;
743     jmethodID mid;
744 
745     s = (*env)->NewStringUTF(env, "sun.net.useExclusiveBind");
746     CHECK_NULL(s);
747     iCls = (*env)->FindClass(env, "java/lang/System");
748     CHECK_NULL(iCls);
749     mid = (*env)->GetStaticMethodID(env, iCls, "getProperty",
750                 "(Ljava/lang/String;)Ljava/lang/String;");
751     CHECK_NULL(mid);
752     flagSet = (*env)->CallStaticObjectMethod(env, iCls, mid, s);
753     if (flagSet != NULL) {
754         useExclBind = 1;
755     }
756 #endif
757 }
758 
759 JNIEXPORT jint JNICALL
NET_EnableFastTcpLoopback(int fd)760 NET_EnableFastTcpLoopback(int fd) {
761     return 0;
762 }
763 
764 /**
765  * See net_util.h for documentation
766  */
767 JNIEXPORT int JNICALL
NET_InetAddressToSockaddr(JNIEnv * env,jobject iaObj,int port,SOCKETADDRESS * sa,int * len,jboolean v4MappedAddress)768 NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port,
769                           SOCKETADDRESS *sa, int *len,
770                           jboolean v4MappedAddress)
771 {
772     jint family = getInetAddress_family(env, iaObj);
773     JNU_CHECK_EXCEPTION_RETURN(env, -1);
774     memset((char *)sa, 0, sizeof(SOCKETADDRESS));
775 
776     if (ipv6_available() &&
777         !(family == java_net_InetAddress_IPv4 &&
778           v4MappedAddress == JNI_FALSE))
779     {
780         jbyte caddr[16];
781         jint address;
782 
783         if (family == java_net_InetAddress_IPv4) {
784             // convert to IPv4-mapped address
785             memset((char *)caddr, 0, 16);
786             address = getInetAddress_addr(env, iaObj);
787             JNU_CHECK_EXCEPTION_RETURN(env, -1);
788             if (address == INADDR_ANY) {
789                 /* we would always prefer IPv6 wildcard address
790                  * caddr[10] = 0xff;
791                  * caddr[11] = 0xff; */
792             } else {
793                 caddr[10] = 0xff;
794                 caddr[11] = 0xff;
795                 caddr[12] = ((address >> 24) & 0xff);
796                 caddr[13] = ((address >> 16) & 0xff);
797                 caddr[14] = ((address >> 8) & 0xff);
798                 caddr[15] = (address & 0xff);
799             }
800         } else {
801             getInet6Address_ipaddress(env, iaObj, (char *)caddr);
802         }
803         sa->sa6.sin6_port = htons(port);
804         memcpy((void *)&sa->sa6.sin6_addr, caddr, sizeof(struct in6_addr));
805         sa->sa6.sin6_family = AF_INET6;
806         if (len != NULL) {
807             *len = sizeof(struct sockaddr_in6);
808         }
809 
810 #ifdef __linux__
811         /*
812          * On Linux if we are connecting to a link-local address
813          * we need to specify the interface in the scope_id (2.4 kernel only)
814          *
815          * If the scope was cached then we use the cached value. If not cached but
816          * specified in the Inet6Address we use that, but we first check if the
817          * address needs to be routed via the loopback interface. In this case,
818          * we override the specified value with that of the loopback interface.
819          * If no cached value exists and no value was specified by user, then
820          * we try to determine a value from the routing table. In all these
821          * cases the used value is cached for further use.
822          */
823         if (IN6_IS_ADDR_LINKLOCAL(&sa->sa6.sin6_addr)) {
824             unsigned int cached_scope_id = 0, scope_id = 0;
825 
826             if (ia6_cachedscopeidID) {
827                 cached_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID);
828                 /* if cached value exists then use it. Otherwise, check
829                  * if scope is set in the address.
830                  */
831                 if (!cached_scope_id) {
832                     if (ia6_scopeidID) {
833                         scope_id = getInet6Address_scopeid(env, iaObj);
834                     }
835                     if (scope_id != 0) {
836                         /* check user-specified value for loopback case
837                          * that needs to be overridden
838                          */
839                         if (kernelIsV24() && needsLoopbackRoute(&sa->sa6.sin6_addr)) {
840                             cached_scope_id = lo_scope_id;
841                             (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
842                         }
843                     } else {
844                         /*
845                          * Otherwise consult the IPv6 routing tables to
846                          * try determine the appropriate interface.
847                          */
848                         if (kernelIsV24()) {
849                             cached_scope_id = getDefaultIPv6Interface(&sa->sa6.sin6_addr);
850                         } else {
851                             cached_scope_id = getLocalScopeID((char *)&(sa->sa6.sin6_addr));
852                             if (cached_scope_id == 0) {
853                                 cached_scope_id = getDefaultIPv6Interface(&sa->sa6.sin6_addr);
854                             }
855                         }
856                         (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
857                     }
858                 }
859             }
860 
861             /*
862              * If we have a scope_id use the extended form
863              * of sockaddr_in6.
864              */
865             sa->sa6.sin6_scope_id = cached_scope_id == 0 ? scope_id : cached_scope_id;
866         }
867 #else
868         /* handle scope_id */
869         if (family != java_net_InetAddress_IPv4) {
870             if (ia6_scopeidID) {
871                 sa->sa6.sin6_scope_id = getInet6Address_scopeid(env, iaObj);
872             }
873         }
874 #endif
875     } else {
876         jint address;
877         if (family != java_net_InetAddress_IPv4) {
878             JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
879             return -1;
880         }
881         address = getInetAddress_addr(env, iaObj);
882         JNU_CHECK_EXCEPTION_RETURN(env, -1);
883         sa->sa4.sin_port = htons(port);
884         sa->sa4.sin_addr.s_addr = htonl(address);
885         sa->sa4.sin_family = AF_INET;
886         if (len != NULL) {
887             *len = sizeof(struct sockaddr_in);
888         }
889     }
890     return 0;
891 }
892 
893 void
NET_SetTrafficClass(SOCKETADDRESS * sa,int trafficClass)894 NET_SetTrafficClass(SOCKETADDRESS *sa, int trafficClass) {
895     if (sa->sa.sa_family == AF_INET6) {
896         sa->sa6.sin6_flowinfo = htonl((trafficClass & 0xff) << 20);
897     }
898 }
899 
900 int
NET_IsIPv4Mapped(jbyte * caddr)901 NET_IsIPv4Mapped(jbyte* caddr) {
902     int i;
903     for (i = 0; i < 10; i++) {
904         if (caddr[i] != 0x00) {
905             return 0; /* false */
906         }
907     }
908 
909     if (((caddr[10] & 0xff) == 0xff) && ((caddr[11] & 0xff) == 0xff)) {
910         return 1; /* true */
911     }
912     return 0; /* false */
913 }
914 
915 int
NET_IPv4MappedToIPv4(jbyte * caddr)916 NET_IPv4MappedToIPv4(jbyte* caddr) {
917     return ((caddr[12] & 0xff) << 24) | ((caddr[13] & 0xff) << 16) | ((caddr[14] & 0xff) << 8)
918         | (caddr[15] & 0xff);
919 }
920 
921 int
NET_IsEqual(jbyte * caddr1,jbyte * caddr2)922 NET_IsEqual(jbyte* caddr1, jbyte* caddr2) {
923     int i;
924     for (i = 0; i < 16; i++) {
925         if (caddr1[i] != caddr2[i]) {
926             return 0; /* false */
927         }
928     }
929     return 1;
930 }
931 
NET_IsZeroAddr(jbyte * caddr)932 int NET_IsZeroAddr(jbyte* caddr) {
933     int i;
934     for (i = 0; i < 16; i++) {
935         if (caddr[i] != 0) {
936             return 0;
937         }
938     }
939     return 1;
940 }
941 
942 /*
943  * Map the Java level socket option to the platform specific
944  * level and option name.
945  */
946 int
NET_MapSocketOption(jint cmd,int * level,int * optname)947 NET_MapSocketOption(jint cmd, int *level, int *optname) {
948     static struct {
949         jint cmd;
950         int level;
951         int optname;
952     } const opts[] = {
953         { java_net_SocketOptions_TCP_NODELAY,           IPPROTO_TCP,    TCP_NODELAY },
954         { java_net_SocketOptions_SO_OOBINLINE,          SOL_SOCKET,     SO_OOBINLINE },
955         { java_net_SocketOptions_SO_LINGER,             SOL_SOCKET,     SO_LINGER },
956         { java_net_SocketOptions_SO_SNDBUF,             SOL_SOCKET,     SO_SNDBUF },
957         { java_net_SocketOptions_SO_RCVBUF,             SOL_SOCKET,     SO_RCVBUF },
958         { java_net_SocketOptions_SO_KEEPALIVE,          SOL_SOCKET,     SO_KEEPALIVE },
959         { java_net_SocketOptions_SO_REUSEADDR,          SOL_SOCKET,     SO_REUSEADDR },
960         { java_net_SocketOptions_SO_REUSEPORT,          SOL_SOCKET,     SO_REUSEPORT },
961         { java_net_SocketOptions_SO_BROADCAST,          SOL_SOCKET,     SO_BROADCAST },
962         { java_net_SocketOptions_IP_TOS,                IPPROTO_IP,     IP_TOS },
963         { java_net_SocketOptions_IP_MULTICAST_IF,       IPPROTO_IP,     IP_MULTICAST_IF },
964         { java_net_SocketOptions_IP_MULTICAST_IF2,      IPPROTO_IP,     IP_MULTICAST_IF },
965         { java_net_SocketOptions_IP_MULTICAST_LOOP,     IPPROTO_IP,     IP_MULTICAST_LOOP },
966     };
967 
968     int i;
969 
970     if (ipv6_available()) {
971         switch (cmd) {
972             // Different multicast options if IPv6 is enabled
973             case java_net_SocketOptions_IP_MULTICAST_IF:
974             case java_net_SocketOptions_IP_MULTICAST_IF2:
975                 *level = IPPROTO_IPV6;
976                 *optname = IPV6_MULTICAST_IF;
977                 return 0;
978 
979             case java_net_SocketOptions_IP_MULTICAST_LOOP:
980                 *level = IPPROTO_IPV6;
981                 *optname = IPV6_MULTICAST_LOOP;
982                 return 0;
983 #if (defined(__solaris__) || defined(MACOSX))
984             // Map IP_TOS request to IPV6_TCLASS
985             case java_net_SocketOptions_IP_TOS:
986                 *level = IPPROTO_IPV6;
987                 *optname = IPV6_TCLASS;
988                 return 0;
989 #endif
990         }
991     }
992 
993     /*
994      * Map the Java level option to the native level
995      */
996     for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) {
997         if (cmd == opts[i].cmd) {
998             *level = opts[i].level;
999             *optname = opts[i].optname;
1000             return 0;
1001         }
1002     }
1003 
1004     /* not found */
1005     return -1;
1006 }
1007 
1008 /*
1009  * Determine the default interface for an IPv6 address.
1010  *
1011  * 1. Scans /proc/net/ipv6_route for a matching route
1012  *    (eg: fe80::/10 or a route for the specific address).
1013  *    This will tell us the interface to use (eg: "eth0").
1014  *
1015  * 2. Lookup /proc/net/if_inet6 to map the interface
1016  *    name to an interface index.
1017  *
1018  * Returns :-
1019  *      -1 if error
1020  *       0 if no matching interface
1021  *      >1 interface index to use for the link-local address.
1022  */
1023 #if defined(__linux__)
getDefaultIPv6Interface(struct in6_addr * target_addr)1024 int getDefaultIPv6Interface(struct in6_addr *target_addr) {
1025     FILE *f;
1026     char srcp[8][5];
1027     char hopp[8][5];
1028     int dest_plen, src_plen, use, refcnt, metric;
1029     unsigned long flags;
1030     char dest_str[40];
1031     struct in6_addr dest_addr;
1032     char device[16];
1033     jboolean match = JNI_FALSE;
1034 
1035     /*
1036      * Scan /proc/net/ipv6_route looking for a matching
1037      * route.
1038      */
1039     if ((f = fopen("/proc/net/ipv6_route", "r")) == NULL) {
1040         return -1;
1041     }
1042     while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
1043                      "%4s%4s%4s%4s%4s%4s%4s%4s %02x "
1044                      "%4s%4s%4s%4s%4s%4s%4s%4s "
1045                      "%08x %08x %08x %08lx %8s",
1046                      dest_str, &dest_str[5], &dest_str[10], &dest_str[15],
1047                      &dest_str[20], &dest_str[25], &dest_str[30], &dest_str[35],
1048                      &dest_plen,
1049                      srcp[0], srcp[1], srcp[2], srcp[3],
1050                      srcp[4], srcp[5], srcp[6], srcp[7],
1051                      &src_plen,
1052                      hopp[0], hopp[1], hopp[2], hopp[3],
1053                      hopp[4], hopp[5], hopp[6], hopp[7],
1054                      &metric, &use, &refcnt, &flags, device) == 31) {
1055 
1056         /*
1057          * Some routes should be ignored
1058          */
1059         if ( (dest_plen < 0 || dest_plen > 128)  ||
1060              (src_plen != 0) ||
1061              (flags & (RTF_POLICY | RTF_FLOW)) ||
1062              ((flags & RTF_REJECT) && dest_plen == 0) ) {
1063             continue;
1064         }
1065 
1066         /*
1067          * Convert the destination address
1068          */
1069         dest_str[4] = ':';
1070         dest_str[9] = ':';
1071         dest_str[14] = ':';
1072         dest_str[19] = ':';
1073         dest_str[24] = ':';
1074         dest_str[29] = ':';
1075         dest_str[34] = ':';
1076         dest_str[39] = '\0';
1077 
1078         if (inet_pton(AF_INET6, dest_str, &dest_addr) < 0) {
1079             /* not an Ipv6 address */
1080             continue;
1081         } else {
1082             /*
1083              * The prefix len (dest_plen) indicates the number of bits we
1084              * need to match on.
1085              *
1086              * dest_plen / 8    => number of bytes to match
1087              * dest_plen % 8    => number of additional bits to match
1088              *
1089              * eg: fe80::/10 => match 1 byte + 2 additional bits in the
1090              *                  next byte.
1091              */
1092             int byte_count = dest_plen >> 3;
1093             int extra_bits = dest_plen & 0x3;
1094 
1095             if (byte_count > 0) {
1096                 if (memcmp(target_addr, &dest_addr, byte_count)) {
1097                     continue;  /* no match */
1098                 }
1099             }
1100 
1101             if (extra_bits > 0) {
1102                 unsigned char c1 = ((unsigned char *)target_addr)[byte_count];
1103                 unsigned char c2 = ((unsigned char *)&dest_addr)[byte_count];
1104                 unsigned char mask = 0xff << (8 - extra_bits);
1105                 if ((c1 & mask) != (c2 & mask)) {
1106                     continue;
1107                 }
1108             }
1109 
1110             /*
1111              * We have a match
1112              */
1113             match = JNI_TRUE;
1114             break;
1115         }
1116     }
1117     fclose(f);
1118 
1119     /*
1120      * If there's a match then we lookup the interface
1121      * index.
1122      */
1123     if (match) {
1124         char devname[21];
1125         char addr6p[8][5];
1126         int plen, scope, dad_status, if_idx;
1127 
1128         if ((f = fopen("/proc/net/if_inet6", "r")) != NULL) {
1129             while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
1130                       addr6p[0], addr6p[1], addr6p[2], addr6p[3],
1131                       addr6p[4], addr6p[5], addr6p[6], addr6p[7],
1132                   &if_idx, &plen, &scope, &dad_status, devname) == 13) {
1133 
1134                 if (strcmp(devname, device) == 0) {
1135                     /*
1136                      * Found - so just return the index
1137                      */
1138                     fclose(f);
1139                     return if_idx;
1140                 }
1141             }
1142             fclose(f);
1143         } else {
1144             /*
1145              * Couldn't open /proc/net/if_inet6
1146              */
1147             return -1;
1148         }
1149     }
1150 
1151     /*
1152      * If we get here it means we didn't there wasn't any
1153      * route or we couldn't get the index of the interface.
1154      */
1155     return 0;
1156 }
1157 #endif
1158 
1159 
1160 /*
1161  * Wrapper for getsockopt system routine - does any necessary
1162  * pre/post processing to deal with OS specific oddities :-
1163  *
1164  * On Linux the SO_SNDBUF/SO_RCVBUF values must be post-processed
1165  * to compensate for an incorrect value returned by the kernel.
1166  */
1167 int
NET_GetSockOpt(int fd,int level,int opt,void * result,int * len)1168 NET_GetSockOpt(int fd, int level, int opt, void *result,
1169                int *len)
1170 {
1171     int rv;
1172     socklen_t socklen = *len;
1173 
1174     rv = getsockopt(fd, level, opt, result, &socklen);
1175     *len = socklen;
1176 
1177     if (rv < 0) {
1178         return rv;
1179     }
1180 
1181 #ifdef __linux__
1182     /*
1183      * On Linux SO_SNDBUF/SO_RCVBUF aren't symmetric. This
1184      * stems from additional socket structures in the send
1185      * and receive buffers.
1186      */
1187     if ((level == SOL_SOCKET) && ((opt == SO_SNDBUF)
1188                                   || (opt == SO_RCVBUF))) {
1189         int n = *((int *)result);
1190         n /= 2;
1191         *((int *)result) = n;
1192     }
1193 #endif
1194 
1195 /* Workaround for Mac OS treating linger value as
1196  *  signed integer
1197  */
1198 #ifdef MACOSX
1199     if (level == SOL_SOCKET && opt == SO_LINGER) {
1200         struct linger* to_cast = (struct linger*)result;
1201         to_cast->l_linger = (unsigned short)to_cast->l_linger;
1202     }
1203 #endif
1204     return rv;
1205 }
1206 
1207 /*
1208  * Wrapper for setsockopt system routine - performs any
1209  * necessary pre/post processing to deal with OS specific
1210  * issue :-
1211  *
1212  * On Solaris need to limit the suggested value for SO_SNDBUF
1213  * and SO_RCVBUF to the kernel configured limit
1214  *
1215  * For IP_TOS socket option need to mask off bits as this
1216  * aren't automatically masked by the kernel and results in
1217  * an error.
1218  */
1219 int
NET_SetSockOpt(int fd,int level,int opt,const void * arg,int len)1220 NET_SetSockOpt(int fd, int level, int  opt, const void *arg,
1221                int len)
1222 {
1223 
1224 #ifndef IPTOS_TOS_MASK
1225 #define IPTOS_TOS_MASK 0x1e
1226 #endif
1227 #ifndef IPTOS_PREC_MASK
1228 #define IPTOS_PREC_MASK 0xe0
1229 #endif
1230 
1231 #if defined(_ALLBSD_SOURCE)
1232 #if defined(KIPC_MAXSOCKBUF)
1233     int mib[3];
1234     size_t rlen;
1235 #endif
1236 
1237     int *bufsize;
1238 
1239 #ifdef __APPLE__
1240     static int maxsockbuf = -1;
1241 #else
1242     static long maxsockbuf = -1;
1243 #endif
1244 #endif
1245 
1246     /*
1247      * IPPROTO/IP_TOS :-
1248      * 1. IPv6 on Solaris/Mac OS:
1249      *    Set the TOS OR Traffic Class value to cater for
1250      *    IPv6 and IPv4 scenarios.
1251      * 2. IPv6 on Linux: By default Linux ignores flowinfo
1252      *    field so enable IPV6_FLOWINFO_SEND so that flowinfo
1253      *    will be examined. We also set the IPv4 TOS option in this case.
1254      * 3. IPv4: set socket option based on ToS and Precedence
1255      *    fields (otherwise get invalid argument)
1256      */
1257     if (level == IPPROTO_IP && opt == IP_TOS) {
1258         int *iptos;
1259 
1260 #if defined(__linux__)
1261         if (ipv6_available()) {
1262             int optval = 1;
1263             if (setsockopt(fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND,
1264                            (void *)&optval, sizeof(optval)) < 0) {
1265                 return -1;
1266             }
1267            /*
1268             * Let's also set the IPV6_TCLASS flag.
1269             * Linux appears to allow both IP_TOS and IPV6_TCLASS to be set
1270             * This helps in mixed environments where IPv4 and IPv6 sockets
1271             * are connecting.
1272             */
1273            if (setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS,
1274                            arg, len) < 0) {
1275                 return -1;
1276             }
1277         }
1278 #endif
1279 
1280         iptos = (int *)arg;
1281         *iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
1282     }
1283 
1284     /*
1285      * SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On Solaris we may need to clamp
1286      * the value when it exceeds the system limit.
1287      */
1288 #ifdef __solaris__
1289     if (level == SOL_SOCKET) {
1290         if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
1291             int sotype=0;
1292             socklen_t arglen;
1293             int *bufsize, maxbuf;
1294             int ret;
1295 
1296             /* Attempt with the original size */
1297             ret = setsockopt(fd, level, opt, arg, len);
1298             if ((ret == 0) || (ret == -1 && errno != ENOBUFS))
1299                 return ret;
1300 
1301             /* Exceeded system limit so clamp and retry */
1302 
1303             arglen = sizeof(sotype);
1304             if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype,
1305                            &arglen) < 0) {
1306                 return -1;
1307             }
1308 
1309             /*
1310              * We try to get tcp_maxbuf (and udp_max_buf) using
1311              * an ioctl() that isn't available on all versions of Solaris.
1312              * If that fails, we use the search algorithm in findMaxBuf()
1313              */
1314             if (!init_tcp_max_buf && sotype == SOCK_STREAM) {
1315                 tcp_max_buf = net_getParam("/dev/tcp", "tcp_max_buf");
1316                 if (tcp_max_buf == -1) {
1317                     tcp_max_buf = findMaxBuf(fd, opt, SOCK_STREAM);
1318                     if (tcp_max_buf == -1) {
1319                         return -1;
1320                     }
1321                 }
1322                 init_tcp_max_buf = 1;
1323             } else if (!init_udp_max_buf && sotype == SOCK_DGRAM) {
1324                 udp_max_buf = net_getParam("/dev/udp", "udp_max_buf");
1325                 if (udp_max_buf == -1) {
1326                     udp_max_buf = findMaxBuf(fd, opt, SOCK_DGRAM);
1327                     if (udp_max_buf == -1) {
1328                         return -1;
1329                     }
1330                 }
1331                 init_udp_max_buf = 1;
1332             }
1333 
1334             maxbuf = (sotype == SOCK_STREAM) ? tcp_max_buf : udp_max_buf;
1335             bufsize = (int *)arg;
1336             if (*bufsize > maxbuf) {
1337                 *bufsize = maxbuf;
1338             }
1339         }
1340     }
1341 #endif
1342 
1343 #ifdef _AIX
1344     if (level == SOL_SOCKET) {
1345         if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
1346             /*
1347              * Just try to set the requested size. If it fails we will leave the
1348              * socket option as is. Setting the buffer size means only a hint in
1349              * the jse2/java software layer, see javadoc. In the previous
1350              * solution the buffer has always been truncated to a length of
1351              * 0x100000 Byte, even if the technical limit has not been reached.
1352              * This kind of absolute truncation was unexpected in the jck tests.
1353              */
1354             int ret = setsockopt(fd, level, opt, arg, len);
1355             if ((ret == 0) || (ret == -1 && errno == ENOBUFS)) {
1356                 // Accept failure because of insufficient buffer memory resources.
1357                 return 0;
1358             } else {
1359                 // Deliver all other kinds of errors.
1360                 return ret;
1361             }
1362         }
1363     }
1364 #endif
1365 
1366     /*
1367      * On Linux the receive buffer is used for both socket
1368      * structures and the packet payload. The implication
1369      * is that if SO_RCVBUF is too small then small packets
1370      * must be discarded.
1371      */
1372 #ifdef __linux__
1373     if (level == SOL_SOCKET && opt == SO_RCVBUF) {
1374         int *bufsize = (int *)arg;
1375         if (*bufsize < 1024) {
1376             *bufsize = 1024;
1377         }
1378     }
1379 #endif
1380 
1381 #if defined(_ALLBSD_SOURCE)
1382     /*
1383      * SOL_SOCKET/{SO_SNDBUF,SO_RCVBUF} - On FreeBSD need to
1384      * ensure that value is <= kern.ipc.maxsockbuf as otherwise we get
1385      * an ENOBUFS error.
1386      */
1387     if (level == SOL_SOCKET) {
1388         if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
1389 #ifdef KIPC_MAXSOCKBUF
1390             if (maxsockbuf == -1) {
1391                mib[0] = CTL_KERN;
1392                mib[1] = KERN_IPC;
1393                mib[2] = KIPC_MAXSOCKBUF;
1394                rlen = sizeof(maxsockbuf);
1395                if (sysctl(mib, 3, &maxsockbuf, &rlen, NULL, 0) == -1)
1396                    maxsockbuf = 1024;
1397 
1398 #if 1
1399                /* XXXBSD: This is a hack to workaround mb_max/mb_max_adj
1400                   problem.  It should be removed when kern.ipc.maxsockbuf
1401                   will be real value. */
1402                maxsockbuf = (maxsockbuf/5)*4;
1403 #endif
1404            }
1405 #elif defined(__OpenBSD__)
1406            maxsockbuf = SB_MAX;
1407 #else
1408            maxsockbuf = 64 * 1024;      /* XXX: NetBSD */
1409 #endif
1410 
1411            bufsize = (int *)arg;
1412            if (*bufsize > maxsockbuf) {
1413                *bufsize = maxsockbuf;
1414            }
1415 
1416            if (opt == SO_RCVBUF && *bufsize < 1024) {
1417                 *bufsize = 1024;
1418            }
1419 
1420         }
1421     }
1422 #endif
1423 
1424 #if defined(_ALLBSD_SOURCE) || defined(_AIX)
1425     /*
1426      * On Solaris, SO_REUSEADDR will allow multiple datagram
1427      * sockets to bind to the same port. The network jck tests check
1428      * for this "feature", so we need to emulate it by turning on
1429      * SO_REUSEPORT as well for that combination.
1430      */
1431     if (level == SOL_SOCKET && opt == SO_REUSEADDR) {
1432         int sotype;
1433         socklen_t arglen;
1434 
1435         arglen = sizeof(sotype);
1436         if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) < 0) {
1437             return -1;
1438         }
1439 
1440         if (sotype == SOCK_DGRAM) {
1441             setsockopt(fd, level, SO_REUSEPORT, arg, len);
1442         }
1443     }
1444 #endif
1445 
1446     return setsockopt(fd, level, opt, arg, len);
1447 }
1448 
1449 /*
1450  * Wrapper for bind system call - performs any necessary pre/post
1451  * processing to deal with OS specific issues :-
1452  *
1453  * Linux allows a socket to bind to 127.0.0.255 which must be
1454  * caught.
1455  *
1456  * On Solaris with IPv6 enabled we must use an exclusive
1457  * bind to guarantee a unique port number across the IPv4 and
1458  * IPv6 port spaces.
1459  *
1460  */
1461 int
NET_Bind(int fd,SOCKETADDRESS * sa,int len)1462 NET_Bind(int fd, SOCKETADDRESS *sa, int len)
1463 {
1464 #if defined(__solaris__)
1465     int level = -1;
1466     int exclbind = -1;
1467 #endif
1468     int rv;
1469     int arg, alen;
1470 
1471 #ifdef __linux__
1472     /*
1473      * ## get bugId for this issue - goes back to 1.2.2 port ##
1474      * ## When IPv6 is enabled this will be an IPv4-mapped
1475      * ## with family set to AF_INET6
1476      */
1477     if (sa->sa.sa_family == AF_INET) {
1478         if ((ntohl(sa->sa4.sin_addr.s_addr) & 0x7f0000ff) == 0x7f0000ff) {
1479             errno = EADDRNOTAVAIL;
1480             return -1;
1481         }
1482     }
1483 #endif
1484 
1485 #if defined(__solaris__)
1486     /*
1487      * Solaris has separate IPv4 and IPv6 port spaces so we
1488      * use an exclusive bind when SO_REUSEADDR is not used to
1489      * give the illusion of a unified port space.
1490      * This also avoids problems with IPv6 sockets connecting
1491      * to IPv4 mapped addresses whereby the socket conversion
1492      * results in a late bind that fails because the
1493      * corresponding IPv4 port is in use.
1494      */
1495     alen = sizeof(arg);
1496 
1497     if (useExclBind ||
1498         getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&arg, &alen) == 0)
1499     {
1500         if (useExclBind || arg == 0) {
1501             /*
1502              * SO_REUSEADDR is disabled or sun.net.useExclusiveBind
1503              * property is true so enable TCP_EXCLBIND or
1504              * UDP_EXCLBIND
1505              */
1506             alen = sizeof(arg);
1507             if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&arg, &alen) == 0)
1508             {
1509                 if (arg == SOCK_STREAM) {
1510                     level = IPPROTO_TCP;
1511                     exclbind = TCP_EXCLBIND;
1512                 } else {
1513                     level = IPPROTO_UDP;
1514                     exclbind = UDP_EXCLBIND;
1515                 }
1516             }
1517 
1518             arg = 1;
1519             setsockopt(fd, level, exclbind, (char *)&arg, sizeof(arg));
1520         }
1521     }
1522 
1523 #endif
1524 
1525     rv = bind(fd, &sa->sa, len);
1526 
1527 #if defined(__solaris__)
1528     if (rv < 0) {
1529         int en = errno;
1530         /* Restore *_EXCLBIND if the bind fails */
1531         if (exclbind != -1) {
1532             int arg = 0;
1533             setsockopt(fd, level, exclbind, (char *)&arg,
1534                        sizeof(arg));
1535         }
1536         errno = en;
1537     }
1538 #endif
1539 
1540     return rv;
1541 }
1542 
1543 /**
1544  * Wrapper for poll with timeout on a single file descriptor.
1545  *
1546  * flags (defined in net_util_md.h can be any combination of
1547  * NET_WAIT_READ, NET_WAIT_WRITE & NET_WAIT_CONNECT.
1548  *
1549  * The function will return when either the socket is ready for one
1550  * of the specified operations or the timeout expired.
1551  *
1552  * It returns the time left from the timeout (possibly 0), or -1 if it expired.
1553  */
1554 
1555 jint
NET_Wait(JNIEnv * env,jint fd,jint flags,jint timeout)1556 NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
1557 {
1558     jlong prevNanoTime = JVM_NanoTime(env, 0);
1559     jlong nanoTimeout = (jlong) timeout * NET_NSEC_PER_MSEC;
1560     jint read_rv;
1561 
1562     while (1) {
1563         jlong newNanoTime;
1564         struct pollfd pfd;
1565         pfd.fd = fd;
1566         pfd.events = 0;
1567         if (flags & NET_WAIT_READ)
1568           pfd.events |= POLLIN;
1569         if (flags & NET_WAIT_WRITE)
1570           pfd.events |= POLLOUT;
1571         if (flags & NET_WAIT_CONNECT)
1572           pfd.events |= POLLOUT;
1573 
1574         errno = 0;
1575         read_rv = NET_Poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
1576 
1577         newNanoTime = JVM_NanoTime(env, 0);
1578         nanoTimeout -= (newNanoTime - prevNanoTime);
1579         if (nanoTimeout < NET_NSEC_PER_MSEC) {
1580           return read_rv > 0 ? 0 : -1;
1581         }
1582         prevNanoTime = newNanoTime;
1583 
1584         if (read_rv > 0) {
1585           break;
1586         }
1587       } /* while */
1588     return (nanoTimeout / NET_NSEC_PER_MSEC);
1589 }
1590