main.C 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * main.cpp
  3. *
  4. * Created on: 2011-05-20
  5. * Author: xaxaxa
  6. */
  7. /*
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * */
  19. #include <assert.h>
  20. #include "include/socketd_internal.H"
  21. #include "include/configparser.H"
  22. #include <cpoll/cpoll.H>
  23. #include <tuple>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <sys/mman.h>
  28. #include <stdexcept>
  29. using namespace RGC;
  30. using namespace socketd;
  31. using namespace std;
  32. void listenthread();
  33. tuple<const char*, int> mapFile(const char* path) {
  34. struct stat st;
  35. if (stat(path, &st) != 0) {
  36. if (errno == ENOENT) return make_tuple((const char*) NULL, 0);
  37. else throw runtime_error(strerror(errno));
  38. }
  39. if (st.st_size == 0) return make_tuple((const char*) 1, 0);
  40. int fd = open(path, O_RDONLY);
  41. if (fd < 0) throw runtime_error(strerror(errno));
  42. void* tmp = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  43. if (tmp == NULL) throw runtime_error(strerror(errno));
  44. return make_tuple((const char*) tmp, st.st_size);
  45. }
  46. socketd::socketd sd;
  47. #define PRINTSIZE(x) printf("sizeof("#x") = %i\n",sizeof(x))
  48. int main(int argc, char** argv) {
  49. if (argc < 2) {
  50. printf("usage: %s socketd.conf\n", argv[0]);
  51. return 1;
  52. }
  53. {
  54. const char* confPath = argv[1];
  55. tuple<const char*, int> conf = mapFile(confPath);
  56. if (get < 0 > (conf) == NULL) {
  57. printf("config file not found: %s\n", strerror(errno));
  58. return 1;
  59. }
  60. loadConfig(get < 0 > (conf), get < 1 > (conf), sd);
  61. munmap((void*)get < 0 > (conf), get < 1 > (conf));
  62. }
  63. /*
  64. sd.listens.push_back( { "0.0.0.0", "16969", 1, 32 });
  65. sd.vhosts.push_back( { { { 0, "/asdf", "", binding::match_httpPath } }, "vhost1",
  66. "/home/xaxaxa/workspace/test/socketd_test", "", true });
  67. sd.vhosts.push_back( { { { 0, "/zxcv", "", binding::match_httpPath } }, "vhost2",
  68. "/home/xaxaxa/workspace/test/socketd_test", "", false });
  69. sd.vhosts.push_back( { { { 0, "/zzz", "", binding::match_httpPath } }, "vhost3",
  70. "exec nc -l 0.0.0.0 12345 < /dev/urandom", "", true });
  71. sd.vhosts.push_back( { { { 0, "/sss", "", binding::match_httpPath } }, "vhost4",
  72. "lighttpd -D -f /home/xaxaxa/workspace/test/lighttpd.conf", "", true });*/
  73. //sd.vhosts.push_back({{{1,"","",binding::match_listenID}},"vhost1","/home/xaxaxa/workspace/test/socketd_test",""});
  74. PRINTSIZE(socketd::socketd);
  75. PRINTSIZE(socketd::vhost);
  76. PRINTSIZE(socketd::listen);
  77. sd.run();
  78. }
  79. void listenthread() {
  80. //CP::Poll p;
  81. /*config::rtconfigmanager *c=config::rtconfigmanager::getmainconfig();
  82. SocketManager m;
  83. int i;
  84. for(i=0;i<c->listens_c;i++)
  85. {
  86. Socket s(AF_INET,SOCK_STREAM|SOCK_CLOEXEC,0);
  87. s.Bind(c->listens[i].ep);
  88. s.Listen(c->listens[i].backlog);
  89. m.BeginAccept(s,SocketManager::Callback(cb1,NULL));
  90. }
  91. m.EventLoop();*/
  92. }