option.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef OPTION_H_
  15. #define OPTION_H_
  16. #include <string>
  17. #include <map>
  18. #include <boost/shared_array.hpp>
  19. namespace isc {
  20. namespace dhcp {
  21. class Option {
  22. public:
  23. enum Universe { V4, V6 };
  24. typedef std::map<unsigned int, boost::shared_ptr<Option> > Option4Lst;
  25. typedef std::multimap<unsigned int, boost::shared_ptr<Option> > Option6Lst;
  26. typedef boost::shared_ptr<Option> Factory(Option::Universe u,
  27. unsigned short type,
  28. boost::shared_array<char> buf,
  29. unsigned int offset,
  30. unsigned int len);
  31. // ctor, used for options constructed, usually during transmission
  32. Option(Universe u, unsigned short type);
  33. // ctor, used for received options
  34. // boost::shared_array allows sharing a buffer, but it requires that
  35. // different instances share pointer to the whole array, not point
  36. // to different elements in shared array. Therefore we need to share
  37. // pointer to the whole array and remember offset where data for
  38. // this option begins
  39. Option(Universe u, unsigned short type, boost::shared_array<char> buf,
  40. unsigned int offset,
  41. unsigned int len);
  42. // writes option in wire-format to buf, returns pointer to first unused
  43. // byte after stored option
  44. virtual unsigned int
  45. pack(boost::shared_array<char> buf,
  46. unsigned int buf_len,
  47. unsigned int offset);
  48. // parses received buffer, returns pointer to first unused byte
  49. // after parsed option
  50. // TODO: Do we need this overload? Commented out for now
  51. // virtual const char* unpack(const char* buf, unsigned int len);
  52. // parses received buffer, returns offset to the first unused byte after
  53. // parsed option
  54. virtual unsigned int
  55. unpack(boost::shared_array<char> buf,
  56. unsigned int buf_len,
  57. unsigned int offset,
  58. unsigned int parse_len);
  59. virtual std::string toText();
  60. unsigned short getType();
  61. // returns length of the complete option (data length + DHCPv4/DHCPv6 option header)
  62. virtual unsigned short len();
  63. // returns if option is valid (e.g. option may be truncated)
  64. virtual bool valid();
  65. void addOption(boost::shared_ptr<Option> opt);
  66. // just to force that every option has virtual dtor
  67. virtual ~Option();
  68. protected:
  69. virtual unsigned int
  70. pack4(boost::shared_array<char> buf,
  71. unsigned int buf_len,
  72. unsigned int offset);
  73. virtual unsigned int
  74. pack6(boost::shared_array<char> buf,
  75. unsigned int buf_len,
  76. unsigned int offset);
  77. virtual unsigned int unpack4(boost::shared_array<char> buf,
  78. unsigned int buf_len,
  79. unsigned int offset,
  80. unsigned int parse_len);
  81. virtual unsigned int unpack6(boost::shared_array<char> buf,
  82. unsigned int buf_len,
  83. unsigned int offset,
  84. unsigned int parse_len);
  85. Universe universe_;
  86. unsigned short type_;
  87. boost::shared_array<char> data_;
  88. unsigned int data_len_; // length of data only. Use len() if you want to know
  89. // proper length with option header overhead
  90. unsigned int offset_; // data is a shared_pointer that points out to the
  91. // whole packet. offset_ specifies where data for
  92. // this option begins.
  93. char * value_;
  94. // 2 different containers are used, because v4 options are unique
  95. // and v6 allows multiple instances of the same option types
  96. // originally 2 separate containers were planned. Let's try if we
  97. // can use a single apporach
  98. Option6Lst optionLst_;
  99. };
  100. } // namespace isc::dhcp
  101. } // namespace isc
  102. #endif