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 (_AIX)
  51 extern int IO_Fcntl(int s, int cmd, void *arg);
  52 extern int IO_Read(int s, void* buf, size_t len);
  53 extern int IO_Write(int s, void* buf, size_t len);
  54 #endif
  55 
  56 #if defined(__linux__) || defined(MACOSX) || defined (_AIX)
  57 extern int NET_Timeout(int s, long timeout);
  58 extern int NET_Read(int s, void* buf, size_t len);
  59 extern int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
  60        struct sockaddr *from, int *fromlen);
  61 extern int NET_ReadV(int s, const struct iovec * vector, int count);
  62 extern int NET_Send(int s, void *msg, int len, unsigned int flags);
  63 extern int NET_SendTo(int s, const void *msg, int len,  unsigned  int
  64        flags, const struct sockaddr *to, int tolen);
  65 extern int NET_WriteV(int s, const struct iovec * vector, int count);
  66 extern int NET_Connect(int s, struct sockaddr *addr, int addrlen);
  67 extern int NET_Accept(int s, struct sockaddr *addr, int *addrlen);
  68 extern int NET_SocketClose(int s);
  69 extern int NET_Dup2(int oldfd, int newfd);
  70 
  71 #ifdef USE_SELECT
  72 extern int NET_Select(int s, fd_set *readfds, fd_set *writefds,
  73                fd_set *exceptfds, struct timeval *timeout);
  74 #else
  75 extern int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout);
  76 #endif
  77 
  78 #else
  79 
  80 #define NET_Timeout     JVM_Timeout
  81 #define NET_Read        JVM_Read
  82 #define NET_RecvFrom    JVM_RecvFrom
  83 #define NET_ReadV       readv
  84 #define NET_Send        JVM_Send
  85 #define NET_SendTo      JVM_SendTo
  86 #define NET_WriteV      writev
  87 #define NET_Connect     JVM_Connect
  88 #define NET_Accept      JVM_Accept
  89 #define NET_SocketClose JVM_SocketClose
  90 #define NET_Dup2        dup2
  91 #define NET_Select      select
  92 #define NET_Poll        poll
  93 
  94 #endif
  95 
  96 #ifdef _AIX
  97 /* defined in src/aix/native/java/net/aix_close.c */
  98 int javaToNativeEvents(int javaEvents);
  99 int nativeToJavaEvents(int nativeEvents);
 100 #endif
 101 
 102 #if defined(__linux__) && defined(AF_INET6)
 103 int getDefaultIPv6Interface(struct in6_addr *target_addr);
 104 #endif
 105 
 106 
 107 /* needed from libsocket on Solaris 8 */
 108 
 109 typedef int (*getaddrinfo_f)(const char *nodename, const char *servname,
 110     const struct addrinfo *hints, struct addrinfo **res);
 111 
 112 typedef void (*freeaddrinfo_f)(struct addrinfo *);
 113 
 114 typedef const char * (*gai_strerror_f)(int ecode);
 115 
 116 typedef int (*getnameinfo_f)(const struct sockaddr *, size_t,
 117     char *, size_t, char *, size_t, int);
 118 
 119 extern getaddrinfo_f getaddrinfo_ptr;
 120 extern freeaddrinfo_f freeaddrinfo_ptr;
 121 extern getnameinfo_f getnameinfo_ptr;
 122 
 123 void ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
 124                                            const char* hostname,
 125                                            int gai_error);
 126 
 127 #define NET_WAIT_READ   0x01
 128 #define NET_WAIT_WRITE  0x02
 129 #define NET_WAIT_CONNECT        0x04
 130 
 131 extern jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
 132 
 133 /************************************************************************
 134  * Macros and constants
 135  */
 136 
 137 /*
 138  * On 64-bit JDKs we use a much larger stack and heap buffer.
 139  */
 140 #ifdef _LP64
 141 #define MAX_BUFFER_LEN 65536
 142 #define MAX_HEAP_BUFFER_LEN 131072
 143 #else
 144 #define MAX_BUFFER_LEN 8192
 145 #define MAX_HEAP_BUFFER_LEN 65536
 146 #endif
 147 
 148 #ifdef AF_INET6
 149 
 150 #define SOCKADDR        union { \
 151                             struct sockaddr_in him4; \
 152                             struct sockaddr_in6 him6; \
 153                         }
 154 
 155 #define SOCKADDR_LEN    (ipv6_available() ? sizeof(SOCKADDR) : \
 156                          sizeof(struct sockaddr_in))
 157 
 158 #else
 159 
 160 #define SOCKADDR        union { struct sockaddr_in him4; }
 161 #define SOCKADDR_LEN    sizeof(SOCKADDR)
 162 
 163 #endif
 164 
 165 /************************************************************************
 166  *  Utilities
 167  */
 168 #ifdef __linux__
 169 extern int kernelIsV24();
 170 #endif
 171 
 172 void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
 173                    const char *defaultDetail);
 174 
 175 
 176 #endif /* NET_UTILS_MD_H */