pkt.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. OptionPtr
  47. Pkt::getOption(uint16_t type) const {
  48. OptionCollection::const_iterator x = options_.find(type);
  49. if (x != options_.end()) {
  50. return (*x).second;
  51. }
  52. return (OptionPtr()); // NULL
  53. }
  54. bool
  55. Pkt::delOption(uint16_t type) {
  56. isc::dhcp::OptionCollection::iterator x = options_.find(type);
  57. if (x!=options_.end()) {
  58. options_.erase(x);
  59. return (true); // delete successful
  60. }
  61. return (false); // can't find option to be deleted
  62. }
  63. bool
  64. Pkt::inClass(const std::string& client_class) {
  65. return (classes_.find(client_class) != classes_.end());
  66. }
  67. void
  68. Pkt::addClass(const std::string& client_class) {
  69. if (classes_.find(client_class) == classes_.end()) {
  70. classes_.insert(client_class);
  71. }
  72. }
  73. void
  74. Pkt::updateTimestamp() {
  75. timestamp_ = boost::posix_time::microsec_clock::universal_time();
  76. }
  77. void Pkt::repack() {
  78. if (!data_.empty()) {
  79. buffer_out_.writeData(&data_[0], data_.size());
  80. }
  81. }
  82. void
  83. Pkt::setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
  84. const std::vector<uint8_t>& mac_addr) {
  85. setHWAddrMember(htype, hlen, mac_addr, remote_hwaddr_);
  86. }
  87. void
  88. Pkt::setRemoteHWAddr(const HWAddrPtr& addr) {
  89. if (!addr) {
  90. isc_throw(BadValue, "Setting remote HW address to NULL is"
  91. << " forbidden.");
  92. }
  93. remote_hwaddr_ = addr;
  94. }
  95. void
  96. Pkt::setHWAddrMember(const uint8_t htype, const uint8_t,
  97. const std::vector<uint8_t>& mac_addr,
  98. HWAddrPtr& hw_addr) {
  99. hw_addr.reset(new HWAddr(mac_addr, htype));
  100. }
  101. HWAddrPtr
  102. Pkt::getMAC(uint32_t hw_addr_src) {
  103. HWAddrPtr mac;
  104. if (hw_addr_src & MAC_SOURCE_RAW) {
  105. mac = getRemoteHWAddr();
  106. if (mac) {
  107. return (mac);
  108. } else if (hw_addr_src == MAC_SOURCE_RAW) {
  109. // If we're interested only in RAW sockets as source of that info,
  110. // there's no point in trying other options.
  111. return (HWAddrPtr());
  112. }
  113. }
  114. /// @todo: add other MAC acquisition methods here
  115. // Ok, none of the methods were suitable. Return NULL.
  116. return (HWAddrPtr());
  117. }
  118. };
  119. };