1 /*
2  * Copyright (c) 1997, 2010, 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 // Android-changed: Fuchsia: Point to correct location of header. http://b/119426171
37 // #include <sys/poll.h>
38 #if !defined(__Fuchsia__)
39 #include <sys/poll.h>
40 #else
41 #include <poll.h>
42 #endif
43 #endif
44 
45 // Android-changed: Fuchsia: Use the non-JVM NET_* on Fuchsia also.
46 // #if defined(__linux__) || defined(MACOSX)
47 #if defined(__linux__) || defined(MACOSX) || defined(__Fuchsia__)
48 extern int NET_Timeout(int s, long timeout);
49 extern int NET_Read(int s, void* buf, size_t len);
50 extern int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
51        struct sockaddr *from, int *fromlen);
52 extern int NET_ReadV(int s, const struct iovec * vector, int count);
53 extern int NET_Send(int s, void *msg, int len, unsigned int flags);
54 extern int NET_SendTo(int s, const void *msg, int len,  unsigned  int
55        flags, const struct sockaddr *to, int tolen);
56 extern int NET_Writev(int s, const struct iovec * vector, int count);
57 extern int NET_Connect(int s, struct sockaddr *addr, int addrlen);
58 extern int NET_Accept(int s, struct sockaddr *addr, int *addrlen);
59 extern int NET_SocketClose(int s);
60 extern int NET_Dup2(int oldfd, int newfd);
61 
62 #ifdef USE_SELECT
63 extern int NET_Select(int s, fd_set *readfds, fd_set *writefds,
64                fd_set *exceptfds, struct timeval *timeout);
65 #else
66 extern int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout);
67 #endif
68 
69 extern int tagSocket(JNIEnv* env, int fd);
70 
71 #else
72 
73 #define NET_Timeout     JVM_Timeout
74 #define NET_Read        JVM_Read
75 #define NET_RecvFrom    JVM_RecvFrom
76 #define NET_ReadV       readv
77 #define NET_Send        JVM_Send
78 #define NET_SendTo      JVM_SendTo
79 #define NET_WriteV      writev
80 #define NET_Connect     JVM_Connect
81 #define NET_Accept      JVM_Accept
82 #define NET_SocketClose JVM_SocketClose
83 #define NET_Dup2        dup2
84 #define NET_Select      select
85 #define NET_Poll        poll
86 
87 #define tagSocket(env,fd)    (void)0
88 
89 #endif
90 
91 
92 /* needed from libsocket on Solaris 8 */
93 
94 typedef int (*getaddrinfo_f)(const char *nodename, const char *servname,
95     const struct addrinfo *hints, struct addrinfo **res);
96 
97 typedef void (*freeaddrinfo_f)(struct addrinfo *);
98 
99 typedef const char * (*gai_strerror_f)(int ecode);
100 
101 typedef int (*getnameinfo_f)(const struct sockaddr *, size_t,
102     char *, size_t, char *, size_t, int);
103 
104 extern getaddrinfo_f getaddrinfo_ptr;
105 extern freeaddrinfo_f freeaddrinfo_ptr;
106 extern getnameinfo_f getnameinfo_ptr;
107 
108 void ThrowUnknownHostExceptionWithGaiError(JNIEnv *env,
109                                            const char* hostname,
110                                            int gai_error);
111 
112 /* do we have address translation support */
113 
114 extern jboolean NET_addrtransAvailable();
115 
116 #define NET_WAIT_READ   0x01
117 #define NET_WAIT_WRITE  0x02
118 #define NET_WAIT_CONNECT        0x04
119 
120 // Android-removed: unused
121 #if 0
122 extern jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
123 #endif
124 
125 /************************************************************************
126  * Macros and constants
127  */
128 
129 /*
130  * On 64-bit JDKs we use a much larger stack and heap buffer.
131  */
132 #ifdef _LP64
133 #define MAX_BUFFER_LEN 65536
134 #define MAX_HEAP_BUFFER_LEN 131072
135 #else
136 #define MAX_BUFFER_LEN 8192
137 #define MAX_HEAP_BUFFER_LEN 65536
138 #endif
139 
140 #ifdef AF_INET6
141 
142 #define SOCKADDR        union { \
143                             struct sockaddr_in him4; \
144                             struct sockaddr_in6 him6; \
145                         }
146 
147 #define SOCKADDR_LEN    (ipv6_available() ? sizeof(SOCKADDR) : \
148                          sizeof(struct sockaddr_in))
149 
150 #else
151 
152 #define SOCKADDR        union { struct sockaddr_in him4; }
153 #define SOCKADDR_LEN    sizeof(SOCKADDR)
154 
155 #endif
156 
157 /************************************************************************
158  *  Utilities
159  */
160 // Android-removed: unused
161 #if 0
162 #ifdef __linux__
163 extern int kernelIsV22();
164 extern int kernelIsV24();
165 #endif
166 #endif
167 
168 void NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
169                    const char *defaultDetail);
170 
171 
172 #endif /* NET_UTILS_MD_H */
173