Search in sources :

Example 11 with CIntPointer

use of org.graalvm.nativeimage.c.type.CIntPointer 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)

Example 12 with CIntPointer

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

the class DarwinExecutableName method apply.

/**
 * This implementation of {@link CompilerCommandPlugin#apply(Object[])} does not use the
 * arguments, and returns a String, possibly null.
 */
@Override
public Object apply(Object[] args) {
    /* Find out how long the executable path is. */
    final CIntPointer sizePointer = StackValue.get(SizeOf.get(CIntPointer.class));
    sizePointer.write(0);
    if (DarwinDyld._NSGetExecutablePath(WordFactory.nullPointer(), sizePointer) != -1) {
        VMError.shouldNotReachHere("DarwinExecutableName.getExecutableName: Executable path length is 0?");
    }
    /* Allocate a correctly-sized buffer and ask again. */
    final byte[] byteBuffer = new byte[sizePointer.read()];
    try (PinnedObject pinnedBuffer = PinnedObject.create(byteBuffer)) {
        final CCharPointer bufferPointer = pinnedBuffer.addressOfArrayElement(0);
        if (DarwinDyld._NSGetExecutablePath(bufferPointer, sizePointer) == -1) {
            /* Failure to find executable path. */
            return null;
        }
        final String executableString = CTypeConversion.toJavaString(bufferPointer);
        final String result = realpath(executableString);
        return result;
    }
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) PinnedObject(org.graalvm.nativeimage.PinnedObject) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 13 with CIntPointer

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

the class JavavmExportJvm method JVM_GetSockOpt.

// 3825 JVM_LEAF(jint, JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen))
static int JVM_GetSockOpt(int fd, int level, int optname, CCharPointer optval, CIntPointer optlen) {
    // 3826   JVMWrapper2("JVM_GetSockOpt (0x%x)", fd);
    // 3827   //%note jvm_r6
    /* typedef u_int socklen_t; */
    // 3828   socklen_t socklen = (socklen_t)(*optlen);
    CIntPointer socklen_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
    socklen_Pointer.write(optlen.read());
    // 3829   jint result = os::get_sock_opt(fd, level, optname, optval, &socklen);
    int result = Socket.getsockopt(fd, level, optname, optval, optlen);
    // 3830   *optlen = (int)socklen;
    optlen.write(socklen_Pointer.read());
    // 3831   return result;
    return result;
// 3832 JVM_END    /* @formatter:on */
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer)

Example 14 with CIntPointer

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

the class JavavmExportJvm method NET_GetSockOpt.

/* @formatter:on */
/* Do not re-wrap commented-out code.  @formatter:off */
// 1195 /*
// 1196  * Wrapper for getsockopt system routine - does any necessary
// 1197  * pre/post processing to deal with OS specific oddities :-
// 1198  *
// 1199  * IP_TOS is a no-op with IPv6 sockets as it's setup when
// 1200  * the connection is established.
// 1201  *
// 1202  * On Linux the SO_SNDBUF/SO_RCVBUF values must be post-processed
// 1203  * to compensate for an incorrect value returned by the kernel.
// 1204  */
// 1205 int
// 1206 NET_GetSockOpt(int fd, int level, int opt, void *result,
// 1207                int *len)
// 1208 {
static int NET_GetSockOpt(int fd, int level, int opt, PointerBase result_Pointer, CIntPointer len_Pointer) {
    // 1209     int rv;
    int rv;
    // 1211 #ifdef AF_INET6
    if (IsDefined.socket_AF_INET6()) {
        // 1212     if ((level == IPPROTO_IP) && (opt == IP_TOS)) {
        if ((level == NetinetIn.IPPROTO_IP()) && (opt == NetinetIn.IP_TOS())) {
            // 1213         if (ipv6_available()) {
            if (JavaNetNetUtil.ipv6_available()) {
                // 1214
                // 1215             /*
                // 1216              * For IPv6 socket option implemented at Java-level
                // 1217              * so return -1.
                // 1218              */
                // 1219             int *tc = (int *)result;
                // 1220             *tc = -1;
                CIntPointer tc = (CIntPointer) result_Pointer;
                tc.write(-1);
                // 1221             return 0;
                return 0;
            }
        }
    }
    // 1226 #ifdef __solaris__
    if (IsDefined.__solaris__()) {
        // 1227     rv = getsockopt(fd, level, opt, result, len);
        rv = Socket.getsockopt(fd, level, opt, result_Pointer, len_Pointer);
    // 1228 #else
    } else {
        // 1230         socklen_t socklen = *len;
        CIntPointer socklen_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
        socklen_Pointer.write(len_Pointer.read());
        // 1231         rv = getsockopt(fd, level, opt, result, &socklen);
        rv = Socket.getsockopt(fd, level, opt, result_Pointer, socklen_Pointer);
        // 1232         *len = socklen;
        len_Pointer.write(socklen_Pointer.read());
    }
    // 1236     if (rv < 0) {
    if (rv < 0) {
        // 1237         return rv;
        return rv;
    }
    // 1240 #ifdef __linux__
    if (IsDefined.__linux__()) {
        // 1247                                   || (opt == SO_RCVBUF))) {
        if ((level == Socket.SOL_SOCKET()) && ((opt == Socket.SO_SNDBUF()) || (opt == Socket.SO_RCVBUF()))) {
            // 1248         int n = *((int *)result);
            int n = ((CIntPointer) result_Pointer).read();
            // 1249         n /= 2;
            n /= 2;
            // 1250         *((int *)result) = n;
            ((CIntPointer) result_Pointer).write(n);
        }
    }
    // 1257 #ifdef MACOSX
    if (IsDefined.MACOSX()) {
        // 1258     if (level == SOL_SOCKET && opt == SO_LINGER) {
        if (level == Socket.SOL_SOCKET() && opt == Socket.SO_LINGER()) {
            // 1259         struct linger* to_cast = (struct linger*)result;
            Socket.linger to_cast = (Socket.linger) result_Pointer;
            // 1260         to_cast->l_linger = (unsigned short)to_cast->l_linger;
            to_cast.set_l_linger(to_cast.l_linger() & 0xFF);
        }
    }
    // 1263     return rv;
    return rv;
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) Socket(com.oracle.svm.core.posix.headers.Socket)

Example 15 with CIntPointer

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

the class JavavmExportJvm method JVM_Accept.

// 3781 JVM_LEAF(jint, JVM_Accept(jint fd, struct sockaddr *him, jint *len))
static int JVM_Accept(int fd, Socket.sockaddr him, CIntPointer len_Pointer) {
    // 3782   JVMWrapper2("JVM_Accept (0x%x)", fd);
    // 3783   //%note jvm_r6
    // 3784   socklen_t socklen = (socklen_t)(*len);
    CIntPointer socklen_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
    socklen_Pointer.write(len_Pointer.read());
    // 3785   jint result = os::accept(fd, him, &socklen);
    int result = Socket.accept(fd, him, socklen_Pointer);
    // 3786   *len = (jint)socklen;
    len_Pointer.write(socklen_Pointer.read());
    // 3787   return result;
    return result;
// 3788 JVM_END
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer)

Aggregations

CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)24 Substitute (com.oracle.svm.core.annotate.Substitute)10 Socket (com.oracle.svm.core.posix.headers.Socket)7 SocketException (java.net.SocketException)7 ServerSocket (java.net.ServerSocket)6 PinnedObject (org.graalvm.nativeimage.PinnedObject)6 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)6 Util_java_io_FileDescriptor (com.oracle.svm.core.posix.PosixOSInterface.Util_java_io_FileDescriptor)5 FileDescriptor (java.io.FileDescriptor)5 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)4 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)4 IOException (java.io.IOException)3 InterruptedIOException (java.io.InterruptedIOException)3 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)2 InetAddress (java.net.InetAddress)2 SocketTimeoutException (java.net.SocketTimeoutException)2 CCharPointerPointer (org.graalvm.nativeimage.c.type.CCharPointerPointer)2 WordPointer (org.graalvm.nativeimage.c.type.WordPointer)2 SignedWord (org.graalvm.word.SignedWord)2 DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)1