pkt.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2014 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 <utility>
  15. #include <dhcp/pkt.h>
  16. namespace isc {
  17. namespace dhcp {
  18. Pkt::Pkt(uint32_t transid, const isc::asiolink::IOAddress& local_addr,
  19. const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
  20. uint16_t remote_port)
  21. :transid_(transid),
  22. iface_(""),
  23. ifindex_(-1),
  24. local_addr_(local_addr),
  25. remote_addr_(remote_addr),
  26. local_port_(local_port),
  27. remote_port_(remote_port),
  28. buffer_out_(0)
  29. {
  30. }
  31. Pkt::Pkt(const uint8_t* buf, uint32_t len, const isc::asiolink::IOAddress& local_addr,
  32. const isc::asiolink::IOAddress& remote_addr, uint16_t local_port,
  33. uint16_t remote_port)
  34. :transid_(0),
  35. iface_(""),
  36. ifindex_(-1),
  37. local_addr_(local_addr),
  38. remote_addr_(remote_addr),
  39. local_port_(local_port),
  40. remote_port_(remote_port),
  41. buffer_out_(0)
  42. {
  43. data_.resize(len);
  44. memcpy(&data_[0], buf, len);
  45. }
  46. void
  47. Pkt::addOption(const OptionPtr& opt) {
  48. options_.insert(std::pair<int, OptionPtr>(opt->getType(), opt));
  49. }
  50. OptionPtr
  51. Pkt::getOption(uint16_t type) const {
  52. OptionCollection::const_iterator x = options_.find(type);
  53. if (x != options_.end()) {
  54. return (*x).second;
  55. }
  56. return (OptionPtr()); // NULL
  57. }
  58. bool
  59. Pkt::delOption(uint16_t type) {
  60. isc::dhcp::OptionCollection::iterator x = options_.find(type);
  61. if (x!=options_.end()) {
  62. options_.erase(x);
  63. return (true); // delete successful
  64. } else {
  65. return (false); // can't find option to be deleted
  66. }
  67. }
  68. bool
  69. Pkt::inClass(const std::string& client_class) {
  70. return (classes_.find(client_class) != classes_.end());
  71. }
  72. void
  73. Pkt::addClass(const std::string& client_class) {
  74. if (classes_.find(client_class) == classes_.end()) {
  75. classes_.insert(client_class);
  76. }
  77. }
  78. void
  79. Pkt::updateTimestamp() {
  80. timestamp_ = boost::posix_time::microsec_clock::universal_time();
  81. }
  82. void Pkt::repack() {
  83. if (!data_.empty()) {
  84. buffer_out_.writeData(&data_[0], data_.size());
  85. }
  86. }
  87. void
  88. Pkt::setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
  89. const std::vector<uint8_t>& hw_addr) {
  90. setHWAddrMember(htype, hlen, hw_addr, remote_hwaddr_);
  91. }
  92. void
  93. Pkt::setRemoteHWAddr(const HWAddrPtr& hw_addr) {
  94. if (!hw_addr) {
  95. isc_throw(BadValue, "Setting remote HW address to NULL is"
  96. << " forbidden.");
  97. }
  98. remote_hwaddr_ = hw_addr;
  99. }
  100. void
  101. Pkt::setHWAddrMember(const uint8_t htype, const uint8_t,
  102. const std::vector<uint8_t>& mac_addr,
  103. HWAddrPtr& hw_addr) {
  104. hw_addr.reset(new HWAddr(mac_addr, htype));
  105. }
  106. HWAddrPtr
  107. Pkt::getMAC(uint32_t hw_addr_src) {
  108. HWAddrPtr mac;
  109. if (hw_addr_src & MAC_SOURCE_RAW) {
  110. mac = getRemoteHWAddr();
  111. if (mac) {
  112. return (mac);
  113. } else if (hw_addr_src == MAC_SOURCE_RAW) {
  114. // If we're interested only in RAW sockets as source of that info,
  115. // there's no point in trying other options.
  116. return (HWAddrPtr());
  117. }
  118. }
  119. /// @todo: add other MAC acquisition methods here
  120. // Ok, none of the methods were suitable. Return NULL.
  121. return (HWAddrPtr());
  122. }
  123. };
  124. };