buffer.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. #ifndef BUFFER_H
  15. #define BUFFER_H 1
  16. #include <stdlib.h>
  17. #include <cstring>
  18. #include <vector>
  19. #include <stdint.h>
  20. #include <exceptions/exceptions.h>
  21. #include <boost/shared_ptr.hpp>
  22. namespace isc {
  23. namespace util {
  24. ///
  25. /// \brief A standard DNS module exception that is thrown if an out-of-range
  26. /// buffer operation is being performed.
  27. ///
  28. class InvalidBufferPosition : public Exception {
  29. public:
  30. InvalidBufferPosition(const char* file, size_t line, const char* what) :
  31. isc::Exception(file, line, what) {}
  32. };
  33. ///\brief The \c InputBuffer class is a buffer abstraction for manipulating
  34. /// read-only data.
  35. ///
  36. /// The main purpose of this class is to provide a safe placeholder for
  37. /// examining wire-format data received from a network.
  38. ///
  39. /// Applications normally use this class only in a limited situation: as an
  40. /// interface between legacy I/O operation (such as receiving data from a BSD
  41. /// socket) and the rest of the Kea DNS library. One common usage of this
  42. /// class for an application would therefore be something like this:
  43. ///
  44. /// \code unsigned char buf[1024];
  45. /// struct sockaddr addr;
  46. /// socklen_t addrlen = sizeof(addr);
  47. /// int cc = recvfrom(s, buf, sizeof(buf), 0, &addr, &addrlen);
  48. /// InputBuffer buffer(buf, cc);
  49. /// // pass the buffer to a DNS message object to parse the message \endcode
  50. ///
  51. /// Other Kea DNS classes will then use methods of this class to get access
  52. /// to the data, but the application normally doesn't have to care about the
  53. /// details.
  54. ///
  55. /// An \c InputBuffer object internally holds a reference to the given data,
  56. /// rather than make a local copy of the data. Also, it does not have an
  57. /// ownership of the given data. It is application's responsibility to ensure
  58. /// the data remains valid throughout the lifetime of the \c InputBuffer
  59. /// object. Likewise, this object generally assumes the data isn't modified
  60. /// throughout its lifetime; if the application modifies the data while this
  61. /// object retains a reference to it, the result is undefined. The application
  62. /// will also be responsible for releasing the data when it's not needed if it
  63. /// was dynamically acquired.
  64. ///
  65. /// This is a deliberate design choice: although it's safer to make a local
  66. /// copy of the given data on construction, it would cause unacceptable
  67. /// performance overhead, especially considering that a DNS message can be
  68. /// as large as a few KB. Alternatively, we could allow the object to allocate
  69. /// memory internally and expose it to the application to store network data
  70. /// in it. This is also a bad design, however, in that we would effectively
  71. /// break the abstraction employed in the class, and do so by publishing
  72. /// "read-only" stuff as a writable memory region. Since there doesn't seem to
  73. /// be a perfect solution, we have adopted what we thought a "least bad" one.
  74. ///
  75. /// Methods for reading data from the buffer generally work like an input
  76. /// stream: it begins with the head of the data, and once some length of data
  77. /// is read from the buffer, the next read operation will take place from the
  78. /// head of the unread data. An object of this class internally holds (a
  79. /// notion of) where the next read operation should start. We call it the
  80. /// <em>read position</em> in this document.
  81. class InputBuffer {
  82. public:
  83. ///
  84. /// \name Constructors and Destructor
  85. //@{
  86. /// \brief Constructor from variable length of data.
  87. ///
  88. /// It is caller's responsibility to ensure that the data is valid as long
  89. /// as the buffer exists.
  90. /// \param data A pointer to the data stored in the buffer.
  91. /// \param len The length of the data in bytes.
  92. InputBuffer(const void* data, size_t len) :
  93. position_(0), data_(static_cast<const uint8_t*>(data)), len_(len) {}
  94. //@}
  95. ///
  96. /// \name Getter Methods
  97. //@{
  98. /// \brief Return the length of the data stored in the buffer.
  99. size_t getLength() const { return (len_); }
  100. /// \brief Return the current read position.
  101. size_t getPosition() const { return (position_); }
  102. //@}
  103. ///
  104. /// \name Setter Methods
  105. ///
  106. //@{
  107. /// \brief Set the read position of the buffer to the given value.
  108. ///
  109. /// The new position must be in the valid range of the buffer; otherwise
  110. /// an exception of class \c isc::dns::InvalidBufferPosition will be thrown.
  111. /// \param position The new position (offset from the beginning of the
  112. /// buffer).
  113. void setPosition(size_t position) {
  114. if (position > len_) {
  115. throwError("position is too large");
  116. }
  117. position_ = position;
  118. }
  119. //@}
  120. ///
  121. /// \name Methods for reading data from the buffer.
  122. //@{
  123. /// \brief Read an unsigned 8-bit integer from the buffer and return it.
  124. ///
  125. /// If the remaining length of the buffer is smaller than 8-bit, an
  126. /// exception of class \c isc::dns::InvalidBufferPosition will be thrown.
  127. uint8_t readUint8() {
  128. if (position_ + sizeof(uint8_t) > len_) {
  129. throwError("read beyond end of buffer");
  130. }
  131. return (data_[position_++]);
  132. }
  133. /// \brief Read an unsigned 16-bit integer in network byte order from the
  134. /// buffer, convert it to host byte order, and return it.
  135. ///
  136. /// If the remaining length of the buffer is smaller than 16-bit, an
  137. /// exception of class \c isc::dns::InvalidBufferPosition will be thrown.
  138. uint16_t readUint16() {
  139. uint16_t data;
  140. const uint8_t* cp;
  141. if (position_ + sizeof(data) > len_) {
  142. throwError("read beyond end of buffer");
  143. }
  144. cp = &data_[position_];
  145. data = ((unsigned int)(cp[0])) << 8;
  146. data |= ((unsigned int)(cp[1]));
  147. position_ += sizeof(data);
  148. return (data);
  149. }
  150. /// \brief Read an unsigned 32-bit integer in network byte order from the
  151. /// buffer, convert it to host byte order, and return it.
  152. ///
  153. /// If the remaining length of the buffer is smaller than 32-bit, an
  154. /// exception of class \c isc::dns::InvalidBufferPosition will be thrown.
  155. uint32_t readUint32() {
  156. uint32_t data;
  157. const uint8_t* cp;
  158. if (position_ + sizeof(data) > len_) {
  159. throwError("read beyond end of buffer");
  160. }
  161. cp = &data_[position_];
  162. data = ((unsigned int)(cp[0])) << 24;
  163. data |= ((unsigned int)(cp[1])) << 16;
  164. data |= ((unsigned int)(cp[2])) << 8;
  165. data |= ((unsigned int)(cp[3]));
  166. position_ += sizeof(data);
  167. return (data);
  168. }
  169. /// \brief Read data of the specified length from the buffer and copy it to
  170. /// the caller supplied buffer.
  171. ///
  172. /// The data is copied as stored in the buffer; no conversion is performed.
  173. /// If the remaining length of the buffer is smaller than the specified
  174. /// length, an exception of class \c isc::dns::InvalidBufferPosition will
  175. /// be thrown.
  176. void readData(void* data, size_t len) {
  177. if (position_ + len > len_) {
  178. throwError("read beyond end of buffer");
  179. }
  180. std::memcpy(data, &data_[position_], len);
  181. position_ += len;
  182. }
  183. //@}
  184. /// @brief Read specified number of bytes as a vector.
  185. ///
  186. /// If specified buffer is too short, it will be expanded
  187. /// using vector::resize() method.
  188. ///
  189. /// @param data Reference to a buffer (data will be stored there).
  190. /// @param len Size specified number of bytes to read in a vector.
  191. ///
  192. void readVector(std::vector<uint8_t>& data, size_t len) {
  193. if (position_ + len > len_) {
  194. throwError("read beyond end of buffer");
  195. }
  196. data.resize(len);
  197. readData(&data[0], len);
  198. }
  199. private:
  200. /// \brief A common helper to throw an exception on invalid operation.
  201. ///
  202. /// Experiments showed that throwing from each method makes the buffer
  203. /// operation slower, so we consolidate it here, and let the methods
  204. /// call this.
  205. static void throwError(const char* msg) {
  206. isc_throw(InvalidBufferPosition, msg);
  207. }
  208. size_t position_;
  209. // XXX: The following must be private, but for a short term workaround with
  210. // Boost.Python binding, we changed it to protected. We should soon
  211. // revisit it.
  212. protected:
  213. const uint8_t* data_;
  214. size_t len_;
  215. };
  216. ///
  217. ///\brief The \c OutputBuffer class is a buffer abstraction for manipulating
  218. /// mutable data.
  219. ///
  220. /// The main purpose of this class is to provide a safe workplace for
  221. /// constructing wire-format data to be sent out to a network. Here,
  222. /// <em>safe</em> means that it automatically allocates necessary memory and
  223. /// avoid buffer overrun.
  224. ///
  225. /// Like for the \c InputBuffer class, applications normally use this class only
  226. /// in a limited situation. One common usage of this class for an application
  227. /// would be something like this:
  228. ///
  229. /// \code OutputBuffer buffer(4096); // give a sufficiently large initial size
  230. /// // pass the buffer to a DNS message object to construct a wire-format
  231. /// // DNS message.
  232. /// struct sockaddr to;
  233. /// sendto(s, buffer.getData(), buffer.getLength(), 0, &to, sizeof(to));
  234. /// \endcode
  235. ///
  236. /// where the \c getData() method gives a reference to the internal memory
  237. /// region stored in the \c buffer object. This is a suboptimal design in that
  238. /// it exposes an encapsulated "handle" of an object to its user.
  239. /// Unfortunately, there is no easy way to avoid this without involving
  240. /// expensive data copy if we want to use this object with a legacy API such as
  241. /// a BSD socket interface. And, indeed, this is one major purpose for this
  242. /// object. Applications should use this method only under such a special
  243. /// circumstance. It should also be noted that the memory region returned by
  244. /// \c getData() may be invalidated after a subsequent write operation.
  245. ///
  246. /// An \c OutputBuffer class object automatically extends its memory region when
  247. /// data is written beyond the end of the current buffer. However, it will
  248. /// involve performance overhead such as reallocating more memory and copying
  249. /// data. It is therefore recommended to construct the buffer object with a
  250. /// sufficiently large initial size.
  251. /// The \c getCapacity() method provides the current maximum size of data
  252. /// (including the portion already written) that can be written into the buffer
  253. /// without causing memory reallocation.
  254. ///
  255. /// Methods for writing data into the buffer generally work like an output
  256. /// stream: it begins with the head of the buffer, and once some length of data
  257. /// is written into the buffer, the next write operation will take place from
  258. /// the end of the buffer. Other methods to emulate "random access" are also
  259. /// provided (e.g., \c writeUint16At()). The normal write operations are
  260. /// normally exception-free as this class automatically extends the buffer
  261. /// when necessary. However, in extreme cases such as an attempt of writing
  262. /// multi-GB data, a separate exception (e.g., \c std::bad_alloc) may be thrown
  263. /// by the system. This also applies to the constructor with a very large
  264. /// initial size.
  265. ///
  266. /// Note to developers: it may make more sense to introduce an abstract base
  267. /// class for the \c OutputBuffer and define the simple implementation as a
  268. /// a concrete derived class. That way we can provide flexibility for future
  269. /// extension such as more efficient buffer implementation or allowing users
  270. /// to have their own customized version without modifying the source code.
  271. /// We in fact considered that option, but at the moment chose the simpler
  272. /// approach with a single concrete class because it may make the
  273. /// implementation unnecessarily complicated while we were still not certain
  274. /// if we really want that flexibility. We may revisit the class design as
  275. /// we see more applications of the class. The same considerations apply to
  276. /// the \c InputBuffer and \c MessageRenderer classes.
  277. class OutputBuffer {
  278. public:
  279. ///
  280. /// \name Constructors and Destructor
  281. ///
  282. //@{
  283. /// \brief Constructor from the initial size of the buffer.
  284. ///
  285. /// \param len The initial length of the buffer in bytes.
  286. OutputBuffer(size_t len) :
  287. buffer_(NULL),
  288. size_(0),
  289. allocated_(len)
  290. {
  291. // We use malloc and free instead of C++ new[] and delete[].
  292. // This way we can use realloc, which may in fact do it without a copy.
  293. buffer_ = static_cast<uint8_t*>(malloc(allocated_));
  294. if (buffer_ == NULL && len != 0) {
  295. throw std::bad_alloc();
  296. }
  297. }
  298. /// \brief Copy constructor
  299. OutputBuffer(const OutputBuffer& other) :
  300. buffer_(NULL),
  301. size_(other.size_),
  302. allocated_(other.allocated_)
  303. {
  304. buffer_ = static_cast<uint8_t*>(malloc(allocated_));
  305. if (buffer_ == NULL && allocated_ != 0) {
  306. throw std::bad_alloc();
  307. }
  308. std::memcpy(buffer_, other.buffer_, size_);
  309. }
  310. /// \brief Destructor
  311. ~ OutputBuffer() {
  312. free(buffer_);
  313. }
  314. //@}
  315. /// \brief Assignment operator
  316. OutputBuffer& operator =(const OutputBuffer& other) {
  317. if (this != &other) {
  318. uint8_t* newbuff(static_cast<uint8_t*>(malloc(other.allocated_)));
  319. if (newbuff == NULL && other.allocated_ != 0) {
  320. throw std::bad_alloc();
  321. }
  322. free(buffer_);
  323. buffer_ = newbuff;
  324. size_ = other.size_;
  325. allocated_ = other.allocated_;
  326. std::memcpy(buffer_, other.buffer_, size_);
  327. }
  328. return (*this);
  329. }
  330. ///
  331. /// \name Getter Methods
  332. ///
  333. //@{
  334. /// \brief Return the current capacity of the buffer.
  335. size_t getCapacity() const { return (allocated_); }
  336. /// \brief Return a pointer to the head of the data stored in the buffer.
  337. ///
  338. /// The caller can assume that the subsequent \c getLength() bytes are
  339. /// identical to the stored data of the buffer.
  340. ///
  341. /// Note: The pointer returned by this method may be invalidated after a
  342. /// subsequent write operation.
  343. const void* getData() const { return (buffer_); }
  344. /// \brief Return the length of data written in the buffer.
  345. size_t getLength() const { return (size_); }
  346. /// \brief Return the value of the buffer at the specified position.
  347. ///
  348. /// \c pos must specify the valid position of the buffer; otherwise an
  349. /// exception class of \c InvalidBufferPosition will be thrown.
  350. ///
  351. /// \param pos The position in the buffer to be returned.
  352. uint8_t operator[](size_t pos) const
  353. {
  354. assert (pos < size_);
  355. return (buffer_[pos]);
  356. }
  357. //@}
  358. ///
  359. /// \name Methods for writing data into the buffer.
  360. ///
  361. //@{
  362. /// \brief Insert a specified length of gap at the end of the buffer.
  363. ///
  364. /// The caller should not assume any particular value to be inserted.
  365. /// This method is provided as a shortcut to make a hole in the buffer
  366. /// that is to be filled in later, e.g, by \ref writeUint16At().
  367. /// \param len The length of the gap to be inserted in bytes.
  368. void skip(size_t len) {
  369. ensureAllocated(size_ + len);
  370. size_ += len;
  371. }
  372. /// \brief Trim the specified length of data from the end of the buffer.
  373. ///
  374. /// The specified length must not exceed the current data size of the
  375. /// buffer; otherwise an exception of class \c isc::OutOfRange will
  376. /// be thrown.
  377. ///
  378. /// \param len The length of data that should be trimmed.
  379. void trim(size_t len)
  380. {
  381. if (len > size_) {
  382. isc_throw(OutOfRange, "trimming too large from output buffer");
  383. }
  384. size_ -= len;
  385. }
  386. /// \brief Clear buffer content.
  387. ///
  388. /// This method can be used to re-initialize and reuse the buffer without
  389. /// constructing a new one.
  390. void clear() { size_ = 0; }
  391. /// \brief Write an unsigned 8-bit integer into the buffer.
  392. ///
  393. /// \param data The 8-bit integer to be written into the buffer.
  394. void writeUint8(uint8_t data) {
  395. ensureAllocated(size_ + 1);
  396. buffer_[size_ ++] = data;
  397. }
  398. /// \brief Write an unsigned 8-bit integer into the buffer.
  399. ///
  400. /// The position must be lower than the size of the buffer,
  401. /// otherwise an exception of class \c isc::dns::InvalidBufferPosition
  402. /// will be thrown.
  403. ///
  404. /// \param data The 8-bit integer to be written into the buffer.
  405. /// \param pos The position in the buffer to write the data.
  406. void writeUint8At(uint8_t data, size_t pos) {
  407. if (pos + sizeof(data) > size_) {
  408. isc_throw(InvalidBufferPosition, "write at invalid position");
  409. }
  410. buffer_[pos] = data;
  411. }
  412. /// \brief Write an unsigned 16-bit integer in host byte order into the
  413. /// buffer in network byte order.
  414. ///
  415. /// \param data The 16-bit integer to be written into the buffer.
  416. void writeUint16(uint16_t data)
  417. {
  418. ensureAllocated(size_ + sizeof(data));
  419. buffer_[size_ ++] = static_cast<uint8_t>((data & 0xff00U) >> 8);
  420. buffer_[size_ ++] = static_cast<uint8_t>(data & 0x00ffU);
  421. }
  422. /// \brief Write an unsigned 16-bit integer in host byte order at the
  423. /// specified position of the buffer in network byte order.
  424. ///
  425. /// The buffer must have a sufficient room to store the given data at the
  426. /// given position, that is, <code>pos + 2 < getLength()</code>;
  427. /// otherwise an exception of class \c isc::dns::InvalidBufferPosition will
  428. /// be thrown.
  429. /// Note also that this method never extends the buffer.
  430. ///
  431. /// \param data The 16-bit integer to be written into the buffer.
  432. /// \param pos The beginning position in the buffer to write the data.
  433. void writeUint16At(uint16_t data, size_t pos)
  434. {
  435. if (pos + sizeof(data) > size_) {
  436. isc_throw(InvalidBufferPosition, "write at invalid position");
  437. }
  438. buffer_[pos] = static_cast<uint8_t>((data & 0xff00U) >> 8);
  439. buffer_[pos + 1] = static_cast<uint8_t>(data & 0x00ffU);
  440. }
  441. /// \brief Write an unsigned 32-bit integer in host byte order
  442. /// into the buffer in network byte order.
  443. ///
  444. /// \param data The 32-bit integer to be written into the buffer.
  445. void writeUint32(uint32_t data)
  446. {
  447. ensureAllocated(size_ + sizeof(data));
  448. buffer_[size_ ++] = static_cast<uint8_t>((data & 0xff000000) >> 24);
  449. buffer_[size_ ++] = static_cast<uint8_t>((data & 0x00ff0000) >> 16);
  450. buffer_[size_ ++] = static_cast<uint8_t>((data & 0x0000ff00) >> 8);
  451. buffer_[size_ ++] = static_cast<uint8_t>(data & 0x000000ff);
  452. }
  453. /// \brief Copy an arbitrary length of data into the buffer.
  454. ///
  455. /// No conversion on the copied data is performed.
  456. ///
  457. /// \param data A pointer to the data to be copied into the buffer.
  458. /// \param len The length of the data in bytes.
  459. void writeData(const void *data, size_t len)
  460. {
  461. ensureAllocated(size_ + len);
  462. std::memcpy(buffer_ + size_, data, len);
  463. size_ += len;
  464. }
  465. //@}
  466. private:
  467. // The actual data
  468. uint8_t* buffer_;
  469. // How many bytes are used
  470. size_t size_;
  471. // How many bytes do we have preallocated (eg. the capacity)
  472. size_t allocated_;
  473. // Make sure at last needed_size bytes are allocated in the buffer
  474. void ensureAllocated(size_t needed_size) {
  475. if (allocated_ < needed_size) {
  476. // Guess some bigger size
  477. size_t new_size = (allocated_ == 0) ? 1024 : allocated_;
  478. while (new_size < needed_size) {
  479. new_size *= 2;
  480. }
  481. // Allocate bigger space
  482. uint8_t* new_buffer_(static_cast<uint8_t*>(realloc(buffer_,
  483. new_size)));
  484. if (new_buffer_ == NULL) {
  485. // If it fails, the original block is left intact by it
  486. throw std::bad_alloc();
  487. }
  488. buffer_ = new_buffer_;
  489. allocated_ = new_size;
  490. }
  491. }
  492. };
  493. /// \brief Pointer-like types pointing to \c InputBuffer or \c OutputBuffer
  494. ///
  495. /// These types are expected to be used as an argument in asynchronous
  496. /// callback functions. The internal reference-counting will ensure that
  497. /// that ongoing state information will not be lost if the object
  498. /// that originated the asynchronous call falls out of scope.
  499. typedef boost::shared_ptr<InputBuffer> InputBufferPtr;
  500. typedef boost::shared_ptr<OutputBuffer> OutputBufferPtr;
  501. } // namespace util
  502. } // namespace isc
  503. #endif // BUFFER_H
  504. // Local Variables:
  505. // mode: c++
  506. // End: