/* securityClient.c
 *
 *  Created on: Apr 29, 2015
 *      Author: kapvx4
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/time.h>
#define size 40
#define row 5
#define col 7

char hostName[50];
struct hostent *myHost;
struct in_addr *addresses;
struct sockaddr_in server, from;
unsigned int length;
int sock, n;
char buffer[size] = " ";
socklen_t fromlen;
int boolval = 1;
unsigned short pnumber = 2000;
int rnum = 0;
char password[6];
char Oipaddress[10], Aipaddress[10];
int i, j;
int status = 0; //0 - clear 1- breach

char string10[2];
int clear = 0;
FILE *fp;


char database[5][7];

void error(const char *msg);
int main(int argc, char *argv[])
{


	//open file
	fp = fopen("Passwords.txt", "r");
	//read from password file store in database array
	for(i =0; i < row; i++)
	{
		//for(j = 0; j < col; j++)
		{
			fscanf(fp, "%s", database[i]);
		}
	}

			printf("database i = %s\n",database[1]);
	//get the host name
	gethostname(hostName,sizeof(hostName));

	//get host by name
	myHost = gethostbyname(hostName);

	//Print host information
	printf("Host Name: %s\n", myHost->h_name);
	printf("Host IP Addresses: \n");
	addresses = (struct in_addr*)myHost->h_addr_list[0];

	printf("%s \n ", inet_ntoa(*addresses));


	bzero((char*)&server, length);
	server.sin_family = AF_INET; //Symbol constant for internet domain
	server.sin_addr.s_addr = INADDR_ANY; //IP address of the machine on which
	printf("What is the Port Number?\n");
	scanf("%hu", &pnumber); //read unsigned short port number from the user
	server.sin_port = htons(pnumber); //port number**
	fromlen = sizeof(struct sockaddr_in); //size of structure
	from.sin_family = AF_INET;
	from.sin_port = htons(pnumber);
	from.sin_addr.s_addr = inet_addr("10.3.52.255"); //Broadcast Address




	sock = socket(AF_INET, SOCK_DGRAM, 0); //Creates Socket Connectionless.

	if(sock <0)
		error("socket");

	//bind the socket to the address of the host and the port number
	if(bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
		error("Error Binding");

	//set broadcast option
	if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &boolval, sizeof(boolval)) < 0)
	{
		printf("error setting socket options\n");
		exit(-1);

	}

	while(1)
	{
	sleep(1);
		//broadcast all clear
		if(clear == 0)
		{

			clear = 0;
			from.sin_addr.s_addr = inet_addr("10.3.52.255");
			char message[size] = "ALL CLEAR";
			strcpy(buffer, message);
			n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr *)&from, fromlen); //Broadcast message
			if(n <0)
				error("Error Broadcasting message\n");
			//sleep(1000);

			bzero(buffer, size);
			//receive password from servers
			n = recvfrom(sock, buffer, size, 0, (struct sockaddr*)&from, &fromlen);
			if(n < 0)
				error("recvfrom");
			printf("Message Received: ");
			printf("%s\n", buffer);
			//check password on database

			if(buffer[0] == '#') //Password data
			{
				clear = 1;
			for(i = 0; i < row; i++)
			{
				if(strncmp(buffer, database[i], 6)==0) //if password is equal open the door
								{

									clear =0;
									char message[size] = "OPENDOOR";
									strcpy(buffer, message);
									//sleep(1000);
									n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr*)&from, fromlen); //Send message to client

								}

			}

			} //End of Password data

		}


		if(clear == 1)// security has been breached
		{
			from.sin_addr.s_addr = inet_addr("10.3.52.255");
			char message[size] = "SECURITY BREACH";
			strcpy(buffer, message);
			//sleep(10000);
			n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr *)&from, fromlen); //Broadcast message
			if(n <0)
				error("Error Broadcasting message\n");



			//Check password to clear breach
			if(buffer[0] == '#') //Password data
			{
							clear = 1;
						for(i = 0; i < row; i++)
						{
							if(strncmp(buffer, database[i], 6)==0) //if password is equal open the door
											{

												clear =0;
												char message[size] = "OPENDOOR";
												strcpy(buffer, message);
												//sleep(1000);
												n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr*)&from, fromlen); //Send message to client

											}
						}
				}

	}


				if (strncmp("ALL CLEAR", buffer,  8)==0) //if all clear proceed as normal
				{
				clear = 0;

				}

				if (strncmp("SECURITY BREACH", buffer,  15)==0) //if all clear proceed as normal
				{
					clear = 1;
					printf("The facility has a Security Breach.!!!\n");

				}


				if ((strncmp("LOCKDOWN", buffer, 8)==0)) //Lock down, dont allow entry of passwords
				{

					clear = 1; //breach
					printf("The facility is on Lockdown.\n");
					printf("All doors have been locked. \n");


				}

				if(strncmp("OPENDOOR", buffer, 8)==0)
				{
					printf("The door has been opened.\n");



				}

}

}

