[C] SmallServ!!! Disfrutadlo y opnidad!!!

Iniciado por Garfield07, 22 Noviembre 2010, 19:48 PM

0 Miembros y 1 Visitante están viendo este tema.

Garfield07

#include <sys/socket.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <signal.h>
#include <time.h>

int socketfd, newsocket;
char *filename;

void shutup (int signal)
{
FILE *log;
log=fopen (filename, "a+");
times ();
printf ("Shutting down...\n\n");
fprintf (log, "Shutting down...\n\n");
fclose (log);
close (newsocket);
close (socketfd);
exit (0);
}

int times ()
{
FILE *log;
time_t now=time (0);
struct tm *ahora;
char buffer [40];
ahora=localtime ((const time_t*)&now);
strftime (buffer, 40, "%d/%m/%Y %H:%M:%S" , ahora);
log=fopen (filename, "a+");
printf ("%s   ", buffer);
fprintf (log,"%s   ", buffer);
fclose (log);
return 0;
}

int main (int argc, char *argv [])
{
time_t now=time (0);
struct tm *ahora;
char hora [40];
ahora=localtime ((const time_t*)&now);
strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
printf ("SmallServ 2.0 - By Sagrini - Sagrini 2010 - %s\n", hora);

if (getuid()!=0)
{
printf ("This proccess must be run by root.\n\n");
return 1;
}
if (argc<3)
{
printf ("Use: %s <PORT> <LOG> [MODE]\nMode:\t\t0) Fork the proccess to background [DEFAULT].\n\t\t1) Run in the terminal.\n\n", argv [0]);
return 1;
}
int cont;
FILE *log;
struct sockaddr_in client, host;
char buffer [1024];
int size=sizeof (client);

filename = argv [2];

socketfd=socket (2, 1 ,  0);
host.sin_family=AF_INET;
host.sin_port=htons (atoi (argv [1]));
host.sin_addr.s_addr=0;
bind (socketfd, (struct sockaddr*)&host, sizeof (struct sockaddr));

listen (socketfd, 3);

log=fopen (filename, "a+");
times ();
if (argv [3] != NULL && atoi (argv [3]) == 1)
printf ("\nStarting up...\n\n");
fprintf (log, "Starting up...\n\n");
fclose (log);

signal (SIGTERM, shutup);
signal (SIGINT, shutup);


if (argv [3] == NULL || atoi (argv [3]) == 0)
daemon (1, 0);

while (1)
{
newsocket=accept (socketfd, (struct sockaddr*)&client, &size);

log=fopen (filename, "a+");
times ();
printf ("Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
fprintf (log, "Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
fclose (log);

cont=recv (newsocket, &buffer, 1024, 0);
while (cont>2)
{
printf ("%d", cont);
log=fopen (filename, "a+");
times ();
buffer [cont-1]='\0';
printf ("RECV %d bytes: %s ", cont-2, buffer);
fprintf (log, "RECV %d bytes: %s\n", cont-2, buffer);
fclose (log);

cont=recv (newsocket, &buffer, 1024, 0);
}
log=fopen (filename, "a+");
times ();
printf ("Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
fprintf (log, "Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
fclose (log);
close (newsocket);
}
close (socketfd);
return 0;
}


Si alguien cree que algo se puede mejorar, que postee xD


* Quiero cambiar el mundo, pero estoy seguro de que no me darían el código fuente.
* No estoy tratando de destruir a Microsoft. Ese será tan solo un efecto colateral no intencionado.
* Si compila esta bien, si arranca es perfecto.

¡Wiki elhacker.net!
Un saludo