dhcp4_srv.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2011 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. #include <dhcp/dhcp4.h>
  15. #include <dhcp/pkt4.h>
  16. #include <dhcp/iface_mgr.h>
  17. #include <dhcp4/dhcp4_srv.h>
  18. #include <asiolink/io_address.h>
  19. using namespace std;
  20. using namespace isc;
  21. using namespace isc::dhcp;
  22. using namespace isc::asiolink;
  23. Dhcpv4Srv::Dhcpv4Srv(uint16_t port) {
  24. cout << "Initialization: opening sockets on port " << port << endl;
  25. // first call to instance() will create IfaceMgr (it's a singleton)
  26. // it may throw something if things go wrong
  27. IfaceMgr::instance();
  28. /// @todo: instantiate LeaseMgr here once it is imlpemented.
  29. setServerID();
  30. shutdown_ = false;
  31. }
  32. Dhcpv4Srv::~Dhcpv4Srv() {
  33. cout << "DHCPv4 server shutdown." << endl;
  34. }
  35. bool
  36. Dhcpv4Srv::run() {
  37. while (!shutdown_) {
  38. boost::shared_ptr<Pkt4> query; // client's message
  39. boost::shared_ptr<Pkt4> rsp; // server's response
  40. #if 0
  41. // uncomment this once ticket 1239 is merged.
  42. query = IfaceMgr::instance().receive4();
  43. #endif
  44. if (query) {
  45. if (!query->unpack()) {
  46. cout << "Failed to parse incoming packet" << endl;
  47. continue;
  48. }
  49. switch (query->getType()) {
  50. case DHCPDISCOVER:
  51. rsp = processDiscover(query);
  52. break;
  53. case DHCPREQUEST:
  54. rsp = processRequest(query);
  55. break;
  56. case DHCPRELEASE:
  57. processRelease(query);
  58. break;
  59. case DHCPDECLINE:
  60. processDecline(query);
  61. break;
  62. case DHCPINFORM:
  63. processInform(query);
  64. break;
  65. default:
  66. cout << "Unknown pkt type received:"
  67. << query->getType() << endl;
  68. }
  69. cout << "Received " << query->len() << " bytes packet type="
  70. << query->getType() << endl;
  71. // TODO: print out received packets only if verbose (or debug)
  72. // mode is enabled
  73. cout << query->toText();
  74. if (rsp) {
  75. rsp->setRemoteAddr(query->getRemoteAddr());
  76. rsp->setLocalAddr(query->getLocalAddr());
  77. rsp->setRemotePort(DHCP4_CLIENT_PORT);
  78. rsp->setLocalPort(DHCP4_SERVER_PORT);
  79. rsp->setIface(query->getIface());
  80. rsp->setIndex(query->getIndex());
  81. cout << "Replying with:" << rsp->getType() << endl;
  82. cout << rsp->toText();
  83. cout << "----" << endl;
  84. if (rsp->pack()) {
  85. cout << "Packet assembled correctly." << endl;
  86. }
  87. #if 0
  88. // uncomment this once ticket 1240 is merged.
  89. IfaceMgr::instance().send4(rsp);
  90. #endif
  91. }
  92. }
  93. // TODO add support for config session (see src/bin/auth/main.cc)
  94. // so this daemon can be controlled from bob
  95. }
  96. return (true);
  97. }
  98. void
  99. Dhcpv4Srv::setServerID() {
  100. /// TODO implement this for real once interface detection (ticket 1237)
  101. /// is done. Use hardcoded server-id for now.
  102. #if 0
  103. // uncomment this once ticket 1350 is merged.
  104. IOAddress srvId("127.0.0.1");
  105. serverid_ = boost::shared_ptr<Option>(
  106. new Option4AddrLst(Option::V4, DHO_DHCP_SERVER_IDENTIFIER, srvId));
  107. #endif
  108. }
  109. boost::shared_ptr<Pkt4>
  110. Dhcpv4Srv::processDiscover(boost::shared_ptr<Pkt4>& discover) {
  111. /// TODO: Currently implemented echo mode. Implement this for real
  112. return (discover);
  113. }
  114. boost::shared_ptr<Pkt4>
  115. Dhcpv4Srv::processRequest(boost::shared_ptr<Pkt4>& request) {
  116. /// TODO: Currently implemented echo mode. Implement this for real
  117. return (request);
  118. }
  119. void Dhcpv4Srv::processRelease(boost::shared_ptr<Pkt4>& release) {
  120. /// TODO: Implement this.
  121. cout << "Received RELEASE on " << release->getIface() << " interface." << endl;
  122. }
  123. void Dhcpv4Srv::processDecline(boost::shared_ptr<Pkt4>& decline) {
  124. /// TODO: Implement this.
  125. cout << "Received DECLINE on " << decline->getIface() << " interface." << endl;
  126. }
  127. boost::shared_ptr<Pkt4> Dhcpv4Srv::processInform(boost::shared_ptr<Pkt4>& inform) {
  128. /// TODO: Currently implemented echo mode. Implement this for real
  129. return (inform);
  130. }