Example #1
0
void
source_accept_cb(uv_stream_t *server, int status) {
    struct source_context *source = new_source();
    struct target_context *target = new_target();

    source->target = target;
    target->source = source;

    uv_tcp_init(server->loop, &source->handle.tcp);
    uv_tcp_init(server->loop, &target->handle.tcp);

    uv_tcp_nodelay(&source->handle.tcp, 0);
    uv_tcp_nodelay(&target->handle.tcp, 0);
    uv_tcp_keepalive(&source->handle.tcp, 1, 60);
    uv_tcp_keepalive(&target->handle.tcp, 1, 60);

    int rc = uv_accept(server, &source->handle.stream);
    if (rc == 0) {
        connect_to_target(target);
    } else {
        logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc));
        close_source(source);
        close_target(target);
    }
}
Example #2
0
static int tcp_keepalive(lua_State *l)
{
    tcp_udata_t        *udata = connection_udata(l);
    ls_tcp_t           *handle = udata->handle;
    int                 enable;
    int                 delay = 0;

    if (handle == NULL)
        return ls_error_return(l, LS_ERRCODE_EOF, "tcp connection closed");

    enable = lua_toboolean(l, 2);

    if (enable)
    {
        delay = luaL_checkint(l, 3);
        luaL_argcheck(l, delay>0, 3, "delay should be > 0");
    }
    
    if (uv_tcp_keepalive(&handle->handle, enable, (unsigned int)delay))
    {
        return ls_error_return(l, LS_ERRCODE_ERROR, "set keepalive failed.");
    }
    
    lua_pushboolean(l, 1);
    return 1;
}
bool TCPClient::SetKeepAlive(int enable, unsigned int delay)
{
    int iret = uv_tcp_keepalive(&client_handle_->tcphandle, enable , delay);
    if (iret) {
        errmsg_ = GetUVError(iret);
        LOG(ERROR)<<(errmsg_);
        return false;
    }
    return true;
}
Example #4
0
int luv_tcp_keepalive (lua_State* L) {
  uv_tcp_t* handle = (uv_tcp_t*)luv_checkudata(L, 1, "tcp");
  int enable = lua_toboolean(L, 2);
  int delay = lua_tointeger(L, 3);

  if (uv_tcp_keepalive(handle, enable, delay)) {
    uv_err_t err = uv_last_error(luv_get_loop(L));
    return luaL_error(L, "tcp_keepalive: %s", uv_strerror(err));
  }
  return 0;

}
Example #5
0
File: tcp.c Project: senlinms/luv
static int luv_tcp_keepalive(lua_State* L) {
  uv_tcp_t* handle = luv_check_tcp(L, 1);
  int ret, enable;
  unsigned int delay = 0;
  luaL_checktype(L, 2, LUA_TBOOLEAN);
  enable = lua_toboolean(L, 2);
  if (enable) {
    delay = luaL_checkinteger(L, 3);
  }
  ret = uv_tcp_keepalive(handle, enable, delay);
  if (ret < 0) return luv_error(L, ret);
  lua_pushinteger(L, ret);
  return 1;
}
Example #6
0
static int lluv_tcp_keepalive(lua_State *L){
  lluv_handle_t *handle = lluv_check_tcp(L, 1, LLUV_FLAG_OPEN);
  int enable = lua_toboolean(L, 2);
  unsigned int delay = 0; int err;

  if(enable) delay = (unsigned int)luaL_checkint(L, 3);
  err = uv_tcp_keepalive(LLUV_H(handle, uv_tcp_t), enable, delay);

  lua_settop(L, 1);

  if(err < 0){
    return lluv_fail(L, handle->flags, LLUV_ERR_UV, err, NULL);
  }
  return 1;
}
Example #7
0
Connection::Connection(uv_loop_t* loop,
                       const Config& config,
                       Metrics* metrics,
                       const Address& address,
                       const std::string& keyspace,
                       int protocol_version,
                       Listener* listener)
    : state_(CONNECTION_STATE_NEW)
    , is_defunct_(false)
    , is_invalid_protocol_(false)
    , is_registered_for_events_(false)
    , is_available_(false)
    , ssl_error_code_(CASS_OK)
    , pending_writes_size_(0)
    , loop_(loop)
    , config_(config)
    , metrics_(metrics)
    , address_(address)
    , addr_string_(address.to_string())
    , keyspace_(keyspace)
    , protocol_version_(protocol_version)
    , listener_(listener)
    , response_(new ResponseMessage())
    , version_("3.0.0")
    , connect_timer_(NULL)
    , ssl_session_(NULL) {
  socket_.data = this;
  uv_tcp_init(loop_, &socket_);

  if (uv_tcp_nodelay(&socket_,
                     config.tcp_nodelay_enable() ? 1 : 0) != 0) {
    LOG_WARN("Unable to set tcp nodelay");
  }

  if (uv_tcp_keepalive(&socket_,
                      config.tcp_keepalive_enable() ? 1 : 0,
                      config.tcp_keepalive_delay_secs()) != 0) {
    LOG_WARN("Unable to set tcp keepalive");
  }

  SslContext* ssl_context = config_.ssl_context();
  if (ssl_context != NULL) {
    ssl_session_.reset(ssl_context->create_session(address_));
  }
}
Example #8
0
static void connect_cb(uv_stream_t* listener, int status)
{
	int n;

	if (status) {
		SHOW_UV_ERROR(listener->loop);
		return;
	}

	server_ctx *ctx = calloc(1, sizeof(server_ctx));
	ctx->handshake_buffer = calloc(1, HANDSHAKE_BUFFER_SIZE);

	if (!ctx || !ctx->handshake_buffer)
		FATAL("malloc() failed!");

	ctx->client.data = ctx;
	ctx->remote.data = ctx;
	
	make_encryptor(&crypto, &ctx->encoder, 0, NULL);

	n = uv_tcp_init(listener->loop, &ctx->client);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_accept(listener, (uv_stream_t *)(void *)&ctx->client);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	n = uv_tcp_nodelay(&ctx->client, 1);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	#ifdef KEEPALIVE_TIMEOUT
	n = uv_tcp_keepalive(&ctx->client, 1, KEEPALIVE_TIMEOUT);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);
	#endif /* KEEPALIVE_TIMEOUT */

	n = uv_read_start((uv_stream_t *)(void *)&ctx->client, client_handshake_alloc_cb, client_handshake_read_cb);
	if (n)
		SHOW_UV_ERROR_AND_EXIT(listener->loop);

	LOGCONN(&ctx->client, "Accepted connection from %s");
}
Example #9
0
Connection::Connection(uv_loop_t* loop,
                       const Config& config,
                       Metrics* metrics,
                       const Host::ConstPtr& host,
                       const std::string& keyspace,
                       int protocol_version,
                       Listener* listener)
    : state_(CONNECTION_STATE_NEW)
    , error_code_(CONNECTION_OK)
    , ssl_error_code_(CASS_OK)
    , pending_writes_size_(0)
    , loop_(loop)
    , config_(config)
    , metrics_(metrics)
    , host_(host)
    , keyspace_(keyspace)
    , protocol_version_(protocol_version)
    , listener_(listener)
    , response_(new ResponseMessage())
    , stream_manager_(protocol_version)
    , ssl_session_(NULL)
    , heartbeat_outstanding_(false) {
  socket_.data = this;
  uv_tcp_init(loop_, &socket_);

  if (uv_tcp_nodelay(&socket_,
                     config.tcp_nodelay_enable() ? 1 : 0) != 0) {
    LOG_WARN("Unable to set tcp nodelay");
  }

  if (uv_tcp_keepalive(&socket_,
                      config.tcp_keepalive_enable() ? 1 : 0,
                      config.tcp_keepalive_delay_secs()) != 0) {
    LOG_WARN("Unable to set tcp keepalive");
  }

  SslContext* ssl_context = config_.ssl_context();
  if (ssl_context != NULL) {
    ssl_session_.reset(ssl_context->create_session(host));
  }
}
Example #10
0
void Client::connect(sockaddr *addr)
{
    setState(ConnectingState);

    reinterpret_cast<struct sockaddr_in*>(addr)->sin_port = htons(m_url.port());
    delete m_socket;

    uv_connect_t *req = new uv_connect_t;
    req->data = this;

    m_socket = new uv_tcp_t;
    m_socket->data = this;

    uv_tcp_init(uv_default_loop(), m_socket);
    uv_tcp_nodelay(m_socket, 1);

#   ifndef WIN32
    uv_tcp_keepalive(m_socket, 1, 60);
#   endif

    uv_tcp_connect(req, m_socket, reinterpret_cast<const sockaddr*>(addr), Client::onConnect);
}
Example #11
0
void timer_cb(uv_timer_t *handle) {
    size_t i,j;
    static size_t fail_count = 0;
    for(i = (size_t)handle->data,j = 0;i < CONN_NUM && j < 10;i++,j++) {
        uv_ip4_addr(addr_str, 1024+i, &addr);
        uv_tcp_bind(&c[i].ts, (const struct sockaddr*)&addr, 0);
        uv_tcp_keepalive(&c[i].ts,1,30);
        c[i].cs.data = (void*)0;
        if (0 != uv_tcp_connect(&c[i].cs,&c[i].ts,(struct sockaddr*)&addr1,connect_cb)) {
            perror("connect_rt");
            sleep(1);
            fail_count++;
            printf("failcount: %d\n",fail_count);
        }
    }
    handle->data = (void*)i;
    if(i == CONN_NUM || fail_count >= 20){
        uv_timer_stop(handle);
        //timer for 100 secs
        uv_timer_start(handle, timer_cb1, WRITE_INTERVAL, WRITE_INTERVAL);
    }
}
Example #12
0
File: tcp.c Project: eagles125/pyuv
static PyObject *
TCP_func_keepalive(TCP *self, PyObject *args)
{
    int r;
    unsigned int delay;
    PyObject *enable;

    RAISE_IF_HANDLE_NOT_INITIALIZED(self, NULL);
    RAISE_IF_HANDLE_CLOSED(self, PyExc_HandleClosedError, NULL);

    if (!PyArg_ParseTuple(args, "O!I:keepalive", &PyBool_Type, &enable, &delay)) {
        return NULL;
    }

    r = uv_tcp_keepalive(&self->tcp_h, (enable == Py_True) ? 1 : 0, delay);
    if (r != 0) {
        RAISE_UV_EXCEPTION(UV_HANDLE_LOOP(self), PyExc_TCPError);
        return NULL;
    }

    Py_RETURN_NONE;
}
Example #13
0
static void
accept_cb(uv_stream_t *stream, int status) {
    struct tundev_context *ctx = stream->data;
    struct client_context *client = new_client(ctx->tun->mtu);

    uv_tcp_init(stream->loop, &client->handle.tcp);
    int rc = uv_accept(stream, &client->handle.stream);
    if (rc == 0) {
        int len = sizeof(struct sockaddr);
        uv_tcp_getpeername(&client->handle.tcp, &client->addr, &len);
        client->handle.stream.data = ctx;
        uv_tcp_nodelay(&client->handle.tcp, 1);
        uv_tcp_keepalive(&client->handle.tcp, 1, 60);

        client->packet.size = 512;
        ctx->connect = AUTHING;

        uv_read_start(&client->handle.stream, alloc_cb, recv_cb);

    } else {
        logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc));
        close_client(client);
    }
}
Example #14
0
void TCPSocket::setKeepAlive(int enable, unsigned int delay) 
{
    init();
    int r = uv_tcp_keepalive(ptr<uv_tcp_t>(), enable, delay);
    if (r) setUVError("TCP socket error", r);
}
Example #15
0
void bud_client_create(bud_config_t* config, uv_stream_t* stream) {
  int r;
  bud_client_t* client;
  bud_client_error_t cerr;
  BIO* enc_in;
  BIO* enc_out;
#ifdef SSL_MODE_RELEASE_BUFFERS
  long mode;
#endif  /* SSL_MODE_RELEASE_BUFFERS */

  client = malloc(sizeof(*client));
  if (client == NULL)
    return;

  client->config = config;
  client->ssl = NULL;
  client->last_handshake = 0;
  client->handshakes = 0;
  client->connect = kBudProgressNone;
  client->close = kBudProgressNone;
  client->cycle = kBudProgressNone;
  client->recycle = 0;
  client->destroy_waiting = 0;

  client->id = bud_config_get_client_id(config);

  client->async_hello = kBudProgressDone;
  if (config->sni.enabled || config->stapling.enabled)
    client->async_hello = kBudProgressNone;

  /* SNI */
  client->sni_req = NULL;
  client->sni_ctx.ctx = NULL;

  /* Stapling */
  client->stapling_cache_req = NULL;
  client->stapling_req = NULL;
  client->stapling_ocsp_resp = NULL;

  /* Availability */
  client->retry = kBudProgressNone;
  client->retry_count = 0;
  client->retry_timer.data = client;
  client->backend_list = NULL;
  client->selected_backend = NULL;

  /* Proxyline */
  client->proxyline_waiting = 2;

  /* X-Forward */
  client->xforward.skip = 0;
  client->xforward.crlf = 0;

  r = uv_timer_init(config->loop, &client->retry_timer);
  if (r != 0)
    goto failed_timer_init;
  client->destroy_waiting++;

  /* Initialize buffers */
  bud_client_side_init(&client->frontend, kBudFrontend, client);
  bud_client_side_init(&client->backend, kBudBackend, client);

  /**
   * Accept client on frontend
   */
  r = uv_tcp_init(config->loop, &client->frontend.tcp);
  if (r != 0)
    goto failed_tcp_in_init;

  client->destroy_waiting++;
  r = uv_accept(stream, (uv_stream_t*) &client->frontend.tcp);
  if (r != 0)
    goto failed_accept;

  cerr = bud_client_read_start(client, &client->frontend);
  if (!bud_is_ok(cerr.err))
    goto failed_accept;
  client->frontend.reading = kBudProgressRunning;

  /* Fill hosts */
  cerr = bud_client_fill_host(client, &client->local);
  if (!bud_is_ok(cerr.err))
    goto failed_accept;

  cerr = bud_client_fill_host(client, &client->remote);
  if (!bud_is_ok(cerr.err))
    goto failed_accept;

  /*
   * Select a backend and connect to it, or wait for a backend to become
   * alive again.
   */
  /* SNI backend comes from `backend` or sni callback */
  client->backend_list = &config->contexts[0].backend;
  client->balance = config->balance_e;
  if (client->balance == kBudBalanceSNI) {
    client->selected_backend = NULL;
    client->connect = kBudProgressRunning;
  } else {
    client->selected_backend = bud_select_backend(client);
  }

  /* No backend can be selected yet, wait for SNI */
  if (client->selected_backend == NULL) {
    client->backend.close = kBudProgressDone;
    cerr = bud_client_ok(&client->backend);

  /* No backend alive, try reconnecting */
  } else if (client->selected_backend->dead) {
    DBG_LN(&client->backend, "all backends dead, scheduling reconnection");
    cerr = bud_client_retry(client);

  /* Backend alive - connect immediately */
  } else {
    cerr = bud_client_connect(client);
  }
  if (!bud_is_ok(cerr.err))
    goto failed_accept;

  /* Adjust sockets */
  r = uv_tcp_nodelay(&client->frontend.tcp, 1);
  if (r == 0 && config->frontend.keepalive > 0)
    r = uv_tcp_keepalive(&client->frontend.tcp, 1, config->frontend.keepalive);
  if (r != 0)
    goto failed_connect;

  /* Initialize SSL */

  /* First context is always default */
  client->ssl = SSL_new(config->contexts[0].ctx);
  if (client->ssl == NULL)
    goto failed_connect;

  if (!SSL_set_ex_data(client->ssl, kBudSSLClientIndex, client))
    goto failed_connect;

  SSL_set_cert_cb(client->ssl, bud_client_ssl_cert_cb, client);
  SSL_set_info_callback(client->ssl, bud_client_ssl_info_cb);

  enc_in = bud_bio_new(&client->frontend.input);
  if (enc_in == NULL)
    goto failed_connect;
  enc_out = bud_bio_new(&client->frontend.output);
  if (enc_out == NULL) {
    BIO_free_all(enc_in);
    goto failed_connect;
  }
  SSL_set_bio(client->ssl, enc_in, enc_out);

#ifdef SSL_MODE_RELEASE_BUFFERS
  mode = SSL_get_mode(client->ssl);
  SSL_set_mode(client->ssl, mode | SSL_MODE_RELEASE_BUFFERS);
#endif  /* SSL_MODE_RELEASE_BUFFERS */

  SSL_set_accept_state(client->ssl);

  bud_trace_frontend_accept(client);
  DBG_LN(&client->frontend, "new");
  return;

failed_connect:
  client->connect = kBudProgressDone;
  client->close = kBudProgressDone;
  uv_close((uv_handle_t*) &client->backend.tcp, bud_client_close_cb);

failed_accept:
  uv_close((uv_handle_t*) &client->frontend.tcp, bud_client_close_cb);

failed_tcp_in_init:
  uv_close((uv_handle_t*) &client->retry_timer, bud_client_close_cb);
  return;

failed_timer_init:
  free(client);
}
Example #16
0
File: tcp.hpp Project: skypjack/uvw
 /**
  * @brief Enables/Disables TCP keep-alive.
  * @param enable True to enable it, false otherwise.
  * @param time Initial delay in seconds (use
  * `std::chrono::duration<unsigned int>`).
  * @return True in case of success, false otherwise.
  */
 bool keepAlive(bool enable = false, Time time = Time{0}) {
     return (0 == uv_tcp_keepalive(get(), enable, time.count()));
 }
Example #17
0
 bool keepalive(bool enable, unsigned int delay) {
   return uv_tcp_keepalive(get<uv_tcp_t>(), enable ? 1 : 0, delay) == 0;
 }
Example #18
0
extern "C" int
rust_uv_tcp_keepalive
(uv_tcp_t* handle, int enable, unsigned int delay) {
    return uv_tcp_keepalive(handle, enable, delay);
}
Example #19
0
int  UvTcpSocket::keepAlive(bool enable, unsigned int value)
{
	return uv_tcp_keepalive(m_uv_tcp,enable?1:0,value);
}
Example #20
0
File: tcp.cpp Project: Xsoda/uvplus
int uvplus_tcp::keepalive(int enable, unsigned int delay) {
  auto tcp = (uv_tcp_t *)context_ptr();
  return uv_tcp_keepalive(tcp, enable, delay);
}