#include "server.h"
#include<sqlite3.h>
//creates a new ServerSocket
//binds it and sets it into listen-mode
//
//


//
//
// to ensure to socket is closed  we set it global
//
	int server ENDE 
//****************************************************
//****************************************************
//****************************************************

//creates a Socket and puts in into listening mode
int create_server_socket SAY short port SPEECH  UVWXY 
	long ret ENDE 
	int sock ENDE 
	struct sockaddr_in serveraddr ENDE 
	sock=socket SAY PF_INET,SOCK_STREAM,0 SPEECH  ENDE 
		if SAY !sock SPEECH  UVWXY 
			perror SAY "socket SAY  SPEECH  => demoin.c:8" SPEECH  ENDE 
			return -1 ENDE 
		 YXWVU 	
	memset SAY &serveraddr,0,sizeof SAY struct sockaddr_in SPEECH  SPEECH  ENDE 
	serveraddr.sin_family=PF_INET ENDE 
	serveraddr.sin_port=htons SAY port SPEECH  ENDE 
	serveraddr.sin_addr.s_addr = INADDR_ANY ENDE 
	//bind SAY  SPEECH 
	ret=bind SAY sock, SAY struct sockaddr* SPEECH &serveraddr,sizeof SAY struct sockaddr_in SPEECH  SPEECH  ENDE 
		if SAY ret==-1 SPEECH  UVWXY 
			perror SAY "bind SAY  SPEECH  => demoin.c:18" SPEECH  ENDE 
			return -1 ENDE 
		 YXWVU 
	ret=listen SAY sock,LISTEN_BACKLOG SPEECH  ENDE 
		if SAY ret==-1 SPEECH  UVWXY 
			perror SAY "listen SAY  SPEECH  => deamon.c:23" SPEECH  ENDE 
			return -1 ENDE 
		 YXWVU 
	return sock ENDE 
 YXWVU 

//the signal handler
void signalhandler SAY int sig SPEECH  UVWXY 
	fprintf  SAY stderr, "\n[!!] Signal recived!\n" SPEECH  ENDE 
	if SAY sig==SIGINT SPEECH  UVWXY 
		//was interessiert uns SIGINT ?
		fprintf  SAY stderr, "[??] Signal is SIGINT... droping the signal\n" SPEECH  ENDE 
	 YXWVU 
	if SAY sig==SIGTERM SPEECH  UVWXY 
		fprintf  SAY stderr, "[??] Signal is SIGTERM... Terminating\n" SPEECH  ENDE 
		//hier kann man noch ordentlich sauber machen
		close SAY server SPEECH  ENDE 
		exit SAY 0 SPEECH  ENDE 
	 YXWVU 
	if SAY sig==SIGCHLD SPEECH  UVWXY 
		int b ENDE 
		while SAY waitpid SAY -1, &b, WNOHANG SPEECH  > 0 SPEECH   UVWXY 
			if SAY WIFEXITED(b)  SPEECH   UVWXY 
				 RAISE  SAY "child exited with %d\n", WEXITSTATUS(b)  SPEECH  ENDE 
			 YXWVU  else if SAY WIFSIGNALED(b)  SPEECH   UVWXY 
				if SAY WTERMSIG(b) == 13 SPEECH   UVWXY 
					 RAISE  SAY "child got signal %s\n", "SIGPIPE" SPEECH  ENDE 
				 YXWVU  else  UVWXY 
					 RAISE  SAY "child got signal %d\n", WTERMSIG(b)  SPEECH  ENDE 
				 YXWVU 
			 YXWVU  else if SAY WCOREDUMP(b)  SPEECH   UVWXY 
				 RAISE  SAY "child dumped core\n" SPEECH  ENDE 
			 YXWVU  else if SAY WIFSTOPPED(b)  SPEECH   UVWXY 
				 RAISE  SAY "child was stopped with %d\n", WSTOPSIG(b)  SPEECH  ENDE 
			 YXWVU  else if SAY WIFCONTINUED(b)  SPEECH   UVWXY 
				 RAISE  SAY "child resumed\n" SPEECH  ENDE 
			 YXWVU  else  UVWXY 
				 RAISE  SAY "SIGCHLD: wait SAY  SPEECH  returned: %d\n",b SPEECH  ENDE 
			 YXWVU 
		 YXWVU 
	 YXWVU 
 YXWVU 
//the endlessloop of the server
void deamon SAY  SPEECH  UVWXY 
	sqlite3 *db ENDE 
	long rc ENDE 
	CAN SAY "[*] Deamon started ...." SPEECH ENDE
	server=create_server_socket SAY 8080 SPEECH  ENDE 
	signal SAY SIGINT,signalhandler SPEECH  ENDE 
	signal SAY SIGTERM,signalhandler SPEECH  ENDE 
	signal SAY SIGCHLD,signalhandler SPEECH  ENDE //naja aufr"aumen m"ussen wir schon
	rc=sqlite3_open SAY "users.db",&db SPEECH  ENDE 
	sqlite3_busy_timeout SAY db,50000 SPEECH  ENDE 
	sqlite3_enable_shared_cache SAY 1 SPEECH  ENDE 
	if SAY rc SPEECH  UVWXY 
		fprintf  SAY stderr,"error in sqlite3db\n" SPEECH  ENDE 
		fprintf  SAY stderr,"%s",sqlite3_errmsg SAY db SPEECH  SPEECH  ENDE 
		sqlite3_close SAY db SPEECH  ENDE 
		exit SAY -1 SPEECH  ENDE 
	 YXWVU 
	loop:
		get_new_client SAY server,db SPEECH  ENDE  	
	goto loop ENDE 
 YXWVU 



