Exemplo n.º 1
0
int CommunicationServer::process_dispatcher(void) {
	// 
	int reuse_addr = 1, reuse_port = 1;
	// 
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if(sock < 0) {
		perror("socket");
		exit(-1);
	}
	// 
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
	setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &reuse_port, sizeof(reuse_port));
	setnonblocking(sock);
	// 
	struct sockaddr_in server_address;
	memset((char *) &server_address, 0, sizeof(server_address));
	server_address.sin_family			= AF_INET;
	server_address.sin_addr.s_addr	= htonl(INADDR_ANY);
	server_address.sin_port				= 6669;
	//
	if(bind(sock, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
		perror("bind");
		close(sock);
		exit(-1);
	}
	//
	listen(sock, 1024);
	printf("process_dispatcher() up and running.\n\r");
	int highsock = sock;
	//
	memset((char *) &connectlist, 0, sizeof(connectlist));
	// 
	struct timeval timeout;
	int readsocks;
	while(1) {
		handle_new_connection();
		// 
		build_select_list();
		// 
		timeout.tv_sec = 0;
		timeout.tv_usec = 32;
		// 
		readsocks = select(highsock + 1, &socks, (fd_set *) 0, (fd_set *) 0, &timeout);
		// 
		if(readsocks < 0) {
			perror("select");
			continue;
		}
		if(readsocks == 0) {
			//printf("process_dispatcher() ticking ..\n\r");
			// write
			//handle_new_connection();
		} else {
			printf("select returned > 0\n\r");
			read_socks();
		}
	}
	return 0;
}
Exemplo n.º 2
0
int acceptForClient()
{
  int readsocks=0;
  struct timeval timeout;  
  while (1) { /* Main server loop - for accept connect*/
		build_select_list();
		timeout.tv_sec=0;
		timeout.tv_usec = 0;
		readsocks = select(highsock+1, &socks, (fd_set *) 0, 
		  (fd_set *) 0,&timeout);
	
		if (readsocks < 0) {
			perror("select");
			exit(EXIT_FAILURE);
		}
		if (readsocks == 0) {
			/* Nothing ready to read, just show that
			   we're alive */
			//printf(".");
		
			fflush(stdout);
		} else{
			printf("read_socks .....\n");
			//system("clear");
			if(checkConnectionLost_connectList()>0)
			  read_socks();//for accept
			else
			  {
			    printf("gd2\n");
			    read_socks();//for relogin
			    printf("indexMax=%d\n",indexMax);
			    if(indexMax==BACKLOG && 0==checkConnectionLost())
			      break;
			  }
		}
	} /* while(1) */
  printf("Yeah, already to go.\n");
  //write_socks("START");
  return 0;
}
Exemplo n.º 3
0
/* Dispatch client read state, following socks5 RFC
 * In each state, it deal with read in buf with soc,
 * test the packet with right function, build a reponse and
 * change state to next.
 *
 * Return:
 * 	-1, error something happen we need to disconnect the client
 * 	 0, success
 */
int dispatch_client_read(s_socket *soc, s_socket *soc_stream,
		s_socks *socks, s_buffer *buf, s_buffer *buf_stream, s_socks_conf *conf)
{
	int k = 0;
	switch(socks->state){
		case S_R_VER_ACK:
			READ_DISP(k, soc, buf, sizeof(Socks5VersionACK));

			k = test_version_ack(socks, conf, buf);
			if (k < 0){ close_socket(soc); break; } /* Error */

			/* It means need to send a authentication packet */
			if ( socks->method == 0x02 ){
				if ( build_auth(socks, conf, buf) < 0 ){
					/* Something goes wrong, maybe no uname/passwd set in conf */
					k = -1;
					break;
				}
				socks->state = S_W_AUTH;
			}else if ( socks->mode == M_DYNAMIC_CLIENT ){
				/* In dynamic mode we do negociation with socks server and reply */
				socks->state = S_REPLY;
				break;
			}else{
				build_request(socks, conf, buf);
				socks->state = S_W_REQ;
			}
			break;

		case S_R_AUTH_ACK:
			READ_DISP(k, soc, buf, sizeof(Socks5AuthACK));

			k = test_auth_ack(socks, conf, buf);
			if (k < 0){ break; } /* Error */

			if ( socks->mode == M_DYNAMIC_CLIENT ){
				/* In dynamic mode we do negociation with socks server and reply */
				socks->state = S_REPLY;
				break;
			}

			build_request(socks, conf, buf);
			socks->state = S_W_REQ;
			break;

		case S_R_REQ_ACK:
			READ_DISP(k, soc, buf, sizeof(Socks5ReqACK));

			k = test_request_ack(socks, conf, buf);
			if (k < 0){ close_socket(soc); break; } /* Error */

			if ( conf->config.cli->cmd == CMD_BIND ){
				if ( socks->connected == 0 && socks->listen == 1)
					socks->state = S_R_REQ_ACK;
				else{
					socks->state = S_REPLY;
					/* End, stop client loop */
					conf->config.cli->loop = 0;
				}
			}else{
				socks->state = S_REPLY;
				/* End, stop client loop */
				conf->config.cli->loop = 0;
			}

			/* Clean buffer before enter in reply mode */
			init_buffer(buf);
			break;

		case S_REPLY:
			if ( buf_free(buf_stream) > 0 ){
				k = read_socks(soc, buf_stream, 0);
				if (k < 0){ close_socket(soc); break; } /* Error */
			}
			break;

		default:
			break;
	}
	return k;
}