Beispiel #1
0
JNIEXPORT jobject JNICALL
Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
{
    SOCKADDR sa;
    socklen_t sa_len = SOCKADDR_LEN;
    int port;
    if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
#ifdef _ALLBSD_SOURCE
        /*
         * XXXBSD:
         * ECONNRESET is specific to the BSDs. We can not return an error,
         * as the calling Java code with raise a java.lang.Error with the expectation
         * that getsockname() will never fail. According to the Single UNIX Specification,
         * it shouldn't fail. As such, we just fill in generic Linux-compatible values.
         */
        if (errno == ECONNRESET) {
            struct sockaddr_in *sin;
            sin = (struct sockaddr_in *) &sa;
            bzero(sin, sizeof(*sin));
            sin->sin_len  = sizeof(struct sockaddr_in);
            sin->sin_family = AF_INET;
            sin->sin_port = htonl(0);
            sin->sin_addr.s_addr = INADDR_ANY;
        } else {
            handleSocketError(env, errno);
            return NULL;
        }
#else /* _ALLBSD_SOURCE */
        handleSocketError(env, errno);
        return NULL;
#endif /* _ALLBSD_SOURCE */
    }
    return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
}
void EPosixClientSocket::onReceive()
{
	if( !handleSocketError())
		return;

	checkMessages();
}
void EPosixClientSocket::onSend()
{
	if( !handleSocketError())
		return;

	sendBufferedData();
}
Beispiel #4
0
/*
 * Class:     sun_nio_ch_sctp_SctpNet
 * Method:    bindx
 * Signature: (I[Ljava/net/InetAddress;IIZ)V
 */
JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_bindx
  (JNIEnv *env, jclass klass, jint fd, jobjectArray addrs, jint port,
   jint addrsLength, jboolean add, jboolean preferIPv6) {
    SOCKETADDRESS *sap, *tmpSap;
    int i, sa_len = sizeof(SOCKETADDRESS);
    jobject ia;

    if (addrsLength < 1)
        return;

    if ((sap = calloc(addrsLength, sa_len)) == NULL) {
          JNU_ThrowOutOfMemoryError(env, "heap allocation failure");
        return;
    }

    tmpSap = sap;
    for (i = 0; i < addrsLength; i++) {
        ia = (*env)->GetObjectArrayElement(env, addrs, i);
        if (NET_InetAddressToSockaddr(env, ia, port, (struct sockaddr*)tmpSap,
                                      &sa_len, preferIPv6) != 0) {
            free(sap);
            return;
        }
        tmpSap++;
    }

    if (nio_sctp_bindx(fd, (void *)sap, addrsLength, add ? SCTP_BINDX_ADD_ADDR :
                       SCTP_BINDX_REM_ADDR) != 0) {
        handleSocketError(env, errno);
    }

    free(sap);
}
Beispiel #5
0
void EClientSocket::onReceive( int i)
{
	if( !handleSocketError( i))
		return;

	checkMessages();
}
void EPosixClientSocket::onConnect()
{
	if( !handleSocketError())
		return;

	onConnectBase();
}
Beispiel #7
0
JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_connect(JNIEnv *env, jclass clazz,
                                jobject fdo, jobject iao, jint port,
                                jint trafficClass)
{
    SOCKADDR sa;
    int sa_len = SOCKADDR_LEN;
    int rv;

    NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *) &sa, &sa_len);

#ifdef AF_INET6
#if 0
    if (trafficClass != 0 && ipv6_available()) { /* ## FIX */
	NET_SetTrafficClass((struct sockaddr *)&sa, trafficClass);
    }
#endif
#endif

    rv = connect(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
    if (rv != 0) {
        if (errno == EINPROGRESS) {
            return IOS_UNAVAILABLE;
        } else if (errno == EINTR) {
	    return IOS_INTERRUPTED;
	}
        return handleSocketError(env, errno);
    }
    return 1;
}
Beispiel #8
0
void EClientSocket::onSend( int i)
{
	if( !handleSocketError( i))
		return;

	sendBufferedData();
}
Beispiel #9
0
void EClientSocket::onSend()
{
	if( !handleSocketError())
		return;

	getTransport()->sendBufferedData();
}
Beispiel #10
0
Network::Network(QObject* parent)
  : QObject(parent), _options(NULL), _handShakingStep(0),
    _port(3128), _retries(0)
{
  this->_ns = dynamic_cast<QNetsoul*>(parent);
  if (this->_ns)
    {
      this->_reconnectionTimer.setSingleShot(true);
      QObject::connect(&this->_reconnectionTimer, SIGNAL(timeout()),
                       this->_ns, SLOT(reconnect()));
      QObject::connect(&this->_socket, SIGNAL(readyRead()),
                       SLOT(processPackets()));
      QObject::connect(&this->_socket,
                       SIGNAL(stateChanged(QAbstractSocket::SocketState)),
                       this->_ns,
                       SLOT(updateWidgets(QAbstractSocket::SocketState)));
      QObject::connect(&this->_socket,
                       SIGNAL(stateChanged(QAbstractSocket::SocketState)),
                       this,
                       SLOT(handleSocketState(QAbstractSocket::SocketState)));
      QObject::connect(&this->_socket,
                       SIGNAL(error(QAbstractSocket::SocketError)),
                       SLOT(handleSocketError(QAbstractSocket::SocketError)));
    }
  else
    qFatal("Network constructor: parent must be a QNetsoul instance !");
  this->_socket.setProxy(QNetworkProxy::NoProxy);
}
Beispiel #11
0
void SshConnectionPrivate::connectToHost()
{
    QSSH_ASSERT_AND_RETURN(m_state == SocketUnconnected);

    m_incomingData.clear();
    m_incomingPacket.reset();
    m_sendFacility.reset();
    m_error = SshNoError;
    m_ignoreNextPacket = false;
    m_errorString.clear();
    m_serverId.clear();
    m_serverHasSentDataBeforeId = false;

    try {
        if (m_connParams.authenticationType == SshConnectionParameters::AuthenticationTypePublicKey)
            createPrivateKey();
    } catch (const SshClientException &ex) {
        m_error = ex.error;
        m_errorString = ex.errorString;
        emit error(m_error);
        return;
    }

    connect(m_socket, SIGNAL(connected()), this, SLOT(handleSocketConnected()));
    connect(m_socket, SIGNAL(readyRead()), this, SLOT(handleIncomingData()));
    connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this,
        SLOT(handleSocketError()));
    connect(m_socket, SIGNAL(disconnected()), this,
        SLOT(handleSocketDisconnected()));
    connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(handleTimeout()));
    m_state = SocketConnecting;
    m_keyExchangeState = NoKeyExchange;
    m_timeoutTimer.start();
    m_socket->connectToHost(m_connParams.host, m_connParams.port);
}
Beispiel #12
0
JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_setIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
                                  jboolean mayNeedConversion, jint level, jint opt, jint arg)
{
    struct linger linger;
    char *parg;
    int arglen, n;

    if (level == SOL_SOCKET && opt == SO_LINGER) {
        parg = (char *)&linger;
        arglen = sizeof(linger);
        if (arg >= 0) {
            linger.l_onoff = 1;
            linger.l_linger = (unsigned short)arg;
        } else {
            linger.l_onoff = 0;
            linger.l_linger = 0;
        }
    } else {
        parg = (char *)&arg;
        arglen = sizeof(arg);
    }

    if (mayNeedConversion) {
        n = NET_SetSockOpt(fdval(env, fdo), level, opt, parg, arglen);
    } else {
        n = setsockopt(fdval(env, fdo), level, opt, parg, arglen);
    }
    if (n < 0)
        handleSocketError(env, WSAGetLastError());
}
Beispiel #13
0
JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_connect0(JNIEnv *env, jclass clazz, jboolean preferIPv6,
                             jobject fdo, jobject iao, jint port)
{
    SOCKADDR sa;
    int sa_len = SOCKADDR_LEN;
    int rv;

    if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *) &sa,
                                  &sa_len, preferIPv6) != 0)
    {
      return IOS_THROWN;
    }

    rv = connect(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
    if (rv != 0) {
        if (errno == EINPROGRESS) {
            return IOS_UNAVAILABLE;
        } else if (errno == EINTR) {
            return IOS_INTERRUPTED;
        }
        return handleSocketError(env, errno);
    }
    return 1;
}
Beispiel #14
0
void EClientSocket::onConnect( int i)
{
	if( !handleSocketError( i))
		return;

	onConnectBase();
}
Beispiel #15
0
JNIEXPORT int JNICALL
Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean stream,
                            jboolean reuse)
{
    int fd;

#ifdef AF_INET6
    if (ipv6_available())
	fd = socket(AF_INET6, (stream ? SOCK_STREAM : SOCK_DGRAM), 0);
    else
#endif /* AF_INET6 */
	fd = socket(AF_INET, (stream ? SOCK_STREAM : SOCK_DGRAM), 0);

    if (fd < 0) {
	return handleSocketError(env, errno);
    }
    if (reuse) {
	int arg = 1;
        if (NET_SetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
                           sizeof(arg)) < 0) {
            JNU_ThrowByNameWithLastError(env,
                                         JNU_JAVANETPKG "SocketException",
                                         "sun.nio.ch.Net.setIntOption");
        }
    }
    return fd;
}
Beispiel #16
0
JNIEXPORT int JNICALL
Java_sun_nio_ch_Net_socket0(JNIEnv *env, jclass cl, jboolean preferIPv6,
                            jboolean stream, jboolean reuse)
{
    int fd;
    int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
#ifdef AF_INET6
    int domain = (ipv6_available() && preferIPv6) ? AF_INET6 : AF_INET;
#else
    int domain = AF_INET;
#endif

    fd = socket(domain, type, 0);
    if (fd < 0) {
        return handleSocketError(env, errno);
    }

#ifdef AF_INET6
    /* Disable IPV6_V6ONLY to ensure dual-socket support */
    if (domain == AF_INET6) {
        int arg = 0;
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
                       sizeof(int)) < 0) {
            JNU_ThrowByNameWithLastError(env,
                                         JNU_JAVANETPKG "SocketException",
                                         "sun.nio.ch.Net.setIntOption");
            close(fd);
            return -1;
        }
    }
#endif

    if (reuse) {
        int arg = 1;
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg,
                       sizeof(arg)) < 0) {
            JNU_ThrowByNameWithLastError(env,
                                         JNU_JAVANETPKG "SocketException",
                                         "sun.nio.ch.Net.setIntOption");
            close(fd);
            return -1;
        }
    }
#if defined(__linux__) && defined(AF_INET6)
    /* By default, Linux uses the route default */
    if (domain == AF_INET6 && type == SOCK_DGRAM) {
        int arg = 1;
        if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &arg,
                       sizeof(arg)) < 0) {
            JNU_ThrowByNameWithLastError(env,
                                         JNU_JAVANETPKG "SocketException",
                                         "sun.nio.ch.Net.setIntOption");
            close(fd);
            return -1;
        }
    }
#endif
    return fd;
}
Beispiel #17
0
void EClientSocket::onClose()
{
	if( !handleSocketError())
		return;

	eDisconnect();
	getWrapper()->connectionClosed();
}
Beispiel #18
0
/*
 * Class:     sun_nio_ch_sctp_SctpNet
 * Method:    branch
 * Signature: (II)I
 */
JNIEXPORT int JNICALL Java_sun_nio_ch_sctp_SctpNet_branch0
  (JNIEnv *env, jclass klass, jint fd, jint assocId) {
    int newfd = 0;
    if ((newfd = nio_sctp_peeloff(fd, assocId)) < 0) {
        handleSocketError(env, errno);
    }

    return newfd;
}
Beispiel #19
0
JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_localPort(JNIEnv *env, jclass clazz, jobject fdo)
{
    SOCKADDR sa;
    int sa_len = SOCKADDR_LEN;
    if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
	handleSocketError(env, errno);
	return -1;
    }
    return NET_GetPortFromSockaddr((struct sockaddr *)&sa);
}
Beispiel #20
0
bool EClientSocket::closeAndSend(std::string msg, unsigned offset)
{
	assert( !msg.empty());
	if( m_useV100Plus) {
		encodeMsgLen( msg, offset);
	}

	if (bufferedSend(msg) == -1)
        return handleSocketError();

    return true;
}
Beispiel #21
0
JNIEXPORT jobject JNICALL
Java_sun_nio_ch_Net_localInetAddress(JNIEnv *env, jclass clazz, jobject fdo)
{
    SOCKADDR sa;
    int sa_len = SOCKADDR_LEN;
    int port;
    if (getsockname(fdval(env, fdo), (struct sockaddr *)&sa, &sa_len) < 0) {
	handleSocketError(env, errno);
	return NULL;
    }
    return NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
}
Beispiel #22
0
/*
 * Class:     sun_nio_ch_sctp_SctpNet
 * Method:    shutdown0
 * Signature: (II)V
 */
JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_shutdown0
  (JNIEnv *env, jclass klass, jint fd, jint assocId) {
    int rv;
    struct msghdr msg[1];
    struct iovec iov[1];
    int cbuf_size = CMSG_SPACE(sizeof (struct sctp_sndrcvinfo));
    char cbuf[CMSG_SPACE(sizeof (struct sctp_sndrcvinfo))];
    struct cmsghdr* cmsg;
    struct sctp_sndrcvinfo *sri;

    /* SctpSocketChannel */
    if (assocId < 0) {
        shutdown(fd, SHUT_WR);
        return;
    }

    memset(msg, 0, sizeof (*msg));
    memset(cbuf, 0, cbuf_size);
    msg->msg_name = NULL;
    msg->msg_namelen = 0;
    iov->iov_base = NULL;
    iov->iov_len = 0;
    msg->msg_iov = iov;
    msg->msg_iovlen = 1;
    msg->msg_control = cbuf;
    msg->msg_controllen = cbuf_size;
    msg->msg_flags = 0;

    cmsg = CMSG_FIRSTHDR(msg);
    cmsg->cmsg_level = IPPROTO_SCTP;
    cmsg->cmsg_type = SCTP_SNDRCV;
    cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));

    /* Initialize the payload: */
    sri = (struct sctp_sndrcvinfo*) CMSG_DATA(cmsg);
    memset(sri, 0, sizeof (*sri));

    if (assocId > 0) {
        sri->sinfo_assoc_id = assocId;
    }

    sri->sinfo_flags = sri->sinfo_flags | SCTP_EOF;

    /* Sum of the length of all control messages in the buffer. */
    msg->msg_controllen = cmsg->cmsg_len;

    if ((rv = sendmsg(fd, msg, 0)) < 0) {
        handleSocketError(env, errno);
    }
}
Beispiel #23
0
int EClientSocket::send(const char* buf, size_t sz)
{
	if( sz <= 0)
		return 0;

	int nResult = m_pSocket->Send( buf, sz);
	if( nResult == SOCKET_ERROR && !handleSocketError( GetLastError())) {
		return -1;
	}
	if( nResult <= 0) {
		return 0;
	}
	return nResult;
}
Beispiel #24
0
bool EClientSocket::eConnect( const char *host, UINT port, int clientId, bool extraAuth)
{
	// already connected?
	if( isConnected()) {
		getWrapper()->error( NO_VALID_ID, ALREADY_CONNECTED.code(), ALREADY_CONNECTED.msg());
		return false;
	}

	// init sockets
	AfxSocketInit();

	// close open connection if there was one
	eDisconnect();

	// create socket
	m_pSocket.reset(new MySocket(this));
	if( !m_pSocket->Create()) {
		eDisconnect();
		getWrapper()->winError( "Failed to create socket", GetLastError() );
		getWrapper()->error( NO_VALID_ID, FAIL_CREATE_SOCK.code(), FAIL_CREATE_SOCK.msg());
		return false;
	}

	// use local machine if no host passed in
	if( !(host && *host)) {
		host = "127.0.0.1";
	}

	// connect to server
	if( !m_pSocket->Connect(host, port)) {
		int lastError = GetLastError();
		if( lastError != WSAEWOULDBLOCK && !handleSocketError(GetLastError())) {
			return false;
		}
	}

	setClientId( clientId);
	setExtraAuth( extraAuth);

	{
		// Wait till we are fully connected (or for an error)
		CWinThread* pThread = AfxGetThread();
		while( m_pSocket.get() && !isConnected()) {
			if (!pThread->PumpMessage())
				return false;
		}
	}
	return true;
}
Beispiel #25
0
/*
 * Class:     sun_nio_ch_sctp_SctpNet
 * Method:    socket0
 * Signature: (Z)I
 */
JNIEXPORT jint JNICALL Java_sun_nio_ch_sctp_SctpNet_socket0
  (JNIEnv *env, jclass klass, jboolean oneToOne) {
    int fd;
    struct sctp_event_subscribe event;
#ifdef AF_INET6
    int domain = ipv6_available() ? AF_INET6 : AF_INET;
#else
    int domain = AF_INET;
#endif

    /* Try to load the socket API extension functions */
    if (!funcsLoaded && !loadSocketExtensionFuncs(env)) {
        return 0;
    }

    fd = socket(domain, (oneToOne ? SOCK_STREAM : SOCK_SEQPACKET), IPPROTO_SCTP);

    if (fd < 0) {
        return handleSocketError(env, errno);
    }

    /* Enable events */
    memset(&event, 0, sizeof(event));
    event.sctp_data_io_event = 1;
    event.sctp_association_event = 1;
    event.sctp_address_event = 1;
    event.sctp_send_failure_event = 1;
    //event.sctp_peer_error_event = 1;
    event.sctp_shutdown_event = 1;
    //event.sctp_partial_delivery_event = 1;
    //event.sctp_adaptation_layer_event = 1;
    if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(event)) != 0) {
       handleSocketError(env, errno);
    }
    return fd;
}
Beispiel #26
0
JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_bind(JNIEnv *env, jclass clazz, /* ## Needs rest of PSI gunk */
			 jobject fdo, jobject ia, int port)
{
    SOCKADDR sa;
    int sa_len = SOCKADDR_LEN;
    int rv = 0;

    NET_InetAddressToSockaddr(env, ia, port, (struct sockaddr *)&sa, &sa_len);

    rv = NET_Bind(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
    if (rv != 0) {
        handleSocketError(env, errno);
    }
}
int EPosixClientSocket::receive(char* buf, size_t sz)
{
	if( sz <= 0)
		return 0;

	int nResult = ::recv( m_fd, buf, sz, 0);

	if( nResult == -1 && !handleSocketError()) {
		return -1;
	}
	if( nResult <= 0) {
		return 0;
	}
	return nResult;
}
Beispiel #28
0
JNIEXPORT void JNICALL
Java_sun_nio_ch_Net_bind0(JNIEnv *env, jclass clazz, jobject fdo, jboolean preferIPv6,
                          jboolean useExclBind, jobject iao, int port)
{
    SOCKADDR sa;
    int sa_len = SOCKADDR_LEN;
    int rv = 0;

    if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) {
      return;
    }

    rv = NET_Bind(fdval(env, fdo), (struct sockaddr *)&sa, sa_len);
    if (rv != 0) {
        handleSocketError(env, errno);
    }
}
Beispiel #29
0
// ChatConnection初始化和信号槽连接
ChatConnection::ChatConnection(QObject *parent) :
    QTcpSocket(parent)
{
    this->greetingMessage = QObject::tr("undefined");
    this->userName = QObject::tr("unknown");
    this->state = WaitingForGreeting;
    this->currentDataType = UNDEFINED;
    this->numBytesForCurrentDataType = -1;
    this->transferTimerId = 0;
    this->isGreetingMessageSent = false;

    connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
    connect(this, SIGNAL(connected()), this, SLOT(sendGreetingMessage()));
    connect(this, SIGNAL(disconnected()), this, SLOT(handleDisconnected()));
    connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleSocketError(QAbstractSocket::SocketError)));

}
Beispiel #30
0
JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
                                  jboolean mayNeedConversion, jint level, jint opt)
{
    int result = 0;
    struct linger linger;
    char *arg;
    int arglen, n;

    if (level == SOL_SOCKET && opt == SO_LINGER) {
        arg = (char *)&linger;
        arglen = sizeof(linger);
    } else {
        arg = (char *)&result;
        arglen = sizeof(result);
    }

    /**
     * HACK: IP_TOS is deprecated on Windows and querying the option
     * returns a protocol error. NET_GetSockOpt handles this and uses
     * a fallback mechanism.
     */
    if (level == IPPROTO_IP && opt == IP_TOS) {
        mayNeedConversion = JNI_TRUE;
    }

    if (mayNeedConversion) {
        n = NET_GetSockOpt(fdval(env, fdo), level, opt, arg, &arglen);
    } else {
        n = getsockopt(fdval(env, fdo), level, opt, arg, &arglen);
    }
    if (n < 0) {
        handleSocketError(env, WSAGetLastError());
        return IOS_THROWN;
    }

    if (level == SOL_SOCKET && opt == SO_LINGER)
        return linger.l_onoff ? linger.l_linger : -1;
    else
        return result;
}