Exemplo n.º 1
0
/**
 * Invoked when a TCP listening socket fd is ready
 * to accept a new client. Accepts the client, initializes
 * the connection buffers, and prepares to start listening
 * for client data
 */
static void handle_new_client(int listen_fd, worker_ev_userdata* data) {
    // Accept the client connection
    struct sockaddr_in client_addr;
    int client_addr_len = sizeof(client_addr);
    int client_fd = accept(listen_fd,
                        (struct sockaddr*)&client_addr,
                        &client_addr_len);

    // Check for an error
    if (client_fd == -1) {
        syslog(LOG_ERR, "Failed to accept() connection! %s.", strerror(errno));
        return;
    }

    // Setup the socket
    if (set_client_sockopts(client_fd)) {
        return;
    }

    // Debug info
    syslog(LOG_DEBUG, "Accepted client connection: %s %d [%d]",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), client_fd);

    // Get the associated conn object
    conn_info *conn = get_conn(data->netconf);

    // Initialize the libev stuff
    ev_io_init(&conn->client, prepare_event, client_fd, EV_READ);
    ev_io_init(&conn->write_client, prepare_event, client_fd, EV_WRITE);

    // Schedule the new client
    schedule_async(data->netconf, SCHEDULE_WATCHER, &conn->client);
}
Exemplo n.º 2
0
/**
 * Invoked when a TCP listening socket fd is ready
 * to accept a new client. Accepts the client, initializes
 * the connection buffers, and stars to listening for data
 */
static void handle_new_client(ev_io *watcher, int ready_events) {
    // Accept the client connection
    int listen_fd = watcher->fd;
    struct sockaddr_in client_addr;
    int client_addr_len = sizeof(client_addr);
    int client_fd = accept(listen_fd,
                        (struct sockaddr*)&client_addr,
                        &client_addr_len);

    // Check for an error
    if (client_fd == -1) {
        syslog(LOG_ERR, "Failed to accept() connection! %s.", strerror(errno));
        return;
    }

    // Setup the socket
    if (set_client_sockopts(client_fd)) {
        return;
    }

    // Debug info
    syslog(LOG_DEBUG, "Accepted client connection: %s %d [%d]",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), client_fd);

    // Get the associated conn object
    conn_info *conn = get_conn();

    // Initialize the libev stuff
    ev_io_init(&conn->client, invoke_event_handler, client_fd, EV_READ);
    ev_io_start(&conn->client);
}
Exemplo n.º 3
0
/**
 * Invoked when a TCP listening socket fd is ready
 * to accept a new client. Accepts the client, initializes
 * the connection buffers, and prepares to start listening
 * for client data
 */
static void handle_new_client(ev_loop *lp, ev_io *watcher, int ready_events) {
    // Get the network configuration
    bloom_networking *netconf = ev_userdata(lp);

    // Accept the client connection
    struct sockaddr_in client_addr;
    int client_addr_len = sizeof(client_addr);
    int client_fd = accept(watcher->fd,
                        (struct sockaddr*)&client_addr,
                        &client_addr_len);

    // Check for an error
    if (client_fd == -1) {
        syslog(LOG_ERR, "Failed to accept() connection! %s.", strerror(errno));
        return;
    }

    // Setup the socket
    if (set_client_sockopts(client_fd)) {
        return;
    }

    // Debug info
    syslog(LOG_DEBUG, "Accepted client connection: %s %d [%d]",
            inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), client_fd);

    // Get the associated conn object
    conn_info *conn = get_conn();

    // Initialize the libev stuff
    ev_io_init(&conn->client, invoke_event_handler, client_fd, EV_READ);
    ev_io_init(&conn->write_client, handle_client_writebuf, client_fd, EV_WRITE);

    // Dispatch this client to a worker thread
    int next_thread = netconf->last_assign++ % netconf->config->worker_threads;
    worker_ev_userdata *data = netconf->workers[next_thread];

    // Sent accept along with the connection
    write(data->pipefd[1], "a", 1);
    write(data->pipefd[1], &conn, sizeof(conn_info*));
}
Exemplo n.º 4
0
/**
 * Attempts to establish connection
 * Updates connected flag in proxy_conn_info
 *
 * @arg proxy_conn proxy connection struct
 * @return 0 on success, 1 on error.
 */
static int connect_proxy(proxy_conn_info *proxy_conn) {
	int tcp_fd;
	struct sockaddr_in proxy_addr;

	parse_ip(&proxy_addr, proxy_conn->addr);

	// Create TCP socket
	tcp_fd = socket(AF_INET, SOCK_STREAM, 0);
	if (tcp_fd < 0) {
		syslog(LOG_ERR, "Failed to create TCP socket");
		return 1;
	}

	// Open TCP Connection
	int res = connect(tcp_fd, (struct sockaddr *)&proxy_addr, sizeof(struct sockaddr));
	if (res) {
		proxy_conn->connected = 0;
		close(tcp_fd);
		syslog(LOG_DEBUG, "Unable to connect to: %s", proxy_conn->addr);
		return 1;
	}

	// Set socket options
	set_client_sockopts(tcp_fd);
	syslog(LOG_DEBUG, "Establish TCP proxy connection: %s %d [%d]",
					inet_ntoa(proxy_addr.sin_addr), ntohs(proxy_addr.sin_port), tcp_fd);

	proxy_conn->connected = 1;
	proxy_conn->tcp_fd = tcp_fd;

	// Initialize the libev stuff
	ev_io_init(&proxy_conn->tcp->client, prepare_event, tcp_fd, EV_READ);
	ev_io_init(&proxy_conn->tcp->write_client, prepare_event, tcp_fd, EV_WRITE);

	return 0;
}