271 |
271 |
272 JNIEXPORT void JNICALL |
272 JNIEXPORT void JNICALL |
273 Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean preferIPv6, |
273 Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean preferIPv6, |
274 jboolean useExclBind, jobject iao, int port) |
274 jboolean useExclBind, jobject iao, int port) |
275 { |
275 { |
276 SOCKADDR sa; |
276 SOCKETADDRESS sa; |
277 int sa_len = SOCKADDR_LEN; |
277 int sa_len = sizeof(SOCKETADDRESS); |
278 int rv = 0; |
278 int rv = 0; |
279 |
279 |
280 if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) { |
280 if (NET_InetAddressToSockaddr(env, iao, port, &sa.sa, &sa_len, |
281 return; |
281 preferIPv6) != 0) { |
282 } |
282 return; |
283 |
283 } |
284 rv = NET_Bind(fdval(env, fdo), (struct sockaddr *)&sa, sa_len); |
284 |
|
285 rv = NET_Bind(fdval(env, fdo), &sa.sa, sa_len); |
285 if (rv != 0) { |
286 if (rv != 0) { |
286 handleSocketError(env, errno); |
287 handleSocketError(env, errno); |
287 } |
288 } |
288 } |
289 } |
289 |
290 |
296 |
297 |
297 JNIEXPORT jint JNICALL |
298 JNIEXPORT jint JNICALL |
298 Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6, |
299 Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6, |
299 jobject fdo, jobject iao, jint port) |
300 jobject fdo, jobject iao, jint port) |
300 { |
301 { |
301 SOCKADDR sa; |
302 SOCKETADDRESS sa; |
302 int sa_len = SOCKADDR_LEN; |
303 int sa_len = sizeof(SOCKETADDRESS); |
303 int rv; |
304 int rv; |
304 |
305 |
305 if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *) &sa, |
306 if (NET_InetAddressToSockaddr(env, iao, port, &sa.sa, &sa_len, |
306 &sa_len, preferIPv6) != 0) |
307 preferIPv6) != 0) { |
307 { |
308 return IOS_THROWN; |
308 return IOS_THROWN; |
309 } |
309 } |
310 |
310 |
311 rv = connect(fdval(env, fdo), &sa.sa, sa_len); |
311 rv = connect(fdval(env, fdo), (struct sockaddr *)&sa, sa_len); |
|
312 if (rv != 0) { |
312 if (rv != 0) { |
313 if (errno == EINPROGRESS) { |
313 if (errno == EINPROGRESS) { |
314 return IOS_UNAVAILABLE; |
314 return IOS_UNAVAILABLE; |
315 } else if (errno == EINTR) { |
315 } else if (errno == EINTR) { |
316 return IOS_INTERRUPTED; |
316 return IOS_INTERRUPTED; |
321 } |
321 } |
322 |
322 |
323 JNIEXPORT jint JNICALL |
323 JNIEXPORT jint JNICALL |
324 Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo) |
324 Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo) |
325 { |
325 { |
326 SOCKADDR sa; |
326 SOCKETADDRESS sa; |
327 socklen_t sa_len = SOCKADDR_LEN; |
327 socklen_t sa_len = sizeof(SOCKETADDRESS); |
328 if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) { |
328 if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) { |
329 #ifdef _ALLBSD_SOURCE |
329 #ifdef _ALLBSD_SOURCE |
330 /* |
330 /* |
331 * XXXBSD: |
331 * XXXBSD: |
332 * ECONNRESET is specific to the BSDs. We can not return an error, |
332 * ECONNRESET is specific to the BSDs. We can not return an error, |
333 * as the calling Java code with raise a java.lang.Error given the expectation |
333 * as the calling Java code with raise a java.lang.Error given the expectation |
334 * that getsockname() will never fail. According to the Single UNIX Specification, |
334 * that getsockname() will never fail. According to the Single UNIX Specification, |
335 * it shouldn't fail. As such, we just fill in generic Linux-compatible values. |
335 * it shouldn't fail. As such, we just fill in generic Linux-compatible values. |
336 */ |
336 */ |
337 if (errno == ECONNRESET) { |
337 if (errno == ECONNRESET) { |
338 struct sockaddr_in *sin; |
338 bzero(&sa.sa4, sizeof(sa)); |
339 sin = (struct sockaddr_in *) &sa; |
339 sa.sa4.sin_len = sizeof(struct sockaddr_in); |
340 bzero(sin, sizeof(*sin)); |
340 sa.sa4.sin_family = AF_INET; |
341 sin->sin_len = sizeof(struct sockaddr_in); |
341 sa.sa4.sin_port = htonl(0); |
342 sin->sin_family = AF_INET; |
342 sa.sa4.sin_addr.s_addr = INADDR_ANY; |
343 sin->sin_port = htonl(0); |
|
344 sin->sin_addr.s_addr = INADDR_ANY; |
|
345 } else { |
343 } else { |
346 handleSocketError(env, errno); |
344 handleSocketError(env, errno); |
347 return -1; |
345 return -1; |
348 } |
346 } |
349 #else /* _ALLBSD_SOURCE */ |
347 #else /* _ALLBSD_SOURCE */ |
350 handleSocketError(env, errno); |
348 handleSocketError(env, errno); |
351 return -1; |
349 return -1; |
352 #endif /* _ALLBSD_SOURCE */ |
350 #endif /* _ALLBSD_SOURCE */ |
353 } |
351 } |
354 return NET_GetPortFromSockaddr((struct sockaddr *)&sa); |
352 return NET_GetPortFromSockaddr(&sa.sa); |
355 } |
353 } |
356 |
354 |
357 JNIEXPORT jobject JNICALL |
355 JNIEXPORT jobject JNICALL |
358 Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo) |
356 Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo) |
359 { |
357 { |
360 SOCKADDR sa; |
358 SOCKETADDRESS sa; |
361 socklen_t sa_len = SOCKADDR_LEN; |
359 socklen_t sa_len = sizeof(SOCKETADDRESS); |
362 int port; |
360 int port; |
363 if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) { |
361 if (getsockname(fdval(env, fdo), &sa.sa, &sa_len) < 0) { |
364 #ifdef _ALLBSD_SOURCE |
362 #ifdef _ALLBSD_SOURCE |
365 /* |
363 /* |
366 * XXXBSD: |
364 * XXXBSD: |
367 * ECONNRESET is specific to the BSDs. We can not return an error, |
365 * ECONNRESET is specific to the BSDs. We can not return an error, |
368 * as the calling Java code with raise a java.lang.Error with the expectation |
366 * as the calling Java code with raise a java.lang.Error with the expectation |
369 * that getsockname() will never fail. According to the Single UNIX Specification, |
367 * that getsockname() will never fail. According to the Single UNIX Specification, |
370 * it shouldn't fail. As such, we just fill in generic Linux-compatible values. |
368 * it shouldn't fail. As such, we just fill in generic Linux-compatible values. |
371 */ |
369 */ |
372 if (errno == ECONNRESET) { |
370 if (errno == ECONNRESET) { |
373 struct sockaddr_in *sin; |
371 bzero(&sa.sa4, sizeof(sa)); |
374 sin = (struct sockaddr_in *) &sa; |
372 sa.sa4.sin_len = sizeof(struct sockaddr_in); |
375 bzero(sin, sizeof(*sin)); |
373 sa.sa4.sin_family = AF_INET; |
376 sin->sin_len = sizeof(struct sockaddr_in); |
374 sa.sa4.sin_port = htonl(0); |
377 sin->sin_family = AF_INET; |
375 sa.sa4.sin_addr.s_addr = INADDR_ANY; |
378 sin->sin_port = htonl(0); |
|
379 sin->sin_addr.s_addr = INADDR_ANY; |
|
380 } else { |
376 } else { |
381 handleSocketError(env, errno); |
377 handleSocketError(env, errno); |
382 return NULL; |
378 return NULL; |
383 } |
379 } |
384 #else /* _ALLBSD_SOURCE */ |
380 #else /* _ALLBSD_SOURCE */ |
385 handleSocketError(env, errno); |
381 handleSocketError(env, errno); |
386 return NULL; |
382 return NULL; |
387 #endif /* _ALLBSD_SOURCE */ |
383 #endif /* _ALLBSD_SOURCE */ |
388 } |
384 } |
389 return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port); |
385 return NET_SockaddrToInetAddress(env, &sa.sa, &port); |
390 } |
386 } |
391 |
387 |
392 JNIEXPORT jint JNICALL |
388 JNIEXPORT jint JNICALL |
393 Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo, |
389 Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo, |
394 jboolean mayNeedConversion, jint level, jint opt) |
390 jboolean mayNeedConversion, jint level, jint opt) |