Search in sources :

Example 31 with CCharPointer

use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.

the class RealLog method character.

@Override
public Log character(char value) {
    CCharPointer bytes = StackValue.get(SizeOf.get(CCharPointer.class));
    bytes.write((byte) value);
    rawBytes(bytes, WordFactory.unsigned(1));
    return this;
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 32 with CCharPointer

use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.

the class JavavmExportJvm method NET_SockaddrToInetAddress.

// 226 JNIEXPORT jobject JNICALL
// 227 NET_SockaddrToInetAddress(JNIEnv *env, struct sockaddr *him, int *port) {
@SuppressWarnings({ "unused" })
static InetAddress NET_SockaddrToInetAddress(Socket.sockaddr him, CIntPointer port) {
    // 228 jobject iaObj;
    InetAddress iaObj;
    // 231 if (him->sa_family == AF_INET6) {
    if (him.sa_family() == Socket.AF_INET6()) {
        // 232 jbyteArray ipaddress;
        byte[] ipaddress;
        // 233 #ifdef WIN32
        // 234 struct SOCKADDR_IN6 *him6 = (struct SOCKADDR_IN6 *)him;
        // 235 #else
        // 236 struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
        // 237 #endif
        NetinetIn.sockaddr_in6 him6 = (NetinetIn.sockaddr_in6) him;
        // 238 jbyte *caddr = (jbyte *)&(him6->sin6_addr);
        CCharPointer caddr = (CCharPointer) him6.sin6_addr();
        // 239 if (NET_IsIPv4Mapped(caddr)) {
        if (JavaNetNetUtil.isIPv4Mapped(caddr)) {
            // 240 int address;
            int address;
            // 249 iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
            iaObj = Util_java_net_Inet4Address.new_Inet4Address();
            // 250 CHECK_NULL_RETURN(iaObj, NULL);
            if (iaObj == null) {
                return null;
            }
            // 251 address = NET_IPv4MappedToIPv4(caddr);
            address = JavaNetNetUtilMD.NET_IPv4MappedToIPv4(caddr);
            // 252 setInetAddress_addr(env, iaObj, address);
            JavaNetNetUtil.setInetAddress_addr(iaObj, address);
            // 253 setInetAddress_family(env, iaObj, IPv4);
            JavaNetNetUtil.setInetAddress_family(iaObj, Target_java_net_InetAddress.IPv4);
        } else {
            // 256 jint scope;
            int scope;
            // 257 int ret;
            int ret;
            // 265 iaObj = (*env)->NewObject(env, inet6Cls, ia6_ctrID);
            iaObj = Util_java_net_Inet6Address.new_Inet6Address();
            // 266 CHECK_NULL_RETURN(iaObj, NULL);
            if (iaObj == null) {
                return null;
            }
            // 267 ret = setInet6Address_ipaddress(env, iaObj, (char *)&(him6->sin6_addr));
            ret = JavaNetNetUtil.setInet6Address_ipaddress((Inet6Address) iaObj, him6.sin6_addr().s6_addr());
            // 268 CHECK_NULL_RETURN(ret, NULL);
            if (ret == 0) {
                return null;
            }
            // 269 setInetAddress_family(env, iaObj, IPv6);
            JavaNetNetUtil.setInetAddress_family(iaObj, Target_java_net_InetAddress.IPv6);
            // 270 scope = getScopeID(him);
            scope = JavaNetNetUtilMD.getScopeID(him);
            // 271 setInet6Address_scopeid(env, iaObj, scope);
            JavaNetNetUtil.setInet6Address_scopeid((Inet6Address) iaObj, scope);
        }
        // 273 *port = ntohs(him6->sin6_port);
        port.write(NetinetIn.ntohs(him6.sin6_port()));
    } else {
        // 277 struct sockaddr_in *him4 = (struct sockaddr_in *)him;
        NetinetIn.sockaddr_in him4 = (NetinetIn.sockaddr_in) him;
        // 287 iaObj = (*env)->NewObject(env, inet4Cls, ia4_ctrID);
        iaObj = Util_java_net_Inet4Address.new_Inet4Address();
        // 288 CHECK_NULL_RETURN(iaObj, NULL);
        if (iaObj == null) {
            return null;
        }
        // 289 setInetAddress_family(env, iaObj, IPv4);
        JavaNetNetUtil.setInetAddress_family(iaObj, Target_java_net_InetAddress.IPv4);
        // 290 setInetAddress_addr(env, iaObj, ntohl(him4->sin_addr.s_addr));
        JavaNetNetUtil.setInetAddress_addr(iaObj, NetinetIn.ntohl(him4.sin_addr().s_addr()));
        // 291 *port = ntohs(him4->sin_port);
        port.write(NetinetIn.ntohs(him4.sin_port()));
    }
    // 293 return iaObj;
    return iaObj;
}
Also used : Inet6Address(java.net.Inet6Address) NetinetIn(com.oracle.svm.core.posix.headers.NetinetIn) InetAddress(java.net.InetAddress) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 33 with CCharPointer

use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.

the class Util_jni method getHostByAddr.

@Substitute
@SuppressWarnings({ "static-method" })
public String getHostByAddr(byte[] addrArray) throws UnknownHostException {
    String ret = null;
    CCharPointer host = StackValue.get(Netdb.NI_MAXHOST() + 1);
    int error = 0;
    int len = 0;
    CCharPointer caddr = StackValue.get(16);
    NetinetIn.sockaddr_in him4 = StackValue.get(SizeOf.get(NetinetIn.sockaddr_in.class));
    NetinetIn.sockaddr_in6 him6 = StackValue.get(SizeOf.get(NetinetIn.sockaddr_in6.class));
    Socket.sockaddr sa;
    if (addrArray.length == 4) {
        /*
             * For IPv4 addresses construct a sockaddr_in structure.
             */
        int addr = 0;
        addr |= ((addrArray[0] << 24) & 0xff000000);
        addr |= ((addrArray[1] << 16) & 0xff0000);
        addr |= ((addrArray[2] << 8) & 0xff00);
        addr |= ((addrArray[3] << 0) & 0xff);
        LibC.memset(him4, WordFactory.signed(0), SizeOf.unsigned(NetinetIn.sockaddr_in.class));
        him4.sin_addr().set_s_addr(NetinetIn.htonl(addr));
        him4.set_sin_family(Socket.AF_INET());
        sa = (Socket.sockaddr) him4;
        len = SizeOf.get(NetinetIn.sockaddr_in.class);
    } else {
        /*
             * For IPv6 address construct a sockaddr_in6 structure.
             */
        try (PinnedObject pinnedAddrArray = PinnedObject.create(addrArray)) {
            CCharPointer addrArray0 = pinnedAddrArray.addressOfArrayElement(0);
            LibC.memcpy(caddr, addrArray0, WordFactory.unsigned(16));
        }
        LibC.memset(him6, WordFactory.signed(0), SizeOf.unsigned(NetinetIn.sockaddr_in6.class));
        LibC.memcpy(him6.sin6_addr(), caddr, SizeOf.unsigned(NetinetIn.in6_addr.class));
        him6.set_sin6_family(Socket.AF_INET6());
        sa = (Socket.sockaddr) him6;
        len = SizeOf.get(NetinetIn.sockaddr_in6.class);
    }
    error = Netdb.getnameinfo(sa, len, host, Netdb.NI_MAXHOST(), WordFactory.nullPointer(), 0, Netdb.NI_NAMEREQD());
    if (error == 0) {
        ret = CTypeConversion.toJavaString(host);
    }
    if (ret == null) {
        throw new UnknownHostException();
    }
    return ret;
}
Also used : UnknownHostException(java.net.UnknownHostException) PinnedObject(org.graalvm.nativeimage.PinnedObject) NetinetIn(com.oracle.svm.core.posix.headers.NetinetIn) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) ServerSocket(java.net.ServerSocket) Socket(com.oracle.svm.core.posix.headers.Socket) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 34 with CCharPointer

use of org.graalvm.nativeimage.c.type.CCharPointer 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;
        }
    }
}
Also used : SocketException(java.net.SocketException) InterruptedIOException(java.io.InterruptedIOException) ProtocolException(java.net.ProtocolException) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) NoRouteToHostException(java.net.NoRouteToHostException) Util_java_io_FileDescriptor(com.oracle.svm.core.posix.PosixOSInterface.Util_java_io_FileDescriptor) FileDescriptor(java.io.FileDescriptor) SocketTimeoutException(java.net.SocketTimeoutException) Poll(com.oracle.svm.core.posix.headers.Poll) PinnedObject(org.graalvm.nativeimage.PinnedObject) ServerSocket(java.net.ServerSocket) Socket(com.oracle.svm.core.posix.headers.Socket) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) ConnectException(java.net.ConnectException) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 35 with CCharPointer

use of org.graalvm.nativeimage.c.type.CCharPointer in project graal by oracle.

the class Util_jni method getLocalHostName.

@Substitute
// 354 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
@SuppressWarnings({ "static-method" })
public String getLocalHostName() {
    // 355     char hostname[NI_MAXHOST+1];
    CCharPointer hostname = StackValue.get(Netdb.NI_MAXHOST() + 1, SizeOf.get(CCharPointer.class));
    // 357     hostname[0] = '\0';
    hostname.write(0, (byte) '\0');
    // 358     if (JVM_GetHostName(hostname, sizeof(hostname))) {
    if (CTypeConversion.toBoolean(VmPrimsJVM.JVM_GetHostName(hostname, Netdb.NI_MAXHOST() + 1))) {
        // 360         strcpy(hostname, "localhost");
        try (CCharPointerHolder pin = CTypeConversion.toCString("localhost")) {
            LibC.strcpy(hostname, pin.get());
        }
    } else {
        // 362         struct addrinfo hints, *res;
        Netdb.addrinfo hints = StackValue.get(SizeOf.get(Netdb.addrinfo.class));
        Netdb.addrinfoPointer res = StackValue.get(SizeOf.get(Netdb.addrinfoPointer.class));
        // 363         int error;
        int error;
        // 365         hostname[NI_MAXHOST] = '\0';
        hostname.write(Netdb.NI_MAXHOST(), (byte) '\0');
        // 366         memset(&hints, 0, sizeof(hints));
        LibC.memset(hints, WordFactory.zero(), SizeOf.unsigned(Netdb.addrinfo.class));
        // 367         hints.ai_flags = AI_CANONNAME;
        hints.set_ai_flags(Netdb.AI_CANONNAME());
        // 368         hints.ai_family = AF_INET;
        hints.set_ai_family(Socket.AF_INET());
        // 370         error = getaddrinfo(hostname, NULL, &hints, &res);
        error = Netdb.getaddrinfo(hostname, WordFactory.nullPointer(), hints, res);
        // 372         if (error == 0) {/* host is known to name service */
        if (error == 0) {
            // 373             getnameinfo(res->ai_addr,
            // 374                         res->ai_addrlen,
            // 375                         hostname,
            // 376                         NI_MAXHOST,
            // 377                         NULL,
            // 378                         0,
            // 379                         NI_NAMEREQD);
            Netdb.getnameinfo(res.read().ai_addr(), res.read().ai_addrlen(), hostname, Netdb.NI_MAXHOST(), WordFactory.nullPointer(), 0, Netdb.NI_NAMEREQD());
            // 381             /* if getnameinfo fails hostname is still the value
            // 382                from gethostname */
            // 384             freeaddrinfo(res);
            Netdb.freeaddrinfo(res.read());
        }
    }
    // 387     return (*env)->NewStringUTF(env, hostname);
    return CTypeConversion.toJavaString(hostname);
}
Also used : Netdb(com.oracle.svm.core.posix.headers.Netdb) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)47 Substitute (com.oracle.svm.core.annotate.Substitute)18 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)15 PinnedObject (org.graalvm.nativeimage.PinnedObject)8 IOException (java.io.IOException)7 NetinetIn (com.oracle.svm.core.posix.headers.NetinetIn)6 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)6 SocketException (java.net.SocketException)5 SignedWord (org.graalvm.word.SignedWord)5 Inet6Address (java.net.Inet6Address)4 InetAddress (java.net.InetAddress)4 UnsignedWord (org.graalvm.word.UnsignedWord)4 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)3 Socket (com.oracle.svm.core.posix.headers.Socket)3 InterruptedIOException (java.io.InterruptedIOException)3 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)3 CCharPointerPointer (org.graalvm.nativeimage.c.type.CCharPointerPointer)3 PosixOSInterface.lastErrorString (com.oracle.svm.core.posix.PosixOSInterface.lastErrorString)2 DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)2 Dirent.dirent (com.oracle.svm.core.posix.headers.Dirent.dirent)2