buffer.h 20 KB

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