/* qADSL final release 1.2 (011003)
 * Copyright (c) 2001 Jakob "kuba" Stasilowicz (kuba@linuxsweden.nu)
 * http://qba.linuxsweden.nu
 *
 * -------------------------------------------------------------------------
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * ---------------------------------------------------------------------------
 * 
 * This program has very dirty code, i know. Alot of globals etc, but I wrote
 * this when I was still in a very early stage of learning C. And after all, I didn't
 * have any teacher here to help me.
 */

#include "qadsl.h"

struct hostent *he;
struct sockaddr_in address;

int login_sw=0,sockfd,i;
char send_msg[MAXDATASIZE], get_msg[MAXDATASIZE];
char user[MAXLEN], pass[MAXLEN], serv[MAXLEN], deamon_s[MAXLEN], deamon_t[MAXLEN], deamon_d[MAXLEN];

char *conf="/etc/qadsl.conf";

int main(int argc, char *argv[])
{
	int con_sw=0;
	int dologin=0;
	int dologout=0;

        struct tm *timep;
        time_t the_time;

        (void) time(&the_time);
        timep=localtime(&the_time);

	printf("qADSL %s by kuba - http://qba.linuxsweden.nu\n",VERSION);

	if(argc < 2) {
		usage(argv[0]);
		exit(0);
	}

	for(i = 0; i < argc; i++) {
		if((strncmp("-l",argv[i],2))==0) {
			if(!dologout) dologin=1;
		} 

		if((strncmp("-c",argv[i],2))==0) {
			conf=argv[i+1];
		}

		if((strncmp("-o",argv[i],2))==0) {
			if(!dologin) dologout=1;
		}

	        if((strncmp("-h",argv[i],2))==0) {
			usage(argv[0]);
        		exit(0);
                }

	}
	
	if((!dologin) && (!dologout)) {
		usage(argv[0]);
		exit(0);
        }
			
	read_conf();

	if ((he=gethostbyname(serv)) == NULL) {   
		perror("gethostbyname");
		exit(1);
	}
	close(sockfd);       
      
	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}         

	address.sin_family = PF_INET;      
	address.sin_port = htons(PORT);    
	address.sin_addr = *((struct in_addr *)he->h_addr);
	memset(&(address.sin_zero),0,sizeof(address.sin_zero));   

	if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == -1) {
		perror("connect");
		exit(1);
	}
   
	if(dologin) {

		preconnect();

		close(sockfd);

		if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
			perror("socket");
			exit(1);
		}         

		printf("\tConnecting to %s...\n", serv);

		if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == -1) {
			perror("connect");
			exit(1);
		}   

		printf("\tConnected!\n");

		log_login(1);

		close(sockfd);

	} else if(dologout) {

		telia_logout();

		close(sockfd);
		exit(0);
	}

	if((strncmp(deamon_s,"YES",3))==0) {
		kill_pid();
		if(fork() == 0) {
			printf("qADSL deamon started, pid: %d\n",getpid());		
			while(1) {
				(void)signal(SIGTERM, quit_deamon);

				create_pid();
				
				sleep(60 * atoi(deamon_d));

				close(sockfd);
				if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) exit(1);

				if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == -1) {
					for(i=0; i < 4; i++) {
						sleep(60 * 30);

						if(connect(sockfd, (struct sockaddr *)&address, sizeof(address))) {
							con_sw=1; 
							break;
						}
					}
					
				} else con_sw=1;

				if(!con_sw) {
					openlog("qADSL ", LOG_PID, LOG_USER);
					syslog(LOG_ERR,"Failed to connect to telia (%02d:%02d:%02d)\n",timep->tm_hour, timep->tm_min, timep->tm_sec);
					exit(1);
				}

				if((strncmp(deamon_t,"LOGIN",5))==0) {
					preconnect();
					close(sockfd);
					if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) exit(1);

					if (connect(sockfd, (struct sockaddr *)&address, sizeof(address)) == -1) exit(1);
					
					log_login(0);	
				}

				if((strncmp(deamon_t,"PING",4))==0) {
					sprintf(send_msg,"GET /sd/init HTTP/1.0\n" \
					"Referer: http://%s/sd/init\n" \
					"Connection: Keep-Alive\n" \
					"User-Agent: Mozilla/4.73 [en] (X11; U; Linux 2.4.0 i686)\n" \
					"Host: %s\n" \
					"Accept: image/gif, image/x-xbitmap, image/jpeg, " \
					"image/pjpeg, image/png, */*\n" \
					"Accept-Encoding: gzip\n" \
					"Accept-Language: en\n" \
					"Accept-Charset: iso-8859-1,*,utf-8\n", serv, serv);

					send_func(send_msg);

				}

			}
		} 
	}
   usleep(50000);
 return 0;
}
