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