Beispiel #1
0
void * handle_client_request(void * workerId)
{
	int id = *((int *) workerId); 

	char * request = NULL;
	int clientSocket, parseSuccess;
	struct http_request_s * request_obj;
	struct http_response_s * response_obj;
	char *response = NULL;
	printf("Hullo in %d\n", id);
	while(1)
	{
		clientSocket = -1;
		printf("Go to loop in %d\n", id);
		pthread_mutex_lock(&queue_mutex);
		{
			if(is_empty_queue(clientQueue))
			{
				pthread_cond_wait(&queue_has_client, &queue_mutex);
			}
			else 
				/* Take 1 client out of the queue */
				dequeue(clientQueue, &clientSocket);
		}
		pthread_mutex_unlock(&queue_mutex);
		printf("Worker with id = %d handles socketId = %d\n", id, clientSocket);
		if(clientSocket >= 0)
		{
			/* Initalize for new request handling */
			request_obj = create_request_struct();
			response_obj = create_response_struct();
			response = NULL;
			
			/* Handle the request of the client */
			request = read_request(clientSocket);
/*			ssize_t numByte = recv(clientSocket, m_request, 1000, 0);
			m_request[numByte] = '\0'; */
			printf("OK reading request\n"); 
			/* Parse request */
			parseSuccess = parse_http_request(request_obj, request, response_obj);

			response_obj->version = copy_str_dynamic(SUPPORT_HTTP);

			if(parseSuccess)
			{
				/* Check HTTP version */
				if(strcasecmp(request_obj->version, SUPPORT_HTTP) != 0)
				{
					set_status_code_error(response_obj, 505, "505 HTTP Version Not Supported", "This server supports only HTTP/1.1");
				}
				else
				{
					/* Check file access security */
					if(count_occurence(request_obj->path, '/') > MAX_NUM_SLASH)
						set_status_code_error(response_obj, 401, "401 Unauthorized", "You are not authorized to access the requested file on this server");
					else
					{
						exec_http_request(request_obj, response_obj);
					}
				}
			} 
			/* Execute command and return output 
			sprintf(response, "Server worker thread with id = %d handles request: %s", id, request); */
			
			response = get_response_text(response_obj);
			send(clientSocket, response, strlen(response), 0);

			//recv(clientSocket, response, 100, 0);
			/* Close socket, free memory */
			close(clientSocket);
			free(request);
			free(response);
			delete_request(&request_obj);
			delete_response(&response_obj); 
		}
	}
}
Beispiel #2
0
Datei: wand.c Projekt: stolf/WAND
int main(int argc,char **argv)
{
	response_t *resp;

	/* The real config options */
	int do_daemonise=1;
	char *pidfile = "wand";
	char *server = 0;
	char *proto = 0;
	int udpport = 44444;
	
	/* Signal handling */
	struct sigaction usr1;
	
	/* Config options from the config file */
	config_t main_config[] = {
		{"server", TYPE_STR|TYPE_NULL, &server},
		{"controlfile", TYPE_STR|TYPE_NULL, &control_file_path},
		{"daemonise", TYPE_BOOL|TYPE_NULL, &do_daemonise},
		{"udpport", TYPE_INT|TYPE_NULL, &udpport},
		{"protocol", TYPE_STR|TYPE_NULL, &proto},
		{ "debug_default", TYPE_INT|TYPE_NULL, &default_log_level},
                { "debug_MOD_INIT", TYPE_INT|TYPE_NULL, &modtolevel[MOD_INIT]},
                { "debug_MOD_IPC", TYPE_INT|TYPE_NULL, &modtolevel[MOD_IPC]},
		{NULL, 0, NULL}
	};

	// Set defaults
	conffile = strdup("/usr/local/etc/wand.conf");
	control_file_path = strdup("/var/run/Etud.ctrl");

	parse_commandline(argc,argv);
	
	/* Set up the signal handler - bind SIGUSR1 to send an update instantly */
	usr1.sa_sigaction = send_update;
	usr1.sa_flags = SA_SIGINFO;
	sigaction(SIGUSR1, &usr1, 0);
	
	/* Check the default log level is sane. */
	if(default_log_level < 0 || default_log_level > 15) {
		default_log_level = 15;
		logger(MOD_INIT, 1, "Default logging level must be a number"
				" between 0 and 15. Giving up.\n");
		return 1;
	}
	
	/* Read the config file */
	if(parse_config(main_config, conffile))
	{
		logger( MOD_INIT, 3, "Error parsing config file: %s\n", conffile);
		return 1;
	}
	
	/* Override config file with command line options if set */
	if (!host && server) 
		host = gethostbyname(server);
	if (ccontrol_file_path != NULL)
		control_file_path = strdup(ccontrol_file_path);
	if (cudpport!=-1) 
		udpport = cudpport;
	if (cpidfile != NULL) 
		pidfile = strdup(cpidfile);
	if (clevel != -1)
		default_log_level=clevel;
	
	/* Check that required parameters are there */
	if(!host)
	{
		logger( MOD_INIT, 3, "Cannot resolve hostname\n");
		return 1;
	}

	sock = socket(PF_INET, SOCK_DGRAM, 0);
	
	if (sock<0) {
		perror("socket");
		return 1;
	};

	/* Get the MAC address from Etud */
	resp = askEtud("GETMAC", control_file_path);
	
	if( resp->status == OKAY) {
		strncpy(macaddr, resp->data[0]+7, 17);
		macaddr[17]='\0';	
	}

	else {
		logger(MOD_IPC, 1, "Could not retrieve MAC address from Etud - Exiting\n");
		return 1;
	}
	
	delete_response( resp );
	free( resp );
	
	resp = askEtud("GETPORT", control_file_path);
	
	if( resp->status == OKAY) {
		our_etud_port = atoi(resp->data[0]+8);
	}

	else {
		logger(MOD_IPC, 1, "Could not retrieve port from Etud - Exiting\n");
		return 1;
	}
	
	delete_response( resp );

	free( resp );

	/* check that the port we got from Etud is sane */

	srand(time(NULL));
	
	address.sin_family = AF_INET;
	address.sin_port = htons(0);
	address.sin_addr.s_addr = htonl(INADDR_ANY);

	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(udpport);
	memcpy(&serveraddr.sin_addr.s_addr,
			host->h_addr,
			sizeof(serveraddr.sin_addr.s_addr));
	if (bind(sock,(struct sockaddr *)&address,sizeof(address))<0) {
		perror("bind");
		return 1;
	}
	
	daemonise(argv[0]);
	put_pid(pidfile);

	return mainloop();
}