/*
purpose: opens a connection to a server on port 5555 and sends login and nas info.  accepts return value.
returns: 0 for login ok, 1 for login not ok, and -1 for error
Author: Ben Hussey
Date: January 2004
*/


#include <string.h>	//has string manipulation functions
#include <stdio.h>	//standard input output fcns
#include <sys/types.h>	//data structs for in.h and socket.h
#include <sys/socket.h>	//socket creation and handling
#include <netinet/in.h>	//stuff for internet domain addresses
#include <netdb.h>	//has the hostent struct
#include <unistd.h>	//for i/o on file descriptors
#include <stdlib.h>	//has atoi and exit functions


void error(char *msg, int exit_signal)	//error handling function prints msg and exits with signal
{
	perror(msg);
	exit(exit_signal);
}


int main(int argc, char *argv[])
{
	char buffer[1024];		//holds values for sending or recieving from the server
	int port_number=5555;
	int socket_file_descriptor;	//holds the file descriptor returned by socket()
	int rw_return_val;		//hold return value from read and write
	int server_return_val;		//hold the return value of the server
	struct hostent *server;		//holds network host information defined in netdb.h use to get info to fill in serv_addr
	struct sockaddr_in serv_addr;	//holds network socket address defined in in.h
	
	
	if(argc != 6)	//make sure we get the proper number of variables
	{
		printf("\n  Usage:  %s <nas_type> <nas_ip> <nas_port> <login> <session_id>\n\n", argv[0]);
		exit(-1);
	}
	
	socket_file_descriptor = socket(PF_INET, SOCK_STREAM, 0);	//create the streaming ,internet socket and let the computer pick the protocol(TCP)
	if(socket_file_descriptor < 0)	//make sure there's no error in socket creation
	{
		error("ERROR opening socket", -1);
	}

	server = gethostbyname("127.0.0.1");	//fill in the hostent struct with localhost info,could avoid getho change this for network connections
	if(server == NULL)	//this is not good, maybe the host doesn't exist....
	{
		error("ERROR unable to get host information", -1);
	}
	
	//now set up the socket address
	memcpy( (char *)&serv_addr, "\0", sizeof(serv_addr));	//copy sizeof(servaddr) nulls to address &serv_addr
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(port_number);	//change port to network byte order(big endian)
	memcpy((char*)&serv_addr.sin_addr.s_addr, (char*)server->h_addr, server->h_length);	//copy the h_addr to s_addr
	
	if( connect(socket_file_descriptor,(struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 )	//try to connect to serv_addr
	{
		error("ERROR unable to connect!", -1);
	}
	
	rw_return_val = read(socket_file_descriptor, buffer, sizeof(buffer));	//try to read data from the socket
	if(rw_return_val == -1)		//make sure the server responded
	{
		error("ERROR the server did not respond", -1);
	}

	if(!strcmp("radcheck Server Ready:", buffer))	//if proper string not sent, error. 
	{
		error("ERROR: server sent bad input", -1);
	}

	memset((char*)&buffer, 0, sizeof(buffer));	//reset buffer to null
	snprintf((char*)&buffer, sizeof(buffer), "CHECK %s %s %s %s\n", argv[2], argv[3], argv[4], argv[5]);	//fill the buffer with the query string
	
	rw_return_val = write(socket_file_descriptor, buffer, sizeof(buffer));	//send the string to the server
	if(rw_return_val == -1)	//make sure send was succesful
	{
		error("ERROR: unable to write to server", -1);
	}

	memset((char*)&buffer, 0, sizeof(buffer));      //reset buffer to null
	rw_return_val  = read(socket_file_descriptor, buffer, sizeof(buffer));   //try to read a 1 or 0 from server
	if(rw_return_val == -1)         //make sure the server responded
	{
		error("ERROR the server did not respond", -1);
	}

	server_return_val = atoi(&buffer[0]);	//change the return value from a char to an int and assign to server return val
	
	memset( (char*)&buffer, 0, sizeof(buffer) );      //reset buffer to null
	snprintf( (char*)&buffer, 6, "QUIT\n" );    //fill the buffer with the query string, tell the server to quit
	rw_return_val = write(socket_file_descriptor, (char*)&buffer, sizeof(buffer) );
	if(rw_return_val == -1)
	{
		error("ERROR unable to write to server", -1);
	}

	//return the same return value as the server, if 0 or 1, else return error(-1)
	if(server_return_val == 0)
	{
		exit(0);
	}else if(server_return_val == 1)
	{
		exit(1);
	}else
	{
		exit(-1);
	}
}





