Ejemplo n.º 1
0
void dino_process_request(dino_http_site_t *dino_site, int socket) {
    dino_handle_t dhandle;
    init_request(&dhandle);

    if (-1 == socket) {
        log_error("accept", __FUNCTION__, __LINE__);
    }
    else {
        dhandle.http.socket = socket;
        accept_request(dino_site, &dhandle);
    }

    // Clear the cache memory...
    // This assumes that there is no memory allocations
    // that will be presisted across calls.
    //
    memory_cache_clear();

    // Free DHANDLE
    //
    free_request(&dhandle);

    // Close Socket
    //
    close(socket);
}
Ejemplo n.º 2
0
int main(void)
{
    int server_sock = -1;
    u_short port = 0;
    int client_sock = -1;
//sockaddr_in 是 IPV4的套接字地址结构。定义在<netinet/in.h>,参读《TLPI》P1202
    struct sockaddr_in client_name;
    int client_name_len = sizeof(client_name);
//pthread_t newthread;

    server_sock = startup(&port);
    printf("httpd running on port %d\n", port);

    while (1)
    {
        //阻塞等待客户端的连接,参读《TLPI》P1157
        client_sock = accept(server_sock,
                             (struct sockaddr *)&client_name,
                             &client_name_len);
        if (client_sock == -1)
            error_die("accept");
        accept_request(client_sock);
        /*if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0)
          perror("pthread_create");*/
    }

    close(server_sock);

    return(0);
}
Ejemplo n.º 3
0
int main(int argc, char *argv[]){
	int listenfd = socket_initialization();
	if(listenfd == -1){
		return -1;
	}

	while(1){
		int connfd;
		struct sockaddr_in clientaddr;
		int clientaddr_size = sizeof(clientaddr);
		memset(&clientaddr, 0, sizeof(clientaddr));

		connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientaddr_size);
		if(connfd == -1){
			continue;
		}
		char ip_printable[INET_ADDRSTRLEN];
		printf("Receive connection from %s:%d\n", inet_ntop(AF_INET, &clientaddr.sin_addr, ip_printable, INET_ADDRSTRLEN), 
													ntohs(clientaddr.sin_port));
		if(fork() == 0){
			close(listenfd);
			struct connection_s conn;
			accept_request(connfd);
			return 0;
		}
		close(connfd);
	}
	close(listenfd);

	return 0;
}
Ejemplo n.º 4
0
int main(void)
{
 int server_sock = -1;
 u_short port = 0;
 int client_sock = -1;
 struct sockaddr_in client_name;
 int client_name_len = sizeof(client_name);
 //pthread_t newthread;

 server_sock = startup(&port);
 printf("httpd running on port %d\n", port);

 while (1)
 {
  client_sock = accept(server_sock,
                       (struct sockaddr *)&client_name,
                       &client_name_len);
  if (client_sock == -1)
   error_die("accept");
  accept_request(client_sock);
 //if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0)
   //perror("pthread_create");
 }

 close(server_sock);

 return(0);
}
Ejemplo n.º 5
0
int CM2PM_server_start(PORT_NUM serv_port)
{
    int listenfd = 0, connfd = 0;
    int action_label;

    listenfd = init_socket();
    if (listenfd < 0)
	return -1;

    if (init_server(serv_port, listenfd) < 0)
	return -1;

    while (TRUE)
    {
	connfd = accept_request(listenfd);
	if (read_data(connfd, &action_label, sizeof(action_label)) < 0)
	{
	    printf("Read Data Error\n");
	    return -1;
	}
	do_action(action_label);
	close(connfd);
    }

    //Success
    return 0;
}
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
	int sock;
	int new_sock;
	pid_t pid = 0;
	int opt = 1;

	parse_options(argc, argv);

	sock = create_sock();

	while (1) {
		new_sock = accept_request(sock);
		if (new_sock < 1) {
			break;
		}

		if ((setsockopt(new_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) < 0) {
			perror("setsockopt");
		}

		pid = client_fork();
		if (pid == 0) {
			printf("Client spawned\n");
			manage_client(new_sock, "A_PASS_HERE", "ENC_TYPE_HERE");
			close(new_sock);
		}
	}

	return 0;
}
Ejemplo n.º 7
0
void friendrequest::on_listWidget_doubleClicked(const QModelIndex &index)
{
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, "Accept Friend Request", "Accept the Friend Request from "+ui->listWidget->currentItem()->text()+" ?",
                                  QMessageBox::Yes|QMessageBox::No);
    if (reply == QMessageBox::Yes) {
        emit accept_request(ui->listWidget->currentItem()->text());
        on_pushButton_clicked();
    } else {

    }
}
Ejemplo n.º 8
0
int listenOnPort(int port)
{
	int keepalive = 1, client_fd, status;

	// sockaddr: structure to contain an internet address.
	struct sockaddr_in svr_addr, cli_addr;
	socklen_t sin_len = sizeof(cli_addr);

	// create endpoint for comm, AF_INET (IPv4 Internet Protocol), 
	// SOCK_STREAM (Provides sequenced, reliable, two-way, connection-based byte streams),
	int sock = socket(AF_INET, SOCK_STREAM, 0); // returns file descriptor for new socket
	if (sock < 0)
		fprintf(stderr, "Can't open socket\n");

	// (int socket SOL_SOCKET to set options at socket level, allows reuse of local addresses,
	// option value, size of socket)
	setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive));


	svr_addr.sin_family = AF_INET;
	svr_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
	svr_addr.sin_port = htons(port); // host to network short (htons)

	// Start server
		if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
			close(sock);
			fprintf(stderr, "Can't bind socket.\n");
			// err(1, "Can't bind");
		}
	 
		listen(sock, 5); // 5 is backlog - limits amount of connections in socket listen queue
		printf("listening on localhost with port %d\n", port);


	while (1)
	{
		client_fd = accept(sock, (struct sockaddr *)&cli_addr, &sin_len);
		printf("Got connection.\n");
			if(fork() == 0){
				/* Perform the client’s request in the child process. */
				accept_request(client_fd);
				exit(0);
			}
			close(client_fd);
			waitpid(-1, &status, WNOHANG);
	}
	close(sock);
}
Ejemplo n.º 9
0
void on_draw_game (int fd, struct message *msg)
{
	printf ("on_draw_game\n");
	msg->type = DRAW_GAME_FEEDBACK;
	if (accept_request ("对方请求和棋\n是否同意"))
	{
		sprintf (msg->buff, "%s 1", get_room_number ());
		/*  同意和棋 */
		game_draw ();
	}
	else
	{
		sprintf (msg->buff, "%s 0", get_room_number ());
	}
	send_message (msg);
}
Ejemplo n.º 10
0
int main(void)
{
 int server_sock = -1;
 u_short port = 0;
 int client_sock = -1;
 struct sockaddr_in client_name;
 int client_name_len = sizeof(client_name);
 pid_t pid;
 //pthread_t newthread;

 //服务器启动,在指定端口或随机选取端口绑定 httpd 服务。
 server_sock = startup(&port);
 printf("httpd running on port %d\n", port);

 while (1)
 {
  //accept返回的是全新的描述符,代表与所返回客户的TCP连接。
  //server_sock是监听套接字,仅一个,服务器的生命期内一直存在
  client_sock = accept(server_sock,
                       (struct sockaddr *)&client_name,
                       &client_name_len);
  if (client_sock == -1)
   error_die("accept");
 printf("Receive a request from %s:%d\n" , inet_ntoa(client_name.sin_addr) , client_name.sin_port);
 if((pid = fork()) == 0)
 {  
    //因子进程也有指向server_sock文件的表项,且它不关心服务器套接字,它只处理客户套接字,因此close它
    //下面的父进程close client_sock也是同一个道理
    close(server_sock);
    accept_request(client_sock); 
    close(client_sock);
    exit(0);
 }
 int status = 0;
 pid_t pid2 = wait(&status);
 printf("My pid is %d\n", pid2);
 close(client_sock);
 //收到一个 HTTP 请求时(其实就是 listen 的端口 accpet 的时候),派生一个线程运行 accept_request 函数。
 // if (pthread_create(&newthread , NULL, accept_request, client_sock) != 0)
 //   perror("pthread_create");
}
 return(0);
}
Ejemplo n.º 11
0
void on_regret (int fd, struct message *msg)
{
	printf ("on_regret\n");

	msg->type = REGRET_FEEDBACK;
	if (accept_request ("对方请求悔棋\n是否同意"))
	{
		sprintf (msg->buff, "%s 1", get_room_number ());
		/*  同意悔棋了,所以要把棋盘恢复到上一步的局面 */
		unset_turn ();
		restore_chessbord ();
		set_regret_flag (FALSE);
		min_step ();
	}
	else
	{
		sprintf (msg->buff, "%s 0", get_room_number ());
	}
	send_message (msg);
}
Ejemplo n.º 12
0
void main(){
	
	int listenSock, connSock;
    struct sockaddr_in serv_addr, client_addr;
    int clientAddrLenght;
    User user[100];
    char tmp[100];
    int max_user;
    char mesg[LEN];
    char mesg2[LEN];
    char buff[LEN]="11|";
    int t,n;
    int recvBytes,sentBytes;
    fd_set readfds,allset;
    int i, maxi,sockfd;
    int rv;
    struct pollfd client[30];

    serv_addr.sin_family = AF_INET;       
    serv_addr.sin_addr.s_addr = INADDR_ANY;  
    serv_addr.sin_port = htons(5556); 

    
    if((listenSock = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        printf("\nTao socket loi!!\n");
        return;
    }
    if(bind(listenSock, (struct sockaddr *) &serv_addr,sizeof(serv_addr))<0) {
        printf("\nBind loi!!\n");
        return;
        }
    if(listen(listenSock, 10) == -1) {
        printf("\nListen loi!!\n");
        return;
    }
    for ( i = 1; i < 30; i++){
        client[i].fd=-1;
    }
    client[0].fd = listenSock;
    client[0].events = POLLRDNORM;	
    max_user= take_user(user);
        
    while(1){
     	rv = poll(client, 30,3); //Number of client = 30, time out=3
    
     	if(client[0].revents & POLLRDNORM){
            clientAddrLenght=sizeof(client_addr);
            connSock = accept(listenSock, (struct sockaddr *) &client_addr, (socklen_t*)&clientAddrLenght);
            

            for(i=1;i < 30;i++){
                if(client[i].fd < 0){
                    client[i].fd = connSock;
                    break;
                }
            }//end for
            client[i].events = POLLRDNORM;
            if(--rv <= 0) //no more readable descriptor. Decrement before return
                continue;
        }//end if
        for(i=1;i<30;i++){
            if((sockfd = client[i].fd) <0)
                continue;
            if(client[i].revents & (POLLRDNORM | POLLERR)){
                strcpy(mesg,"");
                n=recv(sockfd, mesg, LEN, 0);

                printf("\nXau nhan tu Client: %s",mesg );
                
                mesg[n]='\0';
                if((n)==0){
                    close(sockfd);
                    client[i].fd = -1;
                    for ( i = 0; i < max_user; i++){
                    	if(user[i].sockfd==sockfd){
                    		user[i].status=0;
                    		break;
                    	}
                    }
                }else{
                    // printf("\nXau nhan tu Client: %s",mesg );
                    t=check_mark(mesg);
                   	if(t == 0)	max_user = sign_up(sockfd,mesg,user,max_user);
                   	if(t == 1)	{
                   		login(sockfd,mesg,user,max_user);
                   		
                   	}
                   	if(t == 2)	show_user_list(sockfd,user,max_user);
                   	if(t == 3)	play_request(sockfd,mesg,user,max_user);
                   	if(t == 4)	accept_request(sockfd,mesg,user,max_user);
                   	if(t == 5)	not_accept_request(sockfd,mesg,user,max_user);
                   	if(t == 6)	playing(sockfd,mesg,user,max_user);
                   	if(t == 7)	stop_play(sockfd,mesg,user,max_user);
                   	if(t == 8)	log_out(sockfd,user,max_user);
                }
                
            if(--rv <= 0)
                break;
            }
        }
    	
     	
    }//end while
}
Ejemplo n.º 13
0
int
main(int argc, char *argv[])
{
	
	int client_sock = -1;
	int client_name_len = sizeof (client_name);
	int request=0;
	
	struct nunetwork_headerstruct *header_buf;
	header_buf = (struct nunetwork_headerstruct*)malloc( sizeof(*header_buf) );
	/********************
         * Don't forget to make pointer to variable cast (*pointer)
         * When using sizeof(). Otherwise it may cost you a week of
	 * debug time.
         *******************/
	

	if (geteuid()) {
		printf("Netustad must be run as root.\n");
		exit(1);
	}
	
	conffile	= (char *)strdup(default_conf_file);
	
	
	/*setlocale (LC_ALL, "");*/
	bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
	textdomain ( PACKAGE );
	

	/*******************/
	/* Check Arguments */
	/*******************/
	if (argc==2) {	
			
			if (strcmp(argv[1], "-h") == 0 ||
				strcmp(argv[1], "--help") == 0) {
				printf( gettext("\nnetUstad: Network Ustadi (Network Master)\n"));
				printf( gettext("Copyright (C) 2004   by Ozkan KIRIK\n"));
				printf( gettext("Usage: netustad [options]\n"));
				printf( gettext("\n"));
				printf( "%s:\n", gettext("Options"));
				printf( gettext("\t-h\tShow this help screen\n"));
				printf( gettext("\t-v\tShow version\n"));
				printf
					(gettext("\t-c\tUse following parameter as configuration file"));
				printf("\n");
				exit(0);
			} else if (strcmp(argv[1], "-v") == 0 ||
				   strcmp(argv[1], "--version") == 0) {
				printf(gettext("\nnetUstad: Network Ustadi (Network Master)\n"));
				printf(gettext("Copyright (C) 2004  by Ozkan KIRIK\n\n"));
				printf(gettext("Version: %s\n\n"), NUVERSION);
				exit(0);
			}
				
	} else if (argc == 3 && strcmp(argv[1], "-c") == 0) {
			conffile = strdup(argv[2]);

	}  else if (argc!=1) {
			if (strcmp(argv[1], "-c") == 0) {
				printf (gettext("\nnetUstad: Invalid Number Of Arguments\n\n"));
			} else {
				printf(gettext("\nnetUstad: Invalid Argument\n\n"));
			}
			exit(1);

	}


	/**********************/
	/* Start Main Program */
	/**********************/

	readconfig(conffile);
	if (setlocale (LC_ALL, (const char*) lc_all) == NULL) {
		log_msg(mainlogfile, gettext("setlocale failed\n"), 1);
		printf(gettext("setlocale failed\n"));
	}
	bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
	textdomain ( PACKAGE );
	nameoftty = ttyname(0);
	daemonize();

	#ifndef WITHOUT_SSL
        	ssl = nunetwork_init_ssl( nunetwork_init_ctx( cert_file, key_file) );
	#endif

	server_sock = startup(&port);	/* Open Socket & Listen */

	log_msg(mainlogfile, "<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>\n", 0);
	snprintf(log_msg_text, sizeof(log_msg_text)-1, "netUstad-%s\n", NUVERSION);
        log_msg(mainlogfile, log_msg_text, 0);
	snprintf(log_msg_text, sizeof (log_msg_text)-1, "%s",
		 gettext("netUstad is started\n"));
	log_msg(mainlogfile, log_msg_text, 1);
	snprintf(log_msg_text, sizeof (log_msg_text)-1, gettext("\nListening port %d\n"), port);
	log_msg(mainlogfile, log_msg_text, 0);
	log_msg(mainlogfile, gettext("Ready for requests...\n\n"), 0);
	snprintf(log_msg_text, sizeof(log_msg_text)-1, "netUstad-%s\n", NUVERSION);
	log_msg(nameoftty, log_msg_text, 0);
	snprintf(log_msg_text, sizeof (log_msg_text)-1,
		 gettext("\nnetUstad is started\nListening port %d\n"), port);
	log_msg(nameoftty, log_msg_text, 0);

	while (1) {
		client_sock = accept(server_sock,
				     (struct sockaddr *) &client_name,
				     (socklen_t *)&client_name_len);

		if (client_sock == -1)
			continue;
	
		 #ifndef WITHOUT_SSL
		        SSL_set_fd(ssl, client_sock);
		        sslerror = SSL_accept(ssl);
		
		        if ( sslerror <= 0 ) {
		                sslerror= SSL_get_error(ssl, sslerror);
		                ERR_error_string(sslerror, log_msg_text);
		
		                log_msg(mainlogfile, log_msg_text, 1);
				log_msg(mainlogfile, "\n",0);
				SSL_shutdown(ssl);
				SSL_free(ssl);
				close(client_sock);
				ssl = nunetwork_init_ssl(ctx);
				continue;
		        }
			
			request =  nunetwork_getheaders(ssl, header_buf, nu_acceptedheaders); 
			if (request > 0) 
				accept_request(ssl, header_buf);
			else if (request==-2 || request==0)
				bad_request(ssl);
			nunetwork_close(ssl);
		 #else
			request = nunetwork_getheaders(client_sock, header_buf, nu_acceptedheaders);
		        if (request > 0)
				accept_request(client_sock, header_buf);
			else if (request==-2 || request==0 )
				bad_request(ssl);
		 	nunetwork_close(client_sock);
		 #endif
		
	
	
	}
	#ifndef WITHOUT_SSL
		SSL_shutdown(ssl);
		SSL_free(ssl);
		SSL_CTX_free(ctx);
	#else
		close(server_sock);
	#endif


	return (0);
}
Ejemplo n.º 14
0
int main(int argc,char* argv[])
{
	if(argc!=3)
	{
		usage(argv[0]);
		return 1;
	}
	char* ip=argv[1];
	int port=atoi(argv[2]);
	int listen_sock=startup(ip,port);
	struct sockaddr_in client;
	socklen_t len=sizeof(client);
	int epo_fd=epoll_create(256);
	if(epo_fd<0)//success:not 0 fd/error:-1
	{
		printLog(strerror(errno),__FUNCTION__,__LINE__);
		return -1;
	}
	bf_p fd_bf=(bf_p)malloc(sizeof(bf_t));
	memset(fd_bf->_buf,'\0',sizeof(fd_bf->_buf));
	fd_bf->_fd=listen_sock;
	struct epoll_event ev;
	ev.events=EPOLLIN;
	ev.data.fd=listen_sock;
	if(epoll_ctl(epo_fd,EPOLL_CTL_ADD,listen_sock,&ev)<0)//success:0 fail:-1
	{
		printLog(strerror(errno),__FUNCTION__,__LINE__);
		return -1;
	}
	struct epoll_event evfds[_SIZE_];//_SIZE_ 1024
	int _timeout=5000;
	int ret=-1;
	
	int i=0;
	while(1)
	{
		switch((ret=epoll_wait(epo_fd,evfds,_SIZE_,_timeout)))
		{
			case -1://error
				printLog(strerror(errno),__FUNCTION__,__LINE__);
				break;
			case 0://time out
				printf("time out...\n");
				break;
			default://normal
				{
					for(i=0;i<ret;++i)
					{
						if(evfds[i].data.fd==listen_sock&&evfds[i].events&EPOLLIN)
						{
							int new_sock=accept(listen_sock,(struct sockaddr*)&client,&len);
							if(new_sock<0)
							{
								printLog(strerror(errno),__FUNCTION__,__LINE__);
								continue;
							}
							bf_p _bf=(bf_p)malloc(sizeof( bf_t));
							memset(_bf->_buf,'\0',sizeof(_bf->_buf));
							_bf->_fd=new_sock;

							ev.events=EPOLLIN;
							ev.data.ptr=_bf;
							epoll_ctl(epo_fd,EPOLL_CTL_ADD,new_sock,&ev);
						}
						else if(((bf_p)(evfds[i].data.ptr))->_fd>0&&evfds[i].events&EPOLLIN)
						{
							accept_request(evfds[i].data.ptr);
							ev.events=EPOLLOUT;
							ev.data.ptr=evfds[i].data.ptr;
							epoll_ctl(epo_fd,EPOLL_CTL_MOD,((bf_p)(evfds[i].data.ptr))->_fd,&ev);
						}
						else if(((bf_p)(evfds[i].data.ptr))->_fd>0&&evfds[i].events&EPOLLOUT)
						{
							bf_p _bf=(bf_p)evfds[i].data.ptr;
							if(_bf->_err)
							{
								send(_bf->_fd,_bf->_buf,strlen(_bf->_buf),0);
							}
							else if(_bf->_cgi)//cgi=1
							{
								send(_bf->_fd,_bf->_buf,strlen(_bf->_buf),0);
							}
							else
							{
								char* path=_bf->_path;
								int fd=open(path,O_RDONLY);
								if(fd<0)
								{
									printLog(strerror(errno),__FUNCTION__,__LINE__);
									exit(1);
								}
								send(_bf->_fd,_bf->_buf,strlen(_bf->_buf),0);
								if(sendfile(_bf->_fd,fd,NULL,_bf->_st_size)<0)
								{
									printf("error");
								}
								close(fd);
							}
		
							epoll_ctl(epo_fd,EPOLL_CTL_DEL,_bf->_fd,NULL);
							close(_bf->_fd);
							free(_bf);
						}
					}

					break;
				}
		}
	}
	return 0;
}