/* 	Name       : 	serverFinal.c
	Author     : 	Carlos Huezo
	Description:
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <time.h>
#include <ifaddrs.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

   //buffers storing address defined.
   char addBuffer[INET_ADDRSTRLEN];
   char addBufferT[INET_ADDRSTRLEN];
	int sock, length, n;
   int boolval = 1;			// for a socket option
   socklen_t fromlen;
   struct sockaddr_in server;
   struct sockaddr_in addr;
   char buffer[MSG_SIZE];	// to store received messages or messages to be sent.
   char bName[MSG_SIZE];
   char srandNum[10];
   int flag=0;
   int randNum=0;
   char *strToken;
   int myBoard;
   char *boardNum;
   int fd_fifo;
  
   int fd;
   ulong *ptr;
   
void myTask1(void);
void error(const char *msg);

int main(int argc, char *argv[]){
	pthread_t myThread1, myThread2;

	//start FIFO
	fd_fifo=open("/dev/rtf/0",O_RDWR);
		if(fd_fifo<0){
			printf("FIFO can't be opened\n");
			exit(1);
		}
	//soft interrupt starting
	fd=open("/dev/mem",O_RDWR);
	ptr=mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x800C0000);
	pthread_create(&myThread1,NULL,(void*)&myTask1,0);

   //structure accessing for ip addressing
   struct ifaddrs * adStruct = NULL;
   struct ifaddrs *ptr = NULL;
   void * tmpAddrPtr = NULL;
    //creates linked list of struct
   //to access interface addresses
   getifaddrs(&adStruct);

   //points to structure of type if addrs
   ptr = adStruct;
   ptr = ptr->ifa_next;

	//IPv4 addressing commences through the structure sockaddr_in
    if (ptr ->ifa_addr->sa_family==AF_INET){

		//accessing the internet address sin_addr
         tmpAddrPtr=&((struct sockaddr_in *)ptr->ifa_addr)->sin_addr;
		 //takes address of family and returns a readable
		 //address of maximum address length [INET_ADDRSTRLEN]
         inet_ntop(AF_INET, tmpAddrPtr, addBuffer, INET_ADDRSTRLEN);
         printf("MY IP is %s\n", addBuffer);
    }

	sock = socket(AF_INET, SOCK_DGRAM, 0); // Creates socket. Connectionless.
   if (sock < 0){
	   error("Error opening socket");
	}

   length = sizeof(server);					// length of structure
   bzero(&server,length);					// sets all values to zero. memset() could be used
   server.sin_family = AF_INET;				// symbol constant for Internet domain
   server.sin_addr.s_addr = INADDR_ANY;		// IP address of the machine on which
											// the server is running
   server.sin_port = htons(atoi(argv[1]));	// port number

   // binds the socket to the address of the host and the port number
   if (bind(sock, (struct sockaddr *)&server, length) < 0){
       error("binding");
	}

   // change socket permissions to allow broadcast
   if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &boolval, sizeof(boolval)) < 0){

		printf("error setting socket options\n");
   		exit(-1);
   	}
// size of structure
   fromlen = sizeof(struct sockaddr_in);	

   pthread_join(myThread1,NULL);


   return 0;
 }
void error(const char *msg){
    perror(msg);
    exit(0);
}
void myTask1(void){
	
	
	
}



