void process_client(void *arg)
{
	int i;
	char b;
	t_buffer *buffers[4];
	t_buffer buffer[4];
	t_rinoosocket *socket = arg;

	buffer_static(&buffer[0], big_buffer, BUFFER_SIZE);
	buffers[0] = &buffer[0];
	rinoo_log("server - client accepted");
	rinoo_log("server - sending %d bytes", BUFFER_SIZE);
	XTEST(rinoo_socket_writev(socket, buffers, 1) == BUFFER_SIZE);
	rinoo_log("server - receiving 'b'");
	XTEST(rinoo_socket_read(socket, &b, 1) == 1);
	XTEST(b == 'b');
	for (i = 0; i < 4; i++) {
		buffer_static(&buffer[i], big_buffer + (i * (BUFFER_SIZE / 4)), BUFFER_SIZE / 4);
		buffers[i] = &buffer[i];
	}
	rinoo_log("server - sending %d bytes", BUFFER_SIZE);
	XTEST(rinoo_socket_writev(socket, buffers, 4) == BUFFER_SIZE);
	rinoo_log("server - receiving 'b'");
	XTEST(rinoo_socket_read(socket, &b, 1) == 1);
	XTEST(b == 'b');
	rinoo_log("server - receiving nothing");
	XTEST(rinoo_socket_read(socket, &b, 1) == -1);
	rinoo_socket_destroy(socket);
}
Ejemplo n.º 2
0
/**
 * HTTP client processing callback
 *
 * @param context Pointer to a HTTP easy context
 */
static void rinoohttp_easy_client_process(void *context)
{
	int i;
	bool found;
	t_buffer body;
	t_rinoohttp http;
	t_rinoohttp_easy_context *econtext = context;

	rinoohttp_init(econtext->socket, &http);
	while (rinoohttp_request_get(&http)) {
		for (i = 0, found = false; i < econtext->nbroutes && found == false; i++) {
			if (econtext->routes[i].uri == NULL ||
			buffer_strcmp(&http.request.uri, econtext->routes[i].uri) == 0 ||
			(econtext->routes[i].type == RINOO_HTTP_ROUTE_DIR && buffer_strncmp(&http.request.uri, econtext->routes[i].uri, strlen(econtext->routes[i].uri)) == 0)) {
				rinoohttp_easy_route_call(&http, &econtext->routes[i]);
				found = true;
			}
		}
		if (found == false) {
			http.response.code = 404;
			strtobuffer(&body, RINOO_HTTP_ERROR_404);
			rinoohttp_response_send(&http, &body);
		}
		rinoohttp_reset(&http);
	}
	rinoohttp_destroy(&http);
	rinoo_socket_destroy(econtext->socket);
	free(econtext);
}
Ejemplo n.º 3
0
void task_client(void *socket)
{
	char a;

	rinoo_socket_write(socket, "Hello world!\n", 13);
	rinoo_socket_read(socket, &a, 1);
	rinoo_socket_destroy(socket);
}
void process_client(void *arg)
{
	char a;
	t_rinoosocket *socket = arg;

	rinoo_log("server - client accepted");
	rinoo_log("server - receiving nothing, waiting timeout");
	XTEST(rinoo_socket_read(socket, &a, 1) <= 0);
	rinoo_socket_destroy(socket);
}
Ejemplo n.º 5
0
void task_server(void *sched)
{
	t_socket *server;
	t_socket *client;

	server = rinoo_tcp_server(sched, IP_ANY, 4242);
	while ((client = rinoo_tcp_accept(server, NULL, NULL)) != NULL) {
		rinoo_task_start(sched, task_client, client);
	}
	rinoo_socket_destroy(server);
}
Ejemplo n.º 6
0
void server_func(void *unused(arg))
{
	t_socket *server;
	t_socket *client;

	server = rinoo_tcp_server(rinoo_sched_self(), IP_ANY, 4242);
	XTEST(server != NULL);
	client = rinoo_tcp_accept(server, NULL, NULL);
	XTEST(client != NULL);
	rinoo_log("client accepted");
	rinoo_task_start(rinoo_sched_self(), process_client, client);
	rinoo_socket_destroy(server);
}
Ejemplo n.º 7
0
/**
 * HTTP server processing callback
 *
 * @param context Pointer to a HTTP easy context
 */
static void rinoohttp_easy_server_process(void *context)
{
	t_rinoosocket *client;
	t_rinoohttp_easy_context *c_context;
	t_rinoohttp_easy_context *s_context = context;

	while ((client = rinoo_tcp_accept(s_context->socket, NULL, NULL)) != NULL) {
		c_context = malloc(sizeof(*c_context));
		if (c_context == NULL) {
			rinoo_socket_destroy(client);
			rinoo_socket_destroy(s_context->socket);
			free(s_context);
			return;
		}
		c_context->socket = client;
		c_context->routes = s_context->routes;
		c_context->nbroutes = s_context->nbroutes;
		rinoo_task_start(s_context->socket->node.sched, rinoohttp_easy_client_process, c_context);
	}
	rinoo_socket_destroy(s_context->socket);
	free(s_context);
}
Ejemplo n.º 8
0
Archivo: http.c Proyecto: dyu/librinoo
void http_server_process(void *socket)
{
	t_buffer content;
	t_http http;

	XTEST(rinoo_http_init(socket, &http) == 0);
	XTEST(rinoo_http_request_get(&http));
	http.response.code = 200;
	strtobuffer(&content, HTTP_CONTENT);
	XTEST(rinoo_http_response_send(&http, &content) == 0);
	rinoo_http_destroy(&http);
	rinoo_socket_destroy(socket);
}
Ejemplo n.º 9
0
void client_func(void *unused(arg))
{
	t_buffer buffer;
	t_socket *client;

	client = rinoo_tcp_client(rinoo_sched_self(), IP_LOOPBACK, 4242, 0);
	XTEST(client != NULL);
	str = malloc(sizeof(*str) * TRANSFER_SIZE);
	XTEST(str != NULL);
	memset(str, 'a', TRANSFER_SIZE);
	buffer_static(&buffer, str, TRANSFER_SIZE);
	XTEST(rinoo_socket_writeb(client, &buffer) == TRANSFER_SIZE);
	rinoo_socket_destroy(client);
}
Ejemplo n.º 10
0
void client_func(void *arg)
{
	int i;
	ssize_t res;
	ssize_t total;
	char tmp[8];
	struct sockaddr_in6 addr = { 0 };
	t_rinoosocket *socket;
	t_rinoosched *sched = arg;

	socket = rinoo_socket(sched, &socket_class_tcp6);
	XTEST(socket != NULL);
	addr.sin6_port = htons(4242);
	addr.sin6_family = AF_INET6;
	addr.sin6_addr = in6addr_loopback;
	XTEST(rinoo_socket_connect(socket, (struct sockaddr *) &addr, sizeof(addr)) == 0);
	rinoo_log("client - connected");
	rinoo_log("client - reading %d bytes", BUFFER_SIZE);
	for (i = 0; i < BUFFER_SIZE / 8; i++) {
		res = 0;
		total = 0;
		while (total < 8 && (res = rinoo_socket_read(socket, tmp + total, 8 - total)) > 0) {
			total += res;
		}
		if (res < 0) {
			rinoo_log("Error: %s", strerror(errno));
		}
		XTEST(res > 0);
		XTEST(memcmp(tmp, "xxxxxxxx", 8) == 0);
	}
	rinoo_log("client - sending 'b'");
	rinoo_log("client - reading %d bytes", BUFFER_SIZE);
	XTEST(rinoo_socket_write(socket, "b", 1) == 1);
	for (i = 0; i < BUFFER_SIZE / 8; i++) {
		res = 0;
		total = 0;
		while (total < 8 && (res = rinoo_socket_read(socket, tmp + total, 8 - total)) > 0) {
			total += res;
		}
		if (res < 0) {
			rinoo_log("Error: %s", strerror(errno));
		}
		XTEST(res > 0);
		XTEST(memcmp(tmp, "xxxxxxxx", 8) == 0);
	}
	rinoo_log("client - sending 'b'");
	XTEST(rinoo_socket_write(socket, "b", 1) == 1);
	rinoo_socket_destroy(socket);
}
Ejemplo n.º 11
0
void process_client(void *socket)
{
	t_buffer *buffer;

	buffer = buffer_create(NULL);
	XTEST(buffer != NULL);
	while (rinoo_socket_readb(socket, buffer) > 0) {
		rinoo_log("receiving...");
	}
	XTEST(buffer_size(buffer) == TRANSFER_SIZE);
	XTEST(buffer_strncmp(buffer, str, TRANSFER_SIZE) == 0);
	buffer_destroy(buffer);
	free(str);
	rinoo_socket_destroy(socket);
}
Ejemplo n.º 12
0
Archivo: http.c Proyecto: dyu/librinoo
void http_server(void *sched)
{
	t_ip ip;
	uint16_t port;
	t_socket *server;
	t_socket *client;

	server = rinoo_tcp_server(sched, IP_ANY, 4242);
	XTEST(server != NULL);
	client = rinoo_tcp_accept(server, &ip, &port);
	XTEST(client != NULL);
	rinoo_log("server - accepting client (%s:%d)", inet_ntoa(*(struct in_addr *) &ip), port);
	rinoo_task_start(sched, http_server_process, client);
	rinoo_socket_destroy(server);
}
Ejemplo n.º 13
0
Archivo: http.c Proyecto: dyu/librinoo
void http_client(void *sched)
{
	t_http http;
	t_socket *client;

	client = rinoo_tcp_client(sched, IP_LOOPBACK, 4242, 0);
	XTEST(client != NULL);
	XTEST(rinoo_http_init(client, &http) == 0);
	XTEST(rinoo_http_request_send(&http, RINOO_HTTP_METHOD_GET, "/", NULL) == 0);
	XTEST(rinoo_http_response_get(&http));
	XTEST(buffer_size(&http.response.content) == strlen(HTTP_CONTENT));
	XTEST(http.response.code == 200);
	rinoo_http_destroy(&http);
	rinoo_socket_destroy(client);
}
void server_func(void *arg)
{
	t_rinoosocket *server;
	t_rinoosocket *client;
	struct sockaddr_in6 addr = { 0 };
	t_rinoosched *sched = arg;

	server = rinoo_socket(sched, &socket_class_tcp6);
	XTEST(server != NULL);
	addr.sin6_port = htons(4242);
	addr.sin6_family = AF_INET6;
	addr.sin6_addr = in6addr_any;
	XTEST(rinoo_socket_bind(server, (struct sockaddr *) &addr, sizeof(addr), 42) == 0);
	rinoo_log("server listening...");
	client = rinoo_socket_accept(server, NULL, NULL);
	XTEST(client != NULL);
	rinoo_task_start(sched, process_client, client);
	rinoo_socket_destroy(server);
}
void client_func(void *arg)
{
	char a;
	struct sockaddr_in6 addr = { 0 };
	t_rinoosocket *socket;
	t_rinoosched *sched = arg;

	socket = rinoo_socket(sched, &socket_class_tcp6);
	XTEST(socket != NULL);
	addr.sin6_port = htons(4242);
	addr.sin6_family = AF_INET6;
	addr.sin6_addr = in6addr_loopback;
	XTEST(rinoo_socket_connect(socket, (struct sockaddr *) &addr, sizeof(addr)) == 0);
	rinoo_log("client - connected");
	rinoo_socket_timeout(socket, 1000);
	XTEST(rinoo_socket_read(socket, &a, 1) == -1);
	rinoo_log("server  - timeout");
	perror("socket");
	rinoo_socket_destroy(socket);
}
Ejemplo n.º 16
0
void rsc_start_client(void *client_sched)
{
    bool redo;
    uint32_t i;
    t_email email;
    t_session session = { 0 };

    redo = true;
    email = *config->email;
    session.email = &email;
    session.helo = config->session.helo;
    session.read = buffer_create(NULL);
    if (session.read == NULL) {
        goto send_error;
    }
    session.write = buffer_create(NULL);
    if (session.write == NULL) {
        goto send_error;
    }
    rinoo_email_session_set(&email, &session);
    while (redo) {
        session.socket = rinoo_tcp_client(client_sched, &config->connection.ip, config->connection.port, config->connection.timeout * 1000);
        if (session.socket == NULL) {
            rinoo_log("Error: %s", strerror(errno));
            config->stats.thread[rinoo_sched_self()->id].failed++;
            rsc_log(config, RSC_LOG_ERROR, "Couldn't create socket to %s:%d", config->connection.server, config->connection.port);
            goto send_error;
        }
        config->stats.thread[rinoo_sched_self()->id].sessions++;
        if (rinoo_smtp_start(&email) != 0) {
            goto send_error;
        }
        for (i = 0; i < config->session.nbmsg; i++) {
            if (rinoo_smtp_send(&email) != 0) {
                goto send_error;
            }
            config->stats.thread[rinoo_sched_self()->id].sent++;
        }
        if (rinoo_smtp_end(&email) != 0) {
            goto send_error;
        }
        rinoo_socket_destroy(session.socket);
        session.socket = NULL;
        config->stats.thread[rinoo_sched_self()->id].sessions--;
        if (config->mode == RSC_MODE_SECOND) {
            if (rinoo_task_wait(client_sched, 1000) != 0) {
                goto send_error;
            }
        } else if (config->mode != RSC_MODE_FLOOD) {
            redo = false;
        }
    }
send_error:
    if (session.read != NULL) {
        buffer_destroy(session.read);
    }
    if (session.write != NULL) {
        buffer_destroy(session.write);
    }
    if (session.socket != NULL) {
        config->stats.thread[rinoo_sched_self()->id].sessions--;
        config->stats.thread[rinoo_sched_self()->id].failed++;
        rinoo_socket_destroy(session.socket);
    }
    if (__atomic_fetch_sub(&thread_counter, 1, __ATOMIC_SEQ_CST) == 1) {
        sched->stop = true;
    }
}