Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Garfield07 en 22 Noviembre 2010, 19:48 pm



Título: [C] SmallServ!!! Disfrutadlo y opnidad!!!
Publicado por: Garfield07 en 22 Noviembre 2010, 19:48 pm
Código
  1. #include <sys/socket.h>
  2. #include <arpa/inet.h>
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #include <signal.h>
  9. #include <time.h>
  10.  
  11. int socketfd, newsocket;
  12. char *filename;
  13.  
  14. void shutup (int signal)
  15. {
  16. FILE *log;
  17. log=fopen (filename, "a+");
  18. times ();
  19. printf ("Shutting down...\n\n");
  20. fprintf (log, "Shutting down...\n\n");
  21. close (newsocket);
  22. close (socketfd);
  23. exit (0);
  24. }
  25.  
  26. int times ()
  27. {
  28. FILE *log;
  29. time_t now=time (0);
  30. struct tm *ahora;
  31. char buffer [40];
  32. ahora=localtime ((const time_t*)&now);
  33. strftime (buffer, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  34. log=fopen (filename, "a+");
  35. printf ("%s   ", buffer);
  36. fprintf (log,"%s   ", buffer);
  37. return 0;
  38. }
  39.  
  40. int main (int argc, char *argv [])
  41. {
  42. time_t now=time (0);
  43. struct tm *ahora;
  44. char hora [40];
  45. ahora=localtime ((const time_t*)&now);
  46. strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  47. printf ("SmallServ 2.0 - By Sagrini - Sagrini 2010 - %s\n", hora);
  48.  
  49. if (getuid()!=0)
  50. {
  51. printf ("This proccess must be run by root.\n\n");
  52. return 1;
  53. }
  54. if (argc<3)
  55. {
  56. 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]);
  57. return 1;
  58. }
  59. int cont;
  60. FILE *log;
  61. struct sockaddr_in client, host;
  62. char buffer [1024];
  63. int size=sizeof (client);
  64.  
  65. filename = argv [2];
  66.  
  67. socketfd=socket (2, 1 ,  0);
  68. host.sin_family=AF_INET;
  69. host.sin_port=htons (atoi (argv [1]));
  70. host.sin_addr.s_addr=0;
  71. bind (socketfd, (struct sockaddr*)&host, sizeof (struct sockaddr));
  72.  
  73. listen (socketfd, 3);
  74.  
  75. log=fopen (filename, "a+");
  76. times ();
  77. if (argv [3] != NULL && atoi (argv [3]) == 1)
  78. printf ("\nStarting up...\n\n");
  79. fprintf (log, "Starting up...\n\n");
  80.  
  81. signal (SIGTERM, shutup);
  82. signal (SIGINT, shutup);
  83.  
  84.  
  85. if (argv [3] == NULL || atoi (argv [3]) == 0)
  86. daemon (1, 0);
  87.  
  88. while (1)
  89. {
  90. newsocket=accept (socketfd, (struct sockaddr*)&client, &size);
  91.  
  92. log=fopen (filename, "a+");
  93. times ();
  94. printf ("Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  95. fprintf (log, "Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  96.  
  97. cont=recv (newsocket, &buffer, 1024, 0);
  98. while (cont>2)
  99. {
  100. printf ("%d", cont);
  101. log=fopen (filename, "a+");
  102. times ();
  103. buffer [cont-1]='\0';
  104. printf ("RECV %d bytes: %s ", cont-2, buffer);
  105. fprintf (log, "RECV %d bytes: %s\n", cont-2, buffer);
  106.  
  107. cont=recv (newsocket, &buffer, 1024, 0);
  108. }
  109. log=fopen (filename, "a+");
  110. times ();
  111. printf ("Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  112. fprintf (log, "Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  113. close (newsocket);
  114. }
  115. close (socketfd);
  116. return 0;
  117. }
  118.  

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