Search in sources :

Example 46 with CCharPointer

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

the class JavaNetNetworkInterface method createNetworkInterface.

/*
     * Translated from jdk/src/solaris/native/java/net/NetworkInterface.c?v=Java_1.8.0_40_b10
     */
// 630 /*
// 631  * Create a NetworkInterface object, populate the name and index, and
// 632  * populate the InetAddress array based on the IP addresses for this
// 633  * interface.
// 634  */
// 635 jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
static NetworkInterface createNetworkInterface(netif ifs) {
    // 636     jobject netifObj;
    NetworkInterface netifObj;
    Target_java_net_NetworkInterface netif_TJNNI;
    // 637     jobject name;
    String name;
    // 638     jobjectArray addrArr;
    InetAddress[] addrArr;
    // 639     jobjectArray bindArr;
    InterfaceAddress[] bindArr;
    // 640     jobjectArray childArr;
    NetworkInterface[] childArr;
    // 641     netaddr *addrs;
    /* `addrs` is unused. */
    // 642     jint addr_index, addr_count, bind_index;
    int addr_index;
    int addr_count;
    int bind_index;
    // 643     jint child_count, child_index;
    int child_count;
    int child_index;
    // 644     netaddr *addrP;
    netaddr addrP;
    // 645     netif *childP;
    netif childP;
    // 646     jobject tmp;
    NetworkInterface tmp;
    // 647
    // 648     /*
    // 649      * Create a NetworkInterface object and populate it
    // 650      */
    // 651     netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
    netif_TJNNI = new Target_java_net_NetworkInterface();
    netifObj = Util_java_net_NetworkInterface.toNetworkInterface(netif_TJNNI);
    // 652     CHECK_NULL_RETURN(netifObj, NULL);
    // 653     name = (*env)->NewStringUTF(env, ifs->name);
    name = CTypeConversion.toJavaString(ifs.name);
    // 654     CHECK_NULL_RETURN(name, NULL);
    // 655     (*env)->SetObjectField(env, netifObj, ni_nameID, name);
    netif_TJNNI.name = name;
    // 656     (*env)->SetObjectField(env, netifObj, ni_descID, name);
    netif_TJNNI.displayName = name;
    // 657     (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
    netif_TJNNI.index = ifs.index;
    // 658     (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
    netif_TJNNI.virtual = CTypeConversion.toBoolean(ifs.virtual);
    // 659
    // 660     /*
    // 661      * Count the number of address on this interface
    // 662      */
    // 663     addr_count = 0;
    addr_count = 0;
    // 664     addrP = ifs->addr;
    addrP = ifs.addr;
    // 665     while (addrP != NULL) {
    while (addrP != null) {
        // 666         addr_count++;
        addr_count++;
        // 667         addrP = addrP->next;
        addrP = addrP.next;
    }
    // 669
    // 670     /*
    // 671      * Create the array of InetAddresses
    // 672      */
    // 673     addrArr = (*env)->NewObjectArray(env, addr_count,  ni_iacls, NULL);
    addrArr = new InetAddress[addr_count];
    /* `new` never returns null. */
    // 674     if (addrArr == NULL) {
    // 675         return NULL;
    // 676     }
    // 677
    // 678     bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
    bindArr = new InterfaceAddress[addr_count];
    /* `new` never returns null. */
    // 679     if (bindArr == NULL) {
    // 680        return NULL;
    // 681     }
    // 682     addrP = ifs->addr;
    addrP = ifs.addr;
    // 683     addr_index = 0;
    addr_index = 0;
    // 684     bind_index = 0;
    bind_index = 0;
    // 685     while (addrP != NULL) {
    while (addrP != null) {
        // 686         jobject iaObj = NULL;
        Object iaObj = null;
        // 687         jobject ibObj = NULL;
        Object ibObj = null;
        // 689         if (addrP->family == AF_INET) {
        if (addrP.family == Socket.AF_INET()) {
            // 690             iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
            final Inet4Address ia_I4A = Util_java_net_Inet4Address.new_Inet4Address();
            iaObj = ia_I4A;
            // 691             if (iaObj) {
            if (iaObj != null) {
                // 692                  setInetAddress_addr(env, iaObj, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
                JavaNetNetUtil.setInetAddress_addr(ia_I4A, NetinetIn.htonl(((NetinetIn.sockaddr_in) addrP.addr).sin_addr().s_addr()));
            } else {
                // 694                 return NULL;
                return null;
            }
            // 696             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
            final Target_java_net_InterfaceAddress ib_TJNIA = new Target_java_net_InterfaceAddress();
            final InterfaceAddress ib_IA = Util_java_net_InterfaceAddress.toInterfaceAddress(ib_TJNIA);
            ibObj = ib_IA;
            // 697             if (ibObj) {
            if (ibObj != null) {
                // 698                  (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
                ib_TJNIA.address = ia_I4A;
                // 699                  if (addrP->brdcast) {
                if (CTypeConversion.toBoolean(addrP.brdcast)) {
                    // 700                     jobject ia2Obj = NULL;
                    Inet4Address ia2Obj = null;
                    // 701                     ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
                    ia2Obj = Util_java_net_Inet4Address.new_Inet4Address();
                    /* `new` never returns null. */
                    // 702                     if (ia2Obj) {
                    // 703                        setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
                    JavaNetNetUtil.setInetAddress_addr(ia2Obj, NetinetIn.htonl(((NetinetIn.sockaddr_in) addrP.brdcast).sin_addr().s_addr()));
                    // 704                        (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
                    ib_TJNIA.broadcast = ia2Obj;
                // 705                     } else {
                // 706                         return NULL;
                // 707                     }
                }
                // 709                  (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
                ib_TJNIA.maskLength = addrP.mask;
                // 710                  (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
                bindArr[bind_index++] = ib_IA;
            } else {
                // 712                 return NULL;
                return null;
            }
        }
        // 716 #ifdef AF_INET6
        if (IsDefined.socket_AF_INET6()) {
            // 717         if (addrP->family == AF_INET6) {
            if (addrP.family == Socket.AF_INET6()) {
                // 718             int scope=0;
                int scope = 0;
                // 719             iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
                final Inet6Address ia_I6A = Util_java_net_Inet6Address.new_Inet6Address();
                iaObj = ia_I6A;
                // 720             if (iaObj) {
                if (iaObj != null) {
                    // 721                 int ret = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
                    final CCharPointer address = (CCharPointer) ((NetinetIn.sockaddr_in6) addrP.addr).sin6_addr();
                    int ret = JavaNetNetUtil.setInet6Address_ipaddress(ia_I6A, address);
                    // 722                 if (ret == JNI_FALSE) {
                    if (ret == Target_jni.JNI_FALSE()) {
                        // 723                     return NULL;
                        return null;
                    }
                    // 725
                    // 726                 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
                    scope = ((NetinetIn.sockaddr_in6) addrP.addr).sin6_scope_id();
                    // 728                 if (scope != 0) { /* zero is default value, no need to set */
                    if (scope != 0) {
                        // 729                     setInet6Address_scopeid(env, iaObj, scope);
                        JavaNetNetUtil.setInet6Address_scopeid(ia_I6A, scope);
                        // 730                     setInet6Address_scopeifname(env, iaObj, netifObj);
                        JavaNetNetUtil.setInet6Address_scopeifname(ia_I6A, netifObj);
                    }
                } else {
                    // 733                 return NULL;
                    return null;
                }
                // 735             ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
                Target_java_net_InterfaceAddress ib_TJNIA = new Target_java_net_InterfaceAddress();
                InterfaceAddress ib_IA = Util_java_net_InterfaceAddress.toInterfaceAddress(ib_TJNIA);
                ibObj = ib_IA;
                // 736             if (ibObj) {
                if (ibObj != null) {
                    // 737                 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
                    ib_TJNIA.address = (InetAddress) iaObj;
                    // 738                 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
                    ib_TJNIA.maskLength = addrP.mask;
                    // 739                 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
                    bindArr[bind_index++] = ib_IA;
                } else {
                    // 741                 return NULL;
                    return null;
                }
            }
        }
        // 744 #endif
        // 745
        // 746         (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
        addrArr[addr_index++] = (InetAddress) iaObj;
        // 747         addrP = addrP->next;
        addrP = addrP.next;
    }
    // 749
    // 750     /*
    // 751      * See if there is any virtual interface attached to this one.
    // 752      */
    // 753     child_count = 0;
    child_count = 0;
    // 754     childP = ifs->childs;
    childP = ifs.childs;
    // 755     while (childP) {
    while (childP != null) {
        // 756         child_count++;
        child_count++;
        // 757         childP = childP->next;
        childP = childP.next;
    }
    // 759
    // 760     childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
    childArr = new NetworkInterface[child_count];
    // 761     if (childArr == NULL) {
    /* Dead code. */
    // 762         return NULL;
    // 763     }
    // 764
    // 765     /*
    // 766      * Create the NetworkInterface instances for the sub-interfaces as
    // 767      * well.
    // 768      */
    // 769     child_index = 0;
    child_index = 0;
    // 770     childP = ifs->childs;
    childP = ifs.childs;
    // 771     while(childP) {
    while (childP != null) {
        // 772       tmp = createNetworkInterface(env, childP);
        tmp = createNetworkInterface(childP);
        // 773       if (tmp == NULL) {
        if (tmp == null) {
            // 774          return NULL;
            return null;
        }
        // 776       (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
        Util_java_net_NetworkInterface.fromNetworkInterface(tmp).parent = netifObj;
        // 777       (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
        childArr[child_index++] = tmp;
        // 778       childP = childP->next;
        childP = childP.next;
    }
    // 780     (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
    netif_TJNNI.addrs = addrArr;
    // 781     (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
    netif_TJNNI.bindings = bindArr;
    // 782     (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
    netif_TJNNI.childs = childArr;
    // 785     return netifObj;
    return netifObj;
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) NetinetIn(com.oracle.svm.core.posix.headers.NetinetIn) InetAddress(java.net.InetAddress) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 47 with CCharPointer

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

the class CInterfaceTutorial method javaEntryPoint.

/* Java function that can be called directly from C code. */
@CEntryPoint(name = "java_entry_point")
protected static void javaEntryPoint(@SuppressWarnings("unused") IsolateThread thread, MyData data) {
    /* Allocate a C structure in our stack frame. */
    MyData copy = StackValue.get(SizeOf.get(MyData.class));
    /* Get the size of a C structure. */
    int dataSize = SizeOf.get(MyData.class);
    /* Call a function from the standard C library. */
    memcpy(copy, data, WordFactory.unsigned(dataSize));
    dump(copy);
    /* Modify primitive data of a C structure. */
    data.setPrimitive(99);
    data.addressOfArray().write(1, 101);
    /* Pass out a pointer into a Java byte[] array that is pinned. */
    String javaString = CTypeConversion.toJavaString(data.getCString()) + " at " + new Date();
    pin = CTypeConversion.toCString(javaString);
    CCharPointer cString = pin.get();
    data.setCString(cString);
    /* Install a function pointer to a Java function. */
    data.setPrintFunction(javaPrintFunction.getFunctionPointer());
    /* Create a handle to a Java object. */
    data.setJavaObject(ObjectHandles.getGlobal().create(javaString));
}
Also used : CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) Date(java.util.Date) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

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