Exemplo n.º 1
0
static int test_ncache_setup(void **state)
{
    struct ncache_test_ctx *test_ctx;

    assert_true(leak_check_setup());

    test_ctx = talloc_zero(global_talloc_context, struct ncache_test_ctx);
    assert_non_null(test_ctx);

    test_dom_suite_setup(TESTS_PATH);

    test_ctx->tctx = create_dom_test_ctx(test_ctx, TESTS_PATH, TEST_CONF_DB,
                                         TEST_DOM_NAME, "ipa", NULL);
    assert_non_null(test_ctx->tctx);

    create_groups(test_ctx, test_ctx->tctx->dom);
    create_users(test_ctx, test_ctx->tctx->dom);

    check_leaks_push(test_ctx);

    *state = (void *)test_ctx;

    return 0;
}
Exemplo n.º 2
0
int phase1(){

	/*code extracted from beej guide*/
	int status, sockfd = 0;
	struct addrinfo hints;
	struct addrinfo *res, *p; // will point to the results
	const char *addr = "nunki.usc.edu";
	char s[INET6_ADDRSTRLEN];
	int new_fd = 0;
	struct sockaddr_storage their_addr;
	socklen_t sin_size;
	struct sigaction sa;
	int retVal;
	char buf[256];

	memset(&hints, 0, sizeof hints); // make sure the struct is empty

	hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
	hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
	hints.ai_flags = AI_PASSIVE; // fill in my IP for me

	if ((status = getaddrinfo(addr, HCS_TCP_STATIC_PORT, &hints, &res)) != 0) {
		fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
		exit(1);
	}

	/*code extracted from beej guide*/
	p = res;
	void *server_addr;
	struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
	server_addr = &(ipv4->sin_addr);

	/*convert the IP to a string and print it:*/
	inet_ntop(p->ai_family, server_addr, s, sizeof s);
	printf("\nPhase 1: The Health Center Server has port number %s and IP address %s.\n", HCS_TCP_STATIC_PORT, s);

	/*loop through all the results and bind to the first we can*/
	for(p = res; p != NULL; p = p->ai_next) {
		if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
			perror("socket");
			continue;
		}
		if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
			close(sockfd);
			perror("bind");
			continue;
		}
		break; 		/*if we get here, we must have connected successfully*/
	}
	if (p == NULL) {
		/*looped off the end of the list with no successful bind*/
		fprintf(stderr, "failed to bind socket\n");
		exit(2);
	}

	freeaddrinfo(res); 		/*free the linked-list*/

	/*load user.txt file*/
	if((retVal = create_users()) < 0)
		return retVal;

	/*load availabilities.txt file*/
	if((retVal = create_availabilities()) < 0)
		return retVal;

	if(listen(sockfd, BACKLOG) == -1){
		printf("Failed to listen\n");
		return -1;
	}

	/*code extracted from beej guide*/
	sa.sa_handler = sigchld_handler; // reap all dead processes
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	if (sigaction(SIGCHLD, &sa, NULL) == -1) {
		perror("sigaction");
		exit(1);
	}

	/*code extracted from beej guide*/
	while(1)
	{
		sin_size = sizeof their_addr;
		new_fd = 0;
		new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
		if (new_fd == -1)
		{
			/*perror("Healthcenterserver Error: accept");*/
			continue;
		}

		inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
		int port = ntohs(((struct sockaddr_in *)&their_addr)->sin_port);
		if (!fork()) // this is the child process
		{
			close(sockfd); // child doesn't need the listener

			int number_of_bytes;
			memset(buf, '0' ,sizeof(buf));
			if ((number_of_bytes = recv(new_fd, buf, MAXBUFLEN-1, 0)) == -1)
			{
				perror("Healthcenterserver Error: when receiving bytes\n");
				exit(1);
			}

			buf[number_of_bytes] = '\0';

			struct users temp_patient;
			retVal = check_authentication((char *)buf, &temp_patient);

			printf("\nPhase 1: The Health Center Server has received request from a patient with username %s and password %s.\n", temp_patient.username, temp_patient.password);

			if(retVal == 1){
				if (send(new_fd, "success", 7, 0) == -1)
				{
					perror("Healthcenterserver Error: while sending\n");
					exit(1);
				}
				printf("\nPhase 1: The Health Center Server sends the response success to patient with username %s.\n", temp_patient.username);

				/*start of phase2 stage1*/
				retVal = phase2(&new_fd, &port, s);

				printf("\nPhase 2: The Health Center Server sends available time slots to patient with username %s.\n", temp_patient.username);

				memset(buf, '\0' ,sizeof(buf));
				if ((number_of_bytes = recv(new_fd, buf, MAXBUFLEN-1, 0)) == -1)
				{
					perror("Healthcenterserver Error: when receiving bytes\n");
					exit(1);
				}

				buf[number_of_bytes] = '\0';

				printf("\nPhase 2: The Health Center Server receives a request for appointment %c from patient with port number %d and username %s.\n", buf[10], port, temp_patient.username);
				/*end of phase2 stage1*/

				/*start of phase2 stage2*/
				char sendBuf[256];
				memset(sendBuf, '\0' ,sizeof(sendBuf));
				retVal = check_availability(buf, sendBuf);

				if(retVal == 1){
					printf("\nPhase 2: The Health Center Server confirms the following appointment %c to patient with username %s.\n", buf[10], temp_patient.username);
					if (send(new_fd, sendBuf, sizeof(sendBuf), 0) == -1)
					{
						perror("Healthcenterserver Error: while sending\n");
						exit(1);
					}
				}
				else{
					printf("\nPhase 2: The Health Center Server rejects the following appointment %c to patient with username %s.\n", buf[10], temp_patient.username);
					if (send(new_fd, "notavailable", 12, 0) == -1)
					{
						perror("Healthcenterserver Error: while sending\n");
						exit(1);
					}
				}
				memset(sendBuf, '\0' ,sizeof(sendBuf));
				/*end of phase2 stage2*/
			}
			else{
				if (send(new_fd, "failure", 7, 0) == -1)
				{
					perror("Healthcenterserver Error: while sending\n");
					exit(1);
				}
				printf("\nPhase 1: The Health Center Server sends the response failure to patient with username %s.\n", temp_patient.username);
			}
			close(new_fd);
			exit(0);
		}
		else
			close(new_fd);  /*parent doesn't need this*/
		/*while(wait(NULL) > 0);*/
	}
	while(wait(NULL) > 0);
	close(sockfd);
	return 0;
}