94
95 /*
96 * Inet4AddressImpl
97 */
98
99 /*
100 * Class: java_net_Inet4AddressImpl
101 * Method: getLocalHostName
102 * Signature: ()Ljava/lang/String;
103 */
104 JNIEXPORT jstring JNICALL
105 Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
106 char hostname[256];
107
108 if (gethostname(hostname, sizeof hostname) == -1) {
109 strcpy(hostname, "localhost");
110 }
111 return JNU_NewStringPlatform(env, hostname);
112 }
113
114 static jclass ni_iacls;
115 static jclass ni_ia4cls;
116 static jmethodID ni_ia4ctrID;
117 static jfieldID ni_iaaddressID;
118 static jfieldID ni_iahostID;
119 static jfieldID ni_iafamilyID;
120 static int initialized = 0;
121
122 /*
123 * Find an internet address for a given hostname. Not this this
124 * code only works for addresses of type INET. The translation
125 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
126 * String "host" shouldn't be a %d.%d.%d.%d string. The only
127 * exception should be when any of the %d are out of range and
128 * we fallback to a lookup.
129 *
130 * Class: java_net_Inet4AddressImpl
131 * Method: lookupAllHostAddr
132 * Signature: (Ljava/lang/String;)[[B
133 *
134 * This is almost shared code
135 */
136
137 JNIEXPORT jobjectArray JNICALL
138 Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
139 jstring host) {
140 const char *hostname;
141 struct hostent *hp;
142 unsigned int addr[4];
143
144 jobjectArray ret = NULL;
145
146 if (!initialized) {
147 ni_iacls = (*env)->FindClass(env, "java/net/InetAddress");
148 ni_iacls = (*env)->NewGlobalRef(env, ni_iacls);
149 ni_ia4cls = (*env)->FindClass(env, "java/net/Inet4Address");
150 ni_ia4cls = (*env)->NewGlobalRef(env, ni_ia4cls);
151 ni_ia4ctrID = (*env)->GetMethodID(env, ni_ia4cls, "<init>", "()V");
152 ni_iaaddressID = (*env)->GetFieldID(env, ni_iacls, "address", "I");
153 ni_iafamilyID = (*env)->GetFieldID(env, ni_iacls, "family", "I");
154 ni_iahostID = (*env)->GetFieldID(env, ni_iacls, "hostName", "Ljava/lang/String;");
155 initialized = 1;
156 }
157
158 if (IS_NULL(host)) {
159 JNU_ThrowNullPointerException(env, "host argument");
160 return NULL;
161 }
162 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
163 CHECK_NULL_RETURN(hostname, NULL);
164
165 /*
166 * The NT/2000 resolver tolerates a space in front of localhost. This
167 * is not consistent with other implementations of gethostbyname.
168 * In addition we must do a white space check on Solaris to avoid a
169 * bug whereby 0.0.0.0 is returned if any host name has a white space.
170 */
171 if (isspace(hostname[0])) {
172 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
173 goto cleanupAndReturn;
174 }
175
176 /*
180 if (isDottedIPAddress(hostname, &addr[0])) {
181 unsigned int address;
182 jobject iaObj;
183
184 /*
185 * Are any of the octets out of range?
186 */
187 if (addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255) {
188 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
189 goto cleanupAndReturn;
190 }
191
192 /*
193 * Return an byte array with the populated address.
194 */
195 address = (addr[3]<<24) & 0xff000000;
196 address |= (addr[2]<<16) & 0xff0000;
197 address |= (addr[1]<<8) & 0xff00;
198 address |= addr[0];
199
200 ret = (*env)->NewObjectArray(env, 1, ni_iacls, NULL);
201
202 if (IS_NULL(ret)) {
203 goto cleanupAndReturn;
204 }
205
206 iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
207 if (IS_NULL(iaObj)) {
208 ret = NULL;
209 goto cleanupAndReturn;
210 }
211 (*env)->SetIntField(env, iaObj, ni_iaaddressID,
212 ntohl(address));
213 (*env)->SetObjectArrayElement(env, ret, 0, iaObj);
214 JNU_ReleaseStringPlatformChars(env, host, hostname);
215 return ret;
216 }
217
218 /*
219 * Perform the lookup
220 */
221 if ((hp = gethostbyname((char*)hostname)) != NULL) {
222 struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
223 int len = sizeof(struct in_addr);
224 int i = 0;
225
226 while (*addrp != (struct in_addr *) 0) {
227 i++;
228 addrp++;
229 }
230
231 ret = (*env)->NewObjectArray(env, i, ni_iacls, NULL);
232
233 if (IS_NULL(ret)) {
234 goto cleanupAndReturn;
235 }
236
237 addrp = (struct in_addr **) hp->h_addr_list;
238 i = 0;
239 while (*addrp != (struct in_addr *) 0) {
240 jobject iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
241 if (IS_NULL(iaObj)) {
242 ret = NULL;
243 goto cleanupAndReturn;
244 }
245 (*env)->SetIntField(env, iaObj, ni_iaaddressID,
246 ntohl((*addrp)->s_addr));
247 (*env)->SetObjectField(env, iaObj, ni_iahostID, host);
248 (*env)->SetObjectArrayElement(env, ret, i, iaObj);
249 addrp++;
250 i++;
251 }
252 } else {
253 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
254 }
255
256 cleanupAndReturn:
257 JNU_ReleaseStringPlatformChars(env, host, hostname);
258 return ret;
259 }
260
261 /*
262 * Class: java_net_Inet4AddressImpl
263 * Method: getHostByAddr
264 * Signature: (I)Ljava/lang/String;
265 */
266 JNIEXPORT jstring JNICALL
267 Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
|
94
95 /*
96 * Inet4AddressImpl
97 */
98
99 /*
100 * Class: java_net_Inet4AddressImpl
101 * Method: getLocalHostName
102 * Signature: ()Ljava/lang/String;
103 */
104 JNIEXPORT jstring JNICALL
105 Java_java_net_Inet4AddressImpl_getLocalHostName (JNIEnv *env, jobject this) {
106 char hostname[256];
107
108 if (gethostname(hostname, sizeof hostname) == -1) {
109 strcpy(hostname, "localhost");
110 }
111 return JNU_NewStringPlatform(env, hostname);
112 }
113
114 /*
115 * Find an internet address for a given hostname. Not this this
116 * code only works for addresses of type INET. The translation
117 * of %d.%d.%d.%d to an address (int) occurs in java now, so the
118 * String "host" shouldn't be a %d.%d.%d.%d string. The only
119 * exception should be when any of the %d are out of range and
120 * we fallback to a lookup.
121 *
122 * Class: java_net_Inet4AddressImpl
123 * Method: lookupAllHostAddr
124 * Signature: (Ljava/lang/String;)[[B
125 *
126 * This is almost shared code
127 */
128
129 JNIEXPORT jobjectArray JNICALL
130 Java_java_net_Inet4AddressImpl_lookupAllHostAddr(JNIEnv *env, jobject this,
131 jstring host) {
132 const char *hostname;
133 struct hostent *hp;
134 unsigned int addr[4];
135
136 jobjectArray ret = NULL;
137
138 init(env);
139
140 if (IS_NULL(host)) {
141 JNU_ThrowNullPointerException(env, "host argument");
142 return NULL;
143 }
144 hostname = JNU_GetStringPlatformChars(env, host, JNI_FALSE);
145 CHECK_NULL_RETURN(hostname, NULL);
146
147 /*
148 * The NT/2000 resolver tolerates a space in front of localhost. This
149 * is not consistent with other implementations of gethostbyname.
150 * In addition we must do a white space check on Solaris to avoid a
151 * bug whereby 0.0.0.0 is returned if any host name has a white space.
152 */
153 if (isspace(hostname[0])) {
154 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
155 goto cleanupAndReturn;
156 }
157
158 /*
162 if (isDottedIPAddress(hostname, &addr[0])) {
163 unsigned int address;
164 jobject iaObj;
165
166 /*
167 * Are any of the octets out of range?
168 */
169 if (addr[0] > 255 || addr[1] > 255 || addr[2] > 255 || addr[3] > 255) {
170 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
171 goto cleanupAndReturn;
172 }
173
174 /*
175 * Return an byte array with the populated address.
176 */
177 address = (addr[3]<<24) & 0xff000000;
178 address |= (addr[2]<<16) & 0xff0000;
179 address |= (addr[1]<<8) & 0xff00;
180 address |= addr[0];
181
182 ret = (*env)->NewObjectArray(env, 1, ia_class, NULL);
183
184 if (IS_NULL(ret)) {
185 goto cleanupAndReturn;
186 }
187
188 iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
189 if (IS_NULL(iaObj)) {
190 ret = NULL;
191 goto cleanupAndReturn;
192 }
193 (*env)->SetIntField(env, iaObj, ia_addressID,
194 ntohl(address));
195 (*env)->SetObjectArrayElement(env, ret, 0, iaObj);
196 JNU_ReleaseStringPlatformChars(env, host, hostname);
197 return ret;
198 }
199
200 /*
201 * Perform the lookup
202 */
203 if ((hp = gethostbyname((char*)hostname)) != NULL) {
204 struct in_addr **addrp = (struct in_addr **) hp->h_addr_list;
205 int len = sizeof(struct in_addr);
206 int i = 0;
207
208 while (*addrp != (struct in_addr *) 0) {
209 i++;
210 addrp++;
211 }
212
213 ret = (*env)->NewObjectArray(env, i, ia_class, NULL);
214
215 if (IS_NULL(ret)) {
216 goto cleanupAndReturn;
217 }
218
219 addrp = (struct in_addr **) hp->h_addr_list;
220 i = 0;
221 while (*addrp != (struct in_addr *) 0) {
222 jobject iaObj = (*env)->NewObject(env, ia4_class, ia4_ctrID);
223 if (IS_NULL(iaObj)) {
224 ret = NULL;
225 goto cleanupAndReturn;
226 }
227 (*env)->SetIntField(env, iaObj, ia_addressID,
228 ntohl((*addrp)->s_addr));
229 (*env)->SetObjectField(env, iaObj, ia_hostNameID, host);
230 (*env)->SetObjectArrayElement(env, ret, i, iaObj);
231 addrp++;
232 i++;
233 }
234 } else {
235 JNU_ThrowByName(env, JNU_JAVANETPKG "UnknownHostException", hostname);
236 }
237
238 cleanupAndReturn:
239 JNU_ReleaseStringPlatformChars(env, host, hostname);
240 return ret;
241 }
242
243 /*
244 * Class: java_net_Inet4AddressImpl
245 * Method: getHostByAddr
246 * Signature: (I)Ljava/lang/String;
247 */
248 JNIEXPORT jstring JNICALL
249 Java_java_net_Inet4AddressImpl_getHostByAddr(JNIEnv *env, jobject this,
|