Search in sources :

Example 11 with CCharPointer

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

the class PosixJavaLangSubstitutions method doForkAndExec.

@SuppressWarnings("try")
static int doForkAndExec(CCharPointer file, CCharPointer dir, CCharPointerPointer argv, CCharPointerPointer envp, int[] stdioFds, int failFd) {
    final int buflen = SizeOf.get(dirent.class) + Limits.PATH_MAX() + 1;
    final boolean haveProcFs = Platform.includedIn(Platform.LINUX.class);
    try (// only this thread will exist in the child and garbage collection is not possible.
    PinnedObject bufferPin = PinnedObject.create(new byte[buflen]);
        CCharPointerHolder procFdsPath = haveProcFs ? CTypeConversion.toCString("/proc/self/fd/") : null;
        CCharPointerHolder searchPaths = CTypeConversion.toCString(System.getenv("PATH"));
        CCharPointerHolder searchPathSeparator = CTypeConversion.toCString(":");
        NoAllocationVerifier v = NoAllocationVerifier.factory("fragile state after fork()")) {
        CCharPointer procFdsPathPtr = (procFdsPath != null) ? procFdsPath.get() : WordFactory.nullPointer();
        return uninterruptibleForkAndExec(file, dir, argv, envp, stdioFds, failFd, bufferPin.addressOfArrayElement(0), buflen, procFdsPathPtr, searchPaths.get(), searchPathSeparator.get());
    }
}
Also used : Platform(org.graalvm.nativeimage.Platform) PinnedObject(org.graalvm.nativeimage.PinnedObject) NoAllocationVerifier(com.oracle.svm.core.heap.NoAllocationVerifier) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 12 with CCharPointer

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

the class Util_jni method socketWrite0.

/* Do not re-format commented-out code: @formatter:off */
// 056 /*
// 057  * Class:     java_net_SocketOutputStream
// 058  * Method:    socketWrite0
// 059  * Signature: (Ljava/io/FileDescriptor;[BII)V
// 060  */
// 061 JNIEXPORT void JNICALL
// 062 Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
// 063                                               jobject fdObj,
// 064                                               jbyteArray data,
// 065                                               jint off, jint len) {
@Substitute
@SuppressWarnings({ "static-method", "finally" })
private void socketWrite0(FileDescriptor fdObj, byte[] data, int offArg, int lenArg) throws IOException {
    /* Local variable copies rather than assign to formal parameter. */
    int off = offArg;
    int len = lenArg;
    // 066     char *bufP;
    CCharPointer bufP = WordFactory.nullPointer();
    // 067     char BUF[MAX_BUFFER_LEN];
    CCharPointer BUF = StackValue.get(JavaNetNetUtilMD.MAX_BUFFER_LEN(), SizeOf.get(CCharPointer.class));
    // 068     int buflen;
    int buflen;
    // 069     int fd;
    int fd;
    // 071     if (IS_NULL(fdObj)) {
    if (fdObj == null) {
        // 073         return;
        throw new SocketException("socket closed");
    } else {
        // 075         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
        fd = Util_java_io_FileDescriptor.getFD(fdObj);
        // 079         if (fd == -1) {
        if (fd == -1) {
            // 081             return;
            throw new SocketException("Socket closed");
        }
    }
    // 086     if (len <= MAX_BUFFER_LEN) {
    if (len <= JavaNetNetUtilMD.MAX_BUFFER_LEN()) {
        // 087         bufP = BUF;
        bufP = BUF;
        // 088         buflen = MAX_BUFFER_LEN;
        buflen = JavaNetNetUtilMD.MAX_BUFFER_LEN();
    } else {
        // 090         buflen = min(MAX_HEAP_BUFFER_LEN, len);
        buflen = Integer.min(JavaNetNetUtilMD.MAX_HEAP_BUFFER_LEN(), len);
        // 091         bufP = (char *)malloc((size_t)buflen);
        bufP = LibC.malloc(WordFactory.unsigned(buflen));
        // 094         if (bufP == NULL) {
        if (bufP.isNull()) {
            // 095             bufP = BUF;
            bufP = BUF;
            // 096             buflen = MAX_BUFFER_LEN;
            buflen = JavaNetNetUtilMD.MAX_HEAP_BUFFER_LEN();
        }
    }
    try {
        // 100     while(len > 0) {
        while (len > 0) {
            // 101         int loff = 0;
            int loff = 0;
            // 102         int chunkLen = min(buflen, len);
            int chunkLen = Integer.min(buflen, len);
            // 103         int llen = chunkLen;
            int llen = chunkLen;
            // 104         (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
            VmPrimsJNI.GetByteArrayRegion(data, off, chunkLen, bufP);
            // 106         while(llen > 0) {
            while (llen > 0) {
                try {
                    // 107             int n = NET_Send(fd, bufP + loff, llen, 0);
                    int n = JavaNetNetUtilMD.NET_Send(fd, bufP.addressOf(loff), llen, 0);
                    // 108             if (n > 0) {
                    if (n > 0) {
                        // 109                 llen -= n;
                        llen -= n;
                        // 110                 loff += n;
                        loff += n;
                        // 111                 continue;
                        continue;
                    }
                    // 113             if (n == JVM_IO_INTR) {
                    if (n == Target_jvm.JVM_IO_INTR()) {
                        // 114                 JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
                        throw new InterruptedIOException();
                    } else {
                        // 116                 if (errno == ECONNRESET) {
                        if (Errno.errno() == Errno.ECONNRESET()) {
                            // 118                         "Connection reset");
                            throw new sun.net.ConnectionResetException("Connection reset");
                        } else {
                            // 121                         "Write failed");
                            throw new SocketException("Write failed");
                        }
                    }
                } finally {
                    // 124             if (bufP != BUF) {
                    if (bufP.notEqual(BUF) && bufP.isNonNull()) {
                        // 125                 free(bufP);
                        LibC.free(bufP);
                        bufP = WordFactory.nullPointer();
                    }
                    // 127             return;
                    return;
                }
            }
            // 129         len -= chunkLen;
            len -= chunkLen;
            // 130         off += chunkLen;
            off += chunkLen;
        }
    } finally {
        // 133     if (bufP != BUF) {
        if (bufP.notEqual(BUF) && bufP.isNonNull()) {
            // 134         free(bufP);
            LibC.free(bufP);
            bufP = WordFactory.nullPointer();
        }
    }
}
Also used : SocketException(java.net.SocketException) InterruptedIOException(java.io.InterruptedIOException) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 13 with CCharPointer

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

the class Util_jni method socketRead0.

/* Do not re-format commeted-out code: @formatter:off
    // 055 /*
    // 056  * Class: java_net_SocketInputStream
    // 057  * Method: socketRead0
    // 058  * Signature: (Ljava/io/FileDescriptor;[BIII)I
    // 059  */
// 060 JNIEXPORT jint JNICALL
// 061 Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
// 062                                             jobject fdObj, jbyteArray data,
// 063                                             jint off, jint len, jint timeout)
@Substitute
@SuppressWarnings({ "static-method", "finally" })
private int socketRead0(FileDescriptor fdObj, byte[] data, int off, int lenArg, int timeout) throws IOException, OutOfMemoryError, sun.net.ConnectionResetException {
    int len = lenArg;
    // 065     char BUF[MAX_BUFFER_LEN];
    CCharPointer BUF = StackValue.get(JavaNetNetUtilMD.MAX_BUFFER_LEN(), SizeOf.get(CCharPointer.class));
    // 066     char *bufP;
    CCharPointer bufP = WordFactory.nullPointer();
    // 067     jint fd, nread;
    int fd;
    int nread;
    // 069     if (IS_NULL(fdObj)) {
    if (fdObj == null) {
        // 073         return -1;
        throw new SocketException("Socket closed");
    } else {
        // 075         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
        fd = Util_java_io_FileDescriptor.getFD(fdObj);
        // 079         if (fd == -1) {
        if (fd == -1) {
            // 081         return -1;
            throw new SocketException("Socket closed");
        }
    }
    // 089     if (len > MAX_BUFFER_LEN) {
    if (len > JavaNetNetUtilMD.MAX_BUFFER_LEN()) {
        // 090      if (len > MAX_HEAP_BUFFER_LEN) {
        if (len > JavaNetNetUtilMD.MAX_HEAP_BUFFER_LEN()) {
            // 091      len = MAX_HEAP_BUFFER_LEN;
            len = JavaNetNetUtilMD.MAX_HEAP_BUFFER_LEN();
        }
        // 093          bufP = (char *)malloc((size_t)len);
        bufP = LibC.malloc(WordFactory.unsigned(len));
        // 094          if (bufP == NULL) {
        if (bufP.isNull()) {
            // 095         bufP = BUF;
            bufP = BUF;
            // 096         len = MAX_BUFFER_LEN;
            len = JavaNetNetUtilMD.MAX_BUFFER_LEN();
        }
    } else {
        // 099     bufP = BUF;
        bufP = BUF;
    }
    // 102     if (timeout) {
    if (CTypeConversion.toBoolean(timeout)) {
        // 103         nread = NET_Timeout(fd, timeout);
        nread = JavaNetNetUtilMD.NET_Timeout(fd, timeout);
        // 104         if (nread <= 0) {
        if (nread <= 0) {
            try {
                // 105             if (nread == 0) {
                if (nread == 0) {
                    // 107                             "Read timed out");
                    throw new SocketTimeoutException("Read timed out");
                // 108             } else if (nread == JVM_IO_ERR) {
                } else if (nread == Target_jvm.JVM_IO_ERR()) {
                    // 109                 if (errno == EBADF) {
                    if (Errno.errno() == Errno.EBADF()) {
                        // 110                      JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
                        throw new SocketException("Socket closed");
                    // 111                  } else if (errno == ENOMEM) {
                    } else if (Errno.errno() == Errno.ENOMEM()) {
                        // 112                      JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
                        throw new OutOfMemoryError("NET_Timeout native heap allocation failed");
                    } else {
                        // 115                                                   "select/poll failed");
                        throw new SocketException("select/poll failed");
                    }
                // 117             } else if (nread == JVM_IO_INTR) {
                } else if (nread == Target_jvm.JVM_IO_INTR()) {
                    // 119                             "Operation interrupted");
                    throw new InterruptedException("Operation interrupted");
                }
            } finally {
                // 121             if (bufP != BUF) {
                if (bufP.notEqual(BUF) && bufP.isNonNull()) {
                    // 122                 free(bufP);
                    LibC.free(bufP);
                    bufP = WordFactory.nullPointer();
                }
                // 124             return -1;
                return -1;
            }
        }
    }
    try {
        // 128     nread = NET_Read(fd, bufP, len);
        nread = JavaNetNetUtilMD.NET_Read(fd, bufP, len);
        // 130     if (nread <= 0) {
        if (nread <= 0) {
            // 131         if (nread < 0) {
            if (nread < 0) {
                // 135                 case EPIPE:
                if ((Errno.errno() == Errno.ECONNRESET()) || (Errno.errno() == Errno.EPIPE())) {
                    // 138                     break;
                    throw new sun.net.ConnectionResetException("Connection reset");
                // 140                 case EBADF:
                } else if (Errno.errno() == Errno.EBADF()) {
                    // 143                     break;
                    throw new SocketException("Socket closed");
                // 145                 case EINTR:
                } else if (Errno.errno() == Errno.EINTR()) {
                    // 148                      break;
                    throw new InterruptedIOException("Operation interrupted");
                // 150                 default:
                } else {
                    // 152                         JNU_JAVANETPKG "SocketException", "Read failed");
                    throw new SocketException("Read failed");
                }
            }
        } else {
            // 156         (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
            VmPrimsJNI.SetByteArrayRegion(data, off, nread, bufP);
        }
    } finally {
        // 159     if (bufP != BUF) {
        if (bufP.notEqual(BUF) && bufP.isNonNull()) {
            // 160         free(bufP);
            LibC.free(bufP);
            bufP = WordFactory.nullPointer();
        }
    }
    return nread;
}
Also used : SocketException(java.net.SocketException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 14 with CCharPointer

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

the class JavavmExportJvm method NET_InetAddressToSockaddr.

/* Do not re-wrap comments: @formatter:off */
// 780 /* In the case of an IPv4 Inetaddress this method will return an
// 781 * IPv4 mapped address where IPv6 is available and v4MappedAddress is TRUE.
// 782 * Otherwise it will return a sockaddr_in structure for an IPv4 InetAddress.
// 783 */
// 784 JNIEXPORT int JNICALL
// 785 NET_InetAddressToSockaddr(JNIEnv *env, jobject iaObj, int port, struct sockaddr *him,
// 786 int *len, jboolean v4MappedAddress) {
static int NET_InetAddressToSockaddr(InetAddress iaObj, int port, Socket.sockaddr him, CIntPointer len, boolean v4MappedAddress) throws SocketException {
    // 787 jint family;
    int family;
    // 788 family = getInetAddress_family(env, iaObj);
    family = JavaNetNetUtil.getInetAddress_family(iaObj);
    // 791 if (ipv6_available() && !(family == IPv4 && v4MappedAddress == JNI_FALSE)) {
    if (JavaNetNetUtil.ipv6_available() && (!((family == Target_java_net_InetAddress.IPv4) && (v4MappedAddress == Util_jni.JNI_FALSE())))) {
        // 792 struct sockaddr_in6 *him6 = (struct sockaddr_in6 *)him;
        NetinetIn.sockaddr_in6 him6 = (NetinetIn.sockaddr_in6) him;
        // 793 jbyte caddr[16];
        CCharPointer caddr = StackValue.get(16, SizeOf.get(CCharPointer.class));
        // 794 jint address;
        int address;
        // 797 if (family == IPv4) { /* will convert to IPv4-mapped address */
        if (family == Target_java_net_InetAddress.IPv4) {
            // 798 memset((char *) caddr, 0, 16);
            LibC.memset(caddr, WordFactory.zero(), WordFactory.unsigned(16));
            // 799 address = getInetAddress_addr(env, iaObj);
            address = getInetAddress_addr(iaObj);
            // 800 if (address == INADDR_ANY) {
            if (address == NetinetIn.INADDR_ANY()) {
            /* we would always prefer IPv6 wildcard address
                     * 802 caddr[10] = 0xff;
                     * 803 caddr[11] = 0xff;
                     */
            } else {
                // 805 caddr[10] = 0xff;
                caddr.write(10, (byte) 0xff);
                // 806 caddr[11] = 0xff;
                caddr.write(11, (byte) 0xff);
                // 807 caddr[12] = ((address >> 24) & 0xff);
                caddr.write(12, (byte) ((address >> 24) & 0xff));
                // 808 caddr[13] = ((address >> 16) & 0xff);
                caddr.write(13, (byte) ((address >> 16) & 0xff));
                // 809 caddr[14] = ((address >> 8) & 0xff);
                caddr.write(14, (byte) ((address >> 8) & 0xff));
                // 810 caddr[15] = (address & 0xff);
                caddr.write(15, (byte) (address & 0xff));
            }
        } else {
            // 813 getInet6Address_ipaddress(env, iaObj, (char *)caddr);
            JavaNetNetUtil.getInet6Address_ipAddress((Inet6Address) iaObj, caddr);
        }
        // 815 memset((char *)him6, 0, sizeof(struct sockaddr_in6));
        LibC.memset(him6, WordFactory.zero(), SizeOf.unsigned(NetinetIn.sockaddr_in6.class));
        // 816 him6->sin6_port = htons(port);
        him6.set_sin6_port(NetinetIn.htons(port));
        // 817 memcpy((void *)&(him6->sin6_addr), caddr, sizeof(struct in6_addr) );
        LibC.memcpy(him6.sin6_addr(), caddr, SizeOf.unsigned(NetinetIn.in6_addr.class));
        // 818 him6->sin6_family = AF_INET6;
        him6.set_sin6_family(Socket.AF_INET6());
        // 819 *len = sizeof(struct sockaddr_in6) ;
        len.write(SizeOf.get(NetinetIn.sockaddr_in6.class));
        // 821 #if defined(_ALLBSD_SOURCE) && defined(_AF_INET6)
        if (IsDefined._ALLBSD_SOURCE() && IsDefined.socket_AF_INET6()) {
        // 822 // XXXBSD: should we do something with scope id here ? see below linux comment
        // 823 /* MMM: Come back to this! */
        }
        // 838 #ifdef __linux__
        if (IsDefined.__linux__()) {
            // 839 if (IN6_IS_ADDR_LINKLOCAL(&(him6->sin6_addr))) {
            if (JavaNetNetUtilMD.IN6_IS_ADDR_LINKLOCAL(him6.sin6_addr())) {
                // 840 int cached_scope_id = 0, scope_id = 0;
                int cached_scope_id = 0;
                int scope_id = 0;
                /* I am assuming that the field "Inet6Address.cached_scope_id" always exists. */
                final boolean ia6_cachedscopeidID = true;
                // 842 if (ia6_cachedscopeidID) {
                if (ia6_cachedscopeidID) {
                    // 843     cached_scope_id = (int)(*env)->GetIntField(env, iaObj, ia6_cachedscopeidID);
                    cached_scope_id = Util_java_net_Inet6Address.from_Inet6Address((Inet6Address) iaObj).cached_scope_id;
                    // 847 if (!cached_scope_id) {
                    if (!CTypeConversion.toBoolean(cached_scope_id)) {
                        /* I am assuming that the field "Inet6Address.scope_id" exists. */
                        final boolean ia6_scopeidID = true;
                        // 848 if (ia6_scopeidID) {
                        if (ia6_scopeidID) {
                            // 849     scope_id = getInet6Address_scopeid(env, iaObj);
                            scope_id = JavaNetNetUtil.getInet6Address_scopeid((Inet6Address) iaObj);
                        }
                        // 851 if (scope_id != 0) {
                        if (scope_id != 0) {
                            // 855 if (kernelIsV24() && needsLoopbackRoute (&him6->sin6_addr)) {
                            if (JavaNetNetUtilMD.kernelIsV24() && JavaNetNetUtilMD.needsLoopbackRoute(him6.sin6_addr())) {
                                // 856 cached_scope_id = lo_scope_id;
                                cached_scope_id = lo_scope_id;
                                // 857 (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
                                Util_java_net_Inet6Address.from_Inet6Address((Inet6Address) iaObj).cached_scope_id = cached_scope_id;
                            }
                        } else {
                            // 864 if (kernelIsV24()) {
                            if (JavaNetNetUtilMD.kernelIsV24()) {
                                // 865 cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) );
                                cached_scope_id = JavaNetNetUtilMD.getDefaultIPv6Interface(him6.sin6_addr());
                            } else {
                                // 867 cached_scope_id = getLocalScopeID( (char *)&(him6->sin6_addr) );
                                cached_scope_id = JavaNetNetUtilMD.getLocalScopeID(him6.sin6_addr());
                                // 868 if (cached_scope_id == 0) {
                                if (cached_scope_id == 0) {
                                    // 869 cached_scope_id = getDefaultIPv6Interface( &(him6->sin6_addr) );
                                    cached_scope_id = getDefaultIPv6Interface(him6.sin6_addr());
                                }
                            }
                            // 872 (*env)->SetIntField(env, iaObj, ia6_cachedscopeidID, cached_scope_id);
                            Util_java_net_Inet6Address.from_Inet6Address((Inet6Address) iaObj).cached_scope_id = cached_scope_id;
                        }
                    }
                }
                // 877 /*
                // 878  * If we have a scope_id use the extended form
                // 879  * of sockaddr_in6.
                // 880  */
                // 882 struct sockaddr_in6 *him6 =
                // 883         (struct sockaddr_in6 *)him;
                NetinetIn.sockaddr_in6 him6_l882 = (NetinetIn.sockaddr_in6) him;
                // 884 him6->sin6_scope_id = cached_scope_id != 0 ?
                // 885 cached_scope_id : scope_id;
                him6_l882.set_sin6_scope_id((cached_scope_id != 0) ? cached_scope_id : scope_id);
                // 886 *len = sizeof(struct sockaddr_in6);
                len.write(SizeOf.get(NetinetIn.sockaddr_in6.class));
            }
        // 888 #else
        } else {
            // 891 if (family != IPv4) {
            if (family != Target_java_net_InetAddress.IPv4) {
                /* I am assuming that the field "Inet6Address.scopeid" exists. */
                final boolean ia6_scopeidID = true;
                // 892 if (ia6_scopeidID) {
                if (ia6_scopeidID) {
                    // 893 him6->sin6_scope_id = getInet6Address_scopeid(env, iaObj);
                    him6.set_sin6_scope_id(JavaNetNetUtil.getInet6Address_scopeid((Inet6Address) iaObj));
                }
            }
        }
    // 896 #endif /* __linux__ */
    } else // 898 #endif /* AF_INET6 */
    {
        // 900 struct sockaddr_in *him4 = (struct sockaddr_in*)him;
        NetinetIn.sockaddr_in him4 = (NetinetIn.sockaddr_in) him;
        // 901 jint address;
        int address;
        // 902 if (family == IPv6) {
        if (family == Target_java_net_InetAddress.IPv6) {
            // 903 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Protocol family unavailable");
            throw new SocketException("Protocol family unavailable");
        // 904 return -1;
        }
        // 906 memset((char *) him4, 0, sizeof(struct sockaddr_in));
        LibC.memset(him4, WordFactory.zero(), SizeOf.unsigned(NetinetIn.sockaddr_in.class));
        // 907 address = getInetAddress_addr(env, iaObj);
        address = getInetAddress_addr(iaObj);
        // 908 him4->sin_port = htons((short) port);
        him4.set_sin_port(NetinetIn.htons((short) port));
        // 909 him4->sin_addr.s_addr = (uint32_t) htonl(address);
        him4.sin_addr().set_s_addr(NetinetIn.htonl(address));
        // 910 him4->sin_family = AF_INET;
        him4.set_sin_family(Socket.AF_INET());
        // 911 *len = sizeof(struct sockaddr_in);
        len.write(SizeOf.get(NetinetIn.sockaddr_in.class));
    }
    // 913 return 0;
    return 0;
}
Also used : SocketException(java.net.SocketException) Inet6Address(java.net.Inet6Address) NetinetIn(com.oracle.svm.core.posix.headers.NetinetIn) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 15 with CCharPointer

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

the class JavaNetNetworkInterface method addif.

/*
     * Translated from jdk/src/solaris/native/java/net/NetworkInterface.c?v=Java_1.8.0_40_b10
     */
// 881 netif *addif(JNIEnv *env, int sock, const char * if_name,
// 882              netif *ifs, struct sockaddr* ifr_addrP, int family,
// 883              short prefix)
// 884 {
public static netif addif(int sock, CCharPointer if_name, netif ifsParameter, Socket.sockaddr ifr_addrP, int family, short prefix) {
    /* Work around "The parameter ifs should not be assigned" warning. */
    netif ifs = ifsParameter;
    /* Prepare for platform-specific code. */
    final PlatformSupport platformSupport = ImageSingletons.lookup(JavaNetNetworkInterface.PlatformSupport.class);
    // 885     netif *currif = ifs, *parent;
    netif currif;
    currif = ifs;
    netif parent;
    // 886     netaddr *addrP;
    netaddr addrP;
    // 887
    // 888 #ifdef LIFNAMSIZ
    /* LIFNAMSIZ is not defined on Linux or Darwin. */
    // 889     int ifnam_size = LIFNAMSIZ;
    // 890     char name[LIFNAMSIZ], vname[LIFNAMSIZ];
    // 891 #else
    // 892     int ifnam_size = IFNAMSIZ;
    int ifnam_size = NetIf.IFNAMSIZ();
    // 893     char name[IFNAMSIZ], vname[IFNAMSIZ];
    CCharPointer name = StackValue.get(NetIf.IFNAMSIZ(), SizeOf.get(CCharPointer.class));
    CCharPointer vname = StackValue.get(NetIf.IFNAMSIZ(), SizeOf.get(CCharPointer.class));
    // 894 #endif
    // 895
    // 896     char  *name_colonP;
    CCharPointer name_colonP;
    // 897     int mask;
    int mask;
    // 898     int isVirtual = 0;
    int isVirtual = 0;
    // 899     int addr_size;
    int addr_size;
    // 900     int flags = 0;
    CIntPointer flags_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
    flags_Pointer.write(0);
    // 901
    // 902     /*
    // 903      * If the interface name is a logical interface then we
    // 904      * remove the unit number so that we have the physical
    // 905      * interface (eg: hme0:1 -> hme0). NetworkInterface
    // 906      * currently doesn't have any concept of physical vs.
    // 907      * logical interfaces.
    // 908      */
    // 909     strncpy(name, if_name, ifnam_size);
    LibC.strncpy(name, if_name, WordFactory.unsigned(ifnam_size));
    // 910     name[ifnam_size - 1] = '\0';
    name.write(ifnam_size - 1, (byte) 0);
    // 911     *vname = 0;
    vname.write(0, (byte) 0);
    // 919 #ifdef AF_INET6
    if (IsDefined.socket_AF_INET6()) {
        // 920     addr_size = (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
        addr_size = (family == Socket.AF_INET()) ? SizeOf.get(NetinetIn.sockaddr_in.class) : SizeOf.get(NetinetIn.sockaddr_in6.class);
    // 921 #else
    } else {
        // 922     addr_size = sizeof(struct sockaddr_in);
        addr_size = SizeOf.get(NetinetIn.sockaddr_in.class);
    }
    // 923 #endif
    // 924
    // 925     CHECKED_MALLOC3(addrP, netaddr *, sizeof(netaddr)+2*addr_size);
    addrP = netaddr.checked_malloc(addr_size);
    // 926     addrP->addr = (struct sockaddr *)( (char *) addrP+sizeof(netaddr) );
    addrP.addr = addrP.addrSpace;
    // 927     memcpy(addrP->addr, ifr_addrP, addr_size);
    LibC.memcpy(addrP.addr, ifr_addrP, Word.unsigned(addr_size));
    // 928
    // 929     addrP->family = family;
    addrP.family = family;
    // 930     addrP->brdcast = NULL;
    addrP.brdcast = WordFactory.nullPointer();
    // 931     addrP->mask = prefix;
    addrP.mask = prefix;
    // 932     addrP->next = 0;
    addrP.next = null;
    // 933     if (family == AF_INET) {
    if (family == Socket.AF_INET()) {
        // 934        // Deal with broadcast addr & subnet mask
        // 935        struct sockaddr * brdcast_to = (struct sockaddr *) ((char *) addrP + sizeof(netaddr) + addr_size);
        Socket.sockaddr brdcast_to = addrP.brdcastSpace;
        // 936        addrP->brdcast = getBroadcast(env, sock, name,  brdcast_to );
        try {
            addrP.brdcast = platformSupport.getBroadcast(sock, name, brdcast_to);
        // 937        if ((*env)->ExceptionCheck(env) == JNI_TRUE) {
        } catch (SocketException se) {
            // 938            return ifs;
            return ifs;
        }
        // 940        if ((mask = getSubnet(env, sock, name)) != -1) {
        try {
            if ((mask = platformSupport.getSubnet(sock, name)) != -1) {
                // 941            addrP->mask = mask;
                addrP.mask = (short) mask;
            // 942        } else if((*env)->ExceptionCheck(env)) {
            }
        } catch (SocketException se) {
            // 943            return ifs;
            return ifs;
        }
    }
    // 946
    // 947     /**
    // 948      * Deal with virtual interface with colon notation e.g. eth0:1
    // 949      */
    // 950     name_colonP = strchr(name, ':');
    name_colonP = LibC.strchr(name, ':');
    // 951     if (name_colonP != NULL) {
    if (name_colonP.isNonNull()) {
        // 952       /**
        // 953        * This is a virtual interface. If we are able to access the parent
        // 954        * we need to create a new entry if it doesn't exist yet *and* update
        // 955        * the 'parent' interface with the new records.
        // 956        */
        // 957         *name_colonP = 0;
        name_colonP.write((byte) 0);
        // 958         if (getFlags(sock, name, &flags) < 0 || flags < 0) {
        if (platformSupport.getFlags(sock, name, flags_Pointer) < 0 | flags_Pointer.read() < 0) {
            // 959             // failed to access parent interface do not create parent.
            // 960             // We are a virtual interface with no parent.
            // 961             isVirtual = 1;
            isVirtual = 1;
            // 962             *name_colonP = ':';
            name_colonP.write((byte) ':');
        } else {
            // 965            // Got access to parent, so create it if necessary.
            // 966            // Save original name to vname and truncate name by ':'
            // 967             memcpy(vname, name, sizeof(vname) );
            LibC.memcpy(vname, name, Word.unsigned(ifnam_size));
            // 968             vname[name_colonP - name] = ':';
            vname.write((int) PointerUtils.absoluteDifference(name_colonP, name).rawValue(), (byte) ':');
        }
    }
    // 977     while (currif != NULL) {
    while (currif != null) {
        // 978         if (strcmp(name, currif->name) == 0) {
        if (LibC.strcmp(name, currif.name) == 0) {
            // 979             break;
            break;
        }
        // 981         currif = currif->next;
        currif = currif.next;
    }
    // 988     if (currif == NULL) {
    if (currif == null) {
        // 989          CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
        currif = netif.checked_malloc(ifnam_size);
        // 990          currif->name = (char *) currif+sizeof(netif);
        /* Done as part of the checked_malloc. */
        // 991          strncpy(currif->name, name, ifnam_size);
        LibC.strncpy(currif.name, name, Word.unsigned(ifnam_size));
        // 992          currif->name[ifnam_size - 1] = '\0';
        currif.name.write(ifnam_size - 1, (byte) 0);
        // 993          currif->index = getIndex(sock, name);
        currif.index = platformSupport.getIndex(sock, name);
        // 994          currif->addr = NULL;
        currif.addr = null;
        // 995          currif->childs = NULL;
        currif.addr = null;
        // 996          currif->virtual = isVirtual;
        currif.virtual = (byte) isVirtual;
        // 997          currif->next = ifs;
        currif.next = ifs;
        // 998          ifs = currif;
        ifs = currif;
    }
    // 1000
    // 1001     /*
    // 1002      * Finally insert the address on the interface
    // 1003      */
    // 1004     addrP->next = currif->addr;
    addrP.next = currif.addr;
    // 1005     currif->addr = addrP;
    currif.addr = addrP;
    // 1006
    // 1007     parent = currif;
    parent = currif;
    // 1012     if (vname[0]) {
    if (CTypeConversion.toBoolean(vname.read())) {
        // 1013         netaddr *tmpaddr;
        netaddr tmpaddr;
        // 1014
        // 1015         currif = parent->childs;
        currif = parent.childs;
        // 1017         while (currif != NULL) {
        while (currif != null) {
            // 1018             if (strcmp(vname, currif->name) == 0) {
            if (LibC.strcmp(vname, currif.name) == 0) {
                // 1019                 break;
                break;
            }
            // 1021             currif = currif->next;
            currif = currif.next;
        }
        // 1024         if (currif == NULL) {
        if (currif == null) {
            // 1025             CHECKED_MALLOC3(currif, netif *, sizeof(netif) + ifnam_size);
            /* Translating as two separate allocations, one Java one C. */
            currif = new netif();
            CCharPointer currifName = LibC.malloc(Word.unsigned(ifnam_size));
            checkMalloc(currifName);
            // 1026             currif->name = (char *) currif + sizeof(netif);
            currif.name = currifName;
            // 1027             strncpy(currif->name, vname, ifnam_size);
            LibC.strncpy(currif.name, vname, Word.unsigned(ifnam_size));
            // 1028             currif->name[ifnam_size - 1] = '\0';
            currif.name.write(ifnam_size - 1, (byte) 0);
            // 1029             currif->index = getIndex(sock, vname);
            currif.index = platformSupport.getIndex(sock, vname);
            // 1030             currif->addr = NULL;
            currif.addr = null;
            // 1031            /* Need to duplicate the addr entry? */
            // 1032             currif->virtual = 1;
            currif.virtual = (byte) 1;
            // 1033             currif->childs = NULL;
            currif.childs = null;
            // 1034             currif->next = parent->childs;
            currif.next = parent.childs;
            // 1035             parent->childs = currif;
            parent.childs = currif;
        }
        // 1037
        // 1038         CHECKED_MALLOC3(tmpaddr, netaddr *, sizeof(netaddr)+2*addr_size);
        /* Allocate a populated netaddr, and free and null the sockaddrs */
        tmpaddr = netaddr.checked_malloc(addr_size);
        // 1039         memcpy(tmpaddr, addrP, sizeof(netaddr));
        tmpaddr.copyFields(addrP);
        // 1040         if (addrP->addr != NULL) {
        if (addrP.addr.isNonNull()) {
            // 1041             tmpaddr->addr = (struct sockaddr *) ( (char*)tmpaddr + sizeof(netaddr) ) ;
            // 1042             memcpy(tmpaddr->addr, addrP->addr, addr_size);
            LibC.memcpy(tmpaddr.addr, addrP.addr, Word.unsigned(addr_size));
        } else {
            LibC.free(tmpaddr.addr);
            tmpaddr.addr = WordFactory.nullPointer();
        }
        // 1045         if (addrP->brdcast != NULL) {
        if (addrP.brdcast.isNonNull()) {
            // 1046             tmpaddr->brdcast = (struct sockaddr *) ((char *) tmpaddr + sizeof(netaddr)+addr_size);
            // 1047             memcpy(tmpaddr->brdcast, addrP->brdcast, addr_size);
            LibC.memcpy(tmpaddr.brdcast, addrP.brdcast, Word.unsigned(addr_size));
        } else {
            LibC.free(tmpaddr.brdcast);
            tmpaddr.brdcast = WordFactory.nullPointer();
        }
        // 1049
        // 1050         tmpaddr->next = currif->addr;
        tmpaddr.next = currif.addr;
        // 1051         currif->addr = tmpaddr;
        currif.addr = tmpaddr;
    }
    // 1054     return ifs;
    return ifs;
}
Also used : SocketException(java.net.SocketException) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) NetinetIn(com.oracle.svm.core.posix.headers.NetinetIn) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Socket(com.oracle.svm.core.posix.headers.Socket)

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