netbuff.c 3.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2010 Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/err.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/net/netbuff.h>

24 25
grub_err_t
grub_netbuff_put (struct grub_net_buff *nb, grub_size_t len)
26
{
27 28
  nb->tail += len;
  if (nb->tail > nb->end)
29
    return grub_error (GRUB_ERR_BUG, "put out of the packet range.");
30
  return GRUB_ERR_NONE;
31 32
}

33 34
grub_err_t
grub_netbuff_unput (struct grub_net_buff *nb, grub_size_t len)
35
{
36 37
  nb->tail -= len;
  if (nb->tail < nb->head)
38
    return grub_error (GRUB_ERR_BUG,
39 40
		       "unput out of the packet range.");
  return GRUB_ERR_NONE;
41 42
}

43 44
grub_err_t
grub_netbuff_push (struct grub_net_buff *nb, grub_size_t len)
45
{
46 47
  nb->data -= len;
  if (nb->data < nb->head)
48
    return grub_error (GRUB_ERR_BUG,
49 50
		       "push out of the packet range.");
  return GRUB_ERR_NONE;
51 52
}

53 54
grub_err_t
grub_netbuff_pull (struct grub_net_buff *nb, grub_size_t len)
55
{
56 57
  nb->data += len;
  if (nb->data > nb->end)
58
    return grub_error (GRUB_ERR_BUG,
59 60
		       "pull out of the packet range.");
  return GRUB_ERR_NONE;
61 62
}

63 64
grub_err_t
grub_netbuff_reserve (struct grub_net_buff *nb, grub_size_t len)
65
{
66 67 68
  nb->data += len;
  nb->tail += len;
  if ((nb->tail > nb->end) || (nb->data > nb->end))
69
    return grub_error (GRUB_ERR_BUG,
70 71
		       "reserve out of the packet range.");
  return GRUB_ERR_NONE;
72 73
}

74 75
struct grub_net_buff *
grub_netbuff_alloc (grub_size_t len)
76
{
77 78 79
  struct grub_net_buff *nb;
  void *data;

80 81
  COMPILE_TIME_ASSERT (NETBUFF_ALIGN % sizeof (grub_properly_aligned_t) == 0);

82
  if (len < NETBUFFMINLEN)
83
    len = NETBUFFMINLEN;
84 85

  len = ALIGN_UP (len, NETBUFF_ALIGN);
86 87 88
#ifdef GRUB_MACHINE_EMU
  data = grub_malloc (len + sizeof (*nb));
#else
89
  data = grub_memalign (NETBUFF_ALIGN, len + sizeof (*nb));
90
#endif
91 92
  if (!data)
    return NULL;
93 94
  nb = (struct grub_net_buff *) ((grub_properly_aligned_t *) data
				 + len / sizeof (grub_properly_aligned_t));
95
  nb->head = nb->data = nb->tail = data;
96
  nb->end = (grub_uint8_t *) nb;
97
  return nb;
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
struct grub_net_buff *
grub_netbuff_make_pkt (grub_size_t len)
{
  struct grub_net_buff *nb;
  grub_err_t err;
  nb = grub_netbuff_alloc (len + 512);
  if (!nb)
    return NULL;
  err = grub_netbuff_reserve (nb, len + 512);
  if (err)
    goto fail;
  err = grub_netbuff_push (nb, len);
  if (err)
    goto fail;
  return nb;
 fail:
  grub_netbuff_free (nb);
  return NULL;
}

120
void
121
grub_netbuff_free (struct grub_net_buff *nb)
122
{
123 124
  if (!nb)
    return;
125
  grub_free (nb->head);
126 127
}

128 129
grub_err_t
grub_netbuff_clear (struct grub_net_buff *nb)
130
{
131
  nb->data = nb->tail = nb->head;
132
  return GRUB_ERR_NONE;
133
}