/*  This file is part of lsnet. 

    Lsnet is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Lsnet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with lsnet.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "netinfo.h"

int netinfo(int netfrequency, double netarray1[2][9]) 
{
	memset( (void *) netarray1, '\0', sizeof(double) * 2 * 9);
	//sleep(netfrequency);
	//printf("In meminfo, memfrequency slept: %d\n", memfrequency);
        FILE* fp;
        struct timeval starttime,endtime;
        double te0;


	double netin;
        int count = 0;
	int count2;
	/*
	char netin1[12];
	char packin1[12];
	char netout1[12];
	char packout1[sizeof(char)*12];

        char netin2[12];  
        char packin2[12];                         
        char netout2[12];
        char packout2[12];
	*/

        char buf[SIZE];
        char temp[3];                           
        char bufstr[sizeof(temp)*14];            

	//char bufstr[12];
	//char buf[SIZE];
	//char temp[12];

	//int linenum = 0;
        if( ( fp = fopen( "/proc/net/dev", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /proc/net/dev\n" );
                exit( 1 );
        }
        gettimeofday(&starttime, NULL);
        while( fgets(buf, sizeof(buf), fp) != NULL)
        {

		//eth0:1973355872 44136361    0    0    0     0          0         0 1177611554 19597524    0    0    0     0       0          0
		count = 0;
		if (buf[count] == ' '){
			while(buf[count] == ' ') {
				count++;
			}
			//count++;
		}
		if (buf[count] == 'e'){
			count++;
			//printf("found e incremented count\n");
                	if (buf[count] == 't'){
				count++;
                        	if (buf[count] == 'h'){
					count++;
                                	if (buf[count] == '0'){ 
						count++;
                                        	if (buf[count] == ':')
                                                {
								
								count2 = count + 1;
								//printf("netinfo eth0: found\n");
								//temp[0]=buf[count2];
								while(buf[count2] == ' ') {
									count2++;
								}
								temp[0]=buf[count2];
								temp[1]=0;
								strcpy(bufstr, temp);
								//count2++;
								for( (count2 = count2 + 1); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
									temp[0]=buf[count2];
									temp[1]=0;
									strcat(bufstr,temp);									

								}
								//strcpy(netin1, bufstr);
								//printf("NETIN1 FROM STRING: %s\n", bufstr);
								//netin = atof(bufstr);
								//printf("NETIN1 FROM DOUBLE: %lf\n", netin);
								netarray1[1][1] = atof(bufstr);
								memset ((void*) bufstr, '\0', sizeof (bufstr));
								//printf("NETIN1 FROM ARRAY: %lf\n", netarray1[1][1]);

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								for( (count2 = count2); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
									temp[0]=buf[count2];
									temp[1]=0;
									strcat(bufstr,temp);
								}
								//strcpy(packin1, bufstr);
								//printf("PACKIN1: %s\n", bufstr);
								netarray1[1][2] =  atof(bufstr);
								memset ((void*) bufstr, '\0', sizeof (bufstr));


								//6 series of numbers/spaces
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
									//printf("STEP1\n");
								while((buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7' 
								||buf[count2] == '8' || buf[count2] == '9') && (buf[count2] != ' ')) {
									count2++;								
								}
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }

									//printf("STEP2\n");
                                                                while((buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9') && (buf[count2] != ' ')) {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
									//printf("STEP3\n");
                                                                while((buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9') && (buf[count2] != ' ')) {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
									//printf("STEP4\n");
                                                                while((buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9') && (buf[count2] != ' ')) {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
                                                                while((buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9') && (buf[count2] != ' ')) {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }

                                                                while((buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9') && (buf[count2] != ' ')) {
                                                                        count2++;
                                                                }
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
								///end of spaces/numbers crap



                                                                for( (count2 = count2); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                }
                                                                //strcpy(netout1, bufstr);
								//printf("NETOUT1: %s\n", bufstr);
								netarray1[1][3] =  atof(bufstr);
                                                                memset ((void*) bufstr, '\0', sizeof (bufstr));

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }

                                                                for( (count2 = count2); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                }
                                                                //strcpy(packout1, bufstr);
								//printf("PACKOUT1: %s\n", bufstr);
								netarray1[1][4] =  atof(bufstr);
                                                                memset ((void*) bufstr, '\0', sizeof (bufstr));

                                                }
                                        }
                                }
                        }
                }

	}
	fclose( fp );
	
	sleep(netfrequency);
	//sleep(1);

        if( ( fp = fopen( "/proc/net/dev", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /proc/net/dev\n" );
                exit( 1 );
        }
        gettimeofday(&endtime, NULL);
        while( fgets(buf, sizeof(buf), fp) != NULL)
        {
                count = 0;
                if (buf[count] == ' '){                         
                        while(buf[count] == ' ') {
                                count++;
                        }
                        //count++;
                }
                        
                if (buf[count] == 'e'){
			count++;
                        if (buf[count] == 't'){
				count++;
                                if (buf[count] == 'h'){
					count++;
                                        if (buf[count] == '0'){ 
						count++;
                                                if (buf[count] == ':')
                                                {
                                                                count2 = count + 1;
                                                                //temp[0]=buf[count2];     
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
                                                                temp[0]=buf[count2];
                                                                temp[1]=0;
                                                                strcpy(bufstr, temp);
                                                                //count2++;
                                                                for( (count2 = count2 + 1); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                         
                                                                }
                                                                //strcpy(netin2, bufstr);
								netarray1[2][1] =  atof(bufstr);
                                                                memset ((void*) bufstr, '\0', sizeof (bufstr));

                                                                while(buf[count2] == ' ') {
                                                                        count2++; 
                                                                }
                                                                for( (count2 = count2); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                }
                                                                //strcpy(packin2, bufstr);
                                                                netarray1[2][2] =  atof(bufstr);
                                                                memset ((void*) bufstr, '\0', sizeof (bufstr));

                                                                //6 series of numbers/spaces
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
                                                                while(buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9' && buf[count2] != ' ') {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9' && buf[count2] != ' ') {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
                                                                while(buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9' && buf[count2] != ' ') {
                                                                        count2++;
                                                                }
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9' && buf[count2] != ' ') {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9' && buf[count2] != ' ') {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }

                                                                while(buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'
                                                                ||buf[count2] == '8' || buf[count2] == '9' && buf[count2] != ' ') {
                                                                        count2++;
                                                                }
                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
                                                                ///end of spaces/numbers crap

                                                                for( (count2 = count2); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                }
                                                                //strcpy(netout1, bufstr);
                                                                netarray1[2][3] =  atof(bufstr);
                                                                memset ((void*) bufstr, '\0', sizeof (bufstr));

                                                                while(buf[count2] == ' ') {
                                                                        count2++;
                                                                }
                                                                for( (count2 = count2); (count2 <= strlen(buf) && buf[count2] != ' '); count2++){
                                                                        temp[0]=buf[count2];
                                                                        temp[1]=0;
                                                                        strcat(bufstr,temp);
                                                                }
                                                                //strcpy(packout1, bufstr);
                                                                netarray1[2][4] =  atof(bufstr);
                                                                memset ((void*) bufstr, '\0', sizeof (bufstr));

                                                }
                                        }
                                }
                        }
                }
                                                                 
        }
        fclose( fp );
        te0=((double)(endtime.tv_sec*1000000-starttime.tv_sec*1000000+endtime.tv_usec-starttime.tv_usec))/1000000;


	//netarray1[2][4] / 1048576
	//get totals
	netarray1[1][1] = (netarray1[2][1] - netarray1[1][1]) / 1024;
        netarray1[1][2] = netarray1[2][2] - netarray1[1][2];
        netarray1[1][3] = (netarray1[2][3] - netarray1[1][3]) / 1024;
        netarray1[1][4] = netarray1[2][4] - netarray1[1][4];

	//get true kb/second and packet/second results
	//netarray1[1][1] = netarray1[1][1] / te0;
	//netarray1[1][2] = netarray1[1][2] / te0;
	//netarray1[1][3] = netarray1[1][3] / te0;
	//netarray1[1][4] = netarray1[1][4] / te0;


	//netarray1[2][1] / 1048576;
	//netarray1[2][2]
	//netarray1[2][3] / 1048576;
	//netarray1[2][4]




	//	printf("inside netinfo netin: %lf\n", netarray1[1][1] );
	//	printf("inside netinfo packin: %lf\n", netarray1[1][2]);
	//        printf("inside netinfo netout: %lf\n", netarray1[1][3] );
	//        printf("inside netinfo packout: %lf\n", netarray1[1][4]); 


        return (te0);
}

