netbuff.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/err.h>
  19. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/net/netbuff.h>
  22. grub_err_t
  23. grub_netbuff_put (struct grub_net_buff *nb, grub_size_t len)
  24. {
  25. nb->tail += len;
  26. if (nb->tail > nb->end)
  27. return grub_error (GRUB_ERR_BUG, "put out of the packet range.");
  28. return GRUB_ERR_NONE;
  29. }
  30. grub_err_t
  31. grub_netbuff_unput (struct grub_net_buff *nb, grub_size_t len)
  32. {
  33. nb->tail -= len;
  34. if (nb->tail < nb->head)
  35. return grub_error (GRUB_ERR_BUG,
  36. "unput out of the packet range.");
  37. return GRUB_ERR_NONE;
  38. }
  39. grub_err_t
  40. grub_netbuff_push (struct grub_net_buff *nb, grub_size_t len)
  41. {
  42. nb->data -= len;
  43. if (nb->data < nb->head)
  44. return grub_error (GRUB_ERR_BUG,
  45. "push out of the packet range.");
  46. return GRUB_ERR_NONE;
  47. }
  48. grub_err_t
  49. grub_netbuff_pull (struct grub_net_buff *nb, grub_size_t len)
  50. {
  51. nb->data += len;
  52. if (nb->data > nb->end)
  53. return grub_error (GRUB_ERR_BUG,
  54. "pull out of the packet range.");
  55. return GRUB_ERR_NONE;
  56. }
  57. grub_err_t
  58. grub_netbuff_reserve (struct grub_net_buff *nb, grub_size_t len)
  59. {
  60. nb->data += len;
  61. nb->tail += len;
  62. if ((nb->tail > nb->end) || (nb->data > nb->end))
  63. return grub_error (GRUB_ERR_BUG,
  64. "reserve out of the packet range.");
  65. return GRUB_ERR_NONE;
  66. }
  67. struct grub_net_buff *
  68. grub_netbuff_alloc (grub_size_t len)
  69. {
  70. struct grub_net_buff *nb;
  71. void *data;
  72. COMPILE_TIME_ASSERT (NETBUFF_ALIGN % sizeof (grub_properly_aligned_t) == 0);
  73. if (len < NETBUFFMINLEN)
  74. len = NETBUFFMINLEN;
  75. len = ALIGN_UP (len, NETBUFF_ALIGN);
  76. #ifdef GRUB_MACHINE_EMU
  77. data = grub_malloc (len + sizeof (*nb));
  78. #else
  79. data = grub_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
  80. #endif
  81. if (!data)
  82. return NULL;
  83. nb = (struct grub_net_buff *) ((grub_properly_aligned_t *) data
  84. + len / sizeof (grub_properly_aligned_t));
  85. nb->head = nb->data = nb->tail = data;
  86. nb->end = (grub_uint8_t *) nb;
  87. return nb;
  88. }
  89. struct grub_net_buff *
  90. grub_netbuff_make_pkt (grub_size_t len)
  91. {
  92. struct grub_net_buff *nb;
  93. grub_err_t err;
  94. nb = grub_netbuff_alloc (len + 512);
  95. if (!nb)
  96. return NULL;
  97. err = grub_netbuff_reserve (nb, len + 512);
  98. if (err)
  99. goto fail;
  100. err = grub_netbuff_push (nb, len);
  101. if (err)
  102. goto fail;
  103. return nb;
  104. fail:
  105. grub_netbuff_free (nb);
  106. return NULL;
  107. }
  108. void
  109. grub_netbuff_free (struct grub_net_buff *nb)
  110. {
  111. if (!nb)
  112. return;
  113. grub_free (nb->head);
  114. }
  115. grub_err_t
  116. grub_netbuff_clear (struct grub_net_buff *nb)
  117. {
  118. nb->data = nb->tail = nb->head;
  119. return GRUB_ERR_NONE;
  120. }