Example #1
0
void
client_actions(int fd){

    char *tag;

    tag=xmlgetfirsttagname(client[fd].buffer);

    switch (client[fd].state) {

        case STREAM: {
            /* setup a new stream */
            printf("STREAM\n");          
            stream(fd);
        } break;

        case LOGIN: {
            /* perform login procedures */
            printf("LOGIN\n");          

            if (!strcmp(tag,"iq"))
                client_login(fd);
            else
                unknown_msg(fd);

        } break;

        case CHAT: {
            printf("CHAT\n");          

            if (!strcmp(tag,"iq"))
                iq(fd);
            else if (!strcmp(tag,"presence"))
                presence(fd);
            else if (!strcmp(tag,"message"))
                message(fd);
            else
                unknown_msg(fd);
        } break;

    } /* end case */

    free(tag);
    
} /* end client actions */
Example #2
0
static	int do_client_login(int argc, char **argv)
{
	char	*cmd = NULL;
	if (argc > 1) {
		int len, i;
		for (len = i = 1; i < argc; i++)
			len += strlen(argv[i]) + 2;
		cmd = malloc(len + 32);
		if (!cmd) {
			perror("malloc");
			return 1;
		}
		for (*cmd = 0, i = 1; i < argc; i++) {
			strcat(cmd, argv[i]);
			strcat(cmd, " ");
		}
	}
	return client_login(argv[0], cmd);
}
Example #3
0
static void fiber_reader(user_client* client)
{
    acl::socket_stream& conn = client->get_stream();
    conn.set_rw_timeout(0);

    if (client_login(client) == false)
    {
        client_logout(client);
        return;
    }

    printf("fiber-%d: user: %s login OK\r\n", acl_fiber_self(),
           client->get_name());

    acl::string buf;

    int max_loop = client->get_max_loop(), i;

    for (i = 0; i < max_loop; ++i)
    {
        if (conn.gets(buf))
        {
            if (i <= 1)
                printf("fiber-%d: gets->%s\r\n",
                       acl_fiber_self(), buf.c_str());
            __total_read++;
            continue;
        }

        printf("%s(%d): user: %s, gets error %s, fiber: %d\r\n",
               __FUNCTION__, __LINE__, client->get_name(),
               acl::last_serror(), acl_fiber_self());

        break;
    }

    printf(">>%s(%d), user: %s, logout, loop: %d\r\n",
           __FUNCTION__, __LINE__, client->get_name(), i);

    client_logout(client);
}
Example #4
0
/**
 * @brief 登陆按键的回调函数
 *
 * @param button
 * @param data
 */
void login_button_clicked (GtkWidget *button, gpointer data)
{
    const char *password_text = gtk_entry_get_text(GTK_ENTRY(login_entry.password_entry));
    const char *username_text = gtk_entry_get_text(GTK_ENTRY(login_entry.username_entry));
 
    cli = (ChatClient *) malloc(sizeof(ChatClient));   //客户端分配资源
    if (cli == NULL)
    {
        printf("[%s]: failed to malloc\n", username_text);
    }
    bzero(cli, sizeof(ChatClient));
    cli->name = strdup(username_text);
    cli->password = strdup(password_text);
 
    cli->sktfd = socket(AF_INET, SOCK_STREAM, 0);             //建立socket
    if (cli->sktfd < 0)
    {
        printf("Client socket error\n");
    }
    cli->sktaddr.sin_family = AF_INET;
    cli->sktaddr.sin_port = htons(serverport);
    inet_pton(AF_INET, serverip, &(cli->sktaddr.sin_addr));   //点分十进制转换成整数
    connect(cli->sktfd, (struct sockaddr *) &(cli->sktaddr), sizeof(cli->sktaddr));

    if (client_login(cli) < 0)
    {
        printf("failed to login\n");
    }
 
    printf("> ");
    fflush(stdout);
    cli->epfd = epoll_create(MAXEVENTS);                      //创建监听文件集合
    fd_add_events(cli->epfd, fileno(stdin), EPOLLIN);         //监听标准输入
    fd_add_events(cli->epfd, cli->sktfd, EPOLLIN);            //监听socket
 
    g_thread_new("client_main", (GThreadFunc)client_main,  cli);
}
Example #5
0
/*
 * This is called to handle a brand new connection, in it's own thread.
 * Nothing is know about the type of the connection.
 * Assert Class: 3
 */
void *handle_connection(void *arg)
{
	connection_t *con = (connection_t *)arg;
	char line[BUFSIZE] = "";
	int res;

	thread_init(); 

	if (!con) {
		write_log(LOG_DEFAULT, "handle_connection: got NULL connection");
		thread_exit(0);
	}

	if (info.reverse_lookups)
		con->hostname = reverse(con->host);

	sock_set_blocking(con->sock, SOCK_BLOCK);
	
	/* Fill line[] with the user header, ends with \n\n */
	if ((res = sock_read_lines(con->sock, line, BUFSIZE)) <= 0) {
		write_log(LOG_DEFAULT, "Socket error on connection %d", con->id);
		kick_not_connected(con, "Socket error");
		thread_exit(0);
	}

	if (ice_strncmp(line, "GET", 3) == 0) {
		client_login(con, line);
	} else if (ice_strncmp(line, "SOURCE", 6) == 0) {
		source_login (con, line);
	} else {
		write_400 (con);
		kick_not_connected(con, "Invalid header");
	}

	thread_exit(0);
	return NULL;
}
Example #6
0
int client_main(int argc, char **argv) {
	if (argc < 4) {
		fprintf(stderr, "Usage: wetalk -c <ip> <uid> <password>\n"
						"Or   : wetalk -r <ip> <username> <password>\n");
		return wetalk_usage("wetalk");
	}

	int uid = 0;
	char *ipaddr = argv[1];
	char *password = argv[3];

	if (strlen(password) >= PASSWORD_MAX) {
		fprintf(stderr, "错误的密码长度\n");
		return 1;
	}

	if (!client_init(argv[1], on_msg_recv)) {
		wetalk_error("初始化客户端失败\n");
	}

	if (!strcmp(argv[0], "-r")) {
		uid = client_register(argv[2], password);
		if (uid > 0) {
			fprintf(stderr, "注册成功,您的 uid 为: %d\n这是以后登陆时必要的信息,请牢记\n回车键以继续", uid);
			getchar();
		} else {
			fprintf(stderr, "注册失败\n");
			return 1;
		}
	} else {
		uid = atoi(argv[2]);
		if (uid <= 0) {
			fprintf(stderr, "错误的uid\n");
			return 1;
		}
	}

	if (!client_login(uid, password)) {
		wetalk_error("登陆失败\n");
	}

	client_main_run = true;
	int r;

	setlocale(LC_ALL, "");
	initscr();
	getmaxyx(stdscr, window_y, window_x);

	char input[BUFFER_SIZE] = {0};
	while(client_main_run) {
		draw_editarea();

		getnstr(input, BUFFER_SIZE - 1);
		r = strlen(input);
		input[r] = '\0';

		if (r > 0) {
			client_send(input);
		}
	}

	endwin();
	return 0;
}
Example #7
0
int main(int argc, char *argv[])
{
    char hostname[BABBLE_BUFFER_SIZE]="127.0.0.1";
    int portno = BABBLE_PORT;

    int opt;
    int nb_args=1;

    char id_str[BABBLE_BUFFER_SIZE];
    bzero(id_str, BABBLE_BUFFER_SIZE);

    
    /* parsing command options */
    while ((opt = getopt (argc, argv, "+hm:p:i:")) != -1){
        switch (opt){
        case 'm':
            strncpy(hostname,optarg,BABBLE_BUFFER_SIZE);
            nb_args+=2;
            break;
        case 'p':
            portno = atoi(optarg);
            nb_args+=2;
            break;
        case 'i':
            strncpy(id_str,optarg,BABBLE_BUFFER_SIZE);
            nb_args+=2;
            break;
        case 'h':
        case '?':
        default:
            display_help(argv[0]);
            return -1;
        }
    }

    if(nb_args != argc){
        display_help(argv[0]);
        return -1;
    }

    if(strlen(id_str)==0){
        printf("Error: client identifier has to be specified with option -i\n");
        return -1;
    }
    else{
        printf("starting new client with id %s\n",id_str);
    }

    /* connecting to the server */
    printf("Babble client connects to %s:%d\n", hostname, portno);
    
    int sockfd = connect_to_server(hostname, portno);

    if(sockfd == -1){
        fprintf(stderr,"ERROR: failed to connect to server\n");
        return -1;
    }
    
    
    unsigned long key = client_login(sockfd, id_str);
    
    if(key == 0){
        fprintf(stderr,"ERROR: login ack\n");
        close(sockfd);
        return -1;
    }

    printf("Client registered with key %lu\n", key);
    
    client_console(sockfd);
    
    close(sockfd);
    
    return 0;
}
Example #8
0
static void fiber_reader(user_client* client)
{
	acl::socket_stream& conn = client->get_stream();
	conn.set_rw_timeout(5);

	if (client_login(client) == false)
	{
		client_logout(client);
		return;
	}

	printf("fiber-%d: user: %s login OK\r\n", acl_fiber_self(),
		client->get_name());

	acl::string buf;

	const char* to = client->get_to();
	acl::string msg;
	int max_loop = client->get_max_loop(), i = 0, n = 0;

	for (;;)
	{
		msg.format("chat|%s|hello world\r\n", to);
		if (conn.write(msg) != (int) msg.size())
		{
			printf("fiber-%d: msg(%s) write error %s\r\n",
				acl_fiber_self(), msg.c_str(),
				acl::last_serror());
		}

		if (conn.gets(buf))
		{
			if (++i <= 1)
				printf("fiber-%d: gets->%s\r\n",
					acl_fiber_self(), buf.c_str());
			n++;
			__total_read++;
			if (n == max_loop)
				break;
			continue;
		}

		printf("%s(%d): user: %s, gets error %s, fiber: %d\r\n",
			__FUNCTION__, __LINE__, client->get_name(),
			acl::last_serror(), acl_fiber_self());

		if (client->existing())
		{
			printf("----existing now----\r\n");
			break;
		}

		if (errno == ETIMEDOUT)
			printf("ETIMEDOUT\r\n");
		else if (errno == EAGAIN)
			printf("EAGAIN\r\n");
		else {
			printf("gets error: %d, %s\r\n",
				errno, acl::last_serror());
			break;
		}
	}

	printf(">>%s(%d), user: %s, logout, loop: %d, ngets: %d\r\n",
		__FUNCTION__, __LINE__, client->get_name(), i, n);

	client_logout(client);
}
Example #9
0
/**
 * Determines the command to be executed by parsing the user input
 * @param String entered by the user
 */
void comm_process(char *comm_line){

    char *comm_args[257];                    //Stores tokenized arguments of the input command
    char temp[999];                         //Create a copy of entered command line value
    int i=0,j;                                //Counter
    unsigned int arguments=0;               //Stores no of command line arguments


    //Set all elements of temp array to '\0'
    memset((char *)&temp,'\0',sizeof(temp));

    //Set all elements of char array to '\0'
    memset((char *)&comm_args,'\0',sizeof(comm_args));

    //Copy input string
    strcpy(temp,comm_line);

    //Extract command from the user entered string
    comm_args[0]=strtok(comm_line," ");

    //printf("\nDEBUG: comm_args[0] is : %s",comm_args[0]);

    //Extract arguments
    while(comm_args[i]){

        comm_args[++i]=strtok(NULL," ");

        //printf("\nDEBUG: comm_args[%D] : %s",i,comm_args[i]);

        if(i==5 || comm_args[i]==NULL)
        {
            arguments=i-1;
            break;
        }
        //vprintf("\nDEBUG: comm_args[%d] is :%s",i,comm_args[i]);
    }

	comm_line=temp;

    //Common commands
    if (strncmp("AUTHOR",comm_args[0],sizeof("AUTHOR")-1)==0 && arguments==0)
    {

        success(comm_args[0]);
        cse4589_print_and_log("I, alizisha, have read and understood the course academic integrity policy.\n");
        end_(comm_args[0]);

    }
    else if( strncmp("PORT",comm_args[0],sizeof("PORT")-1)==0 && arguments==0 && isLoggedIn==TRUE)
    {

        if(my_port>=0 && my_port<=65536){
            success(comm_args[0]);
            cse4589_print_and_log("PORT:%d\n", my_port);
        }else{
            error_(comm_args[0]);
        }

        end_(comm_args[0]);
    }
    else if( strncmp("IP",comm_args[0],sizeof("IP")-1)==0 && arguments==0 && isLoggedIn==TRUE)
    {

        if(getPublicIP()>=0){
           success(comm_args[0]);
           cse4589_print_and_log("IP:%s\n", my_ip);
        }else{
            error_(comm_args[0]);
        }
        end_(comm_args[0]);
    }
    else if( strncmp("LIST",comm_args[0],sizeof("LIST")-1)==0 && arguments==0 && isLoggedIn==TRUE)
    {
        char *ip_addr=(char *)calloc(INET_ADDRSTRLEN+1,sizeof(char));
        char *host_name=(char *)calloc(50,sizeof(char));

        int list_id;

        if(mode==CLIENT)
        {
            success(comm_args[0]);
            for(i=0,list_id=1;i<=3;i++)
            {
                if(clients[i].port!=NOT_CONNECTED)
                {
                    //printf("\nDEBUG: 1");
                    memset(host_name,'\0',50);
                    memset(ip_addr,'\0',INET_ADDRSTRLEN+1);
                    //printf("\nDEBUG: 2");
                    //printf("\nIP before ntop is: %d",clients[i].ip);
                    inet_ntop(AF_INET,&(clients[i].ip),ip_addr,INET_ADDRSTRLEN);
                    //printf("\nDEBUG: 3");
                    get_name(host_name,clients[i].ip);
                    //printf("\nDEBUG: 4");
                    cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", list_id++, host_name, ip_addr, clients[i].port);
                    //printf("\nDEBUG: 5");
                }
            }
        }else if(mode==SERVER)
        {
            success(comm_args[0]);
            //memset(host_name,'\0',50);
            //memset(ip_addr,'\0',INET_ADDRSTRLEN+1);

            /*

            for(i=0,j=1;i<=3;i++,j++)
                printf("\n%d\tIP: %d\tPort: %d",j,client_list[i].nodeinfo.ip,client_list[i].nodeinfo.port);

            fflush(stdout);*/

            for(i=0,list_id=1;i<=3;i++)
            {
                if(client_list[i].isOnline==TRUE)
                {
                    memset(host_name,'\0',50);
                    memset(ip_addr,'\0',INET_ADDRSTRLEN+1);
                    //printf("\nDEBUG: 2");
                    inet_ntop(AF_INET,&(client_list[i].nodeinfo.ip),ip_addr,INET_ADDRSTRLEN);
                    //printf("\nDEBUG: 3");
                    get_name(host_name,client_list[i].nodeinfo.ip);
                    //printf("\nDEBUG: 4");
                    cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", list_id++, host_name, ip_addr, client_list[i].nodeinfo.port);
                }
            }

            /*

            printf("\nAfter listing!");
            for(i=0,j=1;i<=3;i++,j++)
                printf("\n%d\tIP: %d\tPort: %d",j,client_list[i].nodeinfo.ip,client_list[i].nodeinfo.port);

            fflush(stdout);
            */
        }else
        {
            error_(comm_args[0]);
        }

        free(ip_addr);
        free(host_name);

        end_(comm_args[0]);

    }
    else if( strncmp("EXIT",comm_args[0],sizeof("EXIT")-1)==0 && arguments==0)
    {
        //Insert method to logout

        success(comm_args[0]);
        end_(comm_args[0]);

        exit(0);
    }
    //Server Commands
    else if( strncmp("STATISTICS",comm_args[0],sizeof("STATISTICS")-1)==0 && arguments==0 && mode==SERVER)
    {
        success(comm_args[0]);
        serv_stats();
        end_(comm_args[0]);

    }
    else if( strncmp("BLOCKED",comm_args[0],sizeof("BLOCKED")-1)==0 && arguments==1 && mode==SERVER)
    {
        command_blocked(comm_args[1]);
        end_(comm_args[0]);
    }
    //Client Commands
    else if( strncmp("BLOCK",comm_args[0],sizeof("BLOCK")-1)==0 && arguments==1 && mode==CLIENT && isLoggedIn==TRUE
        && strncmp("BLOCKED",comm_args[0],sizeof("BLOCKED")-1)!=0)
    {
        //printf("\nDEBUG: Extracted IP is : %s",comm_args[1]);
        //fflush(stdout);

        if(client_block(comm_args[1])==TRUE)
        {
            success(comm_args[0]);
        }else
        {
            error_(comm_args[0]);
        }
        end_(comm_args[0]);
    }
    else if( strncmp("LOGIN",comm_args[0],sizeof("LOGIN")-1)==0 && arguments==2 && mode==CLIENT &&isLoggedIn==FALSE )
    {
        if(client_login(comm_args[1],atoi(comm_args[2]))==TRUE)
        {
            success(comm_args[0]);
            isLoggedIn=TRUE;
            isLoggedOut=FALSE;
            FD_SET(client_sock, &master);                   //Add client_socket to client's master FD list
            //FD_SET(client_sock, &read_fds);
            if(client_sock>fdmax)
                fdmax=client_sock;
            //printf("\nDebug: Client_socket successfully added to list!");
            //fflush(stdout);
        }else
        {
            error_(comm_args[0]);
        }

        end_(comm_args[0]);

    }
    else if( strncmp("REFRESH",comm_args[0],sizeof("REFRESH")-1)==0 && arguments==0 && mode==CLIENT && isLoggedIn==TRUE)
    {
        int i,list_id;
        char *ip_addr=(char *)calloc(INET_ADDRSTRLEN+1,sizeof(char));
        char *host_name=(char *)calloc(50,sizeof(char));

        if(client_refresh()==TRUE)
        {
            success(comm_args[0]);
            for(i=0,list_id=1;i<=3;i++)
            {
                if(clients[i].port!=NOT_CONNECTED)
                {
                    //printf("\nDEBUG: 1");
                    memset(host_name,'\0',50);
                    memset(ip_addr,'\0',INET_ADDRSTRLEN+1);
                    //printf("\nDEBUG: 2");
                    //printf("\nIP before ntop is: %d",clients[i].ip);
                    inet_ntop(AF_INET,&(clients[i].ip),ip_addr,INET_ADDRSTRLEN);
                    //printf("\nDEBUG: 3");
                    get_name(host_name,clients[i].ip);
                    //printf("\nDEBUG: 4");
                    cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", list_id++, host_name, ip_addr, clients[i].port);
                    //printf("\nDEBUG: 5");
                }
            }

        }
        else{
            error_(comm_args[0]);
        }

        free(ip_addr);
        free(host_name);

        end_(comm_args[0]);

    }
    else if( strncmp("SEND",comm_args[0],sizeof("SEND")-1)==0 && isLoggedIn==TRUE&& mode==CLIENT && arguments>=2)
    {
        char *ip,*msg;//comm_args[1];
        strtok(comm_line," ");
        ip=strtok(NULL," ");
        msg=strtok(NULL,"\n");

        if(send_message_client(ip,msg)==TRUE)
        {
            success(comm_args[0]);
        }else
        {
            error_(comm_args[0]);
        }
        end_(comm_args[0]);
    }
    else if( strncmp("BROADCAST",comm_args[0],sizeof("BROADCAST")-1)==0  && isLoggedIn==TRUE && mode==CLIENT &&arguments>=1)
    {
        char *msg;

        /* Extract message from the entire command */
        strtok(comm_line," ");                          //Separate command string
        msg=strtok(NULL,"\n");

        if(send_broadcast(msg)==TRUE)
            success(comm_args[0]);
        else
            error_(comm_args[0]);

        end_(comm_args[0]);

    }
    else if( strncmp("UNBLOCK",comm_args[0],sizeof("UNBLOCK")-1)==0 && arguments==1 && mode==CLIENT && isLoggedIn==TRUE)
    {
        if(client_unblock(comm_args[1])==TRUE)
        {
            success(comm_args[0]);
        }else{
            error_(comm_args[0]);
        }

        end_(comm_args[0]);
    }
    else if( strncmp("LOGOUT",comm_args[0],sizeof("LOGOUT")-1)==0 && arguments==0 && mode==CLIENT && isLoggedIn==TRUE)
    {
        if(client_logout()==TRUE)
        {
            success(comm_args[0]);
            isLoggedIn=FALSE;
            isLoggedOut=TRUE;
            //Stop listening on client_sock
            FD_CLR(client_sock, &master);

        }else
        {
            error_(comm_args[0]);
        }

        end_(comm_args[0]);
    }
    else if( strncmp("SENDFILE",comm_args[0],sizeof("SENDFILE")-1)==0 && arguments==2 && mode==CLIENT && isLoggedIn==TRUE){
    }else{
    }
}
Example #10
0
static void fiber_reader(user_client* client)
{
    acl::socket_stream& conn = client->get_stream();
    conn.set_rw_timeout(0);

    client->set_reader();
    client->set_reading(true);

    // 登入服务器
    if (client_login(client) == false)
    {
        client->set_reading(false);
        printf("----------client_logout-------\r\n");

        // 失败,则退出客户端
        client_logout(client);

        printf("----__nreader: %d-----\r\n", --__nreader);
        return;
    }

    // 登入成功,则创建写协程用来向客户端发送消息
    go_stack(STACK_SIZE) [&] {
        __nwriter++;
        fiber_writer(client);
    };

    conn.set_rw_timeout(0);

    bool stop = false;
    acl::string buf;

    // 从客户端循环读取消息
    while (true)
    {
        bool ret = conn.gets(buf);
        if (ret == false)
        {
            printf("%s(%d): user: %s, gets error %s, fiber: %d\r\n",
                   __FUNCTION__, __LINE__, client->get_name(),
                   acl::last_serror(), acl_fiber_self());

            // 客户端退出
            if (client->exiting())
            {
                printf("----exiting now----\r\n");
                break;
            }

            if (errno == ETIMEDOUT)
            {
                if (conn.write("ping\r\n") == -1)
                {
                    printf("ping error\r\n");
                    break;
                }
            }
            else if (errno == EAGAIN)
                printf("EAGAIN\r\n");
            else {
                printf("gets error: %d, %s\r\n",
                       errno, acl::last_serror());
                break;
            }

            continue;
        }

        if (buf.empty())
            continue;

        // 分析客户端发送的消息,交由不同的处理过程
        std::vector<acl::string>& tokens = buf.split2("|");

        // 本客户端要求退出
        if (tokens[0] == "quit" || tokens[0] == "exit")
        {
            conn.write("Bye!\r\n");
            break;
        }

        // 本客户端发送聊天消息
        else if (tokens[0] == "chat")
        {
            if (client_chat(client, tokens) == false)
                break;
        }

        // 本客户端踢出其它客户端
        else if (tokens[0] == "kick")
        {
            if (client_kick(client, tokens) == false)
                break;
        }

        // 要求整个服务进程退出
        else if (tokens[0] == "stop")
        {
            stop = true;
            break;
        }
        else
            printf("invalid data: %s, cmd: [%s]\r\n",
                   buf.c_str(), tokens[0].c_str());
    }

    printf(">>%s(%d), user: %s, logout\r\n", __FUNCTION__, __LINE__,
           client->get_name());

    client->set_reading(false);

    // 退出客户端
    client_logout(client);

    printf("----__nreader: %d-----\r\n", --__nreader);

    if (stop)
    {
        int dumy = 1;
        // 如果要停止服务,则通知监控协程
        __chan_monitor.put(dumy);
    }
}