예제 #1
0
파일: SctpNet.c 프로젝트: netroby/jdk9-dev
/*
 * Class:     sun_nio_ch_sctp_SctpNet
 * Method:    getLocalAddresses0
 * Signature: (I)[Ljava/net/SocketAddress;
 */
JNIEXPORT jobjectArray JNICALL Java_sun_nio_ch_sctp_SctpNet_getLocalAddresses0
  (JNIEnv *env, jclass klass, jint fd) {
    void *addr_buf, *laddr;
    struct sockaddr* sap;
    int i, addrCount;
    jobjectArray isaa;

#ifdef __solaris__
    if ((addrCount = nio_sctp_getladdrs(fd, 0, (void **)&addr_buf)) == -1) {
#else /* __linux__ */
    if ((addrCount = nio_sctp_getladdrs(fd, 0, (struct sockaddr **)&addr_buf)) == -1) {
#endif
        handleSocketError(env, errno);
        return NULL;
    }

    if (addrCount < 1)
        return NULL;

    if (isaCls == 0) {
        initializeISA(env);
        CHECK_NULL_RETURN(isaCls, NULL);
    }

    isaa = (*env)->NewObjectArray(env, addrCount, isaCls, NULL);
    if (isaa == NULL) {
        nio_sctp_freeladdrs(addr_buf);
        return NULL;
    }

    laddr = addr_buf;
    for (i=0; i<addrCount; i++) {
        int port = 0;
        jobject isa = NULL, ia;
        sap = (struct sockaddr*)addr_buf;
        ia = NET_SockaddrToInetAddress(env, sap, &port);
        if (ia != NULL)
            isa = (*env)->NewObject(env, isaCls, isaCtrID, ia, port);
        if (isa == NULL)
            break;
        (*env)->SetObjectArrayElement(env, isaa, i, isa);

        if (sap->sa_family == AF_INET)
            addr_buf = ((struct sockaddr_in*)addr_buf) + 1;
        else
            addr_buf = ((struct sockaddr_in6*)addr_buf) + 1;
    }

    nio_sctp_freeladdrs(laddr);
    return isaa;
}

jobjectArray getRemoteAddresses
  (JNIEnv *env, jint fd, sctp_assoc_t id) {
    void *addr_buf, *paddr;
    struct sockaddr* sap;
    int i, addrCount;
    jobjectArray isaa;

#if __solaris__
    if ((addrCount = nio_sctp_getpaddrs(fd, id, (void **)&addr_buf)) == -1) {
#else /* __linux__ */
    if ((addrCount = nio_sctp_getpaddrs(fd, id, (struct sockaddr**)&addr_buf)) == -1) {
#endif
        handleSocketError(env, errno);
        return NULL;
    }

    if (addrCount < 1)
        return NULL;

    if (isaCls == 0) {
        initializeISA(env);
        CHECK_NULL_RETURN(isaCls, NULL);
    }

    isaa = (*env)->NewObjectArray(env, addrCount, isaCls, NULL);
    if (isaa == NULL) {
        nio_sctp_freepaddrs(addr_buf);
        return NULL;
    }

    paddr = addr_buf;
    for (i=0; i<addrCount; i++) {
        jobject ia, isa = NULL;
        int port = 0;
        sap = (struct sockaddr*)addr_buf;
        ia = NET_SockaddrToInetAddress(env, sap, &port);
        if (ia != NULL)
            isa = (*env)->NewObject(env, isaCls, isaCtrID, ia, port);
        if (isa == NULL)
            break;
        (*env)->SetObjectArrayElement(env, isaa, i, isa);

        if (sap->sa_family == AF_INET)
            addr_buf = ((struct sockaddr_in*)addr_buf) + 1;
        else
            addr_buf = ((struct sockaddr_in6*)addr_buf) + 1;
    }

    nio_sctp_freepaddrs(paddr);

    return isaa;
}

 /*
 * Class:     sun_nio_ch_sctp_SctpNet
 * Method:    getRemoteAddresses0
 * Signature: (II)[Ljava/net/SocketAddress;
 */
JNIEXPORT jobjectArray JNICALL Java_sun_nio_ch_sctp_SctpNet_getRemoteAddresses0
  (JNIEnv *env, jclass klass, jint fd, jint assocId) {
    return getRemoteAddresses(env, fd, assocId);
}

/* Map the Java level option to the native level */
int mapSocketOption
  (jint cmd, int *level, int *optname) {
    static struct {
        jint cmd;
        int level;
        int optname;
    } const opts[] = {
        { sun_nio_ch_sctp_SctpStdSocketOption_SCTP_DISABLE_FRAGMENTS,   IPPROTO_SCTP, SCTP_DISABLE_FRAGMENTS },
        { sun_nio_ch_sctp_SctpStdSocketOption_SCTP_EXPLICIT_COMPLETE,   IPPROTO_SCTP, SCTP_EXPLICIT_EOR },
        { sun_nio_ch_sctp_SctpStdSocketOption_SCTP_FRAGMENT_INTERLEAVE, IPPROTO_SCTP, SCTP_FRAGMENT_INTERLEAVE },
        { sun_nio_ch_sctp_SctpStdSocketOption_SCTP_NODELAY,             IPPROTO_SCTP, SCTP_NODELAY },
        { sun_nio_ch_sctp_SctpStdSocketOption_SO_SNDBUF,                SOL_SOCKET,   SO_SNDBUF },
        { sun_nio_ch_sctp_SctpStdSocketOption_SO_RCVBUF,                SOL_SOCKET,   SO_RCVBUF },
        { sun_nio_ch_sctp_SctpStdSocketOption_SO_LINGER,                SOL_SOCKET,   SO_LINGER } };

    int i;
    for (i=0; i<(int)(sizeof(opts) / sizeof(opts[0])); i++) {
        if (cmd == opts[i].cmd) {
            *level = opts[i].level;
            *optname = opts[i].optname;
            return 0;
        }
    }

    /* not found */
    return -1;
}

/*
 * Class:     sun_nio_ch_sctp_SctpNet
 * Method:    setIntOption0
 * Signature: (III)V
 */
JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_setIntOption0
  (JNIEnv *env, jclass klass, jint fd, jint opt, int arg) {
    int klevel, kopt;
    int result;
    struct linger linger;
    void *parg;
    int arglen;

    if (mapSocketOption(opt, &klevel, &kopt) < 0) {
        JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
                                     "Unsupported socket option");
        return;
    }

    if (opt == sun_nio_ch_sctp_SctpStdSocketOption_SO_LINGER) {
        parg = (void *)&linger;
        arglen = sizeof(linger);
        if (arg >= 0) {
            linger.l_onoff = 1;
            linger.l_linger = arg;
        } else {
            linger.l_onoff = 0;
            linger.l_linger = 0;
        }
    } else {
        parg = (void *)&arg;
        arglen = sizeof(arg);
    }

    if (NET_SetSockOpt(fd, klevel, kopt, parg, arglen) < 0) {
        JNU_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
                                     "sun_nio_ch_sctp_SctpNet.setIntOption0");
    }
}
예제 #2
0
void* initializeClient(void* args){
GtkWidget* dia;
char addr[24];
int res, numdev=NUMDEV, i, ch[NUMDEV], target=0, isdevselected=0, valdevs=0, t;
bdaddr_t *devs;
devs=(bdaddr_t*)malloc(NUMDEV*sizeof(bdaddr_t));
bzero(ch,sizeof(int)*NUMDEV);
dia=gtk_dialog_new();
int valDevs[NUMDEV]={-1};

//g_signal_connect_swapped (dia,"response",G_CALLBACK(gtk_widget_destroy),dia);

g_print("Getting remote addresses\n");
if((res=getRemoteAddresses(devs,&numdev))==ERROR){
printf("error%x\n",aeoserr);
return NULL;
}
if(numdev==0){
g_print("No devices found\n");
return NULL;
}
for(i=0;i<numdev;i++){
ba2str(&devs[i],addr);
g_print("%d - %s ",i,addr);
res=isRunningServer(aeosuuid,&devs[i],&ch[i]);
if(res==OK && ch[i]<=30 && ch[i]>0){
g_print("; channel=%d",ch[i]);
valDevs[i]=1;
valdevs++;
}
g_print("\n");
}
if(valdevs<1){
g_print("No device running server to connect to\n");//announce through dialog
//don't launch gui
return NULL;
}
if(valdevs==1){
//one valid device among 1 device(s)
for(i=0;i<numdev;i++){if(valDevs[i]==1){target=i;}}
sock=getConnectedSocket(devs[target],ch[target]);
isdevselected=1; //flag
//target 0 by default
}else{
//if valid device more than 1
for(i=0;i<numdev;i++){
 if(valDevs[i]==1){//printf("%d/",i);
 ba2str(&devs[i],addr);
 gtk_dialog_add_button(GTK_DIALOG(dia),addr,i);
 }
 }
target=gtk_dialog_run(GTK_DIALOG(dia));
gtk_dialog_response(GTK_DIALOG(dia),GTK_RESPONSE_CLOSE);
g_print("Device %d",t);
//scanf("%d%*c",&target);//get button response
if(valDevs[target]==1){isdevselected=1; sock=getConnectedSocket(devs[target],ch[target]);}
else{printf("Incorrect device chosen\n");}

}
//copied
return NULL;
}