use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.
the class Util_jni method socketSetOption.
/* Do not re-format commented-out code: @formatter:off */
// 886 /*
// 887 * Class: java_net_PlainSocketImpl
// 888 * Method: socketSetOption
// 889 * Signature: (IZLjava/lang/Object;)V
// 890 */
// 891 JNIEXPORT void JNICALL
// 892 Java_java_net_PlainSocketImpl_socketSetOption(JNIEnv *env, jobject this,
// 893 jint cmd, jboolean on,
// 894 jobject value) {
@Substitute
void socketSetOption(int cmd, boolean on, Object value) throws SocketException {
// 895 int fd;
int fd;
// 896 int level, optname, optlen;
CIntPointer level_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
CIntPointer optname_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
int optlen;
/* Translated as a WordPointer to the larger of the arms. */
// 897 union {
// 898 int i;
// 899 struct linger ling;
// 900 } optval;
/* Guess which arm of the union is larger. Trust, but verify. */
final int sizeof_optval = SizeOf.get(Socket.linger.class);
VMError.guarantee(SizeOf.get(CIntPointer.class) <= sizeof_optval, "sizeof(int) <= sizeof(union optval)");
VMError.guarantee(SizeOf.get(Socket.linger.class) <= sizeof_optval, "sizeof(struct linger) <= sizeof(union optval)");
WordPointer optval_Pointer = StackValue.get(sizeof_optval);
// 901
// 902 /*
// 903 * Check that socket hasn't been closed
// 904 */
// 905 fd = getFD(env, this);
fd = Util_java_io_FileDescriptor.getFD(Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).fd);
// 906 if (fd < 0) {
if (fd < 0) {
// 909 return;
throw new SocketException("Socket closed");
}
// 915 if (cmd == java_net_SocketOptions_SO_TIMEOUT) {
if (cmd == SocketOptions.SO_TIMEOUT) {
// 916 return;
return;
}
// 923 if (NET_MapSocketOption(cmd, &level, &optname)) {
if (CTypeConversion.toBoolean(JavaNetNetUtilMD.NET_MapSocketOption(cmd, level_Pointer, optname_Pointer))) {
// 925 return;
throw new SocketException("Invalid option");
}
// 928 switch (cmd) {
switch(cmd) {
// 932 case java_net_SocketOptions_IP_TOS :
case SocketOptions.SO_SNDBUF:
case SocketOptions.SO_RCVBUF:
case SocketOptions.SO_LINGER:
case SocketOptions.IP_TOS:
{
/* Accessing values of java.lang.Integer the easy way. */
Integer valueInteger = (Integer) value;
// 942 if (cmd == java_net_SocketOptions_SO_LINGER) {
if (cmd == SocketOptions.SO_LINGER) {
// 943 if (on) {
if (on) {
// 944 optval.ling.l_onoff = 1;
((Socket.linger) optval_Pointer).set_l_onoff(1);
// 945 optval.ling.l_linger = (*env)->GetIntField(env, value, fid);
((Socket.linger) optval_Pointer).set_l_linger(valueInteger.intValue());
} else {
// 947 optval.ling.l_onoff = 0;
((Socket.linger) optval_Pointer).set_l_onoff(0);
// 948 optval.ling.l_linger = 0;
((Socket.linger) optval_Pointer).set_l_linger(0);
}
// 950 optlen = sizeof(optval.ling);
optlen = SizeOf.get(Socket.linger.class);
/* Copy to optval. */
LibC.memcpy(optval_Pointer, optval_Pointer, WordFactory.unsigned(optlen));
} else {
// 952 optval.i = (*env)->GetIntField(env, value, fid);
((CIntPointer) optval_Pointer).write(valueInteger.intValue());
// 953 optlen = sizeof(optval.i);
optlen = SizeOf.get(CIntPointer.class);
/* Copy to optval. */
LibC.memcpy(optval_Pointer, optval_Pointer, WordFactory.unsigned(optlen));
}
// 956 break;
break;
}
// 960 default :
default:
// 961 optval.i = (on ? 1 : 0);
((CIntPointer) optval_Pointer).write(on ? 1 : 0);
// 962 optlen = sizeof(optval.i);
optlen = SizeOf.get(CIntPointer.class);
/* Copy to optval. */
LibC.memcpy(optval_Pointer, optval_Pointer, WordFactory.unsigned(optlen));
}
// 966 if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
if (JavaNetNetUtilMD.NET_SetSockOpt(fd, level_Pointer.read(), optname_Pointer.read(), optval_Pointer, optlen) < 0) {
// 968 if (errno == EINVAL) {
if (Errno.errno() == Errno.EINVAL()) {
// 974 return;
throw new SocketException("Invalid option or socket reset by remote peer");
}
// 978 "Error setting socket option");
throw new SocketException("Error setting socket option");
}
}
use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.
the class Util_jni method socketConnect.
/* @formatter:on */
/* Do not re-format commented-out code: @formatter:off */
@Substitute
@SuppressWarnings({ "unused" })
void socketConnect(InetAddress iaObj, int port, int timeoutArg) throws IOException {
/* local copy of "timeout" argument so it can be modified. */
int timeout = timeoutArg;
// 259 jint localport = (*env)->GetIntField(env, this, psi_localportID);
int localport = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).localport;
// 260 int len = 0;
CIntPointer len_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
len_Pointer.write(0);
// 262 /* fdObj is the FileDescriptor field on this */
// 263 jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
FileDescriptor fdObj = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).fd;
// 265 class clazz = (*env)->GetObjectClass(env, this);
// 267 jobject fdLock;
Object fdLock;
// 269 jint trafficClass = (*env)->GetIntField(env, this, psi_trafficClassID);
int trafficClass = Util_java_net_PlainSocketImpl.as_Target_java_net_AbstractPlainSocketImpl(this).trafficClass;
// 271 /* fd is an int field on iaObj */
// 272 jint fd;
int fd;
// 274 SOCKADDR him;
Socket.sockaddr him = StackValue.get(JavaNetNetUtilMD.SOCKADDR_LEN());
// 275 /* The result of the connection */
// 276 int connect_rv = -1;
CIntPointer connect_rv_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
connect_rv_Pointer.write(-1);
// 278 if (IS_NULL(fdObj)) {
if (fdObj == null) {
// 279 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
throw new SocketException("Socket closed");
// 280 return;
} else {
// 282 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
fd = Util_java_io_FileDescriptor.getFD(fdObj);
}
// 284 if (IS_NULL(iaObj)) {
if (iaObj == null) {
// 285 JNU_ThrowNullPointerException(env, "inet address argument null.");
throw new NullPointerException("inet address argument null.");
// 286 return;
}
// 290 if (NET_InetAddressToSockaddr(env, iaObj, port, (struct sockaddr *)&him, &len, JNI_TRUE) != 0) {
if (JavaNetNetUtilMD.NET_InetAddressToSockaddr(iaObj, port, him, len_Pointer, Util_jni.JNI_TRUE()) != 0) {
// 291 return;
return;
}
// 293 setDefaultScopeID(env, (struct sockaddr *)&him);
JavaNetNetUtilMD.setDefaultScopeID(him);
// 295 #ifdef AF_INET6
if (IsDefined.socket_AF_INET6()) {
// 296 if (trafficClass != 0 && ipv6_available()) {
if ((trafficClass != 0) && JavaNetNetUtil.ipv6_available()) {
// 297 NET_SetTrafficClass((struct sockaddr *)&him, trafficClass);
JavaNetNetUtilMD.NET_SetTrafficClass(him, trafficClass);
}
}
// 300 if (timeout <= 0) {
if (timeout <= 0) {
// 301 connect_rv = NET_Connect(fd, (struct sockaddr *)&him, len);
connect_rv_Pointer.write(JavaNetNetUtilMD.NET_Connect(fd, him, len_Pointer.read()));
// 302 #ifdef __solaris__
// 303 if (connect_rv == JVM_IO_ERR && errno == EINPROGRESS ) {
// 305 /* This can happen if a blocking connect is interrupted by a signal.
// 306 * See 6343810.
// 307 */
// 308 while (1) {
// 309 #ifndef USE_SELECT
// 310 {
// 311 struct pollfd pfd;
// 312 pfd.fd = fd;
// 313 pfd.events = POLLOUT;
// 314
// 315 connect_rv = NET_Poll(&pfd, 1, -1);
// 316 }
// 317 #else /* USE_SELECT */
// 318 {
// 319 fd_set wr, ex;
// 320
// 321 FD_ZERO(&wr);
// 322 FD_SET(fd, &wr);
// 323 FD_ZERO(&ex);
// 324 FD_SET(fd, &ex);
// 325
// 326 connect_rv = NET_Select(fd+1, 0, &wr, &ex, 0);
// 327 }
// 328 #endif /* USE_SELECT */
// 329
// 330 if (connect_rv == JVM_IO_ERR) {
// 331 if (errno == EINTR) {
// 332 continue;
// 333 } else {
// 334 break;
// 335 }
// 336 }
// 337 if (connect_rv > 0) {
// 338 int optlen;
// 339 /* has connection been established */
// 340 optlen = sizeof(connect_rv);
// 341 if (JVM_GetSockOpt(fd, SOL_SOCKET, SO_ERROR,
// 342 (void*)&connect_rv, &optlen) <0) {
// 343 connect_rv = errno;
// 344 }
// 345
// 346 if (connect_rv != 0) {
// 347 /* restore errno */
// 348 errno = connect_rv;
// 349 connect_rv = JVM_IO_ERR;
// 350 }
// 351 break;
// 352 }
// 353 }
// 354 }
// 355 #endif /* __solaris__ */
} else {
// 357 /*
// 358 * A timeout was specified. We put the socket into non-blocking
// 359 * mode, connect, and then wait for the connection to be
// 360 * established, fail, or timeout.
// 361 */
// 362 SET_NONBLOCKING(fd);
Util_java_net_PlainSocketImpl.SET_NONBLOCKING(fd);
// 364 /* no need to use NET_Connect as non-blocking */
// 365 connect_rv = connect(fd, (struct sockaddr *)&him, len);
connect_rv_Pointer.write(Socket.connect(fd, him, len_Pointer.read()));
// 368 if (connect_rv != 0) {
if (connect_rv_Pointer.read() != 0) {
// 369 int optlen;
CIntPointer optlen_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
// 370 jlong prevTime = JVM_CurrentTimeMillis(env, 0);
long prevTime = Target_java_lang_System.currentTimeMillis();
// 372 if (errno != EINPROGRESS) {
if (Errno.errno() != Errno.EINPROGRESS()) {
// 374 "connect failed");
try {
/* FIXME: Not implementing NET_ThrowByNameWithLastError. */
throw new ConnectException("connect failed");
} finally {
// 375 SET_BLOCKING(fd);
Util_java_net_PlainSocketImpl.SET_BLOCKING(fd);
// 376 return;
}
}
// 385 while (1) {
while (true) {
// 386 jlong newTime;
long newTime;
/*
* I am assuming that USE_SELECT is not defined.
* Cf. https://bugs.openjdk.java.net/browse/JDK-8035949
*/
// 387 #ifndef USE_SELECT
// 388 {
// 389 struct pollfd pfd;
Poll.pollfd pfd = StackValue.get(SizeOf.get(Poll.pollfd.class));
// 390 pfd.fd = fd;
pfd.set_fd(fd);
// 391 pfd.events = POLLOUT;
pfd.set_events(Poll.POLLOUT());
// 393 errno = 0;
Errno.set_errno(0);
// 394 connect_rv = NET_Poll(&pfd, 1, timeout);
connect_rv_Pointer.write(JavaNetNetUtilMD.NET_Poll(pfd, 1, timeout));
// 414 if (connect_rv >= 0) {
if (connect_rv_Pointer.read() >= 0) {
// 415 break;
break;
}
// 417 if (errno != EINTR) {
if (Errno.errno() != Errno.EINTR()) {
// 418 break;
break;
}
// 421 /*
// 422 * The poll was interrupted so adjust timeout and
// 423 * restart
// 424 */
// 425 newTime = JVM_CurrentTimeMillis(env, 0);
newTime = Target_java_lang_System.currentTimeMillis();
// 426 timeout -= (newTime - prevTime);
timeout -= (newTime - prevTime);
// 427 if (timeout <= 0) {
if (timeout <= 0) {
// 428 connect_rv = 0;
connect_rv_Pointer.write(0);
// 429 break;
break;
}
// 431 prevTime = newTime;
prevTime = newTime;
}
// 435 if (connect_rv == 0) {
if (connect_rv_Pointer.read() == 0) {
try {
// 437 "connect timed out");
throw new SocketTimeoutException("connect timed out");
} finally {
// 439 /*
// 440 * Timeout out but connection may still be established.
// 441 * At the high level it should be closed immediately but
// 442 * just in case we make the socket blocking again and
// 443 * shutdown input & output.
// 444 */
// 445 SET_BLOCKING(fd);
Util_java_net_PlainSocketImpl.SET_BLOCKING(fd);
// 446 JVM_SocketShutdown(fd, 2);
VmPrimsJVM.JVM_SocketShutdown(fd, 2);
// 447 return;
}
}
// 449
// 450 /* has connection been established */
// 451 optlen = sizeof(connect_rv);
optlen_Pointer.write(SizeOf.get(CIntPointer.class));
// 453 &optlen) <0) {
if (VmPrimsJVM.JVM_GetSockOpt(fd, Socket.SOL_SOCKET(), Socket.SO_ERROR(), (CCharPointer) connect_rv_Pointer, optlen_Pointer) < 0) {
// 454 connect_rv = errno;
connect_rv_Pointer.write(Errno.errno());
}
}
// 458 /* make socket blocking again */
// 459 SET_BLOCKING(fd);
Util_java_net_PlainSocketImpl.SET_BLOCKING(fd);
// 462 if (connect_rv != 0) {
if (connect_rv_Pointer.read() != 0) {
// 463 errno = connect_rv;
Errno.set_errno(connect_rv_Pointer.read());
// 464 connect_rv = JVM_IO_ERR;
connect_rv_Pointer.write(Target_jvm.JVM_IO_ERR());
}
}
// 469 if (connect_rv < 0) {
if (connect_rv_Pointer.read() < 0) {
// 471 #ifdef __linux__
if (IsDefined.__linux__()) {
// 482 if (connect_rv == JVM_IO_ERR && errno == EINVAL) {
if (connect_rv_Pointer.read() == Target_jvm.JVM_IO_ERR() && Errno.errno() == Errno.EINVAL()) {
// 485 return;
throw new SocketException("Invalid argument or cannot assign requested address");
// 486 }
}
}
// 488 if (connect_rv == JVM_IO_INTR) {
if (connect_rv_Pointer.read() == Target_jvm.JVM_IO_INTR()) {
// 490 "operation interrupted");
throw new InterruptedIOException("operation interrupted");
// 491 #if defined(EPROTO)
// 492 } else if (errno == EPROTO) {
} else if (Errno.errno() == Errno.EPROTO()) {
// 494 "Protocol error");
throw new ProtocolException("Protocol error");
// 495 #endif
// 496 } else if (errno == ECONNREFUSED) {
} else if (Errno.errno() == Errno.ECONNREFUSED()) {
// 498 "Connection refused");
throw new ConnectException("Connection refused");
// 499 } else if (errno == ETIMEDOUT) {
} else if (Errno.errno() == Errno.ETIMEDOUT()) {
// 501 "Connection timed out");
throw new ConnectException("Connection timed out");
// 502 } else if (errno == EHOSTUNREACH) {
} else if (Errno.errno() == Errno.EHOSTUNREACH()) {
// 504 "Host unreachable");
throw new NoRouteToHostException("Host unreachable");
// 505 } else if (errno == EADDRNOTAVAIL) {
} else if (Errno.errno() == Errno.EADDRNOTAVAIL()) {
// 507 "Address not available");
throw new NoRouteToHostException("Address not available");
// 508 } else if ((errno == EISCONN) || (errno == EBADF)) {
} else if ((Errno.errno() == Errno.EISCONN()) || (Errno.errno() == Errno.EBADF())) {
// 510 "Socket closed");
throw new SocketException("Socket closed");
// 511 } else {
} else {
// 512 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "connect failed");
throw new SocketException("connect failed");
}
// 514 return;
/* Elided return because the "throw"s above do that. */
}
// 517 (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
Util_java_io_FileDescriptor.setFD(fdObj, fd);
// 519 /* set the remote peer address and port */
// 520 (*env)->SetObjectField(env, this, psi_addressID, iaObj);
Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).address = iaObj;
// 521 (*env)->SetIntField(env, this, psi_portID, port);
Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).port = port;
// 528 if (localport == 0) {
if (localport == 0) {
// 529 /* Now that we're a connected socket, let's extract the port number
// 530 * that the system chose for us and store it in the Socket object.
// 531 */
// 532 len = SOCKADDR_LEN;
len_Pointer.write(JavaNetNetUtilMD.SOCKADDR_LEN());
// 533 if (JVM_GetSockName(fd, (struct sockaddr *)&him, &len) == -1) {
if (VmPrimsJVM.JVM_GetSockName(fd, him, len_Pointer) == -1) {
// 535 "Error getting socket name");
throw new SocketException("Error getting socket name");
} else {
// 537 localport = NET_GetPortFromSockaddr((struct sockaddr *)&him);
localport = JavaNetNetUtilMD.NET_GetPortFromSockaddr(him);
// 538 (*env)->SetIntField(env, this, psi_localportID, localport);
Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).localport = localport;
}
}
}
use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.
the class Util_jni method lookupIfLocalhost.
static InetAddress[] lookupIfLocalhost(CCharPointer hostname, boolean includeV6) throws SocketException, InterruptedException {
/* #ifdef MACOSX */
if (IsDefined.MACOSX()) {
/* also called from Inet4AddressImpl.c */
InetAddress[] result = null;
CCharPointer myhostname = StackValue.get(Netdb.NI_MAXHOST() + 1, SizeOf.get(CCharPointer.class));
Ifaddrs.ifaddrs ifa = WordFactory.nullPointer();
Ifaddrs.ifaddrsPointer ifaPointer = StackValue.get(SizeOf.get(Ifaddrs.ifaddrsPointer.class));
int i;
int j;
int addrs4 = 0;
int addrs6 = 0;
int numV4Loopbacks = 0;
int numV6Loopbacks = 0;
boolean includeLoopback = false;
String name;
/*
* If the requested name matches this host's hostname, return IP addresses from all
* attached interfaces. (#2844683 et al) This prevents undesired PPP dialup, but may
* return addresses that don't actually correspond to the name (if the name actually
* matches something in DNS etc.
*/
myhostname.write(0, (byte) '\0');
if (Unistd.gethostname(myhostname, WordFactory.unsigned(Netdb.NI_MAXHOST())) == 0) {
/* Something went wrong, maybe networking is not setup? */
return null;
}
myhostname.write(Netdb.NI_MAXHOST(), (byte) '\0');
if (LibC.strcmp(myhostname, hostname) != 0) {
// Non-self lookup
return null;
}
try {
if (Ifaddrs.getifaddrs(ifaPointer) != 0) {
JavaNetNetUtilMD.NET_ThrowNew(Errno.errno(), "Can't get local interface addresses");
return null;
}
ifa = ifaPointer.read();
name = CTypeConversion.toJavaString(hostname);
/*
* Iterate over the interfaces, and total up the number of IPv4 and IPv6 addresses
* we have. Also keep a count of loopback addresses. We need to exclude them in the
* normal case, but return them if we don't get an IP address.
*/
Ifaddrs.ifaddrs iter = ifa;
while (iter.isNonNull()) {
int family = iter.ifa_addr().sa_family();
if ((iter.ifa_name().read(0) != '\0') && iter.ifa_addr().isNonNull()) {
boolean isLoopback = ((iter.ifa_flags() & NetIf.IFF_LOOPBACK()) != 0);
if (family == Socket.AF_INET()) {
addrs4++;
if (isLoopback) {
numV4Loopbacks++;
}
} else if ((family == Socket.AF_INET6()) && includeV6) {
addrs6++;
if (isLoopback) {
numV6Loopbacks++;
}
} else {
/* We don't care e.g. AF_LINK */
}
}
iter = iter.ifa_next();
}
if ((addrs4 == numV4Loopbacks) && (addrs6 == numV6Loopbacks)) {
// We don't have a real IP address, just loopback. We need to include
// loopback in our results.
includeLoopback = true;
}
/* Create and fill the Java array. */
int arraySize = addrs4 + addrs6 - (includeLoopback ? 0 : (numV4Loopbacks + numV6Loopbacks));
result = new InetAddress[arraySize];
if (Target_java_net_InetAddress.preferIPv6Address) {
i = includeLoopback ? addrs6 : (addrs6 - numV6Loopbacks);
j = 0;
} else {
i = 0;
j = includeLoopback ? addrs4 : (addrs4 - numV4Loopbacks);
}
// Now loop around the ifaddrs
iter = ifa;
while (iter.isNonNull()) {
boolean isLoopback = ((iter.ifa_flags() & NetIf.IFF_LOOPBACK()) != 0);
int family = iter.ifa_addr().sa_family();
if ((iter.ifa_name().read(0) != '\0') && (iter.ifa_addr().isNonNull()) && ((family == Socket.AF_INET()) || ((family == Socket.AF_INET6() && includeV6))) && ((!isLoopback) || includeLoopback)) {
int index = (family == Socket.AF_INET()) ? i++ : j++;
// The space pointed to by portPointer is unused here,
// but I have to allocate it because it gets written by the call.
CIntPointer portPointer = StackValue.get(SizeOf.get(CIntPointer.class));
InetAddress o = JavaNetNetUtil.NET_SockaddrToInetAddress(iter.ifa_addr(), portPointer);
if (o != null) {
throw new OutOfMemoryError("Object allocation failed");
}
JavaNetNetUtil.setInetAddress_hostName(o, name);
result[index] = o;
}
iter = iter.ifa_next();
}
} finally {
/* done: */
Ifaddrs.freeifaddrs(ifa);
}
return result;
// #endif MACOSX
} else {
return null;
}
}
use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.
the class PosixJavaLangSubstitutions method waitForProcessExit.
@Substitute
@SuppressWarnings({ "static-method" })
int waitForProcessExit(int ppid) {
CIntPointer statusptr = StackValue.get(SizeOf.get(CIntPointer.class));
while (Wait.waitpid(ppid, statusptr, 0) < 0) {
if (Errno.errno() == Errno.ECHILD()) {
return 0;
} else if (Errno.errno() != Errno.EINTR()) {
return -1;
}
}
int status = statusptr.read();
if (Wait.WIFEXITED(status)) {
return Wait.WEXITSTATUS(status);
} else if (Wait.WIFSIGNALED(status)) {
// Exited because of signal: return 0x80 + signal number like shells do
return 0x80 + Wait.WTERMSIG(status);
}
return status;
}
use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.
the class PthreadThreadLocal method initialize.
@Uninterruptible(reason = "Called from uninterruptible code. Too early for safepoints.")
public void initialize() {
CIntPointer keyPtr = StackValue.get(SizeOf.get(CIntPointer.class));
PthreadVMLockSupport.checkResult(Pthread.pthread_key_create(keyPtr, WordFactory.nullPointer()), "pthread_key_create");
key = keyPtr.read();
}
Aggregations