Example #1
0
/*
 * Class:     java_net_NetworkInterface
 * Method:    getAll
 * Signature: ()[Ljava/net/NetworkInterface;
 */
JNIEXPORT jobjectArray JNICALL Java_java_net_NetworkInterface_getAll
    (JNIEnv *env, jclass cls) {

    netif *ifs, *curr;
    jobjectArray netIFArr;
    jint arr_index, ifCount;

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
        return NULL;
    }

    /* count the interface */
    ifCount = 0;
    curr = ifs;
    while (curr != NULL) {
        ifCount++;
        curr = curr->next;
    }

    /* allocate a NetworkInterface array */
    netIFArr = (*env)->NewObjectArray(env, ifCount, cls, NULL);
    if (netIFArr == NULL) {
        freeif(ifs);
        return NULL;
    }

    /*
     * Iterate through the interfaces, create a NetworkInterface instance
     * for each array element and populate the object.
     */
    curr = ifs;
    arr_index = 0;
    while (curr != NULL) {
        jobject netifObj;

        netifObj = createNetworkInterface(env, curr);
        if (netifObj == NULL) {
            freeif(ifs);
            return NULL;
        }

        /* put the NetworkInterface into the array */
        (*env)->SetObjectArrayElement(env, netIFArr, arr_index++, netifObj);

        curr = curr->next;
    }

    freeif(ifs);
    return netIFArr;
}
Example #2
0
/*
 * Class:     java_net_NetworkInterface
 * Method:    getByIndex
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 */
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex
    (JNIEnv *env, jclass cls, jint index) {

    netif *ifs, *curr;
    jobject obj = NULL;

    if (index <= 0) {
        return NULL;
    }

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
        return NULL;
    }

    /*
     * Search the list of interface based on index
     */
    curr = ifs;
    while (curr != NULL) {
        if (index == curr->index) {
            break;
        }
        curr = curr->next;
    }

    /* if found create a NetworkInterface */
    if (curr != NULL) {;
        obj = createNetworkInterface(env, curr);
    }

    freeif(ifs);
    return obj;
}
/* 
 * Enumerates all interfaces
 */
static netif *enumInterfaces(JNIEnv *env) {
    netif *ifs;

    /*
     * Enumerate IPv4 addresses
     */
    ifs = enumIPv4Interfaces(env, NULL);
    if (ifs == NULL) {
	if ((*env)->ExceptionOccurred(env)) {
	    return NULL;
	}
    }

    /*
     * If IPv6 is available then enumerate IPv6 addresses.
     */
#ifdef AF_INET6
    if (ipv6_available()) {
        ifs = enumIPv6Interfaces(env, ifs);

	if ((*env)->ExceptionOccurred(env)) {
	    freeif(ifs);
	    return NULL;
        }
    }
#endif

    return ifs;
}
/*
 * Class:     java_net_NetworkInterface
 * Method:    getByName0
 * Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
 */
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
    (JNIEnv *env, jclass cls, jstring name) {

    netif *ifs, *curr;
    jboolean isCopy;
    const char* name_utf = (*env)->GetStringUTFChars(env, name, &isCopy);
    jobject obj = NULL;

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
	return NULL;
    }

    /*
     * Search the list of interface based on name
     */
    curr = ifs;
    while (curr != NULL) {
        if (strcmp(name_utf, curr->name) == 0) {
            break;
        }
        curr = curr->next;
    }

    /* if found create a NetworkInterface */
    if (curr != NULL) {;
        obj = createNetworkInterface(env, curr);
    }

    /* release the UTF string and interface list */
    (*env)->ReleaseStringUTFChars(env, name, name_utf);
    freeif(ifs);

    return obj;
}
Example #5
0
SEXP R_ng_asweka(SEXP R_str, SEXP min_n_, SEXP max_n_, SEXP R_sep)
{
	int i, j;
	char *str = CHARPT(R_str, 0); 
	char *sep = CHARPT(R_sep, 0); 
	const int min_n = INTEGER(min_n_)[0];
	const int max_n = INTEGER(max_n_)[0];
	int str_len;
	sentencelist_t *sl;
	wordlist_t *wptr;
	int numwords;
	int cur_n;
	size_t len;
	const char **starts = NULL;
	int *lens = NULL;
	int word_i;
	char *errstr;

	SEXP RET;

	str_len = strlen(str);

	if(*sep == '\0')
		sep=NULL;

	sl = lex_simple(str, str_len, sep);

	if (sl == NULL)
		error("out of memory");

	numwords = 0;
	for(i=0;i<sl->filled;i++)
		for(wptr=sl->words[i];wptr && wptr->word->s;wptr=wptr->next)
			numwords++;

	if (numwords == 0){
		errstr="no words";
		goto memerr;
	}

	len = numwords;
	starts = malloc(sizeof(*starts)*numwords);
	if (starts == NULL){
		errstr="out of memory";
		goto memerr;
	}

	lens = malloc(sizeof(*lens)*numwords);
	if (lens == NULL){
		errstr="out of memory";
		goto memerr;
	}
	
	for(i=sl->filled-1;i>=0;i--){
		for(wptr=sl->words[i];wptr && wptr->word->s;wptr=wptr->next){
			--len;
			starts[len]=wptr->word->s;
			lens[len]=wptr->word->len;
		}
	}

	i = numwords-(min_n-1);
	j = numwords-(max_n-1);
	len = i*(i+1)/2 - j*(j-1)/2;
	PROTECT(RET = allocVector(STRSXP, len));

	word_i = 0;
	for(cur_n=max_n;cur_n>=min_n;cur_n--){
		for(i=0;i<numwords-(cur_n-1);i++){
			len = starts[i+cur_n-1] - starts[i] + lens[i+cur_n-1]; 
			SET_STRING_ELT(RET, word_i, mkCharLen(starts[i], len));
			
			word_i++;
		}
	}

	free(starts);
	free(lens);
	free_sentencelist(sl,free_wordlist);

	UNPROTECT(1);
	return RET;

memerr:
	freeif(starts);
	freeif(lens);
	free_sentencelist(sl,free_wordlist);
	error(errstr);
}
Example #6
0
/*
 * Class:     java_net_NetworkInterface
 * Method:    getByInetAddress0
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
 */
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
    (JNIEnv *env, jclass cls, jobject iaObj) {

    netif *ifs, *curr;
    int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
    jobject obj = NULL;
    jboolean match = JNI_FALSE;

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
        return NULL;
    }

    curr = ifs;
    while (curr != NULL) {
        netaddr *addrP = curr->addr;

        /*
         * Iterate through each address on the interface
         */
        while (addrP != NULL) {

            if (family == addrP->family) {
                if (family == AF_INET) {
                    int address1 = htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr);
                    int address2 = getInetAddress_addr(env, iaObj);

                    if (address1 == address2) {
                        match = JNI_TRUE;
                        break;
                    }
                }

#ifdef AF_INET6
                if (family == AF_INET6) {
                    jbyte *bytes = (jbyte *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr);
                    jbyte caddr[16];
                    int i;
                    getInet6Address_ipaddress(env, iaObj, (char *)caddr);
                    i = 0;
                    while (i < 16) {
                        if (caddr[i] != bytes[i]) {
                            break;
                        }
                        i++;
                    }
                    if (i >= 16) {
                        match = JNI_TRUE;
                        break;
                    }
                }
#endif

            }

            if (match) {
                break;
            }
            addrP = addrP->next;
        }

        if (match) {
            break;
        }
        curr = curr->next;
    }

    /* if found create a NetworkInterface */
    if (match) {;
        obj = createNetworkInterface(env, curr);
    }

    freeif(ifs);
    return obj;
}
/*
 * Enumerates and returns all IPv4 interfaces
 */
static netif *enumIPv4Interfaces(JNIEnv *env, netif *ifs) {
    int sock;
    struct ifconf ifc;
    struct ifreq *ifreqP;
    char *buf;
    int numifs;
    unsigned i;
    unsigned bufsize;

    sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
	/*
	 * If EPROTONOSUPPORT is returned it means we don't have
	 * IPv4 support so don't throw an exception.
	 */
	if (errno != EPROTONOSUPPORT) {
	    NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
                             "Socket creation failed");
	}
	return ifs;
    }

#ifdef __linux__
    /* need to do a dummy SIOCGIFCONF to determine the buffer size.
     * SIOCGIFCOUNT doesn't work
     */
    ifc.ifc_buf = NULL;
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
                         "ioctl SIOCGIFCONF failed");
        close(sock);
        return ifs;
    }
    bufsize = ifc.ifc_len;
#else
    if (ioctl(sock, SIOCGIFNUM, (char *)&numifs) < 0) {
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
                         "ioctl SIOCGIFNUM failed");
        close(sock);
        return ifs;
    }
    bufsize = numifs * sizeof (struct ifreq);
#endif /* __linux__ */

    buf = (char *)malloc(bufsize);
    if (!buf) {
        JNU_ThrowOutOfMemoryError(env, "heap allocation failed");
        (void) close(sock);
        return ifs;
    }
    ifc.ifc_len = bufsize;
    ifc.ifc_buf = buf;
    if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) {
        NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException",
                         "ioctl SIOCGIFCONF failed");
        (void) close(sock);
        (void) free(buf);
        return ifs;
    }

    /*
     * Iterate through each interface
     */
    ifreqP = ifc.ifc_req;
    for (i=0; i<ifc.ifc_len/sizeof (struct ifreq); i++, ifreqP++) {
	int index;
	struct ifreq if2;

	memset((char *)&if2, 0, sizeof(if2));
	strcpy(if2.ifr_name, ifreqP->ifr_name);

	/*
	 * Try to get the interface index
	 * (Not supported on Solaris 2.6 or 7)
	 */
        if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) >= 0) {
            index = if2.ifr_index;
        } else {
	    index = -1;
	}

	/*
	 * Add to the list 
	 */
	ifs = addif(env, ifs, ifreqP->ifr_name, index, AF_INET,
		    (struct sockaddr *)&(ifreqP->ifr_addr),
		    sizeof(struct sockaddr_in));

	/*
	 * If an exception occurred then free the list
	 */
	if ((*env)->ExceptionOccurred(env)) {
	    close(sock);
	    free(buf);
	    freeif(ifs);
	    return NULL;
	}
    }

    /*
     * Free socket and buffer
     */
    close(sock);
    free(buf);
    return ifs;
}
/*
 * Enumerates and returns all IPv4 interfaces
 */
static netif *enumIPvXInterfaces(JNIEnv *env, netif *ifs, TUint family)
{
    TPckgBuf<TSoInetInterfaceInfo> info;
    TSoInetInterfaceInfo &i = info();
    RSocket r;
    _LIT(proto, "ip");
    TInt err = r.Open(sserv, proto);
    if (err != KErrNone) {
	JNU_ThrowByName(env , JNU_JAVANETPKG "SocketException",
			 "Socket creation failed");
	return ifs;
    }

    /*
     * Iterate through each interface
     */
    int idx = 1;
    err = r.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl, 1);
    while ((err = r.GetOpt(KSoInetNextInterface,
	KSolInetIfCtrl, info)) == KErrNone)
    {
#ifdef _UNICODE
	TName n = i.iName;
	TUint8 n8[0x20];
	TPtr8 name8(n8, sizeof n8);
	TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8(name8, n);
	fprintf(stderr, "Interface proto %s status %x\n", name8.PtrZ(),
	    i.iState);
{
	TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8(name8, i.iTag);
	fprintf(stderr, "tag %s\n", name8.PtrZ());
}
	const char *if_name = (const char *)name8.PtrZ();
#else
	const char *if_name = (const char *)i.iTag.PtrZ();
#endif
	if (i.iState == EIfUp) {
	    TInetAddr ia = i.iAddress;
	    fprintf(stderr, "Address %x\n", ia.Address());
	}
	if (i.iAddress.Family() == family) {
	    TInetAddr ia = i.iAddress;
	    /*
	     * Add to the list 
	     */
	    ifs = addif(env, ifs, if_name, idx, KAfInet, ia);

	    /*
	     * If an exception occurred then free the list
	     */
	    if ((*env)->ExceptionOccurred(env)) {
		freeif(ifs);
		ifs = NULL;
		goto done;
	    }
	}
	++idx;
    }

{
    TPckgBuf<TSoInetIfQuery> q1;
    TSoInetIfQuery &q = q1();
    TInt inum = 1;
    q.iIndex = inum;

    while ((err = r.GetOpt(KSoInetIfQueryByIndex,
	KSolInetIfQuery, q1)) == KErrNone)
    {
	fprintf(stderr, "Interface %d up %d\n", inum, q.iIsUp);
#ifdef _UNICODE
	TUint8 n8[0x20];
	TPtr8 name8(n8, sizeof n8);
	TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8(name8, q.iName);
	fprintf(stderr, "Interface %d name %s\n", inum, name8.PtrZ());
#endif
	fprintf(stderr, "src addr %x\n", q.iSrcAddr.Address());
	fprintf(stderr, "dst addr %x\n", q.iDstAddr.Address());
	q.iIndex = ++inum;
    }
}

done:
    r.Close();
    return ifs;
}
/*
 * Class:     java_net_NetworkInterface
 * Method:    getByInetAddress0
 * Signature: (Ljava/net/InetAddress;)Ljava/net/NetworkInterface;
 */
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0
    (JNIEnv *env, jclass cls, jobject iaObj)
{
    netif *ifs, *curr;
    int family = KAfInet;
    jobject obj = NULL;
    jboolean match = JNI_FALSE;

    jint jfamily = (*env)->GetIntField(env, iaObj, ni_iafamilyID);
    if (jfamily != IPv4) {
	family = KAfInet6;
    }

    ifs = enumInterfaces(env);
    if (ifs == NULL) {
	return NULL;
    }

    curr = ifs;
    while (curr != NULL) {
	netaddr *addrP = curr->addr;

	/*
	 * Iterate through each address on the interface
	 */
	while (addrP != NULL) {

	    if (family == addrP->family) {
		if (family == KAfInet) {
		    int address1 = addrP->ia.Address();
                    int address2 = (*env)->GetIntField(env, iaObj, ni_iaaddressID);

                    if (address1 == address2) {
			match = JNI_TRUE;
			break;
		    }
		}

		if (family == KAfInet6) {
		    const TIp6Addr &ipv6address = addrP->ia.Ip6Address();
		    jbyte *bytes = (jbyte *)ipv6address.u.iAddr8;
		    jbyteArray ipaddress = (*env)->GetObjectField(env, iaObj,
			ni_ia6ipaddressID);
		    jbyte caddr[16];
		    int i;

		    (*env)->GetByteArrayRegion(env, ipaddress, 0, 16, caddr);
		    i = 0;
		    while (i < 16) {
                        if (caddr[i] != bytes[i]) {
			    break;
			}
			i++;
                    }
		    if (i >= 16) {
			match = JNI_TRUE;
			break;
		    }
		}

	    }

	    if (match) {
		break;
	    }
	    addrP = addrP->next;
	}

	if (match) {
	    break;
	}
	curr = curr->next;
    }

    /* if found create a NetworkInterface */
    if (match) {;
        obj = createNetworkInterface(env, curr);
    }

    freeif(ifs);
    return obj;
}