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_H
27 #define NET_UTILS_H
28 
29 #include "jvm.h"
30 #include "jni_util.h"
31 #include "net_util_md.h"
32 
33 /************************************************************************
34  * Macros and misc constants
35  */
36 
37 #define MAX_PACKET_LEN 65536
38 
39 #define IPv4 AF_INET
40 #define IPv6 AF_INET6
41 
42 #define NET_ERROR(env, ex, msg) \
43 { if (!(*env)->ExceptionOccurred(env)) JNU_ThrowByName(env, ex, msg); }
44 
45 /************************************************************************
46  * Cached field IDs
47  *
48  * The naming convention for field IDs is
49  *      <class abbrv>_<fieldName>ID
50  * i.e. psi_timeoutID is PlainSocketImpl's timeout field's ID.
51  */
52 extern jclass ia_class;
53 extern jfieldID iac_addressID;
54 extern jfieldID iac_familyID;
55 extern jfieldID iac_hostNameID;
56 extern jfieldID ia_preferIPv6AddressID;
57 
58 /** (Inet6Address accessors)
59  * set_ methods return JNI_TRUE on success JNI_FALSE on error
60  * get_ methods that return int/boolean, return -1 on error
61  * get_ methods that return objects return NULL on error.
62  */
63 extern jobject getInet6Address_scopeifname(JNIEnv *env, jobject ia6Obj);
64 extern int setInet6Address_scopeifname(JNIEnv *env, jobject ia6Obj, jobject scopeifname);
65 extern int getInet6Address_scopeid_set(JNIEnv *env, jobject ia6Obj);
66 extern int getInet6Address_scopeid(JNIEnv *env, jobject ia6Obj);
67 extern int setInet6Address_scopeid(JNIEnv *env, jobject ia6Obj, int scopeid);
68 extern int getInet6Address_ipaddress(JNIEnv *env, jobject ia6Obj, char *dest);
69 extern int setInet6Address_ipaddress(JNIEnv *env, jobject ia6Obj, char *address);
70 
71 extern void setInetAddress_addr(JNIEnv *env, jobject iaObj, int address);
72 extern void setInetAddress_family(JNIEnv *env, jobject iaObj, int family);
73 extern void setInetAddress_hostName(JNIEnv *env, jobject iaObj, jobject h);
74 extern int getInetAddress_addr(JNIEnv *env, jobject iaObj);
75 extern int getInetAddress_family(JNIEnv *env, jobject iaObj);
76 extern jobject getInetAddress_hostName(JNIEnv *env, jobject iaObj);
77 
78 extern jclass ia4_class;
79 extern jmethodID ia4_ctrID;
80 
81 /* NetworkInterface fields */
82 extern jclass ni_class;
83 extern jfieldID ni_nameID;
84 extern jfieldID ni_indexID;
85 extern jfieldID ni_addrsID;
86 extern jfieldID ni_descID;
87 extern jmethodID ni_ctrID;
88 
89 /* PlainSocketImpl fields */
90 extern jfieldID psi_timeoutID;
91 extern jfieldID psi_fdID;
92 extern jfieldID psi_addressID;
93 extern jfieldID psi_portID;
94 extern jfieldID psi_localportID;
95 
96 /* Android-removed: DatagramSocket moved away from JNI */
97 #if 0
98 /* DatagramPacket fields */
99 extern jfieldID dp_addressID;
100 extern jfieldID dp_portID;
101 extern jfieldID dp_bufID;
102 extern jfieldID dp_offsetID;
103 extern jfieldID dp_lengthID;
104 extern jfieldID dp_bufLengthID;
105 #endif
106 
107 /* Inet6Address fields */
108 extern jclass ia6_class;
109 extern jfieldID ia6_holder6ID;
110 extern jfieldID ia6_ipaddressID;
111 extern jfieldID ia6_scopeidID;
112 extern jfieldID ia6_scopeidsetID;
113 extern jfieldID ia6_scopeifnameID;
114 extern jmethodID ia6_ctrID;
115 
116 /************************************************************************
117  *  Utilities
118  */
119 
120 JNIEXPORT void JNICALL NET_ThrowNew(JNIEnv *env, int errorNum, char *msg);
121 int NET_GetError();
122 
123 void NET_ThrowCurrent(JNIEnv *env, char *msg);
124 
125 jfieldID NET_GetFileDescriptorID(JNIEnv *env);
126 
127 JNIEXPORT jint JNICALL ipv6_available();
128 
129 void
130 NET_AllocSockaddr(struct sockaddr **him, int *len);
131 
132 JNIEXPORT int JNICALL
133 NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him, int *len, jboolean v4MappedAddress);
134 
135 JNIEXPORT jobject JNICALL
136 NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port);
137 
138 void initLocalAddrTable ();
139 void parseExclusiveBindProperty(JNIEnv *env);
140 
141 void
142 NET_SetTrafficClass(struct sockaddr *him, int trafficClass);
143 
144 JNIEXPORT jint JNICALL
145 NET_GetPortFromSockaddr(struct sockaddr *him);
146 
147 JNIEXPORT jint JNICALL
148 NET_SockaddrEqualsInetAddress(JNIEnv *env,struct sockaddr *him, jobject iaObj);
149 
150 int
151 NET_IsIPv4Mapped(jbyte* caddr);
152 
153 int
154 NET_IPv4MappedToIPv4(jbyte* caddr);
155 
156 int
157 NET_IsEqual(jbyte* caddr1, jbyte* caddr2);
158 
159 int
160 NET_IsZeroAddr(jbyte* caddr);
161 
162 /* Socket operations
163  *
164  * These work just like the JVM_* procedures, except that they may do some
165  * platform-specific pre/post processing of the arguments and/or results.
166  */
167 
168 JNIEXPORT int JNICALL
169 NET_GetSockOpt(int fd, int level, int opt, void *result, int *len);
170 
171 JNIEXPORT int JNICALL
172 NET_SetSockOpt(int fd, int level, int opt, const void *arg, int len);
173 
174 JNIEXPORT int JNICALL
175 NET_Bind(int fd, struct sockaddr *him, int len);
176 
177 JNIEXPORT int JNICALL
178 NET_MapSocketOption(jint cmd, int *level, int *optname);
179 
180 JNIEXPORT int JNICALL
181 NET_MapSocketOptionV6(jint cmd, int *level, int *optname);
182 
183 int getScopeID (struct sockaddr *);
184 
185 int cmpScopeID (unsigned int, struct sockaddr *);
186 
187 unsigned short in_cksum(unsigned short *addr, int len);
188 #endif /* NET_UTILS_H */
189