Пример #1
0
void
udp_server(lthread_t *lt, void *args)
{
    struct sockaddr_in listener;
    struct sockaddr_in client;
    socklen_t listener_len = sizeof(listener);
    int s;
    int ret;
    char buf[64];
    lthread_detach();

    if ((s=lthread_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
        perror("error");
        return;
    }

    memset((char *) &listener, 0, sizeof(listener));
    listener.sin_family = AF_INET;
    listener.sin_port = htons(5556);
    listener.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(s, (struct sockaddr *)&listener, sizeof(listener)) == -1) {
        perror("Cannot bind");
        return;
    }

    while (1) {
        ret = lthread_recvfrom(s, buf, 64, 0, (struct sockaddr *)&client, &listener_len, 1000);
        printf("ret returned %d: %s\n", ret, buf);
    }

   close(s);
}
Пример #2
0
static void lt_request_cli(void *arg)
{
    int ret;
    lthread_t *lt = NULL;
    proxy_conn_t *priv_conn = proxy_conn_create(-1, 1);
    proxy_conn_t *proxy_conn = (proxy_conn_t*)arg;

    DEFINE_LTHREAD;
    lthread_detach();

    fprintf(stderr, "request new client\n");
    ret = tcp_dial_ipv4(&priv_conn->conn, "192.168.6.1:80");
    if(0 != ret) {
        proxy_conn_free(priv_conn);
        proxy_conn_free(proxy_conn);
        return;
    }

    fprintf(stderr, "dial priv conn ok\n");
    priv_conn->other = proxy_conn;
    proxy_conn->other = priv_conn;
    //lthread_create(&lt1, (void*)lt_proxy_loop, (void*)priv_conn);
    lthread_create(&lt, (void*)lt_proxy_loop, (void*)proxy_conn);
    lt_proxy_loop(priv_conn);

    lthread_join(lt, NULL, LTHREAD_FOREVER);

    void* data = NULL;
    kstring_t *buf = NULL;
    while(0 == chan_recv(priv_conn->write_ch, &data)) {
        if(data != NULL) {
            buf = (kstring_t*)data;
            ks_free(buf);
            free(buf);
        } else {
            //mark as end
            break;
        }
    }
    while(0 == chan_recv(proxy_conn->write_ch, &data)) {
        if(data != NULL) {
            buf = (kstring_t*)data;
            ks_free(buf);
            free(buf);
        } else {
            //mark as end
            break;
        }
    }
    proxy_conn_free(priv_conn);
    proxy_conn_free(proxy_conn);
}
Пример #3
0
static void lt_signal(void *arg)
{
    sigset_t mask;
    int sfd;
    struct signalfd_siginfo fdsi;
    ssize_t s;
    proxy_server *srv = (proxy_server*)arg;

    DEFINE_LTHREAD;
    lthread_detach();

    sigemptyset(&mask);
    sigaddset(&mask, SIGINT);
    sigaddset(&mask, SIGQUIT);

    if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
        handle_error("sigprocmask");
    }

    sfd = signalfd(-1, &mask, SFD_NONBLOCK);
    if (sfd == -1) {
        handle_error("signalfd");
    }
    fprintf(stderr, "%s#%d started\n", __FUNCTION__, __LINE__);

    for (;;) {
        s = lthread_read(sfd, &fdsi, sizeof(struct signalfd_siginfo), LTHREAD_FOREVER);
        if (s != sizeof(struct signalfd_siginfo)) {
            handle_error("read");
        }
        fprintf(stderr, "%s#%d signal\n", __FUNCTION__, __LINE__);

        if (fdsi.ssi_signo == SIGINT) {
            fprintf(stderr, "Got SIGINT\n");
            chan_close(srv->die_ch);
            break;
        } else if (fdsi.ssi_signo == SIGQUIT) {
            fprintf(stderr, "Got SIGQUIT\n");
            chan_close(srv->die_ch);
            break;
        } else {
            printf("Read unexpected signal\n");
        }
    }
    close(sfd);
}
Пример #4
0
void run_query(void *arg) {
	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;

	lthread_detach();
	lthread_print_timestamp(__FUNCTION__);
	putchar('\n');

	char *server = "localhost";
	char *user = "******";
	char *password = "******";
	char *database = "lthread";
	char sql_req[256] = "SELECT id,entry FROM testing";

	conn = mysql_init(NULL);

	/* Connect to database */
	if (!mysql_real_connect(conn, server,
							user, password, database, 0, NULL, 0)) {
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(1);
	}

	printf("[*] connected. sql request will be '%s'\n", sql_req);
	if (mysql_query(conn, sql_req)) {
		fprintf(stderr, "%s\n\n", mysql_error(conn));
		exit(1);
	}

	res = mysql_use_result(conn);

	while ((row = mysql_fetch_row(res)) != NULL)
		printf("%s '%s'\n", row[0], row[1]);

	printf("[*] done reading from db\n");
	mysql_free_result(res);
	mysql_close(conn);

	printf("[*] resources freed, exiting\n\n");
	lthread_print_timestamp(__FUNCTION__);
	return;
}
Пример #5
0
void lt_main(void *arg)
{
    int fd, opt = 1;
    proxy_server *srv = (proxy_server*)arg;
    lthread_t *lt_accept = NULL;
    struct sockaddr cin = {0};
    socklen_t addrlen = sizeof(struct sockaddr);
    proxy_conn_t *proxy = NULL;

    DEFINE_LTHREAD;
    lthread_detach();

    srv->listen_fd = create_listener("0.0.0.0", 9000);
    if(srv->listen_fd < 0) {
        exit(1);
    }
    fprintf(stderr, "listener creating :9000\n");

    lthread_create(&lt_accept, (void*)lt_accept_loop, (void*)srv);

    while(!srv->is_die) {
        fd = lthread_accept(srv->listen_fd, &cin, &addrlen);
        if(fd < 0) {
            perror("accept error");
            break;
        }
        if(srv->quiting) {
            lthread_close(fd);
            break;
        }
        if(srv->is_die) {
            //already die, close and break
            lthread_close(fd);
            fprintf(stderr, "server already die :9000\n");
            break;
        }

        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) == -1) {
            perror("failed to set SOREUSEADDR on socket");
            break;
        }

        fprintf(stderr, "accept new client\n");
        proxy = proxy_conn_create(fd, 0);
        if(0 != chan_send(srv->accepts_ch, proxy)) {
            //send failed, free proxy
            proxy_conn_free(proxy);
            break;
        }

        //yield myself
        lthread_sleep((uint64_t)0);
    }
    if(-1 != srv->listen_fd) {
        close(srv->listen_fd);
        srv->listen_fd = -1;
    }
    if(!srv->is_die) {
        srv->is_die = 1;
        chan_close(srv->die_ch);
        fprintf(stderr, "srv die\n");
    }

    fprintf(stderr, "lt_accept end\n");
    lthread_join(lt_accept, NULL, LTHREAD_FOREVER);

    //server release
    server_release(srv);
}