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 int NET_Timeout(int s, long timeout);
  40 int NET_Read(int s, void* buf, size_t len);
  41 int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
  42                  struct sockaddr *from, socklen_t *fromlen);
  43 int NET_ReadV(int s, const struct iovec * vector, int count);
  44 int NET_Send(int s, void *msg, int len, unsigned int flags);
  45 int NET_SendTo(int s, const void *msg, int len,  unsigned  int
  46        flags, const struct sockaddr *to, int tolen);
  47 int NET_Writev(int s, const struct iovec * vector, int count);
  48 int NET_Connect(int s, struct sockaddr *addr, int addrlen);
  49 int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen);
  50 int NET_SocketClose(int s);
  51 int NET_Dup2(int oldfd, int newfd);
  52 extern int NET_Select(int s, fd_set *readfds, fd_set *writefds,
  53                fd_set *exceptfds, struct timeval *timeout);
  54 extern int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout);
  55 
  56 int NET_SocketAvailable(int s, jint *pbytes);
  57 
  58 #if defined(__linux__) && defined(AF_INET6)
  59 int getDefaultIPv6Interface(struct in6_addr *target_addr);
  60 #endif
  61 
  62 #ifdef __solaris__
  63 extern int net_getParam(char *driver, char *param);
  64 #endif
  65 
  66 /* needed from libsocket on Solaris 8 */
  67 
  68 typedef int (*getaddrinfo_f)(const char *nodename, const char *servname,
  69     const struct addrinfo *hints, struct addrinfo **res);
  70 
  71 typedef void (*freeaddrinfo_f)(struct addrinfo *);
  72 
  73 typedef const char * (*gai_strerror_f)(int ecode);
  74 
  75 typedef int (*getnameinfo_f)(const struct sockaddr *, size_t,
  76     char *, size_t, char *, size_t, int);
  77 
  78 extern getaddrinfo_f getaddrinfo_ptr;
  79 extern freeaddrinfo_f freeaddrinfo_ptr;
  80 extern getnameinfo_f getnameinfo_ptr;
  81 
  82 void ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
  83                                            const char* hostname,
  84                                            int gai_error);
  85 
  86 #define NET_WAIT_READ   0x01
  87 #define NET_WAIT_WRITE  0x02
  88 #define NET_WAIT_CONNECT        0x04
  89 
  90 extern jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
  91 
  92 /************************************************************************
  93  * Macros and constants
  94  */
  95 
  96 /*
  97  * On 64-bit JDKs we use a much larger stack and heap buffer.
  98  */
  99 #ifdef _LP64
 100 #define MAX_BUFFER_LEN 65536
 101 #define MAX_HEAP_BUFFER_LEN 131072
 102 #else
 103 #define MAX_BUFFER_LEN 8192
 104 #define MAX_HEAP_BUFFER_LEN 65536
 105 #endif
 106 
 107 #ifdef AF_INET6
 108 
 109 #define SOCKADDR        union { \
 110                             struct sockaddr_in him4; \
 111                             struct sockaddr_in6 him6; \
 112                         }
 113 
 114 #define SOCKADDR_LEN    (ipv6_available() ? sizeof(SOCKADDR) : \
 115                          sizeof(struct sockaddr_in))
 116 
 117 #else
 118 
 119 #define SOCKADDR        union { struct sockaddr_in him4; }
 120 #define SOCKADDR_LEN    sizeof(SOCKADDR)
 121 
 122 #endif
 123 
 124 /************************************************************************
 125  *  Utilities
 126  */
 127 #ifdef __linux__
 128 extern int kernelIsV24();
 129 #endif
 130 
 131 void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
 132                    const char *defaultDetail);
 133 
 134 
 135 #endif /* NET_UTILS_MD_H */