Search in sources :

Example 36 with CCharPointer

use of org.graalvm.nativeimage.c.type.CCharPointer 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;
    }
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) InetAddress(java.net.InetAddress) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Ifaddrs(com.oracle.svm.core.posix.headers.Ifaddrs)

Example 37 with CCharPointer

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

the class PosixOSInterface method writeBytes0Uninterruptibly.

@Uninterruptible(reason = "byte[] accessed without pinning")
private boolean writeBytes0Uninterruptibly(FileDescriptor descriptor, byte[] bytes, UnsignedWord length, UnsignedWord offsetOfArrayElement) {
    final Pointer addressOfObject = Word.objectToUntrackedPointer(bytes);
    final CCharPointer bytePointer = (CCharPointer) addressOfObject.add(offsetOfArrayElement);
    return writeBytesUninterruptibly(descriptor, bytePointer, length);
}
Also used : Pointer(org.graalvm.word.Pointer) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible)

Example 38 with CCharPointer

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

the class PosixOSInterface method writeBytesUninterruptibly.

@Override
@Uninterruptible(reason = "Bytes might be from an Object.")
public boolean writeBytesUninterruptibly(FileDescriptor descriptor, CCharPointer bytes, UnsignedWord length) {
    CCharPointer curBuf = bytes;
    UnsignedWord curLen = length;
    while (curLen.notEqual(0)) {
        int fd = Util_java_io_FileDescriptor.getFD(descriptor);
        if (fd == -1) {
            return false;
        }
        SignedWord n = UnistdNoTransitions.write(fd, curBuf, curLen);
        if (n.equal(-1)) {
            return false;
        }
        curBuf = curBuf.addressOf(n);
        curLen = curLen.subtract((UnsignedWord) n);
    }
    return true;
}
Also used : UnsignedWord(org.graalvm.word.UnsignedWord) SignedWord(org.graalvm.word.SignedWord) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible)

Example 39 with CCharPointer

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

the class PosixSystemPropertiesSupport method userDirValue.

@Override
protected String userDirValue() {
    int bufSize = MAXPATHLEN();
    CCharPointer buf = StackValue.get(bufSize);
    if (Unistd.getcwd(buf, WordFactory.unsigned(bufSize)).isNonNull()) {
        return CTypeConversion.toJavaString(buf);
    } else {
        throw new java.lang.Error("Properties init: Could not determine current working directory.");
    }
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 40 with CCharPointer

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

the class PosixUtils method readSingle.

static int readSingle(FileDescriptor fd) throws IOException {
    CCharPointer retPtr = StackValue.get(SizeOf.get(CCharPointer.class));
    int handle = PosixUtils.getFDHandle(fd);
    SignedWord nread = read(handle, retPtr, WordFactory.unsigned(1));
    if (nread.equal(0)) {
        // EOF
        return -1;
    } else if (nread.equal(-1)) {
        throw new IOException(lastErrorString("Read error"));
    }
    return retPtr.read() & 0xFF;
}
Also used : IOException(java.io.IOException) SignedWord(org.graalvm.word.SignedWord) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

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