main.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <sys/select.h>
  18. #include <netdb.h>
  19. #include <stdlib.h>
  20. #include <set>
  21. #include <iostream>
  22. #include <dns/buffer.h>
  23. #include <dns/name.h>
  24. #include <dns/rrset.h>
  25. #include <dns/message.h>
  26. #include <cc/cpp/session.h>
  27. #include "zoneset.h"
  28. #include "parkinglot.h"
  29. #include "ccsession.h"
  30. #include "common.h"
  31. using namespace std;
  32. const string PROGRAM = "parkinglot";
  33. const int DNSPORT = 53;
  34. static void
  35. usage() {
  36. cerr << "Usage: parkinglot [-p port]" << endl;
  37. exit(1);
  38. }
  39. int
  40. main(int argc, char* argv[]) {
  41. int ch;
  42. int port = DNSPORT;
  43. while ((ch = getopt(argc, argv, "p:")) != -1) {
  44. switch (ch) {
  45. case 'p':
  46. port = atoi(optarg);
  47. break;
  48. case '?':
  49. default:
  50. usage();
  51. }
  52. }
  53. if (argc - optind > 0)
  54. usage();
  55. // initialize parking lot
  56. ParkingLot plot(port);
  57. // initialize command channel
  58. CommandSession cs;
  59. // main server loop
  60. fd_set fds;
  61. int ps = plot.getSocket();
  62. int ss = cs.getSocket();
  63. int nfds = max(ps, ss) + 1;
  64. cout << "Server started." << endl;
  65. while (true) {
  66. FD_ZERO(&fds);
  67. FD_SET(ps, &fds);
  68. FD_SET(ss, &fds);
  69. int n = select(nfds, &fds, NULL, NULL, NULL);
  70. if (n < 0)
  71. throw FatalError("select error");
  72. if (FD_ISSET(ps, &fds))
  73. plot.processMessage();
  74. if (FD_ISSET(ss, &fds)) {
  75. pair<string,string> cmd = cs.getCommand();
  76. plot.command(cmd);
  77. }
  78. }
  79. return (0);
  80. }