/* Create a new mqtt request instance */ struct tcp_conn *tcp_conn_add(int fd, struct flb_in_tcp_config *ctx) { int ret; struct tcp_conn *conn; struct mk_event *event; conn = flb_malloc(sizeof(struct tcp_conn)); if (!conn) { return NULL; } /* Set data for the event-loop */ event = &conn->event; MK_EVENT_NEW(event); event->fd = fd; event->type = FLB_ENGINE_EV_CUSTOM; event->handler = tcp_conn_event; /* Connection info */ conn->fd = fd; conn->ctx = ctx; conn->buf_len = 0; conn->rest = 0; conn->status = TCP_NEW; conn->buf_data = flb_malloc(ctx->chunk_size); if (!conn->buf_data) { perror("malloc"); close(fd); flb_error("[in_tcp] could not allocate new connection"); flb_free(conn); return NULL; } conn->buf_size = ctx->chunk_size; conn->in = ctx->in; /* Initialize JSON parser */ flb_pack_state_init(&conn->pack_state); /* Register instance into the event loop */ ret = mk_event_add(ctx->evl, fd, FLB_ENGINE_EV_CUSTOM, MK_EVENT_READ, conn); if (ret == -1) { flb_error("[in_tcp] could not register new connection"); close(fd); flb_free(conn->buf_data); flb_free(conn); return NULL; } mk_list_add(&conn->_head, &ctx->connections); return conn; }
/* Remove an event */ int mk_event_del(struct mk_event_loop *loop, struct mk_event *event) { int ret; struct mk_event_ctx *ctx; ctx = loop->data; /* just remove a registered event */ if ((event->status & MK_EVENT_REGISTERED) == 0) { return -1; } ret = _mk_event_del(ctx, event); if (ret == -1) { return -1; } /* Reset the status and mask */ MK_EVENT_NEW(event); return 0; }
static inline void thread_cb_init_vars() { int close; int type = libco_param.type; struct mk_vhost_handler *handler = libco_param.handler; struct mk_http_session *session = libco_param.session; struct mk_http_request *request = libco_param.request; struct mk_thread *th = libco_param.th; struct mk_http_thread *mth; //struct mk_plugin *plugin; /* * Until this point the th->callee already set the variables, so we * wait until the core wanted to resume so we really trigger the * output callback. */ co_switch(th->caller); if (type == MK_HTTP_THREAD_LIB) { /* Invoke the handler callback */ handler->cb(request, handler->data); /* * Once the callback finished, we need to sanitize the connection * so other further requests can be processed. */ int ret; struct mk_sched_worker *sched; struct mk_channel *channel; channel = request->session->channel; sched = mk_sched_get_thread_conf(); MK_EVENT_NEW(channel->event); ret = mk_event_add(sched->loop, channel->fd, MK_EVENT_CONNECTION, MK_EVENT_READ, channel->event); if (ret == -1) { //return -1; } /* Save temporal session */ mth = request->thread; /* * Finalize request internally, if ret == -1 means we should * ask to shutdown the connection. */ ret = mk_http_request_end(session, session->server); if (ret == -1) { close = MK_TRUE; } else { close = MK_FALSE; } mk_http_thread_purge(mth, close); /* Return control to caller */ mk_thread_yield(th); } else if (type == MK_HTTP_THREAD_PLUGIN) { /* FIXME: call plugin handler callback with params */ } }
/* * This routine perform a TCP connection and the required TLS/SSL * handshake, */ FLB_INLINE int flb_io_net_tls_connect(struct flb_io_upstream *u, struct flb_thread *th) { int fd; int ret; int error = 0; int flag; socklen_t len = sizeof(error); struct flb_tls_session *session; if (u->fd > 0) { close(u->fd); } /* Create the socket */ fd = flb_net_socket_create(AF_INET, FLB_TRUE); if (fd == -1) { flb_error("[io] could not create socket"); return -1; } u->fd = fd; /* Make the socket non-blocking */ flb_net_socket_nonblocking(u->fd); /* Start the connection */ ret = flb_net_tcp_fd_connect(fd, u->tcp_host, u->tcp_port); if (ret == -1) { if (errno == EINPROGRESS) { flb_debug("[upstream] connection in process"); } else { close(u->fd); if (u->tls_session) { tls_session_destroy(u->tls_session); u->tls_session = NULL; } return -1; } MK_EVENT_NEW(&u->event); u->thread = th; ret = mk_event_add(u->evl, fd, FLB_ENGINE_EV_THREAD, MK_EVENT_WRITE, &u->event); if (ret == -1) { /* * If we failed here there no much that we can do, just * let the caller we failed */ flb_error("[io_tls] connect failed registering event"); close(fd); return -1; } /* * Return the control to the parent caller, we need to wait for * the event loop to get back to us. */ flb_thread_yield(th, FLB_FALSE); /* Check the connection status */ if (u->event.mask & MK_EVENT_WRITE) { ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len); if (ret == -1) { flb_error("[io_tls] could not validate socket status"); goto error; } if (error != 0) { /* Connection is broken, not much to do here */ flb_error("[io_tls] connection failed"); goto error; } } else { return -1; } } /* Configure TLS and prepare handshake */ session = u->tls_session; mbedtls_ssl_set_bio(&session->ssl, u, mbedtls_net_send, mbedtls_net_recv, NULL); retry_handshake: ret = mbedtls_ssl_handshake(&session->ssl); if (ret != 0) { if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) { io_tls_error(ret); goto error; } if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) { flag = MK_EVENT_WRITE; } else if (ret == MBEDTLS_ERR_SSL_WANT_READ) { flag = MK_EVENT_READ; } else { } /* * FIXME: if we need multiple reads we are invoking the same * system call multiple times. */ ret = mk_event_add(u->evl, u->event.fd, FLB_ENGINE_EV_THREAD, flag, &u->event); if (ret == -1) { goto error; } flb_thread_yield(th, FLB_FALSE); goto retry_handshake; } else { flb_debug("[io_tls] Handshake OK"); } if (u->event.status == MK_EVENT_REGISTERED) { mk_event_del(u->evl, &u->event); } flb_debug("[io_tls] connection OK"); return 0; error: if (u->event.status == MK_EVENT_REGISTERED) { mk_event_del(u->evl, &u->event); } return -1; }