1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef NET_UTILS_MD_H
  27 #define NET_UTILS_MD_H
  28 
  29 #include <sys/socket.h>
  30 #include <sys/types.h>
  31 #include <netdb.h>
  32 #include <netinet/in.h>
  33 #include <unistd.h>
  34 
  35 #ifndef USE_SELECT
  36 #include <sys/poll.h>
  37 #endif
  38 
  39 
  40 /*
  41    AIX needs a workaround for I/O cancellation, see:
  42    http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/close.htm
  43    ...
  44    The close subroutine is blocked until all subroutines which use the file
  45    descriptor return to usr space. For example, when a thread is calling close
  46    and another thread is calling select with the same file descriptor, the
  47    close subroutine does not return until the select call returns.
  48    ...
  49 */
  50 #if defined(__linux__) || defined(MACOSX) || defined (_AIX)
  51 extern int NET_Timeout(int s, long timeout);
  52 extern int NET_Read(int s, void* buf, size_t len);
  53 extern int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
  54        struct sockaddr *from, int *fromlen);
  55 extern int NET_ReadV(int s, const struct iovec * vector, int count);
  56 extern int NET_Send(int s, void *msg, int len, unsigned int flags);
  57 extern int NET_SendTo(int s, const void *msg, int len,  unsigned  int
  58        flags, const struct sockaddr *to, int tolen);
  59 extern int NET_Writev(int s, const struct iovec * vector, int count);
  60 extern int NET_Connect(int s, struct sockaddr *addr, int addrlen);
  61 extern int NET_Accept(int s, struct sockaddr *addr, int *addrlen);
  62 extern int NET_SocketClose(int s);
  63 extern int NET_Dup2(int oldfd, int newfd);
  64 
  65 #ifdef USE_SELECT
  66 extern int NET_Select(int s, fd_set *readfds, fd_set *writefds,
  67                fd_set *exceptfds, struct timeval *timeout);
  68 #else
  69 extern int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout);
  70 #endif
  71 
  72 #else
  73 
  74 #define NET_Timeout     JVM_Timeout
  75 #define NET_Read        JVM_Read
  76 #define NET_RecvFrom    JVM_RecvFrom
  77 #define NET_ReadV       readv
  78 #define NET_Send        JVM_Send
  79 #define NET_SendTo      JVM_SendTo
  80 #define NET_WriteV      writev
  81 #define NET_Connect     JVM_Connect
  82 #define NET_Accept      JVM_Accept
  83 #define NET_SocketClose JVM_SocketClose
  84 #define NET_Dup2        dup2
  85 #define NET_Select      select
  86 #define NET_Poll        poll
  87 
  88 #endif
  89 
  90 #if defined(__linux__) && defined(AF_INET6)
  91 int getDefaultIPv6Interface(struct in6_addr *target_addr);
  92 #endif
  93 
  94 #ifdef __solaris__
  95 extern int net_getParam(char *driver, char *param);
  96 
  97 #ifndef SO_FLOW_SLA
  98 #define SO_FLOW_SLA 0x1018
  99 
 100 #if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
 101 #pragma pack(4)
 102  #endif
 103 
 104 /*
 105  * Used with the setsockopt(SO_FLOW_SLA, ...) call to set
 106  * per socket service level properties.
 107  * When the application uses per-socket API, we will enforce the properties
 108  * on both outbound and inbound packets.
 109  *
 110  * For now, only priority and maxbw are supported in SOCK_FLOW_PROP_VERSION1.
 111  */
 112 typedef struct sock_flow_props_s {
 113         int             sfp_version;
 114         uint32_t        sfp_mask;
 115         int             sfp_priority;   /* flow priority */
 116         uint64_t        sfp_maxbw;      /* bandwidth limit in bps */
 117         int             sfp_status;     /* flow create status for getsockopt */
 118 } sock_flow_props_t;
 119 
 120 #define SOCK_FLOW_PROP_VERSION1 1
 121 
 122 /* bit mask values for sfp_mask */
 123 #define SFP_MAXBW       0x00000001      /* Flow Bandwidth Limit */
 124 #define SFP_PRIORITY    0x00000008      /* Flow priority */
 125 
 126 /* possible values for sfp_priority */
 127 #define SFP_PRIO_NORMAL 1
 128 #define SFP_PRIO_HIGH   2
 129 
 130 #if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
 131 #pragma pack()
 132 #endif /* _LONG_LONG_ALIGNMENT */
 133 
 134 #endif /* SO_FLOW_SLA */
 135 #endif /* __solaris__ */
 136 
 137 void ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
 138                                            const char* hostname,
 139                                            int gai_error);
 140 
 141 #define NET_WAIT_READ   0x01
 142 #define NET_WAIT_WRITE  0x02
 143 #define NET_WAIT_CONNECT        0x04
 144 
 145 extern jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
 146 
 147 /************************************************************************
 148  * Macros and constants
 149  */
 150 
 151 /*
 152  * On 64-bit JDKs we use a much larger stack and heap buffer.
 153  */
 154 #ifdef _LP64
 155 #define MAX_BUFFER_LEN 65536
 156 #define MAX_HEAP_BUFFER_LEN 131072
 157 #else
 158 #define MAX_BUFFER_LEN 8192
 159 #define MAX_HEAP_BUFFER_LEN 65536
 160 #endif
 161 
 162 #ifdef AF_INET6
 163 
 164 #define SOCKADDR        union { \
 165                             struct sockaddr_in him4; \
 166                             struct sockaddr_in6 him6; \
 167                         }
 168 
 169 #define SOCKADDR_LEN    (ipv6_available() ? sizeof(SOCKADDR) : \
 170                          sizeof(struct sockaddr_in))
 171 
 172 #else
 173 
 174 #define SOCKADDR        union { struct sockaddr_in him4; }
 175 #define SOCKADDR_LEN    sizeof(SOCKADDR)
 176 
 177 #endif
 178 
 179 /************************************************************************
 180  *  Utilities
 181  */
 182 #ifdef __linux__
 183 extern int kernelIsV24();
 184 #endif
 185 
 186 void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
 187                    const char *defaultDetail);
 188 
 189 
 190 #endif /* NET_UTILS_MD_H */