main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /***************************************************************************
  2. description
  3. -------------------
  4. author : (C) 2004 by Michael J. Flickinger
  5. email : mjflick@cpan.org
  6. ***************************************************************************/
  7. /***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #include <sys/wait.h>
  25. #include <signal.h>
  26. #define BLOG 10
  27. char *header = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n";
  28. char recvin[500], password[25];
  29. int s_port;
  30. void sigchld_handler(int s)
  31. {
  32. while(wait(NULL) > 0);
  33. }
  34. int main(int argc, char **argv)
  35. {
  36. if (argc < 3)
  37. {
  38. printf("eAthena Web Server\n");
  39. printf("usage: %s [password] [port]\n", argv[0]);
  40. exit(0);
  41. }
  42. s_port = atoi(argv[2]);
  43. if ((s_port < 1) || (s_port > 65534))
  44. {
  45. printf("Error: The port you choose is not valid port.\n");
  46. exit(0);
  47. }
  48. if (strlen(argv[1]) > 25)
  49. {
  50. printf("Error: Your password is too long.\n");
  51. printf("It must be shorter than 25 characters.\n");
  52. exit(0);
  53. }
  54. memset(password, 0x0, 25);
  55. memcpy(password, argv[1], strlen(argv[1]));
  56. int sockfd, new_fd;
  57. struct sockaddr_in my_addr;
  58. struct sockaddr_in their_addr;
  59. int sin_size;
  60. struct sigaction sa;
  61. int yes=1;
  62. if ((sockfd = socket(AF_INET, SOCK_STREAM,0)) == -1)
  63. {
  64. perror("Darn, this is broken.");
  65. exit(0);
  66. }
  67. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  68. {
  69. perror("Error... :-(");
  70. }
  71. //Now we know we have a working socket. :-)
  72. my_addr.sin_family = AF_INET;
  73. my_addr.sin_port = htons(s_port);
  74. my_addr.sin_addr.s_addr = INADDR_ANY;
  75. memset(&(my_addr.sin_zero), '\0', 8);
  76. if ( bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
  77. {
  78. perror("can not bind to this port");
  79. exit(0);
  80. }
  81. if ( listen(sockfd, BLOG) == -1)
  82. {
  83. perror("can not listen on port");
  84. exit(0);
  85. }
  86. sa.sa_handler = sigchld_handler;
  87. sigemptyset(&sa.sa_mask);
  88. sa.sa_flags = SA_RESTART;
  89. if (sigaction(SIGCHLD, &sa, NULL) == -1)
  90. {
  91. perror("sigaction sucks");
  92. exit(0);
  93. }
  94. printf("The eAthena webserver is up and listening on port %i.\n", s_port);
  95. while(1)
  96. {
  97. sin_size = sizeof(struct sockaddr_in);
  98. new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
  99. if (!fork())
  100. {
  101. close(sockfd);
  102. memset(recvin, 0x0, 500);
  103. recv(new_fd, recvin, 500, 0);
  104. send(new_fd, header, strlen(header), 0);
  105. generate_page(password, new_fd, get_query(recvin), inet_ntoa(their_addr.sin_addr));
  106. log_visit(get_query(recvin), inet_ntoa(their_addr.sin_addr));
  107. close(new_fd);
  108. exit(0);
  109. }
  110. close(new_fd);
  111. }
  112. return 0;
  113. }