/*
 * Project 1 for CS640
 *  Blaster
 *
 * Names: Will McCardell & Tristan H.
 * cs logins: mccardel & INSERT NAME HERE PLZKTHXBAI
 *
 *
 * man pages to look up:
 *  gethostbyname(3)
 *  getaddrinfo(3) //check example code
 *  getnameinfo(3)
 *
 *
 * things to do:
 *  FIX THE INPUT STUFF. YUCKY.
 */


#include <sys/types.h>
#include <sys/socket.h>
//following two lines (the netinet ones) are for ip
#include <netinet/in.h>
#include <netinet/ip.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

//stuff for the getopt command
extern char *optarg;
extern int optind, opterr, optopt;

#include <getopt.h>

#include <iostream>

using namespace std;


int main(int argc, char* argv[])
{

    //the character that is returned by getopt
    int optVal;
    //all these next ones are just the command line arguments
    char* blasterHost;
    int blasterPort;
    int blasterRate;
    int blasterNum;
    int seq_no;
    int blasterLen;

    blasterHost = (char*)calloc(20, sizeof(char));
    char *r = blasterHost;

    int blast = socket(PF_INET, SOCK_DGRAM, 0);
    //OH MY GOD NOOOO
    //things broke.
    if(blast == -1)
    {
        perror("socket");
        exit(-1);
    }

    //right number of arguments?
    if(argc != 13)
    {
        printf("Incorrect number of arguments. Command should be of the form:\n");
        printf("blaster -s <hostname> -p <port> -r <rate> -n <num> -q <seq_no> -l <length>\n");
        printf("Please try again.\n");
        exit(-1);
    }

    //read in the arguments...until everything has been parsed
    while((optVal = getopt(argc, argv, "s:p:r:n:q:l:")) != -1)
    {
        switch(optVal)
        {
            case 's':
                //copy whatever optarg is into blastedHost
                //  so we get the host name
                while((*r++ = *optarg++) != '\0');
                cout << "bH = " << blasterHost << endl;
            break;

            case 'p':
                //get the port we're firing packets into
                blasterPort = atoi(optarg);
                cout << "bP = " << blasterPort << endl;
            break;

            case 'r': 
                //get how fast we want to fire packets at the port
                blasterRate = atoi(optarg);
                cout << "bR = " << blasterRate << endl;
            break;

            case 'n':
                //get how many packets are to be fired
                blasterNum = atoi(optarg);
                cout << "bN = " << blasterNum << endl;
            break;

            case 'q':
                //initial sequence of the pkt exchange
                seq_no = atoi(optarg);
                cout << "seq_no = " << seq_no << endl;

            break;

            case 'l':
                //get the length of the packets
                blasterLen = atoi(optarg);
                cout << "bL = " << blasterLen << endl;
            break;
        }
    }
    
    return 0;
}
